x86-relocs.c 19 KB

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