module_64.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* Kernel module help for PPC64.
  3. Copyright (C) 2001, 2003 Rusty Russell IBM Corporation.
  4. */
  5. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  6. #include <linux/module.h>
  7. #include <linux/elf.h>
  8. #include <linux/moduleloader.h>
  9. #include <linux/err.h>
  10. #include <linux/vmalloc.h>
  11. #include <linux/ftrace.h>
  12. #include <linux/bug.h>
  13. #include <linux/uaccess.h>
  14. #include <linux/kernel.h>
  15. #include <asm/module.h>
  16. #include <asm/firmware.h>
  17. #include <asm/code-patching.h>
  18. #include <linux/sort.h>
  19. #include <asm/setup.h>
  20. #include <asm/sections.h>
  21. #include <asm/inst.h>
  22. /* FIXME: We don't do .init separately. To do this, we'd need to have
  23. a separate r2 value in the init and core section, and stub between
  24. them, too.
  25. Using a magic allocator which places modules within 32MB solves
  26. this, and makes other things simpler. Anton?
  27. --RR. */
  28. #ifdef CONFIG_PPC64_ELF_ABI_V2
  29. static func_desc_t func_desc(unsigned long addr)
  30. {
  31. func_desc_t desc = {
  32. .addr = addr,
  33. };
  34. return desc;
  35. }
  36. /* PowerPC64 specific values for the Elf64_Sym st_other field. */
  37. #define STO_PPC64_LOCAL_BIT 5
  38. #define STO_PPC64_LOCAL_MASK (7 << STO_PPC64_LOCAL_BIT)
  39. #define PPC64_LOCAL_ENTRY_OFFSET(other) \
  40. (((1 << (((other) & STO_PPC64_LOCAL_MASK) >> STO_PPC64_LOCAL_BIT)) >> 2) << 2)
  41. static unsigned int local_entry_offset(const Elf64_Sym *sym)
  42. {
  43. /* sym->st_other indicates offset to local entry point
  44. * (otherwise it will assume r12 is the address of the start
  45. * of function and try to derive r2 from it). */
  46. return PPC64_LOCAL_ENTRY_OFFSET(sym->st_other);
  47. }
  48. #else
  49. static func_desc_t func_desc(unsigned long addr)
  50. {
  51. return *(struct func_desc *)addr;
  52. }
  53. static unsigned int local_entry_offset(const Elf64_Sym *sym)
  54. {
  55. return 0;
  56. }
  57. void *dereference_module_function_descriptor(struct module *mod, void *ptr)
  58. {
  59. if (ptr < (void *)mod->arch.start_opd ||
  60. ptr >= (void *)mod->arch.end_opd)
  61. return ptr;
  62. return dereference_function_descriptor(ptr);
  63. }
  64. #endif
  65. static unsigned long func_addr(unsigned long addr)
  66. {
  67. return func_desc(addr).addr;
  68. }
  69. static unsigned long stub_func_addr(func_desc_t func)
  70. {
  71. return func.addr;
  72. }
  73. #define STUB_MAGIC 0x73747562 /* stub */
  74. /* Like PPC32, we need little trampolines to do > 24-bit jumps (into
  75. the kernel itself). But on PPC64, these need to be used for every
  76. jump, actually, to reset r2 (TOC+0x8000). */
  77. struct ppc64_stub_entry
  78. {
  79. /* 28 byte jump instruction sequence (7 instructions). We only
  80. * need 6 instructions on ABIv2 but we always allocate 7 so
  81. * so we don't have to modify the trampoline load instruction. */
  82. u32 jump[7];
  83. /* Used by ftrace to identify stubs */
  84. u32 magic;
  85. /* Data for the above code */
  86. func_desc_t funcdata;
  87. };
  88. /*
  89. * PPC64 uses 24 bit jumps, but we need to jump into other modules or
  90. * the kernel which may be further. So we jump to a stub.
  91. *
  92. * For ELFv1 we need to use this to set up the new r2 value (aka TOC
  93. * pointer). For ELFv2 it's the callee's responsibility to set up the
  94. * new r2, but for both we need to save the old r2.
  95. *
  96. * We could simply patch the new r2 value and function pointer into
  97. * the stub, but it's significantly shorter to put these values at the
  98. * end of the stub code, and patch the stub address (32-bits relative
  99. * to the TOC ptr, r2) into the stub.
  100. */
  101. static u32 ppc64_stub_insns[] = {
  102. PPC_RAW_ADDIS(_R11, _R2, 0),
  103. PPC_RAW_ADDI(_R11, _R11, 0),
  104. /* Save current r2 value in magic place on the stack. */
  105. PPC_RAW_STD(_R2, _R1, R2_STACK_OFFSET),
  106. PPC_RAW_LD(_R12, _R11, 32),
  107. #ifdef CONFIG_PPC64_ELF_ABI_V1
  108. /* Set up new r2 from function descriptor */
  109. PPC_RAW_LD(_R2, _R11, 40),
  110. #endif
  111. PPC_RAW_MTCTR(_R12),
  112. PPC_RAW_BCTR(),
  113. };
  114. /* Count how many different 24-bit relocations (different symbol,
  115. different addend) */
  116. static unsigned int count_relocs(const Elf64_Rela *rela, unsigned int num)
  117. {
  118. unsigned int i, r_info, r_addend, _count_relocs;
  119. /* FIXME: Only count external ones --RR */
  120. _count_relocs = 0;
  121. r_info = 0;
  122. r_addend = 0;
  123. for (i = 0; i < num; i++)
  124. /* Only count 24-bit relocs, others don't need stubs */
  125. if (ELF64_R_TYPE(rela[i].r_info) == R_PPC_REL24 &&
  126. (r_info != ELF64_R_SYM(rela[i].r_info) ||
  127. r_addend != rela[i].r_addend)) {
  128. _count_relocs++;
  129. r_info = ELF64_R_SYM(rela[i].r_info);
  130. r_addend = rela[i].r_addend;
  131. }
  132. return _count_relocs;
  133. }
  134. static int relacmp(const void *_x, const void *_y)
  135. {
  136. const Elf64_Rela *x, *y;
  137. y = (Elf64_Rela *)_x;
  138. x = (Elf64_Rela *)_y;
  139. /* Compare the entire r_info (as opposed to ELF64_R_SYM(r_info) only) to
  140. * make the comparison cheaper/faster. It won't affect the sorting or
  141. * the counting algorithms' performance
  142. */
  143. if (x->r_info < y->r_info)
  144. return -1;
  145. else if (x->r_info > y->r_info)
  146. return 1;
  147. else if (x->r_addend < y->r_addend)
  148. return -1;
  149. else if (x->r_addend > y->r_addend)
  150. return 1;
  151. else
  152. return 0;
  153. }
  154. /* Get size of potential trampolines required. */
  155. static unsigned long get_stubs_size(const Elf64_Ehdr *hdr,
  156. const Elf64_Shdr *sechdrs)
  157. {
  158. /* One extra reloc so it's always 0-addr terminated */
  159. unsigned long relocs = 1;
  160. unsigned i;
  161. /* Every relocated section... */
  162. for (i = 1; i < hdr->e_shnum; i++) {
  163. if (sechdrs[i].sh_type == SHT_RELA) {
  164. pr_debug("Found relocations in section %u\n", i);
  165. pr_debug("Ptr: %p. Number: %Lu\n",
  166. (void *)sechdrs[i].sh_addr,
  167. sechdrs[i].sh_size / sizeof(Elf64_Rela));
  168. /* Sort the relocation information based on a symbol and
  169. * addend key. This is a stable O(n*log n) complexity
  170. * algorithm but it will reduce the complexity of
  171. * count_relocs() to linear complexity O(n)
  172. */
  173. sort((void *)sechdrs[i].sh_addr,
  174. sechdrs[i].sh_size / sizeof(Elf64_Rela),
  175. sizeof(Elf64_Rela), relacmp, NULL);
  176. relocs += count_relocs((void *)sechdrs[i].sh_addr,
  177. sechdrs[i].sh_size
  178. / sizeof(Elf64_Rela));
  179. }
  180. }
  181. #ifdef CONFIG_DYNAMIC_FTRACE
  182. /* make the trampoline to the ftrace_caller */
  183. relocs++;
  184. #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
  185. /* an additional one for ftrace_regs_caller */
  186. relocs++;
  187. #endif
  188. #endif
  189. pr_debug("Looks like a total of %lu stubs, max\n", relocs);
  190. return relocs * sizeof(struct ppc64_stub_entry);
  191. }
  192. /* Still needed for ELFv2, for .TOC. */
  193. static void dedotify_versions(struct modversion_info *vers,
  194. unsigned long size)
  195. {
  196. struct modversion_info *end;
  197. for (end = (void *)vers + size; vers < end; vers++)
  198. if (vers->name[0] == '.') {
  199. memmove(vers->name, vers->name+1, strlen(vers->name));
  200. }
  201. }
  202. /*
  203. * Undefined symbols which refer to .funcname, hack to funcname. Make .TOC.
  204. * seem to be defined (value set later).
  205. */
  206. static void dedotify(Elf64_Sym *syms, unsigned int numsyms, char *strtab)
  207. {
  208. unsigned int i;
  209. for (i = 1; i < numsyms; i++) {
  210. if (syms[i].st_shndx == SHN_UNDEF) {
  211. char *name = strtab + syms[i].st_name;
  212. if (name[0] == '.') {
  213. if (strcmp(name+1, "TOC.") == 0)
  214. syms[i].st_shndx = SHN_ABS;
  215. syms[i].st_name++;
  216. }
  217. }
  218. }
  219. }
  220. static Elf64_Sym *find_dot_toc(Elf64_Shdr *sechdrs,
  221. const char *strtab,
  222. unsigned int symindex)
  223. {
  224. unsigned int i, numsyms;
  225. Elf64_Sym *syms;
  226. syms = (Elf64_Sym *)sechdrs[symindex].sh_addr;
  227. numsyms = sechdrs[symindex].sh_size / sizeof(Elf64_Sym);
  228. for (i = 1; i < numsyms; i++) {
  229. if (syms[i].st_shndx == SHN_ABS
  230. && strcmp(strtab + syms[i].st_name, "TOC.") == 0)
  231. return &syms[i];
  232. }
  233. return NULL;
  234. }
  235. bool module_init_section(const char *name)
  236. {
  237. /* We don't handle .init for the moment: always return false. */
  238. return false;
  239. }
  240. int module_frob_arch_sections(Elf64_Ehdr *hdr,
  241. Elf64_Shdr *sechdrs,
  242. char *secstrings,
  243. struct module *me)
  244. {
  245. unsigned int i;
  246. /* Find .toc and .stubs sections, symtab and strtab */
  247. for (i = 1; i < hdr->e_shnum; i++) {
  248. if (strcmp(secstrings + sechdrs[i].sh_name, ".stubs") == 0)
  249. me->arch.stubs_section = i;
  250. else if (strcmp(secstrings + sechdrs[i].sh_name, ".toc") == 0) {
  251. me->arch.toc_section = i;
  252. if (sechdrs[i].sh_addralign < 8)
  253. sechdrs[i].sh_addralign = 8;
  254. }
  255. else if (strcmp(secstrings+sechdrs[i].sh_name,"__versions")==0)
  256. dedotify_versions((void *)hdr + sechdrs[i].sh_offset,
  257. sechdrs[i].sh_size);
  258. if (sechdrs[i].sh_type == SHT_SYMTAB)
  259. dedotify((void *)hdr + sechdrs[i].sh_offset,
  260. sechdrs[i].sh_size / sizeof(Elf64_Sym),
  261. (void *)hdr
  262. + sechdrs[sechdrs[i].sh_link].sh_offset);
  263. }
  264. if (!me->arch.stubs_section) {
  265. pr_err("%s: doesn't contain .stubs.\n", me->name);
  266. return -ENOEXEC;
  267. }
  268. /* If we don't have a .toc, just use .stubs. We need to set r2
  269. to some reasonable value in case the module calls out to
  270. other functions via a stub, or if a function pointer escapes
  271. the module by some means. */
  272. if (!me->arch.toc_section)
  273. me->arch.toc_section = me->arch.stubs_section;
  274. /* Override the stubs size */
  275. sechdrs[me->arch.stubs_section].sh_size = get_stubs_size(hdr, sechdrs);
  276. return 0;
  277. }
  278. #ifdef CONFIG_MPROFILE_KERNEL
  279. static u32 stub_insns[] = {
  280. PPC_RAW_LD(_R12, _R13, offsetof(struct paca_struct, kernel_toc)),
  281. PPC_RAW_ADDIS(_R12, _R12, 0),
  282. PPC_RAW_ADDI(_R12, _R12, 0),
  283. PPC_RAW_MTCTR(_R12),
  284. PPC_RAW_BCTR(),
  285. };
  286. /*
  287. * For mprofile-kernel we use a special stub for ftrace_caller() because we
  288. * can't rely on r2 containing this module's TOC when we enter the stub.
  289. *
  290. * That can happen if the function calling us didn't need to use the toc. In
  291. * that case it won't have setup r2, and the r2 value will be either the
  292. * kernel's toc, or possibly another modules toc.
  293. *
  294. * To deal with that this stub uses the kernel toc, which is always accessible
  295. * via the paca (in r13). The target (ftrace_caller()) is responsible for
  296. * saving and restoring the toc before returning.
  297. */
  298. static inline int create_ftrace_stub(struct ppc64_stub_entry *entry,
  299. unsigned long addr,
  300. struct module *me)
  301. {
  302. long reladdr;
  303. memcpy(entry->jump, stub_insns, sizeof(stub_insns));
  304. /* Stub uses address relative to kernel toc (from the paca) */
  305. reladdr = addr - kernel_toc_addr();
  306. if (reladdr > 0x7FFFFFFF || reladdr < -(0x80000000L)) {
  307. pr_err("%s: Address of %ps out of range of kernel_toc.\n",
  308. me->name, (void *)addr);
  309. return 0;
  310. }
  311. entry->jump[1] |= PPC_HA(reladdr);
  312. entry->jump[2] |= PPC_LO(reladdr);
  313. /* Even though we don't use funcdata in the stub, it's needed elsewhere. */
  314. entry->funcdata = func_desc(addr);
  315. entry->magic = STUB_MAGIC;
  316. return 1;
  317. }
  318. static bool is_mprofile_ftrace_call(const char *name)
  319. {
  320. if (!strcmp("_mcount", name))
  321. return true;
  322. #ifdef CONFIG_DYNAMIC_FTRACE
  323. if (!strcmp("ftrace_caller", name))
  324. return true;
  325. #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
  326. if (!strcmp("ftrace_regs_caller", name))
  327. return true;
  328. #endif
  329. #endif
  330. return false;
  331. }
  332. #else
  333. static inline int create_ftrace_stub(struct ppc64_stub_entry *entry,
  334. unsigned long addr,
  335. struct module *me)
  336. {
  337. return 0;
  338. }
  339. static bool is_mprofile_ftrace_call(const char *name)
  340. {
  341. return false;
  342. }
  343. #endif
  344. /*
  345. * r2 is the TOC pointer: it actually points 0x8000 into the TOC (this gives the
  346. * value maximum span in an instruction which uses a signed offset). Round down
  347. * to a 256 byte boundary for the odd case where we are setting up r2 without a
  348. * .toc section.
  349. */
  350. static inline unsigned long my_r2(const Elf64_Shdr *sechdrs, struct module *me)
  351. {
  352. return (sechdrs[me->arch.toc_section].sh_addr & ~0xfful) + 0x8000;
  353. }
  354. /* Patch stub to reference function and correct r2 value. */
  355. static inline int create_stub(const Elf64_Shdr *sechdrs,
  356. struct ppc64_stub_entry *entry,
  357. unsigned long addr,
  358. struct module *me,
  359. const char *name)
  360. {
  361. long reladdr;
  362. func_desc_t desc;
  363. int i;
  364. if (is_mprofile_ftrace_call(name))
  365. return create_ftrace_stub(entry, addr, me);
  366. for (i = 0; i < ARRAY_SIZE(ppc64_stub_insns); i++) {
  367. if (patch_instruction(&entry->jump[i],
  368. ppc_inst(ppc64_stub_insns[i])))
  369. return 0;
  370. }
  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. if (patch_instruction(&entry->jump[0],
  380. ppc_inst(entry->jump[0] | PPC_HA(reladdr))))
  381. return 0;
  382. if (patch_instruction(&entry->jump[1],
  383. ppc_inst(entry->jump[1] | PPC_LO(reladdr))))
  384. return 0;
  385. // func_desc_t is 8 bytes if ABIv2, else 16 bytes
  386. desc = func_desc(addr);
  387. for (i = 0; i < sizeof(func_desc_t) / sizeof(u32); i++) {
  388. if (patch_instruction(((u32 *)&entry->funcdata) + i,
  389. ppc_inst(((u32 *)(&desc))[i])))
  390. return 0;
  391. }
  392. if (patch_instruction(&entry->magic, ppc_inst(STUB_MAGIC)))
  393. return 0;
  394. return 1;
  395. }
  396. /* Create stub to jump to function described in this OPD/ptr: we need the
  397. stub to set up the TOC ptr (r2) for the function. */
  398. static unsigned long stub_for_addr(const Elf64_Shdr *sechdrs,
  399. unsigned long addr,
  400. struct module *me,
  401. const char *name)
  402. {
  403. struct ppc64_stub_entry *stubs;
  404. unsigned int i, num_stubs;
  405. num_stubs = sechdrs[me->arch.stubs_section].sh_size / sizeof(*stubs);
  406. /* Find this stub, or if that fails, the next avail. entry */
  407. stubs = (void *)sechdrs[me->arch.stubs_section].sh_addr;
  408. for (i = 0; stub_func_addr(stubs[i].funcdata); i++) {
  409. if (WARN_ON(i >= num_stubs))
  410. return 0;
  411. if (stub_func_addr(stubs[i].funcdata) == func_addr(addr))
  412. return (unsigned long)&stubs[i];
  413. }
  414. if (!create_stub(sechdrs, &stubs[i], addr, me, name))
  415. return 0;
  416. return (unsigned long)&stubs[i];
  417. }
  418. /* We expect a noop next: if it is, replace it with instruction to
  419. restore r2. */
  420. static int restore_r2(const char *name, u32 *instruction, struct module *me)
  421. {
  422. u32 *prev_insn = instruction - 1;
  423. if (is_mprofile_ftrace_call(name))
  424. return 1;
  425. /*
  426. * Make sure the branch isn't a sibling call. Sibling calls aren't
  427. * "link" branches and they don't return, so they don't need the r2
  428. * restore afterwards.
  429. */
  430. if (!instr_is_relative_link_branch(ppc_inst(*prev_insn)))
  431. return 1;
  432. if (*instruction != PPC_RAW_NOP()) {
  433. pr_err("%s: Expected nop after call, got %08x at %pS\n",
  434. me->name, *instruction, instruction);
  435. return 0;
  436. }
  437. /* ld r2,R2_STACK_OFFSET(r1) */
  438. if (patch_instruction(instruction, ppc_inst(PPC_INST_LD_TOC)))
  439. return 0;
  440. return 1;
  441. }
  442. int apply_relocate_add(Elf64_Shdr *sechdrs,
  443. const char *strtab,
  444. unsigned int symindex,
  445. unsigned int relsec,
  446. struct module *me)
  447. {
  448. unsigned int i;
  449. Elf64_Rela *rela = (void *)sechdrs[relsec].sh_addr;
  450. Elf64_Sym *sym;
  451. unsigned long *location;
  452. unsigned long value;
  453. pr_debug("Applying ADD relocate section %u to %u\n", relsec,
  454. sechdrs[relsec].sh_info);
  455. /* First time we're called, we can fix up .TOC. */
  456. if (!me->arch.toc_fixed) {
  457. sym = find_dot_toc(sechdrs, strtab, symindex);
  458. /* It's theoretically possible that a module doesn't want a
  459. * .TOC. so don't fail it just for that. */
  460. if (sym)
  461. sym->st_value = my_r2(sechdrs, me);
  462. me->arch.toc_fixed = true;
  463. }
  464. for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rela); i++) {
  465. /* This is where to make the change */
  466. location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
  467. + rela[i].r_offset;
  468. /* This is the symbol it is referring to */
  469. sym = (Elf64_Sym *)sechdrs[symindex].sh_addr
  470. + ELF64_R_SYM(rela[i].r_info);
  471. pr_debug("RELOC at %p: %li-type as %s (0x%lx) + %li\n",
  472. location, (long)ELF64_R_TYPE(rela[i].r_info),
  473. strtab + sym->st_name, (unsigned long)sym->st_value,
  474. (long)rela[i].r_addend);
  475. /* `Everything is relative'. */
  476. value = sym->st_value + rela[i].r_addend;
  477. switch (ELF64_R_TYPE(rela[i].r_info)) {
  478. case R_PPC64_ADDR32:
  479. /* Simply set it */
  480. *(u32 *)location = value;
  481. break;
  482. case R_PPC64_ADDR64:
  483. /* Simply set it */
  484. *(unsigned long *)location = value;
  485. break;
  486. case R_PPC64_TOC:
  487. *(unsigned long *)location = my_r2(sechdrs, me);
  488. break;
  489. case R_PPC64_TOC16:
  490. /* Subtract TOC pointer */
  491. value -= my_r2(sechdrs, me);
  492. if (value + 0x8000 > 0xffff) {
  493. pr_err("%s: bad TOC16 relocation (0x%lx)\n",
  494. me->name, value);
  495. return -ENOEXEC;
  496. }
  497. *((uint16_t *) location)
  498. = (*((uint16_t *) location) & ~0xffff)
  499. | (value & 0xffff);
  500. break;
  501. case R_PPC64_TOC16_LO:
  502. /* Subtract TOC pointer */
  503. value -= my_r2(sechdrs, me);
  504. *((uint16_t *) location)
  505. = (*((uint16_t *) location) & ~0xffff)
  506. | (value & 0xffff);
  507. break;
  508. case R_PPC64_TOC16_DS:
  509. /* Subtract TOC pointer */
  510. value -= my_r2(sechdrs, me);
  511. if ((value & 3) != 0 || value + 0x8000 > 0xffff) {
  512. pr_err("%s: bad TOC16_DS relocation (0x%lx)\n",
  513. me->name, value);
  514. return -ENOEXEC;
  515. }
  516. *((uint16_t *) location)
  517. = (*((uint16_t *) location) & ~0xfffc)
  518. | (value & 0xfffc);
  519. break;
  520. case R_PPC64_TOC16_LO_DS:
  521. /* Subtract TOC pointer */
  522. value -= my_r2(sechdrs, me);
  523. if ((value & 3) != 0) {
  524. pr_err("%s: bad TOC16_LO_DS relocation (0x%lx)\n",
  525. me->name, value);
  526. return -ENOEXEC;
  527. }
  528. *((uint16_t *) location)
  529. = (*((uint16_t *) location) & ~0xfffc)
  530. | (value & 0xfffc);
  531. break;
  532. case R_PPC64_TOC16_HA:
  533. /* Subtract TOC pointer */
  534. value -= my_r2(sechdrs, me);
  535. value = ((value + 0x8000) >> 16);
  536. *((uint16_t *) location)
  537. = (*((uint16_t *) location) & ~0xffff)
  538. | (value & 0xffff);
  539. break;
  540. case R_PPC_REL24:
  541. /* FIXME: Handle weak symbols here --RR */
  542. if (sym->st_shndx == SHN_UNDEF ||
  543. sym->st_shndx == SHN_LIVEPATCH) {
  544. /* External: go via stub */
  545. value = stub_for_addr(sechdrs, value, me,
  546. strtab + sym->st_name);
  547. if (!value)
  548. return -ENOENT;
  549. if (!restore_r2(strtab + sym->st_name,
  550. (u32 *)location + 1, me))
  551. return -ENOEXEC;
  552. } else
  553. value += local_entry_offset(sym);
  554. /* Convert value to relative */
  555. value -= (unsigned long)location;
  556. if (value + 0x2000000 > 0x3ffffff || (value & 3) != 0){
  557. pr_err("%s: REL24 %li out of range!\n",
  558. me->name, (long int)value);
  559. return -ENOEXEC;
  560. }
  561. /* Only replace bits 2 through 26 */
  562. value = (*(uint32_t *)location & ~PPC_LI_MASK) | PPC_LI(value);
  563. if (patch_instruction((u32 *)location, ppc_inst(value)))
  564. return -EFAULT;
  565. break;
  566. case R_PPC64_REL64:
  567. /* 64 bits relative (used by features fixups) */
  568. *location = value - (unsigned long)location;
  569. break;
  570. case R_PPC64_REL32:
  571. /* 32 bits relative (used by relative exception tables) */
  572. /* Convert value to relative */
  573. value -= (unsigned long)location;
  574. if (value + 0x80000000 > 0xffffffff) {
  575. pr_err("%s: REL32 %li out of range!\n",
  576. me->name, (long int)value);
  577. return -ENOEXEC;
  578. }
  579. *(u32 *)location = value;
  580. break;
  581. case R_PPC64_TOCSAVE:
  582. /*
  583. * Marker reloc indicates we don't have to save r2.
  584. * That would only save us one instruction, so ignore
  585. * it.
  586. */
  587. break;
  588. case R_PPC64_ENTRY:
  589. /*
  590. * Optimize ELFv2 large code model entry point if
  591. * the TOC is within 2GB range of current location.
  592. */
  593. value = my_r2(sechdrs, me) - (unsigned long)location;
  594. if (value + 0x80008000 > 0xffffffff)
  595. break;
  596. /*
  597. * Check for the large code model prolog sequence:
  598. * ld r2, ...(r12)
  599. * add r2, r2, r12
  600. */
  601. if ((((uint32_t *)location)[0] & ~0xfffc) != PPC_RAW_LD(_R2, _R12, 0))
  602. break;
  603. if (((uint32_t *)location)[1] != PPC_RAW_ADD(_R2, _R2, _R12))
  604. break;
  605. /*
  606. * If found, replace it with:
  607. * addis r2, r12, (.TOC.-func)@ha
  608. * addi r2, r2, (.TOC.-func)@l
  609. */
  610. ((uint32_t *)location)[0] = PPC_RAW_ADDIS(_R2, _R12, PPC_HA(value));
  611. ((uint32_t *)location)[1] = PPC_RAW_ADDI(_R2, _R2, PPC_LO(value));
  612. break;
  613. case R_PPC64_REL16_HA:
  614. /* Subtract location pointer */
  615. value -= (unsigned long)location;
  616. value = ((value + 0x8000) >> 16);
  617. *((uint16_t *) location)
  618. = (*((uint16_t *) location) & ~0xffff)
  619. | (value & 0xffff);
  620. break;
  621. case R_PPC64_REL16_LO:
  622. /* Subtract location pointer */
  623. value -= (unsigned long)location;
  624. *((uint16_t *) location)
  625. = (*((uint16_t *) location) & ~0xffff)
  626. | (value & 0xffff);
  627. break;
  628. default:
  629. pr_err("%s: Unknown ADD relocation: %lu\n",
  630. me->name,
  631. (unsigned long)ELF64_R_TYPE(rela[i].r_info));
  632. return -ENOEXEC;
  633. }
  634. }
  635. return 0;
  636. }
  637. #ifdef CONFIG_DYNAMIC_FTRACE
  638. int module_trampoline_target(struct module *mod, unsigned long addr,
  639. unsigned long *target)
  640. {
  641. struct ppc64_stub_entry *stub;
  642. func_desc_t funcdata;
  643. u32 magic;
  644. if (!within_module_core(addr, mod)) {
  645. pr_err("%s: stub %lx not in module %s\n", __func__, addr, mod->name);
  646. return -EFAULT;
  647. }
  648. stub = (struct ppc64_stub_entry *)addr;
  649. if (copy_from_kernel_nofault(&magic, &stub->magic,
  650. sizeof(magic))) {
  651. pr_err("%s: fault reading magic for stub %lx for %s\n", __func__, addr, mod->name);
  652. return -EFAULT;
  653. }
  654. if (magic != STUB_MAGIC) {
  655. pr_err("%s: bad magic for stub %lx for %s\n", __func__, addr, mod->name);
  656. return -EFAULT;
  657. }
  658. if (copy_from_kernel_nofault(&funcdata, &stub->funcdata,
  659. sizeof(funcdata))) {
  660. pr_err("%s: fault reading funcdata for stub %lx for %s\n", __func__, addr, mod->name);
  661. return -EFAULT;
  662. }
  663. *target = stub_func_addr(funcdata);
  664. return 0;
  665. }
  666. int module_finalize_ftrace(struct module *mod, const Elf_Shdr *sechdrs)
  667. {
  668. mod->arch.tramp = stub_for_addr(sechdrs,
  669. (unsigned long)ftrace_caller,
  670. mod,
  671. "ftrace_caller");
  672. #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
  673. mod->arch.tramp_regs = stub_for_addr(sechdrs,
  674. (unsigned long)ftrace_regs_caller,
  675. mod,
  676. "ftrace_regs_caller");
  677. if (!mod->arch.tramp_regs)
  678. return -ENOENT;
  679. #endif
  680. if (!mod->arch.tramp)
  681. return -ENOENT;
  682. return 0;
  683. }
  684. #endif