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