diff --git a/include/llvm/Object/COFFYAML.h b/include/llvm/Object/COFFYAML.h index b5f9cccf85d..3f48e07f575 100644 --- a/include/llvm/Object/COFFYAML.h +++ b/include/llvm/Object/COFFYAML.h @@ -121,8 +121,13 @@ struct ScalarEnumerationTraits { }; template <> -struct ScalarEnumerationTraits { - static void enumeration(IO &IO, COFF::RelocationTypeX86 &Value); +struct ScalarEnumerationTraits { + static void enumeration(IO &IO, COFF::RelocationTypeI386 &Value); +}; + +template <> +struct ScalarEnumerationTraits { + static void enumeration(IO &IO, COFF::RelocationTypeAMD64 &Value); }; template <> diff --git a/include/llvm/Support/COFF.h b/include/llvm/Support/COFF.h index dca7fc6ee89..f0e5c7dc628 100644 --- a/include/llvm/Support/COFF.h +++ b/include/llvm/Support/COFF.h @@ -275,7 +275,7 @@ namespace COFF { uint16_t Type; }; - enum RelocationTypeX86 { + enum RelocationTypeI386 { IMAGE_REL_I386_ABSOLUTE = 0x0000, IMAGE_REL_I386_DIR16 = 0x0001, IMAGE_REL_I386_REL16 = 0x0002, @@ -286,8 +286,10 @@ namespace COFF { IMAGE_REL_I386_SECREL = 0x000B, IMAGE_REL_I386_TOKEN = 0x000C, IMAGE_REL_I386_SECREL7 = 0x000D, - IMAGE_REL_I386_REL32 = 0x0014, + IMAGE_REL_I386_REL32 = 0x0014 + }; + enum RelocationTypeAMD64 { IMAGE_REL_AMD64_ABSOLUTE = 0x0000, IMAGE_REL_AMD64_ADDR64 = 0x0001, IMAGE_REL_AMD64_ADDR32 = 0x0002, diff --git a/lib/Object/COFFYAML.cpp b/lib/Object/COFFYAML.cpp index 94b72ffcbf3..d05576fc49d 100644 --- a/lib/Object/COFFYAML.cpp +++ b/lib/Object/COFFYAML.cpp @@ -132,8 +132,8 @@ void ScalarEnumerationTraits::enumeration( ECase(IMAGE_SYM_DTYPE_ARRAY); } -void ScalarEnumerationTraits::enumeration( - IO &IO, COFF::RelocationTypeX86 &Value) { +void ScalarEnumerationTraits::enumeration( + IO &IO, COFF::RelocationTypeI386 &Value) { ECase(IMAGE_REL_I386_ABSOLUTE); ECase(IMAGE_REL_I386_DIR16); ECase(IMAGE_REL_I386_REL16); @@ -145,6 +145,10 @@ void ScalarEnumerationTraits::enumeration( ECase(IMAGE_REL_I386_TOKEN); ECase(IMAGE_REL_I386_SECREL7); ECase(IMAGE_REL_I386_REL32); +} + +void ScalarEnumerationTraits::enumeration( + IO &IO, COFF::RelocationTypeAMD64 &Value) { ECase(IMAGE_REL_AMD64_ABSOLUTE); ECase(IMAGE_REL_AMD64_ADDR64); ECase(IMAGE_REL_AMD64_ADDR32); @@ -272,22 +276,33 @@ struct NHeaderCharacteristics { COFF::Characteristics Characteristics; }; +template struct NType { - NType(IO &) : Type(COFF::RelocationTypeX86(0)) {} - NType(IO &, uint16_t T) : Type(COFF::RelocationTypeX86(T)) {} + NType(IO &) : Type(RelocType(0)) {} + NType(IO &, uint16_t T) : Type(RelocType(T)) {} uint16_t denormalize(IO &) { return Type; } - COFF::RelocationTypeX86 Type; + RelocType Type; }; } void MappingTraits::mapping(IO &IO, COFFYAML::Relocation &Rel) { - MappingNormalization NT(IO, Rel.Type); - IO.mapRequired("VirtualAddress", Rel.VirtualAddress); IO.mapRequired("SymbolName", Rel.SymbolName); - IO.mapRequired("Type", NT->Type); + + COFF::header &H = *static_cast(IO.getContext()); + if (H.Machine == COFF::IMAGE_FILE_MACHINE_I386) { + MappingNormalization, uint16_t> NT( + IO, Rel.Type); + IO.mapRequired("Type", NT->Type); + } else if (H.Machine == COFF::IMAGE_FILE_MACHINE_AMD64) { + MappingNormalization, uint16_t> NT( + IO, Rel.Type); + IO.mapRequired("Type", NT->Type); + } else { + IO.mapRequired("Type", Rel.Type); + } } void MappingTraits::mapping(IO &IO, COFF::header &H) { @@ -297,6 +312,7 @@ void MappingTraits::mapping(IO &IO, COFF::header &H) { IO.mapRequired("Machine", NM->Machine); IO.mapOptional("Characteristics", NC->Characteristics); + IO.setContext(static_cast(&H)); } void MappingTraits::mapping( diff --git a/test/Object/Inputs/COFF/x86-64.yaml b/test/Object/Inputs/COFF/x86-64.yaml index 1dc2b10cf41..b8a863a429c 100644 --- a/test/Object/Inputs/COFF/x86-64.yaml +++ b/test/Object/Inputs/COFF/x86-64.yaml @@ -30,6 +30,16 @@ sections: Characteristics: [IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_READ, IMAGE_SCN_MEM_WRITE, ] # 0xc0100040 SectionData: !hex "48656C6C6F20576F726C642100" # |Hello World!.| + - !Section + Name: '.CRT$XCU' + Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_READ ] + Alignment: 8 + SectionData: !hex "0000000000000000" + Relocations: + - VirtualAddress: 0 + SymbolName: '??__Ex@@YAXXZ' + Type: IMAGE_REL_AMD64_ADDR64 + symbols: - !Symbol Name: .text @@ -91,3 +101,10 @@ symbols: ComplexType: IMAGE_SYM_DTYPE_NULL # (0) StorageClass: IMAGE_SYM_CLASS_EXTERNAL # (2) + - !Symbol + Name: '??__Ex@@YAXXZ' + Value: 0 + SectionNumber: 3 + SimpleType: IMAGE_SYM_TYPE_NULL # (0) + ComplexType: IMAGE_SYM_DTYPE_FUNCTION # (2) + StorageClass: IMAGE_SYM_CLASS_STATIC # (3) diff --git a/test/Object/Inputs/trivial-object-test.coff-x86-64 b/test/Object/Inputs/trivial-object-test.coff-x86-64 index 077591482ce..ed144d1265f 100644 Binary files a/test/Object/Inputs/trivial-object-test.coff-x86-64 and b/test/Object/Inputs/trivial-object-test.coff-x86-64 differ diff --git a/test/Object/obj2yaml.test b/test/Object/obj2yaml.test index d96275fe2cc..5ab8a152242 100644 --- a/test/Object/obj2yaml.test +++ b/test/Object/obj2yaml.test @@ -112,6 +112,16 @@ COFF-X86-64-NEXT: Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_S COFF-X86-64-NEXT: Alignment: 1 COFF-X86-64-NEXT: SectionData: 48656C6C6F20576F726C642100 +COFF-X86-64: - Name: '.CRT$XCU' +COFF-X86-64-NEXT: Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_READ ] +COFF-X86-64-NEXT: Alignment: 8 +COFF-X86-64-NEXT: SectionData: 0000000000000000 + +COFF-X86-64: Relocations: +COFF-X86-64-NEXT: - VirtualAddress: 0 +COFF-X86-64-NEXT: SymbolName: '??__Ex@@YAXXZ' +COFF-X86-64-NEXT: Type: IMAGE_REL_AMD64_ADDR64 + COFF-X86-64: symbols: COFF-X86-64-NEXT: - Name: .text COFF-X86-64-NEXT: Value: 0 @@ -143,7 +153,7 @@ COFF-X86-64: - Name: main COFF-X86-64-NEXT: Value: 0 COFF-X86-64-NEXT: SectionNumber: 1 COFF-X86-64-NEXT: SimpleType: IMAGE_SYM_TYPE_NULL -COFF-X86-64-NEXT: ComplexType: IMAGE_SYM_DTYPE_NULL +COFF-X86-64-NEXT: ComplexType: IMAGE_SYM_DTYPE_FUNCTION COFF-X86-64-NEXT: StorageClass: IMAGE_SYM_CLASS_EXTERNAL COFF-X86-64: - Name: L.str @@ -166,4 +176,10 @@ COFF-X86-64-NEXT: SectionNumber: 0 COFF-X86-64-NEXT: SimpleType: IMAGE_SYM_TYPE_NULL COFF-X86-64-NEXT: ComplexType: IMAGE_SYM_DTYPE_NULL COFF-X86-64-NEXT: StorageClass: IMAGE_SYM_CLASS_EXTERNAL -COFF-X86-64-NOT: NumberOfAuxSymbols + +COFF-X86-64: - Name: '??__Ex@@YAXXZ' +COFF-X86-64-NEXT: Value: 0 +COFF-X86-64-NEXT: SectionNumber: 3 +COFF-X86-64-NEXT: SimpleType: IMAGE_SYM_TYPE_NULL +COFF-X86-64-NEXT: ComplexType: IMAGE_SYM_DTYPE_FUNCTION +COFF-X86-64-NEXT: StorageClass: IMAGE_SYM_CLASS_STATIC