relocs.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651
  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 ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
  13. static Elf32_Ehdr ehdr;
  14. static unsigned long reloc_count, reloc_idx;
  15. static unsigned long *relocs;
  16. struct section {
  17. Elf32_Shdr shdr;
  18. struct section *link;
  19. Elf32_Sym *symtab;
  20. Elf32_Rel *reltab;
  21. char *strtab;
  22. };
  23. static struct section *secs;
  24. /*
  25. * Following symbols have been audited. There values are constant and do
  26. * not change if bzImage is loaded at a different physical address than
  27. * the address for which it has been compiled. Don't warn user about
  28. * absolute relocations present w.r.t these symbols.
  29. */
  30. static const char* safe_abs_relocs[] = {
  31. "xen_irq_disable_direct_reloc",
  32. "xen_save_fl_direct_reloc",
  33. };
  34. static int is_safe_abs_reloc(const char* sym_name)
  35. {
  36. int i;
  37. for (i = 0; i < ARRAY_SIZE(safe_abs_relocs); i++) {
  38. if (!strcmp(sym_name, safe_abs_relocs[i]))
  39. /* Match found */
  40. return 1;
  41. }
  42. if (strncmp(sym_name, "VDSO", 4) == 0)
  43. return 1;
  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 = secs[ehdr.e_shstrndx].strtab;
  134. name = "<noname>";
  135. if (shndx < ehdr.e_shnum) {
  136. name = sec_strtab + secs[shndx].shdr.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(secs[sym->st_shndx].shdr.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, SELFMAG) != 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. Elf32_Shdr shdr;
  232. secs = calloc(ehdr.e_shnum, sizeof(struct section));
  233. if (!secs) {
  234. die("Unable to allocate %d section headers\n",
  235. ehdr.e_shnum);
  236. }
  237. if (fseek(fp, ehdr.e_shoff, SEEK_SET) < 0) {
  238. die("Seek to %d failed: %s\n",
  239. ehdr.e_shoff, strerror(errno));
  240. }
  241. for (i = 0; i < ehdr.e_shnum; i++) {
  242. struct section *sec = &secs[i];
  243. if (fread(&shdr, sizeof shdr, 1, fp) != 1)
  244. die("Cannot read ELF section headers %d/%d: %s\n",
  245. i, ehdr.e_shnum, strerror(errno));
  246. sec->shdr.sh_name = elf32_to_cpu(shdr.sh_name);
  247. sec->shdr.sh_type = elf32_to_cpu(shdr.sh_type);
  248. sec->shdr.sh_flags = elf32_to_cpu(shdr.sh_flags);
  249. sec->shdr.sh_addr = elf32_to_cpu(shdr.sh_addr);
  250. sec->shdr.sh_offset = elf32_to_cpu(shdr.sh_offset);
  251. sec->shdr.sh_size = elf32_to_cpu(shdr.sh_size);
  252. sec->shdr.sh_link = elf32_to_cpu(shdr.sh_link);
  253. sec->shdr.sh_info = elf32_to_cpu(shdr.sh_info);
  254. sec->shdr.sh_addralign = elf32_to_cpu(shdr.sh_addralign);
  255. sec->shdr.sh_entsize = elf32_to_cpu(shdr.sh_entsize);
  256. if (sec->shdr.sh_link < ehdr.e_shnum)
  257. sec->link = &secs[sec->shdr.sh_link];
  258. }
  259. }
  260. static void read_strtabs(FILE *fp)
  261. {
  262. int i;
  263. for (i = 0; i < ehdr.e_shnum; i++) {
  264. struct section *sec = &secs[i];
  265. if (sec->shdr.sh_type != SHT_STRTAB) {
  266. continue;
  267. }
  268. sec->strtab = malloc(sec->shdr.sh_size);
  269. if (!sec->strtab) {
  270. die("malloc of %d bytes for strtab failed\n",
  271. sec->shdr.sh_size);
  272. }
  273. if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0) {
  274. die("Seek to %d failed: %s\n",
  275. sec->shdr.sh_offset, strerror(errno));
  276. }
  277. if (fread(sec->strtab, 1, sec->shdr.sh_size, fp)
  278. != sec->shdr.sh_size) {
  279. die("Cannot read symbol table: %s\n",
  280. strerror(errno));
  281. }
  282. }
  283. }
  284. static void read_symtabs(FILE *fp)
  285. {
  286. int i,j;
  287. for (i = 0; i < ehdr.e_shnum; i++) {
  288. struct section *sec = &secs[i];
  289. if (sec->shdr.sh_type != SHT_SYMTAB) {
  290. continue;
  291. }
  292. sec->symtab = malloc(sec->shdr.sh_size);
  293. if (!sec->symtab) {
  294. die("malloc of %d bytes for symtab failed\n",
  295. sec->shdr.sh_size);
  296. }
  297. if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0) {
  298. die("Seek to %d failed: %s\n",
  299. sec->shdr.sh_offset, strerror(errno));
  300. }
  301. if (fread(sec->symtab, 1, sec->shdr.sh_size, fp)
  302. != sec->shdr.sh_size) {
  303. die("Cannot read symbol table: %s\n",
  304. strerror(errno));
  305. }
  306. for (j = 0; j < sec->shdr.sh_size/sizeof(Elf32_Sym); j++) {
  307. Elf32_Sym *sym = &sec->symtab[j];
  308. sym->st_name = elf32_to_cpu(sym->st_name);
  309. sym->st_value = elf32_to_cpu(sym->st_value);
  310. sym->st_size = elf32_to_cpu(sym->st_size);
  311. sym->st_shndx = elf16_to_cpu(sym->st_shndx);
  312. }
  313. }
  314. }
  315. static void read_relocs(FILE *fp)
  316. {
  317. int i,j;
  318. for (i = 0; i < ehdr.e_shnum; i++) {
  319. struct section *sec = &secs[i];
  320. if (sec->shdr.sh_type != SHT_REL) {
  321. continue;
  322. }
  323. sec->reltab = malloc(sec->shdr.sh_size);
  324. if (!sec->reltab) {
  325. die("malloc of %d bytes for relocs failed\n",
  326. sec->shdr.sh_size);
  327. }
  328. if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0) {
  329. die("Seek to %d failed: %s\n",
  330. sec->shdr.sh_offset, strerror(errno));
  331. }
  332. if (fread(sec->reltab, 1, sec->shdr.sh_size, fp)
  333. != sec->shdr.sh_size) {
  334. die("Cannot read symbol table: %s\n",
  335. strerror(errno));
  336. }
  337. for (j = 0; j < sec->shdr.sh_size/sizeof(Elf32_Rel); j++) {
  338. Elf32_Rel *rel = &sec->reltab[j];
  339. rel->r_offset = elf32_to_cpu(rel->r_offset);
  340. rel->r_info = elf32_to_cpu(rel->r_info);
  341. }
  342. }
  343. }
  344. static void print_absolute_symbols(void)
  345. {
  346. int i;
  347. printf("Absolute symbols\n");
  348. printf(" Num: Value Size Type Bind Visibility Name\n");
  349. for (i = 0; i < ehdr.e_shnum; i++) {
  350. struct section *sec = &secs[i];
  351. char *sym_strtab;
  352. Elf32_Sym *sh_symtab;
  353. int j;
  354. if (sec->shdr.sh_type != SHT_SYMTAB) {
  355. continue;
  356. }
  357. sh_symtab = sec->symtab;
  358. sym_strtab = sec->link->strtab;
  359. for (j = 0; j < sec->shdr.sh_size/sizeof(Elf32_Sym); j++) {
  360. Elf32_Sym *sym;
  361. const char *name;
  362. sym = &sec->symtab[j];
  363. name = sym_name(sym_strtab, sym);
  364. if (sym->st_shndx != SHN_ABS) {
  365. continue;
  366. }
  367. printf("%5d %08x %5d %10s %10s %12s %s\n",
  368. j, sym->st_value, sym->st_size,
  369. sym_type(ELF32_ST_TYPE(sym->st_info)),
  370. sym_bind(ELF32_ST_BIND(sym->st_info)),
  371. sym_visibility(ELF32_ST_VISIBILITY(sym->st_other)),
  372. name);
  373. }
  374. }
  375. printf("\n");
  376. }
  377. static void print_absolute_relocs(void)
  378. {
  379. int i, printed = 0;
  380. for (i = 0; i < ehdr.e_shnum; i++) {
  381. struct section *sec = &secs[i];
  382. struct section *sec_applies, *sec_symtab;
  383. char *sym_strtab;
  384. Elf32_Sym *sh_symtab;
  385. int j;
  386. if (sec->shdr.sh_type != SHT_REL) {
  387. continue;
  388. }
  389. sec_symtab = sec->link;
  390. sec_applies = &secs[sec->shdr.sh_info];
  391. if (!(sec_applies->shdr.sh_flags & SHF_ALLOC)) {
  392. continue;
  393. }
  394. sh_symtab = sec_symtab->symtab;
  395. sym_strtab = sec_symtab->link->strtab;
  396. for (j = 0; j < sec->shdr.sh_size/sizeof(Elf32_Rel); j++) {
  397. Elf32_Rel *rel;
  398. Elf32_Sym *sym;
  399. const char *name;
  400. rel = &sec->reltab[j];
  401. sym = &sh_symtab[ELF32_R_SYM(rel->r_info)];
  402. name = sym_name(sym_strtab, sym);
  403. if (sym->st_shndx != SHN_ABS) {
  404. continue;
  405. }
  406. /* Absolute symbols are not relocated if bzImage is
  407. * loaded at a non-compiled address. Display a warning
  408. * to user at compile time about the absolute
  409. * relocations present.
  410. *
  411. * User need to audit the code to make sure
  412. * some symbols which should have been section
  413. * relative have not become absolute because of some
  414. * linker optimization or wrong programming usage.
  415. *
  416. * Before warning check if this absolute symbol
  417. * relocation is harmless.
  418. */
  419. if (is_safe_abs_reloc(name))
  420. continue;
  421. if (!printed) {
  422. printf("WARNING: Absolute relocations"
  423. " present\n");
  424. printf("Offset Info Type Sym.Value "
  425. "Sym.Name\n");
  426. printed = 1;
  427. }
  428. printf("%08x %08x %10s %08x %s\n",
  429. rel->r_offset,
  430. rel->r_info,
  431. rel_type(ELF32_R_TYPE(rel->r_info)),
  432. sym->st_value,
  433. name);
  434. }
  435. }
  436. if (printed)
  437. printf("\n");
  438. }
  439. static void walk_relocs(void (*visit)(Elf32_Rel *rel, Elf32_Sym *sym))
  440. {
  441. int i;
  442. /* Walk through the relocations */
  443. for (i = 0; i < ehdr.e_shnum; i++) {
  444. char *sym_strtab;
  445. Elf32_Sym *sh_symtab;
  446. struct section *sec_applies, *sec_symtab;
  447. int j;
  448. struct section *sec = &secs[i];
  449. if (sec->shdr.sh_type != SHT_REL) {
  450. continue;
  451. }
  452. sec_symtab = sec->link;
  453. sec_applies = &secs[sec->shdr.sh_info];
  454. if (!(sec_applies->shdr.sh_flags & SHF_ALLOC)) {
  455. continue;
  456. }
  457. sh_symtab = sec_symtab->symtab;
  458. sym_strtab = sec_symtab->link->strtab;
  459. for (j = 0; j < sec->shdr.sh_size/sizeof(Elf32_Rel); j++) {
  460. Elf32_Rel *rel;
  461. Elf32_Sym *sym;
  462. unsigned r_type;
  463. rel = &sec->reltab[j];
  464. sym = &sh_symtab[ELF32_R_SYM(rel->r_info)];
  465. r_type = ELF32_R_TYPE(rel->r_info);
  466. /* Don't visit relocations to absolute symbols */
  467. if (sym->st_shndx == SHN_ABS) {
  468. continue;
  469. }
  470. if (r_type == R_386_NONE || r_type == R_386_PC32) {
  471. /*
  472. * NONE can be ignored and and PC relative
  473. * relocations don't need to be adjusted.
  474. */
  475. }
  476. else if (r_type == R_386_32) {
  477. /* Visit relocations that need to be adjusted */
  478. visit(rel, sym);
  479. }
  480. else {
  481. die("Unsupported relocation type: %d\n", r_type);
  482. }
  483. }
  484. }
  485. }
  486. static void count_reloc(Elf32_Rel *rel, Elf32_Sym *sym)
  487. {
  488. reloc_count += 1;
  489. }
  490. static void collect_reloc(Elf32_Rel *rel, Elf32_Sym *sym)
  491. {
  492. /* Remember the address that needs to be adjusted. */
  493. relocs[reloc_idx++] = rel->r_offset;
  494. }
  495. static int cmp_relocs(const void *va, const void *vb)
  496. {
  497. const unsigned long *a, *b;
  498. a = va; b = vb;
  499. return (*a == *b)? 0 : (*a > *b)? 1 : -1;
  500. }
  501. static void emit_relocs(int as_text)
  502. {
  503. int i;
  504. /* Count how many relocations I have and allocate space for them. */
  505. reloc_count = 0;
  506. walk_relocs(count_reloc);
  507. relocs = malloc(reloc_count * sizeof(relocs[0]));
  508. if (!relocs) {
  509. die("malloc of %d entries for relocs failed\n",
  510. reloc_count);
  511. }
  512. /* Collect up the relocations */
  513. reloc_idx = 0;
  514. walk_relocs(collect_reloc);
  515. /* Order the relocations for more efficient processing */
  516. qsort(relocs, reloc_count, sizeof(relocs[0]), cmp_relocs);
  517. /* Print the relocations */
  518. if (as_text) {
  519. /* Print the relocations in a form suitable that
  520. * gas will like.
  521. */
  522. printf(".section \".data.reloc\",\"a\"\n");
  523. printf(".balign 4\n");
  524. for (i = 0; i < reloc_count; i++) {
  525. printf("\t .long 0x%08lx\n", relocs[i]);
  526. }
  527. printf("\n");
  528. }
  529. else {
  530. unsigned char buf[4];
  531. buf[0] = buf[1] = buf[2] = buf[3] = 0;
  532. /* Print a stop */
  533. printf("%c%c%c%c", buf[0], buf[1], buf[2], buf[3]);
  534. /* Now print each relocation */
  535. for (i = 0; i < reloc_count; i++) {
  536. buf[0] = (relocs[i] >> 0) & 0xff;
  537. buf[1] = (relocs[i] >> 8) & 0xff;
  538. buf[2] = (relocs[i] >> 16) & 0xff;
  539. buf[3] = (relocs[i] >> 24) & 0xff;
  540. printf("%c%c%c%c", buf[0], buf[1], buf[2], buf[3]);
  541. }
  542. }
  543. }
  544. static void usage(void)
  545. {
  546. die("relocs [--abs-syms |--abs-relocs | --text] vmlinux\n");
  547. }
  548. int main(int argc, char **argv)
  549. {
  550. int show_absolute_syms, show_absolute_relocs;
  551. int as_text;
  552. const char *fname;
  553. FILE *fp;
  554. int i;
  555. show_absolute_syms = 0;
  556. show_absolute_relocs = 0;
  557. as_text = 0;
  558. fname = NULL;
  559. for (i = 1; i < argc; i++) {
  560. char *arg = argv[i];
  561. if (*arg == '-') {
  562. if (strcmp(argv[1], "--abs-syms") == 0) {
  563. show_absolute_syms = 1;
  564. continue;
  565. }
  566. if (strcmp(argv[1], "--abs-relocs") == 0) {
  567. show_absolute_relocs = 1;
  568. continue;
  569. }
  570. else if (strcmp(argv[1], "--text") == 0) {
  571. as_text = 1;
  572. continue;
  573. }
  574. }
  575. else if (!fname) {
  576. fname = arg;
  577. continue;
  578. }
  579. usage();
  580. }
  581. if (!fname) {
  582. usage();
  583. }
  584. fp = fopen(fname, "r");
  585. if (!fp) {
  586. die("Cannot open %s: %s\n",
  587. fname, strerror(errno));
  588. }
  589. read_ehdr(fp);
  590. read_shdrs(fp);
  591. read_strtabs(fp);
  592. read_symtabs(fp);
  593. read_relocs(fp);
  594. if (show_absolute_syms) {
  595. print_absolute_symbols();
  596. return 0;
  597. }
  598. if (show_absolute_relocs) {
  599. print_absolute_relocs();
  600. return 0;
  601. }
  602. emit_relocs(as_text);
  603. return 0;
  604. }