relocs.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* This is included from relocs_32/64.c */
  3. #define ElfW(type) _ElfW(ELF_BITS, type)
  4. #define _ElfW(bits, type) __ElfW(bits, type)
  5. #define __ElfW(bits, type) Elf##bits##_##type
  6. #define Elf_Addr ElfW(Addr)
  7. #define Elf_Ehdr ElfW(Ehdr)
  8. #define Elf_Phdr ElfW(Phdr)
  9. #define Elf_Shdr ElfW(Shdr)
  10. #define Elf_Sym ElfW(Sym)
  11. static Elf_Ehdr ehdr;
  12. struct relocs {
  13. uint32_t *offset;
  14. unsigned long count;
  15. unsigned long size;
  16. };
  17. static struct relocs relocs;
  18. struct section {
  19. Elf_Shdr shdr;
  20. struct section *link;
  21. Elf_Sym *symtab;
  22. Elf_Rel *reltab;
  23. char *strtab;
  24. long shdr_offset;
  25. };
  26. static struct section *secs;
  27. static const char * const regex_sym_kernel = {
  28. /* Symbols matching these regex's should never be relocated */
  29. "^(__crc_)",
  30. };
  31. static regex_t sym_regex_c;
  32. static int regex_skip_reloc(const char *sym_name)
  33. {
  34. return !regexec(&sym_regex_c, sym_name, 0, NULL, 0);
  35. }
  36. static void regex_init(void)
  37. {
  38. char errbuf[128];
  39. int err;
  40. err = regcomp(&sym_regex_c, regex_sym_kernel,
  41. REG_EXTENDED|REG_NOSUB);
  42. if (err) {
  43. regerror(err, &sym_regex_c, errbuf, sizeof(errbuf));
  44. die("%s", errbuf);
  45. }
  46. }
  47. static const char *rel_type(unsigned type)
  48. {
  49. static const char * const type_name[] = {
  50. #define REL_TYPE(X)[X] = #X
  51. REL_TYPE(R_MIPS_NONE),
  52. REL_TYPE(R_MIPS_16),
  53. REL_TYPE(R_MIPS_32),
  54. REL_TYPE(R_MIPS_REL32),
  55. REL_TYPE(R_MIPS_26),
  56. REL_TYPE(R_MIPS_HI16),
  57. REL_TYPE(R_MIPS_LO16),
  58. REL_TYPE(R_MIPS_GPREL16),
  59. REL_TYPE(R_MIPS_LITERAL),
  60. REL_TYPE(R_MIPS_GOT16),
  61. REL_TYPE(R_MIPS_PC16),
  62. REL_TYPE(R_MIPS_CALL16),
  63. REL_TYPE(R_MIPS_GPREL32),
  64. REL_TYPE(R_MIPS_64),
  65. REL_TYPE(R_MIPS_HIGHER),
  66. REL_TYPE(R_MIPS_HIGHEST),
  67. REL_TYPE(R_MIPS_PC21_S2),
  68. REL_TYPE(R_MIPS_PC26_S2),
  69. #undef REL_TYPE
  70. };
  71. const char *name = "unknown type rel type name";
  72. if (type < ARRAY_SIZE(type_name) && type_name[type])
  73. name = type_name[type];
  74. return name;
  75. }
  76. static const char *sec_name(unsigned shndx)
  77. {
  78. const char *sec_strtab;
  79. const char *name;
  80. sec_strtab = secs[ehdr.e_shstrndx].strtab;
  81. if (shndx < ehdr.e_shnum)
  82. name = sec_strtab + secs[shndx].shdr.sh_name;
  83. else if (shndx == SHN_ABS)
  84. name = "ABSOLUTE";
  85. else if (shndx == SHN_COMMON)
  86. name = "COMMON";
  87. else
  88. name = "<noname>";
  89. return name;
  90. }
  91. static struct section *sec_lookup(const char *secname)
  92. {
  93. int i;
  94. for (i = 0; i < ehdr.e_shnum; i++)
  95. if (strcmp(secname, sec_name(i)) == 0)
  96. return &secs[i];
  97. return NULL;
  98. }
  99. static const char *sym_name(const char *sym_strtab, Elf_Sym *sym)
  100. {
  101. const char *name;
  102. if (sym->st_name)
  103. name = sym_strtab + sym->st_name;
  104. else
  105. name = sec_name(sym->st_shndx);
  106. return name;
  107. }
  108. #if BYTE_ORDER == LITTLE_ENDIAN
  109. #define le16_to_cpu(val) (val)
  110. #define le32_to_cpu(val) (val)
  111. #define le64_to_cpu(val) (val)
  112. #define be16_to_cpu(val) bswap_16(val)
  113. #define be32_to_cpu(val) bswap_32(val)
  114. #define be64_to_cpu(val) bswap_64(val)
  115. #define cpu_to_le16(val) (val)
  116. #define cpu_to_le32(val) (val)
  117. #define cpu_to_le64(val) (val)
  118. #define cpu_to_be16(val) bswap_16(val)
  119. #define cpu_to_be32(val) bswap_32(val)
  120. #define cpu_to_be64(val) bswap_64(val)
  121. #endif
  122. #if BYTE_ORDER == BIG_ENDIAN
  123. #define le16_to_cpu(val) bswap_16(val)
  124. #define le32_to_cpu(val) bswap_32(val)
  125. #define le64_to_cpu(val) bswap_64(val)
  126. #define be16_to_cpu(val) (val)
  127. #define be32_to_cpu(val) (val)
  128. #define be64_to_cpu(val) (val)
  129. #define cpu_to_le16(val) bswap_16(val)
  130. #define cpu_to_le32(val) bswap_32(val)
  131. #define cpu_to_le64(val) bswap_64(val)
  132. #define cpu_to_be16(val) (val)
  133. #define cpu_to_be32(val) (val)
  134. #define cpu_to_be64(val) (val)
  135. #endif
  136. static uint16_t elf16_to_cpu(uint16_t val)
  137. {
  138. if (ehdr.e_ident[EI_DATA] == ELFDATA2LSB)
  139. return le16_to_cpu(val);
  140. else
  141. return be16_to_cpu(val);
  142. }
  143. static uint32_t elf32_to_cpu(uint32_t val)
  144. {
  145. if (ehdr.e_ident[EI_DATA] == ELFDATA2LSB)
  146. return le32_to_cpu(val);
  147. else
  148. return be32_to_cpu(val);
  149. }
  150. static uint32_t cpu_to_elf32(uint32_t val)
  151. {
  152. if (ehdr.e_ident[EI_DATA] == ELFDATA2LSB)
  153. return cpu_to_le32(val);
  154. else
  155. return cpu_to_be32(val);
  156. }
  157. #define elf_half_to_cpu(x) elf16_to_cpu(x)
  158. #define elf_word_to_cpu(x) elf32_to_cpu(x)
  159. #if ELF_BITS == 64
  160. static uint64_t elf64_to_cpu(uint64_t val)
  161. {
  162. if (ehdr.e_ident[EI_DATA] == ELFDATA2LSB)
  163. return le64_to_cpu(val);
  164. else
  165. return be64_to_cpu(val);
  166. }
  167. #define elf_addr_to_cpu(x) elf64_to_cpu(x)
  168. #define elf_off_to_cpu(x) elf64_to_cpu(x)
  169. #define elf_xword_to_cpu(x) elf64_to_cpu(x)
  170. #else
  171. #define elf_addr_to_cpu(x) elf32_to_cpu(x)
  172. #define elf_off_to_cpu(x) elf32_to_cpu(x)
  173. #define elf_xword_to_cpu(x) elf32_to_cpu(x)
  174. #endif
  175. static void read_ehdr(FILE *fp)
  176. {
  177. if (fread(&ehdr, sizeof(ehdr), 1, fp) != 1)
  178. die("Cannot read ELF header: %s\n", strerror(errno));
  179. if (memcmp(ehdr.e_ident, ELFMAG, SELFMAG) != 0)
  180. die("No ELF magic\n");
  181. if (ehdr.e_ident[EI_CLASS] != ELF_CLASS)
  182. die("Not a %d bit executable\n", ELF_BITS);
  183. if ((ehdr.e_ident[EI_DATA] != ELFDATA2LSB) &&
  184. (ehdr.e_ident[EI_DATA] != ELFDATA2MSB))
  185. die("Unknown ELF Endianness\n");
  186. if (ehdr.e_ident[EI_VERSION] != EV_CURRENT)
  187. die("Unknown ELF version\n");
  188. /* Convert the fields to native endian */
  189. ehdr.e_type = elf_half_to_cpu(ehdr.e_type);
  190. ehdr.e_machine = elf_half_to_cpu(ehdr.e_machine);
  191. ehdr.e_version = elf_word_to_cpu(ehdr.e_version);
  192. ehdr.e_entry = elf_addr_to_cpu(ehdr.e_entry);
  193. ehdr.e_phoff = elf_off_to_cpu(ehdr.e_phoff);
  194. ehdr.e_shoff = elf_off_to_cpu(ehdr.e_shoff);
  195. ehdr.e_flags = elf_word_to_cpu(ehdr.e_flags);
  196. ehdr.e_ehsize = elf_half_to_cpu(ehdr.e_ehsize);
  197. ehdr.e_phentsize = elf_half_to_cpu(ehdr.e_phentsize);
  198. ehdr.e_phnum = elf_half_to_cpu(ehdr.e_phnum);
  199. ehdr.e_shentsize = elf_half_to_cpu(ehdr.e_shentsize);
  200. ehdr.e_shnum = elf_half_to_cpu(ehdr.e_shnum);
  201. ehdr.e_shstrndx = elf_half_to_cpu(ehdr.e_shstrndx);
  202. if ((ehdr.e_type != ET_EXEC) && (ehdr.e_type != ET_DYN))
  203. die("Unsupported ELF header type\n");
  204. if (ehdr.e_machine != ELF_MACHINE)
  205. die("Not for %s\n", ELF_MACHINE_NAME);
  206. if (ehdr.e_version != EV_CURRENT)
  207. die("Unknown ELF version\n");
  208. if (ehdr.e_ehsize != sizeof(Elf_Ehdr))
  209. die("Bad Elf header size\n");
  210. if (ehdr.e_phentsize != sizeof(Elf_Phdr))
  211. die("Bad program header entry\n");
  212. if (ehdr.e_shentsize != sizeof(Elf_Shdr))
  213. die("Bad section header entry\n");
  214. if (ehdr.e_shstrndx >= ehdr.e_shnum)
  215. die("String table index out of bounds\n");
  216. }
  217. static void read_shdrs(FILE *fp)
  218. {
  219. int i;
  220. Elf_Shdr shdr;
  221. secs = calloc(ehdr.e_shnum, sizeof(struct section));
  222. if (!secs)
  223. die("Unable to allocate %d section headers\n", ehdr.e_shnum);
  224. if (fseek(fp, ehdr.e_shoff, SEEK_SET) < 0)
  225. die("Seek to %d failed: %s\n", ehdr.e_shoff, strerror(errno));
  226. for (i = 0; i < ehdr.e_shnum; i++) {
  227. struct section *sec = &secs[i];
  228. sec->shdr_offset = ftell(fp);
  229. if (fread(&shdr, sizeof(shdr), 1, fp) != 1)
  230. die("Cannot read ELF section headers %d/%d: %s\n",
  231. i, ehdr.e_shnum, strerror(errno));
  232. sec->shdr.sh_name = elf_word_to_cpu(shdr.sh_name);
  233. sec->shdr.sh_type = elf_word_to_cpu(shdr.sh_type);
  234. sec->shdr.sh_flags = elf_xword_to_cpu(shdr.sh_flags);
  235. sec->shdr.sh_addr = elf_addr_to_cpu(shdr.sh_addr);
  236. sec->shdr.sh_offset = elf_off_to_cpu(shdr.sh_offset);
  237. sec->shdr.sh_size = elf_xword_to_cpu(shdr.sh_size);
  238. sec->shdr.sh_link = elf_word_to_cpu(shdr.sh_link);
  239. sec->shdr.sh_info = elf_word_to_cpu(shdr.sh_info);
  240. sec->shdr.sh_addralign = elf_xword_to_cpu(shdr.sh_addralign);
  241. sec->shdr.sh_entsize = elf_xword_to_cpu(shdr.sh_entsize);
  242. if (sec->shdr.sh_link < ehdr.e_shnum)
  243. sec->link = &secs[sec->shdr.sh_link];
  244. }
  245. }
  246. static void read_strtabs(FILE *fp)
  247. {
  248. int i;
  249. for (i = 0; i < ehdr.e_shnum; i++) {
  250. struct section *sec = &secs[i];
  251. if (sec->shdr.sh_type != SHT_STRTAB)
  252. continue;
  253. sec->strtab = malloc(sec->shdr.sh_size);
  254. if (!sec->strtab)
  255. die("malloc of %d bytes for strtab failed\n",
  256. sec->shdr.sh_size);
  257. if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0)
  258. die("Seek to %d failed: %s\n",
  259. sec->shdr.sh_offset, strerror(errno));
  260. if (fread(sec->strtab, 1, sec->shdr.sh_size, fp) !=
  261. sec->shdr.sh_size)
  262. die("Cannot read symbol table: %s\n", strerror(errno));
  263. }
  264. }
  265. static void read_symtabs(FILE *fp)
  266. {
  267. int i, j;
  268. for (i = 0; i < ehdr.e_shnum; i++) {
  269. struct section *sec = &secs[i];
  270. if (sec->shdr.sh_type != SHT_SYMTAB)
  271. continue;
  272. sec->symtab = malloc(sec->shdr.sh_size);
  273. if (!sec->symtab)
  274. die("malloc of %d bytes for symtab failed\n",
  275. sec->shdr.sh_size);
  276. if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0)
  277. die("Seek to %d failed: %s\n",
  278. sec->shdr.sh_offset, strerror(errno));
  279. if (fread(sec->symtab, 1, sec->shdr.sh_size, fp) !=
  280. sec->shdr.sh_size)
  281. die("Cannot read symbol table: %s\n", strerror(errno));
  282. for (j = 0; j < sec->shdr.sh_size/sizeof(Elf_Sym); j++) {
  283. Elf_Sym *sym = &sec->symtab[j];
  284. sym->st_name = elf_word_to_cpu(sym->st_name);
  285. sym->st_value = elf_addr_to_cpu(sym->st_value);
  286. sym->st_size = elf_xword_to_cpu(sym->st_size);
  287. sym->st_shndx = elf_half_to_cpu(sym->st_shndx);
  288. }
  289. }
  290. }
  291. static void read_relocs(FILE *fp)
  292. {
  293. static unsigned long base = 0;
  294. int i, j;
  295. if (!base) {
  296. struct section *sec = sec_lookup(".text");
  297. if (!sec)
  298. die("Could not find .text section\n");
  299. base = sec->shdr.sh_addr;
  300. }
  301. for (i = 0; i < ehdr.e_shnum; i++) {
  302. struct section *sec = &secs[i];
  303. if (sec->shdr.sh_type != SHT_REL_TYPE)
  304. continue;
  305. sec->reltab = malloc(sec->shdr.sh_size);
  306. if (!sec->reltab)
  307. die("malloc of %d bytes for relocs failed\n",
  308. sec->shdr.sh_size);
  309. if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0)
  310. die("Seek to %d failed: %s\n",
  311. sec->shdr.sh_offset, strerror(errno));
  312. if (fread(sec->reltab, 1, sec->shdr.sh_size, fp) !=
  313. sec->shdr.sh_size)
  314. die("Cannot read symbol table: %s\n", strerror(errno));
  315. for (j = 0; j < sec->shdr.sh_size/sizeof(Elf_Rel); j++) {
  316. Elf_Rel *rel = &sec->reltab[j];
  317. rel->r_offset = elf_addr_to_cpu(rel->r_offset);
  318. /* Set offset into kernel image */
  319. rel->r_offset -= base;
  320. #if (ELF_BITS == 32)
  321. rel->r_info = elf_xword_to_cpu(rel->r_info);
  322. #else
  323. /* Convert MIPS64 RELA format - only the symbol
  324. * index needs converting to native endianness
  325. */
  326. rel->r_info = rel->r_info;
  327. ELF_R_SYM(rel->r_info) = elf32_to_cpu(ELF_R_SYM(rel->r_info));
  328. #endif
  329. #if (SHT_REL_TYPE == SHT_RELA)
  330. rel->r_addend = elf_xword_to_cpu(rel->r_addend);
  331. #endif
  332. }
  333. }
  334. }
  335. static void remove_relocs(FILE *fp)
  336. {
  337. int i;
  338. Elf_Shdr shdr;
  339. for (i = 0; i < ehdr.e_shnum; i++) {
  340. struct section *sec = &secs[i];
  341. if (sec->shdr.sh_type != SHT_REL_TYPE)
  342. continue;
  343. if (fseek(fp, sec->shdr_offset, SEEK_SET) < 0)
  344. die("Seek to %d failed: %s\n",
  345. sec->shdr_offset, strerror(errno));
  346. if (fread(&shdr, sizeof(shdr), 1, fp) != 1)
  347. die("Cannot read ELF section headers %d/%d: %s\n",
  348. i, ehdr.e_shnum, strerror(errno));
  349. /* Set relocation section size to 0, effectively removing it.
  350. * This is necessary due to lack of support for relocations
  351. * in objcopy when creating 32bit elf from 64bit elf.
  352. */
  353. shdr.sh_size = 0;
  354. if (fseek(fp, sec->shdr_offset, SEEK_SET) < 0)
  355. die("Seek to %d failed: %s\n",
  356. sec->shdr_offset, strerror(errno));
  357. if (fwrite(&shdr, sizeof(shdr), 1, fp) != 1)
  358. die("Cannot write ELF section headers %d/%d: %s\n",
  359. i, ehdr.e_shnum, strerror(errno));
  360. }
  361. }
  362. static void add_reloc(struct relocs *r, uint32_t offset, unsigned type)
  363. {
  364. /* Relocation representation in binary table:
  365. * |76543210|76543210|76543210|76543210|
  366. * | Type | offset from _text >> 2 |
  367. */
  368. offset >>= 2;
  369. if (offset > 0x00FFFFFF)
  370. die("Kernel image exceeds maximum size for relocation!\n");
  371. offset = (offset & 0x00FFFFFF) | ((type & 0xFF) << 24);
  372. if (r->count == r->size) {
  373. unsigned long newsize = r->size + 50000;
  374. void *mem = realloc(r->offset, newsize * sizeof(r->offset[0]));
  375. if (!mem)
  376. die("realloc failed\n");
  377. r->offset = mem;
  378. r->size = newsize;
  379. }
  380. r->offset[r->count++] = offset;
  381. }
  382. static void walk_relocs(int (*process)(struct section *sec, Elf_Rel *rel,
  383. Elf_Sym *sym, const char *symname))
  384. {
  385. int i;
  386. /* Walk through the relocations */
  387. for (i = 0; i < ehdr.e_shnum; i++) {
  388. char *sym_strtab;
  389. Elf_Sym *sh_symtab;
  390. struct section *sec_applies, *sec_symtab;
  391. int j;
  392. struct section *sec = &secs[i];
  393. if (sec->shdr.sh_type != SHT_REL_TYPE)
  394. continue;
  395. sec_symtab = sec->link;
  396. sec_applies = &secs[sec->shdr.sh_info];
  397. if (!(sec_applies->shdr.sh_flags & SHF_ALLOC))
  398. continue;
  399. sh_symtab = sec_symtab->symtab;
  400. sym_strtab = sec_symtab->link->strtab;
  401. for (j = 0; j < sec->shdr.sh_size/sizeof(Elf_Rel); j++) {
  402. Elf_Rel *rel = &sec->reltab[j];
  403. Elf_Sym *sym = &sh_symtab[ELF_R_SYM(rel->r_info)];
  404. const char *symname = sym_name(sym_strtab, sym);
  405. process(sec, rel, sym, symname);
  406. }
  407. }
  408. }
  409. static int do_reloc(struct section *sec, Elf_Rel *rel, Elf_Sym *sym,
  410. const char *symname)
  411. {
  412. unsigned r_type = ELF_R_TYPE(rel->r_info);
  413. unsigned bind = ELF_ST_BIND(sym->st_info);
  414. if ((bind == STB_WEAK) && (sym->st_value == 0)) {
  415. /* Don't relocate weak symbols without a target */
  416. return 0;
  417. }
  418. if (regex_skip_reloc(symname))
  419. return 0;
  420. switch (r_type) {
  421. case R_MIPS_NONE:
  422. case R_MIPS_REL32:
  423. case R_MIPS_PC16:
  424. case R_MIPS_PC21_S2:
  425. case R_MIPS_PC26_S2:
  426. /*
  427. * NONE can be ignored and PC relative relocations don't
  428. * need to be adjusted.
  429. */
  430. case R_MIPS_HIGHEST:
  431. case R_MIPS_HIGHER:
  432. /* We support relocating within the same 4Gb segment only,
  433. * thus leaving the top 32bits unchanged
  434. */
  435. case R_MIPS_LO16:
  436. /* We support relocating by 64k jumps only
  437. * thus leaving the bottom 16bits unchanged
  438. */
  439. break;
  440. case R_MIPS_64:
  441. case R_MIPS_32:
  442. case R_MIPS_26:
  443. case R_MIPS_HI16:
  444. add_reloc(&relocs, rel->r_offset, r_type);
  445. break;
  446. default:
  447. die("Unsupported relocation type: %s (%d)\n",
  448. rel_type(r_type), r_type);
  449. break;
  450. }
  451. return 0;
  452. }
  453. static int write_reloc_as_bin(uint32_t v, FILE *f)
  454. {
  455. unsigned char buf[4];
  456. v = cpu_to_elf32(v);
  457. memcpy(buf, &v, sizeof(uint32_t));
  458. return fwrite(buf, 1, 4, f);
  459. }
  460. static int write_reloc_as_text(uint32_t v, FILE *f)
  461. {
  462. int res;
  463. res = fprintf(f, "\t.long 0x%08"PRIx32"\n", v);
  464. if (res < 0)
  465. return res;
  466. else
  467. return sizeof(uint32_t);
  468. }
  469. static void emit_relocs(int as_text, int as_bin, FILE *outf)
  470. {
  471. int i;
  472. int (*write_reloc)(uint32_t, FILE *) = write_reloc_as_bin;
  473. int size = 0;
  474. int size_reserved;
  475. struct section *sec_reloc;
  476. sec_reloc = sec_lookup(".data.reloc");
  477. if (!sec_reloc)
  478. die("Could not find relocation section\n");
  479. size_reserved = sec_reloc->shdr.sh_size;
  480. /* Collect up the relocations */
  481. walk_relocs(do_reloc);
  482. /* Print the relocations */
  483. if (as_text) {
  484. /* Print the relocations in a form suitable that
  485. * gas will like.
  486. */
  487. printf(".section \".data.reloc\",\"a\"\n");
  488. printf(".balign 4\n");
  489. /* Output text to stdout */
  490. write_reloc = write_reloc_as_text;
  491. outf = stdout;
  492. } else if (as_bin) {
  493. /* Output raw binary to stdout */
  494. outf = stdout;
  495. } else {
  496. /* Seek to offset of the relocation section.
  497. * Each relocation is then written into the
  498. * vmlinux kernel image.
  499. */
  500. if (fseek(outf, sec_reloc->shdr.sh_offset, SEEK_SET) < 0) {
  501. die("Seek to %d failed: %s\n",
  502. sec_reloc->shdr.sh_offset, strerror(errno));
  503. }
  504. }
  505. for (i = 0; i < relocs.count; i++)
  506. size += write_reloc(relocs.offset[i], outf);
  507. /* Print a stop, but only if we've actually written some relocs */
  508. if (size)
  509. size += write_reloc(0, outf);
  510. if (size > size_reserved)
  511. /* Die, but suggest a value for CONFIG_RELOCATION_TABLE_SIZE
  512. * which will fix this problem and allow a bit of headroom
  513. * if more kernel features are enabled
  514. */
  515. die("Relocations overflow available space!\n" \
  516. "Please adjust CONFIG_RELOCATION_TABLE_SIZE " \
  517. "to at least 0x%08x\n", (size + 0x1000) & ~0xFFF);
  518. }
  519. /*
  520. * As an aid to debugging problems with different linkers
  521. * print summary information about the relocs.
  522. * Since different linkers tend to emit the sections in
  523. * different orders we use the section names in the output.
  524. */
  525. static int do_reloc_info(struct section *sec, Elf_Rel *rel, ElfW(Sym) *sym,
  526. const char *symname)
  527. {
  528. printf("%16s 0x%08x %16s %40s %16s\n",
  529. sec_name(sec->shdr.sh_info),
  530. (unsigned int)rel->r_offset,
  531. rel_type(ELF_R_TYPE(rel->r_info)),
  532. symname,
  533. sec_name(sym->st_shndx));
  534. return 0;
  535. }
  536. static void print_reloc_info(void)
  537. {
  538. printf("%16s %10s %16s %40s %16s\n",
  539. "reloc section",
  540. "offset",
  541. "reloc type",
  542. "symbol",
  543. "symbol section");
  544. walk_relocs(do_reloc_info);
  545. }
  546. #if ELF_BITS == 64
  547. # define process process_64
  548. #else
  549. # define process process_32
  550. #endif
  551. void process(FILE *fp, int as_text, int as_bin,
  552. int show_reloc_info, int keep_relocs)
  553. {
  554. regex_init();
  555. read_ehdr(fp);
  556. read_shdrs(fp);
  557. read_strtabs(fp);
  558. read_symtabs(fp);
  559. read_relocs(fp);
  560. if (show_reloc_info) {
  561. print_reloc_info();
  562. return;
  563. }
  564. emit_relocs(as_text, as_bin, fp);
  565. if (!keep_relocs)
  566. remove_relocs(fp);
  567. }