relocs.c 27 KB

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