From 47f84b1a3cb2bf9335d9a68d8528babed0dbba9f Mon Sep 17 00:00:00 2001 From: George Rimar Date: Thu, 18 May 2017 08:25:11 +0000 Subject: [PATCH] [DWARF] - Simplify RelocVisitor implementation. We do not need to store relocation width field. Patch removes relative code, that simplifies implementation. Differential revision: https://reviews.llvm.org/D33274 llvm-svn: 303335 --- llvm/include/llvm/DebugInfo/DWARF/DWARFRelocMap.h | 2 +- llvm/include/llvm/Object/RelocVisitor.h | 231 +++++++++------------- llvm/lib/DebugInfo/DWARF/DWARFContext.cpp | 4 +- 3 files changed, 99 insertions(+), 138 deletions(-) diff --git a/llvm/include/llvm/DebugInfo/DWARF/DWARFRelocMap.h b/llvm/include/llvm/DebugInfo/DWARF/DWARFRelocMap.h index ec0397a..fabacc0 100644 --- a/llvm/include/llvm/DebugInfo/DWARF/DWARFRelocMap.h +++ b/llvm/include/llvm/DebugInfo/DWARF/DWARFRelocMap.h @@ -17,7 +17,7 @@ namespace llvm { struct RelocAddrEntry { - int64_t Value; + uint64_t Value; }; /// In place of applying the relocations to the data we've read from disk we use diff --git a/llvm/include/llvm/Object/RelocVisitor.h b/llvm/include/llvm/Object/RelocVisitor.h index 73c7ce3..86579b7 100644 --- a/llvm/include/llvm/Object/RelocVisitor.h +++ b/llvm/include/llvm/Object/RelocVisitor.h @@ -32,18 +32,6 @@ namespace llvm { namespace object { -struct RelocToApply { - // The computed value after applying the relevant relocations. - int64_t Value = 0; - - // The width of the value; how many bytes to touch when applying the - // relocation. - char Width = 0; - - RelocToApply() = default; - RelocToApply(int64_t Value, char Width) : Value(Value), Width(Width) {} -}; - /// @brief Base class for object file relocation visitors. class RelocVisitor { public: @@ -52,7 +40,7 @@ public: // TODO: Should handle multiple applied relocations via either passing in the // previously computed value or just count paired relocations as a single // visit. - RelocToApply visit(uint32_t RelocType, RelocationRef R, uint64_t Value = 0) { + uint64_t visit(uint32_t RelocType, RelocationRef R, uint64_t Value = 0) { if (isa(ObjToVisit)) return visitELF(RelocType, R, Value); if (isa(ObjToVisit)) @@ -61,7 +49,7 @@ public: return visitMachO(RelocType, R, Value); HasError = true; - return RelocToApply(); + return 0; } bool error() { return HasError; } @@ -70,7 +58,7 @@ private: const ObjectFile &ObjToVisit; bool HasError = false; - RelocToApply visitELF(uint32_t RelocType, RelocationRef R, uint64_t Value) { + uint64_t visitELF(uint32_t RelocType, RelocationRef R, uint64_t Value) { if (ObjToVisit.getBytesInAddress() == 8) { // 64-bit object file switch (ObjToVisit.getArch()) { case Triple::x86_64: @@ -87,7 +75,7 @@ private: return visitELF_X86_64_32S(R, Value); default: HasError = true; - return RelocToApply(); + return 0; } case Triple::aarch64: case Triple::aarch64_be: @@ -98,7 +86,7 @@ private: return visitELF_AARCH64_ABS64(R, Value); default: HasError = true; - return RelocToApply(); + return 0; } case Triple::bpfel: case Triple::bpfeb: @@ -109,7 +97,7 @@ private: return visitELF_BPF_64_32(R, Value); default: HasError = true; - return RelocToApply(); + return 0; } case Triple::mips64el: case Triple::mips64: @@ -120,7 +108,7 @@ private: return visitELF_MIPS64_64(R, Value); default: HasError = true; - return RelocToApply(); + return 0; } case Triple::ppc64le: case Triple::ppc64: @@ -131,7 +119,7 @@ private: return visitELF_PPC64_ADDR64(R, Value); default: HasError = true; - return RelocToApply(); + return 0; } case Triple::systemz: switch (RelocType) { @@ -141,7 +129,7 @@ private: return visitELF_390_64(R, Value); default: HasError = true; - return RelocToApply(); + return 0; } case Triple::sparcv9: switch (RelocType) { @@ -153,7 +141,7 @@ private: return visitELF_SPARCV9_64(R, Value); default: HasError = true; - return RelocToApply(); + return 0; } case Triple::amdgcn: switch (RelocType) { @@ -163,11 +151,11 @@ private: return visitELF_AMDGPU_ABS64(R, Value); default: HasError = true; - return RelocToApply(); + return 0; } default: HasError = true; - return RelocToApply(); + return 0; } } else if (ObjToVisit.getBytesInAddress() == 4) { // 32-bit object file switch (ObjToVisit.getArch()) { @@ -181,7 +169,7 @@ private: return visitELF_386_PC32(R, Value); default: HasError = true; - return RelocToApply(); + return 0; } case Triple::ppc: switch (RelocType) { @@ -189,14 +177,14 @@ private: return visitELF_PPC_ADDR32(R, Value); default: HasError = true; - return RelocToApply(); + return 0; } case Triple::arm: case Triple::armeb: switch (RelocType) { default: HasError = true; - return RelocToApply(); + return 0; case ELF::R_ARM_ABS32: return visitELF_ARM_ABS32(R, Value); } @@ -206,7 +194,7 @@ private: return visitELF_Lanai_32(R, Value); default: HasError = true; - return RelocToApply(); + return 0; } case Triple::mipsel: case Triple::mips: @@ -215,7 +203,7 @@ private: return visitELF_MIPS_32(R, Value); default: HasError = true; - return RelocToApply(); + return 0; } case Triple::sparc: switch (RelocType) { @@ -224,7 +212,7 @@ private: return visitELF_SPARC_32(R, Value); default: HasError = true; - return RelocToApply(); + return 0; } case Triple::hexagon: switch (RelocType) { @@ -232,18 +220,18 @@ private: return visitELF_HEX_32(R, Value); default: HasError = true; - return RelocToApply(); + return 0; } default: HasError = true; - return RelocToApply(); + return 0; } } else { report_fatal_error("Invalid word size in object file"); } } - RelocToApply visitCOFF(uint32_t RelocType, RelocationRef R, uint64_t Value) { + uint64_t visitCOFF(uint32_t RelocType, RelocationRef R, uint64_t Value) { switch (ObjToVisit.getArch()) { case Triple::x86: switch (RelocType) { @@ -263,10 +251,10 @@ private: break; } HasError = true; - return RelocToApply(); + return 0; } - RelocToApply visitMachO(uint32_t RelocType, RelocationRef R, uint64_t Value) { + uint64_t visitMachO(uint32_t RelocType, RelocationRef R, uint64_t Value) { switch (ObjToVisit.getArch()) { default: break; case Triple::x86_64: @@ -277,7 +265,7 @@ private: } } HasError = true; - return RelocToApply(); + return 0; } int64_t getELFAddend(RelocationRef R) { @@ -287,108 +275,88 @@ private: return *AddendOrErr; } - uint8_t getLengthMachO64(RelocationRef R) { - const MachOObjectFile *Obj = cast(R.getObject()); - return Obj->getRelocationLength(R.getRawDataRefImpl()); - } - /// Operations /// 386-ELF - RelocToApply visitELF_386_NONE(RelocationRef R) { - return RelocToApply(0, 0); + uint64_t visitELF_386_NONE(RelocationRef R) { + return 0; } // Ideally the Addend here will be the addend in the data for // the relocation. It's not actually the case for Rel relocations. - RelocToApply visitELF_386_32(RelocationRef R, uint64_t Value) { - return RelocToApply(Value, 4); + uint64_t visitELF_386_32(RelocationRef R, uint64_t Value) { + return Value; } - RelocToApply visitELF_386_PC32(RelocationRef R, uint64_t Value) { - uint64_t Address = R.getOffset(); - return RelocToApply(Value - Address, 4); + uint64_t visitELF_386_PC32(RelocationRef R, uint64_t Value) { + return Value - R.getOffset(); } /// X86-64 ELF - RelocToApply visitELF_X86_64_NONE(RelocationRef R) { - return RelocToApply(0, 0); + uint64_t visitELF_X86_64_NONE(RelocationRef R) { + return 0; } - RelocToApply visitELF_X86_64_64(RelocationRef R, uint64_t Value) { - int64_t Addend = getELFAddend(R); - return RelocToApply(Value + Addend, 8); + + uint64_t visitELF_X86_64_64(RelocationRef R, uint64_t Value) { + return Value + getELFAddend(R); } - RelocToApply visitELF_X86_64_PC32(RelocationRef R, uint64_t Value) { - int64_t Addend = getELFAddend(R); - uint64_t Address = R.getOffset(); - return RelocToApply(Value + Addend - Address, 4); + + uint64_t visitELF_X86_64_PC32(RelocationRef R, uint64_t Value) { + return Value + getELFAddend(R) - R.getOffset(); } - RelocToApply visitELF_X86_64_32(RelocationRef R, uint64_t Value) { - int64_t Addend = getELFAddend(R); - uint32_t Res = (Value + Addend) & 0xFFFFFFFF; - return RelocToApply(Res, 4); + + uint64_t visitELF_X86_64_32(RelocationRef R, uint64_t Value) { + return (Value + getELFAddend(R)) & 0xFFFFFFFF; } - RelocToApply visitELF_X86_64_32S(RelocationRef R, uint64_t Value) { - int64_t Addend = getELFAddend(R); - int32_t Res = (Value + Addend) & 0xFFFFFFFF; - return RelocToApply(Res, 4); + + uint64_t visitELF_X86_64_32S(RelocationRef R, uint64_t Value) { + return (Value + getELFAddend(R)) & 0xFFFFFFFF; } /// BPF ELF - RelocToApply visitELF_BPF_64_32(RelocationRef R, uint64_t Value) { - uint32_t Res = Value & 0xFFFFFFFF; - return RelocToApply(Res, 4); + uint64_t visitELF_BPF_64_32(RelocationRef R, uint64_t Value) { + return Value & 0xFFFFFFFF; } - RelocToApply visitELF_BPF_64_64(RelocationRef R, uint64_t Value) { - return RelocToApply(Value, 8); + + uint64_t visitELF_BPF_64_64(RelocationRef R, uint64_t Value) { + return Value; } /// PPC64 ELF - RelocToApply visitELF_PPC64_ADDR32(RelocationRef R, uint64_t Value) { - int64_t Addend = getELFAddend(R); - uint32_t Res = (Value + Addend) & 0xFFFFFFFF; - return RelocToApply(Res, 4); + uint64_t visitELF_PPC64_ADDR32(RelocationRef R, uint64_t Value) { + return (Value + getELFAddend(R)) & 0xFFFFFFFF; } - RelocToApply visitELF_PPC64_ADDR64(RelocationRef R, uint64_t Value) { - int64_t Addend = getELFAddend(R); - return RelocToApply(Value + Addend, 8); + + uint64_t visitELF_PPC64_ADDR64(RelocationRef R, uint64_t Value) { + return Value + getELFAddend(R); } /// PPC32 ELF - RelocToApply visitELF_PPC_ADDR32(RelocationRef R, uint64_t Value) { - int64_t Addend = getELFAddend(R); - uint32_t Res = (Value + Addend) & 0xFFFFFFFF; - return RelocToApply(Res, 4); + uint64_t visitELF_PPC_ADDR32(RelocationRef R, uint64_t Value) { + return (Value + getELFAddend(R)) & 0xFFFFFFFF; } /// Lanai ELF - RelocToApply visitELF_Lanai_32(RelocationRef R, uint64_t Value) { - int64_t Addend = getELFAddend(R); - uint32_t Res = (Value + Addend) & 0xFFFFFFFF; - return RelocToApply(Res, 4); + uint64_t visitELF_Lanai_32(RelocationRef R, uint64_t Value) { + return (Value + getELFAddend(R)) & 0xFFFFFFFF; } /// MIPS ELF - RelocToApply visitELF_MIPS_32(RelocationRef R, uint64_t Value) { - uint32_t Res = Value & 0xFFFFFFFF; - return RelocToApply(Res, 4); + uint64_t visitELF_MIPS_32(RelocationRef R, uint64_t Value) { + return Value & 0xFFFFFFFF; } /// MIPS64 ELF - RelocToApply visitELF_MIPS64_32(RelocationRef R, uint64_t Value) { - int64_t Addend = getELFAddend(R); - uint32_t Res = (Value + Addend) & 0xFFFFFFFF; - return RelocToApply(Res, 4); + uint64_t visitELF_MIPS64_32(RelocationRef R, uint64_t Value) { + return (Value + getELFAddend(R)) & 0xFFFFFFFF; } - RelocToApply visitELF_MIPS64_64(RelocationRef R, uint64_t Value) { - int64_t Addend = getELFAddend(R); - uint64_t Res = (Value + Addend); - return RelocToApply(Res, 8); + uint64_t visitELF_MIPS64_64(RelocationRef R, uint64_t Value) { + return Value + getELFAddend(R); } // AArch64 ELF - RelocToApply visitELF_AARCH64_ABS32(RelocationRef R, uint64_t Value) { + uint64_t visitELF_AARCH64_ABS32(RelocationRef R, uint64_t Value) { int64_t Addend = getELFAddend(R); int64_t Res = Value + Addend; @@ -396,16 +364,15 @@ private: if (Res < INT32_MIN || Res > UINT32_MAX) HasError = true; - return RelocToApply(static_cast(Res), 4); + return static_cast(Res); } - RelocToApply visitELF_AARCH64_ABS64(RelocationRef R, uint64_t Value) { - int64_t Addend = getELFAddend(R); - return RelocToApply(Value + Addend, 8); + uint64_t visitELF_AARCH64_ABS64(RelocationRef R, uint64_t Value) { + return Value + getELFAddend(R); } // SystemZ ELF - RelocToApply visitELF_390_32(RelocationRef R, uint64_t Value) { + uint64_t visitELF_390_32(RelocationRef R, uint64_t Value) { int64_t Addend = getELFAddend(R); int64_t Res = Value + Addend; @@ -413,77 +380,71 @@ private: if (Res < INT32_MIN || Res > UINT32_MAX) HasError = true; - return RelocToApply(static_cast(Res), 4); + return static_cast(Res); } - RelocToApply visitELF_390_64(RelocationRef R, uint64_t Value) { - int64_t Addend = getELFAddend(R); - return RelocToApply(Value + Addend, 8); + uint64_t visitELF_390_64(RelocationRef R, uint64_t Value) { + return Value + getELFAddend(R); } - RelocToApply visitELF_SPARC_32(RelocationRef R, uint32_t Value) { - int32_t Addend = getELFAddend(R); - return RelocToApply(Value + Addend, 4); + uint64_t visitELF_SPARC_32(RelocationRef R, uint32_t Value) { + return Value + getELFAddend(R); } - RelocToApply visitELF_SPARCV9_32(RelocationRef R, uint64_t Value) { - int32_t Addend = getELFAddend(R); - return RelocToApply(Value + Addend, 4); + uint64_t visitELF_SPARCV9_32(RelocationRef R, uint64_t Value) { + return Value + getELFAddend(R); } - RelocToApply visitELF_SPARCV9_64(RelocationRef R, uint64_t Value) { - int64_t Addend = getELFAddend(R); - return RelocToApply(Value + Addend, 8); + uint64_t visitELF_SPARCV9_64(RelocationRef R, uint64_t Value) { + return Value + getELFAddend(R); } - RelocToApply visitELF_ARM_ABS32(RelocationRef R, uint64_t Value) { + uint64_t visitELF_ARM_ABS32(RelocationRef R, uint64_t Value) { int64_t Res = Value; // Overflow check allows for both signed and unsigned interpretation. if (Res < INT32_MIN || Res > UINT32_MAX) HasError = true; - return RelocToApply(static_cast(Res), 4); + return static_cast(Res); } - RelocToApply visitELF_HEX_32(RelocationRef R, uint64_t Value) { + uint64_t visitELF_HEX_32(RelocationRef R, uint64_t Value) { int64_t Addend = getELFAddend(R); - return RelocToApply(Value + Addend, 4); + return Value + Addend; } - RelocToApply visitELF_AMDGPU_ABS32(RelocationRef R, uint64_t Value) { + uint64_t visitELF_AMDGPU_ABS32(RelocationRef R, uint64_t Value) { int64_t Addend = getELFAddend(R); - return RelocToApply(Value + Addend, 4); + return Value + Addend; } - RelocToApply visitELF_AMDGPU_ABS64(RelocationRef R, uint64_t Value) { + uint64_t visitELF_AMDGPU_ABS64(RelocationRef R, uint64_t Value) { int64_t Addend = getELFAddend(R); - return RelocToApply(Value + Addend, 8); + return Value + Addend; } /// I386 COFF - RelocToApply visitCOFF_I386_SECREL(RelocationRef R, uint64_t Value) { - return RelocToApply(static_cast(Value), /*Width=*/4); + uint64_t visitCOFF_I386_SECREL(RelocationRef R, uint64_t Value) { + return static_cast(Value); } - RelocToApply visitCOFF_I386_DIR32(RelocationRef R, uint64_t Value) { - return RelocToApply(static_cast(Value), /*Width=*/4); + uint64_t visitCOFF_I386_DIR32(RelocationRef R, uint64_t Value) { + return static_cast(Value); } /// AMD64 COFF - RelocToApply visitCOFF_AMD64_SECREL(RelocationRef R, uint64_t Value) { - return RelocToApply(static_cast(Value), /*Width=*/4); + uint64_t visitCOFF_AMD64_SECREL(RelocationRef R, uint64_t Value) { + return static_cast(Value); } - RelocToApply visitCOFF_AMD64_ADDR64(RelocationRef R, uint64_t Value) { - return RelocToApply(Value, /*Width=*/8); + uint64_t visitCOFF_AMD64_ADDR64(RelocationRef R, uint64_t Value) { + return Value; } // X86_64 MachO - RelocToApply visitMACHO_X86_64_UNSIGNED(RelocationRef R, uint64_t Value) { - uint8_t Length = getLengthMachO64(R); - Length = 1< Name; Reloc.getTypeName(Name); errs() << "error: failed to compute relocation: " << Name << "\n"; continue; } - Map->insert({Reloc.getOffset(), {R.Value}}); + Map->insert({Reloc.getOffset(), {Val}}); } } } -- 2.7.4