[PATCH 18/22] tools: relocate-rela: Extract elf64 reloc to special function

Michal Simek monstr at monstr.eu
Thu Jun 2 15:57:42 CEST 2022


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 at amd.com>
---

 tools/relocate-rela.c | 96 ++++++++++++++++++++++++-------------------
 1 file changed, 53 insertions(+), 43 deletions(-)

diff --git a/tools/relocate-rela.c b/tools/relocate-rela.c
index 36065edb3f01..e62247d51e2a 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;
 }
-- 
2.36.0



More information about the U-Boot mailing list