relocs.c 15 KB

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