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