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
;
24 static struct relocs relocs16
;
25 static struct relocs relocs32
;
27 static struct relocs relocs32neg
;
28 static struct relocs relocs64
;
41 static struct section
*secs
;
43 static const char * const sym_regex_kernel
[S_NSYMTYPES
] = {
45 * Following symbols have been audited. There values are constant and do
46 * not change if bzImage is loaded at a different physical address than
47 * the address for which it has been compiled. Don't warn user about
48 * absolute relocations present w.r.t these symbols.
51 "^(xen_irq_disable_direct_reloc$|"
52 "xen_save_fl_direct_reloc$|"
57 * These symbols are known to be relative, even if the linker marks them
58 * as absolute (typically defined outside any section in the linker script.)
61 "^(__init_(begin|end)|"
62 "__x86_cpu_dev_(start|end)|"
63 "(__parainstructions|__alt_instructions)(_end)?|"
64 "(__iommu_table|__apicdrivers|__smp_locks)(_end)?|"
65 "__(start|end)_pci_.*|"
66 "__(start|end)_builtin_fw|"
67 "__(start|stop)___ksymtab(_gpl)?|"
68 "__(start|stop)___kcrctab(_gpl)?|"
69 "__(start|stop)___param|"
70 "__(start|stop)___modver|"
71 "__(start|stop)___bug_table|"
72 "__tracedata_(start|end)|"
73 "__(start|stop)_notes|"
75 "__end_rodata_aligned|"
77 "(jiffies|jiffies_64)|"
81 "__end_rodata_hpage_align|"
88 static const char * const sym_regex_realmode
[S_NSYMTYPES
] = {
90 * These symbols are known to be relative, even if the linker marks them
91 * as absolute (typically defined outside any section in the linker script.)
97 * These are 16-bit segment symbols when compiling 16-bit code.
103 * These are offsets belonging to segments, as opposed to linear addresses,
104 * when compiling 16-bit code.
110 static const char * const *sym_regex
;
112 static regex_t sym_regex_c
[S_NSYMTYPES
];
113 static int is_reloc(enum symtype type
, const char *sym_name
)
115 return sym_regex
[type
] &&
116 !regexec(&sym_regex_c
[type
], sym_name
, 0, NULL
, 0);
119 static void regex_init(int use_real_mode
)
126 sym_regex
= sym_regex_realmode
;
128 sym_regex
= sym_regex_kernel
;
130 for (i
= 0; i
< S_NSYMTYPES
; i
++) {
134 err
= regcomp(&sym_regex_c
[i
], sym_regex
[i
],
135 REG_EXTENDED
|REG_NOSUB
);
138 regerror(err
, &sym_regex_c
[i
], errbuf
, sizeof(errbuf
));
144 static const char *sym_type(unsigned type
)
146 static const char *type_name
[] = {
147 #define SYM_TYPE(X) [X] = #X
148 SYM_TYPE(STT_NOTYPE
),
149 SYM_TYPE(STT_OBJECT
),
151 SYM_TYPE(STT_SECTION
),
153 SYM_TYPE(STT_COMMON
),
157 const char *name
= "unknown sym type name";
158 if (type
< ARRAY_SIZE(type_name
)) {
159 name
= type_name
[type
];
164 static const char *sym_bind(unsigned bind
)
166 static const char *bind_name
[] = {
167 #define SYM_BIND(X) [X] = #X
169 SYM_BIND(STB_GLOBAL
),
173 const char *name
= "unknown sym bind name";
174 if (bind
< ARRAY_SIZE(bind_name
)) {
175 name
= bind_name
[bind
];
180 static const char *sym_visibility(unsigned visibility
)
182 static const char *visibility_name
[] = {
183 #define SYM_VISIBILITY(X) [X] = #X
184 SYM_VISIBILITY(STV_DEFAULT
),
185 SYM_VISIBILITY(STV_INTERNAL
),
186 SYM_VISIBILITY(STV_HIDDEN
),
187 SYM_VISIBILITY(STV_PROTECTED
),
188 #undef SYM_VISIBILITY
190 const char *name
= "unknown sym visibility name";
191 if (visibility
< ARRAY_SIZE(visibility_name
)) {
192 name
= visibility_name
[visibility
];
197 static const char *rel_type(unsigned type
)
199 static const char *type_name
[] = {
200 #define REL_TYPE(X) [X] = #X
202 REL_TYPE(R_X86_64_NONE
),
203 REL_TYPE(R_X86_64_64
),
204 REL_TYPE(R_X86_64_PC64
),
205 REL_TYPE(R_X86_64_PC32
),
206 REL_TYPE(R_X86_64_GOT32
),
207 REL_TYPE(R_X86_64_PLT32
),
208 REL_TYPE(R_X86_64_COPY
),
209 REL_TYPE(R_X86_64_GLOB_DAT
),
210 REL_TYPE(R_X86_64_JUMP_SLOT
),
211 REL_TYPE(R_X86_64_RELATIVE
),
212 REL_TYPE(R_X86_64_GOTPCREL
),
213 REL_TYPE(R_X86_64_32
),
214 REL_TYPE(R_X86_64_32S
),
215 REL_TYPE(R_X86_64_16
),
216 REL_TYPE(R_X86_64_PC16
),
217 REL_TYPE(R_X86_64_8
),
218 REL_TYPE(R_X86_64_PC8
),
220 REL_TYPE(R_386_NONE
),
222 REL_TYPE(R_386_PC32
),
223 REL_TYPE(R_386_GOT32
),
224 REL_TYPE(R_386_PLT32
),
225 REL_TYPE(R_386_COPY
),
226 REL_TYPE(R_386_GLOB_DAT
),
227 REL_TYPE(R_386_JMP_SLOT
),
228 REL_TYPE(R_386_RELATIVE
),
229 REL_TYPE(R_386_GOTOFF
),
230 REL_TYPE(R_386_GOTPC
),
234 REL_TYPE(R_386_PC16
),
238 const char *name
= "unknown type rel type name";
239 if (type
< ARRAY_SIZE(type_name
) && type_name
[type
]) {
240 name
= type_name
[type
];
245 static const char *sec_name(unsigned shndx
)
247 const char *sec_strtab
;
249 sec_strtab
= secs
[shstrndx
].strtab
;
252 name
= sec_strtab
+ secs
[shndx
].shdr
.sh_name
;
254 else if (shndx
== SHN_ABS
) {
257 else if (shndx
== SHN_COMMON
) {
263 static const char *sym_name(const char *sym_strtab
, Elf_Sym
*sym
)
268 name
= sym_strtab
+ sym
->st_name
;
271 name
= sec_name(sym
->st_shndx
);
276 static Elf_Sym
*sym_lookup(const char *symname
)
279 for (i
= 0; i
< shnum
; i
++) {
280 struct section
*sec
= &secs
[i
];
286 if (sec
->shdr
.sh_type
!= SHT_SYMTAB
)
289 nsyms
= sec
->shdr
.sh_size
/sizeof(Elf_Sym
);
290 symtab
= sec
->symtab
;
291 strtab
= sec
->link
->strtab
;
293 for (sym
= symtab
; --nsyms
>= 0; sym
++) {
296 if (strcmp(symname
, strtab
+ sym
->st_name
) == 0)
303 #if BYTE_ORDER == LITTLE_ENDIAN
304 #define le16_to_cpu(val) (val)
305 #define le32_to_cpu(val) (val)
306 #define le64_to_cpu(val) (val)
308 #if BYTE_ORDER == BIG_ENDIAN
309 #define le16_to_cpu(val) bswap_16(val)
310 #define le32_to_cpu(val) bswap_32(val)
311 #define le64_to_cpu(val) bswap_64(val)
314 static uint16_t elf16_to_cpu(uint16_t val
)
316 return le16_to_cpu(val
);
319 static uint32_t elf32_to_cpu(uint32_t val
)
321 return le32_to_cpu(val
);
324 #define elf_half_to_cpu(x) elf16_to_cpu(x)
325 #define elf_word_to_cpu(x) elf32_to_cpu(x)
328 static uint64_t elf64_to_cpu(uint64_t val
)
330 return le64_to_cpu(val
);
332 #define elf_addr_to_cpu(x) elf64_to_cpu(x)
333 #define elf_off_to_cpu(x) elf64_to_cpu(x)
334 #define elf_xword_to_cpu(x) elf64_to_cpu(x)
336 #define elf_addr_to_cpu(x) elf32_to_cpu(x)
337 #define elf_off_to_cpu(x) elf32_to_cpu(x)
338 #define elf_xword_to_cpu(x) elf32_to_cpu(x)
341 static void read_ehdr(FILE *fp
)
343 if (fread(&ehdr
, sizeof(ehdr
), 1, fp
) != 1) {
344 die("Cannot read ELF header: %s\n",
347 if (memcmp(ehdr
.e_ident
, ELFMAG
, SELFMAG
) != 0) {
348 die("No ELF magic\n");
350 if (ehdr
.e_ident
[EI_CLASS
] != ELF_CLASS
) {
351 die("Not a %d bit executable\n", ELF_BITS
);
353 if (ehdr
.e_ident
[EI_DATA
] != ELFDATA2LSB
) {
354 die("Not a LSB ELF executable\n");
356 if (ehdr
.e_ident
[EI_VERSION
] != EV_CURRENT
) {
357 die("Unknown ELF version\n");
359 /* Convert the fields to native endian */
360 ehdr
.e_type
= elf_half_to_cpu(ehdr
.e_type
);
361 ehdr
.e_machine
= elf_half_to_cpu(ehdr
.e_machine
);
362 ehdr
.e_version
= elf_word_to_cpu(ehdr
.e_version
);
363 ehdr
.e_entry
= elf_addr_to_cpu(ehdr
.e_entry
);
364 ehdr
.e_phoff
= elf_off_to_cpu(ehdr
.e_phoff
);
365 ehdr
.e_shoff
= elf_off_to_cpu(ehdr
.e_shoff
);
366 ehdr
.e_flags
= elf_word_to_cpu(ehdr
.e_flags
);
367 ehdr
.e_ehsize
= elf_half_to_cpu(ehdr
.e_ehsize
);
368 ehdr
.e_phentsize
= elf_half_to_cpu(ehdr
.e_phentsize
);
369 ehdr
.e_phnum
= elf_half_to_cpu(ehdr
.e_phnum
);
370 ehdr
.e_shentsize
= elf_half_to_cpu(ehdr
.e_shentsize
);
371 ehdr
.e_shnum
= elf_half_to_cpu(ehdr
.e_shnum
);
372 ehdr
.e_shstrndx
= elf_half_to_cpu(ehdr
.e_shstrndx
);
374 shnum
= ehdr
.e_shnum
;
375 shstrndx
= ehdr
.e_shstrndx
;
377 if ((ehdr
.e_type
!= ET_EXEC
) && (ehdr
.e_type
!= ET_DYN
))
378 die("Unsupported ELF header type\n");
379 if (ehdr
.e_machine
!= ELF_MACHINE
)
380 die("Not for %s\n", ELF_MACHINE_NAME
);
381 if (ehdr
.e_version
!= EV_CURRENT
)
382 die("Unknown ELF version\n");
383 if (ehdr
.e_ehsize
!= sizeof(Elf_Ehdr
))
384 die("Bad Elf header size\n");
385 if (ehdr
.e_phentsize
!= sizeof(Elf_Phdr
))
386 die("Bad program header entry\n");
387 if (ehdr
.e_shentsize
!= sizeof(Elf_Shdr
))
388 die("Bad section header entry\n");
391 if (shnum
== SHN_UNDEF
|| shstrndx
== SHN_XINDEX
) {
394 if (fseek(fp
, ehdr
.e_shoff
, SEEK_SET
) < 0)
395 die("Seek to %" FMT
" failed: %s\n", ehdr
.e_shoff
, strerror(errno
));
397 if (fread(&shdr
, sizeof(shdr
), 1, fp
) != 1)
398 die("Cannot read initial ELF section header: %s\n", strerror(errno
));
400 if (shnum
== SHN_UNDEF
)
401 shnum
= elf_xword_to_cpu(shdr
.sh_size
);
403 if (shstrndx
== SHN_XINDEX
)
404 shstrndx
= elf_word_to_cpu(shdr
.sh_link
);
407 if (shstrndx
>= shnum
)
408 die("String table index out of bounds\n");
411 static void read_shdrs(FILE *fp
)
416 secs
= calloc(shnum
, sizeof(struct section
));
418 die("Unable to allocate %ld section headers\n",
421 if (fseek(fp
, ehdr
.e_shoff
, SEEK_SET
) < 0) {
422 die("Seek to %" FMT
" failed: %s\n",
423 ehdr
.e_shoff
, strerror(errno
));
425 for (i
= 0; i
< shnum
; i
++) {
426 struct section
*sec
= &secs
[i
];
427 if (fread(&shdr
, sizeof(shdr
), 1, fp
) != 1)
428 die("Cannot read ELF section headers %d/%ld: %s\n",
429 i
, shnum
, strerror(errno
));
430 sec
->shdr
.sh_name
= elf_word_to_cpu(shdr
.sh_name
);
431 sec
->shdr
.sh_type
= elf_word_to_cpu(shdr
.sh_type
);
432 sec
->shdr
.sh_flags
= elf_xword_to_cpu(shdr
.sh_flags
);
433 sec
->shdr
.sh_addr
= elf_addr_to_cpu(shdr
.sh_addr
);
434 sec
->shdr
.sh_offset
= elf_off_to_cpu(shdr
.sh_offset
);
435 sec
->shdr
.sh_size
= elf_xword_to_cpu(shdr
.sh_size
);
436 sec
->shdr
.sh_link
= elf_word_to_cpu(shdr
.sh_link
);
437 sec
->shdr
.sh_info
= elf_word_to_cpu(shdr
.sh_info
);
438 sec
->shdr
.sh_addralign
= elf_xword_to_cpu(shdr
.sh_addralign
);
439 sec
->shdr
.sh_entsize
= elf_xword_to_cpu(shdr
.sh_entsize
);
440 if (sec
->shdr
.sh_link
< shnum
)
441 sec
->link
= &secs
[sec
->shdr
.sh_link
];
446 static void read_strtabs(FILE *fp
)
449 for (i
= 0; i
< shnum
; i
++) {
450 struct section
*sec
= &secs
[i
];
451 if (sec
->shdr
.sh_type
!= SHT_STRTAB
) {
454 sec
->strtab
= malloc(sec
->shdr
.sh_size
);
456 die("malloc of %" FMT
" bytes for strtab failed\n",
459 if (fseek(fp
, sec
->shdr
.sh_offset
, SEEK_SET
) < 0) {
460 die("Seek to %" FMT
" failed: %s\n",
461 sec
->shdr
.sh_offset
, strerror(errno
));
463 if (fread(sec
->strtab
, 1, sec
->shdr
.sh_size
, fp
)
464 != sec
->shdr
.sh_size
) {
465 die("Cannot read symbol table: %s\n",
471 static void read_symtabs(FILE *fp
)
474 for (i
= 0; i
< shnum
; i
++) {
475 struct section
*sec
= &secs
[i
];
476 if (sec
->shdr
.sh_type
!= SHT_SYMTAB
) {
479 sec
->symtab
= malloc(sec
->shdr
.sh_size
);
481 die("malloc of %" FMT
" bytes for symtab failed\n",
484 if (fseek(fp
, sec
->shdr
.sh_offset
, SEEK_SET
) < 0) {
485 die("Seek to %" FMT
" failed: %s\n",
486 sec
->shdr
.sh_offset
, strerror(errno
));
488 if (fread(sec
->symtab
, 1, sec
->shdr
.sh_size
, fp
)
489 != sec
->shdr
.sh_size
) {
490 die("Cannot read symbol table: %s\n",
493 for (j
= 0; j
< sec
->shdr
.sh_size
/sizeof(Elf_Sym
); j
++) {
494 Elf_Sym
*sym
= &sec
->symtab
[j
];
495 sym
->st_name
= elf_word_to_cpu(sym
->st_name
);
496 sym
->st_value
= elf_addr_to_cpu(sym
->st_value
);
497 sym
->st_size
= elf_xword_to_cpu(sym
->st_size
);
498 sym
->st_shndx
= elf_half_to_cpu(sym
->st_shndx
);
504 static void read_relocs(FILE *fp
)
507 for (i
= 0; i
< shnum
; i
++) {
508 struct section
*sec
= &secs
[i
];
509 if (sec
->shdr
.sh_type
!= SHT_REL_TYPE
) {
512 sec
->reltab
= malloc(sec
->shdr
.sh_size
);
514 die("malloc of %" FMT
" bytes for relocs failed\n",
517 if (fseek(fp
, sec
->shdr
.sh_offset
, SEEK_SET
) < 0) {
518 die("Seek to %" FMT
" failed: %s\n",
519 sec
->shdr
.sh_offset
, strerror(errno
));
521 if (fread(sec
->reltab
, 1, sec
->shdr
.sh_size
, fp
)
522 != sec
->shdr
.sh_size
) {
523 die("Cannot read symbol table: %s\n",
526 for (j
= 0; j
< sec
->shdr
.sh_size
/sizeof(Elf_Rel
); j
++) {
527 Elf_Rel
*rel
= &sec
->reltab
[j
];
528 rel
->r_offset
= elf_addr_to_cpu(rel
->r_offset
);
529 rel
->r_info
= elf_xword_to_cpu(rel
->r_info
);
530 #if (SHT_REL_TYPE == SHT_RELA)
531 rel
->r_addend
= elf_xword_to_cpu(rel
->r_addend
);
538 static void print_absolute_symbols(void)
544 format
= "%5d %016"PRIx64
" %5"PRId64
" %10s %10s %12s %s\n";
546 format
= "%5d %08"PRIx32
" %5"PRId32
" %10s %10s %12s %s\n";
548 printf("Absolute symbols\n");
549 printf(" Num: Value Size Type Bind Visibility Name\n");
550 for (i
= 0; i
< shnum
; i
++) {
551 struct section
*sec
= &secs
[i
];
555 if (sec
->shdr
.sh_type
!= SHT_SYMTAB
) {
558 sym_strtab
= sec
->link
->strtab
;
559 for (j
= 0; j
< sec
->shdr
.sh_size
/sizeof(Elf_Sym
); j
++) {
562 sym
= &sec
->symtab
[j
];
563 name
= sym_name(sym_strtab
, sym
);
564 if (sym
->st_shndx
!= SHN_ABS
) {
568 j
, sym
->st_value
, sym
->st_size
,
569 sym_type(ELF_ST_TYPE(sym
->st_info
)),
570 sym_bind(ELF_ST_BIND(sym
->st_info
)),
571 sym_visibility(ELF_ST_VISIBILITY(sym
->st_other
)),
578 static void print_absolute_relocs(void)
584 format
= "%016"PRIx64
" %016"PRIx64
" %10s %016"PRIx64
" %s\n";
586 format
= "%08"PRIx32
" %08"PRIx32
" %10s %08"PRIx32
" %s\n";
588 for (i
= 0; i
< shnum
; i
++) {
589 struct section
*sec
= &secs
[i
];
590 struct section
*sec_applies
, *sec_symtab
;
594 if (sec
->shdr
.sh_type
!= SHT_REL_TYPE
) {
597 sec_symtab
= sec
->link
;
598 sec_applies
= &secs
[sec
->shdr
.sh_info
];
599 if (!(sec_applies
->shdr
.sh_flags
& SHF_ALLOC
)) {
602 sh_symtab
= sec_symtab
->symtab
;
603 sym_strtab
= sec_symtab
->link
->strtab
;
604 for (j
= 0; j
< sec
->shdr
.sh_size
/sizeof(Elf_Rel
); j
++) {
608 rel
= &sec
->reltab
[j
];
609 sym
= &sh_symtab
[ELF_R_SYM(rel
->r_info
)];
610 name
= sym_name(sym_strtab
, sym
);
611 if (sym
->st_shndx
!= SHN_ABS
) {
615 /* Absolute symbols are not relocated if bzImage is
616 * loaded at a non-compiled address. Display a warning
617 * to user at compile time about the absolute
618 * relocations present.
620 * User need to audit the code to make sure
621 * some symbols which should have been section
622 * relative have not become absolute because of some
623 * linker optimization or wrong programming usage.
625 * Before warning check if this absolute symbol
626 * relocation is harmless.
628 if (is_reloc(S_ABS
, name
) || is_reloc(S_REL
, name
))
632 printf("WARNING: Absolute relocations"
634 printf("Offset Info Type Sym.Value "
642 rel_type(ELF_R_TYPE(rel
->r_info
)),
652 static void add_reloc(struct relocs
*r
, uint32_t offset
)
654 if (r
->count
== r
->size
) {
655 unsigned long newsize
= r
->size
+ 50000;
656 void *mem
= realloc(r
->offset
, newsize
* sizeof(r
->offset
[0]));
659 die("realloc of %ld entries for relocs failed\n",
664 r
->offset
[r
->count
++] = offset
;
667 static void walk_relocs(int (*process
)(struct section
*sec
, Elf_Rel
*rel
,
668 Elf_Sym
*sym
, const char *symname
))
671 /* Walk through the relocations */
672 for (i
= 0; i
< shnum
; i
++) {
675 struct section
*sec_applies
, *sec_symtab
;
677 struct section
*sec
= &secs
[i
];
679 if (sec
->shdr
.sh_type
!= SHT_REL_TYPE
) {
682 sec_symtab
= sec
->link
;
683 sec_applies
= &secs
[sec
->shdr
.sh_info
];
684 if (!(sec_applies
->shdr
.sh_flags
& SHF_ALLOC
)) {
687 sh_symtab
= sec_symtab
->symtab
;
688 sym_strtab
= sec_symtab
->link
->strtab
;
689 for (j
= 0; j
< sec
->shdr
.sh_size
/sizeof(Elf_Rel
); j
++) {
690 Elf_Rel
*rel
= &sec
->reltab
[j
];
691 Elf_Sym
*sym
= &sh_symtab
[ELF_R_SYM(rel
->r_info
)];
692 const char *symname
= sym_name(sym_strtab
, sym
);
694 process(sec
, rel
, sym
, symname
);
700 * The .data..percpu section is a special case for x86_64 SMP kernels.
701 * It is used to initialize the actual per_cpu areas and to provide
702 * definitions for the per_cpu variables that correspond to their offsets
703 * within the percpu area. Since the values of all of the symbols need
704 * to be offsets from the start of the per_cpu area the virtual address
705 * (sh_addr) of .data..percpu is 0 in SMP kernels.
709 * Relocations that reference symbols in the per_cpu area do not
710 * need further relocation (since the value is an offset relative
711 * to the start of the per_cpu area that does not change).
713 * Relocations that apply to the per_cpu area need to have their
714 * offset adjusted by by the value of __per_cpu_load to make them
715 * point to the correct place in the loaded image (because the
716 * virtual address of .data..percpu is 0).
718 * For non SMP kernels .data..percpu is linked as part of the normal
719 * kernel data and does not require special treatment.
722 static int per_cpu_shndx
= -1;
723 static Elf_Addr per_cpu_load_addr
;
725 static void percpu_init(void)
728 for (i
= 0; i
< shnum
; i
++) {
730 if (strcmp(sec_name(i
), ".data..percpu"))
733 if (secs
[i
].shdr
.sh_addr
!= 0) /* non SMP kernel */
736 sym
= sym_lookup("__per_cpu_load");
738 die("can't find __per_cpu_load\n");
741 per_cpu_load_addr
= sym
->st_value
;
749 * Check to see if a symbol lies in the .data..percpu section.
751 * The linker incorrectly associates some symbols with the
752 * .data..percpu section so we also need to check the symbol
753 * name to make sure that we classify the symbol correctly.
755 * The GNU linker incorrectly associates:
759 * The "gold" linker incorrectly associates:
760 * init_per_cpu__fixed_percpu_data
761 * init_per_cpu__gdt_page
763 static int is_percpu_sym(ElfW(Sym
) *sym
, const char *symname
)
765 return (sym
->st_shndx
== per_cpu_shndx
) &&
766 strcmp(symname
, "__init_begin") &&
767 strcmp(symname
, "__per_cpu_load") &&
768 strncmp(symname
, "init_per_cpu_", 13);
772 static int do_reloc64(struct section
*sec
, Elf_Rel
*rel
, ElfW(Sym
) *sym
,
775 unsigned r_type
= ELF64_R_TYPE(rel
->r_info
);
776 ElfW(Addr
) offset
= rel
->r_offset
;
777 int shn_abs
= (sym
->st_shndx
== SHN_ABS
) && !is_reloc(S_REL
, symname
);
779 if (sym
->st_shndx
== SHN_UNDEF
)
783 * Adjust the offset if this reloc applies to the percpu section.
785 if (sec
->shdr
.sh_info
== per_cpu_shndx
)
786 offset
+= per_cpu_load_addr
;
790 /* NONE can be ignored. */
796 * PC relative relocations don't need to be adjusted unless
797 * referencing a percpu symbol.
799 * NB: R_X86_64_PLT32 can be treated as R_X86_64_PC32.
801 if (is_percpu_sym(sym
, symname
))
802 add_reloc(&relocs32neg
, offset
);
807 * Only used by jump labels
809 if (is_percpu_sym(sym
, symname
))
810 die("Invalid R_X86_64_PC64 relocation against per-CPU symbol %s\n",
818 * References to the percpu area don't need to be adjusted.
820 if (is_percpu_sym(sym
, symname
))
825 * Whitelisted absolute symbols do not require
828 if (is_reloc(S_ABS
, symname
))
831 die("Invalid absolute %s relocation: %s\n",
832 rel_type(r_type
), symname
);
837 * Relocation offsets for 64 bit kernels are output
838 * as 32 bits and sign extended back to 64 bits when
839 * the relocations are processed.
840 * Make sure that the offset will fit.
842 if ((int32_t)offset
!= (int64_t)offset
)
843 die("Relocation offset doesn't fit in 32 bits\n");
845 if (r_type
== R_X86_64_64
)
846 add_reloc(&relocs64
, offset
);
848 add_reloc(&relocs32
, offset
);
852 die("Unsupported relocation type: %s (%d)\n",
853 rel_type(r_type
), r_type
);
862 static int do_reloc32(struct section
*sec
, Elf_Rel
*rel
, Elf_Sym
*sym
,
865 unsigned r_type
= ELF32_R_TYPE(rel
->r_info
);
866 int shn_abs
= (sym
->st_shndx
== SHN_ABS
) && !is_reloc(S_REL
, symname
);
875 * NONE can be ignored and PC relative relocations don't need
876 * to be adjusted. Because sym must be defined, R_386_PLT32 can
877 * be treated the same way as R_386_PC32.
884 * Whitelisted absolute symbols do not require
887 if (is_reloc(S_ABS
, symname
))
890 die("Invalid absolute %s relocation: %s\n",
891 rel_type(r_type
), symname
);
895 add_reloc(&relocs32
, rel
->r_offset
);
899 die("Unsupported relocation type: %s (%d)\n",
900 rel_type(r_type
), r_type
);
907 static int do_reloc_real(struct section
*sec
, Elf_Rel
*rel
, Elf_Sym
*sym
,
910 unsigned r_type
= ELF32_R_TYPE(rel
->r_info
);
911 int shn_abs
= (sym
->st_shndx
== SHN_ABS
) && !is_reloc(S_REL
, symname
);
920 * NONE can be ignored and PC relative relocations don't need
921 * to be adjusted. Because sym must be defined, R_386_PLT32 can
922 * be treated the same way as R_386_PC32.
929 * Whitelisted absolute symbols do not require
932 if (is_reloc(S_ABS
, symname
))
935 if (is_reloc(S_SEG
, symname
)) {
936 add_reloc(&relocs16
, rel
->r_offset
);
940 if (!is_reloc(S_LIN
, symname
))
943 die("Invalid %s %s relocation: %s\n",
944 shn_abs
? "absolute" : "relative",
945 rel_type(r_type
), symname
);
951 * Whitelisted absolute symbols do not require
954 if (is_reloc(S_ABS
, symname
))
957 if (is_reloc(S_REL
, symname
)) {
958 add_reloc(&relocs32
, rel
->r_offset
);
962 if (is_reloc(S_LIN
, symname
))
963 add_reloc(&relocs32
, rel
->r_offset
);
966 die("Invalid %s %s relocation: %s\n",
967 shn_abs
? "absolute" : "relative",
968 rel_type(r_type
), symname
);
972 die("Unsupported relocation type: %s (%d)\n",
973 rel_type(r_type
), r_type
);
982 static int cmp_relocs(const void *va
, const void *vb
)
984 const uint32_t *a
, *b
;
986 return (*a
== *b
)? 0 : (*a
> *b
)? 1 : -1;
989 static void sort_relocs(struct relocs
*r
)
991 qsort(r
->offset
, r
->count
, sizeof(r
->offset
[0]), cmp_relocs
);
994 static int write32(uint32_t v
, FILE *f
)
996 unsigned char buf
[4];
998 put_unaligned_le32(v
, buf
);
999 return fwrite(buf
, 1, 4, f
) == 4 ? 0 : -1;
1002 static int write32_as_text(uint32_t v
, FILE *f
)
1004 return fprintf(f
, "\t.long 0x%08"PRIx32
"\n", v
) > 0 ? 0 : -1;
1007 static void emit_relocs(int as_text
, int use_real_mode
)
1010 int (*write_reloc
)(uint32_t, FILE *) = write32
;
1011 int (*do_reloc
)(struct section
*sec
, Elf_Rel
*rel
, Elf_Sym
*sym
,
1012 const char *symname
);
1016 do_reloc
= do_reloc64
;
1018 die("--realmode not valid for a 64-bit ELF file");
1021 do_reloc
= do_reloc32
;
1023 do_reloc
= do_reloc_real
;
1026 /* Collect up the relocations */
1027 walk_relocs(do_reloc
);
1029 if (relocs16
.count
&& !use_real_mode
)
1030 die("Segment relocations found but --realmode not specified\n");
1032 /* Order the relocations for more efficient processing */
1033 sort_relocs(&relocs32
);
1035 sort_relocs(&relocs32neg
);
1036 sort_relocs(&relocs64
);
1038 sort_relocs(&relocs16
);
1041 /* Print the relocations */
1043 /* Print the relocations in a form suitable that
1046 printf(".section \".data.reloc\",\"a\"\n");
1047 printf(".balign 4\n");
1048 write_reloc
= write32_as_text
;
1051 if (use_real_mode
) {
1052 write_reloc(relocs16
.count
, stdout
);
1053 for (i
= 0; i
< relocs16
.count
; i
++)
1054 write_reloc(relocs16
.offset
[i
], stdout
);
1056 write_reloc(relocs32
.count
, stdout
);
1057 for (i
= 0; i
< relocs32
.count
; i
++)
1058 write_reloc(relocs32
.offset
[i
], stdout
);
1062 write_reloc(0, stdout
);
1064 /* Now print each relocation */
1065 for (i
= 0; i
< relocs64
.count
; i
++)
1066 write_reloc(relocs64
.offset
[i
], stdout
);
1069 write_reloc(0, stdout
);
1071 /* Now print each inverse 32-bit relocation */
1072 for (i
= 0; i
< relocs32neg
.count
; i
++)
1073 write_reloc(relocs32neg
.offset
[i
], stdout
);
1077 write_reloc(0, stdout
);
1079 /* Now print each relocation */
1080 for (i
= 0; i
< relocs32
.count
; i
++)
1081 write_reloc(relocs32
.offset
[i
], stdout
);
1086 * As an aid to debugging problems with different linkers
1087 * print summary information about the relocs.
1088 * Since different linkers tend to emit the sections in
1089 * different orders we use the section names in the output.
1091 static int do_reloc_info(struct section
*sec
, Elf_Rel
*rel
, ElfW(Sym
) *sym
,
1092 const char *symname
)
1094 printf("%s\t%s\t%s\t%s\n",
1095 sec_name(sec
->shdr
.sh_info
),
1096 rel_type(ELF_R_TYPE(rel
->r_info
)),
1098 sec_name(sym
->st_shndx
));
1102 static void print_reloc_info(void)
1104 printf("reloc section\treloc type\tsymbol\tsymbol section\n");
1105 walk_relocs(do_reloc_info
);
1109 # define process process_64
1111 # define process process_32
1114 void process(FILE *fp
, int use_real_mode
, int as_text
,
1115 int show_absolute_syms
, int show_absolute_relocs
,
1116 int show_reloc_info
)
1118 regex_init(use_real_mode
);
1126 if (show_absolute_syms
) {
1127 print_absolute_symbols();
1130 if (show_absolute_relocs
) {
1131 print_absolute_relocs();
1134 if (show_reloc_info
) {
1138 emit_relocs(as_text
, use_real_mode
);