aboutsummaryrefslogtreecommitdiff
path: root/tools
diff options
context:
space:
mode:
authorMichal Simek2022-06-24 14:15:00 +0200
committerMichal Simek2022-06-24 14:15:00 +0200
commit582ffb5cb3784ec8f4981d464a11f043d1d63846 (patch)
tree56b8bd29a26d7598e4bd2e3f2af5a1d48f81c05b /tools
parent07c052be51d26264a865ae3634964beddca1e1f1 (diff)
tools: relocate-rela: Extract elf64 reloc to special function
Adding support for new type requires to change code layout that's why move elf64 code to own function for easier maintenance. It also solves the problem with not calling fclose in case of error. Return value from rela_elf64 is saved to variable that's why fclose() is called all the time. Signed-off-by: Michal Simek <michal.simek@amd.com> Link: https://lore.kernel.org/r/21763b80527521c85ca7d4ac64ad6ff4885409c8.1655299267.git.michal.simek@amd.com
Diffstat (limited to 'tools')
-rw-r--r--tools/relocate-rela.c96
1 files changed, 53 insertions, 43 deletions
diff --git a/tools/relocate-rela.c b/tools/relocate-rela.c
index 36065edb3f0..e62247d51e2 100644
--- a/tools/relocate-rela.c
+++ b/tools/relocate-rela.c
@@ -216,49 +216,9 @@ static int decode_elf(char **argv)
return 1;
}
-int main(int argc, char **argv)
+static int rela_elf64(char **argv, FILE *f)
{
- FILE *f;
- int i, num, ret;
- uint64_t file_size;
-
- if (argc != 3) {
- fprintf(stderr, "Statically apply ELF rela relocations\n");
- fprintf(stderr, "Usage: %s <bin file> <u-boot ELF>\n",
- argv[0]);
- return 1;
- }
-
- ret = decode_elf(argv);
- if (ret) {
- fprintf(stderr, "ELF decoding failed\n");
- return ret;
- }
-
- if (rela_start > rela_end || rela_start < text_base) {
- fprintf(stderr, "%s: bad rela bounds\n", argv[0]);
- return 3;
- }
-
- rela_start -= text_base;
- rela_end -= text_base;
-
- f = fopen(argv[1], "r+b");
- if (!f) {
- fprintf(stderr, "%s: Cannot open %s: %s\n",
- argv[0], argv[1], strerror(errno));
- return 2;
- }
-
- fseek(f, 0, SEEK_END);
- file_size = ftell(f);
- rewind(f);
-
- if (rela_end > file_size) {
- // Most likely compiler inserted some section that didn't get
- // objcopy-ed into the final binary
- rela_end = file_size;
- }
+ int i, num;
if ((rela_end - rela_start) % sizeof(Elf64_Rela)) {
fprintf(stderr, "%s: rela size isn't a multiple of Elf64_Rela\n", argv[0]);
@@ -316,11 +276,61 @@ int main(int argc, char **argv)
}
}
+ return 0;
+}
+
+int main(int argc, char **argv)
+{
+ FILE *f;
+ int ret;
+ uint64_t file_size;
+
+ if (argc != 3) {
+ fprintf(stderr, "Statically apply ELF rela relocations\n");
+ fprintf(stderr, "Usage: %s <bin file> <u-boot ELF>\n",
+ argv[0]);
+ return 1;
+ }
+
+ ret = decode_elf(argv);
+ if (ret) {
+ fprintf(stderr, "ELF decoding failed\n");
+ return ret;
+ }
+
+ if (rela_start > rela_end || rela_start < text_base) {
+ fprintf(stderr, "%s: bad rela bounds\n", argv[0]);
+ return 3;
+ }
+
+ rela_start -= text_base;
+ rela_end -= text_base;
+
+ f = fopen(argv[1], "r+b");
+ if (!f) {
+ fprintf(stderr, "%s: Cannot open %s: %s\n",
+ argv[0], argv[1], strerror(errno));
+ return 2;
+ }
+
+ fseek(f, 0, SEEK_END);
+ file_size = ftell(f);
+ rewind(f);
+
+ if (rela_end > file_size) {
+ // Most likely compiler inserted some section that didn't get
+ // objcopy-ed into the final binary
+ rela_end = file_size;
+ }
+
+ if (ei_class == 2)
+ ret = rela_elf64(argv, f);
+
if (fclose(f) < 0) {
fprintf(stderr, "%s: %s: close failed: %s\n",
argv[0], argv[1], strerror(errno));
return 4;
}
- return 0;
+ return ret;
}