xref: /openbmc/linux/arch/mips/boot/tools/relocs.c (revision 1ac731c529cd4d6adbce134754b51ff7d822b145)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2766c5803SMatt Redfearn /* This is included from relocs_32/64.c */
3766c5803SMatt Redfearn 
4766c5803SMatt Redfearn #define ElfW(type)		_ElfW(ELF_BITS, type)
5766c5803SMatt Redfearn #define _ElfW(bits, type)	__ElfW(bits, type)
6766c5803SMatt Redfearn #define __ElfW(bits, type)	Elf##bits##_##type
7766c5803SMatt Redfearn 
8766c5803SMatt Redfearn #define Elf_Addr		ElfW(Addr)
9766c5803SMatt Redfearn #define Elf_Ehdr		ElfW(Ehdr)
10766c5803SMatt Redfearn #define Elf_Phdr		ElfW(Phdr)
11766c5803SMatt Redfearn #define Elf_Shdr		ElfW(Shdr)
12766c5803SMatt Redfearn #define Elf_Sym			ElfW(Sym)
13766c5803SMatt Redfearn 
14766c5803SMatt Redfearn static Elf_Ehdr ehdr;
15766c5803SMatt Redfearn 
16766c5803SMatt Redfearn struct relocs {
17766c5803SMatt Redfearn 	uint32_t	*offset;
18766c5803SMatt Redfearn 	unsigned long	count;
19766c5803SMatt Redfearn 	unsigned long	size;
20766c5803SMatt Redfearn };
21766c5803SMatt Redfearn 
22766c5803SMatt Redfearn static struct relocs relocs;
23766c5803SMatt Redfearn 
24766c5803SMatt Redfearn struct section {
25766c5803SMatt Redfearn 	Elf_Shdr       shdr;
26766c5803SMatt Redfearn 	struct section *link;
27766c5803SMatt Redfearn 	Elf_Sym        *symtab;
28766c5803SMatt Redfearn 	Elf_Rel        *reltab;
29766c5803SMatt Redfearn 	char           *strtab;
30766c5803SMatt Redfearn 	long           shdr_offset;
31766c5803SMatt Redfearn };
32766c5803SMatt Redfearn static struct section *secs;
33766c5803SMatt Redfearn 
34766c5803SMatt Redfearn static const char * const regex_sym_kernel = {
35766c5803SMatt Redfearn /* Symbols matching these regex's should never be relocated */
36766c5803SMatt Redfearn 	"^(__crc_)",
37766c5803SMatt Redfearn };
38766c5803SMatt Redfearn 
39766c5803SMatt Redfearn static regex_t sym_regex_c;
40766c5803SMatt Redfearn 
regex_skip_reloc(const char * sym_name)41766c5803SMatt Redfearn static int regex_skip_reloc(const char *sym_name)
42766c5803SMatt Redfearn {
43766c5803SMatt Redfearn 	return !regexec(&sym_regex_c, sym_name, 0, NULL, 0);
44766c5803SMatt Redfearn }
45766c5803SMatt Redfearn 
regex_init(void)46766c5803SMatt Redfearn static void regex_init(void)
47766c5803SMatt Redfearn {
48766c5803SMatt Redfearn 	char errbuf[128];
49766c5803SMatt Redfearn 	int err;
50766c5803SMatt Redfearn 
51766c5803SMatt Redfearn 	err = regcomp(&sym_regex_c, regex_sym_kernel,
52766c5803SMatt Redfearn 			REG_EXTENDED|REG_NOSUB);
53766c5803SMatt Redfearn 
54766c5803SMatt Redfearn 	if (err) {
55766c5803SMatt Redfearn 		regerror(err, &sym_regex_c, errbuf, sizeof(errbuf));
56766c5803SMatt Redfearn 		die("%s", errbuf);
57766c5803SMatt Redfearn 	}
58766c5803SMatt Redfearn }
59766c5803SMatt Redfearn 
rel_type(unsigned type)60766c5803SMatt Redfearn static const char *rel_type(unsigned type)
61766c5803SMatt Redfearn {
62766c5803SMatt Redfearn 	static const char * const type_name[] = {
63766c5803SMatt Redfearn #define REL_TYPE(X)[X] = #X
64766c5803SMatt Redfearn 		REL_TYPE(R_MIPS_NONE),
65766c5803SMatt Redfearn 		REL_TYPE(R_MIPS_16),
66766c5803SMatt Redfearn 		REL_TYPE(R_MIPS_32),
67766c5803SMatt Redfearn 		REL_TYPE(R_MIPS_REL32),
68766c5803SMatt Redfearn 		REL_TYPE(R_MIPS_26),
69766c5803SMatt Redfearn 		REL_TYPE(R_MIPS_HI16),
70766c5803SMatt Redfearn 		REL_TYPE(R_MIPS_LO16),
71766c5803SMatt Redfearn 		REL_TYPE(R_MIPS_GPREL16),
72766c5803SMatt Redfearn 		REL_TYPE(R_MIPS_LITERAL),
73766c5803SMatt Redfearn 		REL_TYPE(R_MIPS_GOT16),
74766c5803SMatt Redfearn 		REL_TYPE(R_MIPS_PC16),
75766c5803SMatt Redfearn 		REL_TYPE(R_MIPS_CALL16),
76766c5803SMatt Redfearn 		REL_TYPE(R_MIPS_GPREL32),
77766c5803SMatt Redfearn 		REL_TYPE(R_MIPS_64),
78766c5803SMatt Redfearn 		REL_TYPE(R_MIPS_HIGHER),
79766c5803SMatt Redfearn 		REL_TYPE(R_MIPS_HIGHEST),
80766c5803SMatt Redfearn 		REL_TYPE(R_MIPS_PC21_S2),
81766c5803SMatt Redfearn 		REL_TYPE(R_MIPS_PC26_S2),
82766c5803SMatt Redfearn #undef REL_TYPE
83766c5803SMatt Redfearn 	};
84766c5803SMatt Redfearn 	const char *name = "unknown type rel type name";
85766c5803SMatt Redfearn 
86766c5803SMatt Redfearn 	if (type < ARRAY_SIZE(type_name) && type_name[type])
87766c5803SMatt Redfearn 		name = type_name[type];
88766c5803SMatt Redfearn 	return name;
89766c5803SMatt Redfearn }
90766c5803SMatt Redfearn 
sec_name(unsigned shndx)91766c5803SMatt Redfearn static const char *sec_name(unsigned shndx)
92766c5803SMatt Redfearn {
93766c5803SMatt Redfearn 	const char *sec_strtab;
94766c5803SMatt Redfearn 	const char *name;
95766c5803SMatt Redfearn 
96766c5803SMatt Redfearn 	sec_strtab = secs[ehdr.e_shstrndx].strtab;
97766c5803SMatt Redfearn 	if (shndx < ehdr.e_shnum)
98766c5803SMatt Redfearn 		name = sec_strtab + secs[shndx].shdr.sh_name;
99766c5803SMatt Redfearn 	else if (shndx == SHN_ABS)
100766c5803SMatt Redfearn 		name = "ABSOLUTE";
101766c5803SMatt Redfearn 	else if (shndx == SHN_COMMON)
102766c5803SMatt Redfearn 		name = "COMMON";
103766c5803SMatt Redfearn 	else
104766c5803SMatt Redfearn 		name = "<noname>";
105766c5803SMatt Redfearn 	return name;
106766c5803SMatt Redfearn }
107766c5803SMatt Redfearn 
sec_lookup(const char * secname)108766c5803SMatt Redfearn static struct section *sec_lookup(const char *secname)
109766c5803SMatt Redfearn {
110766c5803SMatt Redfearn 	int i;
111766c5803SMatt Redfearn 
112766c5803SMatt Redfearn 	for (i = 0; i < ehdr.e_shnum; i++)
113766c5803SMatt Redfearn 		if (strcmp(secname, sec_name(i)) == 0)
114766c5803SMatt Redfearn 			return &secs[i];
115766c5803SMatt Redfearn 
116766c5803SMatt Redfearn 	return NULL;
117766c5803SMatt Redfearn }
118766c5803SMatt Redfearn 
sym_name(const char * sym_strtab,Elf_Sym * sym)119766c5803SMatt Redfearn static const char *sym_name(const char *sym_strtab, Elf_Sym *sym)
120766c5803SMatt Redfearn {
121766c5803SMatt Redfearn 	const char *name;
122766c5803SMatt Redfearn 
123766c5803SMatt Redfearn 	if (sym->st_name)
124766c5803SMatt Redfearn 		name = sym_strtab + sym->st_name;
125766c5803SMatt Redfearn 	else
126766c5803SMatt Redfearn 		name = sec_name(sym->st_shndx);
127766c5803SMatt Redfearn 	return name;
128766c5803SMatt Redfearn }
129766c5803SMatt Redfearn 
130766c5803SMatt Redfearn #if BYTE_ORDER == LITTLE_ENDIAN
131766c5803SMatt Redfearn #define le16_to_cpu(val) (val)
132766c5803SMatt Redfearn #define le32_to_cpu(val) (val)
133766c5803SMatt Redfearn #define le64_to_cpu(val) (val)
134766c5803SMatt Redfearn #define be16_to_cpu(val) bswap_16(val)
135766c5803SMatt Redfearn #define be32_to_cpu(val) bswap_32(val)
136766c5803SMatt Redfearn #define be64_to_cpu(val) bswap_64(val)
137766c5803SMatt Redfearn 
138766c5803SMatt Redfearn #define cpu_to_le16(val) (val)
139766c5803SMatt Redfearn #define cpu_to_le32(val) (val)
140766c5803SMatt Redfearn #define cpu_to_le64(val) (val)
141766c5803SMatt Redfearn #define cpu_to_be16(val) bswap_16(val)
142766c5803SMatt Redfearn #define cpu_to_be32(val) bswap_32(val)
143766c5803SMatt Redfearn #define cpu_to_be64(val) bswap_64(val)
144766c5803SMatt Redfearn #endif
145766c5803SMatt Redfearn #if BYTE_ORDER == BIG_ENDIAN
146766c5803SMatt Redfearn #define le16_to_cpu(val) bswap_16(val)
147766c5803SMatt Redfearn #define le32_to_cpu(val) bswap_32(val)
148766c5803SMatt Redfearn #define le64_to_cpu(val) bswap_64(val)
149766c5803SMatt Redfearn #define be16_to_cpu(val) (val)
150766c5803SMatt Redfearn #define be32_to_cpu(val) (val)
151766c5803SMatt Redfearn #define be64_to_cpu(val) (val)
152766c5803SMatt Redfearn 
153766c5803SMatt Redfearn #define cpu_to_le16(val) bswap_16(val)
154766c5803SMatt Redfearn #define cpu_to_le32(val) bswap_32(val)
155766c5803SMatt Redfearn #define cpu_to_le64(val) bswap_64(val)
156766c5803SMatt Redfearn #define cpu_to_be16(val) (val)
157766c5803SMatt Redfearn #define cpu_to_be32(val) (val)
158766c5803SMatt Redfearn #define cpu_to_be64(val) (val)
159766c5803SMatt Redfearn #endif
160766c5803SMatt Redfearn 
elf16_to_cpu(uint16_t val)161766c5803SMatt Redfearn static uint16_t elf16_to_cpu(uint16_t val)
162766c5803SMatt Redfearn {
163766c5803SMatt Redfearn 	if (ehdr.e_ident[EI_DATA] == ELFDATA2LSB)
164766c5803SMatt Redfearn 		return le16_to_cpu(val);
165766c5803SMatt Redfearn 	else
166766c5803SMatt Redfearn 		return be16_to_cpu(val);
167766c5803SMatt Redfearn }
168766c5803SMatt Redfearn 
elf32_to_cpu(uint32_t val)169766c5803SMatt Redfearn static uint32_t elf32_to_cpu(uint32_t val)
170766c5803SMatt Redfearn {
171766c5803SMatt Redfearn 	if (ehdr.e_ident[EI_DATA] == ELFDATA2LSB)
172766c5803SMatt Redfearn 		return le32_to_cpu(val);
173766c5803SMatt Redfearn 	else
174766c5803SMatt Redfearn 		return be32_to_cpu(val);
175766c5803SMatt Redfearn }
176766c5803SMatt Redfearn 
cpu_to_elf32(uint32_t val)177766c5803SMatt Redfearn static uint32_t cpu_to_elf32(uint32_t val)
178766c5803SMatt Redfearn {
179766c5803SMatt Redfearn 	if (ehdr.e_ident[EI_DATA] == ELFDATA2LSB)
180766c5803SMatt Redfearn 		return cpu_to_le32(val);
181766c5803SMatt Redfearn 	else
182766c5803SMatt Redfearn 		return cpu_to_be32(val);
183766c5803SMatt Redfearn }
184766c5803SMatt Redfearn 
185766c5803SMatt Redfearn #define elf_half_to_cpu(x)	elf16_to_cpu(x)
186766c5803SMatt Redfearn #define elf_word_to_cpu(x)	elf32_to_cpu(x)
187766c5803SMatt Redfearn 
188766c5803SMatt Redfearn #if ELF_BITS == 64
elf64_to_cpu(uint64_t val)189766c5803SMatt Redfearn static uint64_t elf64_to_cpu(uint64_t val)
190766c5803SMatt Redfearn {
191766c5803SMatt Redfearn 	if (ehdr.e_ident[EI_DATA] == ELFDATA2LSB)
192766c5803SMatt Redfearn 		return le64_to_cpu(val);
193766c5803SMatt Redfearn 	else
194766c5803SMatt Redfearn 		return be64_to_cpu(val);
195766c5803SMatt Redfearn }
196766c5803SMatt Redfearn #define elf_addr_to_cpu(x)	elf64_to_cpu(x)
197766c5803SMatt Redfearn #define elf_off_to_cpu(x)	elf64_to_cpu(x)
198766c5803SMatt Redfearn #define elf_xword_to_cpu(x)	elf64_to_cpu(x)
199766c5803SMatt Redfearn #else
200766c5803SMatt Redfearn #define elf_addr_to_cpu(x)	elf32_to_cpu(x)
201766c5803SMatt Redfearn #define elf_off_to_cpu(x)	elf32_to_cpu(x)
202766c5803SMatt Redfearn #define elf_xword_to_cpu(x)	elf32_to_cpu(x)
203766c5803SMatt Redfearn #endif
204766c5803SMatt Redfearn 
read_ehdr(FILE * fp)205766c5803SMatt Redfearn static void read_ehdr(FILE *fp)
206766c5803SMatt Redfearn {
207766c5803SMatt Redfearn 	if (fread(&ehdr, sizeof(ehdr), 1, fp) != 1)
208766c5803SMatt Redfearn 		die("Cannot read ELF header: %s\n", strerror(errno));
209766c5803SMatt Redfearn 
210766c5803SMatt Redfearn 	if (memcmp(ehdr.e_ident, ELFMAG, SELFMAG) != 0)
211766c5803SMatt Redfearn 		die("No ELF magic\n");
212766c5803SMatt Redfearn 
213766c5803SMatt Redfearn 	if (ehdr.e_ident[EI_CLASS] != ELF_CLASS)
214766c5803SMatt Redfearn 		die("Not a %d bit executable\n", ELF_BITS);
215766c5803SMatt Redfearn 
216766c5803SMatt Redfearn 	if ((ehdr.e_ident[EI_DATA] != ELFDATA2LSB) &&
217766c5803SMatt Redfearn 	    (ehdr.e_ident[EI_DATA] != ELFDATA2MSB))
218766c5803SMatt Redfearn 		die("Unknown ELF Endianness\n");
219766c5803SMatt Redfearn 
220766c5803SMatt Redfearn 	if (ehdr.e_ident[EI_VERSION] != EV_CURRENT)
221766c5803SMatt Redfearn 		die("Unknown ELF version\n");
222766c5803SMatt Redfearn 
223766c5803SMatt Redfearn 	/* Convert the fields to native endian */
224766c5803SMatt Redfearn 	ehdr.e_type      = elf_half_to_cpu(ehdr.e_type);
225766c5803SMatt Redfearn 	ehdr.e_machine   = elf_half_to_cpu(ehdr.e_machine);
226766c5803SMatt Redfearn 	ehdr.e_version   = elf_word_to_cpu(ehdr.e_version);
227766c5803SMatt Redfearn 	ehdr.e_entry     = elf_addr_to_cpu(ehdr.e_entry);
228766c5803SMatt Redfearn 	ehdr.e_phoff     = elf_off_to_cpu(ehdr.e_phoff);
229766c5803SMatt Redfearn 	ehdr.e_shoff     = elf_off_to_cpu(ehdr.e_shoff);
230766c5803SMatt Redfearn 	ehdr.e_flags     = elf_word_to_cpu(ehdr.e_flags);
231766c5803SMatt Redfearn 	ehdr.e_ehsize    = elf_half_to_cpu(ehdr.e_ehsize);
232766c5803SMatt Redfearn 	ehdr.e_phentsize = elf_half_to_cpu(ehdr.e_phentsize);
233766c5803SMatt Redfearn 	ehdr.e_phnum     = elf_half_to_cpu(ehdr.e_phnum);
234766c5803SMatt Redfearn 	ehdr.e_shentsize = elf_half_to_cpu(ehdr.e_shentsize);
235766c5803SMatt Redfearn 	ehdr.e_shnum     = elf_half_to_cpu(ehdr.e_shnum);
236766c5803SMatt Redfearn 	ehdr.e_shstrndx  = elf_half_to_cpu(ehdr.e_shstrndx);
237766c5803SMatt Redfearn 
238766c5803SMatt Redfearn 	if ((ehdr.e_type != ET_EXEC) && (ehdr.e_type != ET_DYN))
239766c5803SMatt Redfearn 		die("Unsupported ELF header type\n");
240766c5803SMatt Redfearn 
241766c5803SMatt Redfearn 	if (ehdr.e_machine != ELF_MACHINE)
242766c5803SMatt Redfearn 		die("Not for %s\n", ELF_MACHINE_NAME);
243766c5803SMatt Redfearn 
244766c5803SMatt Redfearn 	if (ehdr.e_version != EV_CURRENT)
245766c5803SMatt Redfearn 		die("Unknown ELF version\n");
246766c5803SMatt Redfearn 
247766c5803SMatt Redfearn 	if (ehdr.e_ehsize != sizeof(Elf_Ehdr))
248*70e79866SAlexey Dobriyan 		die("Bad ELF header size\n");
249766c5803SMatt Redfearn 
250766c5803SMatt Redfearn 	if (ehdr.e_phentsize != sizeof(Elf_Phdr))
251766c5803SMatt Redfearn 		die("Bad program header entry\n");
252766c5803SMatt Redfearn 
253766c5803SMatt Redfearn 	if (ehdr.e_shentsize != sizeof(Elf_Shdr))
254766c5803SMatt Redfearn 		die("Bad section header entry\n");
255766c5803SMatt Redfearn 
256766c5803SMatt Redfearn 	if (ehdr.e_shstrndx >= ehdr.e_shnum)
257766c5803SMatt Redfearn 		die("String table index out of bounds\n");
258766c5803SMatt Redfearn }
259766c5803SMatt Redfearn 
read_shdrs(FILE * fp)260766c5803SMatt Redfearn static void read_shdrs(FILE *fp)
261766c5803SMatt Redfearn {
262766c5803SMatt Redfearn 	int i;
263766c5803SMatt Redfearn 	Elf_Shdr shdr;
264766c5803SMatt Redfearn 
265766c5803SMatt Redfearn 	secs = calloc(ehdr.e_shnum, sizeof(struct section));
266766c5803SMatt Redfearn 	if (!secs)
267766c5803SMatt Redfearn 		die("Unable to allocate %d section headers\n", ehdr.e_shnum);
268766c5803SMatt Redfearn 
269766c5803SMatt Redfearn 	if (fseek(fp, ehdr.e_shoff, SEEK_SET) < 0)
270766c5803SMatt Redfearn 		die("Seek to %d failed: %s\n", ehdr.e_shoff, strerror(errno));
271766c5803SMatt Redfearn 
272766c5803SMatt Redfearn 	for (i = 0; i < ehdr.e_shnum; i++) {
273766c5803SMatt Redfearn 		struct section *sec = &secs[i];
274766c5803SMatt Redfearn 
275766c5803SMatt Redfearn 		sec->shdr_offset = ftell(fp);
276766c5803SMatt Redfearn 		if (fread(&shdr, sizeof(shdr), 1, fp) != 1)
277766c5803SMatt Redfearn 			die("Cannot read ELF section headers %d/%d: %s\n",
278766c5803SMatt Redfearn 			    i, ehdr.e_shnum, strerror(errno));
279766c5803SMatt Redfearn 		sec->shdr.sh_name      = elf_word_to_cpu(shdr.sh_name);
280766c5803SMatt Redfearn 		sec->shdr.sh_type      = elf_word_to_cpu(shdr.sh_type);
281766c5803SMatt Redfearn 		sec->shdr.sh_flags     = elf_xword_to_cpu(shdr.sh_flags);
282766c5803SMatt Redfearn 		sec->shdr.sh_addr      = elf_addr_to_cpu(shdr.sh_addr);
283766c5803SMatt Redfearn 		sec->shdr.sh_offset    = elf_off_to_cpu(shdr.sh_offset);
284766c5803SMatt Redfearn 		sec->shdr.sh_size      = elf_xword_to_cpu(shdr.sh_size);
285766c5803SMatt Redfearn 		sec->shdr.sh_link      = elf_word_to_cpu(shdr.sh_link);
286766c5803SMatt Redfearn 		sec->shdr.sh_info      = elf_word_to_cpu(shdr.sh_info);
287766c5803SMatt Redfearn 		sec->shdr.sh_addralign = elf_xword_to_cpu(shdr.sh_addralign);
288766c5803SMatt Redfearn 		sec->shdr.sh_entsize   = elf_xword_to_cpu(shdr.sh_entsize);
289766c5803SMatt Redfearn 		if (sec->shdr.sh_link < ehdr.e_shnum)
290766c5803SMatt Redfearn 			sec->link = &secs[sec->shdr.sh_link];
291766c5803SMatt Redfearn 	}
292766c5803SMatt Redfearn }
293766c5803SMatt Redfearn 
read_strtabs(FILE * fp)294766c5803SMatt Redfearn static void read_strtabs(FILE *fp)
295766c5803SMatt Redfearn {
296766c5803SMatt Redfearn 	int i;
297766c5803SMatt Redfearn 
298766c5803SMatt Redfearn 	for (i = 0; i < ehdr.e_shnum; i++) {
299766c5803SMatt Redfearn 		struct section *sec = &secs[i];
300766c5803SMatt Redfearn 
301766c5803SMatt Redfearn 		if (sec->shdr.sh_type != SHT_STRTAB)
302766c5803SMatt Redfearn 			continue;
303766c5803SMatt Redfearn 
304766c5803SMatt Redfearn 		sec->strtab = malloc(sec->shdr.sh_size);
305766c5803SMatt Redfearn 		if (!sec->strtab)
306766c5803SMatt Redfearn 			die("malloc of %d bytes for strtab failed\n",
307766c5803SMatt Redfearn 			    sec->shdr.sh_size);
308766c5803SMatt Redfearn 
309766c5803SMatt Redfearn 		if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0)
310766c5803SMatt Redfearn 			die("Seek to %d failed: %s\n",
311766c5803SMatt Redfearn 			    sec->shdr.sh_offset, strerror(errno));
312766c5803SMatt Redfearn 
313766c5803SMatt Redfearn 		if (fread(sec->strtab, 1, sec->shdr.sh_size, fp) !=
314766c5803SMatt Redfearn 		    sec->shdr.sh_size)
315766c5803SMatt Redfearn 			die("Cannot read symbol table: %s\n", strerror(errno));
316766c5803SMatt Redfearn 	}
317766c5803SMatt Redfearn }
318766c5803SMatt Redfearn 
read_symtabs(FILE * fp)319766c5803SMatt Redfearn static void read_symtabs(FILE *fp)
320766c5803SMatt Redfearn {
321766c5803SMatt Redfearn 	int i, j;
322766c5803SMatt Redfearn 
323766c5803SMatt Redfearn 	for (i = 0; i < ehdr.e_shnum; i++) {
324766c5803SMatt Redfearn 		struct section *sec = &secs[i];
325766c5803SMatt Redfearn 		if (sec->shdr.sh_type != SHT_SYMTAB)
326766c5803SMatt Redfearn 			continue;
327766c5803SMatt Redfearn 
328766c5803SMatt Redfearn 		sec->symtab = malloc(sec->shdr.sh_size);
329766c5803SMatt Redfearn 		if (!sec->symtab)
330766c5803SMatt Redfearn 			die("malloc of %d bytes for symtab failed\n",
331766c5803SMatt Redfearn 			    sec->shdr.sh_size);
332766c5803SMatt Redfearn 
333766c5803SMatt Redfearn 		if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0)
334766c5803SMatt Redfearn 			die("Seek to %d failed: %s\n",
335766c5803SMatt Redfearn 			    sec->shdr.sh_offset, strerror(errno));
336766c5803SMatt Redfearn 
337766c5803SMatt Redfearn 		if (fread(sec->symtab, 1, sec->shdr.sh_size, fp) !=
338766c5803SMatt Redfearn 		    sec->shdr.sh_size)
339766c5803SMatt Redfearn 			die("Cannot read symbol table: %s\n", strerror(errno));
340766c5803SMatt Redfearn 
341766c5803SMatt Redfearn 		for (j = 0; j < sec->shdr.sh_size/sizeof(Elf_Sym); j++) {
342766c5803SMatt Redfearn 			Elf_Sym *sym = &sec->symtab[j];
343766c5803SMatt Redfearn 
344766c5803SMatt Redfearn 			sym->st_name  = elf_word_to_cpu(sym->st_name);
345766c5803SMatt Redfearn 			sym->st_value = elf_addr_to_cpu(sym->st_value);
346766c5803SMatt Redfearn 			sym->st_size  = elf_xword_to_cpu(sym->st_size);
347766c5803SMatt Redfearn 			sym->st_shndx = elf_half_to_cpu(sym->st_shndx);
348766c5803SMatt Redfearn 		}
349766c5803SMatt Redfearn 	}
350766c5803SMatt Redfearn }
351766c5803SMatt Redfearn 
read_relocs(FILE * fp)352766c5803SMatt Redfearn static void read_relocs(FILE *fp)
353766c5803SMatt Redfearn {
354a4fa106eSJason Wang 	static unsigned long base;
355766c5803SMatt Redfearn 	int i, j;
356766c5803SMatt Redfearn 
357766c5803SMatt Redfearn 	if (!base) {
358766c5803SMatt Redfearn 		struct section *sec = sec_lookup(".text");
359766c5803SMatt Redfearn 
360766c5803SMatt Redfearn 		if (!sec)
361766c5803SMatt Redfearn 			die("Could not find .text section\n");
362766c5803SMatt Redfearn 
363766c5803SMatt Redfearn 		base = sec->shdr.sh_addr;
364766c5803SMatt Redfearn 	}
365766c5803SMatt Redfearn 
366766c5803SMatt Redfearn 	for (i = 0; i < ehdr.e_shnum; i++) {
367766c5803SMatt Redfearn 		struct section *sec = &secs[i];
368766c5803SMatt Redfearn 
369766c5803SMatt Redfearn 		if (sec->shdr.sh_type != SHT_REL_TYPE)
370766c5803SMatt Redfearn 			continue;
371766c5803SMatt Redfearn 
372766c5803SMatt Redfearn 		sec->reltab = malloc(sec->shdr.sh_size);
373766c5803SMatt Redfearn 		if (!sec->reltab)
374766c5803SMatt Redfearn 			die("malloc of %d bytes for relocs failed\n",
375766c5803SMatt Redfearn 			    sec->shdr.sh_size);
376766c5803SMatt Redfearn 
377766c5803SMatt Redfearn 		if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0)
378766c5803SMatt Redfearn 			die("Seek to %d failed: %s\n",
379766c5803SMatt Redfearn 			    sec->shdr.sh_offset, strerror(errno));
380766c5803SMatt Redfearn 
381766c5803SMatt Redfearn 		if (fread(sec->reltab, 1, sec->shdr.sh_size, fp) !=
382766c5803SMatt Redfearn 		    sec->shdr.sh_size)
383766c5803SMatt Redfearn 			die("Cannot read symbol table: %s\n", strerror(errno));
384766c5803SMatt Redfearn 
385766c5803SMatt Redfearn 		for (j = 0; j < sec->shdr.sh_size/sizeof(Elf_Rel); j++) {
386766c5803SMatt Redfearn 			Elf_Rel *rel = &sec->reltab[j];
387766c5803SMatt Redfearn 
388766c5803SMatt Redfearn 			rel->r_offset = elf_addr_to_cpu(rel->r_offset);
389766c5803SMatt Redfearn 			/* Set offset into kernel image */
390766c5803SMatt Redfearn 			rel->r_offset -= base;
391766c5803SMatt Redfearn #if (ELF_BITS == 32)
392766c5803SMatt Redfearn 			rel->r_info   = elf_xword_to_cpu(rel->r_info);
393766c5803SMatt Redfearn #else
394766c5803SMatt Redfearn 			/* Convert MIPS64 RELA format - only the symbol
395766c5803SMatt Redfearn 			 * index needs converting to native endianness
396766c5803SMatt Redfearn 			 */
397766c5803SMatt Redfearn 			rel->r_info   = rel->r_info;
398766c5803SMatt Redfearn 			ELF_R_SYM(rel->r_info) = elf32_to_cpu(ELF_R_SYM(rel->r_info));
399766c5803SMatt Redfearn #endif
400766c5803SMatt Redfearn #if (SHT_REL_TYPE == SHT_RELA)
401766c5803SMatt Redfearn 			rel->r_addend = elf_xword_to_cpu(rel->r_addend);
402766c5803SMatt Redfearn #endif
403766c5803SMatt Redfearn 		}
404766c5803SMatt Redfearn 	}
405766c5803SMatt Redfearn }
406766c5803SMatt Redfearn 
remove_relocs(FILE * fp)407766c5803SMatt Redfearn static void remove_relocs(FILE *fp)
408766c5803SMatt Redfearn {
409766c5803SMatt Redfearn 	int i;
410766c5803SMatt Redfearn 	Elf_Shdr shdr;
411766c5803SMatt Redfearn 
412766c5803SMatt Redfearn 	for (i = 0; i < ehdr.e_shnum; i++) {
413766c5803SMatt Redfearn 		struct section *sec = &secs[i];
414766c5803SMatt Redfearn 
415766c5803SMatt Redfearn 		if (sec->shdr.sh_type != SHT_REL_TYPE)
416766c5803SMatt Redfearn 			continue;
417766c5803SMatt Redfearn 
418766c5803SMatt Redfearn 		if (fseek(fp, sec->shdr_offset, SEEK_SET) < 0)
419766c5803SMatt Redfearn 			die("Seek to %d failed: %s\n",
420766c5803SMatt Redfearn 			    sec->shdr_offset, strerror(errno));
421766c5803SMatt Redfearn 
422766c5803SMatt Redfearn 		if (fread(&shdr, sizeof(shdr), 1, fp) != 1)
423766c5803SMatt Redfearn 			die("Cannot read ELF section headers %d/%d: %s\n",
424766c5803SMatt Redfearn 			    i, ehdr.e_shnum, strerror(errno));
425766c5803SMatt Redfearn 
426766c5803SMatt Redfearn 		/* Set relocation section size to 0, effectively removing it.
427766c5803SMatt Redfearn 		 * This is necessary due to lack of support for relocations
428766c5803SMatt Redfearn 		 * in objcopy when creating 32bit elf from 64bit elf.
429766c5803SMatt Redfearn 		 */
430766c5803SMatt Redfearn 		shdr.sh_size = 0;
431766c5803SMatt Redfearn 
432766c5803SMatt Redfearn 		if (fseek(fp, sec->shdr_offset, SEEK_SET) < 0)
433766c5803SMatt Redfearn 			die("Seek to %d failed: %s\n",
434766c5803SMatt Redfearn 			    sec->shdr_offset, strerror(errno));
435766c5803SMatt Redfearn 
436766c5803SMatt Redfearn 		if (fwrite(&shdr, sizeof(shdr), 1, fp) != 1)
437766c5803SMatt Redfearn 			die("Cannot write ELF section headers %d/%d: %s\n",
438766c5803SMatt Redfearn 			    i, ehdr.e_shnum, strerror(errno));
439766c5803SMatt Redfearn 	}
440766c5803SMatt Redfearn }
441766c5803SMatt Redfearn 
add_reloc(struct relocs * r,uint32_t offset,unsigned type)442766c5803SMatt Redfearn static void add_reloc(struct relocs *r, uint32_t offset, unsigned type)
443766c5803SMatt Redfearn {
444766c5803SMatt Redfearn 	/* Relocation representation in binary table:
445766c5803SMatt Redfearn 	 * |76543210|76543210|76543210|76543210|
446766c5803SMatt Redfearn 	 * |  Type  |  offset from _text >> 2  |
447766c5803SMatt Redfearn 	 */
448766c5803SMatt Redfearn 	offset >>= 2;
449766c5803SMatt Redfearn 	if (offset > 0x00FFFFFF)
450766c5803SMatt Redfearn 		die("Kernel image exceeds maximum size for relocation!\n");
451766c5803SMatt Redfearn 
452766c5803SMatt Redfearn 	offset = (offset & 0x00FFFFFF) | ((type & 0xFF) << 24);
453766c5803SMatt Redfearn 
454766c5803SMatt Redfearn 	if (r->count == r->size) {
455766c5803SMatt Redfearn 		unsigned long newsize = r->size + 50000;
456766c5803SMatt Redfearn 		void *mem = realloc(r->offset, newsize * sizeof(r->offset[0]));
457766c5803SMatt Redfearn 
458766c5803SMatt Redfearn 		if (!mem)
459766c5803SMatt Redfearn 			die("realloc failed\n");
460766c5803SMatt Redfearn 
461766c5803SMatt Redfearn 		r->offset = mem;
462766c5803SMatt Redfearn 		r->size = newsize;
463766c5803SMatt Redfearn 	}
464766c5803SMatt Redfearn 	r->offset[r->count++] = offset;
465766c5803SMatt Redfearn }
466766c5803SMatt Redfearn 
walk_relocs(int (* process)(struct section * sec,Elf_Rel * rel,Elf_Sym * sym,const char * symname))467766c5803SMatt Redfearn static void walk_relocs(int (*process)(struct section *sec, Elf_Rel *rel,
468766c5803SMatt Redfearn 			Elf_Sym *sym, const char *symname))
469766c5803SMatt Redfearn {
470766c5803SMatt Redfearn 	int i;
471766c5803SMatt Redfearn 
472766c5803SMatt Redfearn 	/* Walk through the relocations */
473766c5803SMatt Redfearn 	for (i = 0; i < ehdr.e_shnum; i++) {
474766c5803SMatt Redfearn 		char *sym_strtab;
475766c5803SMatt Redfearn 		Elf_Sym *sh_symtab;
476766c5803SMatt Redfearn 		struct section *sec_applies, *sec_symtab;
477766c5803SMatt Redfearn 		int j;
478766c5803SMatt Redfearn 		struct section *sec = &secs[i];
479766c5803SMatt Redfearn 
480766c5803SMatt Redfearn 		if (sec->shdr.sh_type != SHT_REL_TYPE)
481766c5803SMatt Redfearn 			continue;
482766c5803SMatt Redfearn 
483766c5803SMatt Redfearn 		sec_symtab  = sec->link;
484766c5803SMatt Redfearn 		sec_applies = &secs[sec->shdr.sh_info];
485766c5803SMatt Redfearn 		if (!(sec_applies->shdr.sh_flags & SHF_ALLOC))
486766c5803SMatt Redfearn 			continue;
487766c5803SMatt Redfearn 
488766c5803SMatt Redfearn 		sh_symtab = sec_symtab->symtab;
489766c5803SMatt Redfearn 		sym_strtab = sec_symtab->link->strtab;
490766c5803SMatt Redfearn 		for (j = 0; j < sec->shdr.sh_size/sizeof(Elf_Rel); j++) {
491766c5803SMatt Redfearn 			Elf_Rel *rel = &sec->reltab[j];
492766c5803SMatt Redfearn 			Elf_Sym *sym = &sh_symtab[ELF_R_SYM(rel->r_info)];
493766c5803SMatt Redfearn 			const char *symname = sym_name(sym_strtab, sym);
494766c5803SMatt Redfearn 
495766c5803SMatt Redfearn 			process(sec, rel, sym, symname);
496766c5803SMatt Redfearn 		}
497766c5803SMatt Redfearn 	}
498766c5803SMatt Redfearn }
499766c5803SMatt Redfearn 
do_reloc(struct section * sec,Elf_Rel * rel,Elf_Sym * sym,const char * symname)500766c5803SMatt Redfearn static int do_reloc(struct section *sec, Elf_Rel *rel, Elf_Sym *sym,
501766c5803SMatt Redfearn 		      const char *symname)
502766c5803SMatt Redfearn {
503766c5803SMatt Redfearn 	unsigned r_type = ELF_R_TYPE(rel->r_info);
504766c5803SMatt Redfearn 	unsigned bind = ELF_ST_BIND(sym->st_info);
505766c5803SMatt Redfearn 
506766c5803SMatt Redfearn 	if ((bind == STB_WEAK) && (sym->st_value == 0)) {
507766c5803SMatt Redfearn 		/* Don't relocate weak symbols without a target */
508766c5803SMatt Redfearn 		return 0;
509766c5803SMatt Redfearn 	}
510766c5803SMatt Redfearn 
511766c5803SMatt Redfearn 	if (regex_skip_reloc(symname))
512766c5803SMatt Redfearn 		return 0;
513766c5803SMatt Redfearn 
514766c5803SMatt Redfearn 	switch (r_type) {
515766c5803SMatt Redfearn 	case R_MIPS_NONE:
516766c5803SMatt Redfearn 	case R_MIPS_REL32:
517766c5803SMatt Redfearn 	case R_MIPS_PC16:
518766c5803SMatt Redfearn 	case R_MIPS_PC21_S2:
519766c5803SMatt Redfearn 	case R_MIPS_PC26_S2:
520766c5803SMatt Redfearn 		/*
521766c5803SMatt Redfearn 		 * NONE can be ignored and PC relative relocations don't
522766c5803SMatt Redfearn 		 * need to be adjusted.
523766c5803SMatt Redfearn 		 */
524766c5803SMatt Redfearn 	case R_MIPS_HIGHEST:
525766c5803SMatt Redfearn 	case R_MIPS_HIGHER:
526766c5803SMatt Redfearn 		/* We support relocating within the same 4Gb segment only,
527766c5803SMatt Redfearn 		 * thus leaving the top 32bits unchanged
528766c5803SMatt Redfearn 		 */
529766c5803SMatt Redfearn 	case R_MIPS_LO16:
530766c5803SMatt Redfearn 		/* We support relocating by 64k jumps only
531766c5803SMatt Redfearn 		 * thus leaving the bottom 16bits unchanged
532766c5803SMatt Redfearn 		 */
533766c5803SMatt Redfearn 		break;
534766c5803SMatt Redfearn 
535766c5803SMatt Redfearn 	case R_MIPS_64:
536766c5803SMatt Redfearn 	case R_MIPS_32:
537766c5803SMatt Redfearn 	case R_MIPS_26:
538766c5803SMatt Redfearn 	case R_MIPS_HI16:
539766c5803SMatt Redfearn 		add_reloc(&relocs, rel->r_offset, r_type);
540766c5803SMatt Redfearn 		break;
541766c5803SMatt Redfearn 
542766c5803SMatt Redfearn 	default:
543766c5803SMatt Redfearn 		die("Unsupported relocation type: %s (%d)\n",
544766c5803SMatt Redfearn 		    rel_type(r_type), r_type);
545766c5803SMatt Redfearn 		break;
546766c5803SMatt Redfearn 	}
547766c5803SMatt Redfearn 
548766c5803SMatt Redfearn 	return 0;
549766c5803SMatt Redfearn }
550766c5803SMatt Redfearn 
write_reloc_as_bin(uint32_t v,FILE * f)551766c5803SMatt Redfearn static int write_reloc_as_bin(uint32_t v, FILE *f)
552766c5803SMatt Redfearn {
553766c5803SMatt Redfearn 	unsigned char buf[4];
554766c5803SMatt Redfearn 
555766c5803SMatt Redfearn 	v = cpu_to_elf32(v);
556766c5803SMatt Redfearn 
557766c5803SMatt Redfearn 	memcpy(buf, &v, sizeof(uint32_t));
558766c5803SMatt Redfearn 	return fwrite(buf, 1, 4, f);
559766c5803SMatt Redfearn }
560766c5803SMatt Redfearn 
write_reloc_as_text(uint32_t v,FILE * f)561766c5803SMatt Redfearn static int write_reloc_as_text(uint32_t v, FILE *f)
562766c5803SMatt Redfearn {
563766c5803SMatt Redfearn 	int res;
564766c5803SMatt Redfearn 
565766c5803SMatt Redfearn 	res = fprintf(f, "\t.long 0x%08"PRIx32"\n", v);
566766c5803SMatt Redfearn 	if (res < 0)
567766c5803SMatt Redfearn 		return res;
568766c5803SMatt Redfearn 	else
569766c5803SMatt Redfearn 		return sizeof(uint32_t);
570766c5803SMatt Redfearn }
571766c5803SMatt Redfearn 
emit_relocs(int as_text,int as_bin,FILE * outf)572766c5803SMatt Redfearn static void emit_relocs(int as_text, int as_bin, FILE *outf)
573766c5803SMatt Redfearn {
574766c5803SMatt Redfearn 	int i;
575766c5803SMatt Redfearn 	int (*write_reloc)(uint32_t, FILE *) = write_reloc_as_bin;
576766c5803SMatt Redfearn 	int size = 0;
577766c5803SMatt Redfearn 	int size_reserved;
578766c5803SMatt Redfearn 	struct section *sec_reloc;
579766c5803SMatt Redfearn 
580766c5803SMatt Redfearn 	sec_reloc = sec_lookup(".data.reloc");
581766c5803SMatt Redfearn 	if (!sec_reloc)
582766c5803SMatt Redfearn 		die("Could not find relocation section\n");
583766c5803SMatt Redfearn 
584766c5803SMatt Redfearn 	size_reserved = sec_reloc->shdr.sh_size;
585766c5803SMatt Redfearn 
586766c5803SMatt Redfearn 	/* Collect up the relocations */
587766c5803SMatt Redfearn 	walk_relocs(do_reloc);
588766c5803SMatt Redfearn 
589766c5803SMatt Redfearn 	/* Print the relocations */
590766c5803SMatt Redfearn 	if (as_text) {
591766c5803SMatt Redfearn 		/* Print the relocations in a form suitable that
592766c5803SMatt Redfearn 		 * gas will like.
593766c5803SMatt Redfearn 		 */
594766c5803SMatt Redfearn 		printf(".section \".data.reloc\",\"a\"\n");
595766c5803SMatt Redfearn 		printf(".balign 4\n");
596766c5803SMatt Redfearn 		/* Output text to stdout */
597766c5803SMatt Redfearn 		write_reloc = write_reloc_as_text;
598766c5803SMatt Redfearn 		outf = stdout;
599766c5803SMatt Redfearn 	} else if (as_bin) {
600766c5803SMatt Redfearn 		/* Output raw binary to stdout */
601766c5803SMatt Redfearn 		outf = stdout;
602766c5803SMatt Redfearn 	} else {
603766c5803SMatt Redfearn 		/* Seek to offset of the relocation section.
604766c5803SMatt Redfearn 		* Each relocation is then written into the
605766c5803SMatt Redfearn 		* vmlinux kernel image.
606766c5803SMatt Redfearn 		*/
607766c5803SMatt Redfearn 		if (fseek(outf, sec_reloc->shdr.sh_offset, SEEK_SET) < 0) {
608766c5803SMatt Redfearn 			die("Seek to %d failed: %s\n",
609766c5803SMatt Redfearn 				sec_reloc->shdr.sh_offset, strerror(errno));
610766c5803SMatt Redfearn 		}
611766c5803SMatt Redfearn 	}
612766c5803SMatt Redfearn 
613766c5803SMatt Redfearn 	for (i = 0; i < relocs.count; i++)
614766c5803SMatt Redfearn 		size += write_reloc(relocs.offset[i], outf);
615766c5803SMatt Redfearn 
616766c5803SMatt Redfearn 	/* Print a stop, but only if we've actually written some relocs */
617766c5803SMatt Redfearn 	if (size)
618766c5803SMatt Redfearn 		size += write_reloc(0, outf);
619766c5803SMatt Redfearn 
620766c5803SMatt Redfearn 	if (size > size_reserved)
621766c5803SMatt Redfearn 		/* Die, but suggest a value for CONFIG_RELOCATION_TABLE_SIZE
622766c5803SMatt Redfearn 		 * which will fix this problem and allow a bit of headroom
623766c5803SMatt Redfearn 		 * if more kernel features are enabled
624766c5803SMatt Redfearn 		 */
625766c5803SMatt Redfearn 		die("Relocations overflow available space!\n" \
626766c5803SMatt Redfearn 		    "Please adjust CONFIG_RELOCATION_TABLE_SIZE " \
627766c5803SMatt Redfearn 		    "to at least 0x%08x\n", (size + 0x1000) & ~0xFFF);
628766c5803SMatt Redfearn }
629766c5803SMatt Redfearn 
630766c5803SMatt Redfearn /*
631766c5803SMatt Redfearn  * As an aid to debugging problems with different linkers
632766c5803SMatt Redfearn  * print summary information about the relocs.
633766c5803SMatt Redfearn  * Since different linkers tend to emit the sections in
634766c5803SMatt Redfearn  * different orders we use the section names in the output.
635766c5803SMatt Redfearn  */
do_reloc_info(struct section * sec,Elf_Rel * rel,ElfW (Sym)* sym,const char * symname)636766c5803SMatt Redfearn static int do_reloc_info(struct section *sec, Elf_Rel *rel, ElfW(Sym) *sym,
637766c5803SMatt Redfearn 				const char *symname)
638766c5803SMatt Redfearn {
639766c5803SMatt Redfearn 	printf("%16s  0x%08x  %16s  %40s  %16s\n",
640766c5803SMatt Redfearn 		sec_name(sec->shdr.sh_info),
641766c5803SMatt Redfearn 		(unsigned int)rel->r_offset,
642766c5803SMatt Redfearn 		rel_type(ELF_R_TYPE(rel->r_info)),
643766c5803SMatt Redfearn 		symname,
644766c5803SMatt Redfearn 		sec_name(sym->st_shndx));
645766c5803SMatt Redfearn 	return 0;
646766c5803SMatt Redfearn }
647766c5803SMatt Redfearn 
print_reloc_info(void)648766c5803SMatt Redfearn static void print_reloc_info(void)
649766c5803SMatt Redfearn {
650766c5803SMatt Redfearn 	printf("%16s  %10s  %16s  %40s  %16s\n",
651766c5803SMatt Redfearn 		"reloc section",
652766c5803SMatt Redfearn 		"offset",
653766c5803SMatt Redfearn 		"reloc type",
654766c5803SMatt Redfearn 		"symbol",
655766c5803SMatt Redfearn 		"symbol section");
656766c5803SMatt Redfearn 	walk_relocs(do_reloc_info);
657766c5803SMatt Redfearn }
658766c5803SMatt Redfearn 
659766c5803SMatt Redfearn #if ELF_BITS == 64
660766c5803SMatt Redfearn # define process process_64
661766c5803SMatt Redfearn #else
662766c5803SMatt Redfearn # define process process_32
663766c5803SMatt Redfearn #endif
664766c5803SMatt Redfearn 
process(FILE * fp,int as_text,int as_bin,int show_reloc_info,int keep_relocs)665766c5803SMatt Redfearn void process(FILE *fp, int as_text, int as_bin,
666766c5803SMatt Redfearn 	     int show_reloc_info, int keep_relocs)
667766c5803SMatt Redfearn {
668766c5803SMatt Redfearn 	regex_init();
669766c5803SMatt Redfearn 	read_ehdr(fp);
670766c5803SMatt Redfearn 	read_shdrs(fp);
671766c5803SMatt Redfearn 	read_strtabs(fp);
672766c5803SMatt Redfearn 	read_symtabs(fp);
673766c5803SMatt Redfearn 	read_relocs(fp);
674766c5803SMatt Redfearn 	if (show_reloc_info) {
675766c5803SMatt Redfearn 		print_reloc_info();
676766c5803SMatt Redfearn 		return;
677766c5803SMatt Redfearn 	}
678766c5803SMatt Redfearn 	emit_relocs(as_text, as_bin, fp);
679766c5803SMatt Redfearn 	if (!keep_relocs)
680766c5803SMatt Redfearn 		remove_relocs(fp);
681766c5803SMatt Redfearn }
682