Skip to content

Commit 76d7e9b

Browse files
wxjstzavpatel
authored andcommitted
firmware: remove copy-base relocation
Remove copy-base relocations that are no longer needed. Signed-off-by: Xiang W <wxjstz@126.com> Reviewed-by: Samuel Holland <samuel.holland@sifive.com> Tested-by: Samuel Holland <samuel.holland@sifive.com> Reviewed-by: Anup Patel <anup@brainfault.org>
1 parent 5186da6 commit 76d7e9b

File tree

5 files changed

+11
-115
lines changed

5 files changed

+11
-115
lines changed

Makefile

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,10 @@ CC_SUPPORT_STRICT_ALIGN := $(shell $(CC) $(CLANG_TARGET) $(RELAX_FLAG) -nostdlib
179179
# Check whether the assembler and the compiler support the Zicsr and Zifencei extensions
180180
CC_SUPPORT_ZICSR_ZIFENCEI := $(shell $(CC) $(CLANG_TARGET) $(RELAX_FLAG) -nostdlib -march=rv$(OPENSBI_CC_XLEN)imafd_zicsr_zifencei -x c /dev/null -o /dev/null 2>&1 | grep "zicsr\|zifencei" > /dev/null && echo n || echo y)
181181
182+
ifneq ($(OPENSBI_LD_PIE),y)
183+
$(error Your linker does not support creating PIEs, opensbi requires this.)
184+
endif
185+
182186
# Build Info:
183187
# OPENSBI_BUILD_TIME_STAMP -- the compilation time stamp
184188
# OPENSBI_BUILD_COMPILER_VERSION -- the compiler version info
@@ -356,7 +360,7 @@ CFLAGS += -mcmodel=$(PLATFORM_RISCV_CODE_MODEL)
356360
CFLAGS += $(RELAX_FLAG)
357361
CFLAGS += $(GENFLAGS)
358362
CFLAGS += $(platform-cflags-y)
359-
CFLAGS += -fno-pie -no-pie
363+
CFLAGS += -fPIE -pie
360364
CFLAGS += $(firmware-cflags-y)
361365
362366
CPPFLAGS += $(GENFLAGS)
@@ -365,6 +369,7 @@ CPPFLAGS += $(firmware-cppflags-y)
365369
366370
ASFLAGS = -g -Wall -nostdlib
367371
ASFLAGS += -fno-omit-frame-pointer -fno-optimize-sibling-calls
372+
ASFLAGS += -fPIE
368373
# Optionally supported flags
369374
ifeq ($(CC_SUPPORT_SAVE_RESTORE),y)
370375
ASFLAGS += -mno-save-restore
@@ -391,6 +396,7 @@ ifeq ($(OPENSBI_LD_EXCLUDE_LIBS),y)
391396
ELFFLAGS += -Wl,--exclude-libs,ALL
392397
endif
393398
ELFFLAGS += -Wl,--build-id=none
399+
ELFFLAGS += -Wl,--no-dynamic-linker -Wl,-pie
394400
ELFFLAGS += $(platform-ldflags-y)
395401
ELFFLAGS += $(firmware-ldflags-y)
396402

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -276,8 +276,7 @@ document.
276276

277277
NOTE: Using Clang with a `riscv*-linux-gnu` GNU binutils linker has been seen
278278
to produce broken binaries with missing relocations; it is therefore currently
279-
recommended that this combination be avoided or *FW_PIC=n* be used to disable
280-
building OpenSBI as a position-independent binary.
279+
recommended that this combination be avoided.
281280

282281
Building with timestamp and compiler info
283282
-----------------------------------------

docs/firmware/fw.md

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,6 @@ parameters:
6969
argument by the prior booting stage.
7070
* **FW_FDT_PADDING** - Optional zero bytes padding to the embedded flattened
7171
device tree binary file specified by **FW_FDT_PATH** option.
72-
* **FW_PIC** - "FW_PIC=y" generates position independent executable firmware
73-
images. OpenSBI can run at arbitrary address with appropriate alignment.
74-
Therefore, the original relocation mechanism ("FW_PIC=n") will be skipped.
75-
In other words, OpenSBI will directly run at the load address without any
76-
code movement. This option requires a toolchain with PIE support, and it
77-
is on by default.
7872

7973
Additionally, each firmware type as a set of type specific configuration
8074
parameters. Detailed information for each firmware type can be found in the

firmware/fw_base.S

Lines changed: 3 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@
1515
#include <sbi/sbi_trap.h>
1616

1717
#define BOOT_STATUS_LOTTERY_DONE 1
18-
#define BOOT_STATUS_RELOCATE_DONE 2
19-
#define BOOT_STATUS_BOOT_HART_DONE 3
18+
#define BOOT_STATUS_BOOT_HART_DONE 2
2019

2120
.macro MOV_3R __d0, __s0, __d1, __s1, __d2, __s2
2221
add \__d0, \__s0, zero
@@ -32,17 +31,6 @@
3231
add \__d4, \__s4, zero
3332
.endm
3433

35-
/*
36-
* If __start_reg <= __check_reg and __check_reg < __end_reg then
37-
* jump to __pass
38-
*/
39-
.macro BRANGE __start_reg, __end_reg, __check_reg, __jump_lable
40-
blt \__check_reg, \__start_reg, 999f
41-
bge \__check_reg, \__end_reg, 999f
42-
j \__jump_lable
43-
999:
44-
.endm
45-
4634
.section .entry, "ax", %progbits
4735
.align 3
4836
.globl _start
@@ -56,15 +44,14 @@ _start:
5644
li a7, -1
5745
beq a6, a7, _try_lottery
5846
/* Jump to relocation wait loop if we are not boot hart */
59-
bne a0, a6, _wait_relocate_copy_done
47+
bne a0, a6, _wait_for_boot_hart
6048
_try_lottery:
6149
/* Jump to relocation wait loop if we don't get relocation lottery */
6250
lla a6, _boot_status
6351
li a7, BOOT_STATUS_LOTTERY_DONE
6452
amoswap.w a6, a7, (a6)
65-
bnez a6, _wait_relocate_copy_done
53+
bnez a6, _wait_for_boot_hart
6654

67-
#ifdef FW_PIC
6855
/* relocate the global table content */
6956
li t0, FW_TEXT_START /* link start */
7057
lla t1, _fw_start /* load start */
@@ -85,86 +72,7 @@ _try_lottery:
8572
3:
8673
addi t0, t0, (REGBYTES * 3)
8774
blt t0, t1, 2b
88-
j _relocate_done
89-
_wait_relocate_copy_done:
90-
j _wait_for_boot_hart
91-
#else
92-
/* Relocate if load address != link address */
93-
_relocate:
94-
li t0, FW_TEXT_START /* link start */
95-
lla t2, _fw_start /* load start */
96-
lla t3, _fw_reloc_end /* load end */
97-
sub t6, t2, t0 /* load offset */
98-
sub t1, t3, t6 /* link end */
99-
beq t0, t2, _relocate_done
100-
lla t4, _relocate_done
101-
sub t4, t4, t6
102-
blt t2, t0, _relocate_copy_to_upper
103-
_relocate_copy_to_lower:
104-
ble t1, t2, _relocate_copy_to_lower_loop
105-
lla t3, _boot_status
106-
BRANGE t2, t1, t3, _start_hang
107-
lla t3, _relocate
108-
lla t5, _relocate_done
109-
BRANGE t2, t1, t3, _start_hang
110-
BRANGE t2, t1, t5, _start_hang
111-
BRANGE t3, t5, t2, _start_hang
112-
_relocate_copy_to_lower_loop:
113-
REG_L t3, 0(t2)
114-
REG_S t3, 0(t0)
115-
add t0, t0, __SIZEOF_POINTER__
116-
add t2, t2, __SIZEOF_POINTER__
117-
blt t0, t1, _relocate_copy_to_lower_loop
118-
jr t4
119-
_relocate_copy_to_upper:
120-
ble t3, t0, _relocate_copy_to_upper_loop
121-
lla t2, _boot_status
122-
BRANGE t0, t3, t2, _start_hang
123-
lla t2, _relocate
124-
lla t5, _relocate_done
125-
BRANGE t0, t3, t2, _start_hang
126-
BRANGE t0, t3, t5, _start_hang
127-
BRANGE t2, t5, t0, _start_hang
128-
_relocate_copy_to_upper_loop:
129-
add t3, t3, -__SIZEOF_POINTER__
130-
add t1, t1, -__SIZEOF_POINTER__
131-
REG_L t2, 0(t3)
132-
REG_S t2, 0(t1)
133-
blt t0, t1, _relocate_copy_to_upper_loop
134-
jr t4
135-
_wait_relocate_copy_done:
136-
lla t0, _fw_start
137-
li t1, FW_TEXT_START
138-
beq t0, t1, _wait_for_boot_hart
139-
lla t2, _boot_status
140-
lla t3, _wait_for_boot_hart
141-
sub t3, t3, t0
142-
add t3, t3, t1
143-
1:
144-
/* waitting for relocate copy done (_boot_status == 1) */
145-
li t4, BOOT_STATUS_RELOCATE_DONE
146-
REG_L t5, 0(t2)
147-
/* Reduce the bus traffic so that boot hart may proceed faster */
148-
nop
149-
nop
150-
nop
151-
bgt t4, t5, 1b
152-
jr t3
153-
#endif
15475
_relocate_done:
155-
156-
/*
157-
* Mark relocate copy done
158-
* Use _boot_status copy relative to the load address
159-
*/
160-
lla t0, _boot_status
161-
#ifndef FW_PIC
162-
add t0, t0, t6
163-
#endif
164-
li t1, BOOT_STATUS_RELOCATE_DONE
165-
REG_S t1, 0(t0)
166-
fence rw, rw
167-
16876
/* At this point we are running from link address */
16977

17078
/* Reset all registers except ra, a0, a1, a2, a3 and a4 for boot HART */

firmware/objects.mk

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,6 @@ firmware-cflags-y +=
1313
firmware-asflags-y +=
1414
firmware-ldflags-y +=
1515

16-
ifndef FW_PIC
17-
FW_PIC := $(OPENSBI_LD_PIE)
18-
endif
19-
20-
ifeq ($(FW_PIC),y)
21-
firmware-genflags-y += -DFW_PIC
22-
firmware-asflags-y += -fpic
23-
firmware-cflags-y += -fPIE -pie
24-
firmware-ldflags-y += -Wl,--no-dynamic-linker -Wl,-pie
25-
endif
26-
2716
ifdef FW_TEXT_START
2817
firmware-genflags-y += -DFW_TEXT_START=$(FW_TEXT_START)
2918
endif

0 commit comments

Comments
 (0)