module.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953
  1. /*
  2. * IA-64-specific support for kernel module loader.
  3. *
  4. * Copyright (C) 2003 Hewlett-Packard Co
  5. * David Mosberger-Tang <davidm@hpl.hp.com>
  6. *
  7. * Loosely based on patch by Rusty Russell.
  8. */
  9. /* relocs tested so far:
  10. DIR64LSB
  11. FPTR64LSB
  12. GPREL22
  13. LDXMOV
  14. LDXMOV
  15. LTOFF22
  16. LTOFF22X
  17. LTOFF22X
  18. LTOFF_FPTR22
  19. PCREL21B (for br.call only; br.cond is not supported out of modules!)
  20. PCREL60B (for brl.cond only; brl.call is not supported for modules!)
  21. PCREL64LSB
  22. SECREL32LSB
  23. SEGREL64LSB
  24. */
  25. #include <linux/kernel.h>
  26. #include <linux/sched.h>
  27. #include <linux/elf.h>
  28. #include <linux/moduleloader.h>
  29. #include <linux/string.h>
  30. #include <linux/vmalloc.h>
  31. #include <asm/patch.h>
  32. #include <asm/unaligned.h>
  33. #define ARCH_MODULE_DEBUG 0
  34. #if ARCH_MODULE_DEBUG
  35. # define DEBUGP printk
  36. # define inline
  37. #else
  38. # define DEBUGP(fmt , a...)
  39. #endif
  40. #ifdef CONFIG_ITANIUM
  41. # define USE_BRL 0
  42. #else
  43. # define USE_BRL 1
  44. #endif
  45. #define MAX_LTOFF ((uint64_t) (1 << 22)) /* max. allowable linkage-table offset */
  46. /* Define some relocation helper macros/types: */
  47. #define FORMAT_SHIFT 0
  48. #define FORMAT_BITS 3
  49. #define FORMAT_MASK ((1 << FORMAT_BITS) - 1)
  50. #define VALUE_SHIFT 3
  51. #define VALUE_BITS 5
  52. #define VALUE_MASK ((1 << VALUE_BITS) - 1)
  53. enum reloc_target_format {
  54. /* direct encoded formats: */
  55. RF_NONE = 0,
  56. RF_INSN14 = 1,
  57. RF_INSN22 = 2,
  58. RF_INSN64 = 3,
  59. RF_32MSB = 4,
  60. RF_32LSB = 5,
  61. RF_64MSB = 6,
  62. RF_64LSB = 7,
  63. /* formats that cannot be directly decoded: */
  64. RF_INSN60,
  65. RF_INSN21B, /* imm21 form 1 */
  66. RF_INSN21M, /* imm21 form 2 */
  67. RF_INSN21F /* imm21 form 3 */
  68. };
  69. enum reloc_value_formula {
  70. RV_DIRECT = 4, /* S + A */
  71. RV_GPREL = 5, /* @gprel(S + A) */
  72. RV_LTREL = 6, /* @ltoff(S + A) */
  73. RV_PLTREL = 7, /* @pltoff(S + A) */
  74. RV_FPTR = 8, /* @fptr(S + A) */
  75. RV_PCREL = 9, /* S + A - P */
  76. RV_LTREL_FPTR = 10, /* @ltoff(@fptr(S + A)) */
  77. RV_SEGREL = 11, /* @segrel(S + A) */
  78. RV_SECREL = 12, /* @secrel(S + A) */
  79. RV_BDREL = 13, /* BD + A */
  80. RV_LTV = 14, /* S + A (like RV_DIRECT, except frozen at static link-time) */
  81. RV_PCREL2 = 15, /* S + A - P */
  82. RV_SPECIAL = 16, /* various (see below) */
  83. RV_RSVD17 = 17,
  84. RV_TPREL = 18, /* @tprel(S + A) */
  85. RV_LTREL_TPREL = 19, /* @ltoff(@tprel(S + A)) */
  86. RV_DTPMOD = 20, /* @dtpmod(S + A) */
  87. RV_LTREL_DTPMOD = 21, /* @ltoff(@dtpmod(S + A)) */
  88. RV_DTPREL = 22, /* @dtprel(S + A) */
  89. RV_LTREL_DTPREL = 23, /* @ltoff(@dtprel(S + A)) */
  90. RV_RSVD24 = 24,
  91. RV_RSVD25 = 25,
  92. RV_RSVD26 = 26,
  93. RV_RSVD27 = 27
  94. /* 28-31 reserved for implementation-specific purposes. */
  95. };
  96. #define N(reloc) [R_IA64_##reloc] = #reloc
  97. static const char *reloc_name[256] = {
  98. N(NONE), N(IMM14), N(IMM22), N(IMM64),
  99. N(DIR32MSB), N(DIR32LSB), N(DIR64MSB), N(DIR64LSB),
  100. N(GPREL22), N(GPREL64I), N(GPREL32MSB), N(GPREL32LSB),
  101. N(GPREL64MSB), N(GPREL64LSB), N(LTOFF22), N(LTOFF64I),
  102. N(PLTOFF22), N(PLTOFF64I), N(PLTOFF64MSB), N(PLTOFF64LSB),
  103. N(FPTR64I), N(FPTR32MSB), N(FPTR32LSB), N(FPTR64MSB),
  104. N(FPTR64LSB), N(PCREL60B), N(PCREL21B), N(PCREL21M),
  105. N(PCREL21F), N(PCREL32MSB), N(PCREL32LSB), N(PCREL64MSB),
  106. N(PCREL64LSB), N(LTOFF_FPTR22), N(LTOFF_FPTR64I), N(LTOFF_FPTR32MSB),
  107. N(LTOFF_FPTR32LSB), N(LTOFF_FPTR64MSB), N(LTOFF_FPTR64LSB), N(SEGREL32MSB),
  108. N(SEGREL32LSB), N(SEGREL64MSB), N(SEGREL64LSB), N(SECREL32MSB),
  109. N(SECREL32LSB), N(SECREL64MSB), N(SECREL64LSB), N(REL32MSB),
  110. N(REL32LSB), N(REL64MSB), N(REL64LSB), N(LTV32MSB),
  111. N(LTV32LSB), N(LTV64MSB), N(LTV64LSB), N(PCREL21BI),
  112. N(PCREL22), N(PCREL64I), N(IPLTMSB), N(IPLTLSB),
  113. N(COPY), N(LTOFF22X), N(LDXMOV), N(TPREL14),
  114. N(TPREL22), N(TPREL64I), N(TPREL64MSB), N(TPREL64LSB),
  115. N(LTOFF_TPREL22), N(DTPMOD64MSB), N(DTPMOD64LSB), N(LTOFF_DTPMOD22),
  116. N(DTPREL14), N(DTPREL22), N(DTPREL64I), N(DTPREL32MSB),
  117. N(DTPREL32LSB), N(DTPREL64MSB), N(DTPREL64LSB), N(LTOFF_DTPREL22)
  118. };
  119. #undef N
  120. struct got_entry {
  121. uint64_t val;
  122. };
  123. struct fdesc {
  124. uint64_t ip;
  125. uint64_t gp;
  126. };
  127. /* Opaque struct for insns, to protect against derefs. */
  128. struct insn;
  129. static inline uint64_t
  130. bundle (const struct insn *insn)
  131. {
  132. return (uint64_t) insn & ~0xfUL;
  133. }
  134. static inline int
  135. slot (const struct insn *insn)
  136. {
  137. return (uint64_t) insn & 0x3;
  138. }
  139. static int
  140. apply_imm64 (struct module *mod, struct insn *insn, uint64_t val)
  141. {
  142. if (slot(insn) != 2) {
  143. printk(KERN_ERR "%s: invalid slot number %d for IMM64\n",
  144. mod->name, slot(insn));
  145. return 0;
  146. }
  147. ia64_patch_imm64((u64) insn, val);
  148. return 1;
  149. }
  150. static int
  151. apply_imm60 (struct module *mod, struct insn *insn, uint64_t val)
  152. {
  153. if (slot(insn) != 2) {
  154. printk(KERN_ERR "%s: invalid slot number %d for IMM60\n",
  155. mod->name, slot(insn));
  156. return 0;
  157. }
  158. if (val + ((uint64_t) 1 << 59) >= (1UL << 60)) {
  159. printk(KERN_ERR "%s: value %ld out of IMM60 range\n", mod->name, (int64_t) val);
  160. return 0;
  161. }
  162. ia64_patch_imm60((u64) insn, val);
  163. return 1;
  164. }
  165. static int
  166. apply_imm22 (struct module *mod, struct insn *insn, uint64_t val)
  167. {
  168. if (val + (1 << 21) >= (1 << 22)) {
  169. printk(KERN_ERR "%s: value %li out of IMM22 range\n", mod->name, (int64_t)val);
  170. return 0;
  171. }
  172. ia64_patch((u64) insn, 0x01fffcfe000UL, ( ((val & 0x200000UL) << 15) /* bit 21 -> 36 */
  173. | ((val & 0x1f0000UL) << 6) /* bit 16 -> 22 */
  174. | ((val & 0x00ff80UL) << 20) /* bit 7 -> 27 */
  175. | ((val & 0x00007fUL) << 13) /* bit 0 -> 13 */));
  176. return 1;
  177. }
  178. static int
  179. apply_imm21b (struct module *mod, struct insn *insn, uint64_t val)
  180. {
  181. if (val + (1 << 20) >= (1 << 21)) {
  182. printk(KERN_ERR "%s: value %li out of IMM21b range\n", mod->name, (int64_t)val);
  183. return 0;
  184. }
  185. ia64_patch((u64) insn, 0x11ffffe000UL, ( ((val & 0x100000UL) << 16) /* bit 20 -> 36 */
  186. | ((val & 0x0fffffUL) << 13) /* bit 0 -> 13 */));
  187. return 1;
  188. }
  189. #if USE_BRL
  190. struct plt_entry {
  191. /* Three instruction bundles in PLT. */
  192. unsigned char bundle[2][16];
  193. };
  194. static const struct plt_entry ia64_plt_template = {
  195. {
  196. {
  197. 0x04, 0x00, 0x00, 0x00, 0x01, 0x00, /* [MLX] nop.m 0 */
  198. 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, /* movl gp=TARGET_GP */
  199. 0x00, 0x00, 0x00, 0x60
  200. },
  201. {
  202. 0x05, 0x00, 0x00, 0x00, 0x01, 0x00, /* [MLX] nop.m 0 */
  203. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* brl.many gp=TARGET_GP */
  204. 0x08, 0x00, 0x00, 0xc0
  205. }
  206. }
  207. };
  208. static int
  209. patch_plt (struct module *mod, struct plt_entry *plt, long target_ip, unsigned long target_gp)
  210. {
  211. if (apply_imm64(mod, (struct insn *) (plt->bundle[0] + 2), target_gp)
  212. && apply_imm60(mod, (struct insn *) (plt->bundle[1] + 2),
  213. (target_ip - (int64_t) plt->bundle[1]) / 16))
  214. return 1;
  215. return 0;
  216. }
  217. unsigned long
  218. plt_target (struct plt_entry *plt)
  219. {
  220. uint64_t b0, b1, *b = (uint64_t *) plt->bundle[1];
  221. long off;
  222. b0 = b[0]; b1 = b[1];
  223. off = ( ((b1 & 0x00fffff000000000UL) >> 36) /* imm20b -> bit 0 */
  224. | ((b0 >> 48) << 20) | ((b1 & 0x7fffffUL) << 36) /* imm39 -> bit 20 */
  225. | ((b1 & 0x0800000000000000UL) << 0)); /* i -> bit 59 */
  226. return (long) plt->bundle[1] + 16*off;
  227. }
  228. #else /* !USE_BRL */
  229. struct plt_entry {
  230. /* Three instruction bundles in PLT. */
  231. unsigned char bundle[3][16];
  232. };
  233. static const struct plt_entry ia64_plt_template = {
  234. {
  235. {
  236. 0x05, 0x00, 0x00, 0x00, 0x01, 0x00, /* [MLX] nop.m 0 */
  237. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* movl r16=TARGET_IP */
  238. 0x02, 0x00, 0x00, 0x60
  239. },
  240. {
  241. 0x04, 0x00, 0x00, 0x00, 0x01, 0x00, /* [MLX] nop.m 0 */
  242. 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, /* movl gp=TARGET_GP */
  243. 0x00, 0x00, 0x00, 0x60
  244. },
  245. {
  246. 0x11, 0x00, 0x00, 0x00, 0x01, 0x00, /* [MIB] nop.m 0 */
  247. 0x60, 0x80, 0x04, 0x80, 0x03, 0x00, /* mov b6=r16 */
  248. 0x60, 0x00, 0x80, 0x00 /* br.few b6 */
  249. }
  250. }
  251. };
  252. static int
  253. patch_plt (struct module *mod, struct plt_entry *plt, long target_ip, unsigned long target_gp)
  254. {
  255. if (apply_imm64(mod, (struct insn *) (plt->bundle[0] + 2), target_ip)
  256. && apply_imm64(mod, (struct insn *) (plt->bundle[1] + 2), target_gp))
  257. return 1;
  258. return 0;
  259. }
  260. unsigned long
  261. plt_target (struct plt_entry *plt)
  262. {
  263. uint64_t b0, b1, *b = (uint64_t *) plt->bundle[0];
  264. b0 = b[0]; b1 = b[1];
  265. return ( ((b1 & 0x000007f000000000) >> 36) /* imm7b -> bit 0 */
  266. | ((b1 & 0x07fc000000000000) >> 43) /* imm9d -> bit 7 */
  267. | ((b1 & 0x0003e00000000000) >> 29) /* imm5c -> bit 16 */
  268. | ((b1 & 0x0000100000000000) >> 23) /* ic -> bit 21 */
  269. | ((b0 >> 46) << 22) | ((b1 & 0x7fffff) << 40) /* imm41 -> bit 22 */
  270. | ((b1 & 0x0800000000000000) << 4)); /* i -> bit 63 */
  271. }
  272. #endif /* !USE_BRL */
  273. void *
  274. module_alloc (unsigned long size)
  275. {
  276. if (!size)
  277. return NULL;
  278. return vmalloc(size);
  279. }
  280. void
  281. module_free (struct module *mod, void *module_region)
  282. {
  283. if (mod->arch.init_unw_table && module_region == mod->module_init) {
  284. unw_remove_unwind_table(mod->arch.init_unw_table);
  285. mod->arch.init_unw_table = NULL;
  286. }
  287. vfree(module_region);
  288. }
  289. /* Have we already seen one of these relocations? */
  290. /* FIXME: we could look in other sections, too --RR */
  291. static int
  292. duplicate_reloc (const Elf64_Rela *rela, unsigned int num)
  293. {
  294. unsigned int i;
  295. for (i = 0; i < num; i++) {
  296. if (rela[i].r_info == rela[num].r_info && rela[i].r_addend == rela[num].r_addend)
  297. return 1;
  298. }
  299. return 0;
  300. }
  301. /* Count how many GOT entries we may need */
  302. static unsigned int
  303. count_gots (const Elf64_Rela *rela, unsigned int num)
  304. {
  305. unsigned int i, ret = 0;
  306. /* Sure, this is order(n^2), but it's usually short, and not
  307. time critical */
  308. for (i = 0; i < num; i++) {
  309. switch (ELF64_R_TYPE(rela[i].r_info)) {
  310. case R_IA64_LTOFF22:
  311. case R_IA64_LTOFF22X:
  312. case R_IA64_LTOFF64I:
  313. case R_IA64_LTOFF_FPTR22:
  314. case R_IA64_LTOFF_FPTR64I:
  315. case R_IA64_LTOFF_FPTR32MSB:
  316. case R_IA64_LTOFF_FPTR32LSB:
  317. case R_IA64_LTOFF_FPTR64MSB:
  318. case R_IA64_LTOFF_FPTR64LSB:
  319. if (!duplicate_reloc(rela, i))
  320. ret++;
  321. break;
  322. }
  323. }
  324. return ret;
  325. }
  326. /* Count how many PLT entries we may need */
  327. static unsigned int
  328. count_plts (const Elf64_Rela *rela, unsigned int num)
  329. {
  330. unsigned int i, ret = 0;
  331. /* Sure, this is order(n^2), but it's usually short, and not
  332. time critical */
  333. for (i = 0; i < num; i++) {
  334. switch (ELF64_R_TYPE(rela[i].r_info)) {
  335. case R_IA64_PCREL21B:
  336. case R_IA64_PLTOFF22:
  337. case R_IA64_PLTOFF64I:
  338. case R_IA64_PLTOFF64MSB:
  339. case R_IA64_PLTOFF64LSB:
  340. case R_IA64_IPLTMSB:
  341. case R_IA64_IPLTLSB:
  342. if (!duplicate_reloc(rela, i))
  343. ret++;
  344. break;
  345. }
  346. }
  347. return ret;
  348. }
  349. /* We need to create an function-descriptors for any internal function
  350. which is referenced. */
  351. static unsigned int
  352. count_fdescs (const Elf64_Rela *rela, unsigned int num)
  353. {
  354. unsigned int i, ret = 0;
  355. /* Sure, this is order(n^2), but it's usually short, and not time critical. */
  356. for (i = 0; i < num; i++) {
  357. switch (ELF64_R_TYPE(rela[i].r_info)) {
  358. case R_IA64_FPTR64I:
  359. case R_IA64_FPTR32LSB:
  360. case R_IA64_FPTR32MSB:
  361. case R_IA64_FPTR64LSB:
  362. case R_IA64_FPTR64MSB:
  363. case R_IA64_LTOFF_FPTR22:
  364. case R_IA64_LTOFF_FPTR32LSB:
  365. case R_IA64_LTOFF_FPTR32MSB:
  366. case R_IA64_LTOFF_FPTR64I:
  367. case R_IA64_LTOFF_FPTR64LSB:
  368. case R_IA64_LTOFF_FPTR64MSB:
  369. case R_IA64_IPLTMSB:
  370. case R_IA64_IPLTLSB:
  371. /*
  372. * Jumps to static functions sometimes go straight to their
  373. * offset. Of course, that may not be possible if the jump is
  374. * from init -> core or vice. versa, so we need to generate an
  375. * FDESC (and PLT etc) for that.
  376. */
  377. case R_IA64_PCREL21B:
  378. if (!duplicate_reloc(rela, i))
  379. ret++;
  380. break;
  381. }
  382. }
  383. return ret;
  384. }
  385. int
  386. module_frob_arch_sections (Elf_Ehdr *ehdr, Elf_Shdr *sechdrs, char *secstrings,
  387. struct module *mod)
  388. {
  389. unsigned long core_plts = 0, init_plts = 0, gots = 0, fdescs = 0;
  390. Elf64_Shdr *s, *sechdrs_end = sechdrs + ehdr->e_shnum;
  391. /*
  392. * To store the PLTs and function-descriptors, we expand the .text section for
  393. * core module-code and the .init.text section for initialization code.
  394. */
  395. for (s = sechdrs; s < sechdrs_end; ++s)
  396. if (strcmp(".core.plt", secstrings + s->sh_name) == 0)
  397. mod->arch.core_plt = s;
  398. else if (strcmp(".init.plt", secstrings + s->sh_name) == 0)
  399. mod->arch.init_plt = s;
  400. else if (strcmp(".got", secstrings + s->sh_name) == 0)
  401. mod->arch.got = s;
  402. else if (strcmp(".opd", secstrings + s->sh_name) == 0)
  403. mod->arch.opd = s;
  404. else if (strcmp(".IA_64.unwind", secstrings + s->sh_name) == 0)
  405. mod->arch.unwind = s;
  406. if (!mod->arch.core_plt || !mod->arch.init_plt || !mod->arch.got || !mod->arch.opd) {
  407. printk(KERN_ERR "%s: sections missing\n", mod->name);
  408. return -ENOEXEC;
  409. }
  410. /* GOT and PLTs can occur in any relocated section... */
  411. for (s = sechdrs + 1; s < sechdrs_end; ++s) {
  412. const Elf64_Rela *rels = (void *)ehdr + s->sh_offset;
  413. unsigned long numrels = s->sh_size/sizeof(Elf64_Rela);
  414. if (s->sh_type != SHT_RELA)
  415. continue;
  416. gots += count_gots(rels, numrels);
  417. fdescs += count_fdescs(rels, numrels);
  418. if (strstr(secstrings + s->sh_name, ".init"))
  419. init_plts += count_plts(rels, numrels);
  420. else
  421. core_plts += count_plts(rels, numrels);
  422. }
  423. mod->arch.core_plt->sh_type = SHT_NOBITS;
  424. mod->arch.core_plt->sh_flags = SHF_EXECINSTR | SHF_ALLOC;
  425. mod->arch.core_plt->sh_addralign = 16;
  426. mod->arch.core_plt->sh_size = core_plts * sizeof(struct plt_entry);
  427. mod->arch.init_plt->sh_type = SHT_NOBITS;
  428. mod->arch.init_plt->sh_flags = SHF_EXECINSTR | SHF_ALLOC;
  429. mod->arch.init_plt->sh_addralign = 16;
  430. mod->arch.init_plt->sh_size = init_plts * sizeof(struct plt_entry);
  431. mod->arch.got->sh_type = SHT_NOBITS;
  432. mod->arch.got->sh_flags = ARCH_SHF_SMALL | SHF_ALLOC;
  433. mod->arch.got->sh_addralign = 8;
  434. mod->arch.got->sh_size = gots * sizeof(struct got_entry);
  435. mod->arch.opd->sh_type = SHT_NOBITS;
  436. mod->arch.opd->sh_flags = SHF_ALLOC;
  437. mod->arch.opd->sh_addralign = 8;
  438. mod->arch.opd->sh_size = fdescs * sizeof(struct fdesc);
  439. DEBUGP("%s: core.plt=%lx, init.plt=%lx, got=%lx, fdesc=%lx\n",
  440. __FUNCTION__, mod->arch.core_plt->sh_size, mod->arch.init_plt->sh_size,
  441. mod->arch.got->sh_size, mod->arch.opd->sh_size);
  442. return 0;
  443. }
  444. static inline int
  445. in_init (const struct module *mod, uint64_t addr)
  446. {
  447. return addr - (uint64_t) mod->module_init < mod->init_size;
  448. }
  449. static inline int
  450. in_core (const struct module *mod, uint64_t addr)
  451. {
  452. return addr - (uint64_t) mod->module_core < mod->core_size;
  453. }
  454. static inline int
  455. is_internal (const struct module *mod, uint64_t value)
  456. {
  457. return in_init(mod, value) || in_core(mod, value);
  458. }
  459. /*
  460. * Get gp-relative offset for the linkage-table entry of VALUE.
  461. */
  462. static uint64_t
  463. get_ltoff (struct module *mod, uint64_t value, int *okp)
  464. {
  465. struct got_entry *got, *e;
  466. if (!*okp)
  467. return 0;
  468. got = (void *) mod->arch.got->sh_addr;
  469. for (e = got; e < got + mod->arch.next_got_entry; ++e)
  470. if (e->val == value)
  471. goto found;
  472. /* Not enough GOT entries? */
  473. if (e >= (struct got_entry *) (mod->arch.got->sh_addr + mod->arch.got->sh_size))
  474. BUG();
  475. e->val = value;
  476. ++mod->arch.next_got_entry;
  477. found:
  478. return (uint64_t) e - mod->arch.gp;
  479. }
  480. static inline int
  481. gp_addressable (struct module *mod, uint64_t value)
  482. {
  483. return value - mod->arch.gp + MAX_LTOFF/2 < MAX_LTOFF;
  484. }
  485. /* Get PC-relative PLT entry for this value. Returns 0 on failure. */
  486. static uint64_t
  487. get_plt (struct module *mod, const struct insn *insn, uint64_t value, int *okp)
  488. {
  489. struct plt_entry *plt, *plt_end;
  490. uint64_t target_ip, target_gp;
  491. if (!*okp)
  492. return 0;
  493. if (in_init(mod, (uint64_t) insn)) {
  494. plt = (void *) mod->arch.init_plt->sh_addr;
  495. plt_end = (void *) plt + mod->arch.init_plt->sh_size;
  496. } else {
  497. plt = (void *) mod->arch.core_plt->sh_addr;
  498. plt_end = (void *) plt + mod->arch.core_plt->sh_size;
  499. }
  500. /* "value" is a pointer to a function-descriptor; fetch the target ip/gp from it: */
  501. target_ip = ((uint64_t *) value)[0];
  502. target_gp = ((uint64_t *) value)[1];
  503. /* Look for existing PLT entry. */
  504. while (plt->bundle[0][0]) {
  505. if (plt_target(plt) == target_ip)
  506. goto found;
  507. if (++plt >= plt_end)
  508. BUG();
  509. }
  510. *plt = ia64_plt_template;
  511. if (!patch_plt(mod, plt, target_ip, target_gp)) {
  512. *okp = 0;
  513. return 0;
  514. }
  515. #if ARCH_MODULE_DEBUG
  516. if (plt_target(plt) != target_ip) {
  517. printk("%s: mistargeted PLT: wanted %lx, got %lx\n",
  518. __FUNCTION__, target_ip, plt_target(plt));
  519. *okp = 0;
  520. return 0;
  521. }
  522. #endif
  523. found:
  524. return (uint64_t) plt;
  525. }
  526. /* Get function descriptor for VALUE. */
  527. static uint64_t
  528. get_fdesc (struct module *mod, uint64_t value, int *okp)
  529. {
  530. struct fdesc *fdesc = (void *) mod->arch.opd->sh_addr;
  531. if (!*okp)
  532. return 0;
  533. if (!value) {
  534. printk(KERN_ERR "%s: fdesc for zero requested!\n", mod->name);
  535. return 0;
  536. }
  537. if (!is_internal(mod, value))
  538. /*
  539. * If it's not a module-local entry-point, "value" already points to a
  540. * function-descriptor.
  541. */
  542. return value;
  543. /* Look for existing function descriptor. */
  544. while (fdesc->ip) {
  545. if (fdesc->ip == value)
  546. return (uint64_t)fdesc;
  547. if ((uint64_t) ++fdesc >= mod->arch.opd->sh_addr + mod->arch.opd->sh_size)
  548. BUG();
  549. }
  550. /* Create new one */
  551. fdesc->ip = value;
  552. fdesc->gp = mod->arch.gp;
  553. return (uint64_t) fdesc;
  554. }
  555. static inline int
  556. do_reloc (struct module *mod, uint8_t r_type, Elf64_Sym *sym, uint64_t addend,
  557. Elf64_Shdr *sec, void *location)
  558. {
  559. enum reloc_target_format format = (r_type >> FORMAT_SHIFT) & FORMAT_MASK;
  560. enum reloc_value_formula formula = (r_type >> VALUE_SHIFT) & VALUE_MASK;
  561. uint64_t val;
  562. int ok = 1;
  563. val = sym->st_value + addend;
  564. switch (formula) {
  565. case RV_SEGREL: /* segment base is arbitrarily chosen to be 0 for kernel modules */
  566. case RV_DIRECT:
  567. break;
  568. case RV_GPREL: val -= mod->arch.gp; break;
  569. case RV_LTREL: val = get_ltoff(mod, val, &ok); break;
  570. case RV_PLTREL: val = get_plt(mod, location, val, &ok); break;
  571. case RV_FPTR: val = get_fdesc(mod, val, &ok); break;
  572. case RV_SECREL: val -= sec->sh_addr; break;
  573. case RV_LTREL_FPTR: val = get_ltoff(mod, get_fdesc(mod, val, &ok), &ok); break;
  574. case RV_PCREL:
  575. switch (r_type) {
  576. case R_IA64_PCREL21B:
  577. if ((in_init(mod, val) && in_core(mod, (uint64_t)location)) ||
  578. (in_core(mod, val) && in_init(mod, (uint64_t)location))) {
  579. /*
  580. * Init section may have been allocated far away from core,
  581. * if the branch won't reach, then allocate a plt for it.
  582. */
  583. uint64_t delta = ((int64_t)val - (int64_t)location) / 16;
  584. if (delta + (1 << 20) >= (1 << 21)) {
  585. val = get_fdesc(mod, val, &ok);
  586. val = get_plt(mod, location, val, &ok);
  587. }
  588. } else if (!is_internal(mod, val))
  589. val = get_plt(mod, location, val, &ok);
  590. /* FALL THROUGH */
  591. default:
  592. val -= bundle(location);
  593. break;
  594. case R_IA64_PCREL32MSB:
  595. case R_IA64_PCREL32LSB:
  596. case R_IA64_PCREL64MSB:
  597. case R_IA64_PCREL64LSB:
  598. val -= (uint64_t) location;
  599. break;
  600. }
  601. switch (r_type) {
  602. case R_IA64_PCREL60B: format = RF_INSN60; break;
  603. case R_IA64_PCREL21B: format = RF_INSN21B; break;
  604. case R_IA64_PCREL21M: format = RF_INSN21M; break;
  605. case R_IA64_PCREL21F: format = RF_INSN21F; break;
  606. default: break;
  607. }
  608. break;
  609. case RV_BDREL:
  610. val -= (uint64_t) (in_init(mod, val) ? mod->module_init : mod->module_core);
  611. break;
  612. case RV_LTV:
  613. /* can link-time value relocs happen here? */
  614. BUG();
  615. break;
  616. case RV_PCREL2:
  617. if (r_type == R_IA64_PCREL21BI) {
  618. if (!is_internal(mod, val)) {
  619. printk(KERN_ERR "%s: %s reloc against non-local symbol (%lx)\n",
  620. __FUNCTION__, reloc_name[r_type], val);
  621. return -ENOEXEC;
  622. }
  623. format = RF_INSN21B;
  624. }
  625. val -= bundle(location);
  626. break;
  627. case RV_SPECIAL:
  628. switch (r_type) {
  629. case R_IA64_IPLTMSB:
  630. case R_IA64_IPLTLSB:
  631. val = get_fdesc(mod, get_plt(mod, location, val, &ok), &ok);
  632. format = RF_64LSB;
  633. if (r_type == R_IA64_IPLTMSB)
  634. format = RF_64MSB;
  635. break;
  636. case R_IA64_SUB:
  637. val = addend - sym->st_value;
  638. format = RF_INSN64;
  639. break;
  640. case R_IA64_LTOFF22X:
  641. if (gp_addressable(mod, val))
  642. val -= mod->arch.gp;
  643. else
  644. val = get_ltoff(mod, val, &ok);
  645. format = RF_INSN22;
  646. break;
  647. case R_IA64_LDXMOV:
  648. if (gp_addressable(mod, val)) {
  649. /* turn "ld8" into "mov": */
  650. DEBUGP("%s: patching ld8 at %p to mov\n", __FUNCTION__, location);
  651. ia64_patch((u64) location, 0x1fff80fe000UL, 0x10000000000UL);
  652. }
  653. return 0;
  654. default:
  655. if (reloc_name[r_type])
  656. printk(KERN_ERR "%s: special reloc %s not supported",
  657. mod->name, reloc_name[r_type]);
  658. else
  659. printk(KERN_ERR "%s: unknown special reloc %x\n",
  660. mod->name, r_type);
  661. return -ENOEXEC;
  662. }
  663. break;
  664. case RV_TPREL:
  665. case RV_LTREL_TPREL:
  666. case RV_DTPMOD:
  667. case RV_LTREL_DTPMOD:
  668. case RV_DTPREL:
  669. case RV_LTREL_DTPREL:
  670. printk(KERN_ERR "%s: %s reloc not supported\n",
  671. mod->name, reloc_name[r_type] ? reloc_name[r_type] : "?");
  672. return -ENOEXEC;
  673. default:
  674. printk(KERN_ERR "%s: unknown reloc %x\n", mod->name, r_type);
  675. return -ENOEXEC;
  676. }
  677. if (!ok)
  678. return -ENOEXEC;
  679. DEBUGP("%s: [%p]<-%016lx = %s(%lx)\n", __FUNCTION__, location, val,
  680. reloc_name[r_type] ? reloc_name[r_type] : "?", sym->st_value + addend);
  681. switch (format) {
  682. case RF_INSN21B: ok = apply_imm21b(mod, location, (int64_t) val / 16); break;
  683. case RF_INSN22: ok = apply_imm22(mod, location, val); break;
  684. case RF_INSN64: ok = apply_imm64(mod, location, val); break;
  685. case RF_INSN60: ok = apply_imm60(mod, location, (int64_t) val / 16); break;
  686. case RF_32LSB: put_unaligned(val, (uint32_t *) location); break;
  687. case RF_64LSB: put_unaligned(val, (uint64_t *) location); break;
  688. case RF_32MSB: /* ia64 Linux is little-endian... */
  689. case RF_64MSB: /* ia64 Linux is little-endian... */
  690. case RF_INSN14: /* must be within-module, i.e., resolved by "ld -r" */
  691. case RF_INSN21M: /* must be within-module, i.e., resolved by "ld -r" */
  692. case RF_INSN21F: /* must be within-module, i.e., resolved by "ld -r" */
  693. printk(KERN_ERR "%s: format %u needed by %s reloc is not supported\n",
  694. mod->name, format, reloc_name[r_type] ? reloc_name[r_type] : "?");
  695. return -ENOEXEC;
  696. default:
  697. printk(KERN_ERR "%s: relocation %s resulted in unknown format %u\n",
  698. mod->name, reloc_name[r_type] ? reloc_name[r_type] : "?", format);
  699. return -ENOEXEC;
  700. }
  701. return ok ? 0 : -ENOEXEC;
  702. }
  703. int
  704. apply_relocate_add (Elf64_Shdr *sechdrs, const char *strtab, unsigned int symindex,
  705. unsigned int relsec, struct module *mod)
  706. {
  707. unsigned int i, n = sechdrs[relsec].sh_size / sizeof(Elf64_Rela);
  708. Elf64_Rela *rela = (void *) sechdrs[relsec].sh_addr;
  709. Elf64_Shdr *target_sec;
  710. int ret;
  711. DEBUGP("%s: applying section %u (%u relocs) to %u\n", __FUNCTION__,
  712. relsec, n, sechdrs[relsec].sh_info);
  713. target_sec = sechdrs + sechdrs[relsec].sh_info;
  714. if (target_sec->sh_entsize == ~0UL)
  715. /*
  716. * If target section wasn't allocated, we don't need to relocate it.
  717. * Happens, e.g., for debug sections.
  718. */
  719. return 0;
  720. if (!mod->arch.gp) {
  721. /*
  722. * XXX Should have an arch-hook for running this after final section
  723. * addresses have been selected...
  724. */
  725. uint64_t gp;
  726. if (mod->core_size > MAX_LTOFF)
  727. /*
  728. * This takes advantage of fact that SHF_ARCH_SMALL gets allocated
  729. * at the end of the module.
  730. */
  731. gp = mod->core_size - MAX_LTOFF / 2;
  732. else
  733. gp = mod->core_size / 2;
  734. gp = (uint64_t) mod->module_core + ((gp + 7) & -8);
  735. mod->arch.gp = gp;
  736. DEBUGP("%s: placing gp at 0x%lx\n", __FUNCTION__, gp);
  737. }
  738. for (i = 0; i < n; i++) {
  739. ret = do_reloc(mod, ELF64_R_TYPE(rela[i].r_info),
  740. ((Elf64_Sym *) sechdrs[symindex].sh_addr
  741. + ELF64_R_SYM(rela[i].r_info)),
  742. rela[i].r_addend, target_sec,
  743. (void *) target_sec->sh_addr + rela[i].r_offset);
  744. if (ret < 0)
  745. return ret;
  746. }
  747. return 0;
  748. }
  749. int
  750. apply_relocate (Elf64_Shdr *sechdrs, const char *strtab, unsigned int symindex,
  751. unsigned int relsec, struct module *mod)
  752. {
  753. printk(KERN_ERR "module %s: REL relocs in section %u unsupported\n", mod->name, relsec);
  754. return -ENOEXEC;
  755. }
  756. /*
  757. * Modules contain a single unwind table which covers both the core and the init text
  758. * sections but since the two are not contiguous, we need to split this table up such that
  759. * we can register (and unregister) each "segment" seperately. Fortunately, this sounds
  760. * more complicated than it really is.
  761. */
  762. static void
  763. register_unwind_table (struct module *mod)
  764. {
  765. struct unw_table_entry *start = (void *) mod->arch.unwind->sh_addr;
  766. struct unw_table_entry *end = start + mod->arch.unwind->sh_size / sizeof (*start);
  767. struct unw_table_entry tmp, *e1, *e2, *core, *init;
  768. unsigned long num_init = 0, num_core = 0;
  769. /* First, count how many init and core unwind-table entries there are. */
  770. for (e1 = start; e1 < end; ++e1)
  771. if (in_init(mod, e1->start_offset))
  772. ++num_init;
  773. else
  774. ++num_core;
  775. /*
  776. * Second, sort the table such that all unwind-table entries for the init and core
  777. * text sections are nicely separated. We do this with a stupid bubble sort
  778. * (unwind tables don't get ridiculously huge).
  779. */
  780. for (e1 = start; e1 < end; ++e1) {
  781. for (e2 = e1 + 1; e2 < end; ++e2) {
  782. if (e2->start_offset < e1->start_offset) {
  783. tmp = *e1;
  784. *e1 = *e2;
  785. *e2 = tmp;
  786. }
  787. }
  788. }
  789. /*
  790. * Third, locate the init and core segments in the unwind table:
  791. */
  792. if (in_init(mod, start->start_offset)) {
  793. init = start;
  794. core = start + num_init;
  795. } else {
  796. core = start;
  797. init = start + num_core;
  798. }
  799. DEBUGP("%s: name=%s, gp=%lx, num_init=%lu, num_core=%lu\n", __FUNCTION__,
  800. mod->name, mod->arch.gp, num_init, num_core);
  801. /*
  802. * Fourth, register both tables (if not empty).
  803. */
  804. if (num_core > 0) {
  805. mod->arch.core_unw_table = unw_add_unwind_table(mod->name, 0, mod->arch.gp,
  806. core, core + num_core);
  807. DEBUGP("%s: core: handle=%p [%p-%p)\n", __FUNCTION__,
  808. mod->arch.core_unw_table, core, core + num_core);
  809. }
  810. if (num_init > 0) {
  811. mod->arch.init_unw_table = unw_add_unwind_table(mod->name, 0, mod->arch.gp,
  812. init, init + num_init);
  813. DEBUGP("%s: init: handle=%p [%p-%p)\n", __FUNCTION__,
  814. mod->arch.init_unw_table, init, init + num_init);
  815. }
  816. }
  817. int
  818. module_finalize (const Elf_Ehdr *hdr, const Elf_Shdr *sechdrs, struct module *mod)
  819. {
  820. DEBUGP("%s: init: entry=%p\n", __FUNCTION__, mod->init);
  821. if (mod->arch.unwind)
  822. register_unwind_table(mod);
  823. return 0;
  824. }
  825. void
  826. module_arch_cleanup (struct module *mod)
  827. {
  828. if (mod->arch.init_unw_table)
  829. unw_remove_unwind_table(mod->arch.init_unw_table);
  830. if (mod->arch.core_unw_table)
  831. unw_remove_unwind_table(mod->arch.core_unw_table);
  832. }
  833. #ifdef CONFIG_SMP
  834. void
  835. percpu_modcopy (void *pcpudst, const void *src, unsigned long size)
  836. {
  837. unsigned int i;
  838. for_each_possible_cpu(i) {
  839. memcpy(pcpudst + __per_cpu_offset[i], src, size);
  840. }
  841. }
  842. #endif /* CONFIG_SMP */