Index: llvm/include/llvm/ObjectYAML/ELFYAML.h =================================================================== --- llvm/include/llvm/ObjectYAML/ELFYAML.h +++ llvm/include/llvm/ObjectYAML/ELFYAML.h @@ -203,6 +203,9 @@ // The following members are used to override section fields which is // useful for creating invalid objects. + // This can be used to override the sh_addralign field. + Optional ShAddrAlign; + // This can be used to override the offset stored in the sh_name field. // It does not affect the name stored in the string table. Optional ShName; Index: llvm/lib/ObjectYAML/ELFEmitter.cpp =================================================================== --- llvm/lib/ObjectYAML/ELFEmitter.cpp +++ llvm/lib/ObjectYAML/ELFEmitter.cpp @@ -552,6 +552,8 @@ static void overrideFields(ELFYAML::Section *From, typename ELFT::Shdr &To) { if (!From) return; + if (From->ShAddrAlign) + To.sh_addralign = *From->ShAddrAlign; if (From->ShFlags) To.sh_flags = *From->ShFlags; if (From->ShName) Index: llvm/lib/ObjectYAML/ELFYAML.cpp =================================================================== --- llvm/lib/ObjectYAML/ELFYAML.cpp +++ llvm/lib/ObjectYAML/ELFYAML.cpp @@ -1113,9 +1113,9 @@ // are producing YAML, because yaml2obj sets appropriate values for them // automatically when they are not explicitly defined. assert(!IO.outputting() || - (!Section.ShOffset.hasValue() && !Section.ShSize.hasValue() && - !Section.ShName.hasValue() && !Section.ShFlags.hasValue() && - !Section.ShType.hasValue())); + (!Section.ShOffset && !Section.ShSize && !Section.ShName && + !Section.ShFlags && !Section.ShType && !Section.ShAddrAlign)); + IO.mapOptional("ShAddrAlign", Section.ShAddrAlign); IO.mapOptional("ShName", Section.ShName); IO.mapOptional("ShOffset", Section.ShOffset); IO.mapOptional("ShSize", Section.ShSize); Index: llvm/test/tools/yaml2obj/ELF/override-shaddralign.yaml =================================================================== --- /dev/null +++ llvm/test/tools/yaml2obj/ELF/override-shaddralign.yaml @@ -0,0 +1,73 @@ +## Check we are able to set a custom sh_addralign field for different sections +## and that doing this does not affect the output size. + +## Demonstrate that we can't simply use the "AddressAlign" key, +## because the output produced is too large. + +# RUN: not yaml2obj %s -DKEY="AddressAlign" 2>&1 | FileCheck %s --check-prefix=ERR + +# ERR: error: the desired output size is greater than permitted. Use the --max-size option to change the limit + +## Test that we are able to override the sh_addralign section +## field with use of "ShAddrAlign" key. + +# RUN: yaml2obj %s -DKEY="ShAddrAlign" -o %t +# RUN: llvm-readelf --sections %t | FileCheck %s --check-prefix=CHECK + +# CHECK: Section Headers: +# CHECK-NEXT: [Nr] Name {{.*}} Inf Al +# CHECK-NEXT: [ 0] {{.*}} 0 0{{$}} +# CHECK-NEXT: [ 1] .dynsym {{.*}} 1 1229782938247303441{{$}} +# CHECK-NEXT: [ 2] .symtab {{.*}} 1 2459565876494606882{{$}} +# CHECK-NEXT: [ 3] .dynamic {{.*}} 0 3689348814741910323{{$}} +# CHECK-NEXT: [ 4] .rela {{.*}} 0 4919131752989213764{{$}} +# CHECK-NEXT: [ 5] .nobits {{.*}} 0 6148914691236517205{{$}} +# CHECK-NEXT: [ 6] .group {{.*}} 0 7378697629483820646{{$}} +# CHECK-NEXT: [ 7] .gnu.version {{.*}} 0 8608480567731124087{{$}} +# CHECK-NEXT: [ 8] .gnu.version_r {{.*}} 1 9838263505978427528{{$}} +# CHECK-NEXT: [ 9] .gnu.version_d {{.*}} 1 11068046444225730969{{$}} +# CHECK-NEXT: [10] .regular {{.*}} 0 12297829382473034410{{$}} +# CHECK-NEXT: [11] .strtab {{.*}} 0 13527612320720337851{{$}} +# CHECK-NEXT: [12] .shstrtab {{.*}} 0 1{{$}} + +--- !ELF +FileHeader: + Class: ELFCLASS64 + Data: ELFDATA2LSB + Type: ET_REL +Sections: + - Name: .dynsym + Type: SHT_DYNSYM + [[KEY]]: 0x1111111111111111 + - Name: .symtab + Type: SHT_SYMTAB + [[KEY]]: 0x2222222222222222 + - Name: .dynamic + Type: SHT_DYNAMIC + [[KEY]]: 0x3333333333333333 + - Name: .rela + Type: SHT_RELA + [[KEY]]: 0x4444444444444444 + - Name: .nobits + Type: SHT_NOBITS + [[KEY]]: 0x5555555555555555 + - Name: .group + Type: SHT_GROUP + [[KEY]]: 0x6666666666666666 + - Name: .gnu.version + Type: SHT_GNU_versym + [[KEY]]: 0x7777777777777777 + - Name: .gnu.version_r + Type: SHT_GNU_verneed + Info: 0x0000000000000001 + [[KEY]]: 0x8888888888888888 + - Name: .gnu.version_d + Type: SHT_GNU_verdef + Info: 0x0000000000000001 + [[KEY]]: 0x9999999999999999 + - Name: .regular + Type: SHT_PROGBITS + [[KEY]]: 0xAAAAAAAAAAAAAAAA + - Name: .strtab + Type: SHT_STRTAB + [[KEY]]: 0xBBBBBBBBBBBBBBBB