relocs.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680
  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. Elf32_Sym *sh_symtab;
  376. int j;
  377. if (sec->shdr.sh_type != SHT_SYMTAB) {
  378. continue;
  379. }
  380. sh_symtab = sec->symtab;
  381. sym_strtab = sec->link->strtab;
  382. for (j = 0; j < sec->shdr.sh_size/sizeof(Elf32_Sym); j++) {
  383. Elf32_Sym *sym;
  384. const char *name;
  385. sym = &sec->symtab[j];
  386. name = sym_name(sym_strtab, sym);
  387. if (sym->st_shndx != SHN_ABS) {
  388. continue;
  389. }
  390. printf("%5d %08x %5d %10s %10s %12s %s\n",
  391. j, sym->st_value, sym->st_size,
  392. sym_type(ELF32_ST_TYPE(sym->st_info)),
  393. sym_bind(ELF32_ST_BIND(sym->st_info)),
  394. sym_visibility(ELF32_ST_VISIBILITY(sym->st_other)),
  395. name);
  396. }
  397. }
  398. printf("\n");
  399. }
  400. static void print_absolute_relocs(void)
  401. {
  402. int i, printed = 0;
  403. for (i = 0; i < ehdr.e_shnum; i++) {
  404. struct section *sec = &secs[i];
  405. struct section *sec_applies, *sec_symtab;
  406. char *sym_strtab;
  407. Elf32_Sym *sh_symtab;
  408. int j;
  409. if (sec->shdr.sh_type != SHT_REL) {
  410. continue;
  411. }
  412. sec_symtab = sec->link;
  413. sec_applies = &secs[sec->shdr.sh_info];
  414. if (!(sec_applies->shdr.sh_flags & SHF_ALLOC)) {
  415. continue;
  416. }
  417. sh_symtab = sec_symtab->symtab;
  418. sym_strtab = sec_symtab->link->strtab;
  419. for (j = 0; j < sec->shdr.sh_size/sizeof(Elf32_Rel); j++) {
  420. Elf32_Rel *rel;
  421. Elf32_Sym *sym;
  422. const char *name;
  423. rel = &sec->reltab[j];
  424. sym = &sh_symtab[ELF32_R_SYM(rel->r_info)];
  425. name = sym_name(sym_strtab, sym);
  426. if (sym->st_shndx != SHN_ABS) {
  427. continue;
  428. }
  429. /* Absolute symbols are not relocated if bzImage is
  430. * loaded at a non-compiled address. Display a warning
  431. * to user at compile time about the absolute
  432. * relocations present.
  433. *
  434. * User need to audit the code to make sure
  435. * some symbols which should have been section
  436. * relative have not become absolute because of some
  437. * linker optimization or wrong programming usage.
  438. *
  439. * Before warning check if this absolute symbol
  440. * relocation is harmless.
  441. */
  442. if (is_abs_reloc(name) || is_rel_reloc(name))
  443. continue;
  444. if (!printed) {
  445. printf("WARNING: Absolute relocations"
  446. " present\n");
  447. printf("Offset Info Type Sym.Value "
  448. "Sym.Name\n");
  449. printed = 1;
  450. }
  451. printf("%08x %08x %10s %08x %s\n",
  452. rel->r_offset,
  453. rel->r_info,
  454. rel_type(ELF32_R_TYPE(rel->r_info)),
  455. sym->st_value,
  456. name);
  457. }
  458. }
  459. if (printed)
  460. printf("\n");
  461. }
  462. static void walk_relocs(void (*visit)(Elf32_Rel *rel, Elf32_Sym *sym))
  463. {
  464. int i;
  465. /* Walk through the relocations */
  466. for (i = 0; i < ehdr.e_shnum; i++) {
  467. char *sym_strtab;
  468. Elf32_Sym *sh_symtab;
  469. struct section *sec_applies, *sec_symtab;
  470. int j;
  471. struct section *sec = &secs[i];
  472. if (sec->shdr.sh_type != SHT_REL) {
  473. continue;
  474. }
  475. sec_symtab = sec->link;
  476. sec_applies = &secs[sec->shdr.sh_info];
  477. if (!(sec_applies->shdr.sh_flags & SHF_ALLOC)) {
  478. continue;
  479. }
  480. sh_symtab = sec_symtab->symtab;
  481. sym_strtab = sec_symtab->link->strtab;
  482. for (j = 0; j < sec->shdr.sh_size/sizeof(Elf32_Rel); j++) {
  483. Elf32_Rel *rel;
  484. Elf32_Sym *sym;
  485. unsigned r_type;
  486. rel = &sec->reltab[j];
  487. sym = &sh_symtab[ELF32_R_SYM(rel->r_info)];
  488. r_type = ELF32_R_TYPE(rel->r_info);
  489. /* Don't visit relocations to absolute symbols */
  490. if (sym->st_shndx == SHN_ABS &&
  491. !is_rel_reloc(sym_name(sym_strtab, sym))) {
  492. continue;
  493. }
  494. switch (r_type) {
  495. case R_386_NONE:
  496. case R_386_PC32:
  497. /*
  498. * NONE can be ignored and and PC relative
  499. * relocations don't need to be adjusted.
  500. */
  501. break;
  502. case R_386_32:
  503. /* Visit relocations that need to be adjusted */
  504. visit(rel, sym);
  505. break;
  506. default:
  507. die("Unsupported relocation type: %s (%d)\n",
  508. rel_type(r_type), r_type);
  509. break;
  510. }
  511. }
  512. }
  513. }
  514. static void count_reloc(Elf32_Rel *rel, Elf32_Sym *sym)
  515. {
  516. reloc_count += 1;
  517. }
  518. static void collect_reloc(Elf32_Rel *rel, Elf32_Sym *sym)
  519. {
  520. /* Remember the address that needs to be adjusted. */
  521. relocs[reloc_idx++] = rel->r_offset;
  522. }
  523. static int cmp_relocs(const void *va, const void *vb)
  524. {
  525. const unsigned long *a, *b;
  526. a = va; b = vb;
  527. return (*a == *b)? 0 : (*a > *b)? 1 : -1;
  528. }
  529. static void emit_relocs(int as_text)
  530. {
  531. int i;
  532. /* Count how many relocations I have and allocate space for them. */
  533. reloc_count = 0;
  534. walk_relocs(count_reloc);
  535. relocs = malloc(reloc_count * sizeof(relocs[0]));
  536. if (!relocs) {
  537. die("malloc of %d entries for relocs failed\n",
  538. reloc_count);
  539. }
  540. /* Collect up the relocations */
  541. reloc_idx = 0;
  542. walk_relocs(collect_reloc);
  543. /* Order the relocations for more efficient processing */
  544. qsort(relocs, reloc_count, sizeof(relocs[0]), cmp_relocs);
  545. /* Print the relocations */
  546. if (as_text) {
  547. /* Print the relocations in a form suitable that
  548. * gas will like.
  549. */
  550. printf(".section \".data.reloc\",\"a\"\n");
  551. printf(".balign 4\n");
  552. for (i = 0; i < reloc_count; i++) {
  553. printf("\t .long 0x%08lx\n", relocs[i]);
  554. }
  555. printf("\n");
  556. }
  557. else {
  558. unsigned char buf[4];
  559. /* Print a stop */
  560. fwrite("\0\0\0\0", 4, 1, stdout);
  561. /* Now print each relocation */
  562. for (i = 0; i < reloc_count; i++) {
  563. put_unaligned_le32(relocs[i], buf);
  564. fwrite(buf, 4, 1, stdout);
  565. }
  566. }
  567. }
  568. static void usage(void)
  569. {
  570. die("relocs [--abs-syms |--abs-relocs | --text] vmlinux\n");
  571. }
  572. int main(int argc, char **argv)
  573. {
  574. int show_absolute_syms, show_absolute_relocs;
  575. int as_text;
  576. const char *fname;
  577. FILE *fp;
  578. int i;
  579. regex_init();
  580. show_absolute_syms = 0;
  581. show_absolute_relocs = 0;
  582. as_text = 0;
  583. fname = NULL;
  584. for (i = 1; i < argc; i++) {
  585. char *arg = argv[i];
  586. if (*arg == '-') {
  587. if (strcmp(argv[1], "--abs-syms") == 0) {
  588. show_absolute_syms = 1;
  589. continue;
  590. }
  591. if (strcmp(argv[1], "--abs-relocs") == 0) {
  592. show_absolute_relocs = 1;
  593. continue;
  594. }
  595. else if (strcmp(argv[1], "--text") == 0) {
  596. as_text = 1;
  597. continue;
  598. }
  599. }
  600. else if (!fname) {
  601. fname = arg;
  602. continue;
  603. }
  604. usage();
  605. }
  606. if (!fname) {
  607. usage();
  608. }
  609. fp = fopen(fname, "r");
  610. if (!fp) {
  611. die("Cannot open %s: %s\n",
  612. fname, strerror(errno));
  613. }
  614. read_ehdr(fp);
  615. read_shdrs(fp);
  616. read_strtabs(fp);
  617. read_symtabs(fp);
  618. read_relocs(fp);
  619. if (show_absolute_syms) {
  620. print_absolute_symbols();
  621. return 0;
  622. }
  623. if (show_absolute_relocs) {
  624. print_absolute_relocs();
  625. return 0;
  626. }
  627. emit_relocs(as_text);
  628. return 0;
  629. }