MC: Disassembled CFG reconstruction.
This patch builds on some existing code to do CFG reconstruction from
a disassembled binary:
- MCModule represents the binary, and has a list of MCAtoms.
- MCAtom represents either disassembled instructions (MCTextAtom), or
contiguous data (MCDataAtom), and covers a specific range of addresses.
- MCBasicBlock and MCFunction form the reconstructed CFG. An MCBB is
backed by an MCTextAtom, and has the usual successors/predecessors.
- MCObjectDisassembler creates a module from an ObjectFile using a
disassembler. It first builds an atom for each section. It can also
construct the CFG, and this splits the text atoms into basic blocks.
MCModule and MCAtom were only sketched out; MCFunction and MCBB were
implemented under the experimental "-cfg" llvm-objdump -macho option.
This cleans them up for further use; llvm-objdump -d -cfg now generates
graphviz files for each function found in the binary.
In the future, MCObjectDisassembler may be the right place to do
"intelligent" disassembly: for example, handling constant islands is just
a matter of splitting the atom, using information that may be available
in the ObjectFile. Also, better initial atom formation than just using
sections is possible using symbols (and things like Mach-O's
function_starts load command).
This brings two minor regressions in llvm-objdump -macho -cfg:
- The printing of a relocation's referenced symbol.
- An annotation on loop BBs, i.e., which are their own successor.
Relocation printing is replaced by the MCSymbolizer; the basic CFG
annotation will be superseded by more related functionality.
llvm-svn: 182628
2013-05-24 03:07:04 +02:00
|
|
|
//===-- llvm/MC/MCFunction.h ------------------------------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines the data structures to hold a CFG reconstructed from
|
|
|
|
// machine code.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_MC_MCFUNCTION_H
|
|
|
|
#define LLVM_MC_MCFUNCTION_H
|
|
|
|
|
|
|
|
#include "llvm/ADT/StringRef.h"
|
|
|
|
#include "llvm/MC/MCInst.h"
|
2014-04-15 06:56:29 +02:00
|
|
|
#include <memory>
|
MC: Disassembled CFG reconstruction.
This patch builds on some existing code to do CFG reconstruction from
a disassembled binary:
- MCModule represents the binary, and has a list of MCAtoms.
- MCAtom represents either disassembled instructions (MCTextAtom), or
contiguous data (MCDataAtom), and covers a specific range of addresses.
- MCBasicBlock and MCFunction form the reconstructed CFG. An MCBB is
backed by an MCTextAtom, and has the usual successors/predecessors.
- MCObjectDisassembler creates a module from an ObjectFile using a
disassembler. It first builds an atom for each section. It can also
construct the CFG, and this splits the text atoms into basic blocks.
MCModule and MCAtom were only sketched out; MCFunction and MCBB were
implemented under the experimental "-cfg" llvm-objdump -macho option.
This cleans them up for further use; llvm-objdump -d -cfg now generates
graphviz files for each function found in the binary.
In the future, MCObjectDisassembler may be the right place to do
"intelligent" disassembly: for example, handling constant islands is just
a matter of splitting the atom, using information that may be available
in the ObjectFile. Also, better initial atom formation than just using
sections is possible using symbols (and things like Mach-O's
function_starts load command).
This brings two minor regressions in llvm-objdump -macho -cfg:
- The printing of a relocation's referenced symbol.
- An annotation on loop BBs, i.e., which are their own successor.
Relocation printing is replaced by the MCSymbolizer; the basic CFG
annotation will be superseded by more related functionality.
llvm-svn: 182628
2013-05-24 03:07:04 +02:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
|
|
|
|
class MCFunction;
|
|
|
|
class MCModule;
|
|
|
|
class MCTextAtom;
|
|
|
|
|
|
|
|
/// \brief Basic block containing a sequence of disassembled instructions.
|
|
|
|
/// The basic block is backed by an MCTextAtom, which holds the instructions,
|
|
|
|
/// and the address range it covers.
|
|
|
|
/// Create a basic block using MCFunction::createBlock.
|
|
|
|
class MCBasicBlock {
|
|
|
|
const MCTextAtom *Insts;
|
|
|
|
|
|
|
|
// MCFunction owns the basic block.
|
|
|
|
MCFunction *Parent;
|
|
|
|
friend class MCFunction;
|
|
|
|
MCBasicBlock(const MCTextAtom &Insts, MCFunction *Parent);
|
|
|
|
|
|
|
|
/// \name Predecessors/Successors, to represent the CFG.
|
|
|
|
/// @{
|
|
|
|
typedef std::vector<const MCBasicBlock *> BasicBlockListTy;
|
|
|
|
BasicBlockListTy Successors;
|
|
|
|
BasicBlockListTy Predecessors;
|
|
|
|
/// @}
|
|
|
|
public:
|
|
|
|
|
|
|
|
/// \brief Get the backing MCTextAtom, containing the instruction sequence.
|
|
|
|
const MCTextAtom *getInsts() const { return Insts; }
|
|
|
|
|
|
|
|
/// \name Get the owning MCFunction.
|
|
|
|
/// @{
|
|
|
|
const MCFunction *getParent() const { return Parent; }
|
|
|
|
MCFunction *getParent() { return Parent; }
|
|
|
|
/// @}
|
|
|
|
|
|
|
|
/// MC CFG access: Predecessors/Successors.
|
|
|
|
/// @{
|
|
|
|
typedef BasicBlockListTy::const_iterator succ_const_iterator;
|
|
|
|
succ_const_iterator succ_begin() const { return Successors.begin(); }
|
|
|
|
succ_const_iterator succ_end() const { return Successors.end(); }
|
|
|
|
|
|
|
|
typedef BasicBlockListTy::const_iterator pred_const_iterator;
|
|
|
|
pred_const_iterator pred_begin() const { return Predecessors.begin(); }
|
|
|
|
pred_const_iterator pred_end() const { return Predecessors.end(); }
|
|
|
|
|
|
|
|
void addSuccessor(const MCBasicBlock *MCBB);
|
|
|
|
bool isSuccessor(const MCBasicBlock *MCBB) const;
|
|
|
|
|
|
|
|
void addPredecessor(const MCBasicBlock *MCBB);
|
|
|
|
bool isPredecessor(const MCBasicBlock *MCBB) const;
|
2013-08-21 09:28:24 +02:00
|
|
|
|
|
|
|
/// \brief Split block, mirrorring NewAtom = Insts->split(..).
|
|
|
|
/// This moves all successors to \p SplitBB, and
|
|
|
|
/// adds a fallthrough to it.
|
|
|
|
/// \p SplitBB The result of splitting Insts, a basic block directly following
|
|
|
|
/// this basic block.
|
|
|
|
void splitBasicBlock(MCBasicBlock *SplitBB);
|
MC: Disassembled CFG reconstruction.
This patch builds on some existing code to do CFG reconstruction from
a disassembled binary:
- MCModule represents the binary, and has a list of MCAtoms.
- MCAtom represents either disassembled instructions (MCTextAtom), or
contiguous data (MCDataAtom), and covers a specific range of addresses.
- MCBasicBlock and MCFunction form the reconstructed CFG. An MCBB is
backed by an MCTextAtom, and has the usual successors/predecessors.
- MCObjectDisassembler creates a module from an ObjectFile using a
disassembler. It first builds an atom for each section. It can also
construct the CFG, and this splits the text atoms into basic blocks.
MCModule and MCAtom were only sketched out; MCFunction and MCBB were
implemented under the experimental "-cfg" llvm-objdump -macho option.
This cleans them up for further use; llvm-objdump -d -cfg now generates
graphviz files for each function found in the binary.
In the future, MCObjectDisassembler may be the right place to do
"intelligent" disassembly: for example, handling constant islands is just
a matter of splitting the atom, using information that may be available
in the ObjectFile. Also, better initial atom formation than just using
sections is possible using symbols (and things like Mach-O's
function_starts load command).
This brings two minor regressions in llvm-objdump -macho -cfg:
- The printing of a relocation's referenced symbol.
- An annotation on loop BBs, i.e., which are their own successor.
Relocation printing is replaced by the MCSymbolizer; the basic CFG
annotation will be superseded by more related functionality.
llvm-svn: 182628
2013-05-24 03:07:04 +02:00
|
|
|
/// @}
|
|
|
|
};
|
|
|
|
|
|
|
|
/// \brief Represents a function in machine code, containing MCBasicBlocks.
|
2013-08-21 09:27:55 +02:00
|
|
|
/// MCFunctions are created by MCModule.
|
MC: Disassembled CFG reconstruction.
This patch builds on some existing code to do CFG reconstruction from
a disassembled binary:
- MCModule represents the binary, and has a list of MCAtoms.
- MCAtom represents either disassembled instructions (MCTextAtom), or
contiguous data (MCDataAtom), and covers a specific range of addresses.
- MCBasicBlock and MCFunction form the reconstructed CFG. An MCBB is
backed by an MCTextAtom, and has the usual successors/predecessors.
- MCObjectDisassembler creates a module from an ObjectFile using a
disassembler. It first builds an atom for each section. It can also
construct the CFG, and this splits the text atoms into basic blocks.
MCModule and MCAtom were only sketched out; MCFunction and MCBB were
implemented under the experimental "-cfg" llvm-objdump -macho option.
This cleans them up for further use; llvm-objdump -d -cfg now generates
graphviz files for each function found in the binary.
In the future, MCObjectDisassembler may be the right place to do
"intelligent" disassembly: for example, handling constant islands is just
a matter of splitting the atom, using information that may be available
in the ObjectFile. Also, better initial atom formation than just using
sections is possible using symbols (and things like Mach-O's
function_starts load command).
This brings two minor regressions in llvm-objdump -macho -cfg:
- The printing of a relocation's referenced symbol.
- An annotation on loop BBs, i.e., which are their own successor.
Relocation printing is replaced by the MCSymbolizer; the basic CFG
annotation will be superseded by more related functionality.
llvm-svn: 182628
2013-05-24 03:07:04 +02:00
|
|
|
class MCFunction {
|
|
|
|
MCFunction (const MCFunction&) LLVM_DELETED_FUNCTION;
|
|
|
|
MCFunction& operator=(const MCFunction&) LLVM_DELETED_FUNCTION;
|
|
|
|
|
|
|
|
std::string Name;
|
2013-08-21 09:27:55 +02:00
|
|
|
MCModule *ParentModule;
|
2014-04-15 06:56:29 +02:00
|
|
|
typedef std::vector<std::unique_ptr<MCBasicBlock>> BasicBlockListTy;
|
MC: Disassembled CFG reconstruction.
This patch builds on some existing code to do CFG reconstruction from
a disassembled binary:
- MCModule represents the binary, and has a list of MCAtoms.
- MCAtom represents either disassembled instructions (MCTextAtom), or
contiguous data (MCDataAtom), and covers a specific range of addresses.
- MCBasicBlock and MCFunction form the reconstructed CFG. An MCBB is
backed by an MCTextAtom, and has the usual successors/predecessors.
- MCObjectDisassembler creates a module from an ObjectFile using a
disassembler. It first builds an atom for each section. It can also
construct the CFG, and this splits the text atoms into basic blocks.
MCModule and MCAtom were only sketched out; MCFunction and MCBB were
implemented under the experimental "-cfg" llvm-objdump -macho option.
This cleans them up for further use; llvm-objdump -d -cfg now generates
graphviz files for each function found in the binary.
In the future, MCObjectDisassembler may be the right place to do
"intelligent" disassembly: for example, handling constant islands is just
a matter of splitting the atom, using information that may be available
in the ObjectFile. Also, better initial atom formation than just using
sections is possible using symbols (and things like Mach-O's
function_starts load command).
This brings two minor regressions in llvm-objdump -macho -cfg:
- The printing of a relocation's referenced symbol.
- An annotation on loop BBs, i.e., which are their own successor.
Relocation printing is replaced by the MCSymbolizer; the basic CFG
annotation will be superseded by more related functionality.
llvm-svn: 182628
2013-05-24 03:07:04 +02:00
|
|
|
BasicBlockListTy Blocks;
|
|
|
|
|
|
|
|
// MCModule owns the function.
|
|
|
|
friend class MCModule;
|
2013-08-21 09:27:55 +02:00
|
|
|
MCFunction(StringRef Name, MCModule *Parent);
|
MC: Disassembled CFG reconstruction.
This patch builds on some existing code to do CFG reconstruction from
a disassembled binary:
- MCModule represents the binary, and has a list of MCAtoms.
- MCAtom represents either disassembled instructions (MCTextAtom), or
contiguous data (MCDataAtom), and covers a specific range of addresses.
- MCBasicBlock and MCFunction form the reconstructed CFG. An MCBB is
backed by an MCTextAtom, and has the usual successors/predecessors.
- MCObjectDisassembler creates a module from an ObjectFile using a
disassembler. It first builds an atom for each section. It can also
construct the CFG, and this splits the text atoms into basic blocks.
MCModule and MCAtom were only sketched out; MCFunction and MCBB were
implemented under the experimental "-cfg" llvm-objdump -macho option.
This cleans them up for further use; llvm-objdump -d -cfg now generates
graphviz files for each function found in the binary.
In the future, MCObjectDisassembler may be the right place to do
"intelligent" disassembly: for example, handling constant islands is just
a matter of splitting the atom, using information that may be available
in the ObjectFile. Also, better initial atom formation than just using
sections is possible using symbols (and things like Mach-O's
function_starts load command).
This brings two minor regressions in llvm-objdump -macho -cfg:
- The printing of a relocation's referenced symbol.
- An annotation on loop BBs, i.e., which are their own successor.
Relocation printing is replaced by the MCSymbolizer; the basic CFG
annotation will be superseded by more related functionality.
llvm-svn: 182628
2013-05-24 03:07:04 +02:00
|
|
|
|
2013-08-21 09:27:55 +02:00
|
|
|
public:
|
MC: Disassembled CFG reconstruction.
This patch builds on some existing code to do CFG reconstruction from
a disassembled binary:
- MCModule represents the binary, and has a list of MCAtoms.
- MCAtom represents either disassembled instructions (MCTextAtom), or
contiguous data (MCDataAtom), and covers a specific range of addresses.
- MCBasicBlock and MCFunction form the reconstructed CFG. An MCBB is
backed by an MCTextAtom, and has the usual successors/predecessors.
- MCObjectDisassembler creates a module from an ObjectFile using a
disassembler. It first builds an atom for each section. It can also
construct the CFG, and this splits the text atoms into basic blocks.
MCModule and MCAtom were only sketched out; MCFunction and MCBB were
implemented under the experimental "-cfg" llvm-objdump -macho option.
This cleans them up for further use; llvm-objdump -d -cfg now generates
graphviz files for each function found in the binary.
In the future, MCObjectDisassembler may be the right place to do
"intelligent" disassembly: for example, handling constant islands is just
a matter of splitting the atom, using information that may be available
in the ObjectFile. Also, better initial atom formation than just using
sections is possible using symbols (and things like Mach-O's
function_starts load command).
This brings two minor regressions in llvm-objdump -macho -cfg:
- The printing of a relocation's referenced symbol.
- An annotation on loop BBs, i.e., which are their own successor.
Relocation printing is replaced by the MCSymbolizer; the basic CFG
annotation will be superseded by more related functionality.
llvm-svn: 182628
2013-05-24 03:07:04 +02:00
|
|
|
/// \brief Create an MCBasicBlock backed by Insts and add it to this function.
|
|
|
|
/// \param Insts Sequence of straight-line code backing the basic block.
|
|
|
|
/// \returns The newly created basic block.
|
|
|
|
MCBasicBlock &createBlock(const MCTextAtom &Insts);
|
|
|
|
|
|
|
|
StringRef getName() const { return Name; }
|
|
|
|
|
2013-08-21 09:28:17 +02:00
|
|
|
/// \name Get the owning MC Module.
|
|
|
|
/// @{
|
|
|
|
const MCModule *getParent() const { return ParentModule; }
|
|
|
|
MCModule *getParent() { return ParentModule; }
|
|
|
|
/// @}
|
|
|
|
|
2013-08-21 09:27:59 +02:00
|
|
|
/// \name Access to the function's basic blocks. No ordering is enforced,
|
|
|
|
/// except that the first block is the entry block.
|
MC: Disassembled CFG reconstruction.
This patch builds on some existing code to do CFG reconstruction from
a disassembled binary:
- MCModule represents the binary, and has a list of MCAtoms.
- MCAtom represents either disassembled instructions (MCTextAtom), or
contiguous data (MCDataAtom), and covers a specific range of addresses.
- MCBasicBlock and MCFunction form the reconstructed CFG. An MCBB is
backed by an MCTextAtom, and has the usual successors/predecessors.
- MCObjectDisassembler creates a module from an ObjectFile using a
disassembler. It first builds an atom for each section. It can also
construct the CFG, and this splits the text atoms into basic blocks.
MCModule and MCAtom were only sketched out; MCFunction and MCBB were
implemented under the experimental "-cfg" llvm-objdump -macho option.
This cleans them up for further use; llvm-objdump -d -cfg now generates
graphviz files for each function found in the binary.
In the future, MCObjectDisassembler may be the right place to do
"intelligent" disassembly: for example, handling constant islands is just
a matter of splitting the atom, using information that may be available
in the ObjectFile. Also, better initial atom formation than just using
sections is possible using symbols (and things like Mach-O's
function_starts load command).
This brings two minor regressions in llvm-objdump -macho -cfg:
- The printing of a relocation's referenced symbol.
- An annotation on loop BBs, i.e., which are their own successor.
Relocation printing is replaced by the MCSymbolizer; the basic CFG
annotation will be superseded by more related functionality.
llvm-svn: 182628
2013-05-24 03:07:04 +02:00
|
|
|
/// @{
|
|
|
|
/// \brief Get the entry point basic block.
|
|
|
|
const MCBasicBlock *getEntryBlock() const { return front(); }
|
|
|
|
MCBasicBlock *getEntryBlock() { return front(); }
|
|
|
|
|
2013-08-21 09:27:59 +02:00
|
|
|
bool empty() const { return Blocks.empty(); }
|
|
|
|
|
MC: Disassembled CFG reconstruction.
This patch builds on some existing code to do CFG reconstruction from
a disassembled binary:
- MCModule represents the binary, and has a list of MCAtoms.
- MCAtom represents either disassembled instructions (MCTextAtom), or
contiguous data (MCDataAtom), and covers a specific range of addresses.
- MCBasicBlock and MCFunction form the reconstructed CFG. An MCBB is
backed by an MCTextAtom, and has the usual successors/predecessors.
- MCObjectDisassembler creates a module from an ObjectFile using a
disassembler. It first builds an atom for each section. It can also
construct the CFG, and this splits the text atoms into basic blocks.
MCModule and MCAtom were only sketched out; MCFunction and MCBB were
implemented under the experimental "-cfg" llvm-objdump -macho option.
This cleans them up for further use; llvm-objdump -d -cfg now generates
graphviz files for each function found in the binary.
In the future, MCObjectDisassembler may be the right place to do
"intelligent" disassembly: for example, handling constant islands is just
a matter of splitting the atom, using information that may be available
in the ObjectFile. Also, better initial atom formation than just using
sections is possible using symbols (and things like Mach-O's
function_starts load command).
This brings two minor regressions in llvm-objdump -macho -cfg:
- The printing of a relocation's referenced symbol.
- An annotation on loop BBs, i.e., which are their own successor.
Relocation printing is replaced by the MCSymbolizer; the basic CFG
annotation will be superseded by more related functionality.
llvm-svn: 182628
2013-05-24 03:07:04 +02:00
|
|
|
typedef BasicBlockListTy::const_iterator const_iterator;
|
|
|
|
typedef BasicBlockListTy:: iterator iterator;
|
|
|
|
const_iterator begin() const { return Blocks.begin(); }
|
|
|
|
iterator begin() { return Blocks.begin(); }
|
|
|
|
const_iterator end() const { return Blocks.end(); }
|
|
|
|
iterator end() { return Blocks.end(); }
|
|
|
|
|
2014-04-15 06:56:29 +02:00
|
|
|
const MCBasicBlock* front() const { return Blocks.front().get(); }
|
|
|
|
MCBasicBlock* front() { return Blocks.front().get(); }
|
|
|
|
const MCBasicBlock* back() const { return Blocks.back().get(); }
|
|
|
|
MCBasicBlock* back() { return Blocks.back().get(); }
|
2013-08-21 09:27:59 +02:00
|
|
|
|
|
|
|
/// \brief Find the basic block, if any, that starts at \p StartAddr.
|
|
|
|
const MCBasicBlock *find(uint64_t StartAddr) const;
|
|
|
|
MCBasicBlock *find(uint64_t StartAddr);
|
MC: Disassembled CFG reconstruction.
This patch builds on some existing code to do CFG reconstruction from
a disassembled binary:
- MCModule represents the binary, and has a list of MCAtoms.
- MCAtom represents either disassembled instructions (MCTextAtom), or
contiguous data (MCDataAtom), and covers a specific range of addresses.
- MCBasicBlock and MCFunction form the reconstructed CFG. An MCBB is
backed by an MCTextAtom, and has the usual successors/predecessors.
- MCObjectDisassembler creates a module from an ObjectFile using a
disassembler. It first builds an atom for each section. It can also
construct the CFG, and this splits the text atoms into basic blocks.
MCModule and MCAtom were only sketched out; MCFunction and MCBB were
implemented under the experimental "-cfg" llvm-objdump -macho option.
This cleans them up for further use; llvm-objdump -d -cfg now generates
graphviz files for each function found in the binary.
In the future, MCObjectDisassembler may be the right place to do
"intelligent" disassembly: for example, handling constant islands is just
a matter of splitting the atom, using information that may be available
in the ObjectFile. Also, better initial atom formation than just using
sections is possible using symbols (and things like Mach-O's
function_starts load command).
This brings two minor regressions in llvm-objdump -macho -cfg:
- The printing of a relocation's referenced symbol.
- An annotation on loop BBs, i.e., which are their own successor.
Relocation printing is replaced by the MCSymbolizer; the basic CFG
annotation will be superseded by more related functionality.
llvm-svn: 182628
2013-05-24 03:07:04 +02:00
|
|
|
/// @}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|