x86-relocs.c 19 KB

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