Index: llvm/lib/ObjectYAML/ELFYAML.cpp =================================================================== --- llvm/lib/ObjectYAML/ELFYAML.cpp +++ llvm/lib/ObjectYAML/ELFYAML.cpp @@ -1079,6 +1079,17 @@ return StringRef(); } +// For some fields we support a special "DEFAULT" value, what means no-op. It is +// useful in YAML descriptions that use macros sometimes. +static void maybeMapHex64(IO &IO, const char *Tag, + Optional &Val) { + StringRef Str; + if (!IO.outputting()) + IO.mapOptional(Tag, Str); + if (Str != "DEFAULT") + IO.mapOptional(Tag, Val); +} + static void commonSectionMapping(IO &IO, ELFYAML::Section &Section) { IO.mapOptional("Name", Section.Name, StringRef()); IO.mapRequired("Type", Section.Type); @@ -1095,9 +1106,13 @@ assert(!IO.outputting() || (!Section.ShOffset.hasValue() && !Section.ShSize.hasValue() && !Section.ShName.hasValue() && !Section.ShFlags.hasValue())); - IO.mapOptional("ShName", Section.ShName); - IO.mapOptional("ShOffset", Section.ShOffset); - IO.mapOptional("ShSize", Section.ShSize); + maybeMapHex64(IO, "ShName", Section.ShName); + maybeMapHex64(IO, "ShOffset", Section.ShOffset); + maybeMapHex64(IO, "ShSize", Section.ShSize); + + // ShFlags is used when we want to work around the Flag values validation. + // Also, Flag and ShFlags can't be used together. It doesn't make sense to + // support the "DEFAULT" value for it. IO.mapOptional("ShFlags", Section.ShFlags); } Index: llvm/test/tools/llvm-readobj/ELF/mips-abiflags.test =================================================================== --- llvm/test/tools/llvm-readobj/ELF/mips-abiflags.test +++ llvm/test/tools/llvm-readobj/ELF/mips-abiflags.test @@ -338,7 +338,7 @@ # RUN: llvm-readelf -A %t.err1 2>&1 | FileCheck %s -DFILE=%t.err1 --check-prefix=CONTENT-ERR # RUN: llvm-readobj -A %t.err1 2>&1 | FileCheck %s -DFILE=%t.err1 --check-prefix=CONTENT-ERR -# CONTENT-ERR: warning: '[[FILE]]': unable to read the .MIPS.abiflags section: section [index 1] has a sh_offset (0xffffffff) + sh_size (0x18) that is greater than the file size (0x240) +# CONTENT-ERR: warning: '[[FILE]]': unable to read the .MIPS.abiflags section: section [index 1] has a sh_offset (0xffffffff) + sh_size (0x18) that is greater than the file size (0x180) # CONTENT-ERR-NEXT: There is no .MIPS.options section in the file. # CONTENT-ERR-NEXT: There is no .reginfo section in the file. @@ -352,9 +352,8 @@ - Name: .MIPS.abiflags Type: SHT_MIPS_ABIFLAGS ISA: MIPS32 - Offset: 0x100 - ShOffset: [[SHOFFSET=0x100]] - ShSize: [[SHSIZE=24]] + ShOffset: [[SHOFFSET=DEFAULT]] + ShSize: [[SHSIZE=DEFAULT]] ## Check we report a warning when the .MIPS.abiflags section has an unexpected size. # RUN: yaml2obj --docnum=3 -DSHSIZE=23 %s -o %t.err2 Index: llvm/test/tools/yaml2obj/ELF/override-shname.yaml =================================================================== --- llvm/test/tools/yaml2obj/ELF/override-shname.yaml +++ llvm/test/tools/yaml2obj/ELF/override-shname.yaml @@ -1,3 +1,5 @@ +## Test how the ShName can be used to override the sh_name field of a section. + ## Check we are able to set a custom sh_name field for different sections ## and that doing this does not affect the names stored in the string table. @@ -85,3 +87,46 @@ - Name: .strtab Type: SHT_STRTAB ShName: 0x00000000B + +## Check we can set the ShName to DEFAULT and this is a no-op. +# RUN: yaml2obj %s --docnum=2 -o %t2 +# RUN: llvm-readobj --sections %t2 | FileCheck %s --check-prefix=DEFAULT +# RUN: yaml2obj %s --docnum=3 -o %t3 +# RUN: llvm-readobj --sections %t3 | FileCheck %s --check-prefix=DEFAULT + +# DEFAULT: Name: .aaa (29) +# DEFAULT: Name: .ccc (1) +# DEFAULT: Name: .bbb (6) + +--- !ELF +FileHeader: + Class: ELFCLASS64 + Data: ELFDATA2LSB + Type: ET_REL + Machine: EM_X86_64 +Sections: + - Name: .aaa + Type: SHT_PROGBITS + - Name: .ccc + Type: SHT_PROGBITS + - Name: .bbb + Type: SHT_PROGBITS + +--- !ELF +FileHeader: + Class: ELFCLASS64 + Data: ELFDATA2LSB + Type: ET_REL + Machine: EM_X86_64 +Sections: + - Name: .aaa + Type: SHT_PROGBITS + ShName: DEFAULT + - Name: .ccc + Type: SHT_PROGBITS + ShName: DEFAULT +## We do not use the TEST macro. It is exist to +## demonstrate the expected use case for the DEFAULT word. + - Name: .bbb + Type: SHT_PROGBITS + ShName: [[TEST=DEFAULT]] Index: llvm/test/tools/yaml2obj/ELF/override-shoffset.yaml =================================================================== --- llvm/test/tools/yaml2obj/ELF/override-shoffset.yaml +++ llvm/test/tools/yaml2obj/ELF/override-shoffset.yaml @@ -103,3 +103,43 @@ Type: SHT_PROGBITS ShOffset: 0xFFFF0000 Content: "fefefefefefefefe" + +## Check we can set the ShOffset to DEFAULT and this is a no-op. +# RUN: yaml2obj %s --docnum=4 -o %t4 +# RUN: llvm-readelf --sections %t4 | FileCheck %s --check-prefix=DEFAULT +# RUN: yaml2obj %s --docnum=5 -o %t5 +# RUN: llvm-readelf --sections %t5 | FileCheck %s --check-prefix=DEFAULT + +# DEFAULT: [Nr] Name Type Address Off +# DEFAULT: [ 1] .aaa PROGBITS 0000000000000000 000040 +# DEFAULT: [ 2] .bbb PROGBITS 0000000000000000 000050 + +--- !ELF +FileHeader: + Class: ELFCLASS64 + Data: ELFDATA2LSB + Type: ET_REL + Machine: EM_X86_64 +Sections: + - Name: .aaa + Type: SHT_PROGBITS + Size: 0x10 + - Name: .bbb + Type: SHT_PROGBITS + +--- !ELF +FileHeader: + Class: ELFCLASS64 + Data: ELFDATA2LSB + Type: ET_REL + Machine: EM_X86_64 +Sections: + - Name: .aaa + Type: SHT_PROGBITS + Size: 0x10 + ShOffset: DEFAULT +## We do not use the TEST macro. It is exist to +## demonstrate the expected use case for the DEFAULT word. + - Name: .bbb + Type: SHT_PROGBITS + ShOffset: [[TEST=DEFAULT]] Index: llvm/test/tools/yaml2obj/ELF/override-shsize.yaml =================================================================== --- llvm/test/tools/yaml2obj/ELF/override-shsize.yaml +++ llvm/test/tools/yaml2obj/ELF/override-shsize.yaml @@ -156,3 +156,29 @@ - Name: .zed Type: SHT_PROGBITS Content: "bbbb" + +## Check we can set the ShSize to DEFAULT and this is a no-op. +# RUN: yaml2obj %s --docnum=6 -o %t6 +# RUN: llvm-readelf --sections %t6 | FileCheck %s --check-prefix=DEFAULT + +# DEFAULT: [Nr] Name Type Address Off Size +# DEFAULT: [ 1] .aaa PROGBITS 0000000000000000 000040 000001 +# DEFAULT: [ 2] .bbb PROGBITS 0000000000000000 000041 000002 + +--- !ELF +FileHeader: + Class: ELFCLASS64 + Data: ELFDATA2LSB + Type: ET_REL + Machine: EM_X86_64 +Sections: + - Name: .aaa + Type: SHT_PROGBITS + Size: 0x1 + ShOffset: DEFAULT +## We do not use the TEST macro. It is exist to +## demonstrate the expected use case for the DEFAULT word. + - Name: .bbb + Type: SHT_PROGBITS + Size: 0x2 + ShOffset: [[TEST=DEFAULT]]