Index: llvm/trunk/lib/ObjectYAML/ELFEmitter.cpp =================================================================== --- llvm/trunk/lib/ObjectYAML/ELFEmitter.cpp +++ llvm/trunk/lib/ObjectYAML/ELFEmitter.cpp @@ -645,16 +645,15 @@ PHeader.p_memsz = YamlPhdr.MemSize ? uint64_t(*YamlPhdr.MemSize) : MemOffset - PHeader.p_offset; - // Set the alignment of the segment to be the same as the maximum alignment - // of the sections with the same offset so that by default the segment - // has a valid and sensible alignment. if (YamlPhdr.Align) { PHeader.p_align = *YamlPhdr.Align; } else { + // Set the alignment of the segment to be the maximum alignment of the + // sections so that by default the segment has a valid and sensible + // alignment. PHeader.p_align = 1; for (Elf_Shdr *SHeader : Sections) - if (SHeader->sh_offset == PHeader.p_offset) - PHeader.p_align = std::max(PHeader.p_align, SHeader->sh_addralign); + PHeader.p_align = std::max(PHeader.p_align, SHeader->sh_addralign); } } } Index: llvm/trunk/test/tools/yaml2obj/program-header-align.yaml =================================================================== --- llvm/trunk/test/tools/yaml2obj/program-header-align.yaml +++ llvm/trunk/test/tools/yaml2obj/program-header-align.yaml @@ -1,63 +1,56 @@ -# RUN: yaml2obj %s -o %t +## Test the p_align field of a program header can be set explicitly or inferred +## from the maximum alignment of contained sections. + +## Explicit Align has priority over section alignments. + +# RUN: yaml2obj --docnum=1 %s -o %t # RUN: llvm-readobj -l %t | FileCheck %s +# CHECK: ProgramHeader { +# CHECK-NOT: ProgramHeader { +# CHECK: Alignment: 16 +# CHECK-NEXT: } -!ELF +--- !ELF FileHeader: - Class: ELFCLASS64 - Data: ELFDATA2LSB - Type: ET_EXEC - Machine: EM_X86_64 + Class: ELFCLASS64 + Data: ELFDATA2LSB + Type: ET_EXEC + Machine: EM_NONE Sections: - - Name: .text - Type: SHT_PROGBITS - Flags: [ SHF_ALLOC, SHF_EXECINSTR ] - AddressAlign: 0x0000000000000008 - Content: "00000000" - - Name: .data - Type: SHT_PROGBITS - Flags: [ SHF_ALLOC, SHF_EXECINSTR ] - Content: "00000000" - AddressAlign: 0x0000000000000010 + - Name: .tdata + Type: SHT_PROGBITS + AddressAlign: 4 + - Name: .tbss + Type: SHT_NOBITS + AddressAlign: 64 ProgramHeaders: - - Type: PT_LOAD - Flags: [ PF_X, PF_R ] - VAddr: 0xAAAA1030 - PAddr: 0xFFFF1030 - Align: 0x1000 + - Type: PT_TLS + Align: 16 Sections: - - Section: .text + - Section: .tdata + - Section: .tbss + +## If Align is not specified, p_align is inferred from the maximum alignment +## of contained sections. + +# RUN: yaml2obj --docnum=2 %s -o %t +# RUN: llvm-readobj -l %t | FileCheck %s + +--- !ELF +FileHeader: + Class: ELFCLASS64 + Data: ELFDATA2LSB + Type: ET_EXEC + Machine: EM_NONE +Sections: + - Name: .text + Type: SHT_PROGBITS + AddressAlign: 4 + - Name: .text.hot + Type: SHT_PROGBITS + AddressAlign: 16 +ProgramHeaders: - Type: PT_LOAD - Flags: [ PF_R ] - VAddr: 0xAAAA2040 - PAddr: 0xFFFF2040 - Align: 0x1000 Sections: - - Section: .data - -#CHECK: ProgramHeaders [ -#CHECK-NEXT: ProgramHeader { -#CHECK-NEXT: Type: PT_LOAD -#CHECK-NEXT: Offset: 0xB0 -#CHECK-NEXT: VirtualAddress: 0xAAAA1030 -#CHECK-NEXT: PhysicalAddress: 0xFFFF1030 -#CHECK-NEXT: FileSize: 4 -#CHECK-NEXT: MemSize: 4 -#CHECK-NEXT: Flags [ -#CHECK-NEXT: PF_R -#CHECK-NEXT: PF_X -#CHECK-NEXT: ] -#CHECK-NEXT: Alignment: 4096 -#CHECK-NEXT: } -#CHECK-NEXT: ProgramHeader { -#CHECK-NEXT: Type: PT_LOAD -#CHECK-NEXT: Offset: 0xC0 -#CHECK-NEXT: VirtualAddress: 0xAAAA2040 -#CHECK-NEXT: PhysicalAddress: 0xFFFF2040 -#CHECK-NEXT: FileSize: 4 -#CHECK-NEXT: MemSize: 4 -#CHECK-NEXT: Flags [ -#CHECK-NEXT: PF_R -#CHECK-NEXT: ] -#CHECK-NEXT: Alignment: 4096 -#CHECK-NEXT: } -#CHECK-NEXT:] + - Section: .text + - Section: .text.hot