blob: f0bc548617a43e8f53d56e2d112c3b635abd35b7 [file] [log] [blame]
Tom Rini4549e782018-05-06 18:27:01 -04001// SPDX-License-Identifier: GPL-2.0+ OR BSD-2-Clause
Scott Wood8137af12013-12-14 11:47:32 +08002/*
3 * Copyright 2013 Freescale Semiconductor, Inc.
4 *
Scott Wood8137af12013-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 Gray43db3e32016-12-11 14:51:13 +110017#include "compiler.h"
Scott Wood8137af12013-12-14 11:47:32 +080018
19#ifndef R_AARCH64_RELATIVE
20#define R_AARCH64_RELATIVE 1027
21#endif
22
23static const bool debug_en;
24
25static void debug(const char *fmt, ...)
26{
27 va_list args;
28
xypron.glpk@gmx.ded27e35f2017-05-03 22:40:11 +020029 if (debug_en) {
30 va_start(args, fmt);
Scott Wood8137af12013-12-14 11:47:32 +080031 vprintf(fmt, args);
xypron.glpk@gmx.ded27e35f2017-05-03 22:40:11 +020032 va_end(args);
33 }
Scott Wood8137af12013-12-14 11:47:32 +080034}
35
36static bool supported_rela(Elf64_Rela *rela)
37{
38 uint64_t mask = 0xffffffffULL; /* would be different on 32-bit */
39 uint32_t type = rela->r_info & mask;
40
41 switch (type) {
42#ifdef R_AARCH64_RELATIVE
43 case R_AARCH64_RELATIVE:
44 return true;
45#endif
46 default:
47 fprintf(stderr, "warning: unsupported relocation type %"
48 PRIu32 " at %" PRIx64 "\n",
49 type, rela->r_offset);
50
51 return false;
52 }
53}
54
Scott Wood8137af12013-12-14 11:47:32 +080055static bool read_num(const char *str, uint64_t *num)
56{
57 char *endptr;
58 *num = strtoull(str, &endptr, 16);
59 return str[0] && !endptr[0];
60}
61
62int main(int argc, char **argv)
63{
64 FILE *f;
65 int i, num;
Alistair Delva9d3d9812021-10-20 21:31:32 +000066 uint64_t rela_start, rela_end, text_base, file_size;
Scott Wood8137af12013-12-14 11:47:32 +080067
68 if (argc != 5) {
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");
73 return 1;
74 }
75
76 f = fopen(argv[1], "r+b");
77 if (!f) {
78 fprintf(stderr, "%s: Cannot open %s: %s\n",
79 argv[0], argv[1], strerror(errno));
80 return 2;
81 }
82
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]);
87 return 3;
88 }
89
Alistair Delva9d3d9812021-10-20 21:31:32 +000090 if (rela_start > rela_end || rela_start < text_base) {
Scott Wood8137af12013-12-14 11:47:32 +080091 fprintf(stderr, "%s: bad rela bounds\n", argv[0]);
92 return 3;
93 }
94
95 rela_start -= text_base;
96 rela_end -= text_base;
97
Alistair Delva9d3d9812021-10-20 21:31:32 +000098 fseek(f, 0, SEEK_END);
99 file_size = ftell(f);
100 rewind(f);
101
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;
106 }
107
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]);
110 return 3;
111 }
112
Scott Wood8137af12013-12-14 11:47:32 +0800113 num = (rela_end - rela_start) / sizeof(Elf64_Rela);
114
115 for (i = 0; i < num; i++) {
116 Elf64_Rela rela, swrela;
117 uint64_t pos = rela_start + sizeof(Elf64_Rela) * i;
118 uint64_t addr;
119
120 if (fseek(f, pos, SEEK_SET) < 0) {
121 fprintf(stderr, "%s: %s: seek to %" PRIx64
122 " failed: %s\n",
123 argv[0], argv[1], pos, strerror(errno));
124 }
125
126 if (fread(&rela, sizeof(rela), 1, f) != 1) {
127 fprintf(stderr, "%s: %s: read rela failed at %"
128 PRIx64 "\n",
129 argv[0], argv[1], pos);
130 return 4;
131 }
132
Jonathan Gray43db3e32016-12-11 14:51:13 +1100133 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);
Scott Wood8137af12013-12-14 11:47:32 +0800136
137 if (!supported_rela(&swrela))
138 continue;
139
140 debug("Rela %" PRIx64 " %" PRIu64 " %" PRIx64 "\n",
141 swrela.r_offset, swrela.r_info, swrela.r_addend);
142
143 if (swrela.r_offset < text_base) {
144 fprintf(stderr, "%s: %s: bad rela at %" PRIx64 "\n",
145 argv[0], argv[1], pos);
146 return 4;
147 }
148
149 addr = swrela.r_offset - text_base;
150
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));
155 }
156
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);
160 return 4;
161 }
162 }
163
164 if (fclose(f) < 0) {
165 fprintf(stderr, "%s: %s: close failed: %s\n",
166 argv[0], argv[1], strerror(errno));
167 return 4;
168 }
169
170 return 0;
171}