1 //===-- llvm/MC/MCObjectDisassembler.h --------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains the declaration of the MCObjectDisassembler class, which
11 // can be used to construct an MCModule and an MC CFG from an ObjectFile.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_MC_MCOBJECTDISASSEMBLER_H
16 #define LLVM_MC_MCOBJECTDISASSEMBLER_H
18 #include "llvm/ADT/ArrayRef.h"
19 #include "llvm/ADT/OwningPtr.h"
20 #include "llvm/ADT/StringRef.h"
21 #include "llvm/Support/DataTypes.h"
22 #include "llvm/Support/MemoryObject.h"
29 class MachOObjectFile;
35 class MCInstrAnalysis;
37 class MCObjectSymbolizer;
39 /// \brief Disassemble an ObjectFile to an MCModule and MCFunctions.
40 /// This class builds on MCDisassembler to disassemble whole sections, creating
41 /// MCAtom (MCTextAtom for disassembled sections and MCDataAtom for raw data).
42 /// It can also be used to create a control flow graph consisting of MCFunctions
43 /// and MCBasicBlocks.
44 class MCObjectDisassembler {
46 MCObjectDisassembler(const object::ObjectFile &Obj,
47 const MCDisassembler &Dis,
48 const MCInstrAnalysis &MIA);
49 virtual ~MCObjectDisassembler() {}
51 /// \brief Build an MCModule, creating atoms and optionally functions.
52 /// \param withCFG Also build a CFG by adding MCFunctions to the Module.
53 /// If withCFG is false, the MCModule built only contains atoms, representing
54 /// what was found in the object file. If withCFG is true, MCFunctions are
55 /// created, containing MCBasicBlocks. All text atoms are split to form basic
56 /// block atoms, which then each back an MCBasicBlock.
57 MCModule *buildModule(bool withCFG = false);
59 MCModule *buildEmptyModule();
61 typedef std::vector<uint64_t> AddressSetTy;
62 /// \name Create a new MCFunction.
63 MCFunction *createFunction(MCModule *Module, uint64_t BeginAddr,
64 AddressSetTy &CallTargets,
65 AddressSetTy &TailCallTargets);
67 /// \brief Set the region on which to fallback if disassembly was requested
68 /// somewhere not accessible in the object file.
69 /// This is used for dynamic disassembly (see RawMemoryObject).
70 void setFallbackRegion(OwningPtr<MemoryObject> &Region) {
71 FallbackRegion.reset(Region.take());
74 /// \brief Set the symbolizer to use to get information on external functions.
75 /// Note that this isn't used to do instruction-level symbolization (that is,
76 /// plugged into MCDisassembler), but to symbolize function call targets.
77 void setSymbolizer(MCObjectSymbolizer *ObjectSymbolizer) {
78 MOS = ObjectSymbolizer;
81 /// \brief Get the effective address of the entrypoint, or 0 if there is none.
82 virtual uint64_t getEntrypoint();
84 /// \name Get the addresses of static constructors/destructors in the object.
85 /// The caller is expected to know how to interpret the addresses;
86 /// for example, Mach-O init functions expect 5 arguments, not for ELF.
87 /// The addresses are original object file load addresses, not effective.
89 virtual ArrayRef<uint64_t> getStaticInitFunctions();
90 virtual ArrayRef<uint64_t> getStaticExitFunctions();
93 /// \name Translation between effective and objectfile load address.
95 /// \brief Compute the effective load address, from an objectfile virtual
96 /// address. This is implemented in a format-specific way, to take into
97 /// account things like PIE/ASLR when doing dynamic disassembly.
98 /// For example, on Mach-O this would be done by adding the VM addr slide,
99 /// on glibc ELF by keeping a map between segment load addresses, filled
100 /// using dl_iterate_phdr, etc..
101 /// In most static situations and in the default impl., this returns \p Addr.
102 virtual uint64_t getEffectiveLoadAddr(uint64_t Addr);
104 /// \brief Compute the original load address, as specified in the objectfile.
105 /// This is the inverse of getEffectiveLoadAddr.
106 virtual uint64_t getOriginalLoadAddr(uint64_t EffectiveAddr);
110 const object::ObjectFile &Obj;
111 const MCDisassembler &Dis;
112 const MCInstrAnalysis &MIA;
113 MCObjectSymbolizer *MOS;
115 /// \brief The fallback memory region, outside the object file.
116 OwningPtr<MemoryObject> FallbackRegion;
118 /// \brief Return a memory region suitable for reading starting at \p Addr.
119 /// In most cases, this returns a StringRefMemoryObject backed by the
120 /// containing section. When no section was found, this returns the
121 /// FallbackRegion, if it is suitable.
122 /// If it is not, or if there is no fallback region, this returns 0.
123 MemoryObject *getRegionFor(uint64_t Addr);
126 /// \brief Fill \p Module by creating an atom for each section.
127 /// This could be made much smarter, using information like symbols, but also
128 /// format-specific features, like mach-o function_start or data_in_code LCs.
129 void buildSectionAtoms(MCModule *Module);
131 /// \brief Enrich \p Module with a CFG consisting of MCFunctions.
132 /// \param Module An MCModule returned by buildModule, with no CFG.
133 /// NOTE: Each MCBasicBlock in a MCFunction is backed by a single MCTextAtom.
134 /// When the CFG is built, contiguous instructions that were previously in a
135 /// single MCTextAtom will be split in multiple basic block atoms.
136 void buildCFG(MCModule *Module);
138 MCBasicBlock *getBBAt(MCModule *Module, MCFunction *MCFN, uint64_t BeginAddr,
139 AddressSetTy &CallTargets,
140 AddressSetTy &TailCallTargets);
143 class MCMachOObjectDisassembler : public MCObjectDisassembler {
144 const object::MachOObjectFile &MOOF;
146 uint64_t VMAddrSlide;
147 uint64_t HeaderLoadAddress;
149 // __DATA;__mod_init_func support.
150 llvm::StringRef ModInitContents;
151 // __DATA;__mod_exit_func support.
152 llvm::StringRef ModExitContents;
155 /// \brief Construct a Mach-O specific object disassembler.
156 /// \param VMAddrSlide The virtual address slide applied by dyld.
157 /// \param HeaderLoadAddress The load address of the mach_header for this
159 MCMachOObjectDisassembler(const object::MachOObjectFile &MOOF,
160 const MCDisassembler &Dis,
161 const MCInstrAnalysis &MIA, uint64_t VMAddrSlide,
162 uint64_t HeaderLoadAddress);
165 uint64_t getEffectiveLoadAddr(uint64_t Addr) LLVM_OVERRIDE;
166 uint64_t getOriginalLoadAddr(uint64_t EffectiveAddr) LLVM_OVERRIDE;
167 uint64_t getEntrypoint() LLVM_OVERRIDE;
169 ArrayRef<uint64_t> getStaticInitFunctions() LLVM_OVERRIDE;
170 ArrayRef<uint64_t> getStaticExitFunctions() LLVM_OVERRIDE;