61 changes: 31 additions & 30 deletions lld/lib/ReaderWriter/MachO/ArchHandler_arm64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,16 +135,16 @@ class ArchHandler_arm64 : public ArchHandler {
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,
Expand Down Expand Up @@ -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,
Expand All @@ -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;
Expand All @@ -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 - .

Expand All @@ -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<GenericError>("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,
Expand All @@ -485,15 +485,15 @@ 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
*kind = page21;
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]
Expand All @@ -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):
Expand All @@ -519,19 +519,20 @@ 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<GenericError>(
"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 - .
*kind = delta32;
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<GenericError>("unsupported arm64 relocation pair");
}
}

Expand Down
54 changes: 25 additions & 29 deletions lld/lib/ReaderWriter/MachO/ArchHandler_x86.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,16 +82,16 @@ class ArchHandler_x86 : public ArchHandler {
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,
Expand Down Expand Up @@ -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,
Expand All @@ -261,15 +261,14 @@ 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;
switch (relocPattern(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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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<GenericError>("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,
Expand All @@ -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;
Expand All @@ -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<GenericError>(
"SECTDIFF relocation where neither target is in atom");
*kind = negDelta32;
*addend = toAddress - value - fromAddress;
Expand All @@ -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<GenericError>("unsupported i386 relocation type");
}
}

Expand Down
66 changes: 32 additions & 34 deletions lld/lib/ReaderWriter/MachO/ArchHandler_x86_64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,16 +138,16 @@ class ArchHandler_x86_64 : public ArchHandler {
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,
Expand Down Expand Up @@ -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,
Expand All @@ -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<GenericError>("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);
Expand All @@ -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.
Expand All @@ -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);
Expand Down Expand Up @@ -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,
Expand All @@ -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<GenericError>("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<GenericError>("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);
Expand Down
130 changes: 68 additions & 62 deletions lld/lib/ReaderWriter/MachO/MachONormalizedFileToAtoms.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 &section,
const NormalizedFile &normalizedFile,
bool scatterable,
MachOFile &file,
ArchHandler &handler) {
llvm::Error convertRelocs(const Section &section,
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<GenericError>(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<GenericError>(Twine("address (" + Twine(addr)
+ ") is not in any section"));
} else {
sect = &normalizedFile.sections[sectIndex-1];
Expand All @@ -538,12 +538,12 @@ std::error_code convertRelocs(const Section &section,
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();
Expand All @@ -556,33 +556,33 @@ std::error_code convertRelocs(const Section &section,
} 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<GenericError>(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<GenericError>(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;
MachODefinedAtom *target = file.findAtomCoveringAddress(symSection,
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<GenericError>("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<GenericError>("no undefined atom found for sym");
} else {
// Search undefs
return make_dynamic_error_code("no atom found for symbol");
return llvm::make_error<GenericError>("no atom found for symbol");
}
};

Expand All @@ -593,7 +593,8 @@ std::error_code convertRelocs(const Section &section,
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<GenericError>(
Twine("r_address (") + Twine(reloc.offset)
+ ") is larger than section size ("
+ Twine(section.content.size()) + ")");
uint32_t offsetInAtom;
Expand All @@ -606,60 +607,65 @@ std::error_code convertRelocs(const Section &section,
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<GenericError> GE) {
return llvm::make_error<GenericError>(
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<GenericError> GE) {
return llvm::make_error<GenericError>(
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.
Expand All @@ -668,7 +674,7 @@ std::error_code convertRelocs(const Section &section,
kind, offsetInAtom, target, addend);
}

return std::error_code();
return llvm::Error();
}

bool isDebugInfoSection(const Section &section) {
Expand Down Expand Up @@ -1165,9 +1171,9 @@ normalizedObjectToAtoms(MachOFile *file,
for (auto &sect : 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
Expand Down