1 // SPDX-License-Identifier: GPL-2.0
2 /* This is included from relocs_32/64.c */
4 #define ElfW(type) _ElfW(ELF_BITS, type)
5 #define _ElfW(bits, type) __ElfW(bits, type)
6 #define __ElfW(bits, type) Elf##bits##_##type
8 #define Elf_Addr ElfW(Addr)
9 #define Elf_Ehdr ElfW(Ehdr)
10 #define Elf_Phdr ElfW(Phdr)
11 #define Elf_Shdr ElfW(Shdr)
12 #define Elf_Sym ElfW(Sym)
15 static unsigned long shnum;
16 static unsigned int shstrndx;
17 static unsigned int shsymtabndx;
18 static unsigned int shxsymtabndx;
20 static int sym_index(Elf_Sym *sym);
28 static struct relocs relocs16;
29 static struct relocs relocs32;
31 static struct relocs relocs32neg;
32 static struct relocs relocs64;
46 static struct section *secs;
48 static const char * const sym_regex_kernel[S_NSYMTYPES] = {
50 * Following symbols have been audited. There values are constant and do
51 * not change if bzImage is loaded at a different physical address than
52 * the address for which it has been compiled. Don't warn user about
53 * absolute relocations present w.r.t these symbols.
56 "^(xen_irq_disable_direct_reloc$|"
57 "xen_save_fl_direct_reloc$|"
62 * These symbols are known to be relative, even if the linker marks them
63 * as absolute (typically defined outside any section in the linker script.)
66 "^(__init_(begin|end)|"
67 "__x86_cpu_dev_(start|end)|"
68 "(__parainstructions|__alt_instructions)(_end)?|"
69 "(__iommu_table|__apicdrivers|__smp_locks)(_end)?|"
70 "__(start|end)_pci_.*|"
72 "__(start|end)_builtin_fw|"
74 "__(start|stop)___ksymtab(_gpl)?|"
75 "__(start|stop)___kcrctab(_gpl)?|"
76 "__(start|stop)___param|"
77 "__(start|stop)___modver|"
78 "__(start|stop)___bug_table|"
79 "__tracedata_(start|end)|"
80 "__(start|stop)_notes|"
82 "__end_rodata_aligned|"
84 "(jiffies|jiffies_64)|"
88 "__end_rodata_hpage_align|"
95 static const char * const sym_regex_realmode[S_NSYMTYPES] = {
97 * These symbols are known to be relative, even if the linker marks them
98 * as absolute (typically defined outside any section in the linker script.)
104 * These are 16-bit segment symbols when compiling 16-bit code.
110 * These are offsets belonging to segments, as opposed to linear addresses,
111 * when compiling 16-bit code.
117 static const char * const *sym_regex;
119 static regex_t sym_regex_c[S_NSYMTYPES];
120 static int is_reloc(enum symtype type, const char *sym_name)
122 return sym_regex[type] &&
123 !regexec(&sym_regex_c[type], sym_name, 0, NULL, 0);
126 static void regex_init(int use_real_mode)
133 sym_regex = sym_regex_realmode;
135 sym_regex = sym_regex_kernel;
137 for (i = 0; i < S_NSYMTYPES; i++) {
141 err = regcomp(&sym_regex_c[i], sym_regex[i],
142 REG_EXTENDED|REG_NOSUB);
145 regerror(err, &sym_regex_c[i], errbuf, sizeof(errbuf));
151 static const char *sym_type(unsigned type)
153 static const char *type_name[] = {
154 #define SYM_TYPE(X) [X] = #X
155 SYM_TYPE(STT_NOTYPE),
156 SYM_TYPE(STT_OBJECT),
158 SYM_TYPE(STT_SECTION),
160 SYM_TYPE(STT_COMMON),
164 const char *name = "unknown sym type name";
165 if (type < ARRAY_SIZE(type_name)) {
166 name = type_name[type];
171 static const char *sym_bind(unsigned bind)
173 static const char *bind_name[] = {
174 #define SYM_BIND(X) [X] = #X
176 SYM_BIND(STB_GLOBAL),
180 const char *name = "unknown sym bind name";
181 if (bind < ARRAY_SIZE(bind_name)) {
182 name = bind_name[bind];
187 static const char *sym_visibility(unsigned visibility)
189 static const char *visibility_name[] = {
190 #define SYM_VISIBILITY(X) [X] = #X
191 SYM_VISIBILITY(STV_DEFAULT),
192 SYM_VISIBILITY(STV_INTERNAL),
193 SYM_VISIBILITY(STV_HIDDEN),
194 SYM_VISIBILITY(STV_PROTECTED),
195 #undef SYM_VISIBILITY
197 const char *name = "unknown sym visibility name";
198 if (visibility < ARRAY_SIZE(visibility_name)) {
199 name = visibility_name[visibility];
204 static const char *rel_type(unsigned type)
206 static const char *type_name[] = {
207 #define REL_TYPE(X) [X] = #X
209 REL_TYPE(R_X86_64_NONE),
210 REL_TYPE(R_X86_64_64),
211 REL_TYPE(R_X86_64_PC64),
212 REL_TYPE(R_X86_64_PC32),
213 REL_TYPE(R_X86_64_GOT32),
214 REL_TYPE(R_X86_64_PLT32),
215 REL_TYPE(R_X86_64_COPY),
216 REL_TYPE(R_X86_64_GLOB_DAT),
217 REL_TYPE(R_X86_64_JUMP_SLOT),
218 REL_TYPE(R_X86_64_RELATIVE),
219 REL_TYPE(R_X86_64_GOTPCREL),
220 REL_TYPE(R_X86_64_32),
221 REL_TYPE(R_X86_64_32S),
222 REL_TYPE(R_X86_64_16),
223 REL_TYPE(R_X86_64_PC16),
224 REL_TYPE(R_X86_64_8),
225 REL_TYPE(R_X86_64_PC8),
227 REL_TYPE(R_386_NONE),
229 REL_TYPE(R_386_PC32),
230 REL_TYPE(R_386_GOT32),
231 REL_TYPE(R_386_PLT32),
232 REL_TYPE(R_386_COPY),
233 REL_TYPE(R_386_GLOB_DAT),
234 REL_TYPE(R_386_JMP_SLOT),
235 REL_TYPE(R_386_RELATIVE),
236 REL_TYPE(R_386_GOTOFF),
237 REL_TYPE(R_386_GOTPC),
241 REL_TYPE(R_386_PC16),
245 const char *name = "unknown type rel type name";
246 if (type < ARRAY_SIZE(type_name) && type_name[type]) {
247 name = type_name[type];
252 static const char *sec_name(unsigned shndx)
254 const char *sec_strtab;
256 sec_strtab = secs[shstrndx].strtab;
259 name = sec_strtab + secs[shndx].shdr.sh_name;
261 else if (shndx == SHN_ABS) {
264 else if (shndx == SHN_COMMON) {
270 static const char *sym_name(const char *sym_strtab, Elf_Sym *sym)
275 name = sym_strtab + sym->st_name;
278 name = sec_name(sym_index(sym));
283 static Elf_Sym *sym_lookup(const char *symname)
286 for (i = 0; i < shnum; i++) {
287 struct section *sec = &secs[i];
293 if (sec->shdr.sh_type != SHT_SYMTAB)
296 nsyms = sec->shdr.sh_size/sizeof(Elf_Sym);
297 symtab = sec->symtab;
298 strtab = sec->link->strtab;
300 for (sym = symtab; --nsyms >= 0; sym++) {
303 if (strcmp(symname, strtab + sym->st_name) == 0)
310 #if BYTE_ORDER == LITTLE_ENDIAN
311 #define le16_to_cpu(val) (val)
312 #define le32_to_cpu(val) (val)
313 #define le64_to_cpu(val) (val)
315 #if BYTE_ORDER == BIG_ENDIAN
316 #define le16_to_cpu(val) bswap_16(val)
317 #define le32_to_cpu(val) bswap_32(val)
318 #define le64_to_cpu(val) bswap_64(val)
321 static uint16_t elf16_to_cpu(uint16_t val)
323 return le16_to_cpu(val);
326 static uint32_t elf32_to_cpu(uint32_t val)
328 return le32_to_cpu(val);
331 #define elf_half_to_cpu(x) elf16_to_cpu(x)
332 #define elf_word_to_cpu(x) elf32_to_cpu(x)
335 static uint64_t elf64_to_cpu(uint64_t val)
337 return le64_to_cpu(val);
339 #define elf_addr_to_cpu(x) elf64_to_cpu(x)
340 #define elf_off_to_cpu(x) elf64_to_cpu(x)
341 #define elf_xword_to_cpu(x) elf64_to_cpu(x)
343 #define elf_addr_to_cpu(x) elf32_to_cpu(x)
344 #define elf_off_to_cpu(x) elf32_to_cpu(x)
345 #define elf_xword_to_cpu(x) elf32_to_cpu(x)
348 static int sym_index(Elf_Sym *sym)
350 Elf_Sym *symtab = secs[shsymtabndx].symtab;
351 Elf32_Word *xsymtab = secs[shxsymtabndx].xsymtab;
352 unsigned long offset;
355 if (sym->st_shndx != SHN_XINDEX)
356 return sym->st_shndx;
358 /* calculate offset of sym from head of table. */
359 offset = (unsigned long)sym - (unsigned long)symtab;
360 index = offset / sizeof(*sym);
362 return elf32_to_cpu(xsymtab[index]);
365 static void read_ehdr(FILE *fp)
367 if (fread(&ehdr, sizeof(ehdr), 1, fp) != 1) {
368 die("Cannot read ELF header: %s\n",
371 if (memcmp(ehdr.e_ident, ELFMAG, SELFMAG) != 0) {
372 die("No ELF magic\n");
374 if (ehdr.e_ident[EI_CLASS] != ELF_CLASS) {
375 die("Not a %d bit executable\n", ELF_BITS);
377 if (ehdr.e_ident[EI_DATA] != ELFDATA2LSB) {
378 die("Not a LSB ELF executable\n");
380 if (ehdr.e_ident[EI_VERSION] != EV_CURRENT) {
381 die("Unknown ELF version\n");
383 /* Convert the fields to native endian */
384 ehdr.e_type = elf_half_to_cpu(ehdr.e_type);
385 ehdr.e_machine = elf_half_to_cpu(ehdr.e_machine);
386 ehdr.e_version = elf_word_to_cpu(ehdr.e_version);
387 ehdr.e_entry = elf_addr_to_cpu(ehdr.e_entry);
388 ehdr.e_phoff = elf_off_to_cpu(ehdr.e_phoff);
389 ehdr.e_shoff = elf_off_to_cpu(ehdr.e_shoff);
390 ehdr.e_flags = elf_word_to_cpu(ehdr.e_flags);
391 ehdr.e_ehsize = elf_half_to_cpu(ehdr.e_ehsize);
392 ehdr.e_phentsize = elf_half_to_cpu(ehdr.e_phentsize);
393 ehdr.e_phnum = elf_half_to_cpu(ehdr.e_phnum);
394 ehdr.e_shentsize = elf_half_to_cpu(ehdr.e_shentsize);
395 ehdr.e_shnum = elf_half_to_cpu(ehdr.e_shnum);
396 ehdr.e_shstrndx = elf_half_to_cpu(ehdr.e_shstrndx);
398 shnum = ehdr.e_shnum;
399 shstrndx = ehdr.e_shstrndx;
401 if ((ehdr.e_type != ET_EXEC) && (ehdr.e_type != ET_DYN))
402 die("Unsupported ELF header type\n");
403 if (ehdr.e_machine != ELF_MACHINE)
404 die("Not for %s\n", ELF_MACHINE_NAME);
405 if (ehdr.e_version != EV_CURRENT)
406 die("Unknown ELF version\n");
407 if (ehdr.e_ehsize != sizeof(Elf_Ehdr))
408 die("Bad Elf header size\n");
409 if (ehdr.e_phentsize != sizeof(Elf_Phdr))
410 die("Bad program header entry\n");
411 if (ehdr.e_shentsize != sizeof(Elf_Shdr))
412 die("Bad section header entry\n");
415 if (shnum == SHN_UNDEF || shstrndx == SHN_XINDEX) {
418 if (fseek(fp, ehdr.e_shoff, SEEK_SET) < 0)
419 die("Seek to %" FMT " failed: %s\n", ehdr.e_shoff, strerror(errno));
421 if (fread(&shdr, sizeof(shdr), 1, fp) != 1)
422 die("Cannot read initial ELF section header: %s\n", strerror(errno));
424 if (shnum == SHN_UNDEF)
425 shnum = elf_xword_to_cpu(shdr.sh_size);
427 if (shstrndx == SHN_XINDEX)
428 shstrndx = elf_word_to_cpu(shdr.sh_link);
431 if (shstrndx >= shnum)
432 die("String table index out of bounds\n");
435 static void read_shdrs(FILE *fp)
440 secs = calloc(shnum, sizeof(struct section));
442 die("Unable to allocate %ld section headers\n",
445 if (fseek(fp, ehdr.e_shoff, SEEK_SET) < 0) {
446 die("Seek to %" FMT " failed: %s\n",
447 ehdr.e_shoff, strerror(errno));
449 for (i = 0; i < shnum; i++) {
450 struct section *sec = &secs[i];
451 if (fread(&shdr, sizeof(shdr), 1, fp) != 1)
452 die("Cannot read ELF section headers %d/%ld: %s\n",
453 i, shnum, strerror(errno));
454 sec->shdr.sh_name = elf_word_to_cpu(shdr.sh_name);
455 sec->shdr.sh_type = elf_word_to_cpu(shdr.sh_type);
456 sec->shdr.sh_flags = elf_xword_to_cpu(shdr.sh_flags);
457 sec->shdr.sh_addr = elf_addr_to_cpu(shdr.sh_addr);
458 sec->shdr.sh_offset = elf_off_to_cpu(shdr.sh_offset);
459 sec->shdr.sh_size = elf_xword_to_cpu(shdr.sh_size);
460 sec->shdr.sh_link = elf_word_to_cpu(shdr.sh_link);
461 sec->shdr.sh_info = elf_word_to_cpu(shdr.sh_info);
462 sec->shdr.sh_addralign = elf_xword_to_cpu(shdr.sh_addralign);
463 sec->shdr.sh_entsize = elf_xword_to_cpu(shdr.sh_entsize);
464 if (sec->shdr.sh_link < shnum)
465 sec->link = &secs[sec->shdr.sh_link];
470 static void read_strtabs(FILE *fp)
473 for (i = 0; i < shnum; i++) {
474 struct section *sec = &secs[i];
475 if (sec->shdr.sh_type != SHT_STRTAB) {
478 sec->strtab = malloc(sec->shdr.sh_size);
480 die("malloc of %" FMT " bytes for strtab failed\n",
483 if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0) {
484 die("Seek to %" FMT " failed: %s\n",
485 sec->shdr.sh_offset, strerror(errno));
487 if (fread(sec->strtab, 1, sec->shdr.sh_size, fp)
488 != sec->shdr.sh_size) {
489 die("Cannot read symbol table: %s\n",
495 static void read_symtabs(FILE *fp)
499 for (i = 0; i < shnum; i++) {
500 struct section *sec = &secs[i];
503 switch (sec->shdr.sh_type) {
504 case SHT_SYMTAB_SHNDX:
505 sec->xsymtab = malloc(sec->shdr.sh_size);
507 die("malloc of %" FMT " bytes for xsymtab failed\n",
510 if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0) {
511 die("Seek to %" FMT " failed: %s\n",
512 sec->shdr.sh_offset, strerror(errno));
514 if (fread(sec->xsymtab, 1, sec->shdr.sh_size, fp)
515 != sec->shdr.sh_size) {
516 die("Cannot read extended symbol table: %s\n",
523 num_syms = sec->shdr.sh_size / sizeof(Elf_Sym);
525 sec->symtab = malloc(sec->shdr.sh_size);
527 die("malloc of %" FMT " bytes for symtab failed\n",
530 if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0) {
531 die("Seek to %" FMT " failed: %s\n",
532 sec->shdr.sh_offset, strerror(errno));
534 if (fread(sec->symtab, 1, sec->shdr.sh_size, fp)
535 != sec->shdr.sh_size) {
536 die("Cannot read symbol table: %s\n",
539 for (j = 0; j < num_syms; j++) {
540 Elf_Sym *sym = &sec->symtab[j];
542 sym->st_name = elf_word_to_cpu(sym->st_name);
543 sym->st_value = elf_addr_to_cpu(sym->st_value);
544 sym->st_size = elf_xword_to_cpu(sym->st_size);
545 sym->st_shndx = elf_half_to_cpu(sym->st_shndx);
557 static void read_relocs(FILE *fp)
560 for (i = 0; i < shnum; i++) {
561 struct section *sec = &secs[i];
562 if (sec->shdr.sh_type != SHT_REL_TYPE) {
565 sec->reltab = malloc(sec->shdr.sh_size);
567 die("malloc of %" FMT " bytes for relocs failed\n",
570 if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0) {
571 die("Seek to %" FMT " failed: %s\n",
572 sec->shdr.sh_offset, strerror(errno));
574 if (fread(sec->reltab, 1, sec->shdr.sh_size, fp)
575 != sec->shdr.sh_size) {
576 die("Cannot read symbol table: %s\n",
579 for (j = 0; j < sec->shdr.sh_size/sizeof(Elf_Rel); j++) {
580 Elf_Rel *rel = &sec->reltab[j];
581 rel->r_offset = elf_addr_to_cpu(rel->r_offset);
582 rel->r_info = elf_xword_to_cpu(rel->r_info);
583 #if (SHT_REL_TYPE == SHT_RELA)
584 rel->r_addend = elf_xword_to_cpu(rel->r_addend);
591 static void print_absolute_symbols(void)
597 format = "%5d %016"PRIx64" %5"PRId64" %10s %10s %12s %s\n";
599 format = "%5d %08"PRIx32" %5"PRId32" %10s %10s %12s %s\n";
601 printf("Absolute symbols\n");
602 printf(" Num: Value Size Type Bind Visibility Name\n");
603 for (i = 0; i < shnum; i++) {
604 struct section *sec = &secs[i];
608 if (sec->shdr.sh_type != SHT_SYMTAB) {
611 sym_strtab = sec->link->strtab;
612 for (j = 0; j < sec->shdr.sh_size/sizeof(Elf_Sym); j++) {
615 sym = &sec->symtab[j];
616 name = sym_name(sym_strtab, sym);
617 if (sym->st_shndx != SHN_ABS) {
621 j, sym->st_value, sym->st_size,
622 sym_type(ELF_ST_TYPE(sym->st_info)),
623 sym_bind(ELF_ST_BIND(sym->st_info)),
624 sym_visibility(ELF_ST_VISIBILITY(sym->st_other)),
631 static void print_absolute_relocs(void)
637 format = "%016"PRIx64" %016"PRIx64" %10s %016"PRIx64" %s\n";
639 format = "%08"PRIx32" %08"PRIx32" %10s %08"PRIx32" %s\n";
641 for (i = 0; i < shnum; i++) {
642 struct section *sec = &secs[i];
643 struct section *sec_applies, *sec_symtab;
647 if (sec->shdr.sh_type != SHT_REL_TYPE) {
650 sec_symtab = sec->link;
651 sec_applies = &secs[sec->shdr.sh_info];
652 if (!(sec_applies->shdr.sh_flags & SHF_ALLOC)) {
655 sh_symtab = sec_symtab->symtab;
656 sym_strtab = sec_symtab->link->strtab;
657 for (j = 0; j < sec->shdr.sh_size/sizeof(Elf_Rel); j++) {
661 rel = &sec->reltab[j];
662 sym = &sh_symtab[ELF_R_SYM(rel->r_info)];
663 name = sym_name(sym_strtab, sym);
664 if (sym->st_shndx != SHN_ABS) {
668 /* Absolute symbols are not relocated if bzImage is
669 * loaded at a non-compiled address. Display a warning
670 * to user at compile time about the absolute
671 * relocations present.
673 * User need to audit the code to make sure
674 * some symbols which should have been section
675 * relative have not become absolute because of some
676 * linker optimization or wrong programming usage.
678 * Before warning check if this absolute symbol
679 * relocation is harmless.
681 if (is_reloc(S_ABS, name) || is_reloc(S_REL, name))
685 printf("WARNING: Absolute relocations"
687 printf("Offset Info Type Sym.Value "
695 rel_type(ELF_R_TYPE(rel->r_info)),
705 static void add_reloc(struct relocs *r, uint32_t offset)
707 if (r->count == r->size) {
708 unsigned long newsize = r->size + 50000;
709 void *mem = realloc(r->offset, newsize * sizeof(r->offset[0]));
712 die("realloc of %ld entries for relocs failed\n",
717 r->offset[r->count++] = offset;
720 static void walk_relocs(int (*process)(struct section *sec, Elf_Rel *rel,
721 Elf_Sym *sym, const char *symname))
724 /* Walk through the relocations */
725 for (i = 0; i < shnum; i++) {
728 struct section *sec_applies, *sec_symtab;
730 struct section *sec = &secs[i];
732 if (sec->shdr.sh_type != SHT_REL_TYPE) {
735 sec_symtab = sec->link;
736 sec_applies = &secs[sec->shdr.sh_info];
737 if (!(sec_applies->shdr.sh_flags & SHF_ALLOC)) {
740 sh_symtab = sec_symtab->symtab;
741 sym_strtab = sec_symtab->link->strtab;
742 for (j = 0; j < sec->shdr.sh_size/sizeof(Elf_Rel); j++) {
743 Elf_Rel *rel = &sec->reltab[j];
744 Elf_Sym *sym = &sh_symtab[ELF_R_SYM(rel->r_info)];
745 const char *symname = sym_name(sym_strtab, sym);
747 process(sec, rel, sym, symname);
753 * The .data..percpu section is a special case for x86_64 SMP kernels.
754 * It is used to initialize the actual per_cpu areas and to provide
755 * definitions for the per_cpu variables that correspond to their offsets
756 * within the percpu area. Since the values of all of the symbols need
757 * to be offsets from the start of the per_cpu area the virtual address
758 * (sh_addr) of .data..percpu is 0 in SMP kernels.
762 * Relocations that reference symbols in the per_cpu area do not
763 * need further relocation (since the value is an offset relative
764 * to the start of the per_cpu area that does not change).
766 * Relocations that apply to the per_cpu area need to have their
767 * offset adjusted by by the value of __per_cpu_load to make them
768 * point to the correct place in the loaded image (because the
769 * virtual address of .data..percpu is 0).
771 * For non SMP kernels .data..percpu is linked as part of the normal
772 * kernel data and does not require special treatment.
775 static int per_cpu_shndx = -1;
776 static Elf_Addr per_cpu_load_addr;
778 static void percpu_init(void)
781 for (i = 0; i < shnum; i++) {
783 if (strcmp(sec_name(i), ".data..percpu"))
786 if (secs[i].shdr.sh_addr != 0) /* non SMP kernel */
789 sym = sym_lookup("__per_cpu_load");
791 die("can't find __per_cpu_load\n");
794 per_cpu_load_addr = sym->st_value;
802 * Check to see if a symbol lies in the .data..percpu section.
804 * The linker incorrectly associates some symbols with the
805 * .data..percpu section so we also need to check the symbol
806 * name to make sure that we classify the symbol correctly.
808 * The GNU linker incorrectly associates:
812 * The "gold" linker incorrectly associates:
813 * init_per_cpu__fixed_percpu_data
814 * init_per_cpu__gdt_page
816 static int is_percpu_sym(ElfW(Sym) *sym, const char *symname)
818 int shndx = sym_index(sym);
820 return (shndx == per_cpu_shndx) &&
821 strcmp(symname, "__init_begin") &&
822 strcmp(symname, "__per_cpu_load") &&
823 strncmp(symname, "init_per_cpu_", 13);
827 static int do_reloc64(struct section *sec, Elf_Rel *rel, ElfW(Sym) *sym,
830 unsigned r_type = ELF64_R_TYPE(rel->r_info);
831 ElfW(Addr) offset = rel->r_offset;
832 int shn_abs = (sym->st_shndx == SHN_ABS) && !is_reloc(S_REL, symname);
834 if (sym->st_shndx == SHN_UNDEF)
838 * Adjust the offset if this reloc applies to the percpu section.
840 if (sec->shdr.sh_info == per_cpu_shndx)
841 offset += per_cpu_load_addr;
845 /* NONE can be ignored. */
851 * PC relative relocations don't need to be adjusted unless
852 * referencing a percpu symbol.
854 * NB: R_X86_64_PLT32 can be treated as R_X86_64_PC32.
856 if (is_percpu_sym(sym, symname))
857 add_reloc(&relocs32neg, offset);
862 * Only used by jump labels
864 if (is_percpu_sym(sym, symname))
865 die("Invalid R_X86_64_PC64 relocation against per-CPU symbol %s\n",
873 * References to the percpu area don't need to be adjusted.
875 if (is_percpu_sym(sym, symname))
880 * Whitelisted absolute symbols do not require
883 if (is_reloc(S_ABS, symname))
886 die("Invalid absolute %s relocation: %s\n",
887 rel_type(r_type), symname);
892 * Relocation offsets for 64 bit kernels are output
893 * as 32 bits and sign extended back to 64 bits when
894 * the relocations are processed.
895 * Make sure that the offset will fit.
897 if ((int32_t)offset != (int64_t)offset)
898 die("Relocation offset doesn't fit in 32 bits\n");
900 if (r_type == R_X86_64_64)
901 add_reloc(&relocs64, offset);
903 add_reloc(&relocs32, offset);
907 die("Unsupported relocation type: %s (%d)\n",
908 rel_type(r_type), r_type);
917 static int do_reloc32(struct section *sec, Elf_Rel *rel, Elf_Sym *sym,
920 unsigned r_type = ELF32_R_TYPE(rel->r_info);
921 int shn_abs = (sym->st_shndx == SHN_ABS) && !is_reloc(S_REL, symname);
930 * NONE can be ignored and PC relative relocations don't need
931 * to be adjusted. Because sym must be defined, R_386_PLT32 can
932 * be treated the same way as R_386_PC32.
939 * Whitelisted absolute symbols do not require
942 if (is_reloc(S_ABS, symname))
945 die("Invalid absolute %s relocation: %s\n",
946 rel_type(r_type), symname);
950 add_reloc(&relocs32, rel->r_offset);
954 die("Unsupported relocation type: %s (%d)\n",
955 rel_type(r_type), r_type);
962 static int do_reloc_real(struct section *sec, Elf_Rel *rel, Elf_Sym *sym,
965 unsigned r_type = ELF32_R_TYPE(rel->r_info);
966 int shn_abs = (sym->st_shndx == SHN_ABS) && !is_reloc(S_REL, symname);
975 * NONE can be ignored and PC relative relocations don't need
976 * to be adjusted. Because sym must be defined, R_386_PLT32 can
977 * be treated the same way as R_386_PC32.
984 * Whitelisted absolute symbols do not require
987 if (is_reloc(S_ABS, symname))
990 if (is_reloc(S_SEG, symname)) {
991 add_reloc(&relocs16, rel->r_offset);
995 if (!is_reloc(S_LIN, symname))
998 die("Invalid %s %s relocation: %s\n",
999 shn_abs ? "absolute" : "relative",
1000 rel_type(r_type), symname);
1006 * Whitelisted absolute symbols do not require
1009 if (is_reloc(S_ABS, symname))
1012 if (is_reloc(S_REL, symname)) {
1013 add_reloc(&relocs32, rel->r_offset);
1017 if (is_reloc(S_LIN, symname))
1018 add_reloc(&relocs32, rel->r_offset);
1021 die("Invalid %s %s relocation: %s\n",
1022 shn_abs ? "absolute" : "relative",
1023 rel_type(r_type), symname);
1027 die("Unsupported relocation type: %s (%d)\n",
1028 rel_type(r_type), r_type);
1037 static int cmp_relocs(const void *va, const void *vb)
1039 const uint32_t *a, *b;
1041 return (*a == *b)? 0 : (*a > *b)? 1 : -1;
1044 static void sort_relocs(struct relocs *r)
1046 qsort(r->offset, r->count, sizeof(r->offset[0]), cmp_relocs);
1049 static int write32(uint32_t v, FILE *f)
1051 unsigned char buf[4];
1053 put_unaligned_le32(v, buf);
1054 return fwrite(buf, 1, 4, f) == 4 ? 0 : -1;
1057 static int write32_as_text(uint32_t v, FILE *f)
1059 return fprintf(f, "\t.long 0x%08"PRIx32"\n", v) > 0 ? 0 : -1;
1062 static void emit_relocs(int as_text, int use_real_mode)
1065 int (*write_reloc)(uint32_t, FILE *) = write32;
1066 int (*do_reloc)(struct section *sec, Elf_Rel *rel, Elf_Sym *sym,
1067 const char *symname);
1071 do_reloc = do_reloc64;
1073 die("--realmode not valid for a 64-bit ELF file");
1076 do_reloc = do_reloc32;
1078 do_reloc = do_reloc_real;
1081 /* Collect up the relocations */
1082 walk_relocs(do_reloc);
1084 if (relocs16.count && !use_real_mode)
1085 die("Segment relocations found but --realmode not specified\n");
1087 /* Order the relocations for more efficient processing */
1088 sort_relocs(&relocs32);
1090 sort_relocs(&relocs32neg);
1091 sort_relocs(&relocs64);
1093 sort_relocs(&relocs16);
1096 /* Print the relocations */
1098 /* Print the relocations in a form suitable that
1101 printf(".section \".data.reloc\",\"a\"\n");
1102 printf(".balign 4\n");
1103 write_reloc = write32_as_text;
1106 if (use_real_mode) {
1107 write_reloc(relocs16.count, stdout);
1108 for (i = 0; i < relocs16.count; i++)
1109 write_reloc(relocs16.offset[i], stdout);
1111 write_reloc(relocs32.count, stdout);
1112 for (i = 0; i < relocs32.count; i++)
1113 write_reloc(relocs32.offset[i], stdout);
1117 write_reloc(0, stdout);
1119 /* Now print each relocation */
1120 for (i = 0; i < relocs64.count; i++)
1121 write_reloc(relocs64.offset[i], stdout);
1124 write_reloc(0, stdout);
1126 /* Now print each inverse 32-bit relocation */
1127 for (i = 0; i < relocs32neg.count; i++)
1128 write_reloc(relocs32neg.offset[i], stdout);
1132 write_reloc(0, stdout);
1134 /* Now print each relocation */
1135 for (i = 0; i < relocs32.count; i++)
1136 write_reloc(relocs32.offset[i], stdout);
1141 * As an aid to debugging problems with different linkers
1142 * print summary information about the relocs.
1143 * Since different linkers tend to emit the sections in
1144 * different orders we use the section names in the output.
1146 static int do_reloc_info(struct section *sec, Elf_Rel *rel, ElfW(Sym) *sym,
1147 const char *symname)
1149 printf("%s\t%s\t%s\t%s\n",
1150 sec_name(sec->shdr.sh_info),
1151 rel_type(ELF_R_TYPE(rel->r_info)),
1153 sec_name(sym_index(sym)));
1157 static void print_reloc_info(void)
1159 printf("reloc section\treloc type\tsymbol\tsymbol section\n");
1160 walk_relocs(do_reloc_info);
1164 # define process process_64
1166 # define process process_32
1169 void process(FILE *fp, int use_real_mode, int as_text,
1170 int show_absolute_syms, int show_absolute_relocs,
1171 int show_reloc_info)
1173 regex_init(use_real_mode);
1181 if (show_absolute_syms) {
1182 print_absolute_symbols();
1185 if (show_absolute_relocs) {
1186 print_absolute_relocs();
1189 if (show_reloc_info) {
1193 emit_relocs(as_text, use_real_mode);