ROSE  0.11.2.0
ModulesElf.h
1 #ifndef ROSE_Partitioner2_ModulesElf_H
2 #define ROSE_Partitioner2_ModulesElf_H
3 
4 #include <rosePublicConfig.h>
5 #ifdef ROSE_BUILD_BINARY_ANALYSIS_SUPPORT
6 
7 #include <Partitioner2/BasicTypes.h>
8 #include <Partitioner2/Function.h>
9 #include <Partitioner2/Modules.h>
10 
11 #include <boost/filesystem.hpp>
12 
13 namespace Rose {
14 namespace BinaryAnalysis {
15 namespace Partitioner2 {
16 
18 namespace ModulesElf {
19 
26 std::vector<Function::Ptr> findErrorHandlingFunctions(SgAsmElfFileHeader*);
27 std::vector<Function::Ptr> findErrorHandlingFunctions(SgAsmInterpretation*);
28 size_t findErrorHandlingFunctions(SgAsmElfFileHeader*, std::vector<Function::Ptr>&);
34 std::vector<Function::Ptr> findPltFunctions(const Partitioner&, SgAsmElfFileHeader*);
35 std::vector<Function::Ptr> findPltFunctions(const Partitioner&, SgAsmInterpretation*);
36 size_t findPltFunctions(const Partitioner&, SgAsmElfFileHeader*, std::vector<Function::Ptr>&);
40 SgAsmGenericSection* findPltGot(const Partitioner &partitioner, SgAsmElfFileHeader *elfHeader);
41 
43 struct PltInfo {
44  SgAsmGenericSection *section;
45  size_t firstOffset;
46  size_t entrySize;
48  PltInfo()
49  : section(NULL), firstOffset(0), entrySize(0) {}
50 };
51 
53 PltInfo findPlt(const Partitioner&, SgAsmElfFileHeader*);
54 
58 std::vector<SgAsmElfSection*> findSectionsByName(SgAsmInterpretation*, const std::string&);
59 
64 bool isImport(const Partitioner&, const Function::Ptr&);
65 
70 bool isLinkedImport(const Partitioner&, const Function::Ptr&);
71 
75 bool isUnlinkedImport(const Partitioner&, const Function::Ptr&);
76 
81 bool isObjectFile(const boost::filesystem::path&);
82 
87 bool isStaticArchive(const boost::filesystem::path&);
88 
90 namespace FixUndefinedSymbols {
92 enum Boolean {
93  NO,
94  YES
95 };
96 } // namespace
97 
102 bool tryLink(const std::string &command, const boost::filesystem::path &outputName,
103  std::vector<boost::filesystem::path> inputNames, Sawyer::Message::Stream &errors,
105 
111 std::vector<boost::filesystem::path>
112 extractStaticArchive(const boost::filesystem::path &directory, const boost::filesystem::path &archive);
113 
117  // These data members are generally optional, and filled in as they're matched.
118  rose_addr_t baseVa_; // base address for computing memAddress_
119  rose_addr_t gotEntryVa_; // address through which an indirect branch branches
120  size_t gotEntryNBytes_; // size of the global offset table entry in bytes
121  rose_addr_t gotEntry_; // address read from the GOT if the address is mapped (or zero)
122  size_t nBytesMatched_; // number of bytes matched for PLT entry
123  rose_addr_t functionNumber_; // function number argument for the dynamic linker (usually a push)
124 
125 public:
126  PltEntryMatcher(rose_addr_t base)
127  : baseVa_(base), gotEntryVa_(0), gotEntryNBytes_(0), gotEntry_(0), nBytesMatched_(0) {}
128  static Ptr instance(rose_addr_t base) { return Ptr(new PltEntryMatcher(base)); }
129  virtual bool match(const Partitioner&, rose_addr_t anchor);
130 
132  size_t nBytesMatched() const { return nBytesMatched_; }
133 
135  rose_addr_t gotEntryVa() const { return gotEntryVa_; }
136 
138  size_t gotEntryNBytes() const { return gotEntryNBytes_; }
139 
141  rose_addr_t gotEntry() const { return gotEntry_; }
142 
143  // [Robb Matzke 2018-04-06]: deprecated: use gotEntryVa
144  rose_addr_t memAddress() const { return gotEntryVa_; }
145 
146 private:
147  SgAsmInstruction* matchNop(const Partitioner&, rose_addr_t va);
148  SgAsmInstruction* matchPush(const Partitioner&, rose_addr_t var, rose_addr_t &n /*out*/);
149  SgAsmInstruction* matchDirectJump(const Partitioner&, rose_addr_t va);
150  SgAsmInstruction* matchIndirectJump(const Partitioner&, rose_addr_t va,
151  rose_addr_t &indirectVa /*out*/, size_t &indirectNBytes /*out*/);
152  SgAsmInstruction* matchA64Adrp(const Partitioner&, rose_addr_t va, rose_addr_t &value /*out*/);
153  SgAsmInstruction* matchA64Ldr(const Partitioner&, rose_addr_t va, rose_addr_t &indirectVa /*in,out*/,
154  rose_addr_t &indirectNBytes /*out*/);
155  SgAsmInstruction* matchA64Add(const Partitioner&, rose_addr_t va);
156  SgAsmInstruction* matchA64Br(const Partitioner&, rose_addr_t va);
157 };
158 
160 void buildMayReturnLists(Partitioner&);
161 
162 } // namespace
163 } // namespace
164 } // namespace
165 } // namespace
166 
167 #endif
168 #endif
std::vector< Function::Ptr > findPltFunctions(const Partitioner &, SgAsmElfFileHeader *)
Reads ELF PLT sections and returns a list of functions.
std::vector< SgAsmElfSection * > findSectionsByName(SgAsmInterpretation *, const std::string &)
Get a list of all ELF sections by name.
Contiguous region of a file.
bool tryLink(const std::string &command, const boost::filesystem::path &outputName, std::vector< boost::filesystem::path > inputNames, Sawyer::Message::Stream &errors, FixUndefinedSymbols::Boolean fixUndefinedSymbols=FixUndefinedSymbols::YES)
Try to run a link command.
rose_addr_t gotEntry() const
Value stored in the GOT entry.
Definition: ModulesElf.h:141
bool isObjectFile(const boost::filesystem::path &)
True if named file is an ELF object file.
Represents the file header of an ELF binary container.
PltInfo findPlt(const Partitioner &, SgAsmElfFileHeader *)
Find information about the PLT.
bool isStaticArchive(const boost::filesystem::path &)
True if named file is a static library archive.
Base class for machine instructions.
rose_addr_t gotEntryVa() const
Address of the corresponding GOT entry.
Definition: ModulesElf.h:135
std::vector< boost::filesystem::path > extractStaticArchive(const boost::filesystem::path &directory, const boost::filesystem::path &archive)
Extract object files from a static archive.
Information about the procedure lookup table.
Definition: ModulesElf.h:43
void buildMayReturnLists(Partitioner &)
Build may-return white and black lists.
size_t entrySize
Size of each entry in bytes.
Definition: ModulesElf.h:46
Main namespace for the ROSE library.
SgAsmGenericSection * findPltGot(const Partitioner &partitioner, SgAsmElfFileHeader *elfHeader)
Find the dynamic linking PLT GOT section.
Base class for matching an instruction pattern.
Definition: Modules.h:82
std::vector< Function::Ptr > findErrorHandlingFunctions(SgAsmElfFileHeader *)
Reads ELF .eh_frames to find function entry addresses.
virtual bool match(const Partitioner &, rose_addr_t anchor)
Attempt to match an instruction pattern.
bool isLinkedImport(const Partitioner &, const Function::Ptr &)
True if function is a linked import.
bool isImport(const Partitioner &, const Function::Ptr &)
True if the function is an import.
size_t nBytesMatched() const
Size of the PLT entry in bytes.
Definition: ModulesElf.h:132
Sawyer::SharedPointer< InstructionMatcher > Ptr
Shared-ownership pointer to an InstructionMatcher.
Definition: Modules.h:85
Converts text to messages.
Definition: Message.h:1396
Partitions instructions into basic blocks and functions.
Definition: Partitioner.h:322
Represents an interpretation of a binary container.
size_t gotEntryNBytes() const
Size of the GOT entry in bytes.
Definition: ModulesElf.h:138
bool isUnlinkedImport(const Partitioner &, const Function::Ptr &)
True if function is a non-linked import.