relocs.c 28 KB

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