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