module.c 25 KB

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