module.c 23 KB

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