Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /* This is included from relocs_32/64.c */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4) #define ElfW(type)		_ElfW(ELF_BITS, type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5) #define _ElfW(bits, type)	__ElfW(bits, type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) #define __ElfW(bits, type)	Elf##bits##_##type
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #define Elf_Addr		ElfW(Addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #define Elf_Ehdr		ElfW(Ehdr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #define Elf_Phdr		ElfW(Phdr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #define Elf_Shdr		ElfW(Shdr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #define Elf_Sym			ElfW(Sym)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) static Elf_Ehdr ehdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) struct relocs {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) 	uint32_t	*offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) 	unsigned long	count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) 	unsigned long	size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) static struct relocs relocs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) struct section {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 	Elf_Shdr       shdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 	struct section *link;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 	Elf_Sym        *symtab;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 	Elf_Rel        *reltab;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 	char           *strtab;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 	long           shdr_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) static struct section *secs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) static const char * const regex_sym_kernel = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) /* Symbols matching these regex's should never be relocated */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	"^(__crc_)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) static regex_t sym_regex_c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) static int regex_skip_reloc(const char *sym_name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	return !regexec(&sym_regex_c, sym_name, 0, NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) static void regex_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	char errbuf[128];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	err = regcomp(&sym_regex_c, regex_sym_kernel,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 			REG_EXTENDED|REG_NOSUB);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 		regerror(err, &sym_regex_c, errbuf, sizeof(errbuf));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 		die("%s", errbuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) static const char *rel_type(unsigned type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	static const char * const type_name[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) #define REL_TYPE(X)[X] = #X
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 		REL_TYPE(R_MIPS_NONE),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 		REL_TYPE(R_MIPS_16),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 		REL_TYPE(R_MIPS_32),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 		REL_TYPE(R_MIPS_REL32),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 		REL_TYPE(R_MIPS_26),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 		REL_TYPE(R_MIPS_HI16),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 		REL_TYPE(R_MIPS_LO16),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 		REL_TYPE(R_MIPS_GPREL16),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 		REL_TYPE(R_MIPS_LITERAL),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 		REL_TYPE(R_MIPS_GOT16),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 		REL_TYPE(R_MIPS_PC16),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 		REL_TYPE(R_MIPS_CALL16),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 		REL_TYPE(R_MIPS_GPREL32),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 		REL_TYPE(R_MIPS_64),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 		REL_TYPE(R_MIPS_HIGHER),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 		REL_TYPE(R_MIPS_HIGHEST),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 		REL_TYPE(R_MIPS_PC21_S2),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 		REL_TYPE(R_MIPS_PC26_S2),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) #undef REL_TYPE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	const char *name = "unknown type rel type name";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	if (type < ARRAY_SIZE(type_name) && type_name[type])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 		name = type_name[type];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	return name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) static const char *sec_name(unsigned shndx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	const char *sec_strtab;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	const char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	sec_strtab = secs[ehdr.e_shstrndx].strtab;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	if (shndx < ehdr.e_shnum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 		name = sec_strtab + secs[shndx].shdr.sh_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	else if (shndx == SHN_ABS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 		name = "ABSOLUTE";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	else if (shndx == SHN_COMMON)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 		name = "COMMON";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 		name = "<noname>";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	return name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) static struct section *sec_lookup(const char *secname)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	for (i = 0; i < ehdr.e_shnum; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 		if (strcmp(secname, sec_name(i)) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 			return &secs[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) static const char *sym_name(const char *sym_strtab, Elf_Sym *sym)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	const char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	if (sym->st_name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 		name = sym_strtab + sym->st_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 		name = sec_name(sym->st_shndx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	return name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) #if BYTE_ORDER == LITTLE_ENDIAN
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) #define le16_to_cpu(val) (val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) #define le32_to_cpu(val) (val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) #define le64_to_cpu(val) (val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) #define be16_to_cpu(val) bswap_16(val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) #define be32_to_cpu(val) bswap_32(val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) #define be64_to_cpu(val) bswap_64(val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) #define cpu_to_le16(val) (val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) #define cpu_to_le32(val) (val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) #define cpu_to_le64(val) (val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) #define cpu_to_be16(val) bswap_16(val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) #define cpu_to_be32(val) bswap_32(val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) #define cpu_to_be64(val) bswap_64(val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) #if BYTE_ORDER == BIG_ENDIAN
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) #define le16_to_cpu(val) bswap_16(val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) #define le32_to_cpu(val) bswap_32(val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) #define le64_to_cpu(val) bswap_64(val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) #define be16_to_cpu(val) (val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) #define be32_to_cpu(val) (val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) #define be64_to_cpu(val) (val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) #define cpu_to_le16(val) bswap_16(val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) #define cpu_to_le32(val) bswap_32(val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) #define cpu_to_le64(val) bswap_64(val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) #define cpu_to_be16(val) (val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) #define cpu_to_be32(val) (val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) #define cpu_to_be64(val) (val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) static uint16_t elf16_to_cpu(uint16_t val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	if (ehdr.e_ident[EI_DATA] == ELFDATA2LSB)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 		return le16_to_cpu(val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 		return be16_to_cpu(val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) static uint32_t elf32_to_cpu(uint32_t val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	if (ehdr.e_ident[EI_DATA] == ELFDATA2LSB)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 		return le32_to_cpu(val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 		return be32_to_cpu(val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) static uint32_t cpu_to_elf32(uint32_t val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	if (ehdr.e_ident[EI_DATA] == ELFDATA2LSB)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 		return cpu_to_le32(val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 		return cpu_to_be32(val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) #define elf_half_to_cpu(x)	elf16_to_cpu(x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) #define elf_word_to_cpu(x)	elf32_to_cpu(x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) #if ELF_BITS == 64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) static uint64_t elf64_to_cpu(uint64_t val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	if (ehdr.e_ident[EI_DATA] == ELFDATA2LSB)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 		return le64_to_cpu(val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 		return be64_to_cpu(val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) #define elf_addr_to_cpu(x)	elf64_to_cpu(x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) #define elf_off_to_cpu(x)	elf64_to_cpu(x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) #define elf_xword_to_cpu(x)	elf64_to_cpu(x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) #define elf_addr_to_cpu(x)	elf32_to_cpu(x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) #define elf_off_to_cpu(x)	elf32_to_cpu(x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) #define elf_xword_to_cpu(x)	elf32_to_cpu(x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) static void read_ehdr(FILE *fp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	if (fread(&ehdr, sizeof(ehdr), 1, fp) != 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 		die("Cannot read ELF header: %s\n", strerror(errno));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	if (memcmp(ehdr.e_ident, ELFMAG, SELFMAG) != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 		die("No ELF magic\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	if (ehdr.e_ident[EI_CLASS] != ELF_CLASS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 		die("Not a %d bit executable\n", ELF_BITS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 	if ((ehdr.e_ident[EI_DATA] != ELFDATA2LSB) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 	    (ehdr.e_ident[EI_DATA] != ELFDATA2MSB))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 		die("Unknown ELF Endianness\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	if (ehdr.e_ident[EI_VERSION] != EV_CURRENT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 		die("Unknown ELF version\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	/* Convert the fields to native endian */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	ehdr.e_type      = elf_half_to_cpu(ehdr.e_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	ehdr.e_machine   = elf_half_to_cpu(ehdr.e_machine);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 	ehdr.e_version   = elf_word_to_cpu(ehdr.e_version);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 	ehdr.e_entry     = elf_addr_to_cpu(ehdr.e_entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	ehdr.e_phoff     = elf_off_to_cpu(ehdr.e_phoff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 	ehdr.e_shoff     = elf_off_to_cpu(ehdr.e_shoff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	ehdr.e_flags     = elf_word_to_cpu(ehdr.e_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 	ehdr.e_ehsize    = elf_half_to_cpu(ehdr.e_ehsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	ehdr.e_phentsize = elf_half_to_cpu(ehdr.e_phentsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	ehdr.e_phnum     = elf_half_to_cpu(ehdr.e_phnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 	ehdr.e_shentsize = elf_half_to_cpu(ehdr.e_shentsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	ehdr.e_shnum     = elf_half_to_cpu(ehdr.e_shnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	ehdr.e_shstrndx  = elf_half_to_cpu(ehdr.e_shstrndx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 	if ((ehdr.e_type != ET_EXEC) && (ehdr.e_type != ET_DYN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 		die("Unsupported ELF header type\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 	if (ehdr.e_machine != ELF_MACHINE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 		die("Not for %s\n", ELF_MACHINE_NAME);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	if (ehdr.e_version != EV_CURRENT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 		die("Unknown ELF version\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	if (ehdr.e_ehsize != sizeof(Elf_Ehdr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 		die("Bad Elf header size\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 	if (ehdr.e_phentsize != sizeof(Elf_Phdr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 		die("Bad program header entry\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 	if (ehdr.e_shentsize != sizeof(Elf_Shdr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 		die("Bad section header entry\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 	if (ehdr.e_shstrndx >= ehdr.e_shnum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 		die("String table index out of bounds\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) static void read_shdrs(FILE *fp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 	Elf_Shdr shdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 	secs = calloc(ehdr.e_shnum, sizeof(struct section));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 	if (!secs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 		die("Unable to allocate %d section headers\n", ehdr.e_shnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 	if (fseek(fp, ehdr.e_shoff, SEEK_SET) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 		die("Seek to %d failed: %s\n", ehdr.e_shoff, strerror(errno));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 	for (i = 0; i < ehdr.e_shnum; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 		struct section *sec = &secs[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 		sec->shdr_offset = ftell(fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 		if (fread(&shdr, sizeof(shdr), 1, fp) != 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 			die("Cannot read ELF section headers %d/%d: %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 			    i, ehdr.e_shnum, strerror(errno));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 		sec->shdr.sh_name      = elf_word_to_cpu(shdr.sh_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 		sec->shdr.sh_type      = elf_word_to_cpu(shdr.sh_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 		sec->shdr.sh_flags     = elf_xword_to_cpu(shdr.sh_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 		sec->shdr.sh_addr      = elf_addr_to_cpu(shdr.sh_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 		sec->shdr.sh_offset    = elf_off_to_cpu(shdr.sh_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 		sec->shdr.sh_size      = elf_xword_to_cpu(shdr.sh_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 		sec->shdr.sh_link      = elf_word_to_cpu(shdr.sh_link);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 		sec->shdr.sh_info      = elf_word_to_cpu(shdr.sh_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 		sec->shdr.sh_addralign = elf_xword_to_cpu(shdr.sh_addralign);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 		sec->shdr.sh_entsize   = elf_xword_to_cpu(shdr.sh_entsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 		if (sec->shdr.sh_link < ehdr.e_shnum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 			sec->link = &secs[sec->shdr.sh_link];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) static void read_strtabs(FILE *fp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	for (i = 0; i < ehdr.e_shnum; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 		struct section *sec = &secs[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 		if (sec->shdr.sh_type != SHT_STRTAB)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 		sec->strtab = malloc(sec->shdr.sh_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 		if (!sec->strtab)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 			die("malloc of %d bytes for strtab failed\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 			    sec->shdr.sh_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 		if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 			die("Seek to %d failed: %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 			    sec->shdr.sh_offset, strerror(errno));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 		if (fread(sec->strtab, 1, sec->shdr.sh_size, fp) !=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 		    sec->shdr.sh_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 			die("Cannot read symbol table: %s\n", strerror(errno));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) static void read_symtabs(FILE *fp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 	int i, j;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 	for (i = 0; i < ehdr.e_shnum; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 		struct section *sec = &secs[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 		if (sec->shdr.sh_type != SHT_SYMTAB)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 		sec->symtab = malloc(sec->shdr.sh_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 		if (!sec->symtab)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 			die("malloc of %d bytes for symtab failed\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 			    sec->shdr.sh_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 		if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 			die("Seek to %d failed: %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 			    sec->shdr.sh_offset, strerror(errno));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 		if (fread(sec->symtab, 1, sec->shdr.sh_size, fp) !=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 		    sec->shdr.sh_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 			die("Cannot read symbol table: %s\n", strerror(errno));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 		for (j = 0; j < sec->shdr.sh_size/sizeof(Elf_Sym); j++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 			Elf_Sym *sym = &sec->symtab[j];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 			sym->st_name  = elf_word_to_cpu(sym->st_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 			sym->st_value = elf_addr_to_cpu(sym->st_value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 			sym->st_size  = elf_xword_to_cpu(sym->st_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 			sym->st_shndx = elf_half_to_cpu(sym->st_shndx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) static void read_relocs(FILE *fp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 	static unsigned long base = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 	int i, j;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 	if (!base) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 		struct section *sec = sec_lookup(".text");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 		if (!sec)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 			die("Could not find .text section\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 		base = sec->shdr.sh_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 	for (i = 0; i < ehdr.e_shnum; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 		struct section *sec = &secs[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 		if (sec->shdr.sh_type != SHT_REL_TYPE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 		sec->reltab = malloc(sec->shdr.sh_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 		if (!sec->reltab)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 			die("malloc of %d bytes for relocs failed\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 			    sec->shdr.sh_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 		if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 			die("Seek to %d failed: %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 			    sec->shdr.sh_offset, strerror(errno));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 		if (fread(sec->reltab, 1, sec->shdr.sh_size, fp) !=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 		    sec->shdr.sh_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 			die("Cannot read symbol table: %s\n", strerror(errno));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 		for (j = 0; j < sec->shdr.sh_size/sizeof(Elf_Rel); j++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 			Elf_Rel *rel = &sec->reltab[j];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 			rel->r_offset = elf_addr_to_cpu(rel->r_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 			/* Set offset into kernel image */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 			rel->r_offset -= base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) #if (ELF_BITS == 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 			rel->r_info   = elf_xword_to_cpu(rel->r_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 			/* Convert MIPS64 RELA format - only the symbol
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 			 * index needs converting to native endianness
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 			rel->r_info   = rel->r_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 			ELF_R_SYM(rel->r_info) = elf32_to_cpu(ELF_R_SYM(rel->r_info));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) #if (SHT_REL_TYPE == SHT_RELA)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 			rel->r_addend = elf_xword_to_cpu(rel->r_addend);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) static void remove_relocs(FILE *fp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 	Elf_Shdr shdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 	for (i = 0; i < ehdr.e_shnum; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 		struct section *sec = &secs[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 		if (sec->shdr.sh_type != SHT_REL_TYPE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 		if (fseek(fp, sec->shdr_offset, SEEK_SET) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 			die("Seek to %d failed: %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 			    sec->shdr_offset, strerror(errno));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 		if (fread(&shdr, sizeof(shdr), 1, fp) != 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 			die("Cannot read ELF section headers %d/%d: %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 			    i, ehdr.e_shnum, strerror(errno));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 		/* Set relocation section size to 0, effectively removing it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 		 * This is necessary due to lack of support for relocations
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 		 * in objcopy when creating 32bit elf from 64bit elf.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 		shdr.sh_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 		if (fseek(fp, sec->shdr_offset, SEEK_SET) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 			die("Seek to %d failed: %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 			    sec->shdr_offset, strerror(errno));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 		if (fwrite(&shdr, sizeof(shdr), 1, fp) != 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 			die("Cannot write ELF section headers %d/%d: %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 			    i, ehdr.e_shnum, strerror(errno));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) static void add_reloc(struct relocs *r, uint32_t offset, unsigned type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 	/* Relocation representation in binary table:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 	 * |76543210|76543210|76543210|76543210|
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 	 * |  Type  |  offset from _text >> 2  |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 	offset >>= 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 	if (offset > 0x00FFFFFF)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 		die("Kernel image exceeds maximum size for relocation!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 	offset = (offset & 0x00FFFFFF) | ((type & 0xFF) << 24);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 	if (r->count == r->size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 		unsigned long newsize = r->size + 50000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 		void *mem = realloc(r->offset, newsize * sizeof(r->offset[0]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 		if (!mem)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 			die("realloc failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 		r->offset = mem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 		r->size = newsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 	r->offset[r->count++] = offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) static void walk_relocs(int (*process)(struct section *sec, Elf_Rel *rel,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 			Elf_Sym *sym, const char *symname))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 	/* Walk through the relocations */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 	for (i = 0; i < ehdr.e_shnum; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 		char *sym_strtab;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 		Elf_Sym *sh_symtab;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 		struct section *sec_applies, *sec_symtab;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 		int j;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 		struct section *sec = &secs[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 		if (sec->shdr.sh_type != SHT_REL_TYPE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 		sec_symtab  = sec->link;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 		sec_applies = &secs[sec->shdr.sh_info];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 		if (!(sec_applies->shdr.sh_flags & SHF_ALLOC))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 		sh_symtab = sec_symtab->symtab;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 		sym_strtab = sec_symtab->link->strtab;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 		for (j = 0; j < sec->shdr.sh_size/sizeof(Elf_Rel); j++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 			Elf_Rel *rel = &sec->reltab[j];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 			Elf_Sym *sym = &sh_symtab[ELF_R_SYM(rel->r_info)];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 			const char *symname = sym_name(sym_strtab, sym);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 			process(sec, rel, sym, symname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) static int do_reloc(struct section *sec, Elf_Rel *rel, Elf_Sym *sym,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 		      const char *symname)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 	unsigned r_type = ELF_R_TYPE(rel->r_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 	unsigned bind = ELF_ST_BIND(sym->st_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 	if ((bind == STB_WEAK) && (sym->st_value == 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 		/* Don't relocate weak symbols without a target */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 	if (regex_skip_reloc(symname))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 	switch (r_type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 	case R_MIPS_NONE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 	case R_MIPS_REL32:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 	case R_MIPS_PC16:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 	case R_MIPS_PC21_S2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 	case R_MIPS_PC26_S2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) 		 * NONE can be ignored and PC relative relocations don't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 		 * need to be adjusted.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) 	case R_MIPS_HIGHEST:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 	case R_MIPS_HIGHER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 		/* We support relocating within the same 4Gb segment only,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) 		 * thus leaving the top 32bits unchanged
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) 	case R_MIPS_LO16:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) 		/* We support relocating by 64k jumps only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) 		 * thus leaving the bottom 16bits unchanged
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) 	case R_MIPS_64:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) 	case R_MIPS_32:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) 	case R_MIPS_26:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) 	case R_MIPS_HI16:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) 		add_reloc(&relocs, rel->r_offset, r_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) 		die("Unsupported relocation type: %s (%d)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) 		    rel_type(r_type), r_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) static int write_reloc_as_bin(uint32_t v, FILE *f)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) 	unsigned char buf[4];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) 	v = cpu_to_elf32(v);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) 	memcpy(buf, &v, sizeof(uint32_t));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) 	return fwrite(buf, 1, 4, f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) static int write_reloc_as_text(uint32_t v, FILE *f)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) 	int res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) 	res = fprintf(f, "\t.long 0x%08"PRIx32"\n", v);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) 	if (res < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) 		return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) 		return sizeof(uint32_t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) static void emit_relocs(int as_text, int as_bin, FILE *outf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) 	int (*write_reloc)(uint32_t, FILE *) = write_reloc_as_bin;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) 	int size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) 	int size_reserved;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) 	struct section *sec_reloc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) 	sec_reloc = sec_lookup(".data.reloc");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) 	if (!sec_reloc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) 		die("Could not find relocation section\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) 	size_reserved = sec_reloc->shdr.sh_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) 	/* Collect up the relocations */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) 	walk_relocs(do_reloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) 	/* Print the relocations */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) 	if (as_text) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) 		/* Print the relocations in a form suitable that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) 		 * gas will like.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) 		printf(".section \".data.reloc\",\"a\"\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) 		printf(".balign 4\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) 		/* Output text to stdout */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) 		write_reloc = write_reloc_as_text;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) 		outf = stdout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) 	} else if (as_bin) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) 		/* Output raw binary to stdout */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) 		outf = stdout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) 		/* Seek to offset of the relocation section.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) 		* Each relocation is then written into the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) 		* vmlinux kernel image.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) 		*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) 		if (fseek(outf, sec_reloc->shdr.sh_offset, SEEK_SET) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) 			die("Seek to %d failed: %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) 				sec_reloc->shdr.sh_offset, strerror(errno));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) 	for (i = 0; i < relocs.count; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) 		size += write_reloc(relocs.offset[i], outf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) 	/* Print a stop, but only if we've actually written some relocs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) 	if (size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) 		size += write_reloc(0, outf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) 	if (size > size_reserved)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) 		/* Die, but suggest a value for CONFIG_RELOCATION_TABLE_SIZE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) 		 * which will fix this problem and allow a bit of headroom
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) 		 * if more kernel features are enabled
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) 		die("Relocations overflow available space!\n" \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) 		    "Please adjust CONFIG_RELOCATION_TABLE_SIZE " \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) 		    "to at least 0x%08x\n", (size + 0x1000) & ~0xFFF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631)  * As an aid to debugging problems with different linkers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632)  * print summary information about the relocs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633)  * Since different linkers tend to emit the sections in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634)  * different orders we use the section names in the output.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) static int do_reloc_info(struct section *sec, Elf_Rel *rel, ElfW(Sym) *sym,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) 				const char *symname)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) 	printf("%16s  0x%08x  %16s  %40s  %16s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) 		sec_name(sec->shdr.sh_info),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) 		(unsigned int)rel->r_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) 		rel_type(ELF_R_TYPE(rel->r_info)),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) 		symname,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) 		sec_name(sym->st_shndx));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) static void print_reloc_info(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) 	printf("%16s  %10s  %16s  %40s  %16s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) 		"reloc section",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) 		"offset",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) 		"reloc type",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) 		"symbol",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) 		"symbol section");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) 	walk_relocs(do_reloc_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) #if ELF_BITS == 64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) # define process process_64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) # define process process_32
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) void process(FILE *fp, int as_text, int as_bin,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) 	     int show_reloc_info, int keep_relocs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) 	regex_init();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) 	read_ehdr(fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) 	read_shdrs(fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) 	read_strtabs(fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) 	read_symtabs(fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) 	read_relocs(fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) 	if (show_reloc_info) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) 		print_reloc_info();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) 	emit_relocs(as_text, as_bin, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) 	if (!keep_relocs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) 		remove_relocs(fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) }