Index: include/llvm/Support/YAMLTraits.h =================================================================== --- include/llvm/Support/YAMLTraits.h +++ include/llvm/Support/YAMLTraits.h @@ -587,10 +587,12 @@ // allow anonymous enum values to be used with LLVM_YAML_STRONG_TYPEDEF template - void enumCase(T &Val, const char* Str, const uint32_t ConstVal) { + bool enumCase(T &Val, const char* Str, const uint32_t ConstVal) { if ( matchEnumScalar(Str, outputting() && Val == static_cast(ConstVal)) ) { Val = ConstVal; + return true; } + return false; } template Index: lib/ObjectYAML/ELFYAML.cpp =================================================================== --- lib/ObjectYAML/ELFYAML.cpp +++ lib/ObjectYAML/ELFYAML.cpp @@ -11,6 +11,7 @@ // //===----------------------------------------------------------------------===// +#include "llvm/ADT/StringExtras.h" #include "llvm/ObjectYAML/ELFYAML.h" #include "llvm/Support/Casting.h" #include "llvm/Support/MipsABIFlags.h" @@ -505,7 +506,7 @@ IO &IO, ELFYAML::ELF_REL &Value) { const auto *Object = static_cast(IO.getContext()); assert(Object && "The IO context is not initialized"); -#define ELF_RELOC(X, Y) IO.enumCase(Value, #X, ELF::X); +#define ELF_RELOC(X, Y) if (IO.enumCase(Value, #X, ELF::X)) return; switch (Object->Header.Machine) { case ELF::EM_X86_64: #include "llvm/BinaryFormat/ELFRelocs/x86_64.def" @@ -542,6 +543,19 @@ llvm_unreachable("Unsupported architecture"); } #undef ELF_RELOC + + // If we failed to match relocation by enum name, + // use integer representation instead. + if (!IO.outputting()) { + StringRef S; + IO.blockScalarString(S); + uint32_t Type; + if (to_integer(S, Type)) + IO.enumCase(Value, S.str().data(), Type); + } else { + std::string Val = std::to_string(Value); + IO.enumCase(Value, Val.data(), Value); + } } void ScalarEnumerationTraits::enumeration( Index: test/Object/X86/yaml-elf-x86-rel-broken.yaml =================================================================== --- test/Object/X86/yaml-elf-x86-rel-broken.yaml +++ test/Object/X86/yaml-elf-x86-rel-broken.yaml @@ -0,0 +1,48 @@ +# RUN: yaml2obj %s > %t +# RUN: obj2yaml %t | FileCheck %s + +# CHECK: --- !ELF +# CHECK-NEXT: FileHeader: +# CHECK-NEXT: Class: ELFCLASS32 +# CHECK-NEXT: Data: ELFDATA2LSB +# CHECK-NEXT: Type: ET_REL +# CHECK-NEXT: Machine: EM_386 +# CHECK-NEXT: Sections: +# CHECK-NEXT: - Name: .text +# CHECK-NEXT: Type: SHT_PROGBITS +# CHECK-NEXT: Flags: [ SHF_ALLOC, SHF_EXECINSTR ] +# CHECK-NEXT: AddressAlign: 0x0000000000000004 +# CHECK-NEXT: Content: '0000000000000000' +# CHECK-NEXT: - Name: .rel.text +# CHECK-NEXT: Type: SHT_REL +# CHECK-NEXT: Link: .symtab +# CHECK-NEXT: AddressAlign: 0x0000000000000004 +# CHECK-NEXT: Info: .text +# CHECK-NEXT: Relocations: +# CHECK-NEXT: - Offset: 0x0000000000000000 +# CHECK-NEXT: Symbol: '' +# CHECK-NEXT: Type: 255 +# CHECK-NEXT: Symbols: +# CHECK-NEXT: ... + +!ELF +FileHeader: + Class: ELFCLASS32 + Data: ELFDATA2LSB + Type: ET_REL + Machine: EM_386 +Sections: + - Type: SHT_PROGBITS + Name: .text + Flags: [ SHF_ALLOC, SHF_EXECINSTR ] + AddressAlign: 0x04 + Content: 0000000000000000 + - Type: SHT_REL + Name: .rel.text + Link: .symtab + Info: .text + AddressAlign: 0x04 + Relocations: + - Offset: 0 + Symbol: main + Type: 0xFF