relocs.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105
  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. case R_X86_64_PLT32:
  700. /*
  701. * PC relative relocations don't need to be adjusted unless
  702. * referencing a percpu symbol.
  703. *
  704. * NB: R_X86_64_PLT32 can be treated as R_X86_64_PC32.
  705. */
  706. if (is_percpu_sym(sym, symname))
  707. add_reloc(&relocs32neg, offset);
  708. break;
  709. case R_X86_64_32:
  710. case R_X86_64_32S:
  711. case R_X86_64_64:
  712. /*
  713. * References to the percpu area don't need to be adjusted.
  714. */
  715. if (is_percpu_sym(sym, symname))
  716. break;
  717. if (shn_abs) {
  718. /*
  719. * Whitelisted absolute symbols do not require
  720. * relocation.
  721. */
  722. if (is_reloc(S_ABS, symname))
  723. break;
  724. die("Invalid absolute %s relocation: %s\n",
  725. rel_type(r_type), symname);
  726. break;
  727. }
  728. /*
  729. * Relocation offsets for 64 bit kernels are output
  730. * as 32 bits and sign extended back to 64 bits when
  731. * the relocations are processed.
  732. * Make sure that the offset will fit.
  733. */
  734. if ((int32_t)offset != (int64_t)offset)
  735. die("Relocation offset doesn't fit in 32 bits\n");
  736. if (r_type == R_X86_64_64)
  737. add_reloc(&relocs64, offset);
  738. else
  739. add_reloc(&relocs32, offset);
  740. break;
  741. default:
  742. die("Unsupported relocation type: %s (%d)\n",
  743. rel_type(r_type), r_type);
  744. break;
  745. }
  746. return 0;
  747. }
  748. #else
  749. static int do_reloc32(struct section *sec, Elf_Rel *rel, Elf_Sym *sym,
  750. const char *symname)
  751. {
  752. unsigned r_type = ELF32_R_TYPE(rel->r_info);
  753. int shn_abs = (sym->st_shndx == SHN_ABS) && !is_reloc(S_REL, symname);
  754. switch (r_type) {
  755. case R_386_NONE:
  756. case R_386_PC32:
  757. case R_386_PC16:
  758. case R_386_PC8:
  759. /*
  760. * NONE can be ignored and PC relative relocations don't
  761. * need to be adjusted.
  762. */
  763. break;
  764. case R_386_32:
  765. if (shn_abs) {
  766. /*
  767. * Whitelisted absolute symbols do not require
  768. * relocation.
  769. */
  770. if (is_reloc(S_ABS, symname))
  771. break;
  772. die("Invalid absolute %s relocation: %s\n",
  773. rel_type(r_type), symname);
  774. break;
  775. }
  776. add_reloc(&relocs32, rel->r_offset);
  777. break;
  778. default:
  779. die("Unsupported relocation type: %s (%d)\n",
  780. rel_type(r_type), r_type);
  781. break;
  782. }
  783. return 0;
  784. }
  785. static int do_reloc_real(struct section *sec, Elf_Rel *rel, Elf_Sym *sym,
  786. const char *symname)
  787. {
  788. unsigned r_type = ELF32_R_TYPE(rel->r_info);
  789. int shn_abs = (sym->st_shndx == SHN_ABS) && !is_reloc(S_REL, symname);
  790. switch (r_type) {
  791. case R_386_NONE:
  792. case R_386_PC32:
  793. case R_386_PC16:
  794. case R_386_PC8:
  795. /*
  796. * NONE can be ignored and PC relative relocations don't
  797. * need to be adjusted.
  798. */
  799. break;
  800. case R_386_16:
  801. if (shn_abs) {
  802. /*
  803. * Whitelisted absolute symbols do not require
  804. * relocation.
  805. */
  806. if (is_reloc(S_ABS, symname))
  807. break;
  808. if (is_reloc(S_SEG, symname)) {
  809. add_reloc(&relocs16, rel->r_offset);
  810. break;
  811. }
  812. } else {
  813. if (!is_reloc(S_LIN, symname))
  814. break;
  815. }
  816. die("Invalid %s %s relocation: %s\n",
  817. shn_abs ? "absolute" : "relative",
  818. rel_type(r_type), symname);
  819. break;
  820. case R_386_32:
  821. if (shn_abs) {
  822. /*
  823. * Whitelisted absolute symbols do not require
  824. * relocation.
  825. */
  826. if (is_reloc(S_ABS, symname))
  827. break;
  828. if (is_reloc(S_REL, symname)) {
  829. add_reloc(&relocs32, rel->r_offset);
  830. break;
  831. }
  832. } else {
  833. if (is_reloc(S_LIN, symname))
  834. add_reloc(&relocs32, rel->r_offset);
  835. break;
  836. }
  837. die("Invalid %s %s relocation: %s\n",
  838. shn_abs ? "absolute" : "relative",
  839. rel_type(r_type), symname);
  840. break;
  841. default:
  842. die("Unsupported relocation type: %s (%d)\n",
  843. rel_type(r_type), r_type);
  844. break;
  845. }
  846. return 0;
  847. }
  848. #endif
  849. static int cmp_relocs(const void *va, const void *vb)
  850. {
  851. const uint32_t *a, *b;
  852. a = va; b = vb;
  853. return (*a == *b)? 0 : (*a > *b)? 1 : -1;
  854. }
  855. static void sort_relocs(struct relocs *r)
  856. {
  857. qsort(r->offset, r->count, sizeof(r->offset[0]), cmp_relocs);
  858. }
  859. static int write32(uint32_t v, FILE *f)
  860. {
  861. unsigned char buf[4];
  862. put_unaligned_le32(v, buf);
  863. return fwrite(buf, 1, 4, f) == 4 ? 0 : -1;
  864. }
  865. static int write32_as_text(uint32_t v, FILE *f)
  866. {
  867. return fprintf(f, "\t.long 0x%08"PRIx32"\n", v) > 0 ? 0 : -1;
  868. }
  869. static void emit_relocs(int as_text, int use_real_mode)
  870. {
  871. int i;
  872. int (*write_reloc)(uint32_t, FILE *) = write32;
  873. int (*do_reloc)(struct section *sec, Elf_Rel *rel, Elf_Sym *sym,
  874. const char *symname);
  875. #if ELF_BITS == 64
  876. if (!use_real_mode)
  877. do_reloc = do_reloc64;
  878. else
  879. die("--realmode not valid for a 64-bit ELF file");
  880. #else
  881. if (!use_real_mode)
  882. do_reloc = do_reloc32;
  883. else
  884. do_reloc = do_reloc_real;
  885. #endif
  886. /* Collect up the relocations */
  887. walk_relocs(do_reloc);
  888. if (relocs16.count && !use_real_mode)
  889. die("Segment relocations found but --realmode not specified\n");
  890. /* Order the relocations for more efficient processing */
  891. sort_relocs(&relocs32);
  892. #if ELF_BITS == 64
  893. sort_relocs(&relocs32neg);
  894. sort_relocs(&relocs64);
  895. #else
  896. sort_relocs(&relocs16);
  897. #endif
  898. /* Print the relocations */
  899. if (as_text) {
  900. /* Print the relocations in a form suitable that
  901. * gas will like.
  902. */
  903. printf(".section \".data.reloc\",\"a\"\n");
  904. printf(".balign 4\n");
  905. write_reloc = write32_as_text;
  906. }
  907. if (use_real_mode) {
  908. write_reloc(relocs16.count, stdout);
  909. for (i = 0; i < relocs16.count; i++)
  910. write_reloc(relocs16.offset[i], stdout);
  911. write_reloc(relocs32.count, stdout);
  912. for (i = 0; i < relocs32.count; i++)
  913. write_reloc(relocs32.offset[i], stdout);
  914. } else {
  915. #if ELF_BITS == 64
  916. /* Print a stop */
  917. write_reloc(0, stdout);
  918. /* Now print each relocation */
  919. for (i = 0; i < relocs64.count; i++)
  920. write_reloc(relocs64.offset[i], stdout);
  921. /* Print a stop */
  922. write_reloc(0, stdout);
  923. /* Now print each inverse 32-bit relocation */
  924. for (i = 0; i < relocs32neg.count; i++)
  925. write_reloc(relocs32neg.offset[i], stdout);
  926. #endif
  927. /* Print a stop */
  928. write_reloc(0, stdout);
  929. /* Now print each relocation */
  930. for (i = 0; i < relocs32.count; i++)
  931. write_reloc(relocs32.offset[i], stdout);
  932. }
  933. }
  934. /*
  935. * As an aid to debugging problems with different linkers
  936. * print summary information about the relocs.
  937. * Since different linkers tend to emit the sections in
  938. * different orders we use the section names in the output.
  939. */
  940. static int do_reloc_info(struct section *sec, Elf_Rel *rel, ElfW(Sym) *sym,
  941. const char *symname)
  942. {
  943. printf("%s\t%s\t%s\t%s\n",
  944. sec_name(sec->shdr.sh_info),
  945. rel_type(ELF_R_TYPE(rel->r_info)),
  946. symname,
  947. sec_name(sym->st_shndx));
  948. return 0;
  949. }
  950. static void print_reloc_info(void)
  951. {
  952. printf("reloc section\treloc type\tsymbol\tsymbol section\n");
  953. walk_relocs(do_reloc_info);
  954. }
  955. #if ELF_BITS == 64
  956. # define process process_64
  957. #else
  958. # define process process_32
  959. #endif
  960. void process(FILE *fp, int use_real_mode, int as_text,
  961. int show_absolute_syms, int show_absolute_relocs,
  962. int show_reloc_info)
  963. {
  964. regex_init(use_real_mode);
  965. read_ehdr(fp);
  966. read_shdrs(fp);
  967. read_strtabs(fp);
  968. read_symtabs(fp);
  969. read_relocs(fp);
  970. if (ELF_BITS == 64)
  971. percpu_init();
  972. if (show_absolute_syms) {
  973. print_absolute_symbols();
  974. return;
  975. }
  976. if (show_absolute_relocs) {
  977. print_absolute_relocs();
  978. return;
  979. }
  980. if (show_reloc_info) {
  981. print_reloc_info();
  982. return;
  983. }
  984. emit_relocs(as_text, use_real_mode);
  985. }