relocs.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101
  1. /* This is included from relocs_32/64.c */
  2. #define ElfW(type) _ElfW(ELF_BITS, type)
  3. #define _ElfW(bits, type) __ElfW(bits, type)
  4. #define __ElfW(bits, type) Elf##bits##_##type
  5. #define Elf_Addr ElfW(Addr)
  6. #define Elf_Ehdr ElfW(Ehdr)
  7. #define Elf_Phdr ElfW(Phdr)
  8. #define Elf_Shdr ElfW(Shdr)
  9. #define Elf_Sym ElfW(Sym)
  10. static Elf_Ehdr ehdr;
  11. struct relocs {
  12. uint32_t *offset;
  13. unsigned long count;
  14. unsigned long size;
  15. };
  16. static struct relocs relocs16;
  17. static struct relocs relocs32;
  18. #if ELF_BITS == 64
  19. static struct relocs relocs32neg;
  20. static struct relocs relocs64;
  21. #endif
  22. struct section {
  23. Elf_Shdr shdr;
  24. struct section *link;
  25. Elf_Sym *symtab;
  26. Elf_Rel *reltab;
  27. char *strtab;
  28. };
  29. static struct section *secs;
  30. static const char * const sym_regex_kernel[S_NSYMTYPES] = {
  31. /*
  32. * Following symbols have been audited. There values are constant and do
  33. * not change if bzImage is loaded at a different physical address than
  34. * the address for which it has been compiled. Don't warn user about
  35. * absolute relocations present w.r.t these symbols.
  36. */
  37. [S_ABS] =
  38. "^(xen_irq_disable_direct_reloc$|"
  39. "xen_save_fl_direct_reloc$|"
  40. "VDSO|"
  41. "__crc_)",
  42. /*
  43. * These symbols are known to be relative, even if the linker marks them
  44. * as absolute (typically defined outside any section in the linker script.)
  45. */
  46. [S_REL] =
  47. "^(__init_(begin|end)|"
  48. "__x86_cpu_dev_(start|end)|"
  49. "(__parainstructions|__alt_instructions)(|_end)|"
  50. "(__iommu_table|__apicdrivers|__smp_locks)(|_end)|"
  51. "__(start|end)_pci_.*|"
  52. "__(start|end)_builtin_fw|"
  53. "__(start|stop)___ksymtab(|_gpl|_unused|_unused_gpl|_gpl_future)|"
  54. "__(start|stop)___kcrctab(|_gpl|_unused|_unused_gpl|_gpl_future)|"
  55. "__(start|stop)___param|"
  56. "__(start|stop)___modver|"
  57. "__(start|stop)___bug_table|"
  58. "__tracedata_(start|end)|"
  59. "__(start|stop)_notes|"
  60. "__end_rodata|"
  61. "__initramfs_start|"
  62. "(jiffies|jiffies_64)|"
  63. #if ELF_BITS == 64
  64. "__per_cpu_load|"
  65. "init_per_cpu__.*|"
  66. "__end_rodata_hpage_align|"
  67. #endif
  68. "__vvar_page|"
  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 const char *sym_type(unsigned type)
  118. {
  119. static const char *type_name[] = {
  120. #define SYM_TYPE(X) [X] = #X
  121. SYM_TYPE(STT_NOTYPE),
  122. SYM_TYPE(STT_OBJECT),
  123. SYM_TYPE(STT_FUNC),
  124. SYM_TYPE(STT_SECTION),
  125. SYM_TYPE(STT_FILE),
  126. SYM_TYPE(STT_COMMON),
  127. SYM_TYPE(STT_TLS),
  128. #undef SYM_TYPE
  129. };
  130. const char *name = "unknown sym type name";
  131. if (type < ARRAY_SIZE(type_name)) {
  132. name = type_name[type];
  133. }
  134. return name;
  135. }
  136. static const char *sym_bind(unsigned bind)
  137. {
  138. static const char *bind_name[] = {
  139. #define SYM_BIND(X) [X] = #X
  140. SYM_BIND(STB_LOCAL),
  141. SYM_BIND(STB_GLOBAL),
  142. SYM_BIND(STB_WEAK),
  143. #undef SYM_BIND
  144. };
  145. const char *name = "unknown sym bind name";
  146. if (bind < ARRAY_SIZE(bind_name)) {
  147. name = bind_name[bind];
  148. }
  149. return name;
  150. }
  151. static const char *sym_visibility(unsigned visibility)
  152. {
  153. static const char *visibility_name[] = {
  154. #define SYM_VISIBILITY(X) [X] = #X
  155. SYM_VISIBILITY(STV_DEFAULT),
  156. SYM_VISIBILITY(STV_INTERNAL),
  157. SYM_VISIBILITY(STV_HIDDEN),
  158. SYM_VISIBILITY(STV_PROTECTED),
  159. #undef SYM_VISIBILITY
  160. };
  161. const char *name = "unknown sym visibility name";
  162. if (visibility < ARRAY_SIZE(visibility_name)) {
  163. name = visibility_name[visibility];
  164. }
  165. return name;
  166. }
  167. static const char *rel_type(unsigned type)
  168. {
  169. static const char *type_name[] = {
  170. #define REL_TYPE(X) [X] = #X
  171. #if ELF_BITS == 64
  172. REL_TYPE(R_X86_64_NONE),
  173. REL_TYPE(R_X86_64_64),
  174. REL_TYPE(R_X86_64_PC32),
  175. REL_TYPE(R_X86_64_GOT32),
  176. REL_TYPE(R_X86_64_PLT32),
  177. REL_TYPE(R_X86_64_COPY),
  178. REL_TYPE(R_X86_64_GLOB_DAT),
  179. REL_TYPE(R_X86_64_JUMP_SLOT),
  180. REL_TYPE(R_X86_64_RELATIVE),
  181. REL_TYPE(R_X86_64_GOTPCREL),
  182. REL_TYPE(R_X86_64_32),
  183. REL_TYPE(R_X86_64_32S),
  184. REL_TYPE(R_X86_64_16),
  185. REL_TYPE(R_X86_64_PC16),
  186. REL_TYPE(R_X86_64_8),
  187. REL_TYPE(R_X86_64_PC8),
  188. #else
  189. REL_TYPE(R_386_NONE),
  190. REL_TYPE(R_386_32),
  191. REL_TYPE(R_386_PC32),
  192. REL_TYPE(R_386_GOT32),
  193. REL_TYPE(R_386_PLT32),
  194. REL_TYPE(R_386_COPY),
  195. REL_TYPE(R_386_GLOB_DAT),
  196. REL_TYPE(R_386_JMP_SLOT),
  197. REL_TYPE(R_386_RELATIVE),
  198. REL_TYPE(R_386_GOTOFF),
  199. REL_TYPE(R_386_GOTPC),
  200. REL_TYPE(R_386_8),
  201. REL_TYPE(R_386_PC8),
  202. REL_TYPE(R_386_16),
  203. REL_TYPE(R_386_PC16),
  204. #endif
  205. #undef REL_TYPE
  206. };
  207. const char *name = "unknown type rel type name";
  208. if (type < ARRAY_SIZE(type_name) && type_name[type]) {
  209. name = type_name[type];
  210. }
  211. return name;
  212. }
  213. static const char *sec_name(unsigned shndx)
  214. {
  215. const char *sec_strtab;
  216. const char *name;
  217. sec_strtab = secs[ehdr.e_shstrndx].strtab;
  218. name = "<noname>";
  219. if (shndx < ehdr.e_shnum) {
  220. name = sec_strtab + secs[shndx].shdr.sh_name;
  221. }
  222. else if (shndx == SHN_ABS) {
  223. name = "ABSOLUTE";
  224. }
  225. else if (shndx == SHN_COMMON) {
  226. name = "COMMON";
  227. }
  228. return name;
  229. }
  230. static const char *sym_name(const char *sym_strtab, Elf_Sym *sym)
  231. {
  232. const char *name;
  233. name = "<noname>";
  234. if (sym->st_name) {
  235. name = sym_strtab + sym->st_name;
  236. }
  237. else {
  238. name = sec_name(sym->st_shndx);
  239. }
  240. return name;
  241. }
  242. static Elf_Sym *sym_lookup(const char *symname)
  243. {
  244. int i;
  245. for (i = 0; i < ehdr.e_shnum; i++) {
  246. struct section *sec = &secs[i];
  247. long nsyms;
  248. char *strtab;
  249. Elf_Sym *symtab;
  250. Elf_Sym *sym;
  251. if (sec->shdr.sh_type != SHT_SYMTAB)
  252. continue;
  253. nsyms = sec->shdr.sh_size/sizeof(Elf_Sym);
  254. symtab = sec->symtab;
  255. strtab = sec->link->strtab;
  256. for (sym = symtab; --nsyms >= 0; sym++) {
  257. if (!sym->st_name)
  258. continue;
  259. if (strcmp(symname, strtab + sym->st_name) == 0)
  260. return sym;
  261. }
  262. }
  263. return 0;
  264. }
  265. #if BYTE_ORDER == LITTLE_ENDIAN
  266. #define le16_to_cpu(val) (val)
  267. #define le32_to_cpu(val) (val)
  268. #define le64_to_cpu(val) (val)
  269. #endif
  270. #if BYTE_ORDER == BIG_ENDIAN
  271. #define le16_to_cpu(val) bswap_16(val)
  272. #define le32_to_cpu(val) bswap_32(val)
  273. #define le64_to_cpu(val) bswap_64(val)
  274. #endif
  275. static uint16_t elf16_to_cpu(uint16_t val)
  276. {
  277. return le16_to_cpu(val);
  278. }
  279. static uint32_t elf32_to_cpu(uint32_t val)
  280. {
  281. return le32_to_cpu(val);
  282. }
  283. #define elf_half_to_cpu(x) elf16_to_cpu(x)
  284. #define elf_word_to_cpu(x) elf32_to_cpu(x)
  285. #if ELF_BITS == 64
  286. static uint64_t elf64_to_cpu(uint64_t val)
  287. {
  288. return le64_to_cpu(val);
  289. }
  290. #define elf_addr_to_cpu(x) elf64_to_cpu(x)
  291. #define elf_off_to_cpu(x) elf64_to_cpu(x)
  292. #define elf_xword_to_cpu(x) elf64_to_cpu(x)
  293. #else
  294. #define elf_addr_to_cpu(x) elf32_to_cpu(x)
  295. #define elf_off_to_cpu(x) elf32_to_cpu(x)
  296. #define elf_xword_to_cpu(x) elf32_to_cpu(x)
  297. #endif
  298. static void read_ehdr(FILE *fp)
  299. {
  300. if (fread(&ehdr, sizeof(ehdr), 1, fp) != 1) {
  301. die("Cannot read ELF header: %s\n",
  302. strerror(errno));
  303. }
  304. if (memcmp(ehdr.e_ident, ELFMAG, SELFMAG) != 0) {
  305. die("No ELF magic\n");
  306. }
  307. if (ehdr.e_ident[EI_CLASS] != ELF_CLASS) {
  308. die("Not a %d bit executable\n", ELF_BITS);
  309. }
  310. if (ehdr.e_ident[EI_DATA] != ELFDATA2LSB) {
  311. die("Not a LSB ELF executable\n");
  312. }
  313. if (ehdr.e_ident[EI_VERSION] != EV_CURRENT) {
  314. die("Unknown ELF version\n");
  315. }
  316. /* Convert the fields to native endian */
  317. ehdr.e_type = elf_half_to_cpu(ehdr.e_type);
  318. ehdr.e_machine = elf_half_to_cpu(ehdr.e_machine);
  319. ehdr.e_version = elf_word_to_cpu(ehdr.e_version);
  320. ehdr.e_entry = elf_addr_to_cpu(ehdr.e_entry);
  321. ehdr.e_phoff = elf_off_to_cpu(ehdr.e_phoff);
  322. ehdr.e_shoff = elf_off_to_cpu(ehdr.e_shoff);
  323. ehdr.e_flags = elf_word_to_cpu(ehdr.e_flags);
  324. ehdr.e_ehsize = elf_half_to_cpu(ehdr.e_ehsize);
  325. ehdr.e_phentsize = elf_half_to_cpu(ehdr.e_phentsize);
  326. ehdr.e_phnum = elf_half_to_cpu(ehdr.e_phnum);
  327. ehdr.e_shentsize = elf_half_to_cpu(ehdr.e_shentsize);
  328. ehdr.e_shnum = elf_half_to_cpu(ehdr.e_shnum);
  329. ehdr.e_shstrndx = elf_half_to_cpu(ehdr.e_shstrndx);
  330. if ((ehdr.e_type != ET_EXEC) && (ehdr.e_type != ET_DYN)) {
  331. die("Unsupported ELF header type\n");
  332. }
  333. if (ehdr.e_machine != ELF_MACHINE) {
  334. die("Not for %s\n", ELF_MACHINE_NAME);
  335. }
  336. if (ehdr.e_version != EV_CURRENT) {
  337. die("Unknown ELF version\n");
  338. }
  339. if (ehdr.e_ehsize != sizeof(Elf_Ehdr)) {
  340. die("Bad Elf header size\n");
  341. }
  342. if (ehdr.e_phentsize != sizeof(Elf_Phdr)) {
  343. die("Bad program header entry\n");
  344. }
  345. if (ehdr.e_shentsize != sizeof(Elf_Shdr)) {
  346. die("Bad section header entry\n");
  347. }
  348. if (ehdr.e_shstrndx >= ehdr.e_shnum) {
  349. die("String table index out of bounds\n");
  350. }
  351. }
  352. static void read_shdrs(FILE *fp)
  353. {
  354. int i;
  355. Elf_Shdr shdr;
  356. secs = calloc(ehdr.e_shnum, sizeof(struct section));
  357. if (!secs) {
  358. die("Unable to allocate %d section headers\n",
  359. ehdr.e_shnum);
  360. }
  361. if (fseek(fp, ehdr.e_shoff, SEEK_SET) < 0) {
  362. die("Seek to %d failed: %s\n",
  363. ehdr.e_shoff, strerror(errno));
  364. }
  365. for (i = 0; i < ehdr.e_shnum; i++) {
  366. struct section *sec = &secs[i];
  367. if (fread(&shdr, sizeof shdr, 1, fp) != 1)
  368. die("Cannot read ELF section headers %d/%d: %s\n",
  369. i, ehdr.e_shnum, strerror(errno));
  370. sec->shdr.sh_name = elf_word_to_cpu(shdr.sh_name);
  371. sec->shdr.sh_type = elf_word_to_cpu(shdr.sh_type);
  372. sec->shdr.sh_flags = elf_xword_to_cpu(shdr.sh_flags);
  373. sec->shdr.sh_addr = elf_addr_to_cpu(shdr.sh_addr);
  374. sec->shdr.sh_offset = elf_off_to_cpu(shdr.sh_offset);
  375. sec->shdr.sh_size = elf_xword_to_cpu(shdr.sh_size);
  376. sec->shdr.sh_link = elf_word_to_cpu(shdr.sh_link);
  377. sec->shdr.sh_info = elf_word_to_cpu(shdr.sh_info);
  378. sec->shdr.sh_addralign = elf_xword_to_cpu(shdr.sh_addralign);
  379. sec->shdr.sh_entsize = elf_xword_to_cpu(shdr.sh_entsize);
  380. if (sec->shdr.sh_link < ehdr.e_shnum)
  381. sec->link = &secs[sec->shdr.sh_link];
  382. }
  383. }
  384. static void read_strtabs(FILE *fp)
  385. {
  386. int i;
  387. for (i = 0; i < ehdr.e_shnum; i++) {
  388. struct section *sec = &secs[i];
  389. if (sec->shdr.sh_type != SHT_STRTAB) {
  390. continue;
  391. }
  392. sec->strtab = malloc(sec->shdr.sh_size);
  393. if (!sec->strtab) {
  394. die("malloc of %d bytes for strtab failed\n",
  395. sec->shdr.sh_size);
  396. }
  397. if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0) {
  398. die("Seek to %d failed: %s\n",
  399. sec->shdr.sh_offset, strerror(errno));
  400. }
  401. if (fread(sec->strtab, 1, sec->shdr.sh_size, fp)
  402. != sec->shdr.sh_size) {
  403. die("Cannot read symbol table: %s\n",
  404. strerror(errno));
  405. }
  406. }
  407. }
  408. static void read_symtabs(FILE *fp)
  409. {
  410. int i,j;
  411. for (i = 0; i < ehdr.e_shnum; i++) {
  412. struct section *sec = &secs[i];
  413. if (sec->shdr.sh_type != SHT_SYMTAB) {
  414. continue;
  415. }
  416. sec->symtab = malloc(sec->shdr.sh_size);
  417. if (!sec->symtab) {
  418. die("malloc of %d bytes for symtab failed\n",
  419. sec->shdr.sh_size);
  420. }
  421. if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0) {
  422. die("Seek to %d failed: %s\n",
  423. sec->shdr.sh_offset, strerror(errno));
  424. }
  425. if (fread(sec->symtab, 1, sec->shdr.sh_size, fp)
  426. != sec->shdr.sh_size) {
  427. die("Cannot read symbol table: %s\n",
  428. strerror(errno));
  429. }
  430. for (j = 0; j < sec->shdr.sh_size/sizeof(Elf_Sym); j++) {
  431. Elf_Sym *sym = &sec->symtab[j];
  432. sym->st_name = elf_word_to_cpu(sym->st_name);
  433. sym->st_value = elf_addr_to_cpu(sym->st_value);
  434. sym->st_size = elf_xword_to_cpu(sym->st_size);
  435. sym->st_shndx = elf_half_to_cpu(sym->st_shndx);
  436. }
  437. }
  438. }
  439. static void read_relocs(FILE *fp)
  440. {
  441. int i,j;
  442. for (i = 0; i < ehdr.e_shnum; i++) {
  443. struct section *sec = &secs[i];
  444. if (sec->shdr.sh_type != SHT_REL_TYPE) {
  445. continue;
  446. }
  447. sec->reltab = malloc(sec->shdr.sh_size);
  448. if (!sec->reltab) {
  449. die("malloc of %d bytes for relocs failed\n",
  450. sec->shdr.sh_size);
  451. }
  452. if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0) {
  453. die("Seek to %d failed: %s\n",
  454. sec->shdr.sh_offset, strerror(errno));
  455. }
  456. if (fread(sec->reltab, 1, sec->shdr.sh_size, fp)
  457. != sec->shdr.sh_size) {
  458. die("Cannot read symbol table: %s\n",
  459. strerror(errno));
  460. }
  461. for (j = 0; j < sec->shdr.sh_size/sizeof(Elf_Rel); j++) {
  462. Elf_Rel *rel = &sec->reltab[j];
  463. rel->r_offset = elf_addr_to_cpu(rel->r_offset);
  464. rel->r_info = elf_xword_to_cpu(rel->r_info);
  465. #if (SHT_REL_TYPE == SHT_RELA)
  466. rel->r_addend = elf_xword_to_cpu(rel->r_addend);
  467. #endif
  468. }
  469. }
  470. }
  471. static void print_absolute_symbols(void)
  472. {
  473. int i;
  474. const char *format;
  475. if (ELF_BITS == 64)
  476. format = "%5d %016"PRIx64" %5"PRId64" %10s %10s %12s %s\n";
  477. else
  478. format = "%5d %08"PRIx32" %5"PRId32" %10s %10s %12s %s\n";
  479. printf("Absolute symbols\n");
  480. printf(" Num: Value Size Type Bind Visibility Name\n");
  481. for (i = 0; i < ehdr.e_shnum; i++) {
  482. struct section *sec = &secs[i];
  483. char *sym_strtab;
  484. int j;
  485. if (sec->shdr.sh_type != SHT_SYMTAB) {
  486. continue;
  487. }
  488. sym_strtab = sec->link->strtab;
  489. for (j = 0; j < sec->shdr.sh_size/sizeof(Elf_Sym); j++) {
  490. Elf_Sym *sym;
  491. const char *name;
  492. sym = &sec->symtab[j];
  493. name = sym_name(sym_strtab, sym);
  494. if (sym->st_shndx != SHN_ABS) {
  495. continue;
  496. }
  497. printf(format,
  498. j, sym->st_value, sym->st_size,
  499. sym_type(ELF_ST_TYPE(sym->st_info)),
  500. sym_bind(ELF_ST_BIND(sym->st_info)),
  501. sym_visibility(ELF_ST_VISIBILITY(sym->st_other)),
  502. name);
  503. }
  504. }
  505. printf("\n");
  506. }
  507. static void print_absolute_relocs(void)
  508. {
  509. int i, printed = 0;
  510. const char *format;
  511. if (ELF_BITS == 64)
  512. format = "%016"PRIx64" %016"PRIx64" %10s %016"PRIx64" %s\n";
  513. else
  514. format = "%08"PRIx32" %08"PRIx32" %10s %08"PRIx32" %s\n";
  515. for (i = 0; i < ehdr.e_shnum; i++) {
  516. struct section *sec = &secs[i];
  517. struct section *sec_applies, *sec_symtab;
  518. char *sym_strtab;
  519. Elf_Sym *sh_symtab;
  520. int j;
  521. if (sec->shdr.sh_type != SHT_REL_TYPE) {
  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(Elf_Rel); j++) {
  532. Elf_Rel *rel;
  533. Elf_Sym *sym;
  534. const char *name;
  535. rel = &sec->reltab[j];
  536. sym = &sh_symtab[ELF_R_SYM(rel->r_info)];
  537. name = sym_name(sym_strtab, sym);
  538. if (sym->st_shndx != SHN_ABS) {
  539. continue;
  540. }
  541. /* Absolute symbols are not relocated if bzImage is
  542. * loaded at a non-compiled address. Display a warning
  543. * to user at compile time about the absolute
  544. * relocations present.
  545. *
  546. * User need to audit the code to make sure
  547. * some symbols which should have been section
  548. * relative have not become absolute because of some
  549. * linker optimization or wrong programming usage.
  550. *
  551. * Before warning check if this absolute symbol
  552. * relocation is harmless.
  553. */
  554. if (is_reloc(S_ABS, name) || is_reloc(S_REL, name))
  555. continue;
  556. if (!printed) {
  557. printf("WARNING: Absolute relocations"
  558. " present\n");
  559. printf("Offset Info Type Sym.Value "
  560. "Sym.Name\n");
  561. printed = 1;
  562. }
  563. printf(format,
  564. rel->r_offset,
  565. rel->r_info,
  566. rel_type(ELF_R_TYPE(rel->r_info)),
  567. sym->st_value,
  568. name);
  569. }
  570. }
  571. if (printed)
  572. printf("\n");
  573. }
  574. static void add_reloc(struct relocs *r, uint32_t offset)
  575. {
  576. if (r->count == r->size) {
  577. unsigned long newsize = r->size + 50000;
  578. void *mem = realloc(r->offset, newsize * sizeof(r->offset[0]));
  579. if (!mem)
  580. die("realloc of %ld entries for relocs failed\n",
  581. newsize);
  582. r->offset = mem;
  583. r->size = newsize;
  584. }
  585. r->offset[r->count++] = offset;
  586. }
  587. static void walk_relocs(int (*process)(struct section *sec, Elf_Rel *rel,
  588. Elf_Sym *sym, const char *symname))
  589. {
  590. int i;
  591. /* Walk through the relocations */
  592. for (i = 0; i < ehdr.e_shnum; i++) {
  593. char *sym_strtab;
  594. Elf_Sym *sh_symtab;
  595. struct section *sec_applies, *sec_symtab;
  596. int j;
  597. struct section *sec = &secs[i];
  598. if (sec->shdr.sh_type != SHT_REL_TYPE) {
  599. continue;
  600. }
  601. sec_symtab = sec->link;
  602. sec_applies = &secs[sec->shdr.sh_info];
  603. if (!(sec_applies->shdr.sh_flags & SHF_ALLOC)) {
  604. continue;
  605. }
  606. sh_symtab = sec_symtab->symtab;
  607. sym_strtab = sec_symtab->link->strtab;
  608. for (j = 0; j < sec->shdr.sh_size/sizeof(Elf_Rel); j++) {
  609. Elf_Rel *rel = &sec->reltab[j];
  610. Elf_Sym *sym = &sh_symtab[ELF_R_SYM(rel->r_info)];
  611. const char *symname = sym_name(sym_strtab, sym);
  612. process(sec, rel, sym, symname);
  613. }
  614. }
  615. }
  616. /*
  617. * The .data..percpu section is a special case for x86_64 SMP kernels.
  618. * It is used to initialize the actual per_cpu areas and to provide
  619. * definitions for the per_cpu variables that correspond to their offsets
  620. * within the percpu area. Since the values of all of the symbols need
  621. * to be offsets from the start of the per_cpu area the virtual address
  622. * (sh_addr) of .data..percpu is 0 in SMP kernels.
  623. *
  624. * This means that:
  625. *
  626. * Relocations that reference symbols in the per_cpu area do not
  627. * need further relocation (since the value is an offset relative
  628. * to the start of the per_cpu area that does not change).
  629. *
  630. * Relocations that apply to the per_cpu area need to have their
  631. * offset adjusted by by the value of __per_cpu_load to make them
  632. * point to the correct place in the loaded image (because the
  633. * virtual address of .data..percpu is 0).
  634. *
  635. * For non SMP kernels .data..percpu is linked as part of the normal
  636. * kernel data and does not require special treatment.
  637. *
  638. */
  639. static int per_cpu_shndx = -1;
  640. static Elf_Addr per_cpu_load_addr;
  641. static void percpu_init(void)
  642. {
  643. int i;
  644. for (i = 0; i < ehdr.e_shnum; i++) {
  645. ElfW(Sym) *sym;
  646. if (strcmp(sec_name(i), ".data..percpu"))
  647. continue;
  648. if (secs[i].shdr.sh_addr != 0) /* non SMP kernel */
  649. return;
  650. sym = sym_lookup("__per_cpu_load");
  651. if (!sym)
  652. die("can't find __per_cpu_load\n");
  653. per_cpu_shndx = i;
  654. per_cpu_load_addr = sym->st_value;
  655. return;
  656. }
  657. }
  658. #if ELF_BITS == 64
  659. /*
  660. * Check to see if a symbol lies in the .data..percpu section.
  661. *
  662. * The linker incorrectly associates some symbols with the
  663. * .data..percpu section so we also need to check the symbol
  664. * name to make sure that we classify the symbol correctly.
  665. *
  666. * The GNU linker incorrectly associates:
  667. * __init_begin
  668. * __per_cpu_load
  669. *
  670. * The "gold" linker incorrectly associates:
  671. * init_per_cpu__irq_stack_union
  672. * init_per_cpu__gdt_page
  673. */
  674. static int is_percpu_sym(ElfW(Sym) *sym, const char *symname)
  675. {
  676. return (sym->st_shndx == per_cpu_shndx) &&
  677. strcmp(symname, "__init_begin") &&
  678. strcmp(symname, "__per_cpu_load") &&
  679. strncmp(symname, "init_per_cpu_", 13);
  680. }
  681. static int do_reloc64(struct section *sec, Elf_Rel *rel, ElfW(Sym) *sym,
  682. const char *symname)
  683. {
  684. unsigned r_type = ELF64_R_TYPE(rel->r_info);
  685. ElfW(Addr) offset = rel->r_offset;
  686. int shn_abs = (sym->st_shndx == SHN_ABS) && !is_reloc(S_REL, symname);
  687. if (sym->st_shndx == SHN_UNDEF)
  688. return 0;
  689. /*
  690. * Adjust the offset if this reloc applies to the percpu section.
  691. */
  692. if (sec->shdr.sh_info == per_cpu_shndx)
  693. offset += per_cpu_load_addr;
  694. switch (r_type) {
  695. case R_X86_64_NONE:
  696. /* NONE can be ignored. */
  697. break;
  698. case R_X86_64_PC32:
  699. /*
  700. * PC relative relocations don't need to be adjusted unless
  701. * referencing a percpu symbol.
  702. */
  703. if (is_percpu_sym(sym, symname))
  704. add_reloc(&relocs32neg, offset);
  705. break;
  706. case R_X86_64_32:
  707. case R_X86_64_32S:
  708. case R_X86_64_64:
  709. /*
  710. * References to the percpu area don't need to be adjusted.
  711. */
  712. if (is_percpu_sym(sym, symname))
  713. break;
  714. if (shn_abs) {
  715. /*
  716. * Whitelisted absolute symbols do not require
  717. * relocation.
  718. */
  719. if (is_reloc(S_ABS, symname))
  720. break;
  721. die("Invalid absolute %s relocation: %s\n",
  722. rel_type(r_type), symname);
  723. break;
  724. }
  725. /*
  726. * Relocation offsets for 64 bit kernels are output
  727. * as 32 bits and sign extended back to 64 bits when
  728. * the relocations are processed.
  729. * Make sure that the offset will fit.
  730. */
  731. if ((int32_t)offset != (int64_t)offset)
  732. die("Relocation offset doesn't fit in 32 bits\n");
  733. if (r_type == R_X86_64_64)
  734. add_reloc(&relocs64, offset);
  735. else
  736. add_reloc(&relocs32, offset);
  737. break;
  738. default:
  739. die("Unsupported relocation type: %s (%d)\n",
  740. rel_type(r_type), r_type);
  741. break;
  742. }
  743. return 0;
  744. }
  745. #else
  746. static int do_reloc32(struct section *sec, Elf_Rel *rel, Elf_Sym *sym,
  747. const char *symname)
  748. {
  749. unsigned r_type = ELF32_R_TYPE(rel->r_info);
  750. int shn_abs = (sym->st_shndx == SHN_ABS) && !is_reloc(S_REL, symname);
  751. switch (r_type) {
  752. case R_386_NONE:
  753. case R_386_PC32:
  754. case R_386_PC16:
  755. case R_386_PC8:
  756. /*
  757. * NONE can be ignored and PC relative relocations don't
  758. * need to be adjusted.
  759. */
  760. break;
  761. case R_386_32:
  762. if (shn_abs) {
  763. /*
  764. * Whitelisted absolute symbols do not require
  765. * relocation.
  766. */
  767. if (is_reloc(S_ABS, symname))
  768. break;
  769. die("Invalid absolute %s relocation: %s\n",
  770. rel_type(r_type), symname);
  771. break;
  772. }
  773. add_reloc(&relocs32, rel->r_offset);
  774. break;
  775. default:
  776. die("Unsupported relocation type: %s (%d)\n",
  777. rel_type(r_type), r_type);
  778. break;
  779. }
  780. return 0;
  781. }
  782. static int do_reloc_real(struct section *sec, Elf_Rel *rel, Elf_Sym *sym,
  783. const char *symname)
  784. {
  785. unsigned r_type = ELF32_R_TYPE(rel->r_info);
  786. int shn_abs = (sym->st_shndx == SHN_ABS) && !is_reloc(S_REL, symname);
  787. switch (r_type) {
  788. case R_386_NONE:
  789. case R_386_PC32:
  790. case R_386_PC16:
  791. case R_386_PC8:
  792. /*
  793. * NONE can be ignored and PC relative relocations don't
  794. * need to be adjusted.
  795. */
  796. break;
  797. case R_386_16:
  798. if (shn_abs) {
  799. /*
  800. * Whitelisted absolute symbols do not require
  801. * relocation.
  802. */
  803. if (is_reloc(S_ABS, symname))
  804. break;
  805. if (is_reloc(S_SEG, symname)) {
  806. add_reloc(&relocs16, rel->r_offset);
  807. break;
  808. }
  809. } else {
  810. if (!is_reloc(S_LIN, symname))
  811. break;
  812. }
  813. die("Invalid %s %s relocation: %s\n",
  814. shn_abs ? "absolute" : "relative",
  815. rel_type(r_type), symname);
  816. break;
  817. case R_386_32:
  818. if (shn_abs) {
  819. /*
  820. * Whitelisted absolute symbols do not require
  821. * relocation.
  822. */
  823. if (is_reloc(S_ABS, symname))
  824. break;
  825. if (is_reloc(S_REL, symname)) {
  826. add_reloc(&relocs32, rel->r_offset);
  827. break;
  828. }
  829. } else {
  830. if (is_reloc(S_LIN, symname))
  831. add_reloc(&relocs32, rel->r_offset);
  832. break;
  833. }
  834. die("Invalid %s %s relocation: %s\n",
  835. shn_abs ? "absolute" : "relative",
  836. rel_type(r_type), symname);
  837. break;
  838. default:
  839. die("Unsupported relocation type: %s (%d)\n",
  840. rel_type(r_type), r_type);
  841. break;
  842. }
  843. return 0;
  844. }
  845. #endif
  846. static int cmp_relocs(const void *va, const void *vb)
  847. {
  848. const uint32_t *a, *b;
  849. a = va; b = vb;
  850. return (*a == *b)? 0 : (*a > *b)? 1 : -1;
  851. }
  852. static void sort_relocs(struct relocs *r)
  853. {
  854. qsort(r->offset, r->count, sizeof(r->offset[0]), cmp_relocs);
  855. }
  856. static int write32(uint32_t v, FILE *f)
  857. {
  858. unsigned char buf[4];
  859. put_unaligned_le32(v, buf);
  860. return fwrite(buf, 1, 4, f) == 4 ? 0 : -1;
  861. }
  862. static int write32_as_text(uint32_t v, FILE *f)
  863. {
  864. return fprintf(f, "\t.long 0x%08"PRIx32"\n", v) > 0 ? 0 : -1;
  865. }
  866. static void emit_relocs(int as_text, int use_real_mode)
  867. {
  868. int i;
  869. int (*write_reloc)(uint32_t, FILE *) = write32;
  870. int (*do_reloc)(struct section *sec, Elf_Rel *rel, Elf_Sym *sym,
  871. const char *symname);
  872. #if ELF_BITS == 64
  873. if (!use_real_mode)
  874. do_reloc = do_reloc64;
  875. else
  876. die("--realmode not valid for a 64-bit ELF file");
  877. #else
  878. if (!use_real_mode)
  879. do_reloc = do_reloc32;
  880. else
  881. do_reloc = do_reloc_real;
  882. #endif
  883. /* Collect up the relocations */
  884. walk_relocs(do_reloc);
  885. if (relocs16.count && !use_real_mode)
  886. die("Segment relocations found but --realmode not specified\n");
  887. /* Order the relocations for more efficient processing */
  888. sort_relocs(&relocs32);
  889. #if ELF_BITS == 64
  890. sort_relocs(&relocs32neg);
  891. sort_relocs(&relocs64);
  892. #else
  893. sort_relocs(&relocs16);
  894. #endif
  895. /* Print the relocations */
  896. if (as_text) {
  897. /* Print the relocations in a form suitable that
  898. * gas will like.
  899. */
  900. printf(".section \".data.reloc\",\"a\"\n");
  901. printf(".balign 4\n");
  902. write_reloc = write32_as_text;
  903. }
  904. if (use_real_mode) {
  905. write_reloc(relocs16.count, stdout);
  906. for (i = 0; i < relocs16.count; i++)
  907. write_reloc(relocs16.offset[i], stdout);
  908. write_reloc(relocs32.count, stdout);
  909. for (i = 0; i < relocs32.count; i++)
  910. write_reloc(relocs32.offset[i], stdout);
  911. } else {
  912. #if ELF_BITS == 64
  913. /* Print a stop */
  914. write_reloc(0, stdout);
  915. /* Now print each relocation */
  916. for (i = 0; i < relocs64.count; i++)
  917. write_reloc(relocs64.offset[i], stdout);
  918. /* Print a stop */
  919. write_reloc(0, stdout);
  920. /* Now print each inverse 32-bit relocation */
  921. for (i = 0; i < relocs32neg.count; i++)
  922. write_reloc(relocs32neg.offset[i], stdout);
  923. #endif
  924. /* Print a stop */
  925. write_reloc(0, stdout);
  926. /* Now print each relocation */
  927. for (i = 0; i < relocs32.count; i++)
  928. write_reloc(relocs32.offset[i], stdout);
  929. }
  930. }
  931. /*
  932. * As an aid to debugging problems with different linkers
  933. * print summary information about the relocs.
  934. * Since different linkers tend to emit the sections in
  935. * different orders we use the section names in the output.
  936. */
  937. static int do_reloc_info(struct section *sec, Elf_Rel *rel, ElfW(Sym) *sym,
  938. const char *symname)
  939. {
  940. printf("%s\t%s\t%s\t%s\n",
  941. sec_name(sec->shdr.sh_info),
  942. rel_type(ELF_R_TYPE(rel->r_info)),
  943. symname,
  944. sec_name(sym->st_shndx));
  945. return 0;
  946. }
  947. static void print_reloc_info(void)
  948. {
  949. printf("reloc section\treloc type\tsymbol\tsymbol section\n");
  950. walk_relocs(do_reloc_info);
  951. }
  952. #if ELF_BITS == 64
  953. # define process process_64
  954. #else
  955. # define process process_32
  956. #endif
  957. void process(FILE *fp, int use_real_mode, int as_text,
  958. int show_absolute_syms, int show_absolute_relocs,
  959. int show_reloc_info)
  960. {
  961. regex_init(use_real_mode);
  962. read_ehdr(fp);
  963. read_shdrs(fp);
  964. read_strtabs(fp);
  965. read_symtabs(fp);
  966. read_relocs(fp);
  967. if (ELF_BITS == 64)
  968. percpu_init();
  969. if (show_absolute_syms) {
  970. print_absolute_symbols();
  971. return;
  972. }
  973. if (show_absolute_relocs) {
  974. print_absolute_relocs();
  975. return;
  976. }
  977. if (show_reloc_info) {
  978. print_reloc_info();
  979. return;
  980. }
  981. emit_relocs(as_text, use_real_mode);
  982. }