relocs.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  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. #define MAX_SHDRS 100
  13. #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
  14. static Elf32_Ehdr ehdr;
  15. static Elf32_Shdr shdr[MAX_SHDRS];
  16. static Elf32_Sym *symtab[MAX_SHDRS];
  17. static Elf32_Rel *reltab[MAX_SHDRS];
  18. static char *strtab[MAX_SHDRS];
  19. static unsigned long reloc_count, reloc_idx;
  20. static unsigned long *relocs;
  21. /*
  22. * Following symbols have been audited. There values are constant and do
  23. * not change if bzImage is loaded at a different physical address than
  24. * the address for which it has been compiled. Don't warn user about
  25. * absolute relocations present w.r.t these symbols.
  26. */
  27. static const char* safe_abs_relocs[] = {
  28. "__kernel_vsyscall",
  29. "__kernel_rt_sigreturn",
  30. "__kernel_sigreturn",
  31. "SYSENTER_RETURN",
  32. "VDSO_NOTE_MASK",
  33. "xen_irq_disable_direct_reloc",
  34. "xen_save_fl_direct_reloc",
  35. };
  36. static int is_safe_abs_reloc(const char* sym_name)
  37. {
  38. int i, array_size;
  39. array_size = sizeof(safe_abs_relocs)/sizeof(char*);
  40. for(i = 0; i < array_size; i++) {
  41. if (!strcmp(sym_name, safe_abs_relocs[i]))
  42. /* Match found */
  43. return 1;
  44. }
  45. if (strncmp(sym_name, "__crc_", 6) == 0)
  46. return 1;
  47. return 0;
  48. }
  49. static void die(char *fmt, ...)
  50. {
  51. va_list ap;
  52. va_start(ap, fmt);
  53. vfprintf(stderr, fmt, ap);
  54. va_end(ap);
  55. exit(1);
  56. }
  57. static const char *sym_type(unsigned type)
  58. {
  59. static const char *type_name[] = {
  60. #define SYM_TYPE(X) [X] = #X
  61. SYM_TYPE(STT_NOTYPE),
  62. SYM_TYPE(STT_OBJECT),
  63. SYM_TYPE(STT_FUNC),
  64. SYM_TYPE(STT_SECTION),
  65. SYM_TYPE(STT_FILE),
  66. SYM_TYPE(STT_COMMON),
  67. SYM_TYPE(STT_TLS),
  68. #undef SYM_TYPE
  69. };
  70. const char *name = "unknown sym type name";
  71. if (type < ARRAY_SIZE(type_name)) {
  72. name = type_name[type];
  73. }
  74. return name;
  75. }
  76. static const char *sym_bind(unsigned bind)
  77. {
  78. static const char *bind_name[] = {
  79. #define SYM_BIND(X) [X] = #X
  80. SYM_BIND(STB_LOCAL),
  81. SYM_BIND(STB_GLOBAL),
  82. SYM_BIND(STB_WEAK),
  83. #undef SYM_BIND
  84. };
  85. const char *name = "unknown sym bind name";
  86. if (bind < ARRAY_SIZE(bind_name)) {
  87. name = bind_name[bind];
  88. }
  89. return name;
  90. }
  91. static const char *sym_visibility(unsigned visibility)
  92. {
  93. static const char *visibility_name[] = {
  94. #define SYM_VISIBILITY(X) [X] = #X
  95. SYM_VISIBILITY(STV_DEFAULT),
  96. SYM_VISIBILITY(STV_INTERNAL),
  97. SYM_VISIBILITY(STV_HIDDEN),
  98. SYM_VISIBILITY(STV_PROTECTED),
  99. #undef SYM_VISIBILITY
  100. };
  101. const char *name = "unknown sym visibility name";
  102. if (visibility < ARRAY_SIZE(visibility_name)) {
  103. name = visibility_name[visibility];
  104. }
  105. return name;
  106. }
  107. static const char *rel_type(unsigned type)
  108. {
  109. static const char *type_name[] = {
  110. #define REL_TYPE(X) [X] = #X
  111. REL_TYPE(R_386_NONE),
  112. REL_TYPE(R_386_32),
  113. REL_TYPE(R_386_PC32),
  114. REL_TYPE(R_386_GOT32),
  115. REL_TYPE(R_386_PLT32),
  116. REL_TYPE(R_386_COPY),
  117. REL_TYPE(R_386_GLOB_DAT),
  118. REL_TYPE(R_386_JMP_SLOT),
  119. REL_TYPE(R_386_RELATIVE),
  120. REL_TYPE(R_386_GOTOFF),
  121. REL_TYPE(R_386_GOTPC),
  122. #undef REL_TYPE
  123. };
  124. const char *name = "unknown type rel type name";
  125. if (type < ARRAY_SIZE(type_name)) {
  126. name = type_name[type];
  127. }
  128. return name;
  129. }
  130. static const char *sec_name(unsigned shndx)
  131. {
  132. const char *sec_strtab;
  133. const char *name;
  134. sec_strtab = strtab[ehdr.e_shstrndx];
  135. name = "<noname>";
  136. if (shndx < ehdr.e_shnum) {
  137. name = sec_strtab + shdr[shndx].sh_name;
  138. }
  139. else if (shndx == SHN_ABS) {
  140. name = "ABSOLUTE";
  141. }
  142. else if (shndx == SHN_COMMON) {
  143. name = "COMMON";
  144. }
  145. return name;
  146. }
  147. static const char *sym_name(const char *sym_strtab, Elf32_Sym *sym)
  148. {
  149. const char *name;
  150. name = "<noname>";
  151. if (sym->st_name) {
  152. name = sym_strtab + sym->st_name;
  153. }
  154. else {
  155. name = sec_name(shdr[sym->st_shndx].sh_name);
  156. }
  157. return name;
  158. }
  159. #if BYTE_ORDER == LITTLE_ENDIAN
  160. #define le16_to_cpu(val) (val)
  161. #define le32_to_cpu(val) (val)
  162. #endif
  163. #if BYTE_ORDER == BIG_ENDIAN
  164. #define le16_to_cpu(val) bswap_16(val)
  165. #define le32_to_cpu(val) bswap_32(val)
  166. #endif
  167. static uint16_t elf16_to_cpu(uint16_t val)
  168. {
  169. return le16_to_cpu(val);
  170. }
  171. static uint32_t elf32_to_cpu(uint32_t val)
  172. {
  173. return le32_to_cpu(val);
  174. }
  175. static void read_ehdr(FILE *fp)
  176. {
  177. if (fread(&ehdr, sizeof(ehdr), 1, fp) != 1) {
  178. die("Cannot read ELF header: %s\n",
  179. strerror(errno));
  180. }
  181. if (memcmp(ehdr.e_ident, ELFMAG, 4) != 0) {
  182. die("No ELF magic\n");
  183. }
  184. if (ehdr.e_ident[EI_CLASS] != ELFCLASS32) {
  185. die("Not a 32 bit executable\n");
  186. }
  187. if (ehdr.e_ident[EI_DATA] != ELFDATA2LSB) {
  188. die("Not a LSB ELF executable\n");
  189. }
  190. if (ehdr.e_ident[EI_VERSION] != EV_CURRENT) {
  191. die("Unknown ELF version\n");
  192. }
  193. /* Convert the fields to native endian */
  194. ehdr.e_type = elf16_to_cpu(ehdr.e_type);
  195. ehdr.e_machine = elf16_to_cpu(ehdr.e_machine);
  196. ehdr.e_version = elf32_to_cpu(ehdr.e_version);
  197. ehdr.e_entry = elf32_to_cpu(ehdr.e_entry);
  198. ehdr.e_phoff = elf32_to_cpu(ehdr.e_phoff);
  199. ehdr.e_shoff = elf32_to_cpu(ehdr.e_shoff);
  200. ehdr.e_flags = elf32_to_cpu(ehdr.e_flags);
  201. ehdr.e_ehsize = elf16_to_cpu(ehdr.e_ehsize);
  202. ehdr.e_phentsize = elf16_to_cpu(ehdr.e_phentsize);
  203. ehdr.e_phnum = elf16_to_cpu(ehdr.e_phnum);
  204. ehdr.e_shentsize = elf16_to_cpu(ehdr.e_shentsize);
  205. ehdr.e_shnum = elf16_to_cpu(ehdr.e_shnum);
  206. ehdr.e_shstrndx = elf16_to_cpu(ehdr.e_shstrndx);
  207. if ((ehdr.e_type != ET_EXEC) && (ehdr.e_type != ET_DYN)) {
  208. die("Unsupported ELF header type\n");
  209. }
  210. if (ehdr.e_machine != EM_386) {
  211. die("Not for x86\n");
  212. }
  213. if (ehdr.e_version != EV_CURRENT) {
  214. die("Unknown ELF version\n");
  215. }
  216. if (ehdr.e_ehsize != sizeof(Elf32_Ehdr)) {
  217. die("Bad Elf header size\n");
  218. }
  219. if (ehdr.e_phentsize != sizeof(Elf32_Phdr)) {
  220. die("Bad program header entry\n");
  221. }
  222. if (ehdr.e_shentsize != sizeof(Elf32_Shdr)) {
  223. die("Bad section header entry\n");
  224. }
  225. if (ehdr.e_shstrndx >= ehdr.e_shnum) {
  226. die("String table index out of bounds\n");
  227. }
  228. }
  229. static void read_shdrs(FILE *fp)
  230. {
  231. int i;
  232. if (ehdr.e_shnum > MAX_SHDRS) {
  233. die("%d section headers supported: %d\n",
  234. ehdr.e_shnum, MAX_SHDRS);
  235. }
  236. if (fseek(fp, ehdr.e_shoff, SEEK_SET) < 0) {
  237. die("Seek to %d failed: %s\n",
  238. ehdr.e_shoff, strerror(errno));
  239. }
  240. if (fread(&shdr, sizeof(shdr[0]), ehdr.e_shnum, fp) != ehdr.e_shnum) {
  241. die("Cannot read ELF section headers: %s\n",
  242. strerror(errno));
  243. }
  244. for(i = 0; i < ehdr.e_shnum; i++) {
  245. shdr[i].sh_name = elf32_to_cpu(shdr[i].sh_name);
  246. shdr[i].sh_type = elf32_to_cpu(shdr[i].sh_type);
  247. shdr[i].sh_flags = elf32_to_cpu(shdr[i].sh_flags);
  248. shdr[i].sh_addr = elf32_to_cpu(shdr[i].sh_addr);
  249. shdr[i].sh_offset = elf32_to_cpu(shdr[i].sh_offset);
  250. shdr[i].sh_size = elf32_to_cpu(shdr[i].sh_size);
  251. shdr[i].sh_link = elf32_to_cpu(shdr[i].sh_link);
  252. shdr[i].sh_info = elf32_to_cpu(shdr[i].sh_info);
  253. shdr[i].sh_addralign = elf32_to_cpu(shdr[i].sh_addralign);
  254. shdr[i].sh_entsize = elf32_to_cpu(shdr[i].sh_entsize);
  255. }
  256. }
  257. static void read_strtabs(FILE *fp)
  258. {
  259. int i;
  260. for(i = 0; i < ehdr.e_shnum; i++) {
  261. if (shdr[i].sh_type != SHT_STRTAB) {
  262. continue;
  263. }
  264. strtab[i] = malloc(shdr[i].sh_size);
  265. if (!strtab[i]) {
  266. die("malloc of %d bytes for strtab failed\n",
  267. shdr[i].sh_size);
  268. }
  269. if (fseek(fp, shdr[i].sh_offset, SEEK_SET) < 0) {
  270. die("Seek to %d failed: %s\n",
  271. shdr[i].sh_offset, strerror(errno));
  272. }
  273. if (fread(strtab[i], 1, shdr[i].sh_size, fp) != shdr[i].sh_size) {
  274. die("Cannot read symbol table: %s\n",
  275. strerror(errno));
  276. }
  277. }
  278. }
  279. static void read_symtabs(FILE *fp)
  280. {
  281. int i,j;
  282. for(i = 0; i < ehdr.e_shnum; i++) {
  283. if (shdr[i].sh_type != SHT_SYMTAB) {
  284. continue;
  285. }
  286. symtab[i] = malloc(shdr[i].sh_size);
  287. if (!symtab[i]) {
  288. die("malloc of %d bytes for symtab failed\n",
  289. shdr[i].sh_size);
  290. }
  291. if (fseek(fp, shdr[i].sh_offset, SEEK_SET) < 0) {
  292. die("Seek to %d failed: %s\n",
  293. shdr[i].sh_offset, strerror(errno));
  294. }
  295. if (fread(symtab[i], 1, shdr[i].sh_size, fp) != shdr[i].sh_size) {
  296. die("Cannot read symbol table: %s\n",
  297. strerror(errno));
  298. }
  299. for(j = 0; j < shdr[i].sh_size/sizeof(symtab[i][0]); j++) {
  300. symtab[i][j].st_name = elf32_to_cpu(symtab[i][j].st_name);
  301. symtab[i][j].st_value = elf32_to_cpu(symtab[i][j].st_value);
  302. symtab[i][j].st_size = elf32_to_cpu(symtab[i][j].st_size);
  303. symtab[i][j].st_shndx = elf16_to_cpu(symtab[i][j].st_shndx);
  304. }
  305. }
  306. }
  307. static void read_relocs(FILE *fp)
  308. {
  309. int i,j;
  310. for(i = 0; i < ehdr.e_shnum; i++) {
  311. if (shdr[i].sh_type != SHT_REL) {
  312. continue;
  313. }
  314. reltab[i] = malloc(shdr[i].sh_size);
  315. if (!reltab[i]) {
  316. die("malloc of %d bytes for relocs failed\n",
  317. shdr[i].sh_size);
  318. }
  319. if (fseek(fp, shdr[i].sh_offset, SEEK_SET) < 0) {
  320. die("Seek to %d failed: %s\n",
  321. shdr[i].sh_offset, strerror(errno));
  322. }
  323. if (fread(reltab[i], 1, shdr[i].sh_size, fp) != shdr[i].sh_size) {
  324. die("Cannot read symbol table: %s\n",
  325. strerror(errno));
  326. }
  327. for(j = 0; j < shdr[i].sh_size/sizeof(reltab[0][0]); j++) {
  328. reltab[i][j].r_offset = elf32_to_cpu(reltab[i][j].r_offset);
  329. reltab[i][j].r_info = elf32_to_cpu(reltab[i][j].r_info);
  330. }
  331. }
  332. }
  333. static void print_absolute_symbols(void)
  334. {
  335. int i;
  336. printf("Absolute symbols\n");
  337. printf(" Num: Value Size Type Bind Visibility Name\n");
  338. for(i = 0; i < ehdr.e_shnum; i++) {
  339. char *sym_strtab;
  340. Elf32_Sym *sh_symtab;
  341. int j;
  342. if (shdr[i].sh_type != SHT_SYMTAB) {
  343. continue;
  344. }
  345. sh_symtab = symtab[i];
  346. sym_strtab = strtab[shdr[i].sh_link];
  347. for(j = 0; j < shdr[i].sh_size/sizeof(symtab[0][0]); j++) {
  348. Elf32_Sym *sym;
  349. const char *name;
  350. sym = &symtab[i][j];
  351. name = sym_name(sym_strtab, sym);
  352. if (sym->st_shndx != SHN_ABS) {
  353. continue;
  354. }
  355. printf("%5d %08x %5d %10s %10s %12s %s\n",
  356. j, sym->st_value, sym->st_size,
  357. sym_type(ELF32_ST_TYPE(sym->st_info)),
  358. sym_bind(ELF32_ST_BIND(sym->st_info)),
  359. sym_visibility(ELF32_ST_VISIBILITY(sym->st_other)),
  360. name);
  361. }
  362. }
  363. printf("\n");
  364. }
  365. static void print_absolute_relocs(void)
  366. {
  367. int i, printed = 0;
  368. for(i = 0; i < ehdr.e_shnum; i++) {
  369. char *sym_strtab;
  370. Elf32_Sym *sh_symtab;
  371. unsigned sec_applies, sec_symtab;
  372. int j;
  373. if (shdr[i].sh_type != SHT_REL) {
  374. continue;
  375. }
  376. sec_symtab = shdr[i].sh_link;
  377. sec_applies = shdr[i].sh_info;
  378. if (!(shdr[sec_applies].sh_flags & SHF_ALLOC)) {
  379. continue;
  380. }
  381. sh_symtab = symtab[sec_symtab];
  382. sym_strtab = strtab[shdr[sec_symtab].sh_link];
  383. for(j = 0; j < shdr[i].sh_size/sizeof(reltab[0][0]); j++) {
  384. Elf32_Rel *rel;
  385. Elf32_Sym *sym;
  386. const char *name;
  387. rel = &reltab[i][j];
  388. sym = &sh_symtab[ELF32_R_SYM(rel->r_info)];
  389. name = sym_name(sym_strtab, sym);
  390. if (sym->st_shndx != SHN_ABS) {
  391. continue;
  392. }
  393. /* Absolute symbols are not relocated if bzImage is
  394. * loaded at a non-compiled address. Display a warning
  395. * to user at compile time about the absolute
  396. * relocations present.
  397. *
  398. * User need to audit the code to make sure
  399. * some symbols which should have been section
  400. * relative have not become absolute because of some
  401. * linker optimization or wrong programming usage.
  402. *
  403. * Before warning check if this absolute symbol
  404. * relocation is harmless.
  405. */
  406. if (is_safe_abs_reloc(name))
  407. continue;
  408. if (!printed) {
  409. printf("WARNING: Absolute relocations"
  410. " present\n");
  411. printf("Offset Info Type Sym.Value "
  412. "Sym.Name\n");
  413. printed = 1;
  414. }
  415. printf("%08x %08x %10s %08x %s\n",
  416. rel->r_offset,
  417. rel->r_info,
  418. rel_type(ELF32_R_TYPE(rel->r_info)),
  419. sym->st_value,
  420. name);
  421. }
  422. }
  423. if (printed)
  424. printf("\n");
  425. }
  426. static void walk_relocs(void (*visit)(Elf32_Rel *rel, Elf32_Sym *sym))
  427. {
  428. int i;
  429. /* Walk through the relocations */
  430. for(i = 0; i < ehdr.e_shnum; i++) {
  431. char *sym_strtab;
  432. Elf32_Sym *sh_symtab;
  433. unsigned sec_applies, sec_symtab;
  434. int j;
  435. if (shdr[i].sh_type != SHT_REL) {
  436. continue;
  437. }
  438. sec_symtab = shdr[i].sh_link;
  439. sec_applies = shdr[i].sh_info;
  440. if (!(shdr[sec_applies].sh_flags & SHF_ALLOC)) {
  441. continue;
  442. }
  443. sh_symtab = symtab[sec_symtab];
  444. sym_strtab = strtab[shdr[sec_symtab].sh_link];
  445. for(j = 0; j < shdr[i].sh_size/sizeof(reltab[0][0]); j++) {
  446. Elf32_Rel *rel;
  447. Elf32_Sym *sym;
  448. unsigned r_type;
  449. rel = &reltab[i][j];
  450. sym = &sh_symtab[ELF32_R_SYM(rel->r_info)];
  451. r_type = ELF32_R_TYPE(rel->r_info);
  452. /* Don't visit relocations to absolute symbols */
  453. if (sym->st_shndx == SHN_ABS) {
  454. continue;
  455. }
  456. if (r_type == R_386_PC32) {
  457. /* PC relative relocations don't need to be adjusted */
  458. }
  459. else if (r_type == R_386_32) {
  460. /* Visit relocations that need to be adjusted */
  461. visit(rel, sym);
  462. }
  463. else {
  464. die("Unsupported relocation type: %d\n", r_type);
  465. }
  466. }
  467. }
  468. }
  469. static void count_reloc(Elf32_Rel *rel, Elf32_Sym *sym)
  470. {
  471. reloc_count += 1;
  472. }
  473. static void collect_reloc(Elf32_Rel *rel, Elf32_Sym *sym)
  474. {
  475. /* Remember the address that needs to be adjusted. */
  476. relocs[reloc_idx++] = rel->r_offset;
  477. }
  478. static int cmp_relocs(const void *va, const void *vb)
  479. {
  480. const unsigned long *a, *b;
  481. a = va; b = vb;
  482. return (*a == *b)? 0 : (*a > *b)? 1 : -1;
  483. }
  484. static void emit_relocs(int as_text)
  485. {
  486. int i;
  487. /* Count how many relocations I have and allocate space for them. */
  488. reloc_count = 0;
  489. walk_relocs(count_reloc);
  490. relocs = malloc(reloc_count * sizeof(relocs[0]));
  491. if (!relocs) {
  492. die("malloc of %d entries for relocs failed\n",
  493. reloc_count);
  494. }
  495. /* Collect up the relocations */
  496. reloc_idx = 0;
  497. walk_relocs(collect_reloc);
  498. /* Order the relocations for more efficient processing */
  499. qsort(relocs, reloc_count, sizeof(relocs[0]), cmp_relocs);
  500. /* Print the relocations */
  501. if (as_text) {
  502. /* Print the relocations in a form suitable that
  503. * gas will like.
  504. */
  505. printf(".section \".data.reloc\",\"a\"\n");
  506. printf(".balign 4\n");
  507. for(i = 0; i < reloc_count; i++) {
  508. printf("\t .long 0x%08lx\n", relocs[i]);
  509. }
  510. printf("\n");
  511. }
  512. else {
  513. unsigned char buf[4];
  514. buf[0] = buf[1] = buf[2] = buf[3] = 0;
  515. /* Print a stop */
  516. printf("%c%c%c%c", buf[0], buf[1], buf[2], buf[3]);
  517. /* Now print each relocation */
  518. for(i = 0; i < reloc_count; i++) {
  519. buf[0] = (relocs[i] >> 0) & 0xff;
  520. buf[1] = (relocs[i] >> 8) & 0xff;
  521. buf[2] = (relocs[i] >> 16) & 0xff;
  522. buf[3] = (relocs[i] >> 24) & 0xff;
  523. printf("%c%c%c%c", buf[0], buf[1], buf[2], buf[3]);
  524. }
  525. }
  526. }
  527. static void usage(void)
  528. {
  529. die("relocs [--abs-syms |--abs-relocs | --text] vmlinux\n");
  530. }
  531. int main(int argc, char **argv)
  532. {
  533. int show_absolute_syms, show_absolute_relocs;
  534. int as_text;
  535. const char *fname;
  536. FILE *fp;
  537. int i;
  538. show_absolute_syms = 0;
  539. show_absolute_relocs = 0;
  540. as_text = 0;
  541. fname = NULL;
  542. for(i = 1; i < argc; i++) {
  543. char *arg = argv[i];
  544. if (*arg == '-') {
  545. if (strcmp(argv[1], "--abs-syms") == 0) {
  546. show_absolute_syms = 1;
  547. continue;
  548. }
  549. if (strcmp(argv[1], "--abs-relocs") == 0) {
  550. show_absolute_relocs = 1;
  551. continue;
  552. }
  553. else if (strcmp(argv[1], "--text") == 0) {
  554. as_text = 1;
  555. continue;
  556. }
  557. }
  558. else if (!fname) {
  559. fname = arg;
  560. continue;
  561. }
  562. usage();
  563. }
  564. if (!fname) {
  565. usage();
  566. }
  567. fp = fopen(fname, "r");
  568. if (!fp) {
  569. die("Cannot open %s: %s\n",
  570. fname, strerror(errno));
  571. }
  572. read_ehdr(fp);
  573. read_shdrs(fp);
  574. read_strtabs(fp);
  575. read_symtabs(fp);
  576. read_relocs(fp);
  577. if (show_absolute_syms) {
  578. print_absolute_symbols();
  579. return 0;
  580. }
  581. if (show_absolute_relocs) {
  582. print_absolute_relocs();
  583. return 0;
  584. }
  585. emit_relocs(as_text);
  586. return 0;
  587. }