relocs.c 26 KB

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