NJU-ProjectN / nemu

NJU EMUlator, a full system x86/mips32/riscv32/riscv64 emulator for teaching
Other
859 stars 184 forks source link

fedora 下 make 失败 #35

Closed siliconrecycle closed 2 years ago

siliconrecycle commented 2 years ago

make menuconfig 是成功的,但是接下来 make 编译的时候失败了,根据提示在 script/build.mk 第 54 行添加了 -fPIE。但是,依然报了同样的错误,我对 gcc 不是很熟悉,关于这个错误,我该怎么解决它呢?

OS: fedora36 llvm: 14.0.0 gcc: 12.1.1 g++: 12.1.1

make[1]: Entering directory '/home/yuan/Documents/ysyx-workbench'
make[1]: Leaving directory '/home/yuan/Documents/ysyx-workbench'
+ LD /home/yuan/Documents/ysyx-workbench/nemu/build/riscv32-nemu-interpreter
/usr/bin/ld: /home/yuan/Documents/ysyx-workbench/nemu/build/obj-riscv32-nemu-interpreter/src/device/io/map.o: relocation R_X86_64_32 against `.rodata.str1.8' can not be used when making a PIE object; recompile with -fPIE
/usr/bin/ld: /home/yuan/Documents/ysyx-workbench/nemu/build/obj-riscv32-nemu-interpreter/src/device/io/mmio.o: relocation R_X86_64_32 against `.rodata' can not be used when making a PIE object; recompile with -fPIE
/usr/bin/ld: /home/yuan/Documents/ysyx-workbench/nemu/build/obj-riscv32-nemu-interpreter/src/device/io/port-io.o: relocation R_X86_64_32 against `.rodata.str1.1' can not be used when making a PIE object; recompile with -fPIE
/usr/bin/ld: /home/yuan/Documents/ysyx-workbench/nemu/build/obj-riscv32-nemu-interpreter/src/engine/interpreter/hostcall.o: relocation R_X86_64_32 against `.rodata.str1.8' can not be used when making a PIE object; recompile with -fPIE
/usr/bin/ld: /home/yuan/Documents/ysyx-workbench/nemu/build/obj-riscv32-nemu-interpreter/src/cpu/cpu-exec.o: relocation R_X86_64_32 against `.rodata.str1.1' can not be used when making a PIE object; recompile with -fPIE
/usr/bin/ld: /home/yuan/Documents/ysyx-workbench/nemu/build/obj-riscv32-nemu-interpreter/src/cpu/difftest/ref.o: relocation R_X86_64_32 against `.rodata' can not be used when making a PIE object; recompile with -fPIE
/usr/bin/ld: /home/yuan/Documents/ysyx-workbench/nemu/build/obj-riscv32-nemu-interpreter/src/monitor/monitor.o: relocation R_X86_64_32S against `.rodata.str1.1' can not be used when making a PIE object; recompile with -fPIE
/usr/bin/ld: /home/yuan/Documents/ysyx-workbench/nemu/build/obj-riscv32-nemu-interpreter/src/monitor/sdb/expr.o: relocation R_X86_64_32 against `.rodata' can not be used when making a PIE object; recompile with -fPIE
/usr/bin/ld: /home/yuan/Documents/ysyx-workbench/nemu/build/obj-riscv32-nemu-interpreter/src/monitor/sdb/sdb.o: relocation R_X86_64_32 against `.rodata.str1.1' can not be used when making a PIE object; recompile with -fPIE
/usr/bin/ld: /home/yuan/Documents/ysyx-workbench/nemu/build/obj-riscv32-nemu-interpreter/src/monitor/sdb/watchpoint.o: relocation R_X86_64_32 against `.bss' can not be used when making a PIE object; recompile with -fPIE
/usr/bin/ld: /home/yuan/Documents/ysyx-workbench/nemu/build/obj-riscv32-nemu-interpreter/src/utils/log.o: relocation R_X86_64_32 against `.rodata.str1.1' can not be used when making a PIE object; recompile with -fPIE
/usr/bin/ld: /home/yuan/Documents/ysyx-workbench/nemu/build/obj-riscv32-nemu-interpreter/src/memory/paddr.o: relocation R_X86_64_32 against `.rodata.str1.8' can not be used when making a PIE object; recompile with -fPIE
/usr/bin/ld: /home/yuan/Documents/ysyx-workbench/nemu/build/obj-riscv32-nemu-interpreter/src/isa/riscv32/inst.o: relocation R_X86_64_32 against `.rodata' can not be used when making a PIE object; recompile with -fPIE
collect2: error: ld returned 1 exit status
make: *** [/home/yuan/Documents/ysyx-workbench/nemu/scripts/build.mk:54: /home/yuan/Documents/ysyx-workbench/nemu/build/riscv32-nemu-interpreter] Error 1
siliconrecycle commented 2 years ago

修改 script/build.mk 中的 CFLAGSLDFLAGS 似乎也没有作用。

[yuan@fedora nemu]$ git diff
diff --git a/nemu/scripts/build.mk b/nemu/scripts/build.mk
index 7d1088d..bd2d067 100644
--- a/nemu/scripts/build.mk
+++ b/nemu/scripts/build.mk
@@ -3,8 +3,8 @@
 # Add necessary options if the target is a shared library
 ifeq ($(SHARE),1)
 SO = -so
-CFLAGS  += -fPIC
-LDFLAGS += -rdynamic -shared -fPIC
+CFLAGS  += -fPIC -fPIE
+LDFLAGS += -rdynamic -shared -fPIC -fPIE
 endif

 WORK_DIR  = $(shell pwd)
[yuan@fedora nemu]$ 
[yuan@fedora nemu]$ make
make[1]: Entering directory '/home/yuan/Documents/ysyx-workbench'
make[1]: Leaving directory '/home/yuan/Documents/ysyx-workbench'
+ LD /home/yuan/Documents/ysyx-workbench/nemu/build/riscv32-nemu-interpreter
/usr/bin/ld: /home/yuan/Documents/ysyx-workbench/nemu/build/obj-riscv32-nemu-interpreter/src/device/io/map.o: relocation R_X86_64_32 against `.rodata.str1.8' can not be used when making a PIE object; recompile with -fPIE
/usr/bin/ld: /home/yuan/Documents/ysyx-workbench/nemu/build/obj-riscv32-nemu-interpreter/src/device/io/mmio.o: relocation R_X86_64_32 against `.rodata' can not be used when making a PIE object; recompile with -fPIE
/usr/bin/ld: /home/yuan/Documents/ysyx-workbench/nemu/build/obj-riscv32-nemu-interpreter/src/device/io/port-io.o: relocation R_X86_64_32 against `.rodata.str1.1' can not be used when making a PIE object; recompile with -fPIE
/usr/bin/ld: /home/yuan/Documents/ysyx-workbench/nemu/build/obj-riscv32-nemu-interpreter/src/engine/interpreter/hostcall.o: relocation R_X86_64_32 against `.rodata.str1.8' can not be used when making a PIE object; recompile with -fPIE
/usr/bin/ld: /home/yuan/Documents/ysyx-workbench/nemu/build/obj-riscv32-nemu-interpreter/src/cpu/cpu-exec.o: relocation R_X86_64_32 against `.rodata.str1.1' can not be used when making a PIE object; recompile with -fPIE
/usr/bin/ld: /home/yuan/Documents/ysyx-workbench/nemu/build/obj-riscv32-nemu-interpreter/src/cpu/difftest/ref.o: relocation R_X86_64_32 against `.rodata' can not be used when making a PIE object; recompile with -fPIE
/usr/bin/ld: /home/yuan/Documents/ysyx-workbench/nemu/build/obj-riscv32-nemu-interpreter/src/monitor/monitor.o: relocation R_X86_64_32S against `.rodata.str1.1' can not be used when making a PIE object; recompile with -fPIE
/usr/bin/ld: /home/yuan/Documents/ysyx-workbench/nemu/build/obj-riscv32-nemu-interpreter/src/monitor/sdb/expr.o: relocation R_X86_64_32 against `.rodata' can not be used when making a PIE object; recompile with -fPIE
/usr/bin/ld: /home/yuan/Documents/ysyx-workbench/nemu/build/obj-riscv32-nemu-interpreter/src/monitor/sdb/sdb.o: relocation R_X86_64_32 against `.rodata.str1.1' can not be used when making a PIE object; recompile with -fPIE
/usr/bin/ld: /home/yuan/Documents/ysyx-workbench/nemu/build/obj-riscv32-nemu-interpreter/src/monitor/sdb/watchpoint.o: relocation R_X86_64_32 against `.bss' can not be used when making a PIE object; recompile with -fPIE
/usr/bin/ld: /home/yuan/Documents/ysyx-workbench/nemu/build/obj-riscv32-nemu-interpreter/src/utils/log.o: relocation R_X86_64_32 against `.rodata.str1.1' can not be used when making a PIE object; recompile with -fPIE
/usr/bin/ld: /home/yuan/Documents/ysyx-workbench/nemu/build/obj-riscv32-nemu-interpreter/src/memory/paddr.o: relocation R_X86_64_32 against `.rodata.str1.8' can not be used when making a PIE object; recompile with -fPIE
/usr/bin/ld: /home/yuan/Documents/ysyx-workbench/nemu/build/obj-riscv32-nemu-interpreter/src/isa/riscv32/inst.o: relocation R_X86_64_32 against `.rodata' can not be used when making a PIE object; recompile with -fPIE
collect2: error: ld returned 1 exit status
make: *** [/home/yuan/Documents/ysyx-workbench/nemu/scripts/build.mk:54: /home/yuan/Documents/ysyx-workbench/nemu/build/riscv32-nemu-interpreter] Error 1
siliconrecycle commented 2 years ago

试了下 -no-pie 还是有问题。【躺倒】

[qian@fedora nemu]$ git diff
diff --git a/nemu/scripts/build.mk b/nemu/scripts/build.mk
index 7d1088d..f06c2cf 100644
--- a/nemu/scripts/build.mk
+++ b/nemu/scripts/build.mk
@@ -3,8 +3,8 @@
 # Add necessary options if the target is a shared library
 ifeq ($(SHARE),1)
 SO = -so
-CFLAGS  += -fPIC
-LDFLAGS += -rdynamic -shared -fPIC
+CFLAGS  += -fPIC -no-pie
+LDFLAGS += -rdynamic -shared -fPIC -no-pie
 endif

 WORK_DIR  = $(shell pwd)
[qian@fedora nemu]$ make clean
rm -rf /home/qian/Documents/ysyx-workbench/nemu/build
[qian@fedora nemu]$ make
+ CC src/nemu-main.c
+ CC src/device/io/map.c
+ CC src/device/io/mmio.c
+ CC src/device/io/port-io.c
+ CC src/engine/interpreter/hostcall.c
+ CC src/engine/interpreter/init.c
+ CC src/cpu/cpu-exec.c
+ CC src/cpu/difftest/dut.c
+ CC src/cpu/difftest/ref.c
+ CC src/monitor/monitor.c
+ CC src/monitor/sdb/expr.c
+ CC src/monitor/sdb/sdb.c
+ CC src/monitor/sdb/watchpoint.c
+ CC src/utils/log.c
+ CC src/utils/rand.c
+ CC src/utils/state.c
+ CC src/utils/timer.c
+ CC src/memory/paddr.c
+ CC src/memory/vaddr.c
+ CC src/isa/riscv32/difftest/dut.c
+ CC src/isa/riscv32/init.c
+ CC src/isa/riscv32/inst.c
+ CC src/isa/riscv32/logo.c
+ CC src/isa/riscv32/reg.c
+ CC src/isa/riscv32/system/intr.c
+ CC src/isa/riscv32/system/mmu.c
+ CXX src/utils/disasm.cc
make[1]: Entering directory '/home/qian/Documents/ysyx-workbench'
make[1]: Leaving directory '/home/qian/Documents/ysyx-workbench'
+ LD /home/qian/Documents/ysyx-workbench/nemu/build/riscv32-nemu-interpreter
/usr/bin/ld: /home/qian/Documents/ysyx-workbench/nemu/build/obj-riscv32-nemu-interpreter/src/device/io/map.o: relocation R_X86_64_32 against `.rodata.str1.8' can not be used when making a PIE object; recompile with -fPIE
/usr/bin/ld: /home/qian/Documents/ysyx-workbench/nemu/build/obj-riscv32-nemu-interpreter/src/device/io/mmio.o: relocation R_X86_64_32 against `.rodata' can not be used when making a PIE object; recompile with -fPIE
/usr/bin/ld: /home/qian/Documents/ysyx-workbench/nemu/build/obj-riscv32-nemu-interpreter/src/device/io/port-io.o: relocation R_X86_64_32 against `.rodata.str1.1' can not be used when making a PIE object; recompile with -fPIE
/usr/bin/ld: /home/qian/Documents/ysyx-workbench/nemu/build/obj-riscv32-nemu-interpreter/src/engine/interpreter/hostcall.o: relocation R_X86_64_32 against `.rodata.str1.8' can not be used when making a PIE object; recompile with -fPIE
/usr/bin/ld: /home/qian/Documents/ysyx-workbench/nemu/build/obj-riscv32-nemu-interpreter/src/cpu/cpu-exec.o: relocation R_X86_64_32 against `.rodata.str1.1' can not be used when making a PIE object; recompile with -fPIE
/usr/bin/ld: /home/qian/Documents/ysyx-workbench/nemu/build/obj-riscv32-nemu-interpreter/src/cpu/difftest/ref.o: relocation R_X86_64_32 against `.rodata' can not be used when making a PIE object; recompile with -fPIE
/usr/bin/ld: /home/qian/Documents/ysyx-workbench/nemu/build/obj-riscv32-nemu-interpreter/src/monitor/monitor.o: relocation R_X86_64_32S against `.rodata.str1.1' can not be used when making a PIE object; recompile with -fPIE
/usr/bin/ld: /home/qian/Documents/ysyx-workbench/nemu/build/obj-riscv32-nemu-interpreter/src/monitor/sdb/expr.o: relocation R_X86_64_32 against `.rodata' can not be used when making a PIE object; recompile with -fPIE
/usr/bin/ld: /home/qian/Documents/ysyx-workbench/nemu/build/obj-riscv32-nemu-interpreter/src/monitor/sdb/sdb.o: relocation R_X86_64_32 against `.rodata.str1.1' can not be used when making a PIE object; recompile with -fPIE
/usr/bin/ld: /home/qian/Documents/ysyx-workbench/nemu/build/obj-riscv32-nemu-interpreter/src/monitor/sdb/watchpoint.o: relocation R_X86_64_32 against `.bss' can not be used when making a PIE object; recompile with -fPIE
/usr/bin/ld: /home/qian/Documents/ysyx-workbench/nemu/build/obj-riscv32-nemu-interpreter/src/utils/log.o: relocation R_X86_64_32 against `.rodata.str1.1' can not be used when making a PIE object; recompile with -fPIE
/usr/bin/ld: /home/qian/Documents/ysyx-workbench/nemu/build/obj-riscv32-nemu-interpreter/src/memory/paddr.o: relocation R_X86_64_32 against `.rodata.str1.8' can not be used when making a PIE object; recompile with -fPIE
/usr/bin/ld: /home/qian/Documents/ysyx-workbench/nemu/build/obj-riscv32-nemu-interpreter/src/isa/riscv32/inst.o: relocation R_X86_64_32 against `.rodata' can not be used when making a PIE object; recompile with -fPIE
collect2: error: ld returned 1 exit status
make: *** [/home/qian/Documents/ysyx-workbench/nemu/scripts/build.mk:54: /home/qian/Documents/ysyx-workbench/nemu/build/riscv32-nemu-interpreter] Error 1
[qian@fedora nemu]$ 
siliconrecycle commented 2 years ago

问题已经解决了。需要把 -fPIE 加在后面,这里的 makefile 的报错不是是否 shared 的意思。

[qian@fedora nemu]$ git diff
diff --git a/nemu/scripts/build.mk b/nemu/scripts/build.mk
index 7d1088d..c6574a0 100644
--- a/nemu/scripts/build.mk
+++ b/nemu/scripts/build.mk
@@ -22,8 +22,8 @@ CXX := g++
 endif
 LD := $(CXX)
 INCLUDES = $(addprefix -I, $(INC_PATH))
-CFLAGS  := -O2 -MMD -Wall -Werror $(INCLUDES) $(CFLAGS)
-LDFLAGS := -O2 $(LDFLAGS)
+CFLAGS  := -O2 -MMD -Wall -Werror $(INCLUDES) $(CFLAGS) -fPIE
+LDFLAGS := -O2 $(LDFLAGS) -fPIE

 OBJS = $(SRCS:%.c=$(OBJ_DIR)/%.o) $(CXXSRC:%.cc=$(OBJ_DIR)/%.o)

[qian@fedora nemu]$ 
mr-xuezzz commented 2 years ago

我遇到了和你一样的问题,感谢你的解答,我将会在slackware下尝试它,希望它有效

jereYang commented 1 year ago

管用,我的deepin20,也是这个问题,添加f-PIE后make成功