summaryrefslogtreecommitdiffstats
path: root/include
diff options
context:
space:
mode:
authorAhmad Fatoum <a.fatoum@pengutronix.de>2022-10-20 15:15:08 +0200
committerSascha Hauer <s.hauer@pengutronix.de>2022-10-24 11:02:13 +0200
commite9c90d6afb5380117e0a2f3c079d525ffb71861f (patch)
tree914a38326575c1ab69569d4905bf923fe5cd9aba /include
parent48314871907bfb3c00f927fe45f4ac07e4940da7 (diff)
downloadbarebox-e9c90d6afb5380117e0a2f3c079d525ffb71861f.tar.gz
barebox-e9c90d6afb5380117e0a2f3c079d525ffb71861f.tar.xz
include: asm-generic: reloc: implement runtime_address()
This introduces runtime_address(__linker_defined_symbol) as an alternative to error-prone __linker_defined_symbol + get_runtime_offset()/global_variable_offset(). While most code is better served by doing a relocate_to_current_adr(); setup_c(); and jumping to a noinline function for anything remotely complicated, we can't do that always: - In relocation code, PBL uncompressing preparatory code, we _must_ access linker defined symbols before relocation unless we reimplement them in assembly. - I believe GCC doesn't guarantee that an external object referenced in a noinline function has its address computed in the same function. Compiler may see occasion to pc-relative read e.g. two addresses located after function return into registers, believing that relocation must have happened before C code first runs. We then do the relocation, but the addresses are never touched again, so we dereference an unrelocated address later on. For these situation we introduce a new runtime_address() macro that hides behind assembly the origin of the address it returns and so the compiler can not assume that it may move it around across calls to functions like relocate_to_current_adr() or the relocation loop in relocate_to_current_adr() itself. This has one major shortcoming that exists with the opencoded addition as well: Compiler will generate PC-relative access to data defined in the same translation unit, so we end up adding the offset twice. We employ some GCC builtin magic to catch most of this at compile-time. If we just did RELOC_HIDE() with a cast, we may lull board code authors into false security when they use it for non linker defined symbols. Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de> Link: https://lore.barebox.org/20221020131510.3734338-2-a.fatoum@pengutronix.de Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
Diffstat (limited to 'include')
-rw-r--r--include/asm-generic/reloc.h69
1 files changed, 69 insertions, 0 deletions
diff --git a/include/asm-generic/reloc.h b/include/asm-generic/reloc.h
index 90459371eb..06fccbd6f3 100644
--- a/include/asm-generic/reloc.h
+++ b/include/asm-generic/reloc.h
@@ -3,8 +3,77 @@
#ifndef _ASM_GENERIC_RELOC_H_
#define _ASM_GENERIC_RELOC_H_
+#include <linux/build_bug.h>
+#include <linux/compiler.h>
+
#ifndef global_variable_offset
#define global_variable_offset() get_runtime_offset()
#endif
+/*
+ * Using sizeof() on incomplete types always fails, so we use GCC's
+ * __builtin_object_size() instead. This is the mechanism underlying
+ * FORTIFY_SOURCE. &symbol should always be something GCC can compute
+ * a size for, even without annotations, unless it's incomplete.
+ * The second argument ensures we get 0 for failure.
+ */
+#define __has_type_complete(sym) __builtin_object_size(&(sym), 2)
+
+#define __has_type_byte_array(sym) (sizeof(*sym) == 1 + __must_be_array(sym))
+
+/*
+ * runtime_address() defined below is supposed to be used exclusively
+ * with linker defined symbols, e.g. unsigned char input_end[].
+ *
+ * We can't completely ensure that, but this gets us close enough
+ * to avoid most abuse of runtime_address().
+ */
+#define __is_incomplete_byte_array(sym) \
+ (!__has_type_complete(sym) && __has_type_byte_array(sym))
+
+/*
+ * While accessing global variables before C environment is setup is
+ * questionable, we can't avoid it when we decide to write our
+ * relocation routines in C. This invites a tricky problem with
+ * this naive code:
+ *
+ * var = &variable + global_variable_offset(); relocate_to_current_adr();
+ *
+ * Compiler is within rights to rematerialize &variable after
+ * relocate_to_current_adr(), which is unfortunate because we
+ * then end up adding a relocated &variable with the relocation
+ * offset once more. We avoid this here by hiding address with
+ * RELOC_HIDE. This is required as a simple compiler barrier()
+ * with "memory" clobber is not immune to compiler proving that
+ * &sym fits in a register and as such is unaffected by the memory
+ * clobber. barrier_data(&sym) would work too, but that comes with
+ * aforementioned compiler "memory" barrier, that we don't care for.
+ *
+ * We don't necessarily need the volatile variable assignment when
+ * using the compiler-gcc.h RELOC_HIDE implementation as __asm__
+ * __volatile__ takes care of it, but the generic RELOC_HIDE
+ * implementation has GCC misscompile runtime_address() when not passing
+ * in a volatile object. Volatile casts instead of variable assignments
+ * also led to miscompilations with GCC v11.1.1 for THUMB2.
+ */
+
+#define runtime_address(sym) ({ \
+ void *volatile __addrof_sym = (sym); \
+ if (!__is_incomplete_byte_array(sym)) \
+ __unsafe_runtime_address(); \
+ RELOC_HIDE(__addrof_sym, global_variable_offset()); \
+})
+
+/*
+ * Above will fail for "near" objects, e.g. data in the same
+ * translation unit or with LTO, as the compiler can be smart
+ * enough to omit relocation entry and just generate PC relative
+ * accesses leading to base address being added twice. We try to
+ * catch most of these here by triggering an error when runtime_address()
+ * is used with anything that is not a byte array of unknown size.
+ */
+extern void *__compiletime_error(
+ "runtime_address() may only be called on linker defined symbols."
+) __unsafe_runtime_address(void);
+
#endif