relocs.c 28 KB

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