1 // SPDX-License-Identifier: GPL-2.0+ OR BSD-2-Clause
3 * Copyright 2013 Freescale Semiconductor, Inc.
5 * 64-bit and little-endian target only until we need to support a different
6 * arch that needs this.
19 #ifndef R_AARCH64_RELATIVE
20 #define R_AARCH64_RELATIVE 1027
25 static uint64_t rela_start, rela_end, text_base;
27 static const bool debug_en;
29 static void debug(const char *fmt, ...)
40 static bool supported_rela(Elf64_Rela *rela)
42 uint64_t mask = 0xffffffffULL; /* would be different on 32-bit */
43 uint32_t type = rela->r_info & mask;
46 #ifdef R_AARCH64_RELATIVE
47 case R_AARCH64_RELATIVE:
51 fprintf(stderr, "warning: unsupported relocation type %"
52 PRIu32 " at %" PRIx64 "\n",
53 type, rela->r_offset);
59 static int decode_elf64(FILE *felf, char **argv)
63 uint64_t section_header_base, section_header_size, sh_offset, sh_size;
64 Elf64_Shdr *sh_table; /* Elf symbol table */
68 debug("64bit version\n");
70 /* Make sure we are at start */
73 size = fread(&header, 1, sizeof(header), felf);
74 if (size != sizeof(header)) {
79 machine = header.e_machine;
80 debug("Machine\t%d\n", machine);
82 text_base = header.e_entry;
83 section_header_base = header.e_shoff;
84 section_header_size = header.e_shentsize * header.e_shnum;
86 sh_table = malloc(section_header_size);
88 fprintf(stderr, "%s: Cannot allocate space for section header\n",
94 ret = fseek(felf, section_header_base, SEEK_SET);
96 fprintf(stderr, "%s: Can't set pointer to section header: %x/%lx\n",
97 argv[0], ret, section_header_base);
103 size = fread(sh_table, 1, section_header_size, felf);
104 if (size != section_header_size) {
105 fprintf(stderr, "%s: Can't read section header: %lx/%lx\n",
106 argv[0], size, section_header_size);
112 sh_size = sh_table[header.e_shstrndx].sh_size;
113 debug("e_shstrndx\t0x%08x\n", header.e_shstrndx);
114 debug("sh_size\t\t0x%08lx\n", sh_size);
116 sh_str = malloc(sh_size);
118 fprintf(stderr, "malloc failed\n");
125 * Specifies the byte offset from the beginning of the file
126 * to the first byte in the section.
128 sh_offset = sh_table[header.e_shstrndx].sh_offset;
130 debug("sh_offset\t0x%08x\n", header.e_shnum);
132 ret = fseek(felf, sh_offset, SEEK_SET);
134 fprintf(stderr, "Setting up sh_offset failed\n");
141 size = fread(sh_str, 1, sh_size, felf);
142 if (size != sh_size) {
143 fprintf(stderr, "%s: Can't read section: %lx/%lx\n",
144 argv[0], size, sh_size);
151 for (i = 0; i < header.e_shnum; i++) {
152 /* fprintf(stderr, "%s\n", sh_str + sh_table[i].sh_name); Debug only */
153 if (!strcmp(".rela.dyn", (sh_str + sh_table[i].sh_name))) {
154 debug("Found section\t\".rela_dyn\"\n");
155 debug(" at addr\t0x%08x\n",
156 (unsigned int)sh_table[i].sh_addr);
157 debug(" at offset\t0x%08x\n",
158 (unsigned int)sh_table[i].sh_offset);
159 debug(" of size\t0x%08x\n",
160 (unsigned int)sh_table[i].sh_size);
161 rela_start = sh_table[i].sh_addr;
162 rela_end = rela_start + sh_table[i].sh_size;
172 debug("text_base\t0x%08lx\n", text_base);
173 debug("rela_start\t0x%08lx\n", rela_start);
174 debug("rela_end\t0x%08lx\n", rela_end);
182 static int decode_elf(char **argv)
186 unsigned char e_ident[EI_NIDENT];
188 felf = fopen(argv[2], "r+b");
190 fprintf(stderr, "%s: Cannot open %s: %s\n",
191 argv[0], argv[5], strerror(errno));
195 size = fread(e_ident, 1, EI_NIDENT, felf);
196 if (size != EI_NIDENT) {
201 /* Check if this is really ELF file */
202 if (e_ident[0] != 0x7f &&
210 ei_class = e_ident[4];
211 debug("EI_CLASS(1=32bit, 2=64bit) %d\n", ei_class);
214 return decode_elf64(felf, argv);
219 int main(int argc, char **argv)
226 fprintf(stderr, "Statically apply ELF rela relocations\n");
227 fprintf(stderr, "Usage: %s <bin file> <u-boot ELF>\n",
232 ret = decode_elf(argv);
234 fprintf(stderr, "ELF decoding failed\n");
238 if (rela_start > rela_end || rela_start < text_base) {
239 fprintf(stderr, "%s: bad rela bounds\n", argv[0]);
243 rela_start -= text_base;
244 rela_end -= text_base;
246 f = fopen(argv[1], "r+b");
248 fprintf(stderr, "%s: Cannot open %s: %s\n",
249 argv[0], argv[1], strerror(errno));
253 fseek(f, 0, SEEK_END);
254 file_size = ftell(f);
257 if (rela_end > file_size) {
258 // Most likely compiler inserted some section that didn't get
259 // objcopy-ed into the final binary
260 rela_end = file_size;
263 if ((rela_end - rela_start) % sizeof(Elf64_Rela)) {
264 fprintf(stderr, "%s: rela size isn't a multiple of Elf64_Rela\n", argv[0]);
268 num = (rela_end - rela_start) / sizeof(Elf64_Rela);
270 for (i = 0; i < num; i++) {
271 Elf64_Rela rela, swrela;
272 uint64_t pos = rela_start + sizeof(Elf64_Rela) * i;
275 if (fseek(f, pos, SEEK_SET) < 0) {
276 fprintf(stderr, "%s: %s: seek to %" PRIx64
278 argv[0], argv[1], pos, strerror(errno));
281 if (fread(&rela, sizeof(rela), 1, f) != 1) {
282 fprintf(stderr, "%s: %s: read rela failed at %"
284 argv[0], argv[1], pos);
288 swrela.r_offset = cpu_to_le64(rela.r_offset);
289 swrela.r_info = cpu_to_le64(rela.r_info);
290 swrela.r_addend = cpu_to_le64(rela.r_addend);
292 if (!supported_rela(&swrela))
295 debug("Rela %" PRIx64 " %" PRIu64 " %" PRIx64 "\n",
296 swrela.r_offset, swrela.r_info, swrela.r_addend);
298 if (swrela.r_offset < text_base) {
299 fprintf(stderr, "%s: %s: bad rela at %" PRIx64 "\n",
300 argv[0], argv[1], pos);
304 addr = swrela.r_offset - text_base;
306 if (fseek(f, addr, SEEK_SET) < 0) {
307 fprintf(stderr, "%s: %s: seek to %"
308 PRIx64 " failed: %s\n",
309 argv[0], argv[1], addr, strerror(errno));
312 if (fwrite(&rela.r_addend, sizeof(rela.r_addend), 1, f) != 1) {
313 fprintf(stderr, "%s: %s: write failed at %" PRIx64 "\n",
314 argv[0], argv[1], addr);
320 fprintf(stderr, "%s: %s: close failed: %s\n",
321 argv[0], argv[1], strerror(errno));