module.c 22 KB

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