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