module.c 25 KB

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