module.c 26 KB

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