module.c 25 KB

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