1 /* 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
7 #define Elf_Addr ElfW(Addr)
8 #define Elf_Ehdr ElfW(Ehdr)
9 #define Elf_Phdr ElfW(Phdr)
10 #define Elf_Shdr ElfW(Shdr)
11 #define Elf_Sym ElfW(Sym)
21 static struct relocs relocs;
31 static struct section *secs;
33 static const char * const regex_sym_kernel = {
34 /* Symbols matching these regex's should never be relocated */
38 static regex_t sym_regex_c;
40 static int regex_skip_reloc(const char *sym_name)
42 return !regexec(&sym_regex_c, sym_name, 0, NULL, 0);
45 static void regex_init(void)
50 err = regcomp(&sym_regex_c, regex_sym_kernel,
51 REG_EXTENDED|REG_NOSUB);
54 regerror(err, &sym_regex_c, errbuf, sizeof(errbuf));
59 static const char *rel_type(unsigned type)
61 static const char * const type_name[] = {
62 #define REL_TYPE(X)[X] = #X
63 REL_TYPE(R_MIPS_NONE),
66 REL_TYPE(R_MIPS_REL32),
68 REL_TYPE(R_MIPS_HI16),
69 REL_TYPE(R_MIPS_LO16),
70 REL_TYPE(R_MIPS_GPREL16),
71 REL_TYPE(R_MIPS_LITERAL),
72 REL_TYPE(R_MIPS_GOT16),
73 REL_TYPE(R_MIPS_PC16),
74 REL_TYPE(R_MIPS_CALL16),
75 REL_TYPE(R_MIPS_GPREL32),
77 REL_TYPE(R_MIPS_HIGHER),
78 REL_TYPE(R_MIPS_HIGHEST),
79 REL_TYPE(R_MIPS_PC21_S2),
80 REL_TYPE(R_MIPS_PC26_S2),
83 const char *name = "unknown type rel type name";
85 if (type < ARRAY_SIZE(type_name) && type_name[type])
86 name = type_name[type];
90 static const char *sec_name(unsigned shndx)
92 const char *sec_strtab;
95 sec_strtab = secs[ehdr.e_shstrndx].strtab;
96 if (shndx < ehdr.e_shnum)
97 name = sec_strtab + secs[shndx].shdr.sh_name;
98 else if (shndx == SHN_ABS)
100 else if (shndx == SHN_COMMON)
107 static struct section *sec_lookup(const char *secname)
111 for (i = 0; i < ehdr.e_shnum; i++)
112 if (strcmp(secname, sec_name(i)) == 0)
118 static const char *sym_name(const char *sym_strtab, Elf_Sym *sym)
123 name = sym_strtab + sym->st_name;
125 name = sec_name(sym->st_shndx);
129 #if BYTE_ORDER == LITTLE_ENDIAN
130 #define le16_to_cpu(val) (val)
131 #define le32_to_cpu(val) (val)
132 #define le64_to_cpu(val) (val)
133 #define be16_to_cpu(val) bswap_16(val)
134 #define be32_to_cpu(val) bswap_32(val)
135 #define be64_to_cpu(val) bswap_64(val)
137 #define cpu_to_le16(val) (val)
138 #define cpu_to_le32(val) (val)
139 #define cpu_to_le64(val) (val)
140 #define cpu_to_be16(val) bswap_16(val)
141 #define cpu_to_be32(val) bswap_32(val)
142 #define cpu_to_be64(val) bswap_64(val)
144 #if BYTE_ORDER == BIG_ENDIAN
145 #define le16_to_cpu(val) bswap_16(val)
146 #define le32_to_cpu(val) bswap_32(val)
147 #define le64_to_cpu(val) bswap_64(val)
148 #define be16_to_cpu(val) (val)
149 #define be32_to_cpu(val) (val)
150 #define be64_to_cpu(val) (val)
152 #define cpu_to_le16(val) bswap_16(val)
153 #define cpu_to_le32(val) bswap_32(val)
154 #define cpu_to_le64(val) bswap_64(val)
155 #define cpu_to_be16(val) (val)
156 #define cpu_to_be32(val) (val)
157 #define cpu_to_be64(val) (val)
160 static uint16_t elf16_to_cpu(uint16_t val)
162 if (ehdr.e_ident[EI_DATA] == ELFDATA2LSB)
163 return le16_to_cpu(val);
165 return be16_to_cpu(val);
168 static uint32_t elf32_to_cpu(uint32_t val)
170 if (ehdr.e_ident[EI_DATA] == ELFDATA2LSB)
171 return le32_to_cpu(val);
173 return be32_to_cpu(val);
176 static uint32_t cpu_to_elf32(uint32_t val)
178 if (ehdr.e_ident[EI_DATA] == ELFDATA2LSB)
179 return cpu_to_le32(val);
181 return cpu_to_be32(val);
184 #define elf_half_to_cpu(x) elf16_to_cpu(x)
185 #define elf_word_to_cpu(x) elf32_to_cpu(x)
188 static uint64_t elf64_to_cpu(uint64_t val)
190 if (ehdr.e_ident[EI_DATA] == ELFDATA2LSB)
191 return le64_to_cpu(val);
193 return be64_to_cpu(val);
195 #define elf_addr_to_cpu(x) elf64_to_cpu(x)
196 #define elf_off_to_cpu(x) elf64_to_cpu(x)
197 #define elf_xword_to_cpu(x) elf64_to_cpu(x)
199 #define elf_addr_to_cpu(x) elf32_to_cpu(x)
200 #define elf_off_to_cpu(x) elf32_to_cpu(x)
201 #define elf_xword_to_cpu(x) elf32_to_cpu(x)
204 static void read_ehdr(FILE *fp)
206 if (fread(&ehdr, sizeof(ehdr), 1, fp) != 1)
207 die("Cannot read ELF header: %s\n", strerror(errno));
209 if (memcmp(ehdr.e_ident, ELFMAG, SELFMAG) != 0)
210 die("No ELF magic\n");
212 if (ehdr.e_ident[EI_CLASS] != ELF_CLASS)
213 die("Not a %d bit executable\n", ELF_BITS);
215 if ((ehdr.e_ident[EI_DATA] != ELFDATA2LSB) &&
216 (ehdr.e_ident[EI_DATA] != ELFDATA2MSB))
217 die("Unknown ELF Endianness\n");
219 if (ehdr.e_ident[EI_VERSION] != EV_CURRENT)
220 die("Unknown ELF version\n");
222 /* Convert the fields to native endian */
223 ehdr.e_type = elf_half_to_cpu(ehdr.e_type);
224 ehdr.e_machine = elf_half_to_cpu(ehdr.e_machine);
225 ehdr.e_version = elf_word_to_cpu(ehdr.e_version);
226 ehdr.e_entry = elf_addr_to_cpu(ehdr.e_entry);
227 ehdr.e_phoff = elf_off_to_cpu(ehdr.e_phoff);
228 ehdr.e_shoff = elf_off_to_cpu(ehdr.e_shoff);
229 ehdr.e_flags = elf_word_to_cpu(ehdr.e_flags);
230 ehdr.e_ehsize = elf_half_to_cpu(ehdr.e_ehsize);
231 ehdr.e_phentsize = elf_half_to_cpu(ehdr.e_phentsize);
232 ehdr.e_phnum = elf_half_to_cpu(ehdr.e_phnum);
233 ehdr.e_shentsize = elf_half_to_cpu(ehdr.e_shentsize);
234 ehdr.e_shnum = elf_half_to_cpu(ehdr.e_shnum);
235 ehdr.e_shstrndx = elf_half_to_cpu(ehdr.e_shstrndx);
237 if ((ehdr.e_type != ET_EXEC) && (ehdr.e_type != ET_DYN))
238 die("Unsupported ELF header type\n");
240 if (ehdr.e_machine != ELF_MACHINE)
241 die("Not for %s\n", ELF_MACHINE_NAME);
243 if (ehdr.e_version != EV_CURRENT)
244 die("Unknown ELF version\n");
246 if (ehdr.e_ehsize != sizeof(Elf_Ehdr))
247 die("Bad Elf header size\n");
249 if (ehdr.e_phentsize != sizeof(Elf_Phdr))
250 die("Bad program header entry\n");
252 if (ehdr.e_shentsize != sizeof(Elf_Shdr))
253 die("Bad section header entry\n");
255 if (ehdr.e_shstrndx >= ehdr.e_shnum)
256 die("String table index out of bounds\n");
259 static void read_shdrs(FILE *fp)
264 secs = calloc(ehdr.e_shnum, sizeof(struct section));
266 die("Unable to allocate %d section headers\n", ehdr.e_shnum);
268 if (fseek(fp, ehdr.e_shoff, SEEK_SET) < 0)
269 die("Seek to %d failed: %s\n", ehdr.e_shoff, strerror(errno));
271 for (i = 0; i < ehdr.e_shnum; i++) {
272 struct section *sec = &secs[i];
274 sec->shdr_offset = ftell(fp);
275 if (fread(&shdr, sizeof(shdr), 1, fp) != 1)
276 die("Cannot read ELF section headers %d/%d: %s\n",
277 i, ehdr.e_shnum, strerror(errno));
278 sec->shdr.sh_name = elf_word_to_cpu(shdr.sh_name);
279 sec->shdr.sh_type = elf_word_to_cpu(shdr.sh_type);
280 sec->shdr.sh_flags = elf_xword_to_cpu(shdr.sh_flags);
281 sec->shdr.sh_addr = elf_addr_to_cpu(shdr.sh_addr);
282 sec->shdr.sh_offset = elf_off_to_cpu(shdr.sh_offset);
283 sec->shdr.sh_size = elf_xword_to_cpu(shdr.sh_size);
284 sec->shdr.sh_link = elf_word_to_cpu(shdr.sh_link);
285 sec->shdr.sh_info = elf_word_to_cpu(shdr.sh_info);
286 sec->shdr.sh_addralign = elf_xword_to_cpu(shdr.sh_addralign);
287 sec->shdr.sh_entsize = elf_xword_to_cpu(shdr.sh_entsize);
288 if (sec->shdr.sh_link < ehdr.e_shnum)
289 sec->link = &secs[sec->shdr.sh_link];
293 static void read_strtabs(FILE *fp)
297 for (i = 0; i < ehdr.e_shnum; i++) {
298 struct section *sec = &secs[i];
300 if (sec->shdr.sh_type != SHT_STRTAB)
303 sec->strtab = malloc(sec->shdr.sh_size);
305 die("malloc of %d bytes for strtab failed\n",
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));
312 if (fread(sec->strtab, 1, sec->shdr.sh_size, fp) !=
314 die("Cannot read symbol table: %s\n", strerror(errno));
318 static void read_symtabs(FILE *fp)
322 for (i = 0; i < ehdr.e_shnum; i++) {
323 struct section *sec = &secs[i];
324 if (sec->shdr.sh_type != SHT_SYMTAB)
327 sec->symtab = malloc(sec->shdr.sh_size);
329 die("malloc of %d bytes for symtab failed\n",
332 if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0)
333 die("Seek to %d failed: %s\n",
334 sec->shdr.sh_offset, strerror(errno));
336 if (fread(sec->symtab, 1, sec->shdr.sh_size, fp) !=
338 die("Cannot read symbol table: %s\n", strerror(errno));
340 for (j = 0; j < sec->shdr.sh_size/sizeof(Elf_Sym); j++) {
341 Elf_Sym *sym = &sec->symtab[j];
343 sym->st_name = elf_word_to_cpu(sym->st_name);
344 sym->st_value = elf_addr_to_cpu(sym->st_value);
345 sym->st_size = elf_xword_to_cpu(sym->st_size);
346 sym->st_shndx = elf_half_to_cpu(sym->st_shndx);
351 static void read_relocs(FILE *fp)
353 static unsigned long base = 0;
357 struct section *sec = sec_lookup(".text");
360 die("Could not find .text section\n");
362 base = sec->shdr.sh_addr;
365 for (i = 0; i < ehdr.e_shnum; i++) {
366 struct section *sec = &secs[i];
368 if (sec->shdr.sh_type != SHT_REL_TYPE)
371 sec->reltab = malloc(sec->shdr.sh_size);
373 die("malloc of %d bytes for relocs failed\n",
376 if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0)
377 die("Seek to %d failed: %s\n",
378 sec->shdr.sh_offset, strerror(errno));
380 if (fread(sec->reltab, 1, sec->shdr.sh_size, fp) !=
382 die("Cannot read symbol table: %s\n", strerror(errno));
384 for (j = 0; j < sec->shdr.sh_size/sizeof(Elf_Rel); j++) {
385 Elf_Rel *rel = &sec->reltab[j];
387 rel->r_offset = elf_addr_to_cpu(rel->r_offset);
388 /* Set offset into kernel image */
389 rel->r_offset -= base;
391 rel->r_info = elf_xword_to_cpu(rel->r_info);
393 /* Convert MIPS64 RELA format - only the symbol
394 * index needs converting to native endianness
396 rel->r_info = rel->r_info;
397 ELF_R_SYM(rel->r_info) = elf32_to_cpu(ELF_R_SYM(rel->r_info));
399 #if (SHT_REL_TYPE == SHT_RELA)
400 rel->r_addend = elf_xword_to_cpu(rel->r_addend);
406 static void remove_relocs(FILE *fp)
411 for (i = 0; i < ehdr.e_shnum; i++) {
412 struct section *sec = &secs[i];
414 if (sec->shdr.sh_type != SHT_REL_TYPE)
417 if (fseek(fp, sec->shdr_offset, SEEK_SET) < 0)
418 die("Seek to %d failed: %s\n",
419 sec->shdr_offset, strerror(errno));
421 if (fread(&shdr, sizeof(shdr), 1, fp) != 1)
422 die("Cannot read ELF section headers %d/%d: %s\n",
423 i, ehdr.e_shnum, strerror(errno));
425 /* Set relocation section size to 0, effectively removing it.
426 * This is necessary due to lack of support for relocations
427 * in objcopy when creating 32bit elf from 64bit elf.
431 if (fseek(fp, sec->shdr_offset, SEEK_SET) < 0)
432 die("Seek to %d failed: %s\n",
433 sec->shdr_offset, strerror(errno));
435 if (fwrite(&shdr, sizeof(shdr), 1, fp) != 1)
436 die("Cannot write ELF section headers %d/%d: %s\n",
437 i, ehdr.e_shnum, strerror(errno));
441 static void add_reloc(struct relocs *r, uint32_t offset, unsigned type)
443 /* Relocation representation in binary table:
444 * |76543210|76543210|76543210|76543210|
445 * | Type | offset from _text >> 2 |
448 if (offset > 0x00FFFFFF)
449 die("Kernel image exceeds maximum size for relocation!\n");
451 offset = (offset & 0x00FFFFFF) | ((type & 0xFF) << 24);
453 if (r->count == r->size) {
454 unsigned long newsize = r->size + 50000;
455 void *mem = realloc(r->offset, newsize * sizeof(r->offset[0]));
458 die("realloc failed\n");
463 r->offset[r->count++] = offset;
466 static void walk_relocs(int (*process)(struct section *sec, Elf_Rel *rel,
467 Elf_Sym *sym, const char *symname))
471 /* Walk through the relocations */
472 for (i = 0; i < ehdr.e_shnum; i++) {
475 struct section *sec_applies, *sec_symtab;
477 struct section *sec = &secs[i];
479 if (sec->shdr.sh_type != SHT_REL_TYPE)
482 sec_symtab = sec->link;
483 sec_applies = &secs[sec->shdr.sh_info];
484 if (!(sec_applies->shdr.sh_flags & SHF_ALLOC))
487 sh_symtab = sec_symtab->symtab;
488 sym_strtab = sec_symtab->link->strtab;
489 for (j = 0; j < sec->shdr.sh_size/sizeof(Elf_Rel); j++) {
490 Elf_Rel *rel = &sec->reltab[j];
491 Elf_Sym *sym = &sh_symtab[ELF_R_SYM(rel->r_info)];
492 const char *symname = sym_name(sym_strtab, sym);
494 process(sec, rel, sym, symname);
499 static int do_reloc(struct section *sec, Elf_Rel *rel, Elf_Sym *sym,
502 unsigned r_type = ELF_R_TYPE(rel->r_info);
503 unsigned bind = ELF_ST_BIND(sym->st_info);
505 if ((bind == STB_WEAK) && (sym->st_value == 0)) {
506 /* Don't relocate weak symbols without a target */
510 if (regex_skip_reloc(symname))
520 * NONE can be ignored and PC relative relocations don't
521 * need to be adjusted.
525 /* We support relocating within the same 4Gb segment only,
526 * thus leaving the top 32bits unchanged
529 /* We support relocating by 64k jumps only
530 * thus leaving the bottom 16bits unchanged
538 add_reloc(&relocs, rel->r_offset, r_type);
542 die("Unsupported relocation type: %s (%d)\n",
543 rel_type(r_type), r_type);
550 static int write_reloc_as_bin(uint32_t v, FILE *f)
552 unsigned char buf[4];
556 memcpy(buf, &v, sizeof(uint32_t));
557 return fwrite(buf, 1, 4, f);
560 static int write_reloc_as_text(uint32_t v, FILE *f)
564 res = fprintf(f, "\t.long 0x%08"PRIx32"\n", v);
568 return sizeof(uint32_t);
571 static void emit_relocs(int as_text, int as_bin, FILE *outf)
574 int (*write_reloc)(uint32_t, FILE *) = write_reloc_as_bin;
577 struct section *sec_reloc;
579 sec_reloc = sec_lookup(".data.reloc");
581 die("Could not find relocation section\n");
583 size_reserved = sec_reloc->shdr.sh_size;
585 /* Collect up the relocations */
586 walk_relocs(do_reloc);
588 /* Print the relocations */
590 /* Print the relocations in a form suitable that
593 printf(".section \".data.reloc\",\"a\"\n");
594 printf(".balign 4\n");
595 /* Output text to stdout */
596 write_reloc = write_reloc_as_text;
599 /* Output raw binary to stdout */
602 /* Seek to offset of the relocation section.
603 * Each relocation is then written into the
604 * vmlinux kernel image.
606 if (fseek(outf, sec_reloc->shdr.sh_offset, SEEK_SET) < 0) {
607 die("Seek to %d failed: %s\n",
608 sec_reloc->shdr.sh_offset, strerror(errno));
612 for (i = 0; i < relocs.count; i++)
613 size += write_reloc(relocs.offset[i], outf);
615 /* Print a stop, but only if we've actually written some relocs */
617 size += write_reloc(0, outf);
619 if (size > size_reserved)
620 /* Die, but suggest a value for CONFIG_RELOCATION_TABLE_SIZE
621 * which will fix this problem and allow a bit of headroom
622 * if more kernel features are enabled
624 die("Relocations overflow available space!\n" \
625 "Please adjust CONFIG_RELOCATION_TABLE_SIZE " \
626 "to at least 0x%08x\n", (size + 0x1000) & ~0xFFF);
630 * As an aid to debugging problems with different linkers
631 * print summary information about the relocs.
632 * Since different linkers tend to emit the sections in
633 * different orders we use the section names in the output.
635 static int do_reloc_info(struct section *sec, Elf_Rel *rel, ElfW(Sym) *sym,
638 printf("%16s 0x%08x %16s %40s %16s\n",
639 sec_name(sec->shdr.sh_info),
640 (unsigned int)rel->r_offset,
641 rel_type(ELF_R_TYPE(rel->r_info)),
643 sec_name(sym->st_shndx));
647 static void print_reloc_info(void)
649 printf("%16s %10s %16s %40s %16s\n",
655 walk_relocs(do_reloc_info);
659 # define process process_64
661 # define process process_32
664 void process(FILE *fp, int as_text, int as_bin,
665 int show_reloc_info, int keep_relocs)
673 if (show_reloc_info) {
677 emit_relocs(as_text, as_bin, fp);