[ottf] Fix global pointer initialization in ottf_start.S
Linker relaxations must be disabled until the global pointer setup. This
commit fixes this issue and moves the initialization of the global
pointer to the start of `_ottf_start` for clarity.
Signed-off-by: Alphan Ulusoy <alphan@google.com>
diff --git a/sw/device/lib/testing/test_framework/ottf_start.S b/sw/device/lib/testing/test_framework/ottf_start.S
index b708145..4028307 100644
--- a/sw/device/lib/testing/test_framework/ottf_start.S
+++ b/sw/device/lib/testing/test_framework/ottf_start.S
@@ -111,6 +111,18 @@
.type _ottf_start, @function
_ottf_start:
/**
+ * Set up the global pointer `gp`.
+ *
+ * Linker relaxations are disabled until the global pointer is setup below,
+ * because otherwise some sequences may be turned into `gp`-relative
+ * sequences, which is incorrect when `gp` is not initialized.
+ */
+ .option push
+ .option norelax
+ la gp, __global_pointer$
+ .option pop
+
+ /**
* Set up the stack pointer.
*
* In RISC-V, the stack grows downwards, so we load the address of the highest
@@ -153,17 +165,6 @@
la a1, _bss_end
call crt_section_clear
- /**
- * Setup global pointer.
- *
- * This requires that we temporarily disable linker relaxations, or it will be
- * relaxed to `mv gp, gp`.
- */
- .option push
- .option norelax
- la gp, __global_pointer$
- .option pop
-
/**
* Call the functions in the `.init_array` section.
*