module.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910
  1. /* Kernel dynamically loadable module help for PARISC.
  2. *
  3. * The best reference for this stuff is probably the Processor-
  4. * Specific ELF Supplement for PA-RISC:
  5. * http://ftp.parisc-linux.org/docs/arch/elf-pa-hp.pdf
  6. *
  7. * Linux/PA-RISC Project (http://www.parisc-linux.org/)
  8. * Copyright (C) 2003 Randolph Chung <tausq at debian . org>
  9. * Copyright (C) 2008 Helge Deller <deller@gmx.de>
  10. *
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  25. *
  26. *
  27. * Notes:
  28. * - PLT stub handling
  29. * On 32bit (and sometimes 64bit) and with big kernel modules like xfs or
  30. * ipv6 the relocation types R_PARISC_PCREL17F and R_PARISC_PCREL22F may
  31. * fail to reach their PLT stub if we only create one big stub array for
  32. * all sections at the beginning of the core or init section.
  33. * Instead we now insert individual PLT stub entries directly in front of
  34. * of the code sections where the stubs are actually called.
  35. * This reduces the distance between the PCREL location and the stub entry
  36. * so that the relocations can be fulfilled.
  37. * While calculating the final layout of the kernel module in memory, the
  38. * kernel module loader calls arch_mod_section_prepend() to request the
  39. * to be reserved amount of memory in front of each individual section.
  40. *
  41. * - SEGREL32 handling
  42. * We are not doing SEGREL32 handling correctly. According to the ABI, we
  43. * should do a value offset, like this:
  44. * if (in_init(me, (void *)val))
  45. * val -= (uint32_t)me->module_init;
  46. * else
  47. * val -= (uint32_t)me->module_core;
  48. * However, SEGREL32 is used only for PARISC unwind entries, and we want
  49. * those entries to have an absolute address, and not just an offset.
  50. *
  51. * The unwind table mechanism has the ability to specify an offset for
  52. * the unwind table; however, because we split off the init functions into
  53. * a different piece of memory, it is not possible to do this using a
  54. * single offset. Instead, we use the above hack for now.
  55. */
  56. #include <linux/moduleloader.h>
  57. #include <linux/elf.h>
  58. #include <linux/vmalloc.h>
  59. #include <linux/fs.h>
  60. #include <linux/string.h>
  61. #include <linux/kernel.h>
  62. #include <linux/bug.h>
  63. #include <asm/unwind.h>
  64. #if 0
  65. #define DEBUGP printk
  66. #else
  67. #define DEBUGP(fmt...)
  68. #endif
  69. #define RELOC_REACHABLE(val, bits) \
  70. (( ( !((val) & (1<<((bits)-1))) && ((val)>>(bits)) != 0 ) || \
  71. ( ((val) & (1<<((bits)-1))) && ((val)>>(bits)) != (((__typeof__(val))(~0))>>((bits)+2)))) ? \
  72. 0 : 1)
  73. #define CHECK_RELOC(val, bits) \
  74. if (!RELOC_REACHABLE(val, bits)) { \
  75. printk(KERN_ERR "module %s relocation of symbol %s is out of range (0x%lx in %d bits)\n", \
  76. me->name, strtab + sym->st_name, (unsigned long)val, bits); \
  77. return -ENOEXEC; \
  78. }
  79. /* Maximum number of GOT entries. We use a long displacement ldd from
  80. * the bottom of the table, which has a maximum signed displacement of
  81. * 0x3fff; however, since we're only going forward, this becomes
  82. * 0x1fff, and thus, since each GOT entry is 8 bytes long we can have
  83. * at most 1023 entries */
  84. #define MAX_GOTS 1023
  85. /* three functions to determine where in the module core
  86. * or init pieces the location is */
  87. static inline int in_init(struct module *me, void *loc)
  88. {
  89. return (loc >= me->module_init &&
  90. loc <= (me->module_init + me->init_size));
  91. }
  92. static inline int in_core(struct module *me, void *loc)
  93. {
  94. return (loc >= me->module_core &&
  95. loc <= (me->module_core + me->core_size));
  96. }
  97. static inline int in_local(struct module *me, void *loc)
  98. {
  99. return in_init(me, loc) || in_core(me, loc);
  100. }
  101. #ifndef CONFIG_64BIT
  102. struct got_entry {
  103. Elf32_Addr addr;
  104. };
  105. struct stub_entry {
  106. Elf32_Word insns[2]; /* each stub entry has two insns */
  107. };
  108. #else
  109. struct got_entry {
  110. Elf64_Addr addr;
  111. };
  112. struct stub_entry {
  113. Elf64_Word insns[4]; /* each stub entry has four insns */
  114. };
  115. #endif
  116. /* Field selection types defined by hppa */
  117. #define rnd(x) (((x)+0x1000)&~0x1fff)
  118. /* fsel: full 32 bits */
  119. #define fsel(v,a) ((v)+(a))
  120. /* lsel: select left 21 bits */
  121. #define lsel(v,a) (((v)+(a))>>11)
  122. /* rsel: select right 11 bits */
  123. #define rsel(v,a) (((v)+(a))&0x7ff)
  124. /* lrsel with rounding of addend to nearest 8k */
  125. #define lrsel(v,a) (((v)+rnd(a))>>11)
  126. /* rrsel with rounding of addend to nearest 8k */
  127. #define rrsel(v,a) ((((v)+rnd(a))&0x7ff)+((a)-rnd(a)))
  128. #define mask(x,sz) ((x) & ~((1<<(sz))-1))
  129. /* The reassemble_* functions prepare an immediate value for
  130. insertion into an opcode. pa-risc uses all sorts of weird bitfields
  131. in the instruction to hold the value. */
  132. static inline int reassemble_14(int as14)
  133. {
  134. return (((as14 & 0x1fff) << 1) |
  135. ((as14 & 0x2000) >> 13));
  136. }
  137. static inline int reassemble_17(int as17)
  138. {
  139. return (((as17 & 0x10000) >> 16) |
  140. ((as17 & 0x0f800) << 5) |
  141. ((as17 & 0x00400) >> 8) |
  142. ((as17 & 0x003ff) << 3));
  143. }
  144. static inline int reassemble_21(int as21)
  145. {
  146. return (((as21 & 0x100000) >> 20) |
  147. ((as21 & 0x0ffe00) >> 8) |
  148. ((as21 & 0x000180) << 7) |
  149. ((as21 & 0x00007c) << 14) |
  150. ((as21 & 0x000003) << 12));
  151. }
  152. static inline int reassemble_22(int as22)
  153. {
  154. return (((as22 & 0x200000) >> 21) |
  155. ((as22 & 0x1f0000) << 5) |
  156. ((as22 & 0x00f800) << 5) |
  157. ((as22 & 0x000400) >> 8) |
  158. ((as22 & 0x0003ff) << 3));
  159. }
  160. void *module_alloc(unsigned long size)
  161. {
  162. if (size == 0)
  163. return NULL;
  164. return vmalloc(size);
  165. }
  166. #ifndef CONFIG_64BIT
  167. static inline unsigned long count_gots(const Elf_Rela *rela, unsigned long n)
  168. {
  169. return 0;
  170. }
  171. static inline unsigned long count_fdescs(const Elf_Rela *rela, unsigned long n)
  172. {
  173. return 0;
  174. }
  175. static inline unsigned long count_stubs(const Elf_Rela *rela, unsigned long n)
  176. {
  177. unsigned long cnt = 0;
  178. for (; n > 0; n--, rela++)
  179. {
  180. switch (ELF32_R_TYPE(rela->r_info)) {
  181. case R_PARISC_PCREL17F:
  182. case R_PARISC_PCREL22F:
  183. cnt++;
  184. }
  185. }
  186. return cnt;
  187. }
  188. #else
  189. static inline unsigned long count_gots(const Elf_Rela *rela, unsigned long n)
  190. {
  191. unsigned long cnt = 0;
  192. for (; n > 0; n--, rela++)
  193. {
  194. switch (ELF64_R_TYPE(rela->r_info)) {
  195. case R_PARISC_LTOFF21L:
  196. case R_PARISC_LTOFF14R:
  197. case R_PARISC_PCREL22F:
  198. cnt++;
  199. }
  200. }
  201. return cnt;
  202. }
  203. static inline unsigned long count_fdescs(const Elf_Rela *rela, unsigned long n)
  204. {
  205. unsigned long cnt = 0;
  206. for (; n > 0; n--, rela++)
  207. {
  208. switch (ELF64_R_TYPE(rela->r_info)) {
  209. case R_PARISC_FPTR64:
  210. cnt++;
  211. }
  212. }
  213. return cnt;
  214. }
  215. static inline unsigned long count_stubs(const Elf_Rela *rela, unsigned long n)
  216. {
  217. unsigned long cnt = 0;
  218. for (; n > 0; n--, rela++)
  219. {
  220. switch (ELF64_R_TYPE(rela->r_info)) {
  221. case R_PARISC_PCREL22F:
  222. cnt++;
  223. }
  224. }
  225. return cnt;
  226. }
  227. #endif
  228. /* Free memory returned from module_alloc */
  229. void module_free(struct module *mod, void *module_region)
  230. {
  231. kfree(mod->arch.section);
  232. mod->arch.section = NULL;
  233. vfree(module_region);
  234. }
  235. /* Additional bytes needed in front of individual sections */
  236. unsigned int arch_mod_section_prepend(struct module *mod,
  237. unsigned int section)
  238. {
  239. /* size needed for all stubs of this section (including
  240. * one additional for correct alignment of the stubs) */
  241. return (mod->arch.section[section].stub_entries + 1)
  242. * sizeof(struct stub_entry);
  243. }
  244. #define CONST
  245. int module_frob_arch_sections(CONST Elf_Ehdr *hdr,
  246. CONST Elf_Shdr *sechdrs,
  247. CONST char *secstrings,
  248. struct module *me)
  249. {
  250. unsigned long gots = 0, fdescs = 0, len;
  251. unsigned int i;
  252. len = hdr->e_shnum * sizeof(me->arch.section[0]);
  253. me->arch.section = kzalloc(len, GFP_KERNEL);
  254. if (!me->arch.section)
  255. return -ENOMEM;
  256. for (i = 1; i < hdr->e_shnum; i++) {
  257. const Elf_Rela *rels = (void *)sechdrs[i].sh_addr;
  258. unsigned long nrels = sechdrs[i].sh_size / sizeof(*rels);
  259. unsigned int count, s;
  260. if (strncmp(secstrings + sechdrs[i].sh_name,
  261. ".PARISC.unwind", 14) == 0)
  262. me->arch.unwind_section = i;
  263. if (sechdrs[i].sh_type != SHT_RELA)
  264. continue;
  265. /* some of these are not relevant for 32-bit/64-bit
  266. * we leave them here to make the code common. the
  267. * compiler will do its thing and optimize out the
  268. * stuff we don't need
  269. */
  270. gots += count_gots(rels, nrels);
  271. fdescs += count_fdescs(rels, nrels);
  272. /* XXX: By sorting the relocs and finding duplicate entries
  273. * we could reduce the number of necessary stubs and save
  274. * some memory. */
  275. count = count_stubs(rels, nrels);
  276. if (!count)
  277. continue;
  278. /* so we need relocation stubs. reserve necessary memory. */
  279. /* sh_info gives the section for which we need to add stubs. */
  280. s = sechdrs[i].sh_info;
  281. /* each code section should only have one relocation section */
  282. WARN_ON(me->arch.section[s].stub_entries);
  283. /* store number of stubs we need for this section */
  284. me->arch.section[s].stub_entries += count;
  285. }
  286. /* align things a bit */
  287. me->core_size = ALIGN(me->core_size, 16);
  288. me->arch.got_offset = me->core_size;
  289. me->core_size += gots * sizeof(struct got_entry);
  290. me->core_size = ALIGN(me->core_size, 16);
  291. me->arch.fdesc_offset = me->core_size;
  292. me->core_size += fdescs * sizeof(Elf_Fdesc);
  293. me->arch.got_max = gots;
  294. me->arch.fdesc_max = fdescs;
  295. return 0;
  296. }
  297. #ifdef CONFIG_64BIT
  298. static Elf64_Word get_got(struct module *me, unsigned long value, long addend)
  299. {
  300. unsigned int i;
  301. struct got_entry *got;
  302. value += addend;
  303. BUG_ON(value == 0);
  304. got = me->module_core + me->arch.got_offset;
  305. for (i = 0; got[i].addr; i++)
  306. if (got[i].addr == value)
  307. goto out;
  308. BUG_ON(++me->arch.got_count > me->arch.got_max);
  309. got[i].addr = value;
  310. out:
  311. DEBUGP("GOT ENTRY %d[%x] val %lx\n", i, i*sizeof(struct got_entry),
  312. value);
  313. return i * sizeof(struct got_entry);
  314. }
  315. #endif /* CONFIG_64BIT */
  316. #ifdef CONFIG_64BIT
  317. static Elf_Addr get_fdesc(struct module *me, unsigned long value)
  318. {
  319. Elf_Fdesc *fdesc = me->module_core + me->arch.fdesc_offset;
  320. if (!value) {
  321. printk(KERN_ERR "%s: zero OPD requested!\n", me->name);
  322. return 0;
  323. }
  324. /* Look for existing fdesc entry. */
  325. while (fdesc->addr) {
  326. if (fdesc->addr == value)
  327. return (Elf_Addr)fdesc;
  328. fdesc++;
  329. }
  330. BUG_ON(++me->arch.fdesc_count > me->arch.fdesc_max);
  331. /* Create new one */
  332. fdesc->addr = value;
  333. fdesc->gp = (Elf_Addr)me->module_core + me->arch.got_offset;
  334. return (Elf_Addr)fdesc;
  335. }
  336. #endif /* CONFIG_64BIT */
  337. enum elf_stub_type {
  338. ELF_STUB_GOT,
  339. ELF_STUB_MILLI,
  340. ELF_STUB_DIRECT,
  341. };
  342. static Elf_Addr get_stub(struct module *me, unsigned long value, long addend,
  343. enum elf_stub_type stub_type, Elf_Addr loc0, unsigned int targetsec)
  344. {
  345. struct stub_entry *stub;
  346. /* initialize stub_offset to point in front of the section */
  347. if (!me->arch.section[targetsec].stub_offset) {
  348. loc0 -= (me->arch.section[targetsec].stub_entries + 1) *
  349. sizeof(struct stub_entry);
  350. /* get correct alignment for the stubs */
  351. loc0 = ALIGN(loc0, sizeof(struct stub_entry));
  352. me->arch.section[targetsec].stub_offset = loc0;
  353. }
  354. /* get address of stub entry */
  355. stub = (void *) me->arch.section[targetsec].stub_offset;
  356. me->arch.section[targetsec].stub_offset += sizeof(struct stub_entry);
  357. /* do not write outside available stub area */
  358. BUG_ON(0 == me->arch.section[targetsec].stub_entries--);
  359. #ifndef CONFIG_64BIT
  360. /* for 32-bit the stub looks like this:
  361. * ldil L'XXX,%r1
  362. * be,n R'XXX(%sr4,%r1)
  363. */
  364. //value = *(unsigned long *)((value + addend) & ~3); /* why? */
  365. stub->insns[0] = 0x20200000; /* ldil L'XXX,%r1 */
  366. stub->insns[1] = 0xe0202002; /* be,n R'XXX(%sr4,%r1) */
  367. stub->insns[0] |= reassemble_21(lrsel(value, addend));
  368. stub->insns[1] |= reassemble_17(rrsel(value, addend) / 4);
  369. #else
  370. /* for 64-bit we have three kinds of stubs:
  371. * for normal function calls:
  372. * ldd 0(%dp),%dp
  373. * ldd 10(%dp), %r1
  374. * bve (%r1)
  375. * ldd 18(%dp), %dp
  376. *
  377. * for millicode:
  378. * ldil 0, %r1
  379. * ldo 0(%r1), %r1
  380. * ldd 10(%r1), %r1
  381. * bve,n (%r1)
  382. *
  383. * for direct branches (jumps between different section of the
  384. * same module):
  385. * ldil 0, %r1
  386. * ldo 0(%r1), %r1
  387. * bve,n (%r1)
  388. */
  389. switch (stub_type) {
  390. case ELF_STUB_GOT:
  391. stub->insns[0] = 0x537b0000; /* ldd 0(%dp),%dp */
  392. stub->insns[1] = 0x53610020; /* ldd 10(%dp),%r1 */
  393. stub->insns[2] = 0xe820d000; /* bve (%r1) */
  394. stub->insns[3] = 0x537b0030; /* ldd 18(%dp),%dp */
  395. stub->insns[0] |= reassemble_14(get_got(me, value, addend) & 0x3fff);
  396. break;
  397. case ELF_STUB_MILLI:
  398. stub->insns[0] = 0x20200000; /* ldil 0,%r1 */
  399. stub->insns[1] = 0x34210000; /* ldo 0(%r1), %r1 */
  400. stub->insns[2] = 0x50210020; /* ldd 10(%r1),%r1 */
  401. stub->insns[3] = 0xe820d002; /* bve,n (%r1) */
  402. stub->insns[0] |= reassemble_21(lrsel(value, addend));
  403. stub->insns[1] |= reassemble_14(rrsel(value, addend));
  404. break;
  405. case ELF_STUB_DIRECT:
  406. stub->insns[0] = 0x20200000; /* ldil 0,%r1 */
  407. stub->insns[1] = 0x34210000; /* ldo 0(%r1), %r1 */
  408. stub->insns[2] = 0xe820d002; /* bve,n (%r1) */
  409. stub->insns[0] |= reassemble_21(lrsel(value, addend));
  410. stub->insns[1] |= reassemble_14(rrsel(value, addend));
  411. break;
  412. }
  413. #endif
  414. return (Elf_Addr)stub;
  415. }
  416. int apply_relocate(Elf_Shdr *sechdrs,
  417. const char *strtab,
  418. unsigned int symindex,
  419. unsigned int relsec,
  420. struct module *me)
  421. {
  422. /* parisc should not need this ... */
  423. printk(KERN_ERR "module %s: RELOCATION unsupported\n",
  424. me->name);
  425. return -ENOEXEC;
  426. }
  427. #ifndef CONFIG_64BIT
  428. int apply_relocate_add(Elf_Shdr *sechdrs,
  429. const char *strtab,
  430. unsigned int symindex,
  431. unsigned int relsec,
  432. struct module *me)
  433. {
  434. int i;
  435. Elf32_Rela *rel = (void *)sechdrs[relsec].sh_addr;
  436. Elf32_Sym *sym;
  437. Elf32_Word *loc;
  438. Elf32_Addr val;
  439. Elf32_Sword addend;
  440. Elf32_Addr dot;
  441. Elf_Addr loc0;
  442. unsigned int targetsec = sechdrs[relsec].sh_info;
  443. //unsigned long dp = (unsigned long)$global$;
  444. register unsigned long dp asm ("r27");
  445. DEBUGP("Applying relocate section %u to %u\n", relsec,
  446. targetsec);
  447. for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
  448. /* This is where to make the change */
  449. loc = (void *)sechdrs[targetsec].sh_addr
  450. + rel[i].r_offset;
  451. /* This is the start of the target section */
  452. loc0 = sechdrs[targetsec].sh_addr;
  453. /* This is the symbol it is referring to */
  454. sym = (Elf32_Sym *)sechdrs[symindex].sh_addr
  455. + ELF32_R_SYM(rel[i].r_info);
  456. if (!sym->st_value) {
  457. printk(KERN_WARNING "%s: Unknown symbol %s\n",
  458. me->name, strtab + sym->st_name);
  459. return -ENOENT;
  460. }
  461. //dot = (sechdrs[relsec].sh_addr + rel->r_offset) & ~0x03;
  462. dot = (Elf32_Addr)loc & ~0x03;
  463. val = sym->st_value;
  464. addend = rel[i].r_addend;
  465. #if 0
  466. #define r(t) ELF32_R_TYPE(rel[i].r_info)==t ? #t :
  467. DEBUGP("Symbol %s loc 0x%x val 0x%x addend 0x%x: %s\n",
  468. strtab + sym->st_name,
  469. (uint32_t)loc, val, addend,
  470. r(R_PARISC_PLABEL32)
  471. r(R_PARISC_DIR32)
  472. r(R_PARISC_DIR21L)
  473. r(R_PARISC_DIR14R)
  474. r(R_PARISC_SEGREL32)
  475. r(R_PARISC_DPREL21L)
  476. r(R_PARISC_DPREL14R)
  477. r(R_PARISC_PCREL17F)
  478. r(R_PARISC_PCREL22F)
  479. "UNKNOWN");
  480. #undef r
  481. #endif
  482. switch (ELF32_R_TYPE(rel[i].r_info)) {
  483. case R_PARISC_PLABEL32:
  484. /* 32-bit function address */
  485. /* no function descriptors... */
  486. *loc = fsel(val, addend);
  487. break;
  488. case R_PARISC_DIR32:
  489. /* direct 32-bit ref */
  490. *loc = fsel(val, addend);
  491. break;
  492. case R_PARISC_DIR21L:
  493. /* left 21 bits of effective address */
  494. val = lrsel(val, addend);
  495. *loc = mask(*loc, 21) | reassemble_21(val);
  496. break;
  497. case R_PARISC_DIR14R:
  498. /* right 14 bits of effective address */
  499. val = rrsel(val, addend);
  500. *loc = mask(*loc, 14) | reassemble_14(val);
  501. break;
  502. case R_PARISC_SEGREL32:
  503. /* 32-bit segment relative address */
  504. /* See note about special handling of SEGREL32 at
  505. * the beginning of this file.
  506. */
  507. *loc = fsel(val, addend);
  508. break;
  509. case R_PARISC_DPREL21L:
  510. /* left 21 bit of relative address */
  511. val = lrsel(val - dp, addend);
  512. *loc = mask(*loc, 21) | reassemble_21(val);
  513. break;
  514. case R_PARISC_DPREL14R:
  515. /* right 14 bit of relative address */
  516. val = rrsel(val - dp, addend);
  517. *loc = mask(*loc, 14) | reassemble_14(val);
  518. break;
  519. case R_PARISC_PCREL17F:
  520. /* 17-bit PC relative address */
  521. /* calculate direct call offset */
  522. val += addend;
  523. val = (val - dot - 8)/4;
  524. if (!RELOC_REACHABLE(val, 17)) {
  525. /* direct distance too far, create
  526. * stub entry instead */
  527. val = get_stub(me, sym->st_value, addend,
  528. ELF_STUB_DIRECT, loc0, targetsec);
  529. val = (val - dot - 8)/4;
  530. CHECK_RELOC(val, 17);
  531. }
  532. *loc = (*loc & ~0x1f1ffd) | reassemble_17(val);
  533. break;
  534. case R_PARISC_PCREL22F:
  535. /* 22-bit PC relative address; only defined for pa20 */
  536. /* calculate direct call offset */
  537. val += addend;
  538. val = (val - dot - 8)/4;
  539. if (!RELOC_REACHABLE(val, 22)) {
  540. /* direct distance too far, create
  541. * stub entry instead */
  542. val = get_stub(me, sym->st_value, addend,
  543. ELF_STUB_DIRECT, loc0, targetsec);
  544. val = (val - dot - 8)/4;
  545. CHECK_RELOC(val, 22);
  546. }
  547. *loc = (*loc & ~0x3ff1ffd) | reassemble_22(val);
  548. break;
  549. default:
  550. printk(KERN_ERR "module %s: Unknown relocation: %u\n",
  551. me->name, ELF32_R_TYPE(rel[i].r_info));
  552. return -ENOEXEC;
  553. }
  554. }
  555. return 0;
  556. }
  557. #else
  558. int apply_relocate_add(Elf_Shdr *sechdrs,
  559. const char *strtab,
  560. unsigned int symindex,
  561. unsigned int relsec,
  562. struct module *me)
  563. {
  564. int i;
  565. Elf64_Rela *rel = (void *)sechdrs[relsec].sh_addr;
  566. Elf64_Sym *sym;
  567. Elf64_Word *loc;
  568. Elf64_Xword *loc64;
  569. Elf64_Addr val;
  570. Elf64_Sxword addend;
  571. Elf64_Addr dot;
  572. Elf_Addr loc0;
  573. unsigned int targetsec = sechdrs[relsec].sh_info;
  574. DEBUGP("Applying relocate section %u to %u\n", relsec,
  575. targetsec);
  576. for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
  577. /* This is where to make the change */
  578. loc = (void *)sechdrs[targetsec].sh_addr
  579. + rel[i].r_offset;
  580. /* This is the start of the target section */
  581. loc0 = sechdrs[targetsec].sh_addr;
  582. /* This is the symbol it is referring to */
  583. sym = (Elf64_Sym *)sechdrs[symindex].sh_addr
  584. + ELF64_R_SYM(rel[i].r_info);
  585. if (!sym->st_value) {
  586. printk(KERN_WARNING "%s: Unknown symbol %s\n",
  587. me->name, strtab + sym->st_name);
  588. return -ENOENT;
  589. }
  590. //dot = (sechdrs[relsec].sh_addr + rel->r_offset) & ~0x03;
  591. dot = (Elf64_Addr)loc & ~0x03;
  592. loc64 = (Elf64_Xword *)loc;
  593. val = sym->st_value;
  594. addend = rel[i].r_addend;
  595. #if 0
  596. #define r(t) ELF64_R_TYPE(rel[i].r_info)==t ? #t :
  597. printk("Symbol %s loc %p val 0x%Lx addend 0x%Lx: %s\n",
  598. strtab + sym->st_name,
  599. loc, val, addend,
  600. r(R_PARISC_LTOFF14R)
  601. r(R_PARISC_LTOFF21L)
  602. r(R_PARISC_PCREL22F)
  603. r(R_PARISC_DIR64)
  604. r(R_PARISC_SEGREL32)
  605. r(R_PARISC_FPTR64)
  606. "UNKNOWN");
  607. #undef r
  608. #endif
  609. switch (ELF64_R_TYPE(rel[i].r_info)) {
  610. case R_PARISC_LTOFF21L:
  611. /* LT-relative; left 21 bits */
  612. val = get_got(me, val, addend);
  613. DEBUGP("LTOFF21L Symbol %s loc %p val %lx\n",
  614. strtab + sym->st_name,
  615. loc, val);
  616. val = lrsel(val, 0);
  617. *loc = mask(*loc, 21) | reassemble_21(val);
  618. break;
  619. case R_PARISC_LTOFF14R:
  620. /* L(ltoff(val+addend)) */
  621. /* LT-relative; right 14 bits */
  622. val = get_got(me, val, addend);
  623. val = rrsel(val, 0);
  624. DEBUGP("LTOFF14R Symbol %s loc %p val %lx\n",
  625. strtab + sym->st_name,
  626. loc, val);
  627. *loc = mask(*loc, 14) | reassemble_14(val);
  628. break;
  629. case R_PARISC_PCREL22F:
  630. /* PC-relative; 22 bits */
  631. DEBUGP("PCREL22F Symbol %s loc %p val %lx\n",
  632. strtab + sym->st_name,
  633. loc, val);
  634. val += addend;
  635. /* can we reach it locally? */
  636. if (in_local(me, (void *)val)) {
  637. /* this is the case where the symbol is local
  638. * to the module, but in a different section,
  639. * so stub the jump in case it's more than 22
  640. * bits away */
  641. val = (val - dot - 8)/4;
  642. if (!RELOC_REACHABLE(val, 22)) {
  643. /* direct distance too far, create
  644. * stub entry instead */
  645. val = get_stub(me, sym->st_value,
  646. addend, ELF_STUB_DIRECT,
  647. loc0, targetsec);
  648. } else {
  649. /* Ok, we can reach it directly. */
  650. val = sym->st_value;
  651. val += addend;
  652. }
  653. } else {
  654. val = sym->st_value;
  655. if (strncmp(strtab + sym->st_name, "$$", 2)
  656. == 0)
  657. val = get_stub(me, val, addend, ELF_STUB_MILLI,
  658. loc0, targetsec);
  659. else
  660. val = get_stub(me, val, addend, ELF_STUB_GOT,
  661. loc0, targetsec);
  662. }
  663. DEBUGP("STUB FOR %s loc %lx, val %lx+%lx at %lx\n",
  664. strtab + sym->st_name, loc, sym->st_value,
  665. addend, val);
  666. val = (val - dot - 8)/4;
  667. CHECK_RELOC(val, 22);
  668. *loc = (*loc & ~0x3ff1ffd) | reassemble_22(val);
  669. break;
  670. case R_PARISC_DIR64:
  671. /* 64-bit effective address */
  672. *loc64 = val + addend;
  673. break;
  674. case R_PARISC_SEGREL32:
  675. /* 32-bit segment relative address */
  676. /* See note about special handling of SEGREL32 at
  677. * the beginning of this file.
  678. */
  679. *loc = fsel(val, addend);
  680. break;
  681. case R_PARISC_FPTR64:
  682. /* 64-bit function address */
  683. if(in_local(me, (void *)(val + addend))) {
  684. *loc64 = get_fdesc(me, val+addend);
  685. DEBUGP("FDESC for %s at %p points to %lx\n",
  686. strtab + sym->st_name, *loc64,
  687. ((Elf_Fdesc *)*loc64)->addr);
  688. } else {
  689. /* if the symbol is not local to this
  690. * module then val+addend is a pointer
  691. * to the function descriptor */
  692. DEBUGP("Non local FPTR64 Symbol %s loc %p val %lx\n",
  693. strtab + sym->st_name,
  694. loc, val);
  695. *loc64 = val + addend;
  696. }
  697. break;
  698. default:
  699. printk(KERN_ERR "module %s: Unknown relocation: %Lu\n",
  700. me->name, ELF64_R_TYPE(rel[i].r_info));
  701. return -ENOEXEC;
  702. }
  703. }
  704. return 0;
  705. }
  706. #endif
  707. static void
  708. register_unwind_table(struct module *me,
  709. const Elf_Shdr *sechdrs)
  710. {
  711. unsigned char *table, *end;
  712. unsigned long gp;
  713. if (!me->arch.unwind_section)
  714. return;
  715. table = (unsigned char *)sechdrs[me->arch.unwind_section].sh_addr;
  716. end = table + sechdrs[me->arch.unwind_section].sh_size;
  717. gp = (Elf_Addr)me->module_core + me->arch.got_offset;
  718. DEBUGP("register_unwind_table(), sect = %d at 0x%p - 0x%p (gp=0x%lx)\n",
  719. me->arch.unwind_section, table, end, gp);
  720. me->arch.unwind = unwind_table_add(me->name, 0, gp, table, end);
  721. }
  722. static void
  723. deregister_unwind_table(struct module *me)
  724. {
  725. if (me->arch.unwind)
  726. unwind_table_remove(me->arch.unwind);
  727. }
  728. int module_finalize(const Elf_Ehdr *hdr,
  729. const Elf_Shdr *sechdrs,
  730. struct module *me)
  731. {
  732. int i;
  733. unsigned long nsyms;
  734. const char *strtab = NULL;
  735. Elf_Sym *newptr, *oldptr;
  736. Elf_Shdr *symhdr = NULL;
  737. #ifdef DEBUG
  738. Elf_Fdesc *entry;
  739. u32 *addr;
  740. entry = (Elf_Fdesc *)me->init;
  741. printk("FINALIZE, ->init FPTR is %p, GP %lx ADDR %lx\n", entry,
  742. entry->gp, entry->addr);
  743. addr = (u32 *)entry->addr;
  744. printk("INSNS: %x %x %x %x\n",
  745. addr[0], addr[1], addr[2], addr[3]);
  746. printk("got entries used %ld, gots max %ld\n"
  747. "fdescs used %ld, fdescs max %ld\n",
  748. me->arch.got_count, me->arch.got_max,
  749. me->arch.fdesc_count, me->arch.fdesc_max);
  750. #endif
  751. register_unwind_table(me, sechdrs);
  752. /* haven't filled in me->symtab yet, so have to find it
  753. * ourselves */
  754. for (i = 1; i < hdr->e_shnum; i++) {
  755. if(sechdrs[i].sh_type == SHT_SYMTAB
  756. && (sechdrs[i].sh_type & SHF_ALLOC)) {
  757. int strindex = sechdrs[i].sh_link;
  758. /* FIXME: AWFUL HACK
  759. * The cast is to drop the const from
  760. * the sechdrs pointer */
  761. symhdr = (Elf_Shdr *)&sechdrs[i];
  762. strtab = (char *)sechdrs[strindex].sh_addr;
  763. break;
  764. }
  765. }
  766. DEBUGP("module %s: strtab %p, symhdr %p\n",
  767. me->name, strtab, symhdr);
  768. if(me->arch.got_count > MAX_GOTS) {
  769. printk(KERN_ERR "%s: Global Offset Table overflow (used %ld, allowed %d)\n",
  770. me->name, me->arch.got_count, MAX_GOTS);
  771. return -EINVAL;
  772. }
  773. kfree(me->arch.section);
  774. me->arch.section = NULL;
  775. /* no symbol table */
  776. if(symhdr == NULL)
  777. return 0;
  778. oldptr = (void *)symhdr->sh_addr;
  779. newptr = oldptr + 1; /* we start counting at 1 */
  780. nsyms = symhdr->sh_size / sizeof(Elf_Sym);
  781. DEBUGP("OLD num_symtab %lu\n", nsyms);
  782. for (i = 1; i < nsyms; i++) {
  783. oldptr++; /* note, count starts at 1 so preincrement */
  784. if(strncmp(strtab + oldptr->st_name,
  785. ".L", 2) == 0)
  786. continue;
  787. if(newptr != oldptr)
  788. *newptr++ = *oldptr;
  789. else
  790. newptr++;
  791. }
  792. nsyms = newptr - (Elf_Sym *)symhdr->sh_addr;
  793. DEBUGP("NEW num_symtab %lu\n", nsyms);
  794. symhdr->sh_size = nsyms * sizeof(Elf_Sym);
  795. return module_bug_finalize(hdr, sechdrs, me);
  796. }
  797. void module_arch_cleanup(struct module *mod)
  798. {
  799. deregister_unwind_table(mod);
  800. module_bug_cleanup(mod);
  801. }