relocs.c 19 KB

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