diff mbox series

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

Message ID 20200420223418.117186-4-mhorne@FreeBSD.org
State New
Headers show
Series Add support for CONFIG_API on RISC-V | expand

Commit Message

Mitchell Horne April 20, 2020, 10:34 p.m. UTC
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

Comments

Bin Meng April 23, 2020, 2:18 p.m. UTC | #1
On Tue, Apr 21, 2020 at 7:28 AM <mhorne at freebsd.org> wrote:
>
> 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

I think we need put .data before return_addr: to make it clearer.

>         .align 8
>         .long 0
>  #else
>  #error No support for this arch!
>  #endif
>
> +       .data

And changing these symbols should be a separate patch.

>         .globl syscall_ptr
>  syscall_ptr:
>         .align  8

Regards,
Bin
diff mbox series

Patch

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*)
+    }
+}