blob: 2f7f1796a0eff4c3108318078a61f4de42a406e4 [file] [log] [blame]
Tom Rini8b0c8a12018-05-06 18:27:01 -04001// SPDX-License-Identifier: GPL-2.0+ OR BSD-2-Clause
Scott Wood49fa0592013-12-14 11:47:32 +08002/*
3 * Copyright 2013 Freescale Semiconductor, Inc.
4 *
Scott Wood49fa0592013-12-14 11:47:32 +08005 * 64-bit and little-endian target only until we need to support a different
6 * arch that needs this.
7 */
8
9#include <elf.h>
10#include <errno.h>
11#include <inttypes.h>
12#include <stdarg.h>
13#include <stdbool.h>
14#include <stdio.h>
15#include <stdlib.h>
16#include <string.h>
Jonathan Gray9dd92042016-12-11 14:51:13 +110017#include "compiler.h"
Scott Wood49fa0592013-12-14 11:47:32 +080018
19#ifndef R_AARCH64_RELATIVE
20#define R_AARCH64_RELATIVE 1027
21#endif
22
Michal Simekef320202022-06-24 14:14:59 +020023static int ei_class;
24
Michal Simek6944cec2022-06-24 14:14:59 +020025static uint64_t rela_start, rela_end, text_base;
26
Scott Wood49fa0592013-12-14 11:47:32 +080027static const bool debug_en;
28
29static void debug(const char *fmt, ...)
30{
31 va_list args;
32
xypron.glpk@gmx.de5cdd5392017-05-03 22:40:11 +020033 if (debug_en) {
34 va_start(args, fmt);
Scott Wood49fa0592013-12-14 11:47:32 +080035 vprintf(fmt, args);
xypron.glpk@gmx.de5cdd5392017-05-03 22:40:11 +020036 va_end(args);
37 }
Scott Wood49fa0592013-12-14 11:47:32 +080038}
39
40static bool supported_rela(Elf64_Rela *rela)
41{
42 uint64_t mask = 0xffffffffULL; /* would be different on 32-bit */
43 uint32_t type = rela->r_info & mask;
44
45 switch (type) {
46#ifdef R_AARCH64_RELATIVE
47 case R_AARCH64_RELATIVE:
48 return true;
49#endif
50 default:
51 fprintf(stderr, "warning: unsupported relocation type %"
52 PRIu32 " at %" PRIx64 "\n",
53 type, rela->r_offset);
54
55 return false;
56 }
Michal Simekef320202022-06-24 14:14:59 +020057}
58
59static int decode_elf64(FILE *felf, char **argv)
60{
61 size_t size;
62 Elf64_Ehdr header;
63 uint64_t section_header_base, section_header_size, sh_offset, sh_size;
64 Elf64_Shdr *sh_table; /* Elf symbol table */
65 int ret, i, machine;
66 char *sh_str;
67
68 debug("64bit version\n");
69
70 /* Make sure we are at start */
71 rewind(felf);
72
73 size = fread(&header, 1, sizeof(header), felf);
74 if (size != sizeof(header)) {
75 fclose(felf);
76 return 25;
77 }
78
79 machine = header.e_machine;
80 debug("Machine\t%d\n", machine);
81
Michal Simek97d50302022-06-24 14:15:00 +020082 if (machine != EM_AARCH64) {
83 fprintf(stderr, "%s: Not supported machine type\n", argv[0]);
84 return 30;
85 }
86
Michal Simekef320202022-06-24 14:14:59 +020087 text_base = header.e_entry;
88 section_header_base = header.e_shoff;
89 section_header_size = header.e_shentsize * header.e_shnum;
90
91 sh_table = malloc(section_header_size);
92 if (!sh_table) {
93 fprintf(stderr, "%s: Cannot allocate space for section header\n",
94 argv[0]);
95 fclose(felf);
96 return 26;
97 }
98
99 ret = fseek(felf, section_header_base, SEEK_SET);
100 if (ret) {
101 fprintf(stderr, "%s: Can't set pointer to section header: %x/%lx\n",
102 argv[0], ret, section_header_base);
103 free(sh_table);
104 fclose(felf);
105 return 26;
106 }
107
108 size = fread(sh_table, 1, section_header_size, felf);
109 if (size != section_header_size) {
110 fprintf(stderr, "%s: Can't read section header: %lx/%lx\n",
111 argv[0], size, section_header_size);
112 free(sh_table);
113 fclose(felf);
114 return 27;
115 }
116
117 sh_size = sh_table[header.e_shstrndx].sh_size;
118 debug("e_shstrndx\t0x%08x\n", header.e_shstrndx);
119 debug("sh_size\t\t0x%08lx\n", sh_size);
120
121 sh_str = malloc(sh_size);
122 if (!sh_str) {
123 fprintf(stderr, "malloc failed\n");
124 free(sh_table);
125 fclose(felf);
126 return 28;
127 }
128
129 /*
130 * Specifies the byte offset from the beginning of the file
131 * to the first byte in the section.
132 */
133 sh_offset = sh_table[header.e_shstrndx].sh_offset;
134
135 debug("sh_offset\t0x%08x\n", header.e_shnum);
136
137 ret = fseek(felf, sh_offset, SEEK_SET);
138 if (ret) {
139 fprintf(stderr, "Setting up sh_offset failed\n");
140 free(sh_str);
141 free(sh_table);
142 fclose(felf);
143 return 29;
144 }
145
146 size = fread(sh_str, 1, sh_size, felf);
147 if (size != sh_size) {
148 fprintf(stderr, "%s: Can't read section: %lx/%lx\n",
149 argv[0], size, sh_size);
150 free(sh_str);
151 free(sh_table);
152 fclose(felf);
153 return 30;
154 }
155
156 for (i = 0; i < header.e_shnum; i++) {
157 /* fprintf(stderr, "%s\n", sh_str + sh_table[i].sh_name); Debug only */
158 if (!strcmp(".rela.dyn", (sh_str + sh_table[i].sh_name))) {
159 debug("Found section\t\".rela_dyn\"\n");
160 debug(" at addr\t0x%08x\n",
161 (unsigned int)sh_table[i].sh_addr);
162 debug(" at offset\t0x%08x\n",
163 (unsigned int)sh_table[i].sh_offset);
164 debug(" of size\t0x%08x\n",
165 (unsigned int)sh_table[i].sh_size);
166 rela_start = sh_table[i].sh_addr;
167 rela_end = rela_start + sh_table[i].sh_size;
168 break;
169 }
170 }
171
172 /* Clean up */
173 free(sh_str);
174 free(sh_table);
175 fclose(felf);
176
177 debug("text_base\t0x%08lx\n", text_base);
178 debug("rela_start\t0x%08lx\n", rela_start);
179 debug("rela_end\t0x%08lx\n", rela_end);
180
181 if (!rela_start)
182 return 1;
183
184 return 0;
Scott Wood49fa0592013-12-14 11:47:32 +0800185}
186
Michal Simekef320202022-06-24 14:14:59 +0200187static int decode_elf(char **argv)
Scott Wood49fa0592013-12-14 11:47:32 +0800188{
Michal Simekef320202022-06-24 14:14:59 +0200189 FILE *felf;
190 size_t size;
191 unsigned char e_ident[EI_NIDENT];
192
193 felf = fopen(argv[2], "r+b");
194 if (!felf) {
195 fprintf(stderr, "%s: Cannot open %s: %s\n",
196 argv[0], argv[5], strerror(errno));
197 return 2;
198 }
199
200 size = fread(e_ident, 1, EI_NIDENT, felf);
201 if (size != EI_NIDENT) {
202 fclose(felf);
203 return 25;
204 }
205
206 /* Check if this is really ELF file */
207 if (e_ident[0] != 0x7f &&
208 e_ident[1] != 'E' &&
209 e_ident[2] != 'L' &&
210 e_ident[3] != 'F') {
211 fclose(felf);
212 return 1;
213 }
214
215 ei_class = e_ident[4];
216 debug("EI_CLASS(1=32bit, 2=64bit) %d\n", ei_class);
217
218 if (ei_class == 2)
219 return decode_elf64(felf, argv);
220
221 return 1;
Scott Wood49fa0592013-12-14 11:47:32 +0800222}
223
Michal Simekf9c28a12022-06-24 14:15:00 +0200224static int rela_elf64(char **argv, FILE *f)
Scott Wood49fa0592013-12-14 11:47:32 +0800225{
Michal Simekf9c28a12022-06-24 14:15:00 +0200226 int i, num;
Alistair Delva5321bfe2021-10-20 21:31:32 +0000227
228 if ((rela_end - rela_start) % sizeof(Elf64_Rela)) {
229 fprintf(stderr, "%s: rela size isn't a multiple of Elf64_Rela\n", argv[0]);
230 return 3;
231 }
232
Scott Wood49fa0592013-12-14 11:47:32 +0800233 num = (rela_end - rela_start) / sizeof(Elf64_Rela);
234
235 for (i = 0; i < num; i++) {
236 Elf64_Rela rela, swrela;
237 uint64_t pos = rela_start + sizeof(Elf64_Rela) * i;
238 uint64_t addr;
239
240 if (fseek(f, pos, SEEK_SET) < 0) {
241 fprintf(stderr, "%s: %s: seek to %" PRIx64
242 " failed: %s\n",
243 argv[0], argv[1], pos, strerror(errno));
244 }
245
246 if (fread(&rela, sizeof(rela), 1, f) != 1) {
247 fprintf(stderr, "%s: %s: read rela failed at %"
248 PRIx64 "\n",
249 argv[0], argv[1], pos);
250 return 4;
251 }
252
Jonathan Gray9dd92042016-12-11 14:51:13 +1100253 swrela.r_offset = cpu_to_le64(rela.r_offset);
254 swrela.r_info = cpu_to_le64(rela.r_info);
255 swrela.r_addend = cpu_to_le64(rela.r_addend);
Scott Wood49fa0592013-12-14 11:47:32 +0800256
257 if (!supported_rela(&swrela))
258 continue;
259
260 debug("Rela %" PRIx64 " %" PRIu64 " %" PRIx64 "\n",
261 swrela.r_offset, swrela.r_info, swrela.r_addend);
262
263 if (swrela.r_offset < text_base) {
264 fprintf(stderr, "%s: %s: bad rela at %" PRIx64 "\n",
265 argv[0], argv[1], pos);
266 return 4;
267 }
268
269 addr = swrela.r_offset - text_base;
270
271 if (fseek(f, addr, SEEK_SET) < 0) {
272 fprintf(stderr, "%s: %s: seek to %"
273 PRIx64 " failed: %s\n",
274 argv[0], argv[1], addr, strerror(errno));
275 }
276
277 if (fwrite(&rela.r_addend, sizeof(rela.r_addend), 1, f) != 1) {
278 fprintf(stderr, "%s: %s: write failed at %" PRIx64 "\n",
279 argv[0], argv[1], addr);
280 return 4;
281 }
282 }
283
Michal Simekf9c28a12022-06-24 14:15:00 +0200284 return 0;
285}
286
287int main(int argc, char **argv)
288{
289 FILE *f;
290 int ret;
291 uint64_t file_size;
292
293 if (argc != 3) {
294 fprintf(stderr, "Statically apply ELF rela relocations\n");
295 fprintf(stderr, "Usage: %s <bin file> <u-boot ELF>\n",
296 argv[0]);
297 return 1;
298 }
299
300 ret = decode_elf(argv);
301 if (ret) {
302 fprintf(stderr, "ELF decoding failed\n");
303 return ret;
304 }
305
306 if (rela_start > rela_end || rela_start < text_base) {
307 fprintf(stderr, "%s: bad rela bounds\n", argv[0]);
308 return 3;
309 }
310
311 rela_start -= text_base;
312 rela_end -= text_base;
313
314 f = fopen(argv[1], "r+b");
315 if (!f) {
316 fprintf(stderr, "%s: Cannot open %s: %s\n",
317 argv[0], argv[1], strerror(errno));
318 return 2;
319 }
320
321 fseek(f, 0, SEEK_END);
322 file_size = ftell(f);
323 rewind(f);
324
325 if (rela_end > file_size) {
326 // Most likely compiler inserted some section that didn't get
327 // objcopy-ed into the final binary
328 rela_end = file_size;
329 }
330
331 if (ei_class == 2)
332 ret = rela_elf64(argv, f);
333
Scott Wood49fa0592013-12-14 11:47:32 +0800334 if (fclose(f) < 0) {
335 fprintf(stderr, "%s: %s: close failed: %s\n",
336 argv[0], argv[1], strerror(errno));
337 return 4;
338 }
339
Michal Simekf9c28a12022-06-24 14:15:00 +0200340 return ret;
Scott Wood49fa0592013-12-14 11:47:32 +0800341}