Converted local offsets from uint64_t to uint32_t. Refactoring.

(cherry picked from FBD2543557)
This commit is contained in:
Maksim Panchenko
2015-10-14 16:46:59 -07:00
parent 4c1da22ae9
commit f79f6302c1
2 changed files with 30 additions and 32 deletions

View File

@@ -105,22 +105,22 @@ private:
/// Release storage used by instructions.
BinaryFunction &clearInstructions() {
std::map<uint64_t, MCInst> TempMap;
InstrMapType TempMap;
Instructions.swap(TempMap);
return *this;
}
/// Release storage used by instructions.
BinaryFunction &clearLabels() {
std::map<uint64_t, MCSymbol *> TempMap;
LabelsMapType TempMap;
Labels.swap(TempMap);
return *this;
}
/// Release memory taken by local branch info.
BinaryFunction &clearLocalBranches() {
std::vector<std::pair<uint64_t, uint64_t>> TempVector;
LocalBranches.swap(TempVector);
LocalBranchesListType TempList;
LocalBranches.swap(TempList);
return *this;
}
@@ -129,19 +129,26 @@ private:
return *this;
}
public:
std::vector<std::pair<uint64_t, uint64_t>> LocalBranches;
/// Storage for all local branches in the function (non-fall-throughs).
using LocalBranchesListType = std::vector<std::pair<uint32_t, uint32_t>>;
LocalBranchesListType LocalBranches;
std::map<uint64_t, MCSymbol *> Labels;
/// Map offset in the function to a local label.
using LabelsMapType = std::map<uint32_t, MCSymbol *>;
LabelsMapType Labels;
/// Temporary holder of instructions before CFG is constructed.
std::map<uint64_t, MCInst> Instructions;
/// Map offset in the function to MCInst.
using InstrMapType = std::map<uint32_t, MCInst>;
InstrMapType Instructions;
// Blocks are kept sorted in the layout order. If we need to change the
// layout, the terminating instructions need to be modified.
typedef std::vector<BinaryBasicBlock> BasicBlockListType;
using BasicBlockListType = std::vector<BinaryBasicBlock>;
BasicBlockListType BasicBlocks;
public:
typedef BasicBlockListType::iterator iterator;
typedef BasicBlockListType::const_iterator const_iterator;
typedef std::reverse_iterator<const_iterator> const_reverse_iterator;

View File

@@ -99,12 +99,10 @@ static void report_error(StringRef Message, std::error_code EC) {
exit(1);
}
static void error(std::error_code EC) {
static void check_error(std::error_code EC, StringRef Message) {
if (!EC)
return;
errs() << ToolName << ": error reading file: " << EC.message() << ".\n";
exit(1);
report_error(Message, EC);
}
template <typename T>
@@ -298,7 +296,7 @@ static void OptimizeFile(ELFObjectFileBase *File, const DataReader &DR) {
continue;
ErrorOr<StringRef> Name = Symbol.getName();
error(Name.getError());
check_error(Name.getError(), "cannot get symbol name");
if (Symbol.getType() == SymbolRef::ST_File) {
// Could be used for local symbol disambiguation.
@@ -307,7 +305,7 @@ static void OptimizeFile(ELFObjectFileBase *File, const DataReader &DR) {
}
ErrorOr<uint64_t> AddressOrErr = Symbol.getAddress();
error(AddressOrErr.getError());
check_error(AddressOrErr.getError(), "cannot get symbol address");
uint64_t Address = *AddressOrErr;
if (Address == 0) {
if (Symbol.getType() == SymbolRef::ST_Function)
@@ -339,7 +337,7 @@ static void OptimizeFile(ELFObjectFileBase *File, const DataReader &DR) {
continue;
ErrorOr<section_iterator> SectionOrErr = Symbol.getSection();
error(SectionOrErr.getError());
check_error(SectionOrErr.getError(), "cannot get symbol section");
section_iterator Section = *SectionOrErr;
if (Section == File->section_end()) {
// Could be an absolute symbol. Could record for pretty printing.
@@ -403,7 +401,8 @@ static void OptimizeFile(ELFObjectFileBase *File, const DataReader &DR) {
}
StringRef SectionContents;
error(Section.getContents(SectionContents));
check_error(Section.getContents(SectionContents),
"cannot get section contents");
assert(SectionContents.size() == Section.getSize() &&
"section size mismatch");
@@ -460,22 +459,18 @@ static void OptimizeFile(ELFObjectFileBase *File, const DataReader &DR) {
}
std::error_code EC;
// This is an object file, which we keep for debugging purposes.
// Once we decide it's useless, we should create it in memory.
std::unique_ptr<tool_output_file> Out =
llvm::make_unique<tool_output_file>(OutputFilename + ".o",
EC,sys::fs::F_None);
if (EC) {
// FIXME: handle error
return;
}
EC, sys::fs::F_None);
check_error(EC, "cannot create output object file");
std::unique_ptr<tool_output_file> RealOut =
llvm::make_unique<tool_output_file>(OutputFilename, EC, sys::fs::F_None,
0777);
if (EC) {
// FIXME: handle error
return;
}
check_error(EC, "cannot create output executable file");
// Copy input file.
RealOut->os() << File->getData();
@@ -565,11 +560,7 @@ static void OptimizeFile(ELFObjectFileBase *File, const DataReader &DR) {
MemoryBuffer::getMemBuffer(BOS->str(), "in-memory object file", false);
ErrorOr<std::unique_ptr<object::ObjectFile>> ObjOrErr =
object::ObjectFile::createObjectFile(ObjectMemBuffer->getMemBufferRef());
if (std::error_code EC = ObjOrErr.getError()) {
report_error(InputFilename, EC);
return;
}
check_error(ObjOrErr.getError(), "error creating in-memory object");
std::unique_ptr<ExecutableFileMemoryManager>
EFMM(new ExecutableFileMemoryManager());