relocs.c 26 KB

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