module.c 23 KB

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