relocs.c 25 KB

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