Index: llvm/test/tools/obj2yaml/program-headers.yaml =================================================================== --- llvm/test/tools/obj2yaml/program-headers.yaml +++ llvm/test/tools/obj2yaml/program-headers.yaml @@ -480,3 +480,98 @@ Type: SHT_PROGBITS Flags: [ SHF_ALLOC, SHF_EXECINSTR ] Address: 0x3000 + +## Check how we dump segments which contain SHT_NOBITS sections. +# RUN: yaml2obj --docnum=6 %s -o %t6 +# RUN: obj2yaml %t6 | FileCheck %s --check-prefix=NOBITS + +# NOBITS: ProgramHeaders: +# NOBITS-NEXT: - Type: PT_LOAD +# NOBITS-NEXT: Flags: [ PF_W, PF_R ] +# NOBITS-NEXT: Sections: +# NOBITS-NEXT: - Section: .bss +# NOBITS-NEXT: - Type: PT_LOAD +# NOBITS-NEXT: Flags: [ PF_W, PF_R ] +# NOBITS-NEXT: Sections: +# NOBITS-NEXT: - Section: .data.1 +# NOBITS-NEXT: - Section: .bss +# NOBITS-NEXT: - Type: PT_LOAD +# NOBITS-NEXT: Flags: [ PF_W, PF_R ] +# NOBITS-NEXT: Sections: +# NOBITS-NEXT: - Section: .data.1 +# NOBITS-NEXT: - Section: .bss +# NOBITS-NEXT: - Section: .data.2 +# NOBITS-NEXT: - Type: PT_LOAD +# NOBITS-NEXT: Flags: [ PF_W, PF_R ] +# NOBITS-NEXT: Sections: +# NOBITS-NEXT: - Section: .bss +# NOBITS-NEXT: - Section: .data.2 +# NOBITS-NEXT: - Type: PT_LOAD +# NOBITS-NEXT: Flags: [ PF_W, PF_R ] +# NOBITS-NEXT: Sections: +# NOBITS-NEXT: - Section: .foo.bss +# NOBITS-NEXT: - Section: .bar.bss +# NOBITS-NEXT: VAddr: 0x0000000200000000 +# NOBITS-NEXT: Sections: + +--- !ELF +FileHeader: + Class: ELFCLASS64 + Data: ELFDATA2LSB + Type: ET_EXEC + Machine: EM_X86_64 +ProgramHeaders: +## Case 1: the segment contains a single SHT_NOBITS section. + - Type: PT_LOAD + Flags: [ PF_W, PF_R ] + Sections: + - Section: .bss +## Case 2: the SHT_NOBITS section is the last section in the segment. + - Type: PT_LOAD + Flags: [ PF_W, PF_R ] + Sections: + - Section: .data.1 + - Section: .bss +## Case 3: the SHT_NOBITS section is in the middle of the segment. + - Type: PT_LOAD + Flags: [ PF_W, PF_R ] + Sections: + - Section: .data.1 + - Section: .bss + - Section: .data.2 +## Case 4: the SHT_NOBITS section is the first section in the segment. + - Type: PT_LOAD + Flags: [ PF_W, PF_R ] + Sections: + - Section: .bss + - Section: .data.2 +## Case 5: another two SHT_NOBITS sections in the different segment. + - Type: PT_LOAD + Flags: [ PF_W, PF_R ] + Sections: + - Section: .foo.bss + - Section: .bar.bss + VAddr: 0x200000000 +Sections: + - Name: .data.1 + Type: SHT_PROGBITS + Flags: [ SHF_WRITE, SHF_ALLOC ] + Address: 0x1000 + Size: 0x1 + - Name: .bss + Type: SHT_NOBITS + Flags: [ SHF_WRITE, SHF_ALLOC ] + Size: 0x00000000FFFFFFFF + - Name: .data.2 + Type: SHT_PROGBITS + Flags: [ SHF_WRITE, SHF_ALLOC ] + Size: 0x1 + - Name: .foo.bss + Type: SHT_NOBITS + Flags: [ SHF_WRITE, SHF_ALLOC ] + Size: 0x10 + Address: 0x200000000 + - Name: .bar.bss + Type: SHT_NOBITS + Flags: [ SHF_WRITE, SHF_ALLOC ] + Size: 0x20 Index: llvm/tools/obj2yaml/elf2yaml.cpp =================================================================== --- llvm/tools/obj2yaml/elf2yaml.cpp +++ llvm/tools/obj2yaml/elf2yaml.cpp @@ -305,17 +305,22 @@ SHdr.sh_offset >= Phdr.p_offset && (SHdr.sh_offset + SHdr.sh_size <= Phdr.p_offset + Phdr.p_filesz); + if (FileOffsetsMatch && SHdr.sh_size > 0) + return true; + + bool VirtualAddressesMatch = SHdr.sh_addr >= Phdr.p_vaddr && + SHdr.sh_addr <= Phdr.p_vaddr + Phdr.p_memsz; + + // The SHT_NOBITS section occupy no physical space in a file. Such section + // belongs to the segment when it resides in segment's virtual address + // space. if (!FileOffsetsMatch) - return false; + return (Sec.Type == ELF::SHT_NOBITS) ? VirtualAddressesMatch : false; // An empty section on the edges of a program header can be outside of the // virtual address space of the segment. This means it is not included in // the segment and we should ignore it. - if (SHdr.sh_size == 0) - return SHdr.sh_addr >= Phdr.p_vaddr && - SHdr.sh_addr <= Phdr.p_vaddr + Phdr.p_memsz; - - return true; + return (SHdr.sh_size == 0) ? VirtualAddressesMatch : true; } template