xref: /openbmc/linux/arch/x86/tools/relocs.c (revision 9fa39135)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2c889ba80SH. Peter Anvin /* This is included from relocs_32/64.c */
36520fe55SH. Peter Anvin 
4bf11655cSKees Cook #define ElfW(type)		_ElfW(ELF_BITS, type)
5bf11655cSKees Cook #define _ElfW(bits, type)	__ElfW(bits, type)
6bf11655cSKees Cook #define __ElfW(bits, type)	Elf##bits##_##type
7bf11655cSKees Cook 
8946166afSKees Cook #define Elf_Addr		ElfW(Addr)
9bf11655cSKees Cook #define Elf_Ehdr		ElfW(Ehdr)
10bf11655cSKees Cook #define Elf_Phdr		ElfW(Phdr)
11bf11655cSKees Cook #define Elf_Shdr		ElfW(Shdr)
12bf11655cSKees Cook #define Elf_Sym			ElfW(Sym)
13bf11655cSKees Cook 
14bf11655cSKees Cook static Elf_Ehdr		ehdr;
15f36e7495SArtem Savkov static unsigned long	shnum;
16f36e7495SArtem Savkov static unsigned int	shstrndx;
17a54c401aSKristen Carlson Accardi static unsigned int	shsymtabndx;
18a54c401aSKristen Carlson Accardi static unsigned int	shxsymtabndx;
19a54c401aSKristen Carlson Accardi 
20a54c401aSKristen Carlson Accardi static int sym_index(Elf_Sym *sym);
215d442e63SKees Cook 
225d442e63SKees Cook struct relocs {
235d442e63SKees Cook 	uint32_t	*offset;
245d442e63SKees Cook 	unsigned long	count;
255d442e63SKees Cook 	unsigned long	size;
265d442e63SKees Cook };
275d442e63SKees Cook 
285d442e63SKees Cook static struct relocs relocs16;
295d442e63SKees Cook static struct relocs relocs32;
306d24c5f7SJan Beulich #if ELF_BITS == 64
316d24c5f7SJan Beulich static struct relocs relocs32neg;
32946166afSKees Cook static struct relocs relocs64;
3303dca99eSBorislav Petkov #define FMT PRIu64
3403dca99eSBorislav Petkov #else
3503dca99eSBorislav Petkov #define FMT PRIu32
366d24c5f7SJan Beulich #endif
376520fe55SH. Peter Anvin 
386520fe55SH. Peter Anvin struct section {
39bf11655cSKees Cook 	Elf_Shdr       shdr;
406520fe55SH. Peter Anvin 	struct section *link;
41bf11655cSKees Cook 	Elf_Sym        *symtab;
42a54c401aSKristen Carlson Accardi 	Elf32_Word     *xsymtab;
43bf11655cSKees Cook 	Elf_Rel        *reltab;
446520fe55SH. Peter Anvin 	char           *strtab;
456520fe55SH. Peter Anvin };
466520fe55SH. Peter Anvin static struct section *secs;
476520fe55SH. Peter Anvin 
486520fe55SH. Peter Anvin static const char * const sym_regex_kernel[S_NSYMTYPES] = {
496520fe55SH. Peter Anvin /*
506520fe55SH. Peter Anvin  * Following symbols have been audited. There values are constant and do
516520fe55SH. Peter Anvin  * not change if bzImage is loaded at a different physical address than
526520fe55SH. Peter Anvin  * the address for which it has been compiled. Don't warn user about
536520fe55SH. Peter Anvin  * absolute relocations present w.r.t these symbols.
546520fe55SH. Peter Anvin  */
556520fe55SH. Peter Anvin 	[S_ABS] =
566520fe55SH. Peter Anvin 	"^(xen_irq_disable_direct_reloc$|"
576520fe55SH. Peter Anvin 	"xen_save_fl_direct_reloc$|"
586520fe55SH. Peter Anvin 	"VDSO|"
59ca7e10bfSSami Tolvanen 	"__kcfi_typeid_|"
606520fe55SH. Peter Anvin 	"__crc_)",
616520fe55SH. Peter Anvin 
626520fe55SH. Peter Anvin /*
636520fe55SH. Peter Anvin  * These symbols are known to be relative, even if the linker marks them
646520fe55SH. Peter Anvin  * as absolute (typically defined outside any section in the linker script.)
656520fe55SH. Peter Anvin  */
666520fe55SH. Peter Anvin 	[S_REL] =
67a3e854d9SH. Peter Anvin 	"^(__init_(begin|end)|"
68a3e854d9SH. Peter Anvin 	"__x86_cpu_dev_(start|end)|"
69fa953adfSH. Nikolaus Schaller 	"(__parainstructions|__alt_instructions)(_end)?|"
70fa953adfSH. Nikolaus Schaller 	"(__iommu_table|__apicdrivers|__smp_locks)(_end)?|"
71fd952815SH. Peter Anvin 	"__(start|end)_pci_.*|"
72d6f12f83SLukas Bulwahn #if CONFIG_FW_LOADER
73fd952815SH. Peter Anvin 	"__(start|end)_builtin_fw|"
74c8dcf655SLuis Chamberlain #endif
75fa953adfSH. Nikolaus Schaller 	"__(start|stop)___ksymtab(_gpl)?|"
76fa953adfSH. Nikolaus Schaller 	"__(start|stop)___kcrctab(_gpl)?|"
77fd952815SH. Peter Anvin 	"__(start|stop)___param|"
78fd952815SH. Peter Anvin 	"__(start|stop)___modver|"
79fd952815SH. Peter Anvin 	"__(start|stop)___bug_table|"
80fd952815SH. Peter Anvin 	"__tracedata_(start|end)|"
81fd952815SH. Peter Anvin 	"__(start|stop)_notes|"
82fd952815SH. Peter Anvin 	"__end_rodata|"
83a29dba16SJoerg Roedel 	"__end_rodata_aligned|"
84fd952815SH. Peter Anvin 	"__initramfs_start|"
85ea17e741SH. Peter Anvin 	"(jiffies|jiffies_64)|"
86c889ba80SH. Peter Anvin #if ELF_BITS == 64
87946166afSKees Cook 	"__per_cpu_load|"
88946166afSKees Cook 	"init_per_cpu__.*|"
89946166afSKees Cook 	"__end_rodata_hpage_align|"
90946166afSKees Cook #endif
91d2312e33SStefani Seibold 	"__vvar_page|"
92a3e854d9SH. Peter Anvin 	"_end)$"
936520fe55SH. Peter Anvin };
946520fe55SH. Peter Anvin 
956520fe55SH. Peter Anvin 
966520fe55SH. Peter Anvin static const char * const sym_regex_realmode[S_NSYMTYPES] = {
976520fe55SH. Peter Anvin /*
98f2604c14SJarkko Sakkinen  * These symbols are known to be relative, even if the linker marks them
99f2604c14SJarkko Sakkinen  * as absolute (typically defined outside any section in the linker script.)
100f2604c14SJarkko Sakkinen  */
101f2604c14SJarkko Sakkinen 	[S_REL] =
102f2604c14SJarkko Sakkinen 	"^pa_",
103f2604c14SJarkko Sakkinen 
104f2604c14SJarkko Sakkinen /*
1056520fe55SH. Peter Anvin  * These are 16-bit segment symbols when compiling 16-bit code.
1066520fe55SH. Peter Anvin  */
1076520fe55SH. Peter Anvin 	[S_SEG] =
1086520fe55SH. Peter Anvin 	"^real_mode_seg$",
1096520fe55SH. Peter Anvin 
1106520fe55SH. Peter Anvin /*
1116520fe55SH. Peter Anvin  * These are offsets belonging to segments, as opposed to linear addresses,
1126520fe55SH. Peter Anvin  * when compiling 16-bit code.
1136520fe55SH. Peter Anvin  */
1146520fe55SH. Peter Anvin 	[S_LIN] =
1156520fe55SH. Peter Anvin 	"^pa_",
1166520fe55SH. Peter Anvin };
1176520fe55SH. Peter Anvin 
1186520fe55SH. Peter Anvin static const char * const *sym_regex;
1196520fe55SH. Peter Anvin 
1206520fe55SH. Peter Anvin static regex_t sym_regex_c[S_NSYMTYPES];
is_reloc(enum symtype type,const char * sym_name)1216520fe55SH. Peter Anvin static int is_reloc(enum symtype type, const char *sym_name)
1226520fe55SH. Peter Anvin {
1236520fe55SH. Peter Anvin 	return sym_regex[type] &&
1246520fe55SH. Peter Anvin 		!regexec(&sym_regex_c[type], sym_name, 0, NULL, 0);
1256520fe55SH. Peter Anvin }
1266520fe55SH. Peter Anvin 
regex_init(int use_real_mode)1276520fe55SH. Peter Anvin static void regex_init(int use_real_mode)
1286520fe55SH. Peter Anvin {
1296520fe55SH. Peter Anvin         char errbuf[128];
1306520fe55SH. Peter Anvin         int err;
1316520fe55SH. Peter Anvin 	int i;
1326520fe55SH. Peter Anvin 
1336520fe55SH. Peter Anvin 	if (use_real_mode)
1346520fe55SH. Peter Anvin 		sym_regex = sym_regex_realmode;
1356520fe55SH. Peter Anvin 	else
1366520fe55SH. Peter Anvin 		sym_regex = sym_regex_kernel;
1376520fe55SH. Peter Anvin 
1386520fe55SH. Peter Anvin 	for (i = 0; i < S_NSYMTYPES; i++) {
1396520fe55SH. Peter Anvin 		if (!sym_regex[i])
1406520fe55SH. Peter Anvin 			continue;
1416520fe55SH. Peter Anvin 
1426520fe55SH. Peter Anvin 		err = regcomp(&sym_regex_c[i], sym_regex[i],
1436520fe55SH. Peter Anvin 			      REG_EXTENDED|REG_NOSUB);
1446520fe55SH. Peter Anvin 
1456520fe55SH. Peter Anvin 		if (err) {
1460e96f31eSJordan Borgner 			regerror(err, &sym_regex_c[i], errbuf, sizeof(errbuf));
1476520fe55SH. Peter Anvin 			die("%s", errbuf);
1486520fe55SH. Peter Anvin 		}
1496520fe55SH. Peter Anvin         }
1506520fe55SH. Peter Anvin }
1516520fe55SH. Peter Anvin 
sym_type(unsigned type)1526520fe55SH. Peter Anvin static const char *sym_type(unsigned type)
1536520fe55SH. Peter Anvin {
1546520fe55SH. Peter Anvin 	static const char *type_name[] = {
1556520fe55SH. Peter Anvin #define SYM_TYPE(X) [X] = #X
1566520fe55SH. Peter Anvin 		SYM_TYPE(STT_NOTYPE),
1576520fe55SH. Peter Anvin 		SYM_TYPE(STT_OBJECT),
1586520fe55SH. Peter Anvin 		SYM_TYPE(STT_FUNC),
1596520fe55SH. Peter Anvin 		SYM_TYPE(STT_SECTION),
1606520fe55SH. Peter Anvin 		SYM_TYPE(STT_FILE),
1616520fe55SH. Peter Anvin 		SYM_TYPE(STT_COMMON),
1626520fe55SH. Peter Anvin 		SYM_TYPE(STT_TLS),
1636520fe55SH. Peter Anvin #undef SYM_TYPE
1646520fe55SH. Peter Anvin 	};
1656520fe55SH. Peter Anvin 	const char *name = "unknown sym type name";
1666520fe55SH. Peter Anvin 	if (type < ARRAY_SIZE(type_name)) {
1676520fe55SH. Peter Anvin 		name = type_name[type];
1686520fe55SH. Peter Anvin 	}
1696520fe55SH. Peter Anvin 	return name;
1706520fe55SH. Peter Anvin }
1716520fe55SH. Peter Anvin 
sym_bind(unsigned bind)1726520fe55SH. Peter Anvin static const char *sym_bind(unsigned bind)
1736520fe55SH. Peter Anvin {
1746520fe55SH. Peter Anvin 	static const char *bind_name[] = {
1756520fe55SH. Peter Anvin #define SYM_BIND(X) [X] = #X
1766520fe55SH. Peter Anvin 		SYM_BIND(STB_LOCAL),
1776520fe55SH. Peter Anvin 		SYM_BIND(STB_GLOBAL),
1786520fe55SH. Peter Anvin 		SYM_BIND(STB_WEAK),
1796520fe55SH. Peter Anvin #undef SYM_BIND
1806520fe55SH. Peter Anvin 	};
1816520fe55SH. Peter Anvin 	const char *name = "unknown sym bind name";
1826520fe55SH. Peter Anvin 	if (bind < ARRAY_SIZE(bind_name)) {
1836520fe55SH. Peter Anvin 		name = bind_name[bind];
1846520fe55SH. Peter Anvin 	}
1856520fe55SH. Peter Anvin 	return name;
1866520fe55SH. Peter Anvin }
1876520fe55SH. Peter Anvin 
sym_visibility(unsigned visibility)1886520fe55SH. Peter Anvin static const char *sym_visibility(unsigned visibility)
1896520fe55SH. Peter Anvin {
1906520fe55SH. Peter Anvin 	static const char *visibility_name[] = {
1916520fe55SH. Peter Anvin #define SYM_VISIBILITY(X) [X] = #X
1926520fe55SH. Peter Anvin 		SYM_VISIBILITY(STV_DEFAULT),
1936520fe55SH. Peter Anvin 		SYM_VISIBILITY(STV_INTERNAL),
1946520fe55SH. Peter Anvin 		SYM_VISIBILITY(STV_HIDDEN),
1956520fe55SH. Peter Anvin 		SYM_VISIBILITY(STV_PROTECTED),
1966520fe55SH. Peter Anvin #undef SYM_VISIBILITY
1976520fe55SH. Peter Anvin 	};
1986520fe55SH. Peter Anvin 	const char *name = "unknown sym visibility name";
1996520fe55SH. Peter Anvin 	if (visibility < ARRAY_SIZE(visibility_name)) {
2006520fe55SH. Peter Anvin 		name = visibility_name[visibility];
2016520fe55SH. Peter Anvin 	}
2026520fe55SH. Peter Anvin 	return name;
2036520fe55SH. Peter Anvin }
2046520fe55SH. Peter Anvin 
rel_type(unsigned type)2056520fe55SH. Peter Anvin static const char *rel_type(unsigned type)
2066520fe55SH. Peter Anvin {
2076520fe55SH. Peter Anvin 	static const char *type_name[] = {
2086520fe55SH. Peter Anvin #define REL_TYPE(X) [X] = #X
209c889ba80SH. Peter Anvin #if ELF_BITS == 64
210946166afSKees Cook 		REL_TYPE(R_X86_64_NONE),
211946166afSKees Cook 		REL_TYPE(R_X86_64_64),
212b40a142bSArd Biesheuvel 		REL_TYPE(R_X86_64_PC64),
213946166afSKees Cook 		REL_TYPE(R_X86_64_PC32),
214946166afSKees Cook 		REL_TYPE(R_X86_64_GOT32),
215946166afSKees Cook 		REL_TYPE(R_X86_64_PLT32),
216946166afSKees Cook 		REL_TYPE(R_X86_64_COPY),
217946166afSKees Cook 		REL_TYPE(R_X86_64_GLOB_DAT),
218946166afSKees Cook 		REL_TYPE(R_X86_64_JUMP_SLOT),
219946166afSKees Cook 		REL_TYPE(R_X86_64_RELATIVE),
220946166afSKees Cook 		REL_TYPE(R_X86_64_GOTPCREL),
221946166afSKees Cook 		REL_TYPE(R_X86_64_32),
222946166afSKees Cook 		REL_TYPE(R_X86_64_32S),
223946166afSKees Cook 		REL_TYPE(R_X86_64_16),
224946166afSKees Cook 		REL_TYPE(R_X86_64_PC16),
225946166afSKees Cook 		REL_TYPE(R_X86_64_8),
226946166afSKees Cook 		REL_TYPE(R_X86_64_PC8),
227946166afSKees Cook #else
2286520fe55SH. Peter Anvin 		REL_TYPE(R_386_NONE),
2296520fe55SH. Peter Anvin 		REL_TYPE(R_386_32),
2306520fe55SH. Peter Anvin 		REL_TYPE(R_386_PC32),
2316520fe55SH. Peter Anvin 		REL_TYPE(R_386_GOT32),
2326520fe55SH. Peter Anvin 		REL_TYPE(R_386_PLT32),
2336520fe55SH. Peter Anvin 		REL_TYPE(R_386_COPY),
2346520fe55SH. Peter Anvin 		REL_TYPE(R_386_GLOB_DAT),
2356520fe55SH. Peter Anvin 		REL_TYPE(R_386_JMP_SLOT),
2366520fe55SH. Peter Anvin 		REL_TYPE(R_386_RELATIVE),
2376520fe55SH. Peter Anvin 		REL_TYPE(R_386_GOTOFF),
2386520fe55SH. Peter Anvin 		REL_TYPE(R_386_GOTPC),
2396520fe55SH. Peter Anvin 		REL_TYPE(R_386_8),
2406520fe55SH. Peter Anvin 		REL_TYPE(R_386_PC8),
2416520fe55SH. Peter Anvin 		REL_TYPE(R_386_16),
2426520fe55SH. Peter Anvin 		REL_TYPE(R_386_PC16),
243946166afSKees Cook #endif
2446520fe55SH. Peter Anvin #undef REL_TYPE
2456520fe55SH. Peter Anvin 	};
2466520fe55SH. Peter Anvin 	const char *name = "unknown type rel type name";
2476520fe55SH. Peter Anvin 	if (type < ARRAY_SIZE(type_name) && type_name[type]) {
2486520fe55SH. Peter Anvin 		name = type_name[type];
2496520fe55SH. Peter Anvin 	}
2506520fe55SH. Peter Anvin 	return name;
2516520fe55SH. Peter Anvin }
2526520fe55SH. Peter Anvin 
sec_name(unsigned shndx)2536520fe55SH. Peter Anvin static const char *sec_name(unsigned shndx)
2546520fe55SH. Peter Anvin {
2556520fe55SH. Peter Anvin 	const char *sec_strtab;
2566520fe55SH. Peter Anvin 	const char *name;
257f36e7495SArtem Savkov 	sec_strtab = secs[shstrndx].strtab;
2586520fe55SH. Peter Anvin 	name = "<noname>";
259f36e7495SArtem Savkov 	if (shndx < shnum) {
2606520fe55SH. Peter Anvin 		name = sec_strtab + secs[shndx].shdr.sh_name;
2616520fe55SH. Peter Anvin 	}
2626520fe55SH. Peter Anvin 	else if (shndx == SHN_ABS) {
2636520fe55SH. Peter Anvin 		name = "ABSOLUTE";
2646520fe55SH. Peter Anvin 	}
2656520fe55SH. Peter Anvin 	else if (shndx == SHN_COMMON) {
2666520fe55SH. Peter Anvin 		name = "COMMON";
2676520fe55SH. Peter Anvin 	}
2686520fe55SH. Peter Anvin 	return name;
2696520fe55SH. Peter Anvin }
2706520fe55SH. Peter Anvin 
sym_name(const char * sym_strtab,Elf_Sym * sym)271bf11655cSKees Cook static const char *sym_name(const char *sym_strtab, Elf_Sym *sym)
2726520fe55SH. Peter Anvin {
2736520fe55SH. Peter Anvin 	const char *name;
2746520fe55SH. Peter Anvin 	name = "<noname>";
2756520fe55SH. Peter Anvin 	if (sym->st_name) {
2766520fe55SH. Peter Anvin 		name = sym_strtab + sym->st_name;
2776520fe55SH. Peter Anvin 	}
2786520fe55SH. Peter Anvin 	else {
279a54c401aSKristen Carlson Accardi 		name = sec_name(sym_index(sym));
2806520fe55SH. Peter Anvin 	}
2816520fe55SH. Peter Anvin 	return name;
2826520fe55SH. Peter Anvin }
2836520fe55SH. Peter Anvin 
sym_lookup(const char * symname)284946166afSKees Cook static Elf_Sym *sym_lookup(const char *symname)
285946166afSKees Cook {
286946166afSKees Cook 	int i;
287f36e7495SArtem Savkov 	for (i = 0; i < shnum; i++) {
288946166afSKees Cook 		struct section *sec = &secs[i];
289946166afSKees Cook 		long nsyms;
290946166afSKees Cook 		char *strtab;
291946166afSKees Cook 		Elf_Sym *symtab;
292946166afSKees Cook 		Elf_Sym *sym;
2936520fe55SH. Peter Anvin 
294946166afSKees Cook 		if (sec->shdr.sh_type != SHT_SYMTAB)
295946166afSKees Cook 			continue;
296946166afSKees Cook 
297946166afSKees Cook 		nsyms = sec->shdr.sh_size/sizeof(Elf_Sym);
298946166afSKees Cook 		symtab = sec->symtab;
299946166afSKees Cook 		strtab = sec->link->strtab;
300946166afSKees Cook 
301946166afSKees Cook 		for (sym = symtab; --nsyms >= 0; sym++) {
302946166afSKees Cook 			if (!sym->st_name)
303946166afSKees Cook 				continue;
304946166afSKees Cook 			if (strcmp(symname, strtab + sym->st_name) == 0)
305946166afSKees Cook 				return sym;
306946166afSKees Cook 		}
307946166afSKees Cook 	}
308946166afSKees Cook 	return 0;
309946166afSKees Cook }
3106520fe55SH. Peter Anvin 
3116520fe55SH. Peter Anvin #if BYTE_ORDER == LITTLE_ENDIAN
3126520fe55SH. Peter Anvin #define le16_to_cpu(val) (val)
3136520fe55SH. Peter Anvin #define le32_to_cpu(val) (val)
314946166afSKees Cook #define le64_to_cpu(val) (val)
3156520fe55SH. Peter Anvin #endif
3166520fe55SH. Peter Anvin #if BYTE_ORDER == BIG_ENDIAN
3176520fe55SH. Peter Anvin #define le16_to_cpu(val) bswap_16(val)
3186520fe55SH. Peter Anvin #define le32_to_cpu(val) bswap_32(val)
319946166afSKees Cook #define le64_to_cpu(val) bswap_64(val)
3206520fe55SH. Peter Anvin #endif
3216520fe55SH. Peter Anvin 
elf16_to_cpu(uint16_t val)3226520fe55SH. Peter Anvin static uint16_t elf16_to_cpu(uint16_t val)
3236520fe55SH. Peter Anvin {
3246520fe55SH. Peter Anvin 	return le16_to_cpu(val);
3256520fe55SH. Peter Anvin }
3266520fe55SH. Peter Anvin 
elf32_to_cpu(uint32_t val)3276520fe55SH. Peter Anvin static uint32_t elf32_to_cpu(uint32_t val)
3286520fe55SH. Peter Anvin {
3296520fe55SH. Peter Anvin 	return le32_to_cpu(val);
3306520fe55SH. Peter Anvin }
3316520fe55SH. Peter Anvin 
332bf11655cSKees Cook #define elf_half_to_cpu(x)	elf16_to_cpu(x)
333bf11655cSKees Cook #define elf_word_to_cpu(x)	elf32_to_cpu(x)
334946166afSKees Cook 
335c889ba80SH. Peter Anvin #if ELF_BITS == 64
elf64_to_cpu(uint64_t val)336946166afSKees Cook static uint64_t elf64_to_cpu(uint64_t val)
337946166afSKees Cook {
338946166afSKees Cook         return le64_to_cpu(val);
339946166afSKees Cook }
340946166afSKees Cook #define elf_addr_to_cpu(x)	elf64_to_cpu(x)
341946166afSKees Cook #define elf_off_to_cpu(x)	elf64_to_cpu(x)
342946166afSKees Cook #define elf_xword_to_cpu(x)	elf64_to_cpu(x)
343946166afSKees Cook #else
344bf11655cSKees Cook #define elf_addr_to_cpu(x)	elf32_to_cpu(x)
345bf11655cSKees Cook #define elf_off_to_cpu(x)	elf32_to_cpu(x)
346bf11655cSKees Cook #define elf_xword_to_cpu(x)	elf32_to_cpu(x)
347946166afSKees Cook #endif
348bf11655cSKees Cook 
sym_index(Elf_Sym * sym)349a54c401aSKristen Carlson Accardi static int sym_index(Elf_Sym *sym)
350a54c401aSKristen Carlson Accardi {
351a54c401aSKristen Carlson Accardi 	Elf_Sym *symtab = secs[shsymtabndx].symtab;
352a54c401aSKristen Carlson Accardi 	Elf32_Word *xsymtab = secs[shxsymtabndx].xsymtab;
353a54c401aSKristen Carlson Accardi 	unsigned long offset;
354a54c401aSKristen Carlson Accardi 	int index;
355a54c401aSKristen Carlson Accardi 
356a54c401aSKristen Carlson Accardi 	if (sym->st_shndx != SHN_XINDEX)
357a54c401aSKristen Carlson Accardi 		return sym->st_shndx;
358a54c401aSKristen Carlson Accardi 
359a54c401aSKristen Carlson Accardi 	/* calculate offset of sym from head of table. */
360a54c401aSKristen Carlson Accardi 	offset = (unsigned long)sym - (unsigned long)symtab;
361a54c401aSKristen Carlson Accardi 	index = offset / sizeof(*sym);
362a54c401aSKristen Carlson Accardi 
363a54c401aSKristen Carlson Accardi 	return elf32_to_cpu(xsymtab[index]);
364a54c401aSKristen Carlson Accardi }
365a54c401aSKristen Carlson Accardi 
read_ehdr(FILE * fp)3666520fe55SH. Peter Anvin static void read_ehdr(FILE *fp)
3676520fe55SH. Peter Anvin {
3686520fe55SH. Peter Anvin 	if (fread(&ehdr, sizeof(ehdr), 1, fp) != 1) {
3696520fe55SH. Peter Anvin 		die("Cannot read ELF header: %s\n",
3706520fe55SH. Peter Anvin 			strerror(errno));
3716520fe55SH. Peter Anvin 	}
3726520fe55SH. Peter Anvin 	if (memcmp(ehdr.e_ident, ELFMAG, SELFMAG) != 0) {
3736520fe55SH. Peter Anvin 		die("No ELF magic\n");
3746520fe55SH. Peter Anvin 	}
375bf11655cSKees Cook 	if (ehdr.e_ident[EI_CLASS] != ELF_CLASS) {
376bf11655cSKees Cook 		die("Not a %d bit executable\n", ELF_BITS);
3776520fe55SH. Peter Anvin 	}
3786520fe55SH. Peter Anvin 	if (ehdr.e_ident[EI_DATA] != ELFDATA2LSB) {
3796520fe55SH. Peter Anvin 		die("Not a LSB ELF executable\n");
3806520fe55SH. Peter Anvin 	}
3816520fe55SH. Peter Anvin 	if (ehdr.e_ident[EI_VERSION] != EV_CURRENT) {
3826520fe55SH. Peter Anvin 		die("Unknown ELF version\n");
3836520fe55SH. Peter Anvin 	}
3846520fe55SH. Peter Anvin 	/* Convert the fields to native endian */
385bf11655cSKees Cook 	ehdr.e_type      = elf_half_to_cpu(ehdr.e_type);
386bf11655cSKees Cook 	ehdr.e_machine   = elf_half_to_cpu(ehdr.e_machine);
387bf11655cSKees Cook 	ehdr.e_version   = elf_word_to_cpu(ehdr.e_version);
388bf11655cSKees Cook 	ehdr.e_entry     = elf_addr_to_cpu(ehdr.e_entry);
389bf11655cSKees Cook 	ehdr.e_phoff     = elf_off_to_cpu(ehdr.e_phoff);
390bf11655cSKees Cook 	ehdr.e_shoff     = elf_off_to_cpu(ehdr.e_shoff);
391bf11655cSKees Cook 	ehdr.e_flags     = elf_word_to_cpu(ehdr.e_flags);
392bf11655cSKees Cook 	ehdr.e_ehsize    = elf_half_to_cpu(ehdr.e_ehsize);
393bf11655cSKees Cook 	ehdr.e_phentsize = elf_half_to_cpu(ehdr.e_phentsize);
394bf11655cSKees Cook 	ehdr.e_phnum     = elf_half_to_cpu(ehdr.e_phnum);
395bf11655cSKees Cook 	ehdr.e_shentsize = elf_half_to_cpu(ehdr.e_shentsize);
396bf11655cSKees Cook 	ehdr.e_shnum     = elf_half_to_cpu(ehdr.e_shnum);
397bf11655cSKees Cook 	ehdr.e_shstrndx  = elf_half_to_cpu(ehdr.e_shstrndx);
3986520fe55SH. Peter Anvin 
399f36e7495SArtem Savkov 	shnum = ehdr.e_shnum;
400f36e7495SArtem Savkov 	shstrndx = ehdr.e_shstrndx;
401f36e7495SArtem Savkov 
402f36e7495SArtem Savkov 	if ((ehdr.e_type != ET_EXEC) && (ehdr.e_type != ET_DYN))
4036520fe55SH. Peter Anvin 		die("Unsupported ELF header type\n");
404f36e7495SArtem Savkov 	if (ehdr.e_machine != ELF_MACHINE)
405bf11655cSKees Cook 		die("Not for %s\n", ELF_MACHINE_NAME);
406f36e7495SArtem Savkov 	if (ehdr.e_version != EV_CURRENT)
4076520fe55SH. Peter Anvin 		die("Unknown ELF version\n");
408f36e7495SArtem Savkov 	if (ehdr.e_ehsize != sizeof(Elf_Ehdr))
40970e79866SAlexey Dobriyan 		die("Bad ELF header size\n");
410f36e7495SArtem Savkov 	if (ehdr.e_phentsize != sizeof(Elf_Phdr))
4116520fe55SH. Peter Anvin 		die("Bad program header entry\n");
412f36e7495SArtem Savkov 	if (ehdr.e_shentsize != sizeof(Elf_Shdr))
4136520fe55SH. Peter Anvin 		die("Bad section header entry\n");
414f36e7495SArtem Savkov 
415f36e7495SArtem Savkov 
416f36e7495SArtem Savkov 	if (shnum == SHN_UNDEF || shstrndx == SHN_XINDEX) {
417f36e7495SArtem Savkov 		Elf_Shdr shdr;
418f36e7495SArtem Savkov 
419f36e7495SArtem Savkov 		if (fseek(fp, ehdr.e_shoff, SEEK_SET) < 0)
42003dca99eSBorislav Petkov 			die("Seek to %" FMT " failed: %s\n", ehdr.e_shoff, strerror(errno));
421f36e7495SArtem Savkov 
422f36e7495SArtem Savkov 		if (fread(&shdr, sizeof(shdr), 1, fp) != 1)
423f36e7495SArtem Savkov 			die("Cannot read initial ELF section header: %s\n", strerror(errno));
424f36e7495SArtem Savkov 
425f36e7495SArtem Savkov 		if (shnum == SHN_UNDEF)
426f36e7495SArtem Savkov 			shnum = elf_xword_to_cpu(shdr.sh_size);
427f36e7495SArtem Savkov 
428f36e7495SArtem Savkov 		if (shstrndx == SHN_XINDEX)
429f36e7495SArtem Savkov 			shstrndx = elf_word_to_cpu(shdr.sh_link);
4306520fe55SH. Peter Anvin 	}
431f36e7495SArtem Savkov 
432f36e7495SArtem Savkov 	if (shstrndx >= shnum)
4336520fe55SH. Peter Anvin 		die("String table index out of bounds\n");
4346520fe55SH. Peter Anvin }
4356520fe55SH. Peter Anvin 
read_shdrs(FILE * fp)4366520fe55SH. Peter Anvin static void read_shdrs(FILE *fp)
4376520fe55SH. Peter Anvin {
4386520fe55SH. Peter Anvin 	int i;
439bf11655cSKees Cook 	Elf_Shdr shdr;
4406520fe55SH. Peter Anvin 
441f36e7495SArtem Savkov 	secs = calloc(shnum, sizeof(struct section));
4426520fe55SH. Peter Anvin 	if (!secs) {
44303dca99eSBorislav Petkov 		die("Unable to allocate %ld section headers\n",
444f36e7495SArtem Savkov 		    shnum);
4456520fe55SH. Peter Anvin 	}
4466520fe55SH. Peter Anvin 	if (fseek(fp, ehdr.e_shoff, SEEK_SET) < 0) {
44703dca99eSBorislav Petkov 		die("Seek to %" FMT " failed: %s\n",
4486520fe55SH. Peter Anvin 		    ehdr.e_shoff, strerror(errno));
4496520fe55SH. Peter Anvin 	}
450f36e7495SArtem Savkov 	for (i = 0; i < shnum; i++) {
4516520fe55SH. Peter Anvin 		struct section *sec = &secs[i];
4520e96f31eSJordan Borgner 		if (fread(&shdr, sizeof(shdr), 1, fp) != 1)
45303dca99eSBorislav Petkov 			die("Cannot read ELF section headers %d/%ld: %s\n",
454f36e7495SArtem Savkov 			    i, shnum, strerror(errno));
455bf11655cSKees Cook 		sec->shdr.sh_name      = elf_word_to_cpu(shdr.sh_name);
456bf11655cSKees Cook 		sec->shdr.sh_type      = elf_word_to_cpu(shdr.sh_type);
457bf11655cSKees Cook 		sec->shdr.sh_flags     = elf_xword_to_cpu(shdr.sh_flags);
458bf11655cSKees Cook 		sec->shdr.sh_addr      = elf_addr_to_cpu(shdr.sh_addr);
459bf11655cSKees Cook 		sec->shdr.sh_offset    = elf_off_to_cpu(shdr.sh_offset);
460bf11655cSKees Cook 		sec->shdr.sh_size      = elf_xword_to_cpu(shdr.sh_size);
461bf11655cSKees Cook 		sec->shdr.sh_link      = elf_word_to_cpu(shdr.sh_link);
462bf11655cSKees Cook 		sec->shdr.sh_info      = elf_word_to_cpu(shdr.sh_info);
463bf11655cSKees Cook 		sec->shdr.sh_addralign = elf_xword_to_cpu(shdr.sh_addralign);
464bf11655cSKees Cook 		sec->shdr.sh_entsize   = elf_xword_to_cpu(shdr.sh_entsize);
465f36e7495SArtem Savkov 		if (sec->shdr.sh_link < shnum)
4666520fe55SH. Peter Anvin 			sec->link = &secs[sec->shdr.sh_link];
4676520fe55SH. Peter Anvin 	}
4686520fe55SH. Peter Anvin 
4696520fe55SH. Peter Anvin }
4706520fe55SH. Peter Anvin 
read_strtabs(FILE * fp)4716520fe55SH. Peter Anvin static void read_strtabs(FILE *fp)
4726520fe55SH. Peter Anvin {
4736520fe55SH. Peter Anvin 	int i;
474f36e7495SArtem Savkov 	for (i = 0; i < shnum; i++) {
4756520fe55SH. Peter Anvin 		struct section *sec = &secs[i];
4766520fe55SH. Peter Anvin 		if (sec->shdr.sh_type != SHT_STRTAB) {
4776520fe55SH. Peter Anvin 			continue;
4786520fe55SH. Peter Anvin 		}
4796520fe55SH. Peter Anvin 		sec->strtab = malloc(sec->shdr.sh_size);
4806520fe55SH. Peter Anvin 		if (!sec->strtab) {
48103dca99eSBorislav Petkov 			die("malloc of %" FMT " bytes for strtab failed\n",
4826520fe55SH. Peter Anvin 			    sec->shdr.sh_size);
4836520fe55SH. Peter Anvin 		}
4846520fe55SH. Peter Anvin 		if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0) {
48503dca99eSBorislav Petkov 			die("Seek to %" FMT " failed: %s\n",
4866520fe55SH. Peter Anvin 			    sec->shdr.sh_offset, strerror(errno));
4876520fe55SH. Peter Anvin 		}
4886520fe55SH. Peter Anvin 		if (fread(sec->strtab, 1, sec->shdr.sh_size, fp)
4896520fe55SH. Peter Anvin 		    != sec->shdr.sh_size) {
4906520fe55SH. Peter Anvin 			die("Cannot read symbol table: %s\n",
4916520fe55SH. Peter Anvin 				strerror(errno));
4926520fe55SH. Peter Anvin 		}
4936520fe55SH. Peter Anvin 	}
4946520fe55SH. Peter Anvin }
4956520fe55SH. Peter Anvin 
read_symtabs(FILE * fp)4966520fe55SH. Peter Anvin static void read_symtabs(FILE *fp)
4976520fe55SH. Peter Anvin {
4986520fe55SH. Peter Anvin 	int i,j;
499a54c401aSKristen Carlson Accardi 
500f36e7495SArtem Savkov 	for (i = 0; i < shnum; i++) {
5016520fe55SH. Peter Anvin 		struct section *sec = &secs[i];
502a54c401aSKristen Carlson Accardi 		int num_syms;
503a54c401aSKristen Carlson Accardi 
504a54c401aSKristen Carlson Accardi 		switch (sec->shdr.sh_type) {
505a54c401aSKristen Carlson Accardi 		case SHT_SYMTAB_SHNDX:
506a54c401aSKristen Carlson Accardi 			sec->xsymtab = malloc(sec->shdr.sh_size);
507a54c401aSKristen Carlson Accardi 			if (!sec->xsymtab) {
508a54c401aSKristen Carlson Accardi 				die("malloc of %" FMT " bytes for xsymtab failed\n",
509a54c401aSKristen Carlson Accardi 				    sec->shdr.sh_size);
5106520fe55SH. Peter Anvin 			}
511a54c401aSKristen Carlson Accardi 			if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0) {
512a54c401aSKristen Carlson Accardi 				die("Seek to %" FMT " failed: %s\n",
513a54c401aSKristen Carlson Accardi 				    sec->shdr.sh_offset, strerror(errno));
514a54c401aSKristen Carlson Accardi 			}
515a54c401aSKristen Carlson Accardi 			if (fread(sec->xsymtab, 1, sec->shdr.sh_size, fp)
516a54c401aSKristen Carlson Accardi 			    != sec->shdr.sh_size) {
517a54c401aSKristen Carlson Accardi 				die("Cannot read extended symbol table: %s\n",
518a54c401aSKristen Carlson Accardi 				    strerror(errno));
519a54c401aSKristen Carlson Accardi 			}
520a54c401aSKristen Carlson Accardi 			shxsymtabndx = i;
521a54c401aSKristen Carlson Accardi 			continue;
522a54c401aSKristen Carlson Accardi 
523a54c401aSKristen Carlson Accardi 		case SHT_SYMTAB:
524a54c401aSKristen Carlson Accardi 			num_syms = sec->shdr.sh_size / sizeof(Elf_Sym);
525a54c401aSKristen Carlson Accardi 
5266520fe55SH. Peter Anvin 			sec->symtab = malloc(sec->shdr.sh_size);
5276520fe55SH. Peter Anvin 			if (!sec->symtab) {
52803dca99eSBorislav Petkov 				die("malloc of %" FMT " bytes for symtab failed\n",
5296520fe55SH. Peter Anvin 				    sec->shdr.sh_size);
5306520fe55SH. Peter Anvin 			}
5316520fe55SH. Peter Anvin 			if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0) {
53203dca99eSBorislav Petkov 				die("Seek to %" FMT " failed: %s\n",
5336520fe55SH. Peter Anvin 				    sec->shdr.sh_offset, strerror(errno));
5346520fe55SH. Peter Anvin 			}
5356520fe55SH. Peter Anvin 			if (fread(sec->symtab, 1, sec->shdr.sh_size, fp)
5366520fe55SH. Peter Anvin 			    != sec->shdr.sh_size) {
5376520fe55SH. Peter Anvin 				die("Cannot read symbol table: %s\n",
5386520fe55SH. Peter Anvin 				    strerror(errno));
5396520fe55SH. Peter Anvin 			}
540a54c401aSKristen Carlson Accardi 			for (j = 0; j < num_syms; j++) {
541bf11655cSKees Cook 				Elf_Sym *sym = &sec->symtab[j];
542a54c401aSKristen Carlson Accardi 
543bf11655cSKees Cook 				sym->st_name  = elf_word_to_cpu(sym->st_name);
544bf11655cSKees Cook 				sym->st_value = elf_addr_to_cpu(sym->st_value);
545bf11655cSKees Cook 				sym->st_size  = elf_xword_to_cpu(sym->st_size);
546bf11655cSKees Cook 				sym->st_shndx = elf_half_to_cpu(sym->st_shndx);
5476520fe55SH. Peter Anvin 			}
548a54c401aSKristen Carlson Accardi 			shsymtabndx = i;
549a54c401aSKristen Carlson Accardi 			continue;
550a54c401aSKristen Carlson Accardi 
551a54c401aSKristen Carlson Accardi 		default:
552a54c401aSKristen Carlson Accardi 			continue;
553a54c401aSKristen Carlson Accardi 		}
5546520fe55SH. Peter Anvin 	}
5556520fe55SH. Peter Anvin }
5566520fe55SH. Peter Anvin 
5576520fe55SH. Peter Anvin 
read_relocs(FILE * fp)5586520fe55SH. Peter Anvin static void read_relocs(FILE *fp)
5596520fe55SH. Peter Anvin {
5606520fe55SH. Peter Anvin 	int i,j;
561f36e7495SArtem Savkov 	for (i = 0; i < shnum; i++) {
5626520fe55SH. Peter Anvin 		struct section *sec = &secs[i];
563bf11655cSKees Cook 		if (sec->shdr.sh_type != SHT_REL_TYPE) {
5646520fe55SH. Peter Anvin 			continue;
5656520fe55SH. Peter Anvin 		}
5666520fe55SH. Peter Anvin 		sec->reltab = malloc(sec->shdr.sh_size);
5676520fe55SH. Peter Anvin 		if (!sec->reltab) {
56803dca99eSBorislav Petkov 			die("malloc of %" FMT " bytes for relocs failed\n",
5696520fe55SH. Peter Anvin 			    sec->shdr.sh_size);
5706520fe55SH. Peter Anvin 		}
5716520fe55SH. Peter Anvin 		if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0) {
57203dca99eSBorislav Petkov 			die("Seek to %" FMT " failed: %s\n",
5736520fe55SH. Peter Anvin 			    sec->shdr.sh_offset, strerror(errno));
5746520fe55SH. Peter Anvin 		}
5756520fe55SH. Peter Anvin 		if (fread(sec->reltab, 1, sec->shdr.sh_size, fp)
5766520fe55SH. Peter Anvin 		    != sec->shdr.sh_size) {
5776520fe55SH. Peter Anvin 			die("Cannot read symbol table: %s\n",
5786520fe55SH. Peter Anvin 				strerror(errno));
5796520fe55SH. Peter Anvin 		}
580bf11655cSKees Cook 		for (j = 0; j < sec->shdr.sh_size/sizeof(Elf_Rel); j++) {
581bf11655cSKees Cook 			Elf_Rel *rel = &sec->reltab[j];
582bf11655cSKees Cook 			rel->r_offset = elf_addr_to_cpu(rel->r_offset);
583bf11655cSKees Cook 			rel->r_info   = elf_xword_to_cpu(rel->r_info);
584946166afSKees Cook #if (SHT_REL_TYPE == SHT_RELA)
585946166afSKees Cook 			rel->r_addend = elf_xword_to_cpu(rel->r_addend);
586946166afSKees Cook #endif
5876520fe55SH. Peter Anvin 		}
5886520fe55SH. Peter Anvin 	}
5896520fe55SH. Peter Anvin }
5906520fe55SH. Peter Anvin 
5916520fe55SH. Peter Anvin 
print_absolute_symbols(void)5926520fe55SH. Peter Anvin static void print_absolute_symbols(void)
5936520fe55SH. Peter Anvin {
5946520fe55SH. Peter Anvin 	int i;
595946166afSKees Cook 	const char *format;
596946166afSKees Cook 
597c889ba80SH. Peter Anvin 	if (ELF_BITS == 64)
598946166afSKees Cook 		format = "%5d %016"PRIx64" %5"PRId64" %10s %10s %12s %s\n";
599946166afSKees Cook 	else
600946166afSKees Cook 		format = "%5d %08"PRIx32"  %5"PRId32" %10s %10s %12s %s\n";
601946166afSKees Cook 
6026520fe55SH. Peter Anvin 	printf("Absolute symbols\n");
6036520fe55SH. Peter Anvin 	printf(" Num:    Value Size  Type       Bind        Visibility  Name\n");
604f36e7495SArtem Savkov 	for (i = 0; i < shnum; i++) {
6056520fe55SH. Peter Anvin 		struct section *sec = &secs[i];
6066520fe55SH. Peter Anvin 		char *sym_strtab;
6076520fe55SH. Peter Anvin 		int j;
6086520fe55SH. Peter Anvin 
6096520fe55SH. Peter Anvin 		if (sec->shdr.sh_type != SHT_SYMTAB) {
6106520fe55SH. Peter Anvin 			continue;
6116520fe55SH. Peter Anvin 		}
6126520fe55SH. Peter Anvin 		sym_strtab = sec->link->strtab;
613bf11655cSKees Cook 		for (j = 0; j < sec->shdr.sh_size/sizeof(Elf_Sym); j++) {
614bf11655cSKees Cook 			Elf_Sym *sym;
6156520fe55SH. Peter Anvin 			const char *name;
6166520fe55SH. Peter Anvin 			sym = &sec->symtab[j];
6176520fe55SH. Peter Anvin 			name = sym_name(sym_strtab, sym);
6186520fe55SH. Peter Anvin 			if (sym->st_shndx != SHN_ABS) {
6196520fe55SH. Peter Anvin 				continue;
6206520fe55SH. Peter Anvin 			}
621946166afSKees Cook 			printf(format,
6226520fe55SH. Peter Anvin 				j, sym->st_value, sym->st_size,
623bf11655cSKees Cook 				sym_type(ELF_ST_TYPE(sym->st_info)),
624bf11655cSKees Cook 				sym_bind(ELF_ST_BIND(sym->st_info)),
625bf11655cSKees Cook 				sym_visibility(ELF_ST_VISIBILITY(sym->st_other)),
6266520fe55SH. Peter Anvin 				name);
6276520fe55SH. Peter Anvin 		}
6286520fe55SH. Peter Anvin 	}
6296520fe55SH. Peter Anvin 	printf("\n");
6306520fe55SH. Peter Anvin }
6316520fe55SH. Peter Anvin 
print_absolute_relocs(void)6326520fe55SH. Peter Anvin static void print_absolute_relocs(void)
6336520fe55SH. Peter Anvin {
6346520fe55SH. Peter Anvin 	int i, printed = 0;
635946166afSKees Cook 	const char *format;
636946166afSKees Cook 
637c889ba80SH. Peter Anvin 	if (ELF_BITS == 64)
638946166afSKees Cook 		format = "%016"PRIx64" %016"PRIx64" %10s %016"PRIx64"  %s\n";
639946166afSKees Cook 	else
640946166afSKees Cook 		format = "%08"PRIx32" %08"PRIx32" %10s %08"PRIx32"  %s\n";
6416520fe55SH. Peter Anvin 
642f36e7495SArtem Savkov 	for (i = 0; i < shnum; i++) {
6436520fe55SH. Peter Anvin 		struct section *sec = &secs[i];
6446520fe55SH. Peter Anvin 		struct section *sec_applies, *sec_symtab;
6456520fe55SH. Peter Anvin 		char *sym_strtab;
646bf11655cSKees Cook 		Elf_Sym *sh_symtab;
6476520fe55SH. Peter Anvin 		int j;
648bf11655cSKees Cook 		if (sec->shdr.sh_type != SHT_REL_TYPE) {
6496520fe55SH. Peter Anvin 			continue;
6506520fe55SH. Peter Anvin 		}
6516520fe55SH. Peter Anvin 		sec_symtab  = sec->link;
6526520fe55SH. Peter Anvin 		sec_applies = &secs[sec->shdr.sh_info];
6536520fe55SH. Peter Anvin 		if (!(sec_applies->shdr.sh_flags & SHF_ALLOC)) {
6546520fe55SH. Peter Anvin 			continue;
6556520fe55SH. Peter Anvin 		}
656af2a9f98SKees Cook 		/*
657af2a9f98SKees Cook 		 * Do not perform relocations in .notes section; any
658af2a9f98SKees Cook 		 * values there are meant for pre-boot consumption (e.g.
659af2a9f98SKees Cook 		 * startup_xen).
660af2a9f98SKees Cook 		 */
661af2a9f98SKees Cook 		if (sec_applies->shdr.sh_type == SHT_NOTE) {
662af2a9f98SKees Cook 			continue;
663af2a9f98SKees Cook 		}
6646520fe55SH. Peter Anvin 		sh_symtab  = sec_symtab->symtab;
6656520fe55SH. Peter Anvin 		sym_strtab = sec_symtab->link->strtab;
666bf11655cSKees Cook 		for (j = 0; j < sec->shdr.sh_size/sizeof(Elf_Rel); j++) {
667bf11655cSKees Cook 			Elf_Rel *rel;
668bf11655cSKees Cook 			Elf_Sym *sym;
6696520fe55SH. Peter Anvin 			const char *name;
6706520fe55SH. Peter Anvin 			rel = &sec->reltab[j];
671bf11655cSKees Cook 			sym = &sh_symtab[ELF_R_SYM(rel->r_info)];
6726520fe55SH. Peter Anvin 			name = sym_name(sym_strtab, sym);
6736520fe55SH. Peter Anvin 			if (sym->st_shndx != SHN_ABS) {
6746520fe55SH. Peter Anvin 				continue;
6756520fe55SH. Peter Anvin 			}
6766520fe55SH. Peter Anvin 
6776520fe55SH. Peter Anvin 			/* Absolute symbols are not relocated if bzImage is
6786520fe55SH. Peter Anvin 			 * loaded at a non-compiled address. Display a warning
6796520fe55SH. Peter Anvin 			 * to user at compile time about the absolute
6806520fe55SH. Peter Anvin 			 * relocations present.
6816520fe55SH. Peter Anvin 			 *
6826520fe55SH. Peter Anvin 			 * User need to audit the code to make sure
6836520fe55SH. Peter Anvin 			 * some symbols which should have been section
6846520fe55SH. Peter Anvin 			 * relative have not become absolute because of some
6856520fe55SH. Peter Anvin 			 * linker optimization or wrong programming usage.
6866520fe55SH. Peter Anvin 			 *
6876520fe55SH. Peter Anvin 			 * Before warning check if this absolute symbol
6886520fe55SH. Peter Anvin 			 * relocation is harmless.
6896520fe55SH. Peter Anvin 			 */
6906520fe55SH. Peter Anvin 			if (is_reloc(S_ABS, name) || is_reloc(S_REL, name))
6916520fe55SH. Peter Anvin 				continue;
6926520fe55SH. Peter Anvin 
6936520fe55SH. Peter Anvin 			if (!printed) {
6946520fe55SH. Peter Anvin 				printf("WARNING: Absolute relocations"
6956520fe55SH. Peter Anvin 					" present\n");
6966520fe55SH. Peter Anvin 				printf("Offset     Info     Type     Sym.Value "
6976520fe55SH. Peter Anvin 					"Sym.Name\n");
6986520fe55SH. Peter Anvin 				printed = 1;
6996520fe55SH. Peter Anvin 			}
7006520fe55SH. Peter Anvin 
701946166afSKees Cook 			printf(format,
7026520fe55SH. Peter Anvin 				rel->r_offset,
7036520fe55SH. Peter Anvin 				rel->r_info,
704bf11655cSKees Cook 				rel_type(ELF_R_TYPE(rel->r_info)),
7056520fe55SH. Peter Anvin 				sym->st_value,
7066520fe55SH. Peter Anvin 				name);
7076520fe55SH. Peter Anvin 		}
7086520fe55SH. Peter Anvin 	}
7096520fe55SH. Peter Anvin 
7106520fe55SH. Peter Anvin 	if (printed)
7116520fe55SH. Peter Anvin 		printf("\n");
7126520fe55SH. Peter Anvin }
7136520fe55SH. Peter Anvin 
add_reloc(struct relocs * r,uint32_t offset)7145d442e63SKees Cook static void add_reloc(struct relocs *r, uint32_t offset)
7155d442e63SKees Cook {
7165d442e63SKees Cook 	if (r->count == r->size) {
7175d442e63SKees Cook 		unsigned long newsize = r->size + 50000;
7185d442e63SKees Cook 		void *mem = realloc(r->offset, newsize * sizeof(r->offset[0]));
7195d442e63SKees Cook 
7205d442e63SKees Cook 		if (!mem)
7215d442e63SKees Cook 			die("realloc of %ld entries for relocs failed\n",
7225d442e63SKees Cook                                 newsize);
7235d442e63SKees Cook 		r->offset = mem;
7245d442e63SKees Cook 		r->size = newsize;
7255d442e63SKees Cook 	}
7265d442e63SKees Cook 	r->offset[r->count++] = offset;
7275d442e63SKees Cook }
7285d442e63SKees Cook 
walk_relocs(int (* process)(struct section * sec,Elf_Rel * rel,Elf_Sym * sym,const char * symname))7295d442e63SKees Cook static void walk_relocs(int (*process)(struct section *sec, Elf_Rel *rel,
7305d442e63SKees Cook 			Elf_Sym *sym, const char *symname))
7316520fe55SH. Peter Anvin {
7326520fe55SH. Peter Anvin 	int i;
7336520fe55SH. Peter Anvin 	/* Walk through the relocations */
734f36e7495SArtem Savkov 	for (i = 0; i < shnum; i++) {
7356520fe55SH. Peter Anvin 		char *sym_strtab;
736bf11655cSKees Cook 		Elf_Sym *sh_symtab;
7376520fe55SH. Peter Anvin 		struct section *sec_applies, *sec_symtab;
7386520fe55SH. Peter Anvin 		int j;
7396520fe55SH. Peter Anvin 		struct section *sec = &secs[i];
7406520fe55SH. Peter Anvin 
741bf11655cSKees Cook 		if (sec->shdr.sh_type != SHT_REL_TYPE) {
7426520fe55SH. Peter Anvin 			continue;
7436520fe55SH. Peter Anvin 		}
7446520fe55SH. Peter Anvin 		sec_symtab  = sec->link;
7456520fe55SH. Peter Anvin 		sec_applies = &secs[sec->shdr.sh_info];
7466520fe55SH. Peter Anvin 		if (!(sec_applies->shdr.sh_flags & SHF_ALLOC)) {
7476520fe55SH. Peter Anvin 			continue;
7486520fe55SH. Peter Anvin 		}
7499fa39135SGuixiong Wei 
7509fa39135SGuixiong Wei 		/*
7519fa39135SGuixiong Wei 		 * Do not perform relocations in .notes sections; any
7529fa39135SGuixiong Wei 		 * values there are meant for pre-boot consumption (e.g.
7539fa39135SGuixiong Wei 		 * startup_xen).
7549fa39135SGuixiong Wei 		 */
7559fa39135SGuixiong Wei 		if (sec_applies->shdr.sh_type == SHT_NOTE)
7569fa39135SGuixiong Wei 			continue;
7579fa39135SGuixiong Wei 
7586520fe55SH. Peter Anvin 		sh_symtab = sec_symtab->symtab;
7596520fe55SH. Peter Anvin 		sym_strtab = sec_symtab->link->strtab;
760bf11655cSKees Cook 		for (j = 0; j < sec->shdr.sh_size/sizeof(Elf_Rel); j++) {
7615d442e63SKees Cook 			Elf_Rel *rel = &sec->reltab[j];
7625d442e63SKees Cook 			Elf_Sym *sym = &sh_symtab[ELF_R_SYM(rel->r_info)];
7635d442e63SKees Cook 			const char *symname = sym_name(sym_strtab, sym);
76424ab82bdSH. Peter Anvin 
7655d442e63SKees Cook 			process(sec, rel, sym, symname);
7665d442e63SKees Cook 		}
7675d442e63SKees Cook 	}
7685d442e63SKees Cook }
7696520fe55SH. Peter Anvin 
770946166afSKees Cook /*
771946166afSKees Cook  * The .data..percpu section is a special case for x86_64 SMP kernels.
772946166afSKees Cook  * It is used to initialize the actual per_cpu areas and to provide
773946166afSKees Cook  * definitions for the per_cpu variables that correspond to their offsets
774946166afSKees Cook  * within the percpu area. Since the values of all of the symbols need
775946166afSKees Cook  * to be offsets from the start of the per_cpu area the virtual address
776946166afSKees Cook  * (sh_addr) of .data..percpu is 0 in SMP kernels.
777946166afSKees Cook  *
778946166afSKees Cook  * This means that:
779946166afSKees Cook  *
780946166afSKees Cook  *	Relocations that reference symbols in the per_cpu area do not
781946166afSKees Cook  *	need further relocation (since the value is an offset relative
782946166afSKees Cook  *	to the start of the per_cpu area that does not change).
783946166afSKees Cook  *
784946166afSKees Cook  *	Relocations that apply to the per_cpu area need to have their
785946166afSKees Cook  *	offset adjusted by by the value of __per_cpu_load to make them
786946166afSKees Cook  *	point to the correct place in the loaded image (because the
787946166afSKees Cook  *	virtual address of .data..percpu is 0).
788946166afSKees Cook  *
789946166afSKees Cook  * For non SMP kernels .data..percpu is linked as part of the normal
790946166afSKees Cook  * kernel data and does not require special treatment.
791946166afSKees Cook  *
792946166afSKees Cook  */
793946166afSKees Cook static int per_cpu_shndx	= -1;
794eeeda4cdSBen Hutchings static Elf_Addr per_cpu_load_addr;
795946166afSKees Cook 
percpu_init(void)796946166afSKees Cook static void percpu_init(void)
797946166afSKees Cook {
798946166afSKees Cook 	int i;
799f36e7495SArtem Savkov 	for (i = 0; i < shnum; i++) {
800946166afSKees Cook 		ElfW(Sym) *sym;
801946166afSKees Cook 		if (strcmp(sec_name(i), ".data..percpu"))
802946166afSKees Cook 			continue;
803946166afSKees Cook 
804946166afSKees Cook 		if (secs[i].shdr.sh_addr != 0)	/* non SMP kernel */
805946166afSKees Cook 			return;
806946166afSKees Cook 
807946166afSKees Cook 		sym = sym_lookup("__per_cpu_load");
808946166afSKees Cook 		if (!sym)
809946166afSKees Cook 			die("can't find __per_cpu_load\n");
810946166afSKees Cook 
811946166afSKees Cook 		per_cpu_shndx = i;
812946166afSKees Cook 		per_cpu_load_addr = sym->st_value;
813946166afSKees Cook 		return;
814946166afSKees Cook 	}
815946166afSKees Cook }
816946166afSKees Cook 
817c889ba80SH. Peter Anvin #if ELF_BITS == 64
818c889ba80SH. Peter Anvin 
819946166afSKees Cook /*
820946166afSKees Cook  * Check to see if a symbol lies in the .data..percpu section.
821d751c169SMichael Davidson  *
822d751c169SMichael Davidson  * The linker incorrectly associates some symbols with the
823d751c169SMichael Davidson  * .data..percpu section so we also need to check the symbol
824d751c169SMichael Davidson  * name to make sure that we classify the symbol correctly.
825d751c169SMichael Davidson  *
826d751c169SMichael Davidson  * The GNU linker incorrectly associates:
827d751c169SMichael Davidson  *	__init_begin
828aec58bafSKees Cook  *	__per_cpu_load
829d751c169SMichael Davidson  *
830d751c169SMichael Davidson  * The "gold" linker incorrectly associates:
831e6401c13SAndy Lutomirski  *	init_per_cpu__fixed_percpu_data
832d751c169SMichael Davidson  *	init_per_cpu__gdt_page
833946166afSKees Cook  */
is_percpu_sym(ElfW (Sym)* sym,const char * symname)834946166afSKees Cook static int is_percpu_sym(ElfW(Sym) *sym, const char *symname)
835946166afSKees Cook {
836a54c401aSKristen Carlson Accardi 	int shndx = sym_index(sym);
837a54c401aSKristen Carlson Accardi 
838a54c401aSKristen Carlson Accardi 	return (shndx == per_cpu_shndx) &&
839d751c169SMichael Davidson 		strcmp(symname, "__init_begin") &&
840aec58bafSKees Cook 		strcmp(symname, "__per_cpu_load") &&
841d751c169SMichael Davidson 		strncmp(symname, "init_per_cpu_", 13);
842946166afSKees Cook }
843946166afSKees Cook 
844c889ba80SH. Peter Anvin 
do_reloc64(struct section * sec,Elf_Rel * rel,ElfW (Sym)* sym,const char * symname)845946166afSKees Cook static int do_reloc64(struct section *sec, Elf_Rel *rel, ElfW(Sym) *sym,
846946166afSKees Cook 		      const char *symname)
847946166afSKees Cook {
848946166afSKees Cook 	unsigned r_type = ELF64_R_TYPE(rel->r_info);
849946166afSKees Cook 	ElfW(Addr) offset = rel->r_offset;
850946166afSKees Cook 	int shn_abs = (sym->st_shndx == SHN_ABS) && !is_reloc(S_REL, symname);
851946166afSKees Cook 
852946166afSKees Cook 	if (sym->st_shndx == SHN_UNDEF)
853946166afSKees Cook 		return 0;
854946166afSKees Cook 
855946166afSKees Cook 	/*
856946166afSKees Cook 	 * Adjust the offset if this reloc applies to the percpu section.
857946166afSKees Cook 	 */
858946166afSKees Cook 	if (sec->shdr.sh_info == per_cpu_shndx)
859946166afSKees Cook 		offset += per_cpu_load_addr;
860946166afSKees Cook 
861946166afSKees Cook 	switch (r_type) {
862946166afSKees Cook 	case R_X86_64_NONE:
8636d24c5f7SJan Beulich 		/* NONE can be ignored. */
8646d24c5f7SJan Beulich 		break;
8656d24c5f7SJan Beulich 
866946166afSKees Cook 	case R_X86_64_PC32:
867b21ebf2fSH.J. Lu 	case R_X86_64_PLT32:
868946166afSKees Cook 		/*
8696d24c5f7SJan Beulich 		 * PC relative relocations don't need to be adjusted unless
8706d24c5f7SJan Beulich 		 * referencing a percpu symbol.
871b21ebf2fSH.J. Lu 		 *
872b21ebf2fSH.J. Lu 		 * NB: R_X86_64_PLT32 can be treated as R_X86_64_PC32.
873946166afSKees Cook 		 */
8746d24c5f7SJan Beulich 		if (is_percpu_sym(sym, symname))
8756d24c5f7SJan Beulich 			add_reloc(&relocs32neg, offset);
876946166afSKees Cook 		break;
877946166afSKees Cook 
878b40a142bSArd Biesheuvel 	case R_X86_64_PC64:
879b40a142bSArd Biesheuvel 		/*
880b40a142bSArd Biesheuvel 		 * Only used by jump labels
881b40a142bSArd Biesheuvel 		 */
882b40a142bSArd Biesheuvel 		if (is_percpu_sym(sym, symname))
883b40a142bSArd Biesheuvel 			die("Invalid R_X86_64_PC64 relocation against per-CPU symbol %s\n",
884b40a142bSArd Biesheuvel 			    symname);
885b40a142bSArd Biesheuvel 		break;
886b40a142bSArd Biesheuvel 
887946166afSKees Cook 	case R_X86_64_32:
888946166afSKees Cook 	case R_X86_64_32S:
889946166afSKees Cook 	case R_X86_64_64:
890946166afSKees Cook 		/*
891946166afSKees Cook 		 * References to the percpu area don't need to be adjusted.
892946166afSKees Cook 		 */
893946166afSKees Cook 		if (is_percpu_sym(sym, symname))
894946166afSKees Cook 			break;
895946166afSKees Cook 
896946166afSKees Cook 		if (shn_abs) {
897946166afSKees Cook 			/*
898946166afSKees Cook 			 * Whitelisted absolute symbols do not require
899946166afSKees Cook 			 * relocation.
900946166afSKees Cook 			 */
901946166afSKees Cook 			if (is_reloc(S_ABS, symname))
902946166afSKees Cook 				break;
903946166afSKees Cook 
904946166afSKees Cook 			die("Invalid absolute %s relocation: %s\n",
905946166afSKees Cook 			    rel_type(r_type), symname);
906946166afSKees Cook 			break;
907946166afSKees Cook 		}
908946166afSKees Cook 
909946166afSKees Cook 		/*
910946166afSKees Cook 		 * Relocation offsets for 64 bit kernels are output
911946166afSKees Cook 		 * as 32 bits and sign extended back to 64 bits when
912946166afSKees Cook 		 * the relocations are processed.
913946166afSKees Cook 		 * Make sure that the offset will fit.
914946166afSKees Cook 		 */
915946166afSKees Cook 		if ((int32_t)offset != (int64_t)offset)
916946166afSKees Cook 			die("Relocation offset doesn't fit in 32 bits\n");
917946166afSKees Cook 
918946166afSKees Cook 		if (r_type == R_X86_64_64)
919946166afSKees Cook 			add_reloc(&relocs64, offset);
920946166afSKees Cook 		else
921946166afSKees Cook 			add_reloc(&relocs32, offset);
922946166afSKees Cook 		break;
923946166afSKees Cook 
924946166afSKees Cook 	default:
925946166afSKees Cook 		die("Unsupported relocation type: %s (%d)\n",
926946166afSKees Cook 		    rel_type(r_type), r_type);
927946166afSKees Cook 		break;
928946166afSKees Cook 	}
929946166afSKees Cook 
930946166afSKees Cook 	return 0;
931946166afSKees Cook }
932946166afSKees Cook 
933c889ba80SH. Peter Anvin #else
934946166afSKees Cook 
do_reloc32(struct section * sec,Elf_Rel * rel,Elf_Sym * sym,const char * symname)935946166afSKees Cook static int do_reloc32(struct section *sec, Elf_Rel *rel, Elf_Sym *sym,
9365d442e63SKees Cook 		      const char *symname)
9375d442e63SKees Cook {
9385d442e63SKees Cook 	unsigned r_type = ELF32_R_TYPE(rel->r_info);
9395d442e63SKees Cook 	int shn_abs = (sym->st_shndx == SHN_ABS) && !is_reloc(S_REL, symname);
94024ab82bdSH. Peter Anvin 
9416520fe55SH. Peter Anvin 	switch (r_type) {
9426520fe55SH. Peter Anvin 	case R_386_NONE:
9436520fe55SH. Peter Anvin 	case R_386_PC32:
9446520fe55SH. Peter Anvin 	case R_386_PC16:
9456520fe55SH. Peter Anvin 	case R_386_PC8:
946bb73d071SFangrui Song 	case R_386_PLT32:
9476520fe55SH. Peter Anvin 		/*
948bb73d071SFangrui Song 		 * NONE can be ignored and PC relative relocations don't need
949bb73d071SFangrui Song 		 * to be adjusted. Because sym must be defined, R_386_PLT32 can
950bb73d071SFangrui Song 		 * be treated the same way as R_386_PC32.
9516520fe55SH. Peter Anvin 		 */
9526520fe55SH. Peter Anvin 		break;
9536520fe55SH. Peter Anvin 
9545d442e63SKees Cook 	case R_386_32:
95524ab82bdSH. Peter Anvin 		if (shn_abs) {
9565d442e63SKees Cook 			/*
9575d442e63SKees Cook 			 * Whitelisted absolute symbols do not require
9585d442e63SKees Cook 			 * relocation.
9595d442e63SKees Cook 			 */
9606520fe55SH. Peter Anvin 			if (is_reloc(S_ABS, symname))
9616520fe55SH. Peter Anvin 				break;
9626520fe55SH. Peter Anvin 
9635d442e63SKees Cook 			die("Invalid absolute %s relocation: %s\n",
9645d442e63SKees Cook 			    rel_type(r_type), symname);
9656520fe55SH. Peter Anvin 			break;
9666520fe55SH. Peter Anvin 		}
9675d442e63SKees Cook 
9685d442e63SKees Cook 		add_reloc(&relocs32, rel->r_offset);
9696520fe55SH. Peter Anvin 		break;
9705d442e63SKees Cook 
9716520fe55SH. Peter Anvin 	default:
9726520fe55SH. Peter Anvin 		die("Unsupported relocation type: %s (%d)\n",
9736520fe55SH. Peter Anvin 		    rel_type(r_type), r_type);
9746520fe55SH. Peter Anvin 		break;
9755d442e63SKees Cook 	}
9765d442e63SKees Cook 
9775d442e63SKees Cook 	return 0;
9785d442e63SKees Cook }
9795d442e63SKees Cook 
do_reloc_real(struct section * sec,Elf_Rel * rel,Elf_Sym * sym,const char * symname)9805d442e63SKees Cook static int do_reloc_real(struct section *sec, Elf_Rel *rel, Elf_Sym *sym,
9815d442e63SKees Cook 			 const char *symname)
9825d442e63SKees Cook {
9835d442e63SKees Cook 	unsigned r_type = ELF32_R_TYPE(rel->r_info);
9845d442e63SKees Cook 	int shn_abs = (sym->st_shndx == SHN_ABS) && !is_reloc(S_REL, symname);
9855d442e63SKees Cook 
9865d442e63SKees Cook 	switch (r_type) {
9875d442e63SKees Cook 	case R_386_NONE:
9885d442e63SKees Cook 	case R_386_PC32:
9895d442e63SKees Cook 	case R_386_PC16:
9905d442e63SKees Cook 	case R_386_PC8:
991bb73d071SFangrui Song 	case R_386_PLT32:
9925d442e63SKees Cook 		/*
993bb73d071SFangrui Song 		 * NONE can be ignored and PC relative relocations don't need
994bb73d071SFangrui Song 		 * to be adjusted. Because sym must be defined, R_386_PLT32 can
995bb73d071SFangrui Song 		 * be treated the same way as R_386_PC32.
9965d442e63SKees Cook 		 */
9975d442e63SKees Cook 		break;
9985d442e63SKees Cook 
9995d442e63SKees Cook 	case R_386_16:
10005d442e63SKees Cook 		if (shn_abs) {
10015d442e63SKees Cook 			/*
10025d442e63SKees Cook 			 * Whitelisted absolute symbols do not require
10035d442e63SKees Cook 			 * relocation.
10045d442e63SKees Cook 			 */
10055d442e63SKees Cook 			if (is_reloc(S_ABS, symname))
10065d442e63SKees Cook 				break;
10075d442e63SKees Cook 
10085d442e63SKees Cook 			if (is_reloc(S_SEG, symname)) {
10095d442e63SKees Cook 				add_reloc(&relocs16, rel->r_offset);
10105d442e63SKees Cook 				break;
10115d442e63SKees Cook 			}
10125d442e63SKees Cook 		} else {
10135d442e63SKees Cook 			if (!is_reloc(S_LIN, symname))
10145d442e63SKees Cook 				break;
10155d442e63SKees Cook 		}
101624ab82bdSH. Peter Anvin 		die("Invalid %s %s relocation: %s\n",
101724ab82bdSH. Peter Anvin 		    shn_abs ? "absolute" : "relative",
10186520fe55SH. Peter Anvin 		    rel_type(r_type), symname);
10195d442e63SKees Cook 		break;
10205d442e63SKees Cook 
10215d442e63SKees Cook 	case R_386_32:
10225d442e63SKees Cook 		if (shn_abs) {
10235d442e63SKees Cook 			/*
10245d442e63SKees Cook 			 * Whitelisted absolute symbols do not require
10255d442e63SKees Cook 			 * relocation.
10265d442e63SKees Cook 			 */
10275d442e63SKees Cook 			if (is_reloc(S_ABS, symname))
10285d442e63SKees Cook 				break;
10295d442e63SKees Cook 
10305d442e63SKees Cook 			if (is_reloc(S_REL, symname)) {
10315d442e63SKees Cook 				add_reloc(&relocs32, rel->r_offset);
10325d442e63SKees Cook 				break;
10336520fe55SH. Peter Anvin 			}
10345d442e63SKees Cook 		} else {
10355d442e63SKees Cook 			if (is_reloc(S_LIN, symname))
10365d442e63SKees Cook 				add_reloc(&relocs32, rel->r_offset);
10375d442e63SKees Cook 			break;
10386520fe55SH. Peter Anvin 		}
10395d442e63SKees Cook 		die("Invalid %s %s relocation: %s\n",
10405d442e63SKees Cook 		    shn_abs ? "absolute" : "relative",
10415d442e63SKees Cook 		    rel_type(r_type), symname);
10425d442e63SKees Cook 		break;
10435d442e63SKees Cook 
10445d442e63SKees Cook 	default:
10455d442e63SKees Cook 		die("Unsupported relocation type: %s (%d)\n",
10465d442e63SKees Cook 		    rel_type(r_type), r_type);
10475d442e63SKees Cook 		break;
10486520fe55SH. Peter Anvin 	}
10496520fe55SH. Peter Anvin 
10505d442e63SKees Cook 	return 0;
10516520fe55SH. Peter Anvin }
10526520fe55SH. Peter Anvin 
1053c889ba80SH. Peter Anvin #endif
1054c889ba80SH. Peter Anvin 
cmp_relocs(const void * va,const void * vb)10556520fe55SH. Peter Anvin static int cmp_relocs(const void *va, const void *vb)
10566520fe55SH. Peter Anvin {
10575d442e63SKees Cook 	const uint32_t *a, *b;
10586520fe55SH. Peter Anvin 	a = va; b = vb;
10596520fe55SH. Peter Anvin 	return (*a == *b)? 0 : (*a > *b)? 1 : -1;
10606520fe55SH. Peter Anvin }
10616520fe55SH. Peter Anvin 
sort_relocs(struct relocs * r)10625d442e63SKees Cook static void sort_relocs(struct relocs *r)
10635d442e63SKees Cook {
10645d442e63SKees Cook 	qsort(r->offset, r->count, sizeof(r->offset[0]), cmp_relocs);
10655d442e63SKees Cook }
10665d442e63SKees Cook 
write32(uint32_t v,FILE * f)10675d442e63SKees Cook static int write32(uint32_t v, FILE *f)
10686520fe55SH. Peter Anvin {
10696520fe55SH. Peter Anvin 	unsigned char buf[4];
10706520fe55SH. Peter Anvin 
10716520fe55SH. Peter Anvin 	put_unaligned_le32(v, buf);
10726520fe55SH. Peter Anvin 	return fwrite(buf, 1, 4, f) == 4 ? 0 : -1;
10736520fe55SH. Peter Anvin }
10746520fe55SH. Peter Anvin 
write32_as_text(uint32_t v,FILE * f)10755d442e63SKees Cook static int write32_as_text(uint32_t v, FILE *f)
10765d442e63SKees Cook {
10775d442e63SKees Cook 	return fprintf(f, "\t.long 0x%08"PRIx32"\n", v) > 0 ? 0 : -1;
10785d442e63SKees Cook }
10795d442e63SKees Cook 
emit_relocs(int as_text,int use_real_mode)10806520fe55SH. Peter Anvin static void emit_relocs(int as_text, int use_real_mode)
10816520fe55SH. Peter Anvin {
10826520fe55SH. Peter Anvin 	int i;
10835d442e63SKees Cook 	int (*write_reloc)(uint32_t, FILE *) = write32;
1084946166afSKees Cook 	int (*do_reloc)(struct section *sec, Elf_Rel *rel, Elf_Sym *sym,
1085946166afSKees Cook 			const char *symname);
1086946166afSKees Cook 
1087c889ba80SH. Peter Anvin #if ELF_BITS == 64
1088c889ba80SH. Peter Anvin 	if (!use_real_mode)
1089946166afSKees Cook 		do_reloc = do_reloc64;
1090c889ba80SH. Peter Anvin 	else
1091c889ba80SH. Peter Anvin 		die("--realmode not valid for a 64-bit ELF file");
1092c889ba80SH. Peter Anvin #else
1093c889ba80SH. Peter Anvin 	if (!use_real_mode)
1094946166afSKees Cook 		do_reloc = do_reloc32;
1095946166afSKees Cook 	else
1096946166afSKees Cook 		do_reloc = do_reloc_real;
1097c889ba80SH. Peter Anvin #endif
10986520fe55SH. Peter Anvin 
10996520fe55SH. Peter Anvin 	/* Collect up the relocations */
1100946166afSKees Cook 	walk_relocs(do_reloc);
11016520fe55SH. Peter Anvin 
11025d442e63SKees Cook 	if (relocs16.count && !use_real_mode)
11036520fe55SH. Peter Anvin 		die("Segment relocations found but --realmode not specified\n");
11046520fe55SH. Peter Anvin 
11056520fe55SH. Peter Anvin 	/* Order the relocations for more efficient processing */
11065d442e63SKees Cook 	sort_relocs(&relocs32);
11076d24c5f7SJan Beulich #if ELF_BITS == 64
11086d24c5f7SJan Beulich 	sort_relocs(&relocs32neg);
1109946166afSKees Cook 	sort_relocs(&relocs64);
11107ebb9167SMarkus Trippelsdorf #else
11117ebb9167SMarkus Trippelsdorf 	sort_relocs(&relocs16);
11126d24c5f7SJan Beulich #endif
11136520fe55SH. Peter Anvin 
11146520fe55SH. Peter Anvin 	/* Print the relocations */
11156520fe55SH. Peter Anvin 	if (as_text) {
11166520fe55SH. Peter Anvin 		/* Print the relocations in a form suitable that
11176520fe55SH. Peter Anvin 		 * gas will like.
11186520fe55SH. Peter Anvin 		 */
11196520fe55SH. Peter Anvin 		printf(".section \".data.reloc\",\"a\"\n");
11206520fe55SH. Peter Anvin 		printf(".balign 4\n");
11215d442e63SKees Cook 		write_reloc = write32_as_text;
11226520fe55SH. Peter Anvin 	}
11235d442e63SKees Cook 
11245d442e63SKees Cook 	if (use_real_mode) {
11255d442e63SKees Cook 		write_reloc(relocs16.count, stdout);
11265d442e63SKees Cook 		for (i = 0; i < relocs16.count; i++)
11275d442e63SKees Cook 			write_reloc(relocs16.offset[i], stdout);
11285d442e63SKees Cook 
11295d442e63SKees Cook 		write_reloc(relocs32.count, stdout);
11305d442e63SKees Cook 		for (i = 0; i < relocs32.count; i++)
11315d442e63SKees Cook 			write_reloc(relocs32.offset[i], stdout);
11326520fe55SH. Peter Anvin 	} else {
11336d24c5f7SJan Beulich #if ELF_BITS == 64
1134946166afSKees Cook 		/* Print a stop */
1135946166afSKees Cook 		write_reloc(0, stdout);
1136946166afSKees Cook 
1137946166afSKees Cook 		/* Now print each relocation */
1138946166afSKees Cook 		for (i = 0; i < relocs64.count; i++)
1139946166afSKees Cook 			write_reloc(relocs64.offset[i], stdout);
11406d24c5f7SJan Beulich 
11416d24c5f7SJan Beulich 		/* Print a stop */
11426d24c5f7SJan Beulich 		write_reloc(0, stdout);
11436d24c5f7SJan Beulich 
11446d24c5f7SJan Beulich 		/* Now print each inverse 32-bit relocation */
11456d24c5f7SJan Beulich 		for (i = 0; i < relocs32neg.count; i++)
11466d24c5f7SJan Beulich 			write_reloc(relocs32neg.offset[i], stdout);
11476d24c5f7SJan Beulich #endif
1148946166afSKees Cook 
11496520fe55SH. Peter Anvin 		/* Print a stop */
11505d442e63SKees Cook 		write_reloc(0, stdout);
11516520fe55SH. Peter Anvin 
11526520fe55SH. Peter Anvin 		/* Now print each relocation */
11535d442e63SKees Cook 		for (i = 0; i < relocs32.count; i++)
11545d442e63SKees Cook 			write_reloc(relocs32.offset[i], stdout);
11556520fe55SH. Peter Anvin 	}
11566520fe55SH. Peter Anvin }
11576520fe55SH. Peter Anvin 
1158214a8876SMichael Davidson /*
1159214a8876SMichael Davidson  * As an aid to debugging problems with different linkers
1160214a8876SMichael Davidson  * print summary information about the relocs.
1161214a8876SMichael Davidson  * Since different linkers tend to emit the sections in
1162214a8876SMichael Davidson  * different orders we use the section names in the output.
1163214a8876SMichael Davidson  */
do_reloc_info(struct section * sec,Elf_Rel * rel,ElfW (Sym)* sym,const char * symname)1164214a8876SMichael Davidson static int do_reloc_info(struct section *sec, Elf_Rel *rel, ElfW(Sym) *sym,
1165214a8876SMichael Davidson 				const char *symname)
1166214a8876SMichael Davidson {
1167214a8876SMichael Davidson 	printf("%s\t%s\t%s\t%s\n",
1168214a8876SMichael Davidson 		sec_name(sec->shdr.sh_info),
1169214a8876SMichael Davidson 		rel_type(ELF_R_TYPE(rel->r_info)),
1170214a8876SMichael Davidson 		symname,
1171a54c401aSKristen Carlson Accardi 		sec_name(sym_index(sym)));
1172214a8876SMichael Davidson 	return 0;
1173214a8876SMichael Davidson }
1174214a8876SMichael Davidson 
print_reloc_info(void)1175214a8876SMichael Davidson static void print_reloc_info(void)
1176214a8876SMichael Davidson {
1177214a8876SMichael Davidson 	printf("reloc section\treloc type\tsymbol\tsymbol section\n");
1178214a8876SMichael Davidson 	walk_relocs(do_reloc_info);
1179214a8876SMichael Davidson }
1180214a8876SMichael Davidson 
1181c889ba80SH. Peter Anvin #if ELF_BITS == 64
1182c889ba80SH. Peter Anvin # define process process_64
1183c889ba80SH. Peter Anvin #else
1184c889ba80SH. Peter Anvin # define process process_32
1185c889ba80SH. Peter Anvin #endif
11866520fe55SH. Peter Anvin 
process(FILE * fp,int use_real_mode,int as_text,int show_absolute_syms,int show_absolute_relocs,int show_reloc_info)1187c889ba80SH. Peter Anvin void process(FILE *fp, int use_real_mode, int as_text,
1188214a8876SMichael Davidson 	     int show_absolute_syms, int show_absolute_relocs,
1189214a8876SMichael Davidson 	     int show_reloc_info)
11906520fe55SH. Peter Anvin {
11916520fe55SH. Peter Anvin 	regex_init(use_real_mode);
11926520fe55SH. Peter Anvin 	read_ehdr(fp);
11936520fe55SH. Peter Anvin 	read_shdrs(fp);
11946520fe55SH. Peter Anvin 	read_strtabs(fp);
11956520fe55SH. Peter Anvin 	read_symtabs(fp);
11966520fe55SH. Peter Anvin 	read_relocs(fp);
1197c889ba80SH. Peter Anvin 	if (ELF_BITS == 64)
1198946166afSKees Cook 		percpu_init();
11996520fe55SH. Peter Anvin 	if (show_absolute_syms) {
12006520fe55SH. Peter Anvin 		print_absolute_symbols();
1201c889ba80SH. Peter Anvin 		return;
12026520fe55SH. Peter Anvin 	}
12036520fe55SH. Peter Anvin 	if (show_absolute_relocs) {
12046520fe55SH. Peter Anvin 		print_absolute_relocs();
1205c889ba80SH. Peter Anvin 		return;
12066520fe55SH. Peter Anvin 	}
1207214a8876SMichael Davidson 	if (show_reloc_info) {
1208214a8876SMichael Davidson 		print_reloc_info();
1209214a8876SMichael Davidson 		return;
1210214a8876SMichael Davidson 	}
12116520fe55SH. Peter Anvin 	emit_relocs(as_text, use_real_mode);
12126520fe55SH. Peter Anvin }
1213