Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[BOLT] Add initial bits for parsing MachO files
Summary: Start adding initial bits for MachO, this diff contains some small preparations for finding functions inside a MachO binary, this will be done in the next diff. The concept of a section in the MachO world is quite different from ELF, nevertheless, for functions for now it more or less fits into the current picture (in BOLT), but things will diverge more significantly a bit later. (cherry picked from FBD19648161)
- Loading branch information
Showing
9 changed files
with
226 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
//===--- MachORewriteInstance.cpp - Instance of a rewriting process. ------===// | ||
// | ||
// The LLVM Compiler Infrastructure | ||
// | ||
// This file is distributed under the University of Illinois Open Source | ||
// License. See LICENSE.TXT for details. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "MachORewriteInstance.h" | ||
#include "BinaryContext.h" | ||
#include "BinaryFunction.h" | ||
#include "Utils.h" | ||
#include "llvm/Support/Timer.h" | ||
|
||
namespace opts { | ||
|
||
using namespace llvm; | ||
extern cl::opt<bool> PrintSections; | ||
|
||
} // namespace opts | ||
|
||
namespace llvm { | ||
namespace bolt { | ||
|
||
#undef DEBUG_TYPE | ||
#define DEBUG_TYPE "bolt" | ||
|
||
MachORewriteInstance::MachORewriteInstance(object::MachOObjectFile *InputFile, | ||
DataReader &DR) | ||
: InputFile(InputFile), | ||
BC(BinaryContext::createBinaryContext( | ||
InputFile, DR, | ||
DWARFContext::create(*InputFile, nullptr, | ||
DWARFContext::defaultErrorHandler, "", false))) { | ||
} | ||
|
||
void MachORewriteInstance::readSpecialSections() { | ||
for (const auto &Section : InputFile->sections()) { | ||
StringRef SectionName; | ||
check_error(Section.getName(SectionName), "cannot get section name"); | ||
// Only register sections with names. | ||
if (!SectionName.empty()) { | ||
BC->registerSection(Section); | ||
DEBUG(dbgs() << "BOLT-DEBUG: registering section " << SectionName | ||
<< " @ 0x" << Twine::utohexstr(Section.getAddress()) << ":0x" | ||
<< Twine::utohexstr(Section.getAddress() + Section.getSize()) | ||
<< "\n"); | ||
} | ||
} | ||
|
||
if (opts::PrintSections) { | ||
outs() << "BOLT-INFO: Sections from original binary:\n"; | ||
BC->printSections(outs()); | ||
} | ||
} | ||
|
||
void MachORewriteInstance::run() { | ||
readSpecialSections(); | ||
} | ||
|
||
MachORewriteInstance::~MachORewriteInstance() {} | ||
|
||
} // namespace bolt | ||
} // namespace llvm |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
//===--- MachORewriteInstance.h - Instance of a rewriting process. --------===// | ||
// | ||
// The LLVM Compiler Infrastructure | ||
// | ||
// This file is distributed under the University of Illinois Open Source | ||
// License. See LICENSE.TXT for details. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// Interface to control an instance of a macho binary rewriting process. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef LLVM_TOOLS_LLVM_BOLT_MACHO_REWRITE_INSTANCE_H | ||
#define LLVM_TOOLS_LLVM_BOLT_MACHO_REWRITE_INSTANCE_H | ||
|
||
#include "llvm/Object/MachO.h" | ||
#include <memory> | ||
|
||
namespace llvm { | ||
namespace bolt { | ||
|
||
class BinaryContext; | ||
class DataReader; | ||
|
||
class MachORewriteInstance { | ||
object::MachOObjectFile *InputFile; | ||
std::unique_ptr<BinaryContext> BC; | ||
|
||
void readSpecialSections(); | ||
|
||
public: | ||
MachORewriteInstance(object::MachOObjectFile *InputFile, DataReader &DR); | ||
~MachORewriteInstance(); | ||
|
||
/// Run all the necessary steps to read, optimize and rewrite the binary. | ||
void run(); | ||
}; | ||
|
||
} // namespace bolt | ||
} // namespace llvm | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
//===--- Utils.cpp - Common helper functions ------------------------------===// | ||
// | ||
// The LLVM Compiler Infrastructure | ||
// | ||
// This file is distributed under the University of Illinois Open Source | ||
// License. See LICENSE.TXT for details. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// Common helper functions. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "Utils.h" | ||
#include "llvm/Support/raw_ostream.h" | ||
|
||
namespace llvm { | ||
namespace bolt { | ||
|
||
void report_error(StringRef Message, std::error_code EC) { | ||
assert(EC); | ||
errs() << "BOLT-ERROR: '" << Message << "': " << EC.message() << ".\n"; | ||
exit(1); | ||
} | ||
|
||
void report_error(StringRef Message, Error E) { | ||
assert(E); | ||
errs() << "BOLT-ERROR: '" << Message << "': " << toString(std::move(E)) | ||
<< ".\n"; | ||
exit(1); | ||
} | ||
|
||
void check_error(std::error_code EC, StringRef Message) { | ||
if (!EC) | ||
return; | ||
report_error(Message, EC); | ||
} | ||
|
||
void check_error(Error E, Twine Message) { | ||
if (!E) | ||
return; | ||
handleAllErrors(std::move(E), [&](const llvm::ErrorInfoBase &EIB) { | ||
llvm::errs() << "BOLT-ERROR: '" << Message << "': " << EIB.message() | ||
<< '\n'; | ||
exit(1); | ||
}); | ||
} | ||
|
||
} // namespace bolt | ||
} // namespace llvm |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
//===--- Utils.h - Common helper functions --------------------------------===// | ||
// | ||
// The LLVM Compiler Infrastructure | ||
// | ||
// This file is distributed under the University of Illinois Open Source | ||
// License. See LICENSE.TXT for details. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// Common helper functions. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef LLVM_TOOLS_LLVM_BOLT_UTILS_H | ||
#define LLVM_TOOLS_LLVM_BOLT_UTILS_H | ||
|
||
#include "llvm/ADT/StringRef.h" | ||
#include "llvm/ADT/Twine.h" | ||
#include "llvm/Support/Error.h" | ||
|
||
namespace llvm { | ||
namespace bolt { | ||
|
||
void report_error(StringRef Message, std::error_code EC); | ||
|
||
void report_error(StringRef Message, Error E); | ||
|
||
void check_error(std::error_code EC, StringRef Message); | ||
|
||
void check_error(Error E, Twine Message); | ||
|
||
} // namespace bolt | ||
} // namespace llvm | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters