[PATCH 3/7] examples: add a linker script for the API demo

mhorne at FreeBSD.org mhorne at FreeBSD.org
Tue Apr 21 00:34:14 CEST 2020


From: Mitchell Horne <mhorne at FreeBSD.org>

When compiling the API demo program, the first object file in the linker
arguments is crt0.o, which contains the _start routine. This is done
with the hope that it will appear first in the .text section, but the
linker doesn't guarantee this behaviour.

Add a simple linker script that ensures this ordering. This fixes
execution of the API demo binary for cases where _start was placed
elsewhere in the .text section.

Signed-off-by: Mitchell Horne <mhorne at FreeBSD.org>
---
 examples/api/Makefile |  4 +++-
 examples/api/crt0.S   |  2 ++
 examples/api/demo.lds | 14 ++++++++++++++
 3 files changed, 19 insertions(+), 1 deletion(-)
 create mode 100644 examples/api/demo.lds

diff --git a/examples/api/Makefile b/examples/api/Makefile
index 9ff793206f..8fa9c04118 100644
--- a/examples/api/Makefile
+++ b/examples/api/Makefile
@@ -48,10 +48,12 @@ OBJS := $(OBJ-y) $(notdir $(EXT_COBJ-y) $(EXT_SOBJ-y))
 targets += $(OBJS)
 OBJS := $(addprefix $(obj)/,$(OBJS))
 
+LDS = $(obj)/demo.lds
 #########################################################################
 
 quiet_cmd_link_demo = LD      $@
-cmd_link_demo = $(LD) --gc-sections -Ttext $(LOAD_ADDR) -o $@ $(filter-out $(PHONY), $^) $(PLATFORM_LIBS)
+cmd_link_demo = $(LD) --gc-sections -Ttext $(LOAD_ADDR) -T $(LDS) \
+	-o $@ $(filter-out $(PHONY), $^) $(PLATFORM_LIBS)
 
 $(obj)/demo: $(OBJS) FORCE
 	$(call if_changed,link_demo)
diff --git a/examples/api/crt0.S b/examples/api/crt0.S
index 2f75f5a036..658bc59a82 100644
--- a/examples/api/crt0.S
+++ b/examples/api/crt0.S
@@ -62,12 +62,14 @@ syscall:
 	.end syscall
 
 return_addr:
+	.data
 	.align 8
 	.long 0
 #else
 #error No support for this arch!
 #endif
 
+	.data
 	.globl syscall_ptr
 syscall_ptr:
 	.align	8
diff --git a/examples/api/demo.lds b/examples/api/demo.lds
new file mode 100644
index 0000000000..a6b88dedef
--- /dev/null
+++ b/examples/api/demo.lds
@@ -0,0 +1,14 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ * Copyright (C) 2020 Mitchell Horne <mhorne at FreeBSD.org>
+ */
+
+ENTRY(_start)
+SECTIONS
+{
+    .text :
+    {
+        *crt0.S(.text*)
+        *(.text*)
+    }
+}
-- 
2.26.1



More information about the U-Boot mailing list