public inbox for u-boot@lists.denx.de
 help / color / mirror / Atom feed
From: Alistair Delva <adelva@google.com>
To: u-boot@lists.denx.de
Cc: David Brazdil <dbrazdil@google.com>,
	Scott Wood <scottwood@freescale.com>,
	 Tom Rini <trini@konsulko.com>
Subject: [PATCH] arm64: relocate-rela: Add support for ld.lld
Date: Wed, 20 Oct 2021 21:31:32 +0000	[thread overview]
Message-ID: <20211020213134.7534-4-adelva@google.com> (raw)
In-Reply-To: <20211020213134.7534-1-adelva@google.com>

Cap end of relocations by the binary size.

Linkers like to insert some auxiliary sections between .rela.dyn and
.bss_start. These sections don't make their way to the final binary, but
reloc_rela still tries to relocate them, resulting in attempted read
past the end of file.

When linking U-Boot with ld.lld, the STATIC_RELA feature (enabled by
default on arm64) breaks the build. After this patch, U-Boot can be
linked successfully with and without CONFIG_STATIC_RELA.

Originally-from: Elena Petrova <lenaptr@google.com>
Signed-off-by: Alistair Delva <adelva@google.com>
Cc: David Brazdil <dbrazdil@google.com>
Cc: Scott Wood <scottwood@freescale.com>
Cc: Tom Rini <trini@konsulko.com>
---
 tools/relocate-rela.c | 20 +++++++++++++++++---
 1 file changed, 17 insertions(+), 3 deletions(-)

diff --git a/tools/relocate-rela.c b/tools/relocate-rela.c
index 6a524014b7..f0bc548617 100644
--- a/tools/relocate-rela.c
+++ b/tools/relocate-rela.c
@@ -63,7 +63,7 @@ int main(int argc, char **argv)
 {
 	FILE *f;
 	int i, num;
-	uint64_t rela_start, rela_end, text_base;
+	uint64_t rela_start, rela_end, text_base, file_size;
 
 	if (argc != 5) {
 		fprintf(stderr, "Statically apply ELF rela relocations\n");
@@ -87,8 +87,7 @@ int main(int argc, char **argv)
 		return 3;
 	}
 
-	if (rela_start > rela_end || rela_start < text_base ||
-	    (rela_end - rela_start) % sizeof(Elf64_Rela)) {
+	if (rela_start > rela_end || rela_start < text_base) {
 		fprintf(stderr, "%s: bad rela bounds\n", argv[0]);
 		return 3;
 	}
@@ -96,6 +95,21 @@ int main(int argc, char **argv)
 	rela_start -= text_base;
 	rela_end -= text_base;
 
+	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 ((rela_end - rela_start) % sizeof(Elf64_Rela)) {
+		fprintf(stderr, "%s: rela size isn't a multiple of Elf64_Rela\n", argv[0]);
+		return 3;
+	}
+
 	num = (rela_end - rela_start) / sizeof(Elf64_Rela);
 
 	for (i = 0; i < num; i++) {
-- 
2.30.2


  parent reply	other threads:[~2021-10-20 21:40 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-10-20 21:31 [PATCH] x86: chromebook_coral: fix C block comment Alistair Delva
2021-10-20 21:31 ` [PATCH] x86: Fix i8254 ifdef include guard Alistair Delva
2021-10-21 20:18   ` Simon Glass
2021-10-27  3:08   ` Bin Meng
2021-10-27  3:28     ` Bin Meng
2021-10-20 21:31 ` [PATCH] efi_loader: Fix link of EFI apps with ld.lld Alistair Delva
2021-10-20 22:05   ` Heinrich Schuchardt
2021-10-20 21:31 ` Alistair Delva [this message]
2021-10-25 13:30   ` [PATCH] arm64: relocate-rela: Add support for ld.lld Tom Rini
2021-10-25 17:46     ` Alistair Delva
2021-10-25 18:17       ` Tom Rini
2021-11-16 19:25   ` Tom Rini
2021-10-20 21:31 ` [PATCH] x86: Fix linking u-boot with ld.lld Alistair Delva
2021-10-21 20:18   ` Simon Glass
2021-10-31 12:57     ` Simon Glass
2021-10-20 21:31 ` [PATCH] RFC: arm: pci: Add PCI cam support to PCI-E ecam driver Alistair Delva
2021-11-18 19:15   ` Tom Rini
2021-10-21 20:18 ` [PATCH] x86: chromebook_coral: fix C block comment Simon Glass
2021-10-27  3:07 ` Bin Meng
2021-10-27  3:27   ` Bin Meng

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20211020213134.7534-4-adelva@google.com \
    --to=adelva@google.com \
    --cc=dbrazdil@google.com \
    --cc=scottwood@freescale.com \
    --cc=trini@konsulko.com \
    --cc=u-boot@lists.denx.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox