module.c 23 KB

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