module.c 25 KB

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