relocs.c 26 KB

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