module.c 25 KB

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