I'm trying to build a riscv elf binary. To do so I'm using :
- app.ld
- kernel.ld
- ldscript.ld
- user.s
- reset.s
- exception.s
- test.s
Basically what I am doing is building .o files ie user.o, reset.o and exception.o using the app.ld script for the users files and kernel.ld for the priviledge files. Here's a look at the ldscript : app.ld :
OUTPUT_ARCH( "riscv" )
SECTIONS
{
. = seg_user;
.text : { *(.text) }
. = ALIGN(0x1000);
.tohost : { *(.tohost) }
. = ALIGN(0x1000);
.data : { *(.data) }
.data.string : { *(.data.string)}
.bss : { *(.bss) }
_app_end = .;
}
kernel.ld :
OUTPUT_ARCH( "riscv" )
SECTIONS
{
. = seg_reset;
.reset : { *(.reset) }
. = ALIGN(0x1000);
.kernel : { *(.kernel) }
. = ALIGN(0x1000);
.exit : { *(.exit) }
_kernel_end = .;
}
ldscript.ld :
seg_reset = 0xBFC00000;
seg_user = 0x80000000;
ENTRY(_reset)
SECTIONS
{
. = seg_reset;
.reset : { *(.reset) }
. = ALIGN(0x1000);
.kernel : { *(.kernel) }
. = ALIGN(0x1000);
.exit : { *(.exit) }
. = seg_user;
.text : { *(.text) }
. = ALIGN(0x1000);
.tohost : { *(.tohost) }
. = ALIGN(0x1000);
.data : { *(.data) }
.data.string : { *(.data.string)}
.bss : { *(.bss) }
_end = .;
}
To build it I'm using a makefile :
#---------------------------------------
# Building programm
#---------------------------------------
build_sw: build_dir kernel_obj user_obj
$(RISCV) -nostdlib -march=rv32im -T sw/ldscript/ldscript.ld obj_dir/reset.o obj_dir/exception.o obj_dir/exit.o $(TEST)
kernel_obj: build_dir $(patsubst $(SW_DIR)/kernel/%.s,$(ODIR)/%.o,$(wildcard $(SW_DIR)/kernel/*.s))
user_obj : build_dir $(patsubst $(SW_DIR)/user/%.s,$(ODIR)/%.o,$(wildcard $(SW_DIR)/user/*.s))
build_dir :
mkdir -p $(ODIR)
# build kernel
$(ODIR)/%.o: $(SW_DIR)/kernel/%.s $(LD_DIR)/kernel.ld
$(RISCV) $(RISC_FLAGS) -T $(LD_DIR)/kernel.ld $< -c -o $@
# build user
$(ODIR)/%.o: $(SW_DIR)/user/%.s $(LD_DIR)/app.ld
$(RISCV) $(RISC_FLAGS) -T $(LD_DIR)/app.ld $< -c -o $@
The thing is I need the sections reset, kernel..etc to be in the Program Headers, right now if I run readelf -l on my executable I got :
Elf file type is EXEC (Executable file)
Entry point 0xbfc00000
There are 2 program headers, starting at offset 52
Program Headers:
Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align
LOAD 0x001000 0x80000000 0x80000000 0x00044 0x00044 R E 0x1000
LOAD 0x002000 0x80001000 0x80001000 0x00078 0x00078 RW 0x1000
Section to Segment mapping:
Segment Sections...
00 .text
01 .tohost
I've read some stuff about PHDRS but when I try to add this in my ldscript.ld :
seg_reset = 0xBFC00000;
seg_user = 0x80000000;
ENTRY(_reset)
PHDRS
{
reset PT_LOAD ;
kernel PT_LOAD ;
exit PT_LOAD;
text PT_LOAD ;
data PT_NULL ;
data.string PT_NULL ;
bss PT_NULL ;
}
SECTIONS
{
. = seg_reset;
.reset : { *(.reset) }
. = ALIGN(0x1000);
.kernel : { *(.kernel) }
. = ALIGN(0x1000);
.exit : { *(.exit) }
. = seg_user;
.text : { *(.text) }
. = ALIGN(0x1000);
.tohost : { *(.tohost) }
. = ALIGN(0x1000);
.data : { *(.data) }
.data.string : { *(.data.string)}
.bss : { *(.bss) }
_end = .;
}
I got the following error : no sections assigned to phdrs And i have no idea what it means, any help would be welcomed