tyfkda / xv6

64bit port of xv6
Other
6 stars 0 forks source link

Confirm how to handle .bss segment #19

Closed tyfkda closed 5 years ago

takeharukato commented 6 years ago

Hi,

First, ELF executables in xv6 does not have a bss segment. Although they have a .bss section in its binary containers (ELF), it is included in the first segment in the ELF executable. It might be important to tell the meaning of segments from the meaning of sections in ELF specification correctly. In typical cases, segments should be handled by OS kernels, on the other hand, sections should be handled by language systems.

Program Headers:
  Type           Offset             VirtAddr           PhysAddr
                 FileSiz            MemSiz              Flags  Align
  LOAD           0x00000000000000c0 0x0000000000000000 0x0000000000000000
                 0x0000000000001e64 0x0000000000001f08  RWE    20
  GNU_STACK      0x0000000000000000 0x0000000000000000 0x0000000000000000
                 0x0000000000000000 0x0000000000000000  RWE    10

 Section to Segment mapping:
  Segment Sections...
   00     .text .rodata .eh_frame .data .bss
   01

In addition to this, xv6 kernel provides zeroed-pages to prepare user VMs. Thus, xv6 kernel handle .bss section properly.

Would you please pay attention to the fact that exec function copies contents of the segment with ph.filesz NOT ph.memsz. This is a key trick to make .bss section zeroed because ph.filesz is lesser than ph.memsz if .bss section exists. So exec() function can keep pages in .bss section zero.

    if(loaduvm(pgdir, (char*)ph.vaddr, ip, ph.off, ph.filesz) < 0)
      goto bad;

Thanks, Regards,

tyfkda commented 6 years ago

Thank you for your information. I'll look into the code.