Convert a few macho reader/writer helpers to new error handling. NFC.
These methods were responsible for some of the few remaining calls to llvm::errorCodeToError. Converting them makes us have more Error's in the api and fewer error_code's. llvm-svn: 264974
This commit is contained in:
parent
c6e7b8146a
commit
514594bdd3
|
@ -53,7 +53,7 @@ namespace mach_o {
|
|||
namespace normalized {
|
||||
|
||||
// Utility to call a lambda expression on each load command.
|
||||
static std::error_code forEachLoadCommand(
|
||||
static llvm::Error forEachLoadCommand(
|
||||
StringRef lcRange, unsigned lcCount, bool isBig, bool is64,
|
||||
std::function<bool(uint32_t cmd, uint32_t size, const char *lc)> func) {
|
||||
const char* p = lcRange.begin();
|
||||
|
@ -67,15 +67,15 @@ static std::error_code forEachLoadCommand(
|
|||
slc = &lcCopy;
|
||||
}
|
||||
if ( (p + slc->cmdsize) > lcRange.end() )
|
||||
return make_error_code(llvm::errc::executable_format_error);
|
||||
return llvm::make_error<GenericError>("Load command exceeds range");
|
||||
|
||||
if (func(slc->cmd, slc->cmdsize, p))
|
||||
return std::error_code();
|
||||
return llvm::Error();
|
||||
|
||||
p += slc->cmdsize;
|
||||
}
|
||||
|
||||
return std::error_code();
|
||||
return llvm::Error();
|
||||
}
|
||||
|
||||
static std::error_code appendRelocations(Relocations &relocs, StringRef buffer,
|
||||
|
@ -257,9 +257,9 @@ readBinary(std::unique_ptr<MemoryBuffer> &mb,
|
|||
// Pre-scan load commands looking for indirect symbol table.
|
||||
uint32_t indirectSymbolTableOffset = 0;
|
||||
uint32_t indirectSymbolTableCount = 0;
|
||||
std::error_code ec = forEachLoadCommand(lcRange, lcCount, isBig, is64,
|
||||
[&](uint32_t cmd, uint32_t size,
|
||||
const char *lc) -> bool {
|
||||
auto ec = forEachLoadCommand(lcRange, lcCount, isBig, is64,
|
||||
[&](uint32_t cmd, uint32_t size,
|
||||
const char *lc) -> bool {
|
||||
if (cmd == LC_DYSYMTAB) {
|
||||
const dysymtab_command *d = reinterpret_cast<const dysymtab_command*>(lc);
|
||||
indirectSymbolTableOffset = read32(&d->indirectsymoff, isBig);
|
||||
|
@ -269,7 +269,7 @@ readBinary(std::unique_ptr<MemoryBuffer> &mb,
|
|||
return false;
|
||||
});
|
||||
if (ec)
|
||||
return llvm::errorCodeToError(ec);
|
||||
return std::move(ec);
|
||||
|
||||
// Walk load commands looking for segments/sections and the symbol table.
|
||||
const data_in_code_entry *dataInCode = nullptr;
|
||||
|
@ -485,7 +485,7 @@ readBinary(std::unique_ptr<MemoryBuffer> &mb,
|
|||
return false;
|
||||
});
|
||||
if (ec)
|
||||
return llvm::errorCodeToError(ec);
|
||||
return std::move(ec);
|
||||
|
||||
if (dataInCode) {
|
||||
// Convert on-disk data_in_code_entry array to DataInCode vector.
|
||||
|
|
|
@ -139,7 +139,7 @@ private:
|
|||
uint32_t loadCommandsSize(uint32_t &count);
|
||||
void buildFileOffsets();
|
||||
void writeMachHeader();
|
||||
std::error_code writeLoadCommands();
|
||||
llvm::Error writeLoadCommands();
|
||||
void writeSectionContent();
|
||||
void writeRelocations();
|
||||
void writeSymbolTable();
|
||||
|
@ -179,8 +179,8 @@ private:
|
|||
};
|
||||
|
||||
template <typename T>
|
||||
std::error_code writeSingleSegmentLoadCommand(uint8_t *&lc);
|
||||
template <typename T> std::error_code writeSegmentLoadCommands(uint8_t *&lc);
|
||||
llvm::Error writeSingleSegmentLoadCommand(uint8_t *&lc);
|
||||
template <typename T> llvm::Error writeSegmentLoadCommands(uint8_t *&lc);
|
||||
|
||||
uint32_t pointerAlign(uint32_t value);
|
||||
static StringRef dyldPath();
|
||||
|
@ -628,7 +628,7 @@ uint32_t MachOFileLayout::indirectSymbolElementSize(const Section §) {
|
|||
}
|
||||
|
||||
template <typename T>
|
||||
std::error_code MachOFileLayout::writeSingleSegmentLoadCommand(uint8_t *&lc) {
|
||||
llvm::Error MachOFileLayout::writeSingleSegmentLoadCommand(uint8_t *&lc) {
|
||||
typename T::command* seg = reinterpret_cast<typename T::command*>(lc);
|
||||
seg->cmd = T::LC;
|
||||
seg->cmdsize = sizeof(typename T::command)
|
||||
|
@ -668,11 +668,11 @@ std::error_code MachOFileLayout::writeSingleSegmentLoadCommand(uint8_t *&lc) {
|
|||
++sout;
|
||||
}
|
||||
lc = next;
|
||||
return std::error_code();
|
||||
return llvm::Error();
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
std::error_code MachOFileLayout::writeSegmentLoadCommands(uint8_t *&lc) {
|
||||
llvm::Error MachOFileLayout::writeSegmentLoadCommands(uint8_t *&lc) {
|
||||
uint32_t indirectSymRunningIndex = 0;
|
||||
for (const Segment &seg : _file.segments) {
|
||||
// Link edit has no sections and a custom range of address, so handle it
|
||||
|
@ -738,7 +738,7 @@ std::error_code MachOFileLayout::writeSegmentLoadCommands(uint8_t *&lc) {
|
|||
}
|
||||
lc = reinterpret_cast<uint8_t*>(next);
|
||||
}
|
||||
return std::error_code();
|
||||
return llvm::Error();
|
||||
}
|
||||
|
||||
static void writeVersionMinLoadCommand(const NormalizedFile &_file,
|
||||
|
@ -773,15 +773,17 @@ static void writeVersionMinLoadCommand(const NormalizedFile &_file,
|
|||
lc += sizeof(version_min_command);
|
||||
}
|
||||
|
||||
std::error_code MachOFileLayout::writeLoadCommands() {
|
||||
std::error_code ec;
|
||||
llvm::Error MachOFileLayout::writeLoadCommands() {
|
||||
uint8_t *lc = &_buffer[_startOfLoadCommands];
|
||||
if (_file.fileType == llvm::MachO::MH_OBJECT) {
|
||||
// Object files have one unnamed segment which holds all sections.
|
||||
if (_is64)
|
||||
ec = writeSingleSegmentLoadCommand<MachO64Trait>(lc);
|
||||
else
|
||||
ec = writeSingleSegmentLoadCommand<MachO32Trait>(lc);
|
||||
if (_is64) {
|
||||
if (auto ec = writeSingleSegmentLoadCommand<MachO64Trait>(lc))
|
||||
return std::move(ec);
|
||||
} else {
|
||||
if (auto ec = writeSingleSegmentLoadCommand<MachO32Trait>(lc))
|
||||
return std::move(ec);
|
||||
}
|
||||
// Add LC_SYMTAB with symbol table info
|
||||
symtab_command* st = reinterpret_cast<symtab_command*>(lc);
|
||||
st->cmd = LC_SYMTAB;
|
||||
|
@ -824,10 +826,13 @@ std::error_code MachOFileLayout::writeLoadCommands() {
|
|||
}
|
||||
} else {
|
||||
// Final linked images have sections under segments.
|
||||
if (_is64)
|
||||
ec = writeSegmentLoadCommands<MachO64Trait>(lc);
|
||||
else
|
||||
ec = writeSegmentLoadCommands<MachO32Trait>(lc);
|
||||
if (_is64) {
|
||||
if (auto ec = writeSegmentLoadCommands<MachO64Trait>(lc))
|
||||
return std::move(ec);
|
||||
} else {
|
||||
if (auto ec = writeSegmentLoadCommands<MachO32Trait>(lc))
|
||||
return std::move(ec);
|
||||
}
|
||||
|
||||
// Add LC_ID_DYLIB command for dynamic libraries.
|
||||
if (_file.fileType == llvm::MachO::MH_DYLIB) {
|
||||
|
@ -1012,7 +1017,7 @@ std::error_code MachOFileLayout::writeLoadCommands() {
|
|||
lc += sizeof(linkedit_data_command);
|
||||
}
|
||||
}
|
||||
return ec;
|
||||
return llvm::Error();
|
||||
}
|
||||
|
||||
void MachOFileLayout::writeSectionContent() {
|
||||
|
@ -1475,9 +1480,8 @@ llvm::Error MachOFileLayout::writeBinary(StringRef path) {
|
|||
// Write content.
|
||||
_buffer = fob->getBufferStart();
|
||||
writeMachHeader();
|
||||
std::error_code ec = writeLoadCommands();
|
||||
if (ec)
|
||||
return llvm::errorCodeToError(ec);
|
||||
if (auto ec = writeLoadCommands())
|
||||
return std::move(ec);
|
||||
writeSectionContent();
|
||||
writeLinkEditContent();
|
||||
fob->commit();
|
||||
|
|
Loading…
Reference in New Issue