obj2yaml: Use the correct relocation type for different machine types

The IO normalizer would essentially lump I386 and AMD64 relocations
together.  Relocation types with the same numeric value would then get
mapped in appropriately.

For example:
IMAGE_REL_AMD64_ADDR64 and IMAGE_REL_I386_DIR16 both have a numeric
value of one.  We would see IMAGE_REL_I386_DIR16 in obj2yaml conversions
of object files with a machine type of IMAGE_FILE_MACHINE_AMD64.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@205746 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
David Majnemer 2014-04-07 23:12:20 +00:00
parent b9a65fd002
commit de6256c938
6 changed files with 70 additions and 14 deletions

View File

@ -121,8 +121,13 @@ struct ScalarEnumerationTraits<COFF::SymbolComplexType> {
};
template <>
struct ScalarEnumerationTraits<COFF::RelocationTypeX86> {
static void enumeration(IO &IO, COFF::RelocationTypeX86 &Value);
struct ScalarEnumerationTraits<COFF::RelocationTypeI386> {
static void enumeration(IO &IO, COFF::RelocationTypeI386 &Value);
};
template <>
struct ScalarEnumerationTraits<COFF::RelocationTypeAMD64> {
static void enumeration(IO &IO, COFF::RelocationTypeAMD64 &Value);
};
template <>

View File

@ -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,

View File

@ -132,8 +132,8 @@ void ScalarEnumerationTraits<COFF::SymbolComplexType>::enumeration(
ECase(IMAGE_SYM_DTYPE_ARRAY);
}
void ScalarEnumerationTraits<COFF::RelocationTypeX86>::enumeration(
IO &IO, COFF::RelocationTypeX86 &Value) {
void ScalarEnumerationTraits<COFF::RelocationTypeI386>::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<COFF::RelocationTypeX86>::enumeration(
ECase(IMAGE_REL_I386_TOKEN);
ECase(IMAGE_REL_I386_SECREL7);
ECase(IMAGE_REL_I386_REL32);
}
void ScalarEnumerationTraits<COFF::RelocationTypeAMD64>::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 <typename RelocType>
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<COFFYAML::Relocation>::mapping(IO &IO,
COFFYAML::Relocation &Rel) {
MappingNormalization<NType, uint16_t> NT(IO, Rel.Type);
IO.mapRequired("VirtualAddress", Rel.VirtualAddress);
IO.mapRequired("SymbolName", Rel.SymbolName);
IO.mapRequired("Type", NT->Type);
COFF::header &H = *static_cast<COFF::header *>(IO.getContext());
if (H.Machine == COFF::IMAGE_FILE_MACHINE_I386) {
MappingNormalization<NType<COFF::RelocationTypeI386>, uint16_t> NT(
IO, Rel.Type);
IO.mapRequired("Type", NT->Type);
} else if (H.Machine == COFF::IMAGE_FILE_MACHINE_AMD64) {
MappingNormalization<NType<COFF::RelocationTypeAMD64>, uint16_t> NT(
IO, Rel.Type);
IO.mapRequired("Type", NT->Type);
} else {
IO.mapRequired("Type", Rel.Type);
}
}
void MappingTraits<COFF::header>::mapping(IO &IO, COFF::header &H) {
@ -297,6 +312,7 @@ void MappingTraits<COFF::header>::mapping(IO &IO, COFF::header &H) {
IO.mapRequired("Machine", NM->Machine);
IO.mapOptional("Characteristics", NC->Characteristics);
IO.setContext(static_cast<void *>(&H));
}
void MappingTraits<COFF::AuxiliaryFunctionDefinition>::mapping(

View File

@ -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)

View File

@ -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