xref: /openbmc/linux/arch/x86/tools/relocs.c (revision a54c401ae66fc78f3f0002938b3465ebd6379009)
1 // SPDX-License-Identifier: GPL-2.0
2 /* This is included from relocs_32/64.c */
3 
4 #define ElfW(type)		_ElfW(ELF_BITS, type)
5 #define _ElfW(bits, type)	__ElfW(bits, type)
6 #define __ElfW(bits, type)	Elf##bits##_##type
7 
8 #define Elf_Addr		ElfW(Addr)
9 #define Elf_Ehdr		ElfW(Ehdr)
10 #define Elf_Phdr		ElfW(Phdr)
11 #define Elf_Shdr		ElfW(Shdr)
12 #define Elf_Sym			ElfW(Sym)
13 
14 static Elf_Ehdr		ehdr;
15 static unsigned long	shnum;
16 static unsigned int	shstrndx;
17 static unsigned int	shsymtabndx;
18 static unsigned int	shxsymtabndx;
19 
20 static int sym_index(Elf_Sym *sym);
21 
22 struct relocs {
23 	uint32_t	*offset;
24 	unsigned long	count;
25 	unsigned long	size;
26 };
27 
28 static struct relocs relocs16;
29 static struct relocs relocs32;
30 #if ELF_BITS == 64
31 static struct relocs relocs32neg;
32 static struct relocs relocs64;
33 #define FMT PRIu64
34 #else
35 #define FMT PRIu32
36 #endif
37 
38 struct section {
39 	Elf_Shdr       shdr;
40 	struct section *link;
41 	Elf_Sym        *symtab;
42 	Elf32_Word     *xsymtab;
43 	Elf_Rel        *reltab;
44 	char           *strtab;
45 };
46 static struct section *secs;
47 
48 static const char * const sym_regex_kernel[S_NSYMTYPES] = {
49 /*
50  * Following symbols have been audited. There values are constant and do
51  * not change if bzImage is loaded at a different physical address than
52  * the address for which it has been compiled. Don't warn user about
53  * absolute relocations present w.r.t these symbols.
54  */
55 	[S_ABS] =
56 	"^(xen_irq_disable_direct_reloc$|"
57 	"xen_save_fl_direct_reloc$|"
58 	"VDSO|"
59 	"__crc_)",
60 
61 /*
62  * These symbols are known to be relative, even if the linker marks them
63  * as absolute (typically defined outside any section in the linker script.)
64  */
65 	[S_REL] =
66 	"^(__init_(begin|end)|"
67 	"__x86_cpu_dev_(start|end)|"
68 	"(__parainstructions|__alt_instructions)(_end)?|"
69 	"(__iommu_table|__apicdrivers|__smp_locks)(_end)?|"
70 	"__(start|end)_pci_.*|"
71 	"__(start|end)_builtin_fw|"
72 	"__(start|stop)___ksymtab(_gpl)?|"
73 	"__(start|stop)___kcrctab(_gpl)?|"
74 	"__(start|stop)___param|"
75 	"__(start|stop)___modver|"
76 	"__(start|stop)___bug_table|"
77 	"__tracedata_(start|end)|"
78 	"__(start|stop)_notes|"
79 	"__end_rodata|"
80 	"__end_rodata_aligned|"
81 	"__initramfs_start|"
82 	"(jiffies|jiffies_64)|"
83 #if ELF_BITS == 64
84 	"__per_cpu_load|"
85 	"init_per_cpu__.*|"
86 	"__end_rodata_hpage_align|"
87 #endif
88 	"__vvar_page|"
89 	"_end)$"
90 };
91 
92 
93 static const char * const sym_regex_realmode[S_NSYMTYPES] = {
94 /*
95  * These symbols are known to be relative, even if the linker marks them
96  * as absolute (typically defined outside any section in the linker script.)
97  */
98 	[S_REL] =
99 	"^pa_",
100 
101 /*
102  * These are 16-bit segment symbols when compiling 16-bit code.
103  */
104 	[S_SEG] =
105 	"^real_mode_seg$",
106 
107 /*
108  * These are offsets belonging to segments, as opposed to linear addresses,
109  * when compiling 16-bit code.
110  */
111 	[S_LIN] =
112 	"^pa_",
113 };
114 
115 static const char * const *sym_regex;
116 
117 static regex_t sym_regex_c[S_NSYMTYPES];
118 static int is_reloc(enum symtype type, const char *sym_name)
119 {
120 	return sym_regex[type] &&
121 		!regexec(&sym_regex_c[type], sym_name, 0, NULL, 0);
122 }
123 
124 static void regex_init(int use_real_mode)
125 {
126         char errbuf[128];
127         int err;
128 	int i;
129 
130 	if (use_real_mode)
131 		sym_regex = sym_regex_realmode;
132 	else
133 		sym_regex = sym_regex_kernel;
134 
135 	for (i = 0; i < S_NSYMTYPES; i++) {
136 		if (!sym_regex[i])
137 			continue;
138 
139 		err = regcomp(&sym_regex_c[i], sym_regex[i],
140 			      REG_EXTENDED|REG_NOSUB);
141 
142 		if (err) {
143 			regerror(err, &sym_regex_c[i], errbuf, sizeof(errbuf));
144 			die("%s", errbuf);
145 		}
146         }
147 }
148 
149 static const char *sym_type(unsigned type)
150 {
151 	static const char *type_name[] = {
152 #define SYM_TYPE(X) [X] = #X
153 		SYM_TYPE(STT_NOTYPE),
154 		SYM_TYPE(STT_OBJECT),
155 		SYM_TYPE(STT_FUNC),
156 		SYM_TYPE(STT_SECTION),
157 		SYM_TYPE(STT_FILE),
158 		SYM_TYPE(STT_COMMON),
159 		SYM_TYPE(STT_TLS),
160 #undef SYM_TYPE
161 	};
162 	const char *name = "unknown sym type name";
163 	if (type < ARRAY_SIZE(type_name)) {
164 		name = type_name[type];
165 	}
166 	return name;
167 }
168 
169 static const char *sym_bind(unsigned bind)
170 {
171 	static const char *bind_name[] = {
172 #define SYM_BIND(X) [X] = #X
173 		SYM_BIND(STB_LOCAL),
174 		SYM_BIND(STB_GLOBAL),
175 		SYM_BIND(STB_WEAK),
176 #undef SYM_BIND
177 	};
178 	const char *name = "unknown sym bind name";
179 	if (bind < ARRAY_SIZE(bind_name)) {
180 		name = bind_name[bind];
181 	}
182 	return name;
183 }
184 
185 static const char *sym_visibility(unsigned visibility)
186 {
187 	static const char *visibility_name[] = {
188 #define SYM_VISIBILITY(X) [X] = #X
189 		SYM_VISIBILITY(STV_DEFAULT),
190 		SYM_VISIBILITY(STV_INTERNAL),
191 		SYM_VISIBILITY(STV_HIDDEN),
192 		SYM_VISIBILITY(STV_PROTECTED),
193 #undef SYM_VISIBILITY
194 	};
195 	const char *name = "unknown sym visibility name";
196 	if (visibility < ARRAY_SIZE(visibility_name)) {
197 		name = visibility_name[visibility];
198 	}
199 	return name;
200 }
201 
202 static const char *rel_type(unsigned type)
203 {
204 	static const char *type_name[] = {
205 #define REL_TYPE(X) [X] = #X
206 #if ELF_BITS == 64
207 		REL_TYPE(R_X86_64_NONE),
208 		REL_TYPE(R_X86_64_64),
209 		REL_TYPE(R_X86_64_PC64),
210 		REL_TYPE(R_X86_64_PC32),
211 		REL_TYPE(R_X86_64_GOT32),
212 		REL_TYPE(R_X86_64_PLT32),
213 		REL_TYPE(R_X86_64_COPY),
214 		REL_TYPE(R_X86_64_GLOB_DAT),
215 		REL_TYPE(R_X86_64_JUMP_SLOT),
216 		REL_TYPE(R_X86_64_RELATIVE),
217 		REL_TYPE(R_X86_64_GOTPCREL),
218 		REL_TYPE(R_X86_64_32),
219 		REL_TYPE(R_X86_64_32S),
220 		REL_TYPE(R_X86_64_16),
221 		REL_TYPE(R_X86_64_PC16),
222 		REL_TYPE(R_X86_64_8),
223 		REL_TYPE(R_X86_64_PC8),
224 #else
225 		REL_TYPE(R_386_NONE),
226 		REL_TYPE(R_386_32),
227 		REL_TYPE(R_386_PC32),
228 		REL_TYPE(R_386_GOT32),
229 		REL_TYPE(R_386_PLT32),
230 		REL_TYPE(R_386_COPY),
231 		REL_TYPE(R_386_GLOB_DAT),
232 		REL_TYPE(R_386_JMP_SLOT),
233 		REL_TYPE(R_386_RELATIVE),
234 		REL_TYPE(R_386_GOTOFF),
235 		REL_TYPE(R_386_GOTPC),
236 		REL_TYPE(R_386_8),
237 		REL_TYPE(R_386_PC8),
238 		REL_TYPE(R_386_16),
239 		REL_TYPE(R_386_PC16),
240 #endif
241 #undef REL_TYPE
242 	};
243 	const char *name = "unknown type rel type name";
244 	if (type < ARRAY_SIZE(type_name) && type_name[type]) {
245 		name = type_name[type];
246 	}
247 	return name;
248 }
249 
250 static const char *sec_name(unsigned shndx)
251 {
252 	const char *sec_strtab;
253 	const char *name;
254 	sec_strtab = secs[shstrndx].strtab;
255 	name = "<noname>";
256 	if (shndx < shnum) {
257 		name = sec_strtab + secs[shndx].shdr.sh_name;
258 	}
259 	else if (shndx == SHN_ABS) {
260 		name = "ABSOLUTE";
261 	}
262 	else if (shndx == SHN_COMMON) {
263 		name = "COMMON";
264 	}
265 	return name;
266 }
267 
268 static const char *sym_name(const char *sym_strtab, Elf_Sym *sym)
269 {
270 	const char *name;
271 	name = "<noname>";
272 	if (sym->st_name) {
273 		name = sym_strtab + sym->st_name;
274 	}
275 	else {
276 		name = sec_name(sym_index(sym));
277 	}
278 	return name;
279 }
280 
281 static Elf_Sym *sym_lookup(const char *symname)
282 {
283 	int i;
284 	for (i = 0; i < shnum; i++) {
285 		struct section *sec = &secs[i];
286 		long nsyms;
287 		char *strtab;
288 		Elf_Sym *symtab;
289 		Elf_Sym *sym;
290 
291 		if (sec->shdr.sh_type != SHT_SYMTAB)
292 			continue;
293 
294 		nsyms = sec->shdr.sh_size/sizeof(Elf_Sym);
295 		symtab = sec->symtab;
296 		strtab = sec->link->strtab;
297 
298 		for (sym = symtab; --nsyms >= 0; sym++) {
299 			if (!sym->st_name)
300 				continue;
301 			if (strcmp(symname, strtab + sym->st_name) == 0)
302 				return sym;
303 		}
304 	}
305 	return 0;
306 }
307 
308 #if BYTE_ORDER == LITTLE_ENDIAN
309 #define le16_to_cpu(val) (val)
310 #define le32_to_cpu(val) (val)
311 #define le64_to_cpu(val) (val)
312 #endif
313 #if BYTE_ORDER == BIG_ENDIAN
314 #define le16_to_cpu(val) bswap_16(val)
315 #define le32_to_cpu(val) bswap_32(val)
316 #define le64_to_cpu(val) bswap_64(val)
317 #endif
318 
319 static uint16_t elf16_to_cpu(uint16_t val)
320 {
321 	return le16_to_cpu(val);
322 }
323 
324 static uint32_t elf32_to_cpu(uint32_t val)
325 {
326 	return le32_to_cpu(val);
327 }
328 
329 #define elf_half_to_cpu(x)	elf16_to_cpu(x)
330 #define elf_word_to_cpu(x)	elf32_to_cpu(x)
331 
332 #if ELF_BITS == 64
333 static uint64_t elf64_to_cpu(uint64_t val)
334 {
335         return le64_to_cpu(val);
336 }
337 #define elf_addr_to_cpu(x)	elf64_to_cpu(x)
338 #define elf_off_to_cpu(x)	elf64_to_cpu(x)
339 #define elf_xword_to_cpu(x)	elf64_to_cpu(x)
340 #else
341 #define elf_addr_to_cpu(x)	elf32_to_cpu(x)
342 #define elf_off_to_cpu(x)	elf32_to_cpu(x)
343 #define elf_xword_to_cpu(x)	elf32_to_cpu(x)
344 #endif
345 
346 static int sym_index(Elf_Sym *sym)
347 {
348 	Elf_Sym *symtab = secs[shsymtabndx].symtab;
349 	Elf32_Word *xsymtab = secs[shxsymtabndx].xsymtab;
350 	unsigned long offset;
351 	int index;
352 
353 	if (sym->st_shndx != SHN_XINDEX)
354 		return sym->st_shndx;
355 
356 	/* calculate offset of sym from head of table. */
357 	offset = (unsigned long)sym - (unsigned long)symtab;
358 	index = offset / sizeof(*sym);
359 
360 	return elf32_to_cpu(xsymtab[index]);
361 }
362 
363 static void read_ehdr(FILE *fp)
364 {
365 	if (fread(&ehdr, sizeof(ehdr), 1, fp) != 1) {
366 		die("Cannot read ELF header: %s\n",
367 			strerror(errno));
368 	}
369 	if (memcmp(ehdr.e_ident, ELFMAG, SELFMAG) != 0) {
370 		die("No ELF magic\n");
371 	}
372 	if (ehdr.e_ident[EI_CLASS] != ELF_CLASS) {
373 		die("Not a %d bit executable\n", ELF_BITS);
374 	}
375 	if (ehdr.e_ident[EI_DATA] != ELFDATA2LSB) {
376 		die("Not a LSB ELF executable\n");
377 	}
378 	if (ehdr.e_ident[EI_VERSION] != EV_CURRENT) {
379 		die("Unknown ELF version\n");
380 	}
381 	/* Convert the fields to native endian */
382 	ehdr.e_type      = elf_half_to_cpu(ehdr.e_type);
383 	ehdr.e_machine   = elf_half_to_cpu(ehdr.e_machine);
384 	ehdr.e_version   = elf_word_to_cpu(ehdr.e_version);
385 	ehdr.e_entry     = elf_addr_to_cpu(ehdr.e_entry);
386 	ehdr.e_phoff     = elf_off_to_cpu(ehdr.e_phoff);
387 	ehdr.e_shoff     = elf_off_to_cpu(ehdr.e_shoff);
388 	ehdr.e_flags     = elf_word_to_cpu(ehdr.e_flags);
389 	ehdr.e_ehsize    = elf_half_to_cpu(ehdr.e_ehsize);
390 	ehdr.e_phentsize = elf_half_to_cpu(ehdr.e_phentsize);
391 	ehdr.e_phnum     = elf_half_to_cpu(ehdr.e_phnum);
392 	ehdr.e_shentsize = elf_half_to_cpu(ehdr.e_shentsize);
393 	ehdr.e_shnum     = elf_half_to_cpu(ehdr.e_shnum);
394 	ehdr.e_shstrndx  = elf_half_to_cpu(ehdr.e_shstrndx);
395 
396 	shnum = ehdr.e_shnum;
397 	shstrndx = ehdr.e_shstrndx;
398 
399 	if ((ehdr.e_type != ET_EXEC) && (ehdr.e_type != ET_DYN))
400 		die("Unsupported ELF header type\n");
401 	if (ehdr.e_machine != ELF_MACHINE)
402 		die("Not for %s\n", ELF_MACHINE_NAME);
403 	if (ehdr.e_version != EV_CURRENT)
404 		die("Unknown ELF version\n");
405 	if (ehdr.e_ehsize != sizeof(Elf_Ehdr))
406 		die("Bad Elf header size\n");
407 	if (ehdr.e_phentsize != sizeof(Elf_Phdr))
408 		die("Bad program header entry\n");
409 	if (ehdr.e_shentsize != sizeof(Elf_Shdr))
410 		die("Bad section header entry\n");
411 
412 
413 	if (shnum == SHN_UNDEF || shstrndx == SHN_XINDEX) {
414 		Elf_Shdr shdr;
415 
416 		if (fseek(fp, ehdr.e_shoff, SEEK_SET) < 0)
417 			die("Seek to %" FMT " failed: %s\n", ehdr.e_shoff, strerror(errno));
418 
419 		if (fread(&shdr, sizeof(shdr), 1, fp) != 1)
420 			die("Cannot read initial ELF section header: %s\n", strerror(errno));
421 
422 		if (shnum == SHN_UNDEF)
423 			shnum = elf_xword_to_cpu(shdr.sh_size);
424 
425 		if (shstrndx == SHN_XINDEX)
426 			shstrndx = elf_word_to_cpu(shdr.sh_link);
427 	}
428 
429 	if (shstrndx >= shnum)
430 		die("String table index out of bounds\n");
431 }
432 
433 static void read_shdrs(FILE *fp)
434 {
435 	int i;
436 	Elf_Shdr shdr;
437 
438 	secs = calloc(shnum, sizeof(struct section));
439 	if (!secs) {
440 		die("Unable to allocate %ld section headers\n",
441 		    shnum);
442 	}
443 	if (fseek(fp, ehdr.e_shoff, SEEK_SET) < 0) {
444 		die("Seek to %" FMT " failed: %s\n",
445 		    ehdr.e_shoff, strerror(errno));
446 	}
447 	for (i = 0; i < shnum; i++) {
448 		struct section *sec = &secs[i];
449 		if (fread(&shdr, sizeof(shdr), 1, fp) != 1)
450 			die("Cannot read ELF section headers %d/%ld: %s\n",
451 			    i, shnum, strerror(errno));
452 		sec->shdr.sh_name      = elf_word_to_cpu(shdr.sh_name);
453 		sec->shdr.sh_type      = elf_word_to_cpu(shdr.sh_type);
454 		sec->shdr.sh_flags     = elf_xword_to_cpu(shdr.sh_flags);
455 		sec->shdr.sh_addr      = elf_addr_to_cpu(shdr.sh_addr);
456 		sec->shdr.sh_offset    = elf_off_to_cpu(shdr.sh_offset);
457 		sec->shdr.sh_size      = elf_xword_to_cpu(shdr.sh_size);
458 		sec->shdr.sh_link      = elf_word_to_cpu(shdr.sh_link);
459 		sec->shdr.sh_info      = elf_word_to_cpu(shdr.sh_info);
460 		sec->shdr.sh_addralign = elf_xword_to_cpu(shdr.sh_addralign);
461 		sec->shdr.sh_entsize   = elf_xword_to_cpu(shdr.sh_entsize);
462 		if (sec->shdr.sh_link < shnum)
463 			sec->link = &secs[sec->shdr.sh_link];
464 	}
465 
466 }
467 
468 static void read_strtabs(FILE *fp)
469 {
470 	int i;
471 	for (i = 0; i < shnum; i++) {
472 		struct section *sec = &secs[i];
473 		if (sec->shdr.sh_type != SHT_STRTAB) {
474 			continue;
475 		}
476 		sec->strtab = malloc(sec->shdr.sh_size);
477 		if (!sec->strtab) {
478 			die("malloc of %" FMT " bytes for strtab failed\n",
479 			    sec->shdr.sh_size);
480 		}
481 		if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0) {
482 			die("Seek to %" FMT " failed: %s\n",
483 			    sec->shdr.sh_offset, strerror(errno));
484 		}
485 		if (fread(sec->strtab, 1, sec->shdr.sh_size, fp)
486 		    != sec->shdr.sh_size) {
487 			die("Cannot read symbol table: %s\n",
488 				strerror(errno));
489 		}
490 	}
491 }
492 
493 static void read_symtabs(FILE *fp)
494 {
495 	int i,j;
496 
497 	for (i = 0; i < shnum; i++) {
498 		struct section *sec = &secs[i];
499 		int num_syms;
500 
501 		switch (sec->shdr.sh_type) {
502 		case SHT_SYMTAB_SHNDX:
503 			sec->xsymtab = malloc(sec->shdr.sh_size);
504 			if (!sec->xsymtab) {
505 				die("malloc of %" FMT " bytes for xsymtab failed\n",
506 				    sec->shdr.sh_size);
507 			}
508 			if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0) {
509 				die("Seek to %" FMT " failed: %s\n",
510 				    sec->shdr.sh_offset, strerror(errno));
511 			}
512 			if (fread(sec->xsymtab, 1, sec->shdr.sh_size, fp)
513 			    != sec->shdr.sh_size) {
514 				die("Cannot read extended symbol table: %s\n",
515 				    strerror(errno));
516 			}
517 			shxsymtabndx = i;
518 			continue;
519 
520 		case SHT_SYMTAB:
521 			num_syms = sec->shdr.sh_size / sizeof(Elf_Sym);
522 
523 			sec->symtab = malloc(sec->shdr.sh_size);
524 			if (!sec->symtab) {
525 				die("malloc of %" FMT " bytes for symtab failed\n",
526 				    sec->shdr.sh_size);
527 			}
528 			if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0) {
529 				die("Seek to %" FMT " failed: %s\n",
530 				    sec->shdr.sh_offset, strerror(errno));
531 			}
532 			if (fread(sec->symtab, 1, sec->shdr.sh_size, fp)
533 			    != sec->shdr.sh_size) {
534 				die("Cannot read symbol table: %s\n",
535 				    strerror(errno));
536 			}
537 			for (j = 0; j < num_syms; j++) {
538 				Elf_Sym *sym = &sec->symtab[j];
539 
540 				sym->st_name  = elf_word_to_cpu(sym->st_name);
541 				sym->st_value = elf_addr_to_cpu(sym->st_value);
542 				sym->st_size  = elf_xword_to_cpu(sym->st_size);
543 				sym->st_shndx = elf_half_to_cpu(sym->st_shndx);
544 			}
545 			shsymtabndx = i;
546 			continue;
547 
548 		default:
549 			continue;
550 		}
551 	}
552 }
553 
554 
555 static void read_relocs(FILE *fp)
556 {
557 	int i,j;
558 	for (i = 0; i < shnum; i++) {
559 		struct section *sec = &secs[i];
560 		if (sec->shdr.sh_type != SHT_REL_TYPE) {
561 			continue;
562 		}
563 		sec->reltab = malloc(sec->shdr.sh_size);
564 		if (!sec->reltab) {
565 			die("malloc of %" FMT " bytes for relocs failed\n",
566 			    sec->shdr.sh_size);
567 		}
568 		if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0) {
569 			die("Seek to %" FMT " failed: %s\n",
570 			    sec->shdr.sh_offset, strerror(errno));
571 		}
572 		if (fread(sec->reltab, 1, sec->shdr.sh_size, fp)
573 		    != sec->shdr.sh_size) {
574 			die("Cannot read symbol table: %s\n",
575 				strerror(errno));
576 		}
577 		for (j = 0; j < sec->shdr.sh_size/sizeof(Elf_Rel); j++) {
578 			Elf_Rel *rel = &sec->reltab[j];
579 			rel->r_offset = elf_addr_to_cpu(rel->r_offset);
580 			rel->r_info   = elf_xword_to_cpu(rel->r_info);
581 #if (SHT_REL_TYPE == SHT_RELA)
582 			rel->r_addend = elf_xword_to_cpu(rel->r_addend);
583 #endif
584 		}
585 	}
586 }
587 
588 
589 static void print_absolute_symbols(void)
590 {
591 	int i;
592 	const char *format;
593 
594 	if (ELF_BITS == 64)
595 		format = "%5d %016"PRIx64" %5"PRId64" %10s %10s %12s %s\n";
596 	else
597 		format = "%5d %08"PRIx32"  %5"PRId32" %10s %10s %12s %s\n";
598 
599 	printf("Absolute symbols\n");
600 	printf(" Num:    Value Size  Type       Bind        Visibility  Name\n");
601 	for (i = 0; i < shnum; i++) {
602 		struct section *sec = &secs[i];
603 		char *sym_strtab;
604 		int j;
605 
606 		if (sec->shdr.sh_type != SHT_SYMTAB) {
607 			continue;
608 		}
609 		sym_strtab = sec->link->strtab;
610 		for (j = 0; j < sec->shdr.sh_size/sizeof(Elf_Sym); j++) {
611 			Elf_Sym *sym;
612 			const char *name;
613 			sym = &sec->symtab[j];
614 			name = sym_name(sym_strtab, sym);
615 			if (sym->st_shndx != SHN_ABS) {
616 				continue;
617 			}
618 			printf(format,
619 				j, sym->st_value, sym->st_size,
620 				sym_type(ELF_ST_TYPE(sym->st_info)),
621 				sym_bind(ELF_ST_BIND(sym->st_info)),
622 				sym_visibility(ELF_ST_VISIBILITY(sym->st_other)),
623 				name);
624 		}
625 	}
626 	printf("\n");
627 }
628 
629 static void print_absolute_relocs(void)
630 {
631 	int i, printed = 0;
632 	const char *format;
633 
634 	if (ELF_BITS == 64)
635 		format = "%016"PRIx64" %016"PRIx64" %10s %016"PRIx64"  %s\n";
636 	else
637 		format = "%08"PRIx32" %08"PRIx32" %10s %08"PRIx32"  %s\n";
638 
639 	for (i = 0; i < shnum; i++) {
640 		struct section *sec = &secs[i];
641 		struct section *sec_applies, *sec_symtab;
642 		char *sym_strtab;
643 		Elf_Sym *sh_symtab;
644 		int j;
645 		if (sec->shdr.sh_type != SHT_REL_TYPE) {
646 			continue;
647 		}
648 		sec_symtab  = sec->link;
649 		sec_applies = &secs[sec->shdr.sh_info];
650 		if (!(sec_applies->shdr.sh_flags & SHF_ALLOC)) {
651 			continue;
652 		}
653 		sh_symtab  = sec_symtab->symtab;
654 		sym_strtab = sec_symtab->link->strtab;
655 		for (j = 0; j < sec->shdr.sh_size/sizeof(Elf_Rel); j++) {
656 			Elf_Rel *rel;
657 			Elf_Sym *sym;
658 			const char *name;
659 			rel = &sec->reltab[j];
660 			sym = &sh_symtab[ELF_R_SYM(rel->r_info)];
661 			name = sym_name(sym_strtab, sym);
662 			if (sym->st_shndx != SHN_ABS) {
663 				continue;
664 			}
665 
666 			/* Absolute symbols are not relocated if bzImage is
667 			 * loaded at a non-compiled address. Display a warning
668 			 * to user at compile time about the absolute
669 			 * relocations present.
670 			 *
671 			 * User need to audit the code to make sure
672 			 * some symbols which should have been section
673 			 * relative have not become absolute because of some
674 			 * linker optimization or wrong programming usage.
675 			 *
676 			 * Before warning check if this absolute symbol
677 			 * relocation is harmless.
678 			 */
679 			if (is_reloc(S_ABS, name) || is_reloc(S_REL, name))
680 				continue;
681 
682 			if (!printed) {
683 				printf("WARNING: Absolute relocations"
684 					" present\n");
685 				printf("Offset     Info     Type     Sym.Value "
686 					"Sym.Name\n");
687 				printed = 1;
688 			}
689 
690 			printf(format,
691 				rel->r_offset,
692 				rel->r_info,
693 				rel_type(ELF_R_TYPE(rel->r_info)),
694 				sym->st_value,
695 				name);
696 		}
697 	}
698 
699 	if (printed)
700 		printf("\n");
701 }
702 
703 static void add_reloc(struct relocs *r, uint32_t offset)
704 {
705 	if (r->count == r->size) {
706 		unsigned long newsize = r->size + 50000;
707 		void *mem = realloc(r->offset, newsize * sizeof(r->offset[0]));
708 
709 		if (!mem)
710 			die("realloc of %ld entries for relocs failed\n",
711                                 newsize);
712 		r->offset = mem;
713 		r->size = newsize;
714 	}
715 	r->offset[r->count++] = offset;
716 }
717 
718 static void walk_relocs(int (*process)(struct section *sec, Elf_Rel *rel,
719 			Elf_Sym *sym, const char *symname))
720 {
721 	int i;
722 	/* Walk through the relocations */
723 	for (i = 0; i < shnum; i++) {
724 		char *sym_strtab;
725 		Elf_Sym *sh_symtab;
726 		struct section *sec_applies, *sec_symtab;
727 		int j;
728 		struct section *sec = &secs[i];
729 
730 		if (sec->shdr.sh_type != SHT_REL_TYPE) {
731 			continue;
732 		}
733 		sec_symtab  = sec->link;
734 		sec_applies = &secs[sec->shdr.sh_info];
735 		if (!(sec_applies->shdr.sh_flags & SHF_ALLOC)) {
736 			continue;
737 		}
738 		sh_symtab = sec_symtab->symtab;
739 		sym_strtab = sec_symtab->link->strtab;
740 		for (j = 0; j < sec->shdr.sh_size/sizeof(Elf_Rel); j++) {
741 			Elf_Rel *rel = &sec->reltab[j];
742 			Elf_Sym *sym = &sh_symtab[ELF_R_SYM(rel->r_info)];
743 			const char *symname = sym_name(sym_strtab, sym);
744 
745 			process(sec, rel, sym, symname);
746 		}
747 	}
748 }
749 
750 /*
751  * The .data..percpu section is a special case for x86_64 SMP kernels.
752  * It is used to initialize the actual per_cpu areas and to provide
753  * definitions for the per_cpu variables that correspond to their offsets
754  * within the percpu area. Since the values of all of the symbols need
755  * to be offsets from the start of the per_cpu area the virtual address
756  * (sh_addr) of .data..percpu is 0 in SMP kernels.
757  *
758  * This means that:
759  *
760  *	Relocations that reference symbols in the per_cpu area do not
761  *	need further relocation (since the value is an offset relative
762  *	to the start of the per_cpu area that does not change).
763  *
764  *	Relocations that apply to the per_cpu area need to have their
765  *	offset adjusted by by the value of __per_cpu_load to make them
766  *	point to the correct place in the loaded image (because the
767  *	virtual address of .data..percpu is 0).
768  *
769  * For non SMP kernels .data..percpu is linked as part of the normal
770  * kernel data and does not require special treatment.
771  *
772  */
773 static int per_cpu_shndx	= -1;
774 static Elf_Addr per_cpu_load_addr;
775 
776 static void percpu_init(void)
777 {
778 	int i;
779 	for (i = 0; i < shnum; i++) {
780 		ElfW(Sym) *sym;
781 		if (strcmp(sec_name(i), ".data..percpu"))
782 			continue;
783 
784 		if (secs[i].shdr.sh_addr != 0)	/* non SMP kernel */
785 			return;
786 
787 		sym = sym_lookup("__per_cpu_load");
788 		if (!sym)
789 			die("can't find __per_cpu_load\n");
790 
791 		per_cpu_shndx = i;
792 		per_cpu_load_addr = sym->st_value;
793 		return;
794 	}
795 }
796 
797 #if ELF_BITS == 64
798 
799 /*
800  * Check to see if a symbol lies in the .data..percpu section.
801  *
802  * The linker incorrectly associates some symbols with the
803  * .data..percpu section so we also need to check the symbol
804  * name to make sure that we classify the symbol correctly.
805  *
806  * The GNU linker incorrectly associates:
807  *	__init_begin
808  *	__per_cpu_load
809  *
810  * The "gold" linker incorrectly associates:
811  *	init_per_cpu__fixed_percpu_data
812  *	init_per_cpu__gdt_page
813  */
814 static int is_percpu_sym(ElfW(Sym) *sym, const char *symname)
815 {
816 	int shndx = sym_index(sym);
817 
818 	return (shndx == per_cpu_shndx) &&
819 		strcmp(symname, "__init_begin") &&
820 		strcmp(symname, "__per_cpu_load") &&
821 		strncmp(symname, "init_per_cpu_", 13);
822 }
823 
824 
825 static int do_reloc64(struct section *sec, Elf_Rel *rel, ElfW(Sym) *sym,
826 		      const char *symname)
827 {
828 	unsigned r_type = ELF64_R_TYPE(rel->r_info);
829 	ElfW(Addr) offset = rel->r_offset;
830 	int shn_abs = (sym->st_shndx == SHN_ABS) && !is_reloc(S_REL, symname);
831 
832 	if (sym->st_shndx == SHN_UNDEF)
833 		return 0;
834 
835 	/*
836 	 * Adjust the offset if this reloc applies to the percpu section.
837 	 */
838 	if (sec->shdr.sh_info == per_cpu_shndx)
839 		offset += per_cpu_load_addr;
840 
841 	switch (r_type) {
842 	case R_X86_64_NONE:
843 		/* NONE can be ignored. */
844 		break;
845 
846 	case R_X86_64_PC32:
847 	case R_X86_64_PLT32:
848 		/*
849 		 * PC relative relocations don't need to be adjusted unless
850 		 * referencing a percpu symbol.
851 		 *
852 		 * NB: R_X86_64_PLT32 can be treated as R_X86_64_PC32.
853 		 */
854 		if (is_percpu_sym(sym, symname))
855 			add_reloc(&relocs32neg, offset);
856 		break;
857 
858 	case R_X86_64_PC64:
859 		/*
860 		 * Only used by jump labels
861 		 */
862 		if (is_percpu_sym(sym, symname))
863 			die("Invalid R_X86_64_PC64 relocation against per-CPU symbol %s\n",
864 			    symname);
865 		break;
866 
867 	case R_X86_64_32:
868 	case R_X86_64_32S:
869 	case R_X86_64_64:
870 		/*
871 		 * References to the percpu area don't need to be adjusted.
872 		 */
873 		if (is_percpu_sym(sym, symname))
874 			break;
875 
876 		if (shn_abs) {
877 			/*
878 			 * Whitelisted absolute symbols do not require
879 			 * relocation.
880 			 */
881 			if (is_reloc(S_ABS, symname))
882 				break;
883 
884 			die("Invalid absolute %s relocation: %s\n",
885 			    rel_type(r_type), symname);
886 			break;
887 		}
888 
889 		/*
890 		 * Relocation offsets for 64 bit kernels are output
891 		 * as 32 bits and sign extended back to 64 bits when
892 		 * the relocations are processed.
893 		 * Make sure that the offset will fit.
894 		 */
895 		if ((int32_t)offset != (int64_t)offset)
896 			die("Relocation offset doesn't fit in 32 bits\n");
897 
898 		if (r_type == R_X86_64_64)
899 			add_reloc(&relocs64, offset);
900 		else
901 			add_reloc(&relocs32, offset);
902 		break;
903 
904 	default:
905 		die("Unsupported relocation type: %s (%d)\n",
906 		    rel_type(r_type), r_type);
907 		break;
908 	}
909 
910 	return 0;
911 }
912 
913 #else
914 
915 static int do_reloc32(struct section *sec, Elf_Rel *rel, Elf_Sym *sym,
916 		      const char *symname)
917 {
918 	unsigned r_type = ELF32_R_TYPE(rel->r_info);
919 	int shn_abs = (sym->st_shndx == SHN_ABS) && !is_reloc(S_REL, symname);
920 
921 	switch (r_type) {
922 	case R_386_NONE:
923 	case R_386_PC32:
924 	case R_386_PC16:
925 	case R_386_PC8:
926 	case R_386_PLT32:
927 		/*
928 		 * NONE can be ignored and PC relative relocations don't need
929 		 * to be adjusted. Because sym must be defined, R_386_PLT32 can
930 		 * be treated the same way as R_386_PC32.
931 		 */
932 		break;
933 
934 	case R_386_32:
935 		if (shn_abs) {
936 			/*
937 			 * Whitelisted absolute symbols do not require
938 			 * relocation.
939 			 */
940 			if (is_reloc(S_ABS, symname))
941 				break;
942 
943 			die("Invalid absolute %s relocation: %s\n",
944 			    rel_type(r_type), symname);
945 			break;
946 		}
947 
948 		add_reloc(&relocs32, rel->r_offset);
949 		break;
950 
951 	default:
952 		die("Unsupported relocation type: %s (%d)\n",
953 		    rel_type(r_type), r_type);
954 		break;
955 	}
956 
957 	return 0;
958 }
959 
960 static int do_reloc_real(struct section *sec, Elf_Rel *rel, Elf_Sym *sym,
961 			 const char *symname)
962 {
963 	unsigned r_type = ELF32_R_TYPE(rel->r_info);
964 	int shn_abs = (sym->st_shndx == SHN_ABS) && !is_reloc(S_REL, symname);
965 
966 	switch (r_type) {
967 	case R_386_NONE:
968 	case R_386_PC32:
969 	case R_386_PC16:
970 	case R_386_PC8:
971 	case R_386_PLT32:
972 		/*
973 		 * NONE can be ignored and PC relative relocations don't need
974 		 * to be adjusted. Because sym must be defined, R_386_PLT32 can
975 		 * be treated the same way as R_386_PC32.
976 		 */
977 		break;
978 
979 	case R_386_16:
980 		if (shn_abs) {
981 			/*
982 			 * Whitelisted absolute symbols do not require
983 			 * relocation.
984 			 */
985 			if (is_reloc(S_ABS, symname))
986 				break;
987 
988 			if (is_reloc(S_SEG, symname)) {
989 				add_reloc(&relocs16, rel->r_offset);
990 				break;
991 			}
992 		} else {
993 			if (!is_reloc(S_LIN, symname))
994 				break;
995 		}
996 		die("Invalid %s %s relocation: %s\n",
997 		    shn_abs ? "absolute" : "relative",
998 		    rel_type(r_type), symname);
999 		break;
1000 
1001 	case R_386_32:
1002 		if (shn_abs) {
1003 			/*
1004 			 * Whitelisted absolute symbols do not require
1005 			 * relocation.
1006 			 */
1007 			if (is_reloc(S_ABS, symname))
1008 				break;
1009 
1010 			if (is_reloc(S_REL, symname)) {
1011 				add_reloc(&relocs32, rel->r_offset);
1012 				break;
1013 			}
1014 		} else {
1015 			if (is_reloc(S_LIN, symname))
1016 				add_reloc(&relocs32, rel->r_offset);
1017 			break;
1018 		}
1019 		die("Invalid %s %s relocation: %s\n",
1020 		    shn_abs ? "absolute" : "relative",
1021 		    rel_type(r_type), symname);
1022 		break;
1023 
1024 	default:
1025 		die("Unsupported relocation type: %s (%d)\n",
1026 		    rel_type(r_type), r_type);
1027 		break;
1028 	}
1029 
1030 	return 0;
1031 }
1032 
1033 #endif
1034 
1035 static int cmp_relocs(const void *va, const void *vb)
1036 {
1037 	const uint32_t *a, *b;
1038 	a = va; b = vb;
1039 	return (*a == *b)? 0 : (*a > *b)? 1 : -1;
1040 }
1041 
1042 static void sort_relocs(struct relocs *r)
1043 {
1044 	qsort(r->offset, r->count, sizeof(r->offset[0]), cmp_relocs);
1045 }
1046 
1047 static int write32(uint32_t v, FILE *f)
1048 {
1049 	unsigned char buf[4];
1050 
1051 	put_unaligned_le32(v, buf);
1052 	return fwrite(buf, 1, 4, f) == 4 ? 0 : -1;
1053 }
1054 
1055 static int write32_as_text(uint32_t v, FILE *f)
1056 {
1057 	return fprintf(f, "\t.long 0x%08"PRIx32"\n", v) > 0 ? 0 : -1;
1058 }
1059 
1060 static void emit_relocs(int as_text, int use_real_mode)
1061 {
1062 	int i;
1063 	int (*write_reloc)(uint32_t, FILE *) = write32;
1064 	int (*do_reloc)(struct section *sec, Elf_Rel *rel, Elf_Sym *sym,
1065 			const char *symname);
1066 
1067 #if ELF_BITS == 64
1068 	if (!use_real_mode)
1069 		do_reloc = do_reloc64;
1070 	else
1071 		die("--realmode not valid for a 64-bit ELF file");
1072 #else
1073 	if (!use_real_mode)
1074 		do_reloc = do_reloc32;
1075 	else
1076 		do_reloc = do_reloc_real;
1077 #endif
1078 
1079 	/* Collect up the relocations */
1080 	walk_relocs(do_reloc);
1081 
1082 	if (relocs16.count && !use_real_mode)
1083 		die("Segment relocations found but --realmode not specified\n");
1084 
1085 	/* Order the relocations for more efficient processing */
1086 	sort_relocs(&relocs32);
1087 #if ELF_BITS == 64
1088 	sort_relocs(&relocs32neg);
1089 	sort_relocs(&relocs64);
1090 #else
1091 	sort_relocs(&relocs16);
1092 #endif
1093 
1094 	/* Print the relocations */
1095 	if (as_text) {
1096 		/* Print the relocations in a form suitable that
1097 		 * gas will like.
1098 		 */
1099 		printf(".section \".data.reloc\",\"a\"\n");
1100 		printf(".balign 4\n");
1101 		write_reloc = write32_as_text;
1102 	}
1103 
1104 	if (use_real_mode) {
1105 		write_reloc(relocs16.count, stdout);
1106 		for (i = 0; i < relocs16.count; i++)
1107 			write_reloc(relocs16.offset[i], stdout);
1108 
1109 		write_reloc(relocs32.count, stdout);
1110 		for (i = 0; i < relocs32.count; i++)
1111 			write_reloc(relocs32.offset[i], stdout);
1112 	} else {
1113 #if ELF_BITS == 64
1114 		/* Print a stop */
1115 		write_reloc(0, stdout);
1116 
1117 		/* Now print each relocation */
1118 		for (i = 0; i < relocs64.count; i++)
1119 			write_reloc(relocs64.offset[i], stdout);
1120 
1121 		/* Print a stop */
1122 		write_reloc(0, stdout);
1123 
1124 		/* Now print each inverse 32-bit relocation */
1125 		for (i = 0; i < relocs32neg.count; i++)
1126 			write_reloc(relocs32neg.offset[i], stdout);
1127 #endif
1128 
1129 		/* Print a stop */
1130 		write_reloc(0, stdout);
1131 
1132 		/* Now print each relocation */
1133 		for (i = 0; i < relocs32.count; i++)
1134 			write_reloc(relocs32.offset[i], stdout);
1135 	}
1136 }
1137 
1138 /*
1139  * As an aid to debugging problems with different linkers
1140  * print summary information about the relocs.
1141  * Since different linkers tend to emit the sections in
1142  * different orders we use the section names in the output.
1143  */
1144 static int do_reloc_info(struct section *sec, Elf_Rel *rel, ElfW(Sym) *sym,
1145 				const char *symname)
1146 {
1147 	printf("%s\t%s\t%s\t%s\n",
1148 		sec_name(sec->shdr.sh_info),
1149 		rel_type(ELF_R_TYPE(rel->r_info)),
1150 		symname,
1151 		sec_name(sym_index(sym)));
1152 	return 0;
1153 }
1154 
1155 static void print_reloc_info(void)
1156 {
1157 	printf("reloc section\treloc type\tsymbol\tsymbol section\n");
1158 	walk_relocs(do_reloc_info);
1159 }
1160 
1161 #if ELF_BITS == 64
1162 # define process process_64
1163 #else
1164 # define process process_32
1165 #endif
1166 
1167 void process(FILE *fp, int use_real_mode, int as_text,
1168 	     int show_absolute_syms, int show_absolute_relocs,
1169 	     int show_reloc_info)
1170 {
1171 	regex_init(use_real_mode);
1172 	read_ehdr(fp);
1173 	read_shdrs(fp);
1174 	read_strtabs(fp);
1175 	read_symtabs(fp);
1176 	read_relocs(fp);
1177 	if (ELF_BITS == 64)
1178 		percpu_init();
1179 	if (show_absolute_syms) {
1180 		print_absolute_symbols();
1181 		return;
1182 	}
1183 	if (show_absolute_relocs) {
1184 		print_absolute_relocs();
1185 		return;
1186 	}
1187 	if (show_reloc_info) {
1188 		print_reloc_info();
1189 		return;
1190 	}
1191 	emit_relocs(as_text, use_real_mode);
1192 }
1193