Index: include/llvm/ObjectYAML/ELFYAML.h =================================================================== --- include/llvm/ObjectYAML/ELFYAML.h +++ include/llvm/ObjectYAML/ELFYAML.h @@ -68,6 +68,7 @@ ELF_ELFCLASS Class; ELF_ELFDATA Data; ELF_ELFOSABI OSABI; + llvm::yaml::Hex8 ABIVersion; ELF_ET Type; ELF_EM Machine; ELF_EF Flags; Index: lib/ObjectYAML/ELFYAML.cpp =================================================================== --- lib/ObjectYAML/ELFYAML.cpp +++ lib/ObjectYAML/ELFYAML.cpp @@ -753,6 +753,7 @@ IO.mapRequired("Class", FileHdr.Class); IO.mapRequired("Data", FileHdr.Data); IO.mapOptional("OSABI", FileHdr.OSABI, ELFYAML::ELF_ELFOSABI(0)); + IO.mapOptional("ABIVersion", FileHdr.ABIVersion, Hex8(0)); IO.mapRequired("Type", FileHdr.Type); IO.mapRequired("Machine", FileHdr.Machine); IO.mapOptional("Flags", FileHdr.Flags, ELFYAML::ELF_EF(0)); Index: test/tools/llvm-objcopy/ELF/copy-osabi.test =================================================================== --- /dev/null +++ test/tools/llvm-objcopy/ELF/copy-osabi.test @@ -0,0 +1,16 @@ +# RUN: yaml2obj %s > %t +# RUN: llvm-objcopy %t %t2 +# RUN: llvm-readobj -file-headers %t2 | FileCheck %s + +## Check that llvm-objcopy preserves the OSABI and ABIVersion fields. +# CHECK: OS/ABI: FreeBSD (0x9) +# CHECK: ABIVersion: 1 + +!ELF +FileHeader: + Class: ELFCLASS64 + Data: ELFDATA2LSB + OSABI: ELFOSABI_FREEBSD + ABIVersion: 1 + Type: ET_REL + Machine: EM_AARCH64 Index: test/tools/yaml2obj/elf-abiversion.yaml =================================================================== --- /dev/null +++ test/tools/yaml2obj/elf-abiversion.yaml @@ -0,0 +1,16 @@ +# RUN: yaml2obj %s -o %t +# RUN: llvm-readobj -file-headers %t | FileCheck %s --check-prefix=FROMYAML +# RUN: obj2yaml %t | FileCheck %s --check-prefix=TOYAML + +## Check we are able to parse/emit the ABI version with yaml2obj/obj2yaml. + +# FROMYAML: ABIVersion: 5 +# TOYAML: ABIVersion: 0x05 + +--- !ELF +FileHeader: + Class: ELFCLASS64 + Data: ELFDATA2LSB + Type: ET_REL + Machine: EM_X86_64 + ABIVersion: 0x05 Index: tools/llvm-objcopy/ELF/Object.h =================================================================== --- tools/llvm-objcopy/ELF/Object.h +++ tools/llvm-objcopy/ELF/Object.h @@ -733,6 +733,8 @@ Segment ElfHdrSegment; Segment ProgramHdrSegment; + uint8_t OSABI; + uint8_t ABIVersion; uint64_t Entry; uint64_t SHOffset; uint32_t Type; Index: tools/llvm-objcopy/ELF/Object.cpp =================================================================== --- tools/llvm-objcopy/ELF/Object.cpp +++ tools/llvm-objcopy/ELF/Object.cpp @@ -703,6 +703,8 @@ template void BinaryELFBuilder::initFileHeader() { Obj->Flags = 0x0; Obj->Type = ET_REL; + Obj->OSABI = 0; + Obj->ABIVersion = 0; Obj->Entry = 0x0; Obj->Machine = EMachine; Obj->Version = 1; @@ -1086,6 +1088,8 @@ template void ELFBuilder::build() { const auto &Ehdr = *ElfFile.getHeader(); + Obj.OSABI = Ehdr.e_ident[EI_OSABI]; + Obj.ABIVersion = Ehdr.e_ident[EI_ABIVERSION]; Obj.Type = Ehdr.e_type; Obj.Machine = Ehdr.e_machine; Obj.Version = Ehdr.e_version; @@ -1162,8 +1166,8 @@ Ehdr.e_ident[EI_DATA] = ELFT::TargetEndianness == support::big ? ELFDATA2MSB : ELFDATA2LSB; Ehdr.e_ident[EI_VERSION] = EV_CURRENT; - Ehdr.e_ident[EI_OSABI] = ELFOSABI_NONE; - Ehdr.e_ident[EI_ABIVERSION] = 0; + Ehdr.e_ident[EI_OSABI] = Obj.OSABI; + Ehdr.e_ident[EI_ABIVERSION] = Obj.ABIVersion; Ehdr.e_type = Obj.Type; Ehdr.e_machine = Obj.Machine; Index: tools/obj2yaml/elf2yaml.cpp =================================================================== --- tools/obj2yaml/elf2yaml.cpp +++ tools/obj2yaml/elf2yaml.cpp @@ -114,6 +114,7 @@ Y->Header.Class = ELFYAML::ELF_ELFCLASS(Obj.getHeader()->getFileClass()); Y->Header.Data = ELFYAML::ELF_ELFDATA(Obj.getHeader()->getDataEncoding()); Y->Header.OSABI = Obj.getHeader()->e_ident[ELF::EI_OSABI]; + Y->Header.ABIVersion = Obj.getHeader()->e_ident[ELF::EI_ABIVERSION]; Y->Header.Type = Obj.getHeader()->e_type; Y->Header.Machine = Obj.getHeader()->e_machine; Y->Header.Flags = Obj.getHeader()->e_flags; Index: tools/yaml2obj/yaml2elf.cpp =================================================================== --- tools/yaml2obj/yaml2elf.cpp +++ tools/yaml2obj/yaml2elf.cpp @@ -196,7 +196,7 @@ Header.e_ident[EI_DATA] = IsLittleEndian ? ELFDATA2LSB : ELFDATA2MSB; Header.e_ident[EI_VERSION] = EV_CURRENT; Header.e_ident[EI_OSABI] = Doc.Header.OSABI; - Header.e_ident[EI_ABIVERSION] = 0; + Header.e_ident[EI_ABIVERSION] = Doc.Header.ABIVersion; Header.e_type = Doc.Header.Type; Header.e_machine = Doc.Header.Machine; Header.e_version = EV_CURRENT;