Skip to contentSkip to conversation

Cleanup some linker list usage issues

PATCH In review Tom Rini 1 participant 5 messages May 20 – Jun 4

lore
Share & tools
lore

Tom Rini

author
#0 lore raw

cover Cleanup some linker list usage issues

Hey all,

In looking at some other issues, I saw the comment in include/event.h about LTO discarding linker lists sometimes. Upon further investigation, this was a sandbox-specific problem for which this problem was a symptom. Also, the linker list code has always marked itself as "unused" and while Marek thinks this might have been due to warnings at the time, that is no longer the case. This series corrects sandbox to not have an issue with linker lists in two files, stops marking linker lists as unused and removes a now obsolete comment.

Signature · 1 line
Tom
author
#1 lore raw

PATCH 1/3 sandbox: Drop special link order treatment of start.o and sdl.o

On hardware architectures, we need to treat start.o (generated from start.S) very special due to the constraints of being a program running on hardware in an unknown state. These objects are treated a little different than the rest by the linker and linker scripts on various architectures.

Sandbox is different, and doesn't need to do that. In fact, it can lead to hard to diagnose problems because of just how subtly different the treatment is. For example, the comment about LTO in include/event.h introduced with commit 87a5d1b5d012 ("event: Add basic support for events") was only a sandbox issue because of the event in start.c and in turn linking start.o isn't treated the same way as an archive with all its sections considered.

Correct all of this by removing the "head-" lines for cpu.o and sdl.o from arch/sandbox/Makefile (and unused cmd_cc_sdl.o lines) and change arch/sandbox/cpu/Makefile to treating them both with "obj-" and not "extra-".

---
Cc: Simon Glass <[email protected]>

---
 arch/sandbox/Makefile     | 9 ---------
 arch/sandbox/cpu/Makefile | 5 ++---
 2 files changed, 2 insertions(+), 12 deletions(-)
arch/sandbox/Makefile +0 -9
index 5bbf9f1f96bb..0360d2bd8861 100644
@@ -1,13 +1,4 @@
 # SPDX-License-Identifier: GPL-2.0+
 
-head-y := arch/sandbox/cpu/start.o
-head-$(CONFIG_SANDBOX_SDL) += arch/sandbox/cpu/sdl.o
 libs-y += arch/sandbox/cpu/
 libs-y += arch/sandbox/lib/
-
-# sdl.c fails to compile with -fshort-wchar using musl.
-cmd_cc_sdl.o = $(CC) $(filter-out -nostdinc -fshort-wchar, \
-	$(patsubst -I%,-idirafter%,$(c_flags))) -fno-lto -c -o $@ $<
-
-$(obj)/sdl.o: $(src)/sdl.c FORCE
-	$(call if_changed_dep,cc_sdl.o)
arch/sandbox/cpu/Makefile +2 -3
index ee3c04c49e14..eb6bf6302e23 100644
@@ -5,9 +5,8 @@
 # (C) Copyright 2000-2003
 # Wolfgang Denk, DENX Software Engineering, [email protected].
 
-obj-y	:= cache.o cpu.o state.o initjmp.o os.o
-extra-y	:= start.o
-extra-$(CONFIG_SANDBOX_SDL)    += sdl.o
+obj-y	:= start.o cache.o cpu.o state.o initjmp.o os.o
+obj-$(CONFIG_SANDBOX_SDL)    += sdl.o
 obj-$(CONFIG_XPL_BUILD)	+= spl.o
 obj-$(CONFIG_ETH_SANDBOX_RAW)	+= eth-raw-os.o
 
Signature · 1 line
2.43.0
author
#2 lore raw

PATCH 2/3 linker_lists: Do not set "unused" attribute

In reply to Tom Rini

Whenever we declare something to be in a linker list, we want it to be included. This is why all of our linker scripts have a line similar to:

	KEEP(*(SORT(__u_boot_list*)));

to ensure that any linker list found in any of the archives we are linking together makes it to the final object. Remove the places where we set an attribute saying that it is unused, or maybe_unused.

---
Cc: Simon Glass <[email protected]>
Cc: Rasmus Villemoes <[email protected]>

---
 include/linker_lists.h | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)
include/linker_lists.h +3 -6
index 4425fcb67852..a8b7e2c9f2bb 100644
@@ -69,7 +69,6 @@
  */
 #define ll_entry_declare(_type, _name, _list)				\
 	_type _u_boot_list_2_##_list##_2_##_name __aligned(4)		\
-			__attribute__((unused))				\
 			__section("__u_boot_list_2_"#_list"_2_"#_name)
 
 /**
@@ -92,7 +91,6 @@
  */
 #define ll_entry_declare_list(_type, _name, _list)			\
 	_type _u_boot_list_2_##_list##_2_##_name[] __aligned(4)		\
-			__attribute__((unused))				\
 			__section("__u_boot_list_2_"#_list"_2_"#_name)
 
 /*
@@ -125,7 +123,6 @@
 #define ll_entry_start(_type, _list)					\
 ({									\
 	static char start[0] __aligned(CONFIG_LINKER_LIST_ALIGN)	\
-		__attribute__((unused))					\
 		__section("__u_boot_list_2_"#_list"_1");			\
 	_type * tmp = (_type *)&start;					\
 	asm("":"+r"(tmp));						\
@@ -167,7 +164,7 @@
  */
 #define ll_entry_end(_type, _list)					\
 ({									\
-	static char end[0] __aligned(1) __attribute__((unused))		\
+	static char end[0] __aligned(1)					\
 		__section("__u_boot_list_2_"#_list"_3");			\
 	_type * tmp = (_type *)&end;					\
 	asm("":"+r"(tmp));						\
@@ -251,7 +248,7 @@
  */
 #define ll_start_decl(_sym, _type, _list)					\
 	static _type _sym[0] __aligned(CONFIG_LINKER_LIST_ALIGN)	\
-		__maybe_unused __section("__u_boot_list_2_" #_list "_1")
+		__section("__u_boot_list_2_" #_list "_1")
 
 /*
  * ll_end_decl uses __aligned(1) to avoid padding before the end marker.
@@ -259,7 +256,7 @@
  */
 #define ll_end_decl(_sym, _type, _list)					\
 	static _type _sym[0] __aligned(1)				\
-		__maybe_unused __section("__u_boot_list_2_" #_list "_3")
+		__section("__u_boot_list_2_" #_list "_3")
 
 /**
  * ll_entry_get() - Retrieve entry from linker-generated array by name
Signature · 1 line
2.43.0
author
#3 lore raw

PATCH 3/3 event: Remove obsolete comment and __used attributes

In reply to Tom Rini

Now that we have both resolved the problem on sandbox that lead to a comment about linker list entries being omitted as well as made linker lists never list themselves as unused, we can update the event header file. Remove the now obsolete comment and "__used" attribute marker.

---
Cc: Simon Glass <[email protected]>

---
 include/event.h | 35 ++---------------------------------
 1 file changed, 2 insertions(+), 33 deletions(-)
include/event.h +2 -33
index 3ce5f992b04e..450281f666a8 100644
@@ -308,44 +308,13 @@ static inline const char *event_spy_id(struct evspy_info *spy)
 #endif
 }
 
-/*
- * It seems that LTO will drop list entries if it decides they are not used,
- * although the conditions that cause this are unclear.
- *
- * The example found is the following:
- *
- * static int sandbox_misc_init_f(void *ctx, struct event *event)
- * {
- *    return sandbox_early_getopt_check();
- * }
- * EVENT_SPY_FULL(EVT_MISC_INIT_F, sandbox_misc_init_f);
- *
- * where EVENT_SPY_FULL uses ll_entry_declare()
- *
- * In this case, LTO decides to drop the sandbox_misc_init_f() function
- * (which is fine) but then drops the linker-list entry too. This means
- * that the code no longer works, in this case sandbox no-longer checks its
- * command-line arguments properly.
- *
- * Without LTO, the KEEP() command in the .lds file is enough to keep the
- * entry around. But with LTO it seems that the entry has already been
- * dropped before the link script is considered.
- *
- * The only solution I can think of is to mark linker-list entries as 'used'
- * using an attribute. This should be safe, since we don't actually want to drop
- * any of these. However this does slightly limit LTO's optimisation choices.
- *
- * Another issue has come up, only with clang: using 'static' makes it throw
- * away the linker-list entry sometimes, e.g. with the EVT_FT_FIXUP entry in
- * vbe_simple.c - so for now, make it global.
- */
 #define EVENT_SPY_FULL(_type, _func) \
-	__used ll_entry_declare(struct evspy_info, _type ## _3_ ## _func, \
+	ll_entry_declare(struct evspy_info, _type ## _3_ ## _func, \
 		evspy_info) = _ESPY_REC(_type, _func)
 
 /* Simple spy with no function arguments */
 #define EVENT_SPY_SIMPLE(_type, _func) \
-	__used ll_entry_declare(struct evspy_info_simple, \
+	ll_entry_declare(struct evspy_info_simple, \
 		_type ## _3_ ## _func, \
 		evspy_info) = _ESPY_REC_SIMPLE(_type, _func)
 
Signature · 1 line
2.43.0
author
#4 lore raw
In reply to Tom Rini
Quoted 11 lines · Tom Rini

Hey all,

In looking at some other issues, I saw the comment in include/event.h about LTO discarding linker lists sometimes. Upon further investigation, this was a sandbox-specific problem for which this problem was a symptom. Also, the linker list code has always marked itself as "unused" and while Marek thinks this might have been due to warnings at the time, that is no longer the case. This series corrects sandbox to not have an issue with linker lists in two files, stops marking linker lists as unused and removes a now obsolete comment.

Quoted 1 line

[...]

Applied to u-boot/next, thanks!

[1/3] sandbox: Drop special link order treatment of start.o and sdl.o

      commit: 3ab8ea4a444ae798a173a25811081ae06cb358e3
[2/3] linker_lists: Do not set "unused" attribute
      commit: 4355a9a5f0065b31e19178a377bc5f0a7968768d
[3/3] event: Remove obsolete comment and __used attributes
      commit: 6e73890c739378776fe53f231471f202200bc79c
Signature · 1 line
Tom