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