relocs.c 17 KB

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