relocs.c 15 KB

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