[PATCH 13/49] image: Avoid #ifdefs for manual relocation

Simon Glass sjg at chromium.org
Tue May 4 01:11:00 CEST 2021


Add a macro to handle manually relocating a pointer. Update the iamge code
to use this to avoid needing #ifdefs.

Signed-off-by: Simon Glass <sjg at chromium.org>
---

(no changes since v1)

 common/image-sig.c | 43 ++++++++++++++++++++++++-------------------
 include/relocate.h |  6 ++++++
 2 files changed, 30 insertions(+), 19 deletions(-)

diff --git a/common/image-sig.c b/common/image-sig.c
index 0f8e592aba7..12310bfca1a 100644
--- a/common/image-sig.c
+++ b/common/image-sig.c
@@ -16,6 +16,7 @@
 DECLARE_GLOBAL_DATA_PTR;
 #endif /* !USE_HOSTCC*/
 #include <image.h>
+#include <relocate.h>
 #include <u-boot/ecdsa.h>
 #include <u-boot/rsa.h>
 #include <u-boot/hash-checksum.h>
@@ -112,20 +113,22 @@ struct checksum_algo *image_get_checksum_algo(const char *full_name)
 	int i;
 	const char *name;
 
-#if !defined(USE_HOSTCC) && defined(CONFIG_NEEDS_MANUAL_RELOC)
-	static bool done;
+	if (IS_ENABLED(CONFIG_NEEDS_MANUAL_RELOC)) {
+		static bool done;
 
-	if (!done) {
-		done = true;
-		for (i = 0; i < ARRAY_SIZE(checksum_algos); i++) {
-			checksum_algos[i].name += gd->reloc_off;
+		if (!done) {
+			done = true;
+			for (i = 0; i < ARRAY_SIZE(checksum_algos); i++) {
+				struct checksum_algo *algo = &checksum_algos[i];
+
+				MANUAL_RELOC(algo->name);
 #if IMAGE_ENABLE_SIGN
-			checksum_algos[i].calculate_sign += gd->reloc_off;
+				MANUAL_RELOC(algo->calculate_sign);
 #endif
-			checksum_algos[i].calculate += gd->reloc_off;
+				MANUAL_RELOC(algo->calculate);
+			}
 		}
 	}
-#endif
 
 	for (i = 0; i < ARRAY_SIZE(checksum_algos); i++) {
 		name = checksum_algos[i].name;
@@ -143,19 +146,21 @@ struct crypto_algo *image_get_crypto_algo(const char *full_name)
 	int i;
 	const char *name;
 
-#if !defined(USE_HOSTCC) && defined(CONFIG_NEEDS_MANUAL_RELOC)
-	static bool done;
+	if (IS_ENABLED(CONFIG_NEEDS_MANUAL_RELOC)) {
+		static bool done;
 
-	if (!done) {
-		done = true;
-		for (i = 0; i < ARRAY_SIZE(crypto_algos); i++) {
-			crypto_algos[i].name += gd->reloc_off;
-			crypto_algos[i].sign += gd->reloc_off;
-			crypto_algos[i].add_verify_data += gd->reloc_off;
-			crypto_algos[i].verify += gd->reloc_off;
+		if (!done) {
+			done = true;
+			for (i = 0; i < ARRAY_SIZE(crypto_algos); i++) {
+				struct crypto_algo *algo = &crypto_algos[i];
+
+				MANUAL_RELOC(algo->name);
+				MANUAL_RELOC(algo->sign);
+				MANUAL_RELOC(algo->add_verify_data);
+				MANUAL_RELOC(algo->verify);
+			}
 		}
 	}
-#endif
 
 	/* Move name to after the comma */
 	name = strchr(full_name, ',');
diff --git a/include/relocate.h b/include/relocate.h
index c4fad336128..26682da955f 100644
--- a/include/relocate.h
+++ b/include/relocate.h
@@ -57,4 +57,10 @@ static inline void *manual_reloc(void *ptr)
 		return ptr;
 }
 
+#if !defined(USE_HOSTCC) && defined(CONFIG_NEEDS_MANUAL_RELOC)
+#define MANUAL_RELOC(ptr)	(ptr) = manual_reloc(ptr)
+#else
+#define MANUAL_RELOC(ptr)	(void)(ptr)
+#endif
+
 #endif	/* _RELOCATE_H_ */
-- 
2.31.1.527.g47e6f16901-goog



More information about the U-Boot mailing list