relocs.c 16 KB

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