1 #include <stdio.h> 2 #include <stdarg.h> 3 #include <stdlib.h> 4 #include <stdint.h> 5 #include <string.h> 6 #include <errno.h> 7 #include <unistd.h> 8 #include <elf.h> 9 #include <byteswap.h> 10 #define USE_BSD 11 #include <endian.h> 12 #include <regex.h> 13 #include <tools/le_byteshift.h> 14 15 static void die(char *fmt, ...); 16 17 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) 18 static Elf32_Ehdr ehdr; 19 static unsigned long reloc_count, reloc_idx; 20 static unsigned long *relocs; 21 static unsigned long reloc16_count, reloc16_idx; 22 static unsigned long *relocs16; 23 24 struct section { 25 Elf32_Shdr shdr; 26 struct section *link; 27 Elf32_Sym *symtab; 28 Elf32_Rel *reltab; 29 char *strtab; 30 }; 31 static struct section *secs; 32 33 enum symtype { 34 S_ABS, 35 S_REL, 36 S_SEG, 37 S_LIN, 38 S_NSYMTYPES 39 }; 40 41 static const char * const sym_regex_kernel[S_NSYMTYPES] = { 42 /* 43 * Following symbols have been audited. There values are constant and do 44 * not change if bzImage is loaded at a different physical address than 45 * the address for which it has been compiled. Don't warn user about 46 * absolute relocations present w.r.t these symbols. 47 */ 48 [S_ABS] = 49 "^(xen_irq_disable_direct_reloc$|" 50 "xen_save_fl_direct_reloc$|" 51 "VDSO|" 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 "_end)$" 64 }; 65 66 67 static const char * const sym_regex_realmode[S_NSYMTYPES] = { 68 /* 69 * These symbols are known to be relative, even if the linker marks them 70 * as absolute (typically defined outside any section in the linker script.) 71 */ 72 [S_REL] = 73 "^pa_", 74 75 /* 76 * These are 16-bit segment symbols when compiling 16-bit code. 77 */ 78 [S_SEG] = 79 "^real_mode_seg$", 80 81 /* 82 * These are offsets belonging to segments, as opposed to linear addresses, 83 * when compiling 16-bit code. 84 */ 85 [S_LIN] = 86 "^pa_", 87 }; 88 89 static const char * const *sym_regex; 90 91 static regex_t sym_regex_c[S_NSYMTYPES]; 92 static int is_reloc(enum symtype type, const char *sym_name) 93 { 94 return sym_regex[type] && 95 !regexec(&sym_regex_c[type], sym_name, 0, NULL, 0); 96 } 97 98 static void regex_init(int use_real_mode) 99 { 100 char errbuf[128]; 101 int err; 102 int i; 103 104 if (use_real_mode) 105 sym_regex = sym_regex_realmode; 106 else 107 sym_regex = sym_regex_kernel; 108 109 for (i = 0; i < S_NSYMTYPES; i++) { 110 if (!sym_regex[i]) 111 continue; 112 113 err = regcomp(&sym_regex_c[i], sym_regex[i], 114 REG_EXTENDED|REG_NOSUB); 115 116 if (err) { 117 regerror(err, &sym_regex_c[i], errbuf, sizeof errbuf); 118 die("%s", errbuf); 119 } 120 } 121 } 122 123 static void die(char *fmt, ...) 124 { 125 va_list ap; 126 va_start(ap, fmt); 127 vfprintf(stderr, fmt, ap); 128 va_end(ap); 129 exit(1); 130 } 131 132 static const char *sym_type(unsigned type) 133 { 134 static const char *type_name[] = { 135 #define SYM_TYPE(X) [X] = #X 136 SYM_TYPE(STT_NOTYPE), 137 SYM_TYPE(STT_OBJECT), 138 SYM_TYPE(STT_FUNC), 139 SYM_TYPE(STT_SECTION), 140 SYM_TYPE(STT_FILE), 141 SYM_TYPE(STT_COMMON), 142 SYM_TYPE(STT_TLS), 143 #undef SYM_TYPE 144 }; 145 const char *name = "unknown sym type name"; 146 if (type < ARRAY_SIZE(type_name)) { 147 name = type_name[type]; 148 } 149 return name; 150 } 151 152 static const char *sym_bind(unsigned bind) 153 { 154 static const char *bind_name[] = { 155 #define SYM_BIND(X) [X] = #X 156 SYM_BIND(STB_LOCAL), 157 SYM_BIND(STB_GLOBAL), 158 SYM_BIND(STB_WEAK), 159 #undef SYM_BIND 160 }; 161 const char *name = "unknown sym bind name"; 162 if (bind < ARRAY_SIZE(bind_name)) { 163 name = bind_name[bind]; 164 } 165 return name; 166 } 167 168 static const char *sym_visibility(unsigned visibility) 169 { 170 static const char *visibility_name[] = { 171 #define SYM_VISIBILITY(X) [X] = #X 172 SYM_VISIBILITY(STV_DEFAULT), 173 SYM_VISIBILITY(STV_INTERNAL), 174 SYM_VISIBILITY(STV_HIDDEN), 175 SYM_VISIBILITY(STV_PROTECTED), 176 #undef SYM_VISIBILITY 177 }; 178 const char *name = "unknown sym visibility name"; 179 if (visibility < ARRAY_SIZE(visibility_name)) { 180 name = visibility_name[visibility]; 181 } 182 return name; 183 } 184 185 static const char *rel_type(unsigned type) 186 { 187 static const char *type_name[] = { 188 #define REL_TYPE(X) [X] = #X 189 REL_TYPE(R_386_NONE), 190 REL_TYPE(R_386_32), 191 REL_TYPE(R_386_PC32), 192 REL_TYPE(R_386_GOT32), 193 REL_TYPE(R_386_PLT32), 194 REL_TYPE(R_386_COPY), 195 REL_TYPE(R_386_GLOB_DAT), 196 REL_TYPE(R_386_JMP_SLOT), 197 REL_TYPE(R_386_RELATIVE), 198 REL_TYPE(R_386_GOTOFF), 199 REL_TYPE(R_386_GOTPC), 200 REL_TYPE(R_386_8), 201 REL_TYPE(R_386_PC8), 202 REL_TYPE(R_386_16), 203 REL_TYPE(R_386_PC16), 204 #undef REL_TYPE 205 }; 206 const char *name = "unknown type rel type name"; 207 if (type < ARRAY_SIZE(type_name) && type_name[type]) { 208 name = type_name[type]; 209 } 210 return name; 211 } 212 213 static const char *sec_name(unsigned shndx) 214 { 215 const char *sec_strtab; 216 const char *name; 217 sec_strtab = secs[ehdr.e_shstrndx].strtab; 218 name = "<noname>"; 219 if (shndx < ehdr.e_shnum) { 220 name = sec_strtab + secs[shndx].shdr.sh_name; 221 } 222 else if (shndx == SHN_ABS) { 223 name = "ABSOLUTE"; 224 } 225 else if (shndx == SHN_COMMON) { 226 name = "COMMON"; 227 } 228 return name; 229 } 230 231 static const char *sym_name(const char *sym_strtab, Elf32_Sym *sym) 232 { 233 const char *name; 234 name = "<noname>"; 235 if (sym->st_name) { 236 name = sym_strtab + sym->st_name; 237 } 238 else { 239 name = sec_name(sym->st_shndx); 240 } 241 return name; 242 } 243 244 245 246 #if BYTE_ORDER == LITTLE_ENDIAN 247 #define le16_to_cpu(val) (val) 248 #define le32_to_cpu(val) (val) 249 #endif 250 #if BYTE_ORDER == BIG_ENDIAN 251 #define le16_to_cpu(val) bswap_16(val) 252 #define le32_to_cpu(val) bswap_32(val) 253 #endif 254 255 static uint16_t elf16_to_cpu(uint16_t val) 256 { 257 return le16_to_cpu(val); 258 } 259 260 static uint32_t elf32_to_cpu(uint32_t val) 261 { 262 return le32_to_cpu(val); 263 } 264 265 static void read_ehdr(FILE *fp) 266 { 267 if (fread(&ehdr, sizeof(ehdr), 1, fp) != 1) { 268 die("Cannot read ELF header: %s\n", 269 strerror(errno)); 270 } 271 if (memcmp(ehdr.e_ident, ELFMAG, SELFMAG) != 0) { 272 die("No ELF magic\n"); 273 } 274 if (ehdr.e_ident[EI_CLASS] != ELFCLASS32) { 275 die("Not a 32 bit executable\n"); 276 } 277 if (ehdr.e_ident[EI_DATA] != ELFDATA2LSB) { 278 die("Not a LSB ELF executable\n"); 279 } 280 if (ehdr.e_ident[EI_VERSION] != EV_CURRENT) { 281 die("Unknown ELF version\n"); 282 } 283 /* Convert the fields to native endian */ 284 ehdr.e_type = elf16_to_cpu(ehdr.e_type); 285 ehdr.e_machine = elf16_to_cpu(ehdr.e_machine); 286 ehdr.e_version = elf32_to_cpu(ehdr.e_version); 287 ehdr.e_entry = elf32_to_cpu(ehdr.e_entry); 288 ehdr.e_phoff = elf32_to_cpu(ehdr.e_phoff); 289 ehdr.e_shoff = elf32_to_cpu(ehdr.e_shoff); 290 ehdr.e_flags = elf32_to_cpu(ehdr.e_flags); 291 ehdr.e_ehsize = elf16_to_cpu(ehdr.e_ehsize); 292 ehdr.e_phentsize = elf16_to_cpu(ehdr.e_phentsize); 293 ehdr.e_phnum = elf16_to_cpu(ehdr.e_phnum); 294 ehdr.e_shentsize = elf16_to_cpu(ehdr.e_shentsize); 295 ehdr.e_shnum = elf16_to_cpu(ehdr.e_shnum); 296 ehdr.e_shstrndx = elf16_to_cpu(ehdr.e_shstrndx); 297 298 if ((ehdr.e_type != ET_EXEC) && (ehdr.e_type != ET_DYN)) { 299 die("Unsupported ELF header type\n"); 300 } 301 if (ehdr.e_machine != EM_386) { 302 die("Not for x86\n"); 303 } 304 if (ehdr.e_version != EV_CURRENT) { 305 die("Unknown ELF version\n"); 306 } 307 if (ehdr.e_ehsize != sizeof(Elf32_Ehdr)) { 308 die("Bad Elf header size\n"); 309 } 310 if (ehdr.e_phentsize != sizeof(Elf32_Phdr)) { 311 die("Bad program header entry\n"); 312 } 313 if (ehdr.e_shentsize != sizeof(Elf32_Shdr)) { 314 die("Bad section header entry\n"); 315 } 316 if (ehdr.e_shstrndx >= ehdr.e_shnum) { 317 die("String table index out of bounds\n"); 318 } 319 } 320 321 static void read_shdrs(FILE *fp) 322 { 323 int i; 324 Elf32_Shdr shdr; 325 326 secs = calloc(ehdr.e_shnum, sizeof(struct section)); 327 if (!secs) { 328 die("Unable to allocate %d section headers\n", 329 ehdr.e_shnum); 330 } 331 if (fseek(fp, ehdr.e_shoff, SEEK_SET) < 0) { 332 die("Seek to %d failed: %s\n", 333 ehdr.e_shoff, strerror(errno)); 334 } 335 for (i = 0; i < ehdr.e_shnum; i++) { 336 struct section *sec = &secs[i]; 337 if (fread(&shdr, sizeof shdr, 1, fp) != 1) 338 die("Cannot read ELF section headers %d/%d: %s\n", 339 i, ehdr.e_shnum, strerror(errno)); 340 sec->shdr.sh_name = elf32_to_cpu(shdr.sh_name); 341 sec->shdr.sh_type = elf32_to_cpu(shdr.sh_type); 342 sec->shdr.sh_flags = elf32_to_cpu(shdr.sh_flags); 343 sec->shdr.sh_addr = elf32_to_cpu(shdr.sh_addr); 344 sec->shdr.sh_offset = elf32_to_cpu(shdr.sh_offset); 345 sec->shdr.sh_size = elf32_to_cpu(shdr.sh_size); 346 sec->shdr.sh_link = elf32_to_cpu(shdr.sh_link); 347 sec->shdr.sh_info = elf32_to_cpu(shdr.sh_info); 348 sec->shdr.sh_addralign = elf32_to_cpu(shdr.sh_addralign); 349 sec->shdr.sh_entsize = elf32_to_cpu(shdr.sh_entsize); 350 if (sec->shdr.sh_link < ehdr.e_shnum) 351 sec->link = &secs[sec->shdr.sh_link]; 352 } 353 354 } 355 356 static void read_strtabs(FILE *fp) 357 { 358 int i; 359 for (i = 0; i < ehdr.e_shnum; i++) { 360 struct section *sec = &secs[i]; 361 if (sec->shdr.sh_type != SHT_STRTAB) { 362 continue; 363 } 364 sec->strtab = malloc(sec->shdr.sh_size); 365 if (!sec->strtab) { 366 die("malloc of %d bytes for strtab failed\n", 367 sec->shdr.sh_size); 368 } 369 if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0) { 370 die("Seek to %d failed: %s\n", 371 sec->shdr.sh_offset, strerror(errno)); 372 } 373 if (fread(sec->strtab, 1, sec->shdr.sh_size, fp) 374 != sec->shdr.sh_size) { 375 die("Cannot read symbol table: %s\n", 376 strerror(errno)); 377 } 378 } 379 } 380 381 static void read_symtabs(FILE *fp) 382 { 383 int i,j; 384 for (i = 0; i < ehdr.e_shnum; i++) { 385 struct section *sec = &secs[i]; 386 if (sec->shdr.sh_type != SHT_SYMTAB) { 387 continue; 388 } 389 sec->symtab = malloc(sec->shdr.sh_size); 390 if (!sec->symtab) { 391 die("malloc of %d bytes for symtab failed\n", 392 sec->shdr.sh_size); 393 } 394 if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0) { 395 die("Seek to %d failed: %s\n", 396 sec->shdr.sh_offset, strerror(errno)); 397 } 398 if (fread(sec->symtab, 1, sec->shdr.sh_size, fp) 399 != sec->shdr.sh_size) { 400 die("Cannot read symbol table: %s\n", 401 strerror(errno)); 402 } 403 for (j = 0; j < sec->shdr.sh_size/sizeof(Elf32_Sym); j++) { 404 Elf32_Sym *sym = &sec->symtab[j]; 405 sym->st_name = elf32_to_cpu(sym->st_name); 406 sym->st_value = elf32_to_cpu(sym->st_value); 407 sym->st_size = elf32_to_cpu(sym->st_size); 408 sym->st_shndx = elf16_to_cpu(sym->st_shndx); 409 } 410 } 411 } 412 413 414 static void read_relocs(FILE *fp) 415 { 416 int i,j; 417 for (i = 0; i < ehdr.e_shnum; i++) { 418 struct section *sec = &secs[i]; 419 if (sec->shdr.sh_type != SHT_REL) { 420 continue; 421 } 422 sec->reltab = malloc(sec->shdr.sh_size); 423 if (!sec->reltab) { 424 die("malloc of %d bytes for relocs failed\n", 425 sec->shdr.sh_size); 426 } 427 if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0) { 428 die("Seek to %d failed: %s\n", 429 sec->shdr.sh_offset, strerror(errno)); 430 } 431 if (fread(sec->reltab, 1, sec->shdr.sh_size, fp) 432 != sec->shdr.sh_size) { 433 die("Cannot read symbol table: %s\n", 434 strerror(errno)); 435 } 436 for (j = 0; j < sec->shdr.sh_size/sizeof(Elf32_Rel); j++) { 437 Elf32_Rel *rel = &sec->reltab[j]; 438 rel->r_offset = elf32_to_cpu(rel->r_offset); 439 rel->r_info = elf32_to_cpu(rel->r_info); 440 } 441 } 442 } 443 444 445 static void print_absolute_symbols(void) 446 { 447 int i; 448 printf("Absolute symbols\n"); 449 printf(" Num: Value Size Type Bind Visibility Name\n"); 450 for (i = 0; i < ehdr.e_shnum; i++) { 451 struct section *sec = &secs[i]; 452 char *sym_strtab; 453 int j; 454 455 if (sec->shdr.sh_type != SHT_SYMTAB) { 456 continue; 457 } 458 sym_strtab = sec->link->strtab; 459 for (j = 0; j < sec->shdr.sh_size/sizeof(Elf32_Sym); j++) { 460 Elf32_Sym *sym; 461 const char *name; 462 sym = &sec->symtab[j]; 463 name = sym_name(sym_strtab, sym); 464 if (sym->st_shndx != SHN_ABS) { 465 continue; 466 } 467 printf("%5d %08x %5d %10s %10s %12s %s\n", 468 j, sym->st_value, sym->st_size, 469 sym_type(ELF32_ST_TYPE(sym->st_info)), 470 sym_bind(ELF32_ST_BIND(sym->st_info)), 471 sym_visibility(ELF32_ST_VISIBILITY(sym->st_other)), 472 name); 473 } 474 } 475 printf("\n"); 476 } 477 478 static void print_absolute_relocs(void) 479 { 480 int i, printed = 0; 481 482 for (i = 0; i < ehdr.e_shnum; i++) { 483 struct section *sec = &secs[i]; 484 struct section *sec_applies, *sec_symtab; 485 char *sym_strtab; 486 Elf32_Sym *sh_symtab; 487 int j; 488 if (sec->shdr.sh_type != SHT_REL) { 489 continue; 490 } 491 sec_symtab = sec->link; 492 sec_applies = &secs[sec->shdr.sh_info]; 493 if (!(sec_applies->shdr.sh_flags & SHF_ALLOC)) { 494 continue; 495 } 496 sh_symtab = sec_symtab->symtab; 497 sym_strtab = sec_symtab->link->strtab; 498 for (j = 0; j < sec->shdr.sh_size/sizeof(Elf32_Rel); j++) { 499 Elf32_Rel *rel; 500 Elf32_Sym *sym; 501 const char *name; 502 rel = &sec->reltab[j]; 503 sym = &sh_symtab[ELF32_R_SYM(rel->r_info)]; 504 name = sym_name(sym_strtab, sym); 505 if (sym->st_shndx != SHN_ABS) { 506 continue; 507 } 508 509 /* Absolute symbols are not relocated if bzImage is 510 * loaded at a non-compiled address. Display a warning 511 * to user at compile time about the absolute 512 * relocations present. 513 * 514 * User need to audit the code to make sure 515 * some symbols which should have been section 516 * relative have not become absolute because of some 517 * linker optimization or wrong programming usage. 518 * 519 * Before warning check if this absolute symbol 520 * relocation is harmless. 521 */ 522 if (is_reloc(S_ABS, name) || is_reloc(S_REL, name)) 523 continue; 524 525 if (!printed) { 526 printf("WARNING: Absolute relocations" 527 " present\n"); 528 printf("Offset Info Type Sym.Value " 529 "Sym.Name\n"); 530 printed = 1; 531 } 532 533 printf("%08x %08x %10s %08x %s\n", 534 rel->r_offset, 535 rel->r_info, 536 rel_type(ELF32_R_TYPE(rel->r_info)), 537 sym->st_value, 538 name); 539 } 540 } 541 542 if (printed) 543 printf("\n"); 544 } 545 546 static void walk_relocs(void (*visit)(Elf32_Rel *rel, Elf32_Sym *sym), 547 int use_real_mode) 548 { 549 int i; 550 /* Walk through the relocations */ 551 for (i = 0; i < ehdr.e_shnum; i++) { 552 char *sym_strtab; 553 Elf32_Sym *sh_symtab; 554 struct section *sec_applies, *sec_symtab; 555 int j; 556 struct section *sec = &secs[i]; 557 558 if (sec->shdr.sh_type != SHT_REL) { 559 continue; 560 } 561 sec_symtab = sec->link; 562 sec_applies = &secs[sec->shdr.sh_info]; 563 if (!(sec_applies->shdr.sh_flags & SHF_ALLOC)) { 564 continue; 565 } 566 sh_symtab = sec_symtab->symtab; 567 sym_strtab = sec_symtab->link->strtab; 568 for (j = 0; j < sec->shdr.sh_size/sizeof(Elf32_Rel); j++) { 569 Elf32_Rel *rel; 570 Elf32_Sym *sym; 571 unsigned r_type; 572 const char *symname; 573 rel = &sec->reltab[j]; 574 sym = &sh_symtab[ELF32_R_SYM(rel->r_info)]; 575 r_type = ELF32_R_TYPE(rel->r_info); 576 577 switch (r_type) { 578 case R_386_NONE: 579 case R_386_PC32: 580 case R_386_PC16: 581 case R_386_PC8: 582 /* 583 * NONE can be ignored and and PC relative 584 * relocations don't need to be adjusted. 585 */ 586 break; 587 588 case R_386_16: 589 symname = sym_name(sym_strtab, sym); 590 if (!use_real_mode) 591 goto bad; 592 if (sym->st_shndx == SHN_ABS) { 593 if (is_reloc(S_ABS, symname)) 594 break; 595 else if (!is_reloc(S_SEG, symname)) 596 goto bad; 597 } else { 598 if (is_reloc(S_LIN, symname)) 599 goto bad; 600 else 601 break; 602 } 603 visit(rel, sym); 604 break; 605 606 case R_386_32: 607 symname = sym_name(sym_strtab, sym); 608 if (sym->st_shndx == SHN_ABS) { 609 if (is_reloc(S_ABS, symname)) 610 break; 611 else if (!is_reloc(S_REL, symname)) 612 goto bad; 613 } else { 614 if (use_real_mode && 615 !is_reloc(S_LIN, symname)) 616 break; 617 } 618 visit(rel, sym); 619 break; 620 default: 621 die("Unsupported relocation type: %s (%d)\n", 622 rel_type(r_type), r_type); 623 break; 624 bad: 625 symname = sym_name(sym_strtab, sym); 626 die("Invalid %s relocation: %s\n", 627 rel_type(r_type), symname); 628 } 629 } 630 } 631 } 632 633 static void count_reloc(Elf32_Rel *rel, Elf32_Sym *sym) 634 { 635 if (ELF32_R_TYPE(rel->r_info) == R_386_16) 636 reloc16_count++; 637 else 638 reloc_count++; 639 } 640 641 static void collect_reloc(Elf32_Rel *rel, Elf32_Sym *sym) 642 { 643 /* Remember the address that needs to be adjusted. */ 644 if (ELF32_R_TYPE(rel->r_info) == R_386_16) 645 relocs16[reloc16_idx++] = rel->r_offset; 646 else 647 relocs[reloc_idx++] = rel->r_offset; 648 } 649 650 static int cmp_relocs(const void *va, const void *vb) 651 { 652 const unsigned long *a, *b; 653 a = va; b = vb; 654 return (*a == *b)? 0 : (*a > *b)? 1 : -1; 655 } 656 657 static int write32(unsigned int v, FILE *f) 658 { 659 unsigned char buf[4]; 660 661 put_unaligned_le32(v, buf); 662 return fwrite(buf, 1, 4, f) == 4 ? 0 : -1; 663 } 664 665 static void emit_relocs(int as_text, int use_real_mode) 666 { 667 int i; 668 /* Count how many relocations I have and allocate space for them. */ 669 reloc_count = 0; 670 walk_relocs(count_reloc, use_real_mode); 671 relocs = malloc(reloc_count * sizeof(relocs[0])); 672 if (!relocs) { 673 die("malloc of %d entries for relocs failed\n", 674 reloc_count); 675 } 676 677 relocs16 = malloc(reloc16_count * sizeof(relocs[0])); 678 if (!relocs16) { 679 die("malloc of %d entries for relocs16 failed\n", 680 reloc16_count); 681 } 682 /* Collect up the relocations */ 683 reloc_idx = 0; 684 walk_relocs(collect_reloc, use_real_mode); 685 686 if (reloc16_count && !use_real_mode) 687 die("Segment relocations found but --realmode not specified\n"); 688 689 /* Order the relocations for more efficient processing */ 690 qsort(relocs, reloc_count, sizeof(relocs[0]), cmp_relocs); 691 qsort(relocs16, reloc16_count, sizeof(relocs16[0]), cmp_relocs); 692 693 /* Print the relocations */ 694 if (as_text) { 695 /* Print the relocations in a form suitable that 696 * gas will like. 697 */ 698 printf(".section \".data.reloc\",\"a\"\n"); 699 printf(".balign 4\n"); 700 if (use_real_mode) { 701 printf("\t.long %lu\n", reloc16_count); 702 for (i = 0; i < reloc16_count; i++) 703 printf("\t.long 0x%08lx\n", relocs16[i]); 704 printf("\t.long %lu\n", reloc_count); 705 for (i = 0; i < reloc_count; i++) { 706 printf("\t.long 0x%08lx\n", relocs[i]); 707 } 708 } else { 709 /* Print a stop */ 710 printf("\t.long 0x%08lx\n", (unsigned long)0); 711 for (i = 0; i < reloc_count; i++) { 712 printf("\t.long 0x%08lx\n", relocs[i]); 713 } 714 } 715 716 printf("\n"); 717 } 718 else { 719 if (use_real_mode) { 720 write32(reloc16_count, stdout); 721 for (i = 0; i < reloc16_count; i++) 722 write32(relocs16[i], stdout); 723 write32(reloc_count, stdout); 724 725 /* Now print each relocation */ 726 for (i = 0; i < reloc_count; i++) 727 write32(relocs[i], stdout); 728 } else { 729 /* Print a stop */ 730 write32(0, stdout); 731 732 /* Now print each relocation */ 733 for (i = 0; i < reloc_count; i++) { 734 write32(relocs[i], stdout); 735 } 736 } 737 } 738 } 739 740 static void usage(void) 741 { 742 die("relocs [--abs-syms|--abs-relocs|--text|--realmode] vmlinux\n"); 743 } 744 745 int main(int argc, char **argv) 746 { 747 int show_absolute_syms, show_absolute_relocs; 748 int as_text, use_real_mode; 749 const char *fname; 750 FILE *fp; 751 int i; 752 753 show_absolute_syms = 0; 754 show_absolute_relocs = 0; 755 as_text = 0; 756 use_real_mode = 0; 757 fname = NULL; 758 for (i = 1; i < argc; i++) { 759 char *arg = argv[i]; 760 if (*arg == '-') { 761 if (strcmp(arg, "--abs-syms") == 0) { 762 show_absolute_syms = 1; 763 continue; 764 } 765 if (strcmp(arg, "--abs-relocs") == 0) { 766 show_absolute_relocs = 1; 767 continue; 768 } 769 if (strcmp(arg, "--text") == 0) { 770 as_text = 1; 771 continue; 772 } 773 if (strcmp(arg, "--realmode") == 0) { 774 use_real_mode = 1; 775 continue; 776 } 777 } 778 else if (!fname) { 779 fname = arg; 780 continue; 781 } 782 usage(); 783 } 784 if (!fname) { 785 usage(); 786 } 787 regex_init(use_real_mode); 788 fp = fopen(fname, "r"); 789 if (!fp) { 790 die("Cannot open %s: %s\n", 791 fname, strerror(errno)); 792 } 793 read_ehdr(fp); 794 read_shdrs(fp); 795 read_strtabs(fp); 796 read_symtabs(fp); 797 read_relocs(fp); 798 if (show_absolute_syms) { 799 print_absolute_symbols(); 800 return 0; 801 } 802 if (show_absolute_relocs) { 803 print_absolute_relocs(); 804 return 0; 805 } 806 emit_relocs(as_text, use_real_mode); 807 return 0; 808 } 809