module.c 26 KB

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