module_64.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704
  1. /* Kernel module help for PPC64.
  2. Copyright (C) 2001, 2003 Rusty Russell IBM Corporation.
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  14. */
  15. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  16. #include <linux/module.h>
  17. #include <linux/elf.h>
  18. #include <linux/moduleloader.h>
  19. #include <linux/err.h>
  20. #include <linux/vmalloc.h>
  21. #include <linux/ftrace.h>
  22. #include <linux/bug.h>
  23. #include <linux/uaccess.h>
  24. #include <asm/module.h>
  25. #include <asm/firmware.h>
  26. #include <asm/code-patching.h>
  27. #include <linux/sort.h>
  28. #include <asm/setup.h>
  29. /* FIXME: We don't do .init separately. To do this, we'd need to have
  30. a separate r2 value in the init and core section, and stub between
  31. them, too.
  32. Using a magic allocator which places modules within 32MB solves
  33. this, and makes other things simpler. Anton?
  34. --RR. */
  35. #if defined(_CALL_ELF) && _CALL_ELF == 2
  36. #define R2_STACK_OFFSET 24
  37. /* An address is simply the address of the function. */
  38. typedef unsigned long func_desc_t;
  39. static func_desc_t func_desc(unsigned long addr)
  40. {
  41. return addr;
  42. }
  43. static unsigned long func_addr(unsigned long addr)
  44. {
  45. return addr;
  46. }
  47. static unsigned long stub_func_addr(func_desc_t func)
  48. {
  49. return func;
  50. }
  51. /* PowerPC64 specific values for the Elf64_Sym st_other field. */
  52. #define STO_PPC64_LOCAL_BIT 5
  53. #define STO_PPC64_LOCAL_MASK (7 << STO_PPC64_LOCAL_BIT)
  54. #define PPC64_LOCAL_ENTRY_OFFSET(other) \
  55. (((1 << (((other) & STO_PPC64_LOCAL_MASK) >> STO_PPC64_LOCAL_BIT)) >> 2) << 2)
  56. static unsigned int local_entry_offset(const Elf64_Sym *sym)
  57. {
  58. /* sym->st_other indicates offset to local entry point
  59. * (otherwise it will assume r12 is the address of the start
  60. * of function and try to derive r2 from it). */
  61. return PPC64_LOCAL_ENTRY_OFFSET(sym->st_other);
  62. }
  63. #else
  64. #define R2_STACK_OFFSET 40
  65. /* An address is address of the OPD entry, which contains address of fn. */
  66. typedef struct ppc64_opd_entry func_desc_t;
  67. static func_desc_t func_desc(unsigned long addr)
  68. {
  69. return *(struct ppc64_opd_entry *)addr;
  70. }
  71. static unsigned long func_addr(unsigned long addr)
  72. {
  73. return func_desc(addr).funcaddr;
  74. }
  75. static unsigned long stub_func_addr(func_desc_t func)
  76. {
  77. return func.funcaddr;
  78. }
  79. static unsigned int local_entry_offset(const Elf64_Sym *sym)
  80. {
  81. return 0;
  82. }
  83. #endif
  84. /* Like PPC32, we need little trampolines to do > 24-bit jumps (into
  85. the kernel itself). But on PPC64, these need to be used for every
  86. jump, actually, to reset r2 (TOC+0x8000). */
  87. struct ppc64_stub_entry
  88. {
  89. /* 28 byte jump instruction sequence (7 instructions). We only
  90. * need 6 instructions on ABIv2 but we always allocate 7 so
  91. * so we don't have to modify the trampoline load instruction. */
  92. u32 jump[7];
  93. u32 unused;
  94. /* Data for the above code */
  95. func_desc_t funcdata;
  96. };
  97. /*
  98. * PPC64 uses 24 bit jumps, but we need to jump into other modules or
  99. * the kernel which may be further. So we jump to a stub.
  100. *
  101. * For ELFv1 we need to use this to set up the new r2 value (aka TOC
  102. * pointer). For ELFv2 it's the callee's responsibility to set up the
  103. * new r2, but for both we need to save the old r2.
  104. *
  105. * We could simply patch the new r2 value and function pointer into
  106. * the stub, but it's significantly shorter to put these values at the
  107. * end of the stub code, and patch the stub address (32-bits relative
  108. * to the TOC ptr, r2) into the stub.
  109. */
  110. static u32 ppc64_stub_insns[] = {
  111. 0x3d620000, /* addis r11,r2, <high> */
  112. 0x396b0000, /* addi r11,r11, <low> */
  113. /* Save current r2 value in magic place on the stack. */
  114. 0xf8410000|R2_STACK_OFFSET, /* std r2,R2_STACK_OFFSET(r1) */
  115. 0xe98b0020, /* ld r12,32(r11) */
  116. #if !defined(_CALL_ELF) || _CALL_ELF != 2
  117. /* Set up new r2 from function descriptor */
  118. 0xe84b0028, /* ld r2,40(r11) */
  119. #endif
  120. 0x7d8903a6, /* mtctr r12 */
  121. 0x4e800420 /* bctr */
  122. };
  123. #ifdef CONFIG_DYNAMIC_FTRACE
  124. static u32 ppc64_stub_mask[] = {
  125. 0xffff0000,
  126. 0xffff0000,
  127. 0xffffffff,
  128. 0xffffffff,
  129. #if !defined(_CALL_ELF) || _CALL_ELF != 2
  130. 0xffffffff,
  131. #endif
  132. 0xffffffff,
  133. 0xffffffff
  134. };
  135. bool is_module_trampoline(u32 *p)
  136. {
  137. unsigned int i;
  138. u32 insns[ARRAY_SIZE(ppc64_stub_insns)];
  139. BUILD_BUG_ON(sizeof(ppc64_stub_insns) != sizeof(ppc64_stub_mask));
  140. if (probe_kernel_read(insns, p, sizeof(insns)))
  141. return -EFAULT;
  142. for (i = 0; i < ARRAY_SIZE(ppc64_stub_insns); i++) {
  143. u32 insna = insns[i];
  144. u32 insnb = ppc64_stub_insns[i];
  145. u32 mask = ppc64_stub_mask[i];
  146. if ((insna & mask) != (insnb & mask))
  147. return false;
  148. }
  149. return true;
  150. }
  151. int module_trampoline_target(struct module *mod, u32 *trampoline,
  152. unsigned long *target)
  153. {
  154. u32 buf[2];
  155. u16 upper, lower;
  156. long offset;
  157. void *toc_entry;
  158. if (probe_kernel_read(buf, trampoline, sizeof(buf)))
  159. return -EFAULT;
  160. upper = buf[0] & 0xffff;
  161. lower = buf[1] & 0xffff;
  162. /* perform the addis/addi, both signed */
  163. offset = ((short)upper << 16) + (short)lower;
  164. /*
  165. * Now get the address this trampoline jumps to. This
  166. * is always 32 bytes into our trampoline stub.
  167. */
  168. toc_entry = (void *)mod->arch.toc + offset + 32;
  169. if (probe_kernel_read(target, toc_entry, sizeof(*target)))
  170. return -EFAULT;
  171. return 0;
  172. }
  173. #endif
  174. /* Count how many different 24-bit relocations (different symbol,
  175. different addend) */
  176. static unsigned int count_relocs(const Elf64_Rela *rela, unsigned int num)
  177. {
  178. unsigned int i, r_info, r_addend, _count_relocs;
  179. /* FIXME: Only count external ones --RR */
  180. _count_relocs = 0;
  181. r_info = 0;
  182. r_addend = 0;
  183. for (i = 0; i < num; i++)
  184. /* Only count 24-bit relocs, others don't need stubs */
  185. if (ELF64_R_TYPE(rela[i].r_info) == R_PPC_REL24 &&
  186. (r_info != ELF64_R_SYM(rela[i].r_info) ||
  187. r_addend != rela[i].r_addend)) {
  188. _count_relocs++;
  189. r_info = ELF64_R_SYM(rela[i].r_info);
  190. r_addend = rela[i].r_addend;
  191. }
  192. return _count_relocs;
  193. }
  194. static int relacmp(const void *_x, const void *_y)
  195. {
  196. const Elf64_Rela *x, *y;
  197. y = (Elf64_Rela *)_x;
  198. x = (Elf64_Rela *)_y;
  199. /* Compare the entire r_info (as opposed to ELF64_R_SYM(r_info) only) to
  200. * make the comparison cheaper/faster. It won't affect the sorting or
  201. * the counting algorithms' performance
  202. */
  203. if (x->r_info < y->r_info)
  204. return -1;
  205. else if (x->r_info > y->r_info)
  206. return 1;
  207. else if (x->r_addend < y->r_addend)
  208. return -1;
  209. else if (x->r_addend > y->r_addend)
  210. return 1;
  211. else
  212. return 0;
  213. }
  214. static void relaswap(void *_x, void *_y, int size)
  215. {
  216. uint64_t *x, *y, tmp;
  217. int i;
  218. y = (uint64_t *)_x;
  219. x = (uint64_t *)_y;
  220. for (i = 0; i < sizeof(Elf64_Rela) / sizeof(uint64_t); i++) {
  221. tmp = x[i];
  222. x[i] = y[i];
  223. y[i] = tmp;
  224. }
  225. }
  226. /* Get size of potential trampolines required. */
  227. static unsigned long get_stubs_size(const Elf64_Ehdr *hdr,
  228. const Elf64_Shdr *sechdrs)
  229. {
  230. /* One extra reloc so it's always 0-funcaddr terminated */
  231. unsigned long relocs = 1;
  232. unsigned i;
  233. /* Every relocated section... */
  234. for (i = 1; i < hdr->e_shnum; i++) {
  235. if (sechdrs[i].sh_type == SHT_RELA) {
  236. pr_debug("Found relocations in section %u\n", i);
  237. pr_debug("Ptr: %p. Number: %Lu\n",
  238. (void *)sechdrs[i].sh_addr,
  239. sechdrs[i].sh_size / sizeof(Elf64_Rela));
  240. /* Sort the relocation information based on a symbol and
  241. * addend key. This is a stable O(n*log n) complexity
  242. * alogrithm but it will reduce the complexity of
  243. * count_relocs() to linear complexity O(n)
  244. */
  245. sort((void *)sechdrs[i].sh_addr,
  246. sechdrs[i].sh_size / sizeof(Elf64_Rela),
  247. sizeof(Elf64_Rela), relacmp, relaswap);
  248. relocs += count_relocs((void *)sechdrs[i].sh_addr,
  249. sechdrs[i].sh_size
  250. / sizeof(Elf64_Rela));
  251. }
  252. }
  253. #ifdef CONFIG_DYNAMIC_FTRACE
  254. /* make the trampoline to the ftrace_caller */
  255. relocs++;
  256. #endif
  257. pr_debug("Looks like a total of %lu stubs, max\n", relocs);
  258. return relocs * sizeof(struct ppc64_stub_entry);
  259. }
  260. /* Still needed for ELFv2, for .TOC. */
  261. static void dedotify_versions(struct modversion_info *vers,
  262. unsigned long size)
  263. {
  264. struct modversion_info *end;
  265. for (end = (void *)vers + size; vers < end; vers++)
  266. if (vers->name[0] == '.') {
  267. memmove(vers->name, vers->name+1, strlen(vers->name));
  268. #ifdef ARCH_RELOCATES_KCRCTAB
  269. /* The TOC symbol has no CRC computed. To avoid CRC
  270. * check failing, we must force it to the expected
  271. * value (see CRC check in module.c).
  272. */
  273. if (!strcmp(vers->name, "TOC."))
  274. vers->crc = -(unsigned long)reloc_start;
  275. #endif
  276. }
  277. }
  278. /*
  279. * Undefined symbols which refer to .funcname, hack to funcname. Make .TOC.
  280. * seem to be defined (value set later).
  281. */
  282. static void dedotify(Elf64_Sym *syms, unsigned int numsyms, char *strtab)
  283. {
  284. unsigned int i;
  285. for (i = 1; i < numsyms; i++) {
  286. if (syms[i].st_shndx == SHN_UNDEF) {
  287. char *name = strtab + syms[i].st_name;
  288. if (name[0] == '.') {
  289. if (strcmp(name+1, "TOC.") == 0)
  290. syms[i].st_shndx = SHN_ABS;
  291. syms[i].st_name++;
  292. }
  293. }
  294. }
  295. }
  296. static Elf64_Sym *find_dot_toc(Elf64_Shdr *sechdrs,
  297. const char *strtab,
  298. unsigned int symindex)
  299. {
  300. unsigned int i, numsyms;
  301. Elf64_Sym *syms;
  302. syms = (Elf64_Sym *)sechdrs[symindex].sh_addr;
  303. numsyms = sechdrs[symindex].sh_size / sizeof(Elf64_Sym);
  304. for (i = 1; i < numsyms; i++) {
  305. if (syms[i].st_shndx == SHN_ABS
  306. && strcmp(strtab + syms[i].st_name, "TOC.") == 0)
  307. return &syms[i];
  308. }
  309. return NULL;
  310. }
  311. int module_frob_arch_sections(Elf64_Ehdr *hdr,
  312. Elf64_Shdr *sechdrs,
  313. char *secstrings,
  314. struct module *me)
  315. {
  316. unsigned int i;
  317. /* Find .toc and .stubs sections, symtab and strtab */
  318. for (i = 1; i < hdr->e_shnum; i++) {
  319. char *p;
  320. if (strcmp(secstrings + sechdrs[i].sh_name, ".stubs") == 0)
  321. me->arch.stubs_section = i;
  322. else if (strcmp(secstrings + sechdrs[i].sh_name, ".toc") == 0)
  323. me->arch.toc_section = i;
  324. else if (strcmp(secstrings+sechdrs[i].sh_name,"__versions")==0)
  325. dedotify_versions((void *)hdr + sechdrs[i].sh_offset,
  326. sechdrs[i].sh_size);
  327. /* We don't handle .init for the moment: rename to _init */
  328. while ((p = strstr(secstrings + sechdrs[i].sh_name, ".init")))
  329. p[0] = '_';
  330. if (sechdrs[i].sh_type == SHT_SYMTAB)
  331. dedotify((void *)hdr + sechdrs[i].sh_offset,
  332. sechdrs[i].sh_size / sizeof(Elf64_Sym),
  333. (void *)hdr
  334. + sechdrs[sechdrs[i].sh_link].sh_offset);
  335. }
  336. if (!me->arch.stubs_section) {
  337. pr_err("%s: doesn't contain .stubs.\n", me->name);
  338. return -ENOEXEC;
  339. }
  340. /* If we don't have a .toc, just use .stubs. We need to set r2
  341. to some reasonable value in case the module calls out to
  342. other functions via a stub, or if a function pointer escapes
  343. the module by some means. */
  344. if (!me->arch.toc_section)
  345. me->arch.toc_section = me->arch.stubs_section;
  346. /* Override the stubs size */
  347. sechdrs[me->arch.stubs_section].sh_size = get_stubs_size(hdr, sechdrs);
  348. return 0;
  349. }
  350. /* r2 is the TOC pointer: it actually points 0x8000 into the TOC (this
  351. gives the value maximum span in an instruction which uses a signed
  352. offset) */
  353. static inline unsigned long my_r2(Elf64_Shdr *sechdrs, struct module *me)
  354. {
  355. return sechdrs[me->arch.toc_section].sh_addr + 0x8000;
  356. }
  357. /* Both low and high 16 bits are added as SIGNED additions, so if low
  358. 16 bits has high bit set, high 16 bits must be adjusted. These
  359. macros do that (stolen from binutils). */
  360. #define PPC_LO(v) ((v) & 0xffff)
  361. #define PPC_HI(v) (((v) >> 16) & 0xffff)
  362. #define PPC_HA(v) PPC_HI ((v) + 0x8000)
  363. /* Patch stub to reference function and correct r2 value. */
  364. static inline int create_stub(Elf64_Shdr *sechdrs,
  365. struct ppc64_stub_entry *entry,
  366. unsigned long addr,
  367. struct module *me)
  368. {
  369. long reladdr;
  370. memcpy(entry->jump, ppc64_stub_insns, sizeof(ppc64_stub_insns));
  371. /* Stub uses address relative to r2. */
  372. reladdr = (unsigned long)entry - my_r2(sechdrs, me);
  373. if (reladdr > 0x7FFFFFFF || reladdr < -(0x80000000L)) {
  374. pr_err("%s: Address %p of stub out of range of %p.\n",
  375. me->name, (void *)reladdr, (void *)my_r2);
  376. return 0;
  377. }
  378. pr_debug("Stub %p get data from reladdr %li\n", entry, reladdr);
  379. entry->jump[0] |= PPC_HA(reladdr);
  380. entry->jump[1] |= PPC_LO(reladdr);
  381. entry->funcdata = func_desc(addr);
  382. return 1;
  383. }
  384. /* Create stub to jump to function described in this OPD/ptr: we need the
  385. stub to set up the TOC ptr (r2) for the function. */
  386. static unsigned long stub_for_addr(Elf64_Shdr *sechdrs,
  387. unsigned long addr,
  388. struct module *me)
  389. {
  390. struct ppc64_stub_entry *stubs;
  391. unsigned int i, num_stubs;
  392. num_stubs = sechdrs[me->arch.stubs_section].sh_size / sizeof(*stubs);
  393. /* Find this stub, or if that fails, the next avail. entry */
  394. stubs = (void *)sechdrs[me->arch.stubs_section].sh_addr;
  395. for (i = 0; stub_func_addr(stubs[i].funcdata); i++) {
  396. BUG_ON(i >= num_stubs);
  397. if (stub_func_addr(stubs[i].funcdata) == func_addr(addr))
  398. return (unsigned long)&stubs[i];
  399. }
  400. if (!create_stub(sechdrs, &stubs[i], addr, me))
  401. return 0;
  402. return (unsigned long)&stubs[i];
  403. }
  404. /* We expect a noop next: if it is, replace it with instruction to
  405. restore r2. */
  406. static int restore_r2(u32 *instruction, struct module *me)
  407. {
  408. if (*instruction != PPC_INST_NOP) {
  409. pr_err("%s: Expect noop after relocate, got %08x\n",
  410. me->name, *instruction);
  411. return 0;
  412. }
  413. /* ld r2,R2_STACK_OFFSET(r1) */
  414. *instruction = 0xe8410000 | R2_STACK_OFFSET;
  415. return 1;
  416. }
  417. int apply_relocate_add(Elf64_Shdr *sechdrs,
  418. const char *strtab,
  419. unsigned int symindex,
  420. unsigned int relsec,
  421. struct module *me)
  422. {
  423. unsigned int i;
  424. Elf64_Rela *rela = (void *)sechdrs[relsec].sh_addr;
  425. Elf64_Sym *sym;
  426. unsigned long *location;
  427. unsigned long value;
  428. pr_debug("Applying ADD relocate section %u to %u\n", relsec,
  429. sechdrs[relsec].sh_info);
  430. /* First time we're called, we can fix up .TOC. */
  431. if (!me->arch.toc_fixed) {
  432. sym = find_dot_toc(sechdrs, strtab, symindex);
  433. /* It's theoretically possible that a module doesn't want a
  434. * .TOC. so don't fail it just for that. */
  435. if (sym)
  436. sym->st_value = my_r2(sechdrs, me);
  437. me->arch.toc_fixed = true;
  438. }
  439. for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rela); i++) {
  440. /* This is where to make the change */
  441. location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
  442. + rela[i].r_offset;
  443. /* This is the symbol it is referring to */
  444. sym = (Elf64_Sym *)sechdrs[symindex].sh_addr
  445. + ELF64_R_SYM(rela[i].r_info);
  446. pr_debug("RELOC at %p: %li-type as %s (0x%lx) + %li\n",
  447. location, (long)ELF64_R_TYPE(rela[i].r_info),
  448. strtab + sym->st_name, (unsigned long)sym->st_value,
  449. (long)rela[i].r_addend);
  450. /* `Everything is relative'. */
  451. value = sym->st_value + rela[i].r_addend;
  452. switch (ELF64_R_TYPE(rela[i].r_info)) {
  453. case R_PPC64_ADDR32:
  454. /* Simply set it */
  455. *(u32 *)location = value;
  456. break;
  457. case R_PPC64_ADDR64:
  458. /* Simply set it */
  459. *(unsigned long *)location = value;
  460. break;
  461. case R_PPC64_TOC:
  462. *(unsigned long *)location = my_r2(sechdrs, me);
  463. break;
  464. case R_PPC64_TOC16:
  465. /* Subtract TOC pointer */
  466. value -= my_r2(sechdrs, me);
  467. if (value + 0x8000 > 0xffff) {
  468. pr_err("%s: bad TOC16 relocation (0x%lx)\n",
  469. me->name, value);
  470. return -ENOEXEC;
  471. }
  472. *((uint16_t *) location)
  473. = (*((uint16_t *) location) & ~0xffff)
  474. | (value & 0xffff);
  475. break;
  476. case R_PPC64_TOC16_LO:
  477. /* Subtract TOC pointer */
  478. value -= my_r2(sechdrs, me);
  479. *((uint16_t *) location)
  480. = (*((uint16_t *) location) & ~0xffff)
  481. | (value & 0xffff);
  482. break;
  483. case R_PPC64_TOC16_DS:
  484. /* Subtract TOC pointer */
  485. value -= my_r2(sechdrs, me);
  486. if ((value & 3) != 0 || value + 0x8000 > 0xffff) {
  487. pr_err("%s: bad TOC16_DS relocation (0x%lx)\n",
  488. me->name, value);
  489. return -ENOEXEC;
  490. }
  491. *((uint16_t *) location)
  492. = (*((uint16_t *) location) & ~0xfffc)
  493. | (value & 0xfffc);
  494. break;
  495. case R_PPC64_TOC16_LO_DS:
  496. /* Subtract TOC pointer */
  497. value -= my_r2(sechdrs, me);
  498. if ((value & 3) != 0) {
  499. pr_err("%s: bad TOC16_LO_DS relocation (0x%lx)\n",
  500. me->name, value);
  501. return -ENOEXEC;
  502. }
  503. *((uint16_t *) location)
  504. = (*((uint16_t *) location) & ~0xfffc)
  505. | (value & 0xfffc);
  506. break;
  507. case R_PPC64_TOC16_HA:
  508. /* Subtract TOC pointer */
  509. value -= my_r2(sechdrs, me);
  510. value = ((value + 0x8000) >> 16);
  511. *((uint16_t *) location)
  512. = (*((uint16_t *) location) & ~0xffff)
  513. | (value & 0xffff);
  514. break;
  515. case R_PPC_REL24:
  516. /* FIXME: Handle weak symbols here --RR */
  517. if (sym->st_shndx == SHN_UNDEF) {
  518. /* External: go via stub */
  519. value = stub_for_addr(sechdrs, value, me);
  520. if (!value)
  521. return -ENOENT;
  522. if (!restore_r2((u32 *)location + 1, me))
  523. return -ENOEXEC;
  524. } else
  525. value += local_entry_offset(sym);
  526. /* Convert value to relative */
  527. value -= (unsigned long)location;
  528. if (value + 0x2000000 > 0x3ffffff || (value & 3) != 0){
  529. pr_err("%s: REL24 %li out of range!\n",
  530. me->name, (long int)value);
  531. return -ENOEXEC;
  532. }
  533. /* Only replace bits 2 through 26 */
  534. *(uint32_t *)location
  535. = (*(uint32_t *)location & ~0x03fffffc)
  536. | (value & 0x03fffffc);
  537. break;
  538. case R_PPC64_REL64:
  539. /* 64 bits relative (used by features fixups) */
  540. *location = value - (unsigned long)location;
  541. break;
  542. case R_PPC64_TOCSAVE:
  543. /*
  544. * Marker reloc indicates we don't have to save r2.
  545. * That would only save us one instruction, so ignore
  546. * it.
  547. */
  548. break;
  549. case R_PPC64_ENTRY:
  550. /*
  551. * Optimize ELFv2 large code model entry point if
  552. * the TOC is within 2GB range of current location.
  553. */
  554. value = my_r2(sechdrs, me) - (unsigned long)location;
  555. if (value + 0x80008000 > 0xffffffff)
  556. break;
  557. /*
  558. * Check for the large code model prolog sequence:
  559. * ld r2, ...(r12)
  560. * add r2, r2, r12
  561. */
  562. if ((((uint32_t *)location)[0] & ~0xfffc)
  563. != 0xe84c0000)
  564. break;
  565. if (((uint32_t *)location)[1] != 0x7c426214)
  566. break;
  567. /*
  568. * If found, replace it with:
  569. * addis r2, r12, (.TOC.-func)@ha
  570. * addi r2, r12, (.TOC.-func)@l
  571. */
  572. ((uint32_t *)location)[0] = 0x3c4c0000 + PPC_HA(value);
  573. ((uint32_t *)location)[1] = 0x38420000 + PPC_LO(value);
  574. break;
  575. case R_PPC64_REL16_HA:
  576. /* Subtract location pointer */
  577. value -= (unsigned long)location;
  578. value = ((value + 0x8000) >> 16);
  579. *((uint16_t *) location)
  580. = (*((uint16_t *) location) & ~0xffff)
  581. | (value & 0xffff);
  582. break;
  583. case R_PPC64_REL16_LO:
  584. /* Subtract location pointer */
  585. value -= (unsigned long)location;
  586. *((uint16_t *) location)
  587. = (*((uint16_t *) location) & ~0xffff)
  588. | (value & 0xffff);
  589. break;
  590. default:
  591. pr_err("%s: Unknown ADD relocation: %lu\n",
  592. me->name,
  593. (unsigned long)ELF64_R_TYPE(rela[i].r_info));
  594. return -ENOEXEC;
  595. }
  596. }
  597. #ifdef CONFIG_DYNAMIC_FTRACE
  598. me->arch.toc = my_r2(sechdrs, me);
  599. me->arch.tramp = stub_for_addr(sechdrs,
  600. (unsigned long)ftrace_caller,
  601. me);
  602. #endif
  603. return 0;
  604. }