From: Pete Cooper Date: Wed, 30 Mar 2016 20:15:06 +0000 (+0000) Subject: Change getReferenceInfo/getPairReferenceInfo to use new Error handling. NFC. X-Git-Tag: llvmorg-3.9.0-rc1~10486 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=1e00911ee02b4394d35c1def5181bc1e182fde73;p=platform%2Fupstream%2Fllvm.git Change getReferenceInfo/getPairReferenceInfo to use new Error handling. NFC. Adds a GenericError class to lld/Core which can carry a string. This is analygous to the dynamic_error we currently use in lld/Core. Use this GenericError instead of make_dynamic_error_code. Also, provide an implemention of GenericError::convertToErrorCode which for now converts it in to the dynamic_error_code we used to have. This will go away once all the APIs are converted. llvm-svn: 264910 --- diff --git a/lld/include/lld/Core/Error.h b/lld/include/lld/Core/Error.h index 5e08046..8150ab8 100644 --- a/lld/include/lld/Core/Error.h +++ b/lld/include/lld/Core/Error.h @@ -15,6 +15,8 @@ #define LLD_CORE_ERROR_H #include "lld/Core/LLVM.h" +#include "llvm/ADT/Twine.h" +#include "llvm/Support/Error.h" #include namespace lld { @@ -40,6 +42,25 @@ std::error_code make_dynamic_error_code(const char *msg); std::error_code make_dynamic_error_code(StringRef msg); std::error_code make_dynamic_error_code(const Twine &msg); +/// Generic error. +/// +/// For errors that don't require their own specific sub-error (most errors) +/// this class can be used to describe the error via a string message. +class GenericError : public llvm::ErrorInfo { +public: + static char ID; + GenericError(Twine Msg); + const std::string &getMessage() const { return Msg; } + void log(llvm::raw_ostream &OS) const override; + + std::error_code convertToErrorCode() const override { + return make_dynamic_error_code(StringRef(getMessage())); + } + +private: + std::string Msg; +}; + } // end namespace lld namespace std { diff --git a/lld/lib/Core/Error.cpp b/lld/lib/Core/Error.cpp index 646f89d..097318e 100644 --- a/lld/lib/Core/Error.cpp +++ b/lld/lib/Core/Error.cpp @@ -88,4 +88,12 @@ std::error_code make_dynamic_error_code(const Twine &msg) { return std::error_code(categorySingleton.add(msg.str()), categorySingleton); } +char GenericError::ID = 0; + +GenericError::GenericError(Twine Msg) : Msg(Msg.str()) { } + +void GenericError::log(raw_ostream &OS) const { + OS << Msg; +} + } // namespace lld diff --git a/lld/lib/ReaderWriter/MachO/ArchHandler.h b/lld/lib/ReaderWriter/MachO/ArchHandler.h index 4e48633..70a63bd 100644 --- a/lld/lib/ReaderWriter/MachO/ArchHandler.h +++ b/lld/lib/ReaderWriter/MachO/ArchHandler.h @@ -14,6 +14,7 @@ #include "File.h" #include "MachONormalizedFile.h" #include "lld/Core/LLVM.h" +#include "lld/Core/Error.h" #include "lld/Core/Reference.h" #include "lld/Core/Simple.h" #include "lld/ReaderWriter/MachOLinkingContext.h" @@ -116,20 +117,20 @@ public: /// Prototype for a helper function. Given a sectionIndex and address, /// finds the atom and offset with that atom of that address. - typedef std::function FindAtomBySectionAndAddress; /// Prototype for a helper function. Given a symbolIndex, finds the atom /// representing that symbol. - typedef std::function FindAtomBySymbolIndex; /// Analyzes a relocation from a .o file and returns the info /// (kind, target, addend) needed to instantiate a Reference. /// Two helper functions are passed as parameters to find the target atom /// given a symbol index or address. - virtual std::error_code + virtual llvm::Error getReferenceInfo(const normalized::Relocation &reloc, const DefinedAtom *inAtom, uint32_t offsetInAtom, @@ -144,7 +145,7 @@ public: /// (kind, target, addend) needed to instantiate a Reference. /// Two helper functions are passed as parameters to find the target atom /// given a symbol index or address. - virtual std::error_code + virtual llvm::Error getPairReferenceInfo(const normalized::Relocation &reloc1, const normalized::Relocation &reloc2, const DefinedAtom *inAtom, diff --git a/lld/lib/ReaderWriter/MachO/ArchHandler_arm.cpp b/lld/lib/ReaderWriter/MachO/ArchHandler_arm.cpp index 51e2f3f..3286fe0 100644 --- a/lld/lib/ReaderWriter/MachO/ArchHandler_arm.cpp +++ b/lld/lib/ReaderWriter/MachO/ArchHandler_arm.cpp @@ -76,16 +76,16 @@ public: return -1; } - std::error_code getReferenceInfo(const normalized::Relocation &reloc, - const DefinedAtom *inAtom, - uint32_t offsetInAtom, - uint64_t fixupAddress, bool swap, - FindAtomBySectionAndAddress atomFromAddress, - FindAtomBySymbolIndex atomFromSymbolIndex, - Reference::KindValue *kind, - const lld::Atom **target, - Reference::Addend *addend) override; - std::error_code + llvm::Error getReferenceInfo(const normalized::Relocation &reloc, + const DefinedAtom *inAtom, + uint32_t offsetInAtom, + uint64_t fixupAddress, bool swap, + FindAtomBySectionAndAddress atomFromAddress, + FindAtomBySymbolIndex atomFromSymbolIndex, + Reference::KindValue *kind, + const lld::Atom **target, + Reference::Addend *addend) override; + llvm::Error getPairReferenceInfo(const normalized::Relocation &reloc1, const normalized::Relocation &reloc2, const DefinedAtom *inAtom, @@ -518,13 +518,12 @@ uint32_t ArchHandler_arm::clearThumbBit(uint32_t value, const Atom *target) { return value; } -std::error_code ArchHandler_arm::getReferenceInfo( +llvm::Error ArchHandler_arm::getReferenceInfo( const Relocation &reloc, const DefinedAtom *inAtom, uint32_t offsetInAtom, uint64_t fixupAddress, bool isBig, FindAtomBySectionAndAddress atomFromAddress, FindAtomBySymbolIndex atomFromSymbolIndex, Reference::KindValue *kind, const lld::Atom **target, Reference::Addend *addend) { - typedef std::error_code E; const uint8_t *fixupContent = &inAtom->rawContent()[offsetInAtom]; uint64_t targetAddress; uint32_t instruction = *(const ulittle32_t *)fixupContent; @@ -536,12 +535,12 @@ std::error_code ArchHandler_arm::getReferenceInfo( *kind = thumb_b22; else *kind = thumb_bl22; - if (E ec = atomFromSymbolIndex(reloc.symbol, target)) + if (auto ec = atomFromSymbolIndex(reloc.symbol, target)) return ec; // Instruction contains branch to addend. displacement = getDisplacementFromThumbBranch(instruction, fixupAddress); *addend = fixupAddress + 4 + displacement; - return std::error_code(); + return llvm::Error(); case ARM_THUMB_RELOC_BR22 | rPcRel | rLength4: // ex: bl _foo (and _foo is defined) if ((instruction & 0xD000F800) == 0x9000F000) @@ -559,12 +558,12 @@ std::error_code ArchHandler_arm::getReferenceInfo( *kind = thumb_bl22; displacement = getDisplacementFromThumbBranch(instruction, fixupAddress); targetAddress = fixupAddress + 4 + displacement; - if (E ec = atomFromAddress(0, reloc.value, target, addend)) + if (auto ec = atomFromAddress(0, reloc.value, target, addend)) return ec; // reloc.value is target atom's address. Instruction contains branch // to atom+addend. *addend += (targetAddress - reloc.value); - return std::error_code(); + return llvm::Error(); case ARM_RELOC_BR24 | rPcRel | rExtern | rLength4: // ex: bl _foo (and _foo is undefined) if (((instruction & 0x0F000000) == 0x0A000000) @@ -572,12 +571,12 @@ std::error_code ArchHandler_arm::getReferenceInfo( *kind = arm_b24; else *kind = arm_bl24; - if (E ec = atomFromSymbolIndex(reloc.symbol, target)) + if (auto ec = atomFromSymbolIndex(reloc.symbol, target)) return ec; // Instruction contains branch to addend. displacement = getDisplacementFromArmBranch(instruction); *addend = fixupAddress + 8 + displacement; - return std::error_code(); + return llvm::Error(); case ARM_RELOC_BR24 | rPcRel | rLength4: // ex: bl _foo (and _foo is defined) if (((instruction & 0x0F000000) == 0x0A000000) @@ -597,40 +596,40 @@ std::error_code ArchHandler_arm::getReferenceInfo( *kind = arm_bl24; displacement = getDisplacementFromArmBranch(instruction); targetAddress = fixupAddress + 8 + displacement; - if (E ec = atomFromAddress(0, reloc.value, target, addend)) + if (auto ec = atomFromAddress(0, reloc.value, target, addend)) return ec; // reloc.value is target atom's address. Instruction contains branch // to atom+addend. *addend += (targetAddress - reloc.value); - return std::error_code(); + return llvm::Error(); case ARM_RELOC_VANILLA | rExtern | rLength4: // ex: .long _foo (and _foo is undefined) *kind = pointer32; - if (E ec = atomFromSymbolIndex(reloc.symbol, target)) + if (auto ec = atomFromSymbolIndex(reloc.symbol, target)) return ec; *addend = instruction; - return std::error_code(); + return llvm::Error(); case ARM_RELOC_VANILLA | rLength4: // ex: .long _foo (and _foo is defined) *kind = pointer32; - if (E ec = atomFromAddress(reloc.symbol, instruction, target, addend)) + if (auto ec = atomFromAddress(reloc.symbol, instruction, target, addend)) return ec; *addend = clearThumbBit((uint32_t) * addend, *target); - return std::error_code(); + return llvm::Error(); case ARM_RELOC_VANILLA | rScattered | rLength4: // ex: .long _foo+a (and _foo is defined) *kind = pointer32; - if (E ec = atomFromAddress(0, reloc.value, target, addend)) + if (auto ec = atomFromAddress(0, reloc.value, target, addend)) return ec; *addend += (clearThumbBit(instruction, *target) - reloc.value); - return std::error_code(); + return llvm::Error(); default: - return make_dynamic_error_code("unsupported arm relocation type"); + return llvm::make_error("unsupported arm relocation type"); } - return std::error_code(); + return llvm::Error(); } -std::error_code +llvm::Error ArchHandler_arm::getPairReferenceInfo(const normalized::Relocation &reloc1, const normalized::Relocation &reloc2, const DefinedAtom *inAtom, @@ -783,10 +782,9 @@ ArchHandler_arm::getPairReferenceInfo(const normalized::Relocation &reloc1, pointerDiff = true; break; default: - return make_dynamic_error_code("unsupported arm relocation pair"); + return llvm::make_error("unsupported arm relocation pair"); } const uint8_t *fixupContent = &inAtom->rawContent()[offsetInAtom]; - std::error_code ec; uint32_t instruction = *(const ulittle32_t *)fixupContent; uint32_t value; uint32_t fromAddress; @@ -799,14 +797,12 @@ ArchHandler_arm::getPairReferenceInfo(const normalized::Relocation &reloc1, if (pointerDiff) { toAddress = reloc1.value; fromAddress = reloc2.value; - ec = atomFromAddr(0, toAddress, target, &offsetInTo); - if (ec) + if (auto ec = atomFromAddr(0, toAddress, target, &offsetInTo)) return ec; - ec = atomFromAddr(0, fromAddress, &fromTarget, &offsetInFrom); - if (ec) + if (auto ec = atomFromAddr(0, fromAddress, &fromTarget, &offsetInFrom)) return ec; if (scatterable && (fromTarget != inAtom)) - return make_dynamic_error_code( + return llvm::make_error( "SECTDIFF relocation where subtrahend label is not in atom"); *kind = delta32; value = clearThumbBit(instruction, *target); @@ -814,35 +810,33 @@ ArchHandler_arm::getPairReferenceInfo(const normalized::Relocation &reloc1, } else if (funcRel) { toAddress = reloc1.value; fromAddress = reloc2.value; - ec = atomFromAddr(0, toAddress, target, &offsetInTo); - if (ec) + if (auto ec = atomFromAddr(0, toAddress, target, &offsetInTo)) return ec; - ec = atomFromAddr(0, fromAddress, &fromTarget, &offsetInFrom); - if (ec) + if (auto ec = atomFromAddr(0, fromAddress, &fromTarget, &offsetInFrom)) return ec; if (fromTarget != inAtom) - return make_dynamic_error_code("ARM_RELOC_HALF_SECTDIFF relocation " - "where subtrahend label is not in atom"); + return llvm::make_error("ARM_RELOC_HALF_SECTDIFF relocation" + " where subtrahend label is not in atom"); other16 = (reloc2.offset & 0xFFFF); if (thumbReloc) { if (top) { if (!isThumbMovt(instruction)) - return make_dynamic_error_code("expected movt instruction"); + return llvm::make_error("expected movt instruction"); } else { if (!isThumbMovw(instruction)) - return make_dynamic_error_code("expected movw instruction"); + return llvm::make_error("expected movw instruction"); } instruction16 = getWordFromThumbMov(instruction); } else { if (top) { if (!isArmMovt(instruction)) - return make_dynamic_error_code("expected movt instruction"); + return llvm::make_error("expected movt instruction"); } else { if (!isArmMovw(instruction)) - return make_dynamic_error_code("expected movw instruction"); + return llvm::make_error("expected movw instruction"); } instruction16 = getWordFromArmMov(instruction); } @@ -853,28 +847,28 @@ ArchHandler_arm::getPairReferenceInfo(const normalized::Relocation &reloc1, value = clearThumbBit(value, *target); int64_t ta = (int64_t) value - (toAddress - fromAddress); *addend = ta - offsetInFrom; - return std::error_code(); + return llvm::Error(); } else { uint32_t sectIndex; if (thumbReloc) { if (top) { if (!isThumbMovt(instruction)) - return make_dynamic_error_code("expected movt instruction"); + return llvm::make_error("expected movt instruction"); } else { if (!isThumbMovw(instruction)) - return make_dynamic_error_code("expected movw instruction"); + return llvm::make_error("expected movw instruction"); } instruction16 = getWordFromThumbMov(instruction); } else { if (top) { if (!isArmMovt(instruction)) - return make_dynamic_error_code("expected movt instruction"); + return llvm::make_error("expected movt instruction"); } else { if (!isArmMovw(instruction)) - return make_dynamic_error_code("expected movw instruction"); + return llvm::make_error("expected movw instruction"); } instruction16 = getWordFromArmMov(instruction); } @@ -884,8 +878,7 @@ ArchHandler_arm::getPairReferenceInfo(const normalized::Relocation &reloc1, else value = (other16 << 16) | instruction16; if (reloc1.isExtern) { - ec = atomFromSymbolIndex(reloc1.symbol, target); - if (ec) + if (auto ec = atomFromSymbolIndex(reloc1.symbol, target)) return ec; *addend = value; } else { @@ -896,14 +889,13 @@ ArchHandler_arm::getPairReferenceInfo(const normalized::Relocation &reloc1, toAddress = value; sectIndex = reloc1.symbol; } - ec = atomFromAddr(sectIndex, toAddress, target, &offsetInTo); - if (ec) + if (auto ec = atomFromAddr(sectIndex, toAddress, target, &offsetInTo)) return ec; *addend = value - toAddress; } } - return std::error_code(); + return llvm::Error(); } void ArchHandler_arm::applyFixupFinal(const Reference &ref, uint8_t *loc, diff --git a/lld/lib/ReaderWriter/MachO/ArchHandler_arm64.cpp b/lld/lib/ReaderWriter/MachO/ArchHandler_arm64.cpp index 103365f..a61f6aa 100644 --- a/lld/lib/ReaderWriter/MachO/ArchHandler_arm64.cpp +++ b/lld/lib/ReaderWriter/MachO/ArchHandler_arm64.cpp @@ -135,16 +135,16 @@ public: return 0x03000000; } - std::error_code getReferenceInfo(const normalized::Relocation &reloc, - const DefinedAtom *inAtom, - uint32_t offsetInAtom, - uint64_t fixupAddress, bool isBig, - FindAtomBySectionAndAddress atomFromAddress, - FindAtomBySymbolIndex atomFromSymbolIndex, - Reference::KindValue *kind, - const lld::Atom **target, - Reference::Addend *addend) override; - std::error_code + llvm::Error getReferenceInfo(const normalized::Relocation &reloc, + const DefinedAtom *inAtom, + uint32_t offsetInAtom, + uint64_t fixupAddress, bool isBig, + FindAtomBySectionAndAddress atomFromAddress, + FindAtomBySymbolIndex atomFromSymbolIndex, + Reference::KindValue *kind, + const lld::Atom **target, + Reference::Addend *addend) override; + llvm::Error getPairReferenceInfo(const normalized::Relocation &reloc1, const normalized::Relocation &reloc2, const DefinedAtom *inAtom, @@ -375,7 +375,7 @@ uint32_t ArchHandler_arm64::setImm12(uint32_t instruction, uint32_t offset) { return (instruction & 0xFFC003FF) | imm12; } -std::error_code ArchHandler_arm64::getReferenceInfo( +llvm::Error ArchHandler_arm64::getReferenceInfo( const Relocation &reloc, const DefinedAtom *inAtom, uint32_t offsetInAtom, uint64_t fixupAddress, bool isBig, FindAtomBySectionAndAddress atomFromAddress, @@ -389,56 +389,56 @@ std::error_code ArchHandler_arm64::getReferenceInfo( if (auto ec = atomFromSymbolIndex(reloc.symbol, target)) return ec; *addend = 0; - return std::error_code(); + return llvm::Error(); case ARM64_RELOC_PAGE21 | rPcRel | rExtern | rLength4: // ex: adrp x1, _foo@PAGE *kind = page21; if (auto ec = atomFromSymbolIndex(reloc.symbol, target)) return ec; *addend = 0; - return std::error_code(); + return llvm::Error(); case ARM64_RELOC_PAGEOFF12 | rExtern | rLength4: // ex: ldr x0, [x1, _foo@PAGEOFF] *kind = offset12KindFromInstruction(*(const little32_t *)fixupContent); if (auto ec = atomFromSymbolIndex(reloc.symbol, target)) return ec; *addend = 0; - return std::error_code(); + return llvm::Error(); case ARM64_RELOC_GOT_LOAD_PAGE21 | rPcRel | rExtern | rLength4: // ex: adrp x1, _foo@GOTPAGE *kind = gotPage21; if (auto ec = atomFromSymbolIndex(reloc.symbol, target)) return ec; *addend = 0; - return std::error_code(); + return llvm::Error(); case ARM64_RELOC_GOT_LOAD_PAGEOFF12 | rExtern | rLength4: // ex: ldr x0, [x1, _foo@GOTPAGEOFF] *kind = gotOffset12; if (auto ec = atomFromSymbolIndex(reloc.symbol, target)) return ec; *addend = 0; - return std::error_code(); + return llvm::Error(); case ARM64_RELOC_TLVP_LOAD_PAGE21 | rPcRel | rExtern | rLength4: // ex: adrp x1, _foo@TLVPAGE *kind = tlvPage21; if (auto ec = atomFromSymbolIndex(reloc.symbol, target)) return ec; *addend = 0; - return std::error_code(); + return llvm::Error(); case ARM64_RELOC_TLVP_LOAD_PAGEOFF12 | rExtern | rLength4: // ex: ldr x0, [x1, _foo@TLVPAGEOFF] *kind = tlvOffset12; if (auto ec = atomFromSymbolIndex(reloc.symbol, target)) return ec; *addend = 0; - return std::error_code(); + return llvm::Error(); case ARM64_RELOC_UNSIGNED | rExtern | rLength8: // ex: .quad _foo + N *kind = pointer64; if (auto ec = atomFromSymbolIndex(reloc.symbol, target)) return ec; *addend = *(const little64_t *)fixupContent; - return std::error_code(); + return llvm::Error(); case ARM64_RELOC_UNSIGNED | rLength8: // ex: .quad Lfoo + N *kind = pointer64; @@ -450,7 +450,7 @@ std::error_code ArchHandler_arm64::getReferenceInfo( if (auto ec = atomFromSymbolIndex(reloc.symbol, target)) return ec; *addend = 0; - return std::error_code(); + return llvm::Error(); case ARM64_RELOC_POINTER_TO_GOT | rPcRel | rExtern | rLength4: // ex: .long _foo@GOT - . @@ -464,13 +464,13 @@ std::error_code ArchHandler_arm64::getReferenceInfo( if (auto ec = atomFromSymbolIndex(reloc.symbol, target)) return ec; *addend = 0; - return std::error_code(); + return llvm::Error(); default: - return make_dynamic_error_code("unsupported arm64 relocation type"); + return llvm::make_error("unsupported arm64 relocation type"); } } -std::error_code ArchHandler_arm64::getPairReferenceInfo( +llvm::Error ArchHandler_arm64::getPairReferenceInfo( const normalized::Relocation &reloc1, const normalized::Relocation &reloc2, const DefinedAtom *inAtom, uint32_t offsetInAtom, uint64_t fixupAddress, bool swap, bool scatterable, FindAtomBySectionAndAddress atomFromAddress, @@ -485,7 +485,7 @@ std::error_code ArchHandler_arm64::getPairReferenceInfo( if (auto ec = atomFromSymbolIndex(reloc2.symbol, target)) return ec; *addend = reloc1.symbol; - return std::error_code(); + return llvm::Error(); case ((ARM64_RELOC_ADDEND | rLength4) << 16 | ARM64_RELOC_PAGE21 | rPcRel | rExtern | rLength4): // ex: adrp x1, _foo@PAGE @@ -493,7 +493,7 @@ std::error_code ArchHandler_arm64::getPairReferenceInfo( if (auto ec = atomFromSymbolIndex(reloc2.symbol, target)) return ec; *addend = reloc1.symbol; - return std::error_code(); + return llvm::Error(); case ((ARM64_RELOC_ADDEND | rLength4) << 16 | ARM64_RELOC_PAGEOFF12 | rExtern | rLength4): { // ex: ldr w0, [x1, _foo@PAGEOFF] @@ -502,7 +502,7 @@ std::error_code ArchHandler_arm64::getPairReferenceInfo( if (auto ec = atomFromSymbolIndex(reloc2.symbol, target)) return ec; *addend = reloc1.symbol; - return std::error_code(); + return llvm::Error(); } case ((ARM64_RELOC_SUBTRACTOR | rExtern | rLength8) << 16 | ARM64_RELOC_UNSIGNED | rExtern | rLength8): @@ -519,9 +519,10 @@ std::error_code ArchHandler_arm64::getPairReferenceInfo( // The offsets of the 2 relocations must match if (reloc1.offset != reloc2.offset) - return make_dynamic_error_code("paired relocs must have the same offset"); + return llvm::make_error( + "paired relocs must have the same offset"); *addend = (int64_t)*(const little64_t *)fixupContent + offsetInAtom; - return std::error_code(); + return llvm::Error(); case ((ARM64_RELOC_SUBTRACTOR | rExtern | rLength4) << 16 | ARM64_RELOC_UNSIGNED | rExtern | rLength4): // ex: .quad _foo - . @@ -529,9 +530,9 @@ std::error_code ArchHandler_arm64::getPairReferenceInfo( if (auto ec = atomFromSymbolIndex(reloc2.symbol, target)) return ec; *addend = (int32_t)*(const little32_t *)fixupContent + offsetInAtom; - return std::error_code(); + return llvm::Error(); default: - return make_dynamic_error_code("unsupported arm64 relocation pair"); + return llvm::make_error("unsupported arm64 relocation pair"); } } diff --git a/lld/lib/ReaderWriter/MachO/ArchHandler_x86.cpp b/lld/lib/ReaderWriter/MachO/ArchHandler_x86.cpp index 26b8698..15f1f79 100644 --- a/lld/lib/ReaderWriter/MachO/ArchHandler_x86.cpp +++ b/lld/lib/ReaderWriter/MachO/ArchHandler_x86.cpp @@ -82,16 +82,16 @@ public: return 0x04000000U; } - std::error_code getReferenceInfo(const normalized::Relocation &reloc, - const DefinedAtom *inAtom, - uint32_t offsetInAtom, - uint64_t fixupAddress, bool swap, - FindAtomBySectionAndAddress atomFromAddress, - FindAtomBySymbolIndex atomFromSymbolIndex, - Reference::KindValue *kind, - const lld::Atom **target, - Reference::Addend *addend) override; - std::error_code + llvm::Error getReferenceInfo(const normalized::Relocation &reloc, + const DefinedAtom *inAtom, + uint32_t offsetInAtom, + uint64_t fixupAddress, bool swap, + FindAtomBySectionAndAddress atomFromAddress, + FindAtomBySymbolIndex atomFromSymbolIndex, + Reference::KindValue *kind, + const lld::Atom **target, + Reference::Addend *addend) override; + llvm::Error getPairReferenceInfo(const normalized::Relocation &reloc1, const normalized::Relocation &reloc2, const DefinedAtom *inAtom, @@ -251,7 +251,7 @@ bool ArchHandler_x86::isPairedReloc(const Relocation &reloc) { (reloc.type == GENERIC_RELOC_SECTDIFF); } -std::error_code +llvm::Error ArchHandler_x86::getReferenceInfo(const Relocation &reloc, const DefinedAtom *inAtom, uint32_t offsetInAtom, @@ -261,7 +261,6 @@ ArchHandler_x86::getReferenceInfo(const Relocation &reloc, Reference::KindValue *kind, const lld::Atom **target, Reference::Addend *addend) { - typedef std::error_code E; DefinedAtom::ContentPermissions perms; const uint8_t *fixupContent = &inAtom->rawContent()[offsetInAtom]; uint64_t targetAddress; @@ -269,7 +268,7 @@ ArchHandler_x86::getReferenceInfo(const Relocation &reloc, case GENERIC_RELOC_VANILLA | rPcRel | rExtern | rLength4: // ex: call _foo (and _foo undefined) *kind = branch32; - if (E ec = atomFromSymbolIndex(reloc.symbol, target)) + if (auto ec = atomFromSymbolIndex(reloc.symbol, target)) return ec; *addend = fixupAddress + 4 + (int32_t)*(const little32_t *)fixupContent; break; @@ -285,14 +284,14 @@ ArchHandler_x86::getReferenceInfo(const Relocation &reloc, *kind = branch32; targetAddress = fixupAddress + 4 + (int32_t) * (const little32_t *)fixupContent; - if (E ec = atomFromAddress(0, reloc.value, target, addend)) + if (auto ec = atomFromAddress(0, reloc.value, target, addend)) return ec; *addend = targetAddress - reloc.value; break; case GENERIC_RELOC_VANILLA | rPcRel | rExtern | rLength2: // ex: callw _foo (and _foo undefined) *kind = branch16; - if (E ec = atomFromSymbolIndex(reloc.symbol, target)) + if (auto ec = atomFromSymbolIndex(reloc.symbol, target)) return ec; *addend = fixupAddress + 2 + (int16_t)*(const little16_t *)fixupContent; break; @@ -308,7 +307,7 @@ ArchHandler_x86::getReferenceInfo(const Relocation &reloc, *kind = branch16; targetAddress = fixupAddress + 2 + (int16_t) * (const little16_t *)fixupContent; - if (E ec = atomFromAddress(0, reloc.value, target, addend)) + if (auto ec = atomFromAddress(0, reloc.value, target, addend)) return ec; *addend = targetAddress - reloc.value; break; @@ -319,7 +318,7 @@ ArchHandler_x86::getReferenceInfo(const Relocation &reloc, *kind = ((perms & DefinedAtom::permR_X) == DefinedAtom::permR_X) ? abs32 : pointer32; - if (E ec = atomFromSymbolIndex(reloc.symbol, target)) + if (auto ec = atomFromSymbolIndex(reloc.symbol, target)) return ec; *addend = *(const ulittle32_t *)fixupContent; break; @@ -339,17 +338,17 @@ ArchHandler_x86::getReferenceInfo(const Relocation &reloc, *kind = ((perms & DefinedAtom::permR_X) == DefinedAtom::permR_X) ? abs32 : pointer32; - if (E ec = atomFromAddress(0, reloc.value, target, addend)) + if (auto ec = atomFromAddress(0, reloc.value, target, addend)) return ec; *addend = *(const ulittle32_t *)fixupContent - reloc.value; break; default: - return make_dynamic_error_code("unsupported i386 relocation type"); + return llvm::make_error("unsupported i386 relocation type"); } - return std::error_code(); + return llvm::Error(); } -std::error_code +llvm::Error ArchHandler_x86::getPairReferenceInfo(const normalized::Relocation &reloc1, const normalized::Relocation &reloc2, const DefinedAtom *inAtom, @@ -362,7 +361,6 @@ ArchHandler_x86::getPairReferenceInfo(const normalized::Relocation &reloc1, const lld::Atom **target, Reference::Addend *addend) { const uint8_t *fixupContent = &inAtom->rawContent()[offsetInAtom]; - std::error_code ec; DefinedAtom::ContentPermissions perms = inAtom->permissions(); uint32_t fromAddress; uint32_t toAddress; @@ -378,15 +376,13 @@ ArchHandler_x86::getPairReferenceInfo(const normalized::Relocation &reloc1, toAddress = reloc1.value; fromAddress = reloc2.value; value = *(const little32_t *)fixupContent; - ec = atomFromAddr(0, toAddress, target, &offsetInTo); - if (ec) + if (auto ec = atomFromAddr(0, toAddress, target, &offsetInTo)) return ec; - ec = atomFromAddr(0, fromAddress, &fromTarget, &offsetInFrom); - if (ec) + if (auto ec = atomFromAddr(0, fromAddress, &fromTarget, &offsetInFrom)) return ec; if (fromTarget != inAtom) { if (*target != inAtom) - return make_dynamic_error_code( + return llvm::make_error( "SECTDIFF relocation where neither target is in atom"); *kind = negDelta32; *addend = toAddress - value - fromAddress; @@ -407,10 +403,10 @@ ArchHandler_x86::getPairReferenceInfo(const normalized::Relocation &reloc1, *addend = fromAddress + value - toAddress; } } - return std::error_code(); + return llvm::Error(); break; default: - return make_dynamic_error_code("unsupported i386 relocation type"); + return llvm::make_error("unsupported i386 relocation type"); } } diff --git a/lld/lib/ReaderWriter/MachO/ArchHandler_x86_64.cpp b/lld/lib/ReaderWriter/MachO/ArchHandler_x86_64.cpp index f43477a..d8485d4 100644 --- a/lld/lib/ReaderWriter/MachO/ArchHandler_x86_64.cpp +++ b/lld/lib/ReaderWriter/MachO/ArchHandler_x86_64.cpp @@ -138,16 +138,16 @@ public: bool isPointer(const Reference &) override; bool isPairedReloc(const normalized::Relocation &) override; - std::error_code getReferenceInfo(const normalized::Relocation &reloc, - const DefinedAtom *inAtom, - uint32_t offsetInAtom, - uint64_t fixupAddress, bool swap, - FindAtomBySectionAndAddress atomFromAddress, - FindAtomBySymbolIndex atomFromSymbolIndex, - Reference::KindValue *kind, - const lld::Atom **target, - Reference::Addend *addend) override; - std::error_code + llvm::Error getReferenceInfo(const normalized::Relocation &reloc, + const DefinedAtom *inAtom, + uint32_t offsetInAtom, + uint64_t fixupAddress, bool swap, + FindAtomBySectionAndAddress atomFromAddress, + FindAtomBySymbolIndex atomFromSymbolIndex, + Reference::KindValue *kind, + const lld::Atom **target, + Reference::Addend *addend) override; + llvm::Error getPairReferenceInfo(const normalized::Relocation &reloc1, const normalized::Relocation &reloc2, const DefinedAtom *inAtom, @@ -361,7 +361,7 @@ ArchHandler_x86_64::kindFromReloc(const Relocation &reloc) { } } -std::error_code +llvm::Error ArchHandler_x86_64::getReferenceInfo(const Relocation &reloc, const DefinedAtom *inAtom, uint32_t offsetInAtom, @@ -371,34 +371,33 @@ ArchHandler_x86_64::getReferenceInfo(const Relocation &reloc, Reference::KindValue *kind, const lld::Atom **target, Reference::Addend *addend) { - typedef std::error_code E; *kind = kindFromReloc(reloc); if (*kind == invalid) - return make_dynamic_error_code("unknown type"); + return llvm::make_error("unknown type"); const uint8_t *fixupContent = &inAtom->rawContent()[offsetInAtom]; uint64_t targetAddress; switch (*kind) { case branch32: case ripRel32: - if (E ec = atomFromSymbolIndex(reloc.symbol, target)) + if (auto ec = atomFromSymbolIndex(reloc.symbol, target)) return ec; *addend = *(const little32_t *)fixupContent; - return std::error_code(); + return llvm::Error(); case ripRel32Minus1: - if (E ec = atomFromSymbolIndex(reloc.symbol, target)) + if (auto ec = atomFromSymbolIndex(reloc.symbol, target)) return ec; *addend = (int32_t)*(const little32_t *)fixupContent + 1; - return std::error_code(); + return llvm::Error(); case ripRel32Minus2: - if (E ec = atomFromSymbolIndex(reloc.symbol, target)) + if (auto ec = atomFromSymbolIndex(reloc.symbol, target)) return ec; *addend = (int32_t)*(const little32_t *)fixupContent + 2; - return std::error_code(); + return llvm::Error(); case ripRel32Minus4: - if (E ec = atomFromSymbolIndex(reloc.symbol, target)) + if (auto ec = atomFromSymbolIndex(reloc.symbol, target)) return ec; *addend = (int32_t)*(const little32_t *)fixupContent + 4; - return std::error_code(); + return llvm::Error(); case ripRel32Anon: targetAddress = fixupAddress + 4 + *(const little32_t *)fixupContent; return atomFromAddress(reloc.symbol, targetAddress, target, addend); @@ -414,13 +413,13 @@ ArchHandler_x86_64::getReferenceInfo(const Relocation &reloc, case ripRel32GotLoad: case ripRel32Got: case ripRel32Tlv: - if (E ec = atomFromSymbolIndex(reloc.symbol, target)) + if (auto ec = atomFromSymbolIndex(reloc.symbol, target)) return ec; *addend = *(const little32_t *)fixupContent; - return std::error_code(); + return llvm::Error(); case tlvInitSectionOffset: case pointer64: - if (E ec = atomFromSymbolIndex(reloc.symbol, target)) + if (auto ec = atomFromSymbolIndex(reloc.symbol, target)) return ec; // If this is the 3rd pointer of a tlv-thunk (i.e. the pointer to the TLV's // initial value) we need to handle it specially. @@ -430,7 +429,7 @@ ArchHandler_x86_64::getReferenceInfo(const Relocation &reloc, assert(*addend == 0 && "TLV-init has non-zero addend?"); } else *addend = *(const little64_t *)fixupContent; - return std::error_code(); + return llvm::Error(); case pointer64Anon: targetAddress = *(const little64_t *)fixupContent; return atomFromAddress(reloc.symbol, targetAddress, target, addend); @@ -460,7 +459,7 @@ ArchHandler_x86_64::kindFromRelocPair(const normalized::Relocation &reloc1, } } -std::error_code +llvm::Error ArchHandler_x86_64::getPairReferenceInfo(const normalized::Relocation &reloc1, const normalized::Relocation &reloc2, const DefinedAtom *inAtom, @@ -474,26 +473,25 @@ ArchHandler_x86_64::getPairReferenceInfo(const normalized::Relocation &reloc1, Reference::Addend *addend) { *kind = kindFromRelocPair(reloc1, reloc2); if (*kind == invalid) - return make_dynamic_error_code("unknown pair"); + return llvm::make_error("unknown pair"); const uint8_t *fixupContent = &inAtom->rawContent()[offsetInAtom]; - typedef std::error_code E; uint64_t targetAddress; const lld::Atom *fromTarget; - if (E ec = atomFromSymbolIndex(reloc1.symbol, &fromTarget)) + if (auto ec = atomFromSymbolIndex(reloc1.symbol, &fromTarget)) return ec; if (fromTarget != inAtom) - return make_dynamic_error_code("pointer diff not in base atom"); + return llvm::make_error("pointer diff not in base atom"); switch (*kind) { case delta64: - if (E ec = atomFromSymbolIndex(reloc2.symbol, target)) + if (auto ec = atomFromSymbolIndex(reloc2.symbol, target)) return ec; *addend = (int64_t)*(const little64_t *)fixupContent + offsetInAtom; - return std::error_code(); + return llvm::Error(); case delta32: - if (E ec = atomFromSymbolIndex(reloc2.symbol, target)) + if (auto ec = atomFromSymbolIndex(reloc2.symbol, target)) return ec; *addend = (int32_t)*(const little32_t *)fixupContent + offsetInAtom; - return std::error_code(); + return llvm::Error(); case delta64Anon: targetAddress = offsetInAtom + (int64_t)*(const little64_t *)fixupContent; return atomFromAddress(reloc2.symbol, targetAddress, target, addend); diff --git a/lld/lib/ReaderWriter/MachO/MachONormalizedFileToAtoms.cpp b/lld/lib/ReaderWriter/MachO/MachONormalizedFileToAtoms.cpp index 4a830d5..c981e6b 100644 --- a/lld/lib/ReaderWriter/MachO/MachONormalizedFileToAtoms.cpp +++ b/lld/lib/ReaderWriter/MachO/MachONormalizedFileToAtoms.cpp @@ -513,23 +513,23 @@ findAtomCoveringAddress(const NormalizedFile &normalizedFile, MachOFile &file, // Walks all relocations for a section in a normalized .o file and // creates corresponding lld::Reference objects. -std::error_code convertRelocs(const Section §ion, - const NormalizedFile &normalizedFile, - bool scatterable, - MachOFile &file, - ArchHandler &handler) { +llvm::Error convertRelocs(const Section §ion, + const NormalizedFile &normalizedFile, + bool scatterable, + MachOFile &file, + ArchHandler &handler) { // Utility function for ArchHandler to find atom by its address. auto atomByAddr = [&] (uint32_t sectIndex, uint64_t addr, const lld::Atom **atom, Reference::Addend *addend) - -> std::error_code { + -> llvm::Error { if (sectIndex > normalizedFile.sections.size()) - return make_dynamic_error_code(Twine("out of range section " + return llvm::make_error(Twine("out of range section " "index (") + Twine(sectIndex) + ")"); const Section *sect = nullptr; if (sectIndex == 0) { sect = findSectionCoveringAddress(normalizedFile, addr); if (!sect) - return make_dynamic_error_code(Twine("address (" + Twine(addr) + return llvm::make_error(Twine("address (" + Twine(addr) + ") is not in any section")); } else { sect = &normalizedFile.sections[sectIndex-1]; @@ -538,12 +538,12 @@ std::error_code convertRelocs(const Section §ion, uint64_t offsetInSect = addr - sect->address; *atom = file.findAtomCoveringAddress(*sect, offsetInSect, &offsetInTarget); *addend = offsetInTarget; - return std::error_code(); + return llvm::Error(); }; // Utility function for ArchHandler to find atom by its symbol index. auto atomBySymbol = [&] (uint32_t symbolIndex, const lld::Atom **result) - -> std::error_code { + -> llvm::Error { // Find symbol from index. const Symbol *sym = nullptr; uint32_t numLocal = normalizedFile.localSymbols.size(); @@ -556,13 +556,13 @@ std::error_code convertRelocs(const Section §ion, } else if (symbolIndex < numLocal+numGlobal+numUndef) { sym = &normalizedFile.undefinedSymbols[symbolIndex-numLocal-numGlobal]; } else { - return make_dynamic_error_code(Twine("symbol index (") + return llvm::make_error(Twine("symbol index (") + Twine(symbolIndex) + ") out of range"); } // Find atom from symbol. if ((sym->type & N_TYPE) == N_SECT) { if (sym->sect > normalizedFile.sections.size()) - return make_dynamic_error_code(Twine("symbol section index (") + return llvm::make_error(Twine("symbol section index (") + Twine(sym->sect) + ") out of range "); const Section &symSection = normalizedFile.sections[sym->sect-1]; uint64_t targetOffsetInSect = sym->value - symSection.address; @@ -570,19 +570,19 @@ std::error_code convertRelocs(const Section §ion, targetOffsetInSect); if (target) { *result = target; - return std::error_code(); + return llvm::Error(); } - return make_dynamic_error_code("no atom found for defined symbol"); + return llvm::make_error("no atom found for defined symbol"); } else if ((sym->type & N_TYPE) == N_UNDF) { const lld::Atom *target = file.findUndefAtom(sym->name); if (target) { *result = target; - return std::error_code(); + return llvm::Error(); } - return make_dynamic_error_code("no undefined atom found for sym"); + return llvm::make_error("no undefined atom found for sym"); } else { // Search undefs - return make_dynamic_error_code("no atom found for symbol"); + return llvm::make_error("no atom found for symbol"); } }; @@ -593,7 +593,8 @@ std::error_code convertRelocs(const Section §ion, const Relocation &reloc = *it; // Find atom this relocation is in. if (reloc.offset > section.content.size()) - return make_dynamic_error_code(Twine("r_address (") + Twine(reloc.offset) + return llvm::make_error( + Twine("r_address (") + Twine(reloc.offset) + ") is larger than section size (" + Twine(section.content.size()) + ")"); uint32_t offsetInAtom; @@ -606,60 +607,65 @@ std::error_code convertRelocs(const Section §ion, const lld::Atom *target = nullptr; Reference::Addend addend = 0; Reference::KindValue kind; - std::error_code relocErr; if (handler.isPairedReloc(reloc)) { // Handle paired relocations together. const Relocation &reloc2 = *++it; - relocErr = handler.getPairReferenceInfo( + auto relocErr = handler.getPairReferenceInfo( reloc, reloc2, inAtom, offsetInAtom, fixupAddress, isBig, scatterable, atomByAddr, atomBySymbol, &kind, &target, &addend); if (relocErr) { - return make_dynamic_error_code( - Twine("bad relocation (") + relocErr.message() - + ") in section " - + section.segmentName + "/" + section.sectionName - + " (r1_address=" + Twine::utohexstr(reloc.offset) - + ", r1_type=" + Twine(reloc.type) - + ", r1_extern=" + Twine(reloc.isExtern) - + ", r1_length=" + Twine((int)reloc.length) - + ", r1_pcrel=" + Twine(reloc.pcRel) - + (!reloc.scattered ? (Twine(", r1_symbolnum=") - + Twine(reloc.symbol)) - : (Twine(", r1_scattered=1, r1_value=") - + Twine(reloc.value))) - + ")" - + ", (r2_address=" + Twine::utohexstr(reloc2.offset) - + ", r2_type=" + Twine(reloc2.type) - + ", r2_extern=" + Twine(reloc2.isExtern) - + ", r2_length=" + Twine((int)reloc2.length) - + ", r2_pcrel=" + Twine(reloc2.pcRel) - + (!reloc2.scattered ? (Twine(", r2_symbolnum=") - + Twine(reloc2.symbol)) - : (Twine(", r2_scattered=1, r2_value=") - + Twine(reloc2.value))) - + ")" ); + return handleErrors(std::move(relocErr), + [&](std::unique_ptr GE) { + return llvm::make_error( + Twine("bad relocation (") + GE->getMessage() + + ") in section " + + section.segmentName + "/" + section.sectionName + + " (r1_address=" + Twine::utohexstr(reloc.offset) + + ", r1_type=" + Twine(reloc.type) + + ", r1_extern=" + Twine(reloc.isExtern) + + ", r1_length=" + Twine((int)reloc.length) + + ", r1_pcrel=" + Twine(reloc.pcRel) + + (!reloc.scattered ? (Twine(", r1_symbolnum=") + + Twine(reloc.symbol)) + : (Twine(", r1_scattered=1, r1_value=") + + Twine(reloc.value))) + + ")" + + ", (r2_address=" + Twine::utohexstr(reloc2.offset) + + ", r2_type=" + Twine(reloc2.type) + + ", r2_extern=" + Twine(reloc2.isExtern) + + ", r2_length=" + Twine((int)reloc2.length) + + ", r2_pcrel=" + Twine(reloc2.pcRel) + + (!reloc2.scattered ? (Twine(", r2_symbolnum=") + + Twine(reloc2.symbol)) + : (Twine(", r2_scattered=1, r2_value=") + + Twine(reloc2.value))) + + ")" ); + }); } } else { // Use ArchHandler to convert relocation record into information // needed to instantiate an lld::Reference object. - relocErr = handler.getReferenceInfo( + auto relocErr = handler.getReferenceInfo( reloc, inAtom, offsetInAtom, fixupAddress, isBig, atomByAddr, atomBySymbol, &kind, &target, &addend); if (relocErr) { - return make_dynamic_error_code( - Twine("bad relocation (") + relocErr.message() - + ") in section " - + section.segmentName + "/" + section.sectionName - + " (r_address=" + Twine::utohexstr(reloc.offset) - + ", r_type=" + Twine(reloc.type) - + ", r_extern=" + Twine(reloc.isExtern) - + ", r_length=" + Twine((int)reloc.length) - + ", r_pcrel=" + Twine(reloc.pcRel) - + (!reloc.scattered ? (Twine(", r_symbolnum=") + Twine(reloc.symbol)) - : (Twine(", r_scattered=1, r_value=") - + Twine(reloc.value))) - + ")" ); + return handleErrors(std::move(relocErr), + [&](std::unique_ptr GE) { + return llvm::make_error( + Twine("bad relocation (") + GE->getMessage() + + ") in section " + + section.segmentName + "/" + section.sectionName + + " (r_address=" + Twine::utohexstr(reloc.offset) + + ", r_type=" + Twine(reloc.type) + + ", r_extern=" + Twine(reloc.isExtern) + + ", r_length=" + Twine((int)reloc.length) + + ", r_pcrel=" + Twine(reloc.pcRel) + + (!reloc.scattered ? (Twine(", r_symbolnum=") + Twine(reloc.symbol)) + : (Twine(", r_scattered=1, r_value=") + + Twine(reloc.value))) + + ")" ); + }); } } // Instantiate an lld::Reference object and add to its atom. @@ -668,7 +674,7 @@ std::error_code convertRelocs(const Section §ion, kind, offsetInAtom, target, addend); } - return std::error_code(); + return llvm::Error(); } bool isDebugInfoSection(const Section §ion) { @@ -1165,9 +1171,9 @@ normalizedObjectToAtoms(MachOFile *file, for (auto § : normalizedFile.sections) { if (isDebugInfoSection(sect)) continue; - if (std::error_code ec = convertRelocs(sect, normalizedFile, scatterable, - *file, *handler)) - return ec; + if (llvm::Error ec = convertRelocs(sect, normalizedFile, scatterable, + *file, *handler)) + return llvm::errorToErrorCode(std::move(ec)); } // Add additional arch-specific References