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
23 static const bool debug_en;
25 static void debug(const char *fmt, ...)
36 static bool supported_rela(Elf64_Rela *rela)
38 uint64_t mask = 0xffffffffULL; /* would be different on 32-bit */
39 uint32_t type = rela->r_info & mask;
42 #ifdef R_AARCH64_RELATIVE
43 case R_AARCH64_RELATIVE:
47 fprintf(stderr, "warning: unsupported relocation type %"
48 PRIu32 " at %" PRIx64 "\n",
49 type, rela->r_offset);
55 static bool read_num(const char *str, uint64_t *num)
58 *num = strtoull(str, &endptr, 16);
59 return str[0] && !endptr[0];
62 int main(int argc, char **argv)
66 uint64_t rela_start, rela_end, text_base, file_size;
69 fprintf(stderr, "Statically apply ELF rela relocations\n");
70 fprintf(stderr, "Usage: %s <bin file> <text base> " \
71 "<rela start> <rela end>\n", argv[0]);
72 fprintf(stderr, "All numbers in hex.\n");
76 f = fopen(argv[1], "r+b");
78 fprintf(stderr, "%s: Cannot open %s: %s\n",
79 argv[0], argv[1], strerror(errno));
83 if (!read_num(argv[2], &text_base) ||
84 !read_num(argv[3], &rela_start) ||
85 !read_num(argv[4], &rela_end)) {
86 fprintf(stderr, "%s: bad number\n", argv[0]);
90 if (rela_start > rela_end || rela_start < text_base) {
91 fprintf(stderr, "%s: bad rela bounds\n", argv[0]);
95 rela_start -= text_base;
96 rela_end -= text_base;
98 fseek(f, 0, SEEK_END);
102 if (rela_end > file_size) {
103 // Most likely compiler inserted some section that didn't get
104 // objcopy-ed into the final binary
105 rela_end = file_size;
108 if ((rela_end - rela_start) % sizeof(Elf64_Rela)) {
109 fprintf(stderr, "%s: rela size isn't a multiple of Elf64_Rela\n", argv[0]);
113 num = (rela_end - rela_start) / sizeof(Elf64_Rela);
115 for (i = 0; i < num; i++) {
116 Elf64_Rela rela, swrela;
117 uint64_t pos = rela_start + sizeof(Elf64_Rela) * i;
120 if (fseek(f, pos, SEEK_SET) < 0) {
121 fprintf(stderr, "%s: %s: seek to %" PRIx64
123 argv[0], argv[1], pos, strerror(errno));
126 if (fread(&rela, sizeof(rela), 1, f) != 1) {
127 fprintf(stderr, "%s: %s: read rela failed at %"
129 argv[0], argv[1], pos);
133 swrela.r_offset = cpu_to_le64(rela.r_offset);
134 swrela.r_info = cpu_to_le64(rela.r_info);
135 swrela.r_addend = cpu_to_le64(rela.r_addend);
137 if (!supported_rela(&swrela))
140 debug("Rela %" PRIx64 " %" PRIu64 " %" PRIx64 "\n",
141 swrela.r_offset, swrela.r_info, swrela.r_addend);
143 if (swrela.r_offset < text_base) {
144 fprintf(stderr, "%s: %s: bad rela at %" PRIx64 "\n",
145 argv[0], argv[1], pos);
149 addr = swrela.r_offset - text_base;
151 if (fseek(f, addr, SEEK_SET) < 0) {
152 fprintf(stderr, "%s: %s: seek to %"
153 PRIx64 " failed: %s\n",
154 argv[0], argv[1], addr, strerror(errno));
157 if (fwrite(&rela.r_addend, sizeof(rela.r_addend), 1, f) != 1) {
158 fprintf(stderr, "%s: %s: write failed at %" PRIx64 "\n",
159 argv[0], argv[1], addr);
165 fprintf(stderr, "%s: %s: close failed: %s\n",
166 argv[0], argv[1], strerror(errno));