mirror of
https://github.com/RPCS3/llvm-mirror.git
synced 2025-01-31 12:41:49 +01:00
92582f2b18
Produce another specific error message for a malformed Mach-O file when a symbol’s string index is past the end of the string table. The existing test case in test/Object/macho-invalid.test for macho-invalid-symbol-name-past-eof now reports the error with the message indicating that a symbol at a specific index has a bad sting index and that bad string index value. Again converting interfaces to Expected<> from ErrorOr<> does involve touching a number of places. Where the existing code reported the error with a string message or an error code it was converted to do the same. There is some code for this that could be factored into a routine but I would like to leave that for the code owners post-commit to do as they want for handling an llvm::Error. An example of how this could be done is shown in the diff in lib/ExecutionEngine/RuntimeDyld/RuntimeDyldImpl.h which had a Check() routine already for std::error_code so I added one like it for llvm::Error . Also there some were bugs in the existing code that did not deal with the old ErrorOr<> return values. So now with Expected<> since they must be checked and the error handled, I added a TODO and a comment: “// TODO: Actually report errors helpfully” and a call something like consumeError(NameOrErr.takeError()) so the buggy code will not crash since needed to deal with the Error. Note there fixes needed to lld that goes along with this that I will commit right after this. So expect lld not to built after this commit and before the next one. llvm-svn: 266919
207 lines
7.6 KiB
C++
207 lines
7.6 KiB
C++
//===--- RuntimeDyldCOFFI386.h --- COFF/X86_64 specific code ---*- C++ --*-===//
|
|
//
|
|
// The LLVM Compiler Infrastructure
|
|
//
|
|
// This file is distributed under the University of Illinois Open Source
|
|
// License. See LICENSE.TXT for details.
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
//
|
|
// COFF x86 support for MC-JIT runtime dynamic linker.
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#ifndef LLVM_LIB_EXECUTIONENGINE_RUNTIMEDYLD_TARGETS_RUNTIMEDYLDCOFFI386_H
|
|
#define LLVM_LIB_EXECUTIONENGINE_RUNTIMEDYLD_TARGETS_RUNTIMEDYLDCOFFI386_H
|
|
|
|
#include "llvm/Object/COFF.h"
|
|
#include "llvm/Support/COFF.h"
|
|
#include "../RuntimeDyldCOFF.h"
|
|
|
|
#define DEBUG_TYPE "dyld"
|
|
|
|
namespace llvm {
|
|
|
|
class RuntimeDyldCOFFI386 : public RuntimeDyldCOFF {
|
|
public:
|
|
RuntimeDyldCOFFI386(RuntimeDyld::MemoryManager &MM,
|
|
RuntimeDyld::SymbolResolver &Resolver)
|
|
: RuntimeDyldCOFF(MM, Resolver) {}
|
|
|
|
unsigned getMaxStubSize() override {
|
|
return 8; // 2-byte jmp instruction + 32-bit relative address + 2 byte pad
|
|
}
|
|
|
|
unsigned getStubAlignment() override { return 1; }
|
|
|
|
relocation_iterator processRelocationRef(unsigned SectionID,
|
|
relocation_iterator RelI,
|
|
const ObjectFile &Obj,
|
|
ObjSectionToIDMap &ObjSectionToID,
|
|
StubMap &Stubs) override {
|
|
auto Symbol = RelI->getSymbol();
|
|
if (Symbol == Obj.symbol_end())
|
|
report_fatal_error("Unknown symbol in relocation");
|
|
|
|
Expected<StringRef> TargetNameOrErr = Symbol->getName();
|
|
if (!TargetNameOrErr) {
|
|
std::string Buf;
|
|
raw_string_ostream OS(Buf);
|
|
logAllUnhandledErrors(TargetNameOrErr.takeError(), OS, "");
|
|
OS.flush();
|
|
report_fatal_error(Buf);
|
|
}
|
|
StringRef TargetName = *TargetNameOrErr;
|
|
|
|
auto Section = *Symbol->getSection();
|
|
|
|
uint64_t RelType = RelI->getType();
|
|
uint64_t Offset = RelI->getOffset();
|
|
|
|
#if !defined(NDEBUG)
|
|
SmallString<32> RelTypeName;
|
|
RelI->getTypeName(RelTypeName);
|
|
#endif
|
|
DEBUG(dbgs() << "\t\tIn Section " << SectionID << " Offset " << Offset
|
|
<< " RelType: " << RelTypeName << " TargetName: " << TargetName
|
|
<< "\n");
|
|
|
|
unsigned TargetSectionID = -1;
|
|
if (Section == Obj.section_end()) {
|
|
RelocationEntry RE(SectionID, Offset, RelType, 0, -1, 0, 0, 0, false, 0);
|
|
addRelocationForSymbol(RE, TargetName);
|
|
} else {
|
|
TargetSectionID =
|
|
findOrEmitSection(Obj, *Section, Section->isText(), ObjSectionToID);
|
|
|
|
switch (RelType) {
|
|
case COFF::IMAGE_REL_I386_ABSOLUTE:
|
|
// This relocation is ignored.
|
|
break;
|
|
case COFF::IMAGE_REL_I386_DIR32:
|
|
case COFF::IMAGE_REL_I386_DIR32NB:
|
|
case COFF::IMAGE_REL_I386_REL32: {
|
|
RelocationEntry RE =
|
|
RelocationEntry(SectionID, Offset, RelType, 0, TargetSectionID,
|
|
getSymbolOffset(*Symbol), 0, 0, false, 0);
|
|
addRelocationForSection(RE, TargetSectionID);
|
|
break;
|
|
}
|
|
case COFF::IMAGE_REL_I386_SECTION: {
|
|
RelocationEntry RE =
|
|
RelocationEntry(TargetSectionID, Offset, RelType, 0);
|
|
addRelocationForSection(RE, TargetSectionID);
|
|
break;
|
|
}
|
|
case COFF::IMAGE_REL_I386_SECREL: {
|
|
RelocationEntry RE = RelocationEntry(SectionID, Offset, RelType,
|
|
getSymbolOffset(*Symbol));
|
|
addRelocationForSection(RE, TargetSectionID);
|
|
break;
|
|
}
|
|
default:
|
|
llvm_unreachable("unsupported relocation type");
|
|
}
|
|
|
|
}
|
|
|
|
return ++RelI;
|
|
}
|
|
|
|
void resolveRelocation(const RelocationEntry &RE, uint64_t Value) override {
|
|
const auto Section = Sections[RE.SectionID];
|
|
uint8_t *Target = Section.getAddressWithOffset(RE.Offset);
|
|
|
|
switch (RE.RelType) {
|
|
case COFF::IMAGE_REL_I386_ABSOLUTE:
|
|
// This relocation is ignored.
|
|
break;
|
|
case COFF::IMAGE_REL_I386_DIR32: {
|
|
// The target's 32-bit VA.
|
|
uint64_t Result =
|
|
RE.Sections.SectionA == static_cast<uint32_t>(-1)
|
|
? Value
|
|
: Sections[RE.Sections.SectionA].getLoadAddressWithOffset(
|
|
RE.Addend);
|
|
assert(static_cast<int32_t>(Result) <= INT32_MAX &&
|
|
"relocation overflow");
|
|
assert(static_cast<int32_t>(Result) >= INT32_MIN &&
|
|
"relocation underflow");
|
|
DEBUG(dbgs() << "\t\tOffset: " << RE.Offset
|
|
<< " RelType: IMAGE_REL_I386_DIR32"
|
|
<< " TargetSection: " << RE.Sections.SectionA
|
|
<< " Value: " << format("0x%08" PRIx32, Result) << '\n');
|
|
writeBytesUnaligned(Result, Target, 4);
|
|
break;
|
|
}
|
|
case COFF::IMAGE_REL_I386_DIR32NB: {
|
|
// The target's 32-bit RVA.
|
|
// NOTE: use Section[0].getLoadAddress() as an approximation of ImageBase
|
|
uint64_t Result =
|
|
Sections[RE.Sections.SectionA].getLoadAddressWithOffset(RE.Addend) -
|
|
Sections[0].getLoadAddress();
|
|
assert(static_cast<int32_t>(Result) <= INT32_MAX &&
|
|
"relocation overflow");
|
|
assert(static_cast<int32_t>(Result) >= INT32_MIN &&
|
|
"relocation underflow");
|
|
DEBUG(dbgs() << "\t\tOffset: " << RE.Offset
|
|
<< " RelType: IMAGE_REL_I386_DIR32NB"
|
|
<< " TargetSection: " << RE.Sections.SectionA
|
|
<< " Value: " << format("0x%08" PRIx32, Result) << '\n');
|
|
writeBytesUnaligned(Result, Target, 4);
|
|
break;
|
|
}
|
|
case COFF::IMAGE_REL_I386_REL32: {
|
|
// 32-bit relative displacement to the target.
|
|
uint64_t Result = Sections[RE.Sections.SectionA].getLoadAddress() -
|
|
Section.getLoadAddress() + RE.Addend - 4 - RE.Offset;
|
|
assert(static_cast<int32_t>(Result) <= INT32_MAX &&
|
|
"relocation overflow");
|
|
assert(static_cast<int32_t>(Result) >= INT32_MIN &&
|
|
"relocation underflow");
|
|
DEBUG(dbgs() << "\t\tOffset: " << RE.Offset
|
|
<< " RelType: IMAGE_REL_I386_REL32"
|
|
<< " TargetSection: " << RE.Sections.SectionA
|
|
<< " Value: " << format("0x%08" PRIx32, Result) << '\n');
|
|
writeBytesUnaligned(Result, Target, 4);
|
|
break;
|
|
}
|
|
case COFF::IMAGE_REL_I386_SECTION:
|
|
// 16-bit section index of the section that contains the target.
|
|
assert(static_cast<int32_t>(RE.SectionID) <= INT16_MAX &&
|
|
"relocation overflow");
|
|
assert(static_cast<int32_t>(RE.SectionID) >= INT16_MIN &&
|
|
"relocation underflow");
|
|
DEBUG(dbgs() << "\t\tOffset: " << RE.Offset
|
|
<< " RelType: IMAGE_REL_I386_SECTION Value: " << RE.SectionID
|
|
<< '\n');
|
|
writeBytesUnaligned(RE.SectionID, Target, 2);
|
|
break;
|
|
case COFF::IMAGE_REL_I386_SECREL:
|
|
// 32-bit offset of the target from the beginning of its section.
|
|
assert(static_cast<int32_t>(RE.Addend) <= INT32_MAX &&
|
|
"relocation overflow");
|
|
assert(static_cast<int32_t>(RE.Addend) >= INT32_MIN &&
|
|
"relocation underflow");
|
|
DEBUG(dbgs() << "\t\tOffset: " << RE.Offset
|
|
<< " RelType: IMAGE_REL_I386_SECREL Value: " << RE.Addend
|
|
<< '\n');
|
|
writeBytesUnaligned(RE.Addend, Target, 2);
|
|
break;
|
|
default:
|
|
llvm_unreachable("unsupported relocation type");
|
|
}
|
|
}
|
|
|
|
void registerEHFrames() override {}
|
|
void deregisterEHFrames() override {}
|
|
|
|
void finalizeLoad(const ObjectFile &Obj,
|
|
ObjSectionToIDMap &SectionMap) override {}
|
|
};
|
|
|
|
}
|
|
|
|
#endif
|
|
|