relocs.c 19 KB

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