diff mbox series

arm64: relocate-rela: Add support for ld.lld

Message ID 20211020213134.7534-4-adelva@google.com
State Accepted
Commit 9d3d981661000d8496d3b7836f3bd55d6534dd05
Delegated to: Tom Rini
Headers show
Series arm64: relocate-rela: Add support for ld.lld | expand

Commit Message

Alistair Delva Oct. 20, 2021, 9:31 p.m. UTC
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(-)

Comments

Tom Rini Oct. 25, 2021, 1:30 p.m. UTC | #1
On Wed, Oct 20, 2021 at 09:31:32PM +0000, Alistair Delva wrote:

> 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>

Thanks for the patch.  Do you have any other changes for LLVM/LLDB
support on arm64 platforms?  I think I've had a few stumbles whenever
I've tried and not been able to cycle back to them.
Alistair Delva Oct. 25, 2021, 5:46 p.m. UTC | #2
-Scott's ancient non-functioning email address

Hi Tom,

On Mon, Oct 25, 2021 at 6:30 AM Tom Rini <trini@konsulko.com> wrote:
>
> On Wed, Oct 20, 2021 at 09:31:32PM +0000, Alistair Delva wrote:
>
> > 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>
>
> Thanks for the patch.  Do you have any other changes for LLVM/LLDB
> support on arm64 platforms?  I think I've had a few stumbles whenever
> I've tried and not been able to cycle back to them.

I think the patches I posted are enough to get U-Boot to build with
LLVM LD for QEMU targets on arm64 and x86_64, but I have some other
build problems to work through on physical device targets like
rockchip.

Oddly, the arm64 binaries seem to be booting up (as far as we can see
in a debugger), but there's no serial output, so there's still some
work to do.

I'll keep upstreaming what we find. Our CI system is over here:
https://ci.android.com/builds/branches/aosp_u-boot-mainline/grid?
(using Clang but still ld.bfd for now)

> --
> Tom
Tom Rini Oct. 25, 2021, 6:17 p.m. UTC | #3
On Mon, Oct 25, 2021 at 10:46:32AM -0700, Alistair Delva wrote:
> -Scott's ancient non-functioning email address
> 
> Hi Tom,
> 
> On Mon, Oct 25, 2021 at 6:30 AM Tom Rini <trini@konsulko.com> wrote:
> >
> > On Wed, Oct 20, 2021 at 09:31:32PM +0000, Alistair Delva wrote:
> >
> > > 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>
> >
> > Thanks for the patch.  Do you have any other changes for LLVM/LLDB
> > support on arm64 platforms?  I think I've had a few stumbles whenever
> > I've tried and not been able to cycle back to them.
> 
> I think the patches I posted are enough to get U-Boot to build with
> LLVM LD for QEMU targets on arm64 and x86_64, but I have some other
> build problems to work through on physical device targets like
> rockchip.
> 
> Oddly, the arm64 binaries seem to be booting up (as far as we can see
> in a debugger), but there's no serial output, so there's still some
> work to do.
> 
> I'll keep upstreaming what we find. Our CI system is over here:
> https://ci.android.com/builds/branches/aosp_u-boot-mainline/grid?
> (using Clang but still ld.bfd for now)

OK, thanks!  I would like to see this get to the point of building +
booting, so I can hopefully get a platform or two both in CI and my
local HW CI loop building with clang as well.
Tom Rini Nov. 16, 2021, 7:25 p.m. UTC | #4
On Wed, Oct 20, 2021 at 09:31:32PM +0000, Alistair Delva wrote:

> 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>

Applied to u-boot/master, thanks!
diff mbox series

Patch

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++) {