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