User-defined section overlapped with .bss section when compiling linux kernel

40 views Asked by At

I'm learning the operating system, and I'm trying to use vmlinux.lds to lay out my code to specific locations. The following is my vmlinux.lds:

OUTPUT_ARCH( "riscv" )
ENTRY( _start )
MEMORY {
  ram (wxa!ri) : ORIGIN = 0x0000000080000000, LENGTH = 20M
  ramv (wxa!ri) : ORIGIN = 0xffffffc000000000, LENGTH = 4096M
}
PHDRS {
  text PT_LOAD;
  rodata PT_LOAD;
  data PT_LOAD;
  user_program PT_LOAD;
  bss PT_LOAD;
}
SECTIONS {
 . = 0xffffffc000000000;
 .text : 
 {
  PROVIDE(text_start = .);
  *(.text.init)
  *(.text.entry)
  *(.text .text.*)
  PROVIDE(text_end = .);
 } >ramv AT>ram :text

 .rodata : ALIGN(0x1000) 
 {
  PROVIDE(rodata_start = .);
  *(.srodata .srodata.*)
  *(.rodata .rodata.*)
  PROVIDE(rodata_end = .);
 } >ramv AT>ram :rodata

 .data : ALIGN(0x1000) 
 {
  PROVIDE(data_start = .);
  *(.sdata .sdata.*)
  *(.data .data.*)
  PROVIDE(data_end = .);
 } >ramv AT>ram :data

 .user_program : ALIGN(0x1000)
 {
  PROVIDE(user_program_start = .);
  *(.user_program.entry)
 }>ramv AT>ram :user_program

 .bss : ALIGN(0x1000) 
 {
  PROVIDE(bss_start = .);
  *(.sbss .sbss.*)
  *(.bss .bss.*)
  PROVIDE(bss_end = .);
 } >ramv AT>ram :bss


 
 . = ALIGN(0x1000);
 . += 0x1000;
 init_stack_top = .;
 . += 0x1000;
 stack_top = .;

 _end = .;
}

The section ".user_program" is expected to be at 0xffffffc000004000 and the length is 0x5000. I use objdump to check this, and I find that the ".user_program" section is overlayed by the ".bss" section. And after objcopy, the ".user_program" section disappear.

This is the result of objdump:

Section Headers:
  [Nr] Name              Type             Address           Offset
       Size              EntSize          Flags  Link  Info  Align
  [ 0]                   NULL             0000000000000000  00000000
       0000000000000000  0000000000000000           0     0     0
  [ 1] .text             PROGBITS         ffffffc000000000  00001000
       0000000000001a64  0000000000000000  AX       0     0     8
  [ 2] .rodata           PROGBITS         ffffffc000002000  00003000
       0000000000000213  0000000000000000   A       0     0     4096
  [ 3] .data             PROGBITS         ffffffc000003000  00004000
       0000000000000078  0000000000000000  WA       0     0     4096
  [ 4] .user_program     PROGBITS         ffffffc000004000  00005000
       0000000000005000  0000000000000000           0     0     4096
  [ 5] .bss              NOBITS           ffffffc000004000  00005000
       0000000000000238  0000000000000000  WA       0     0     4096

this is the result of "ls -l". The size of "Image" refers that it only contains sections ".text" ".todata" and ".data".

# ls -l arch/riscv/boot/Image 
-rwxr-xr-x 1 root root 12408 Oct  9 09:36 arch/riscv/boot/Image
0

There are 0 answers