MC CFG: Add an MCObjectSymbolizer in the MCObjectDisassembler.
[oota-llvm.git] / include / llvm / MC / MCObjectDisassembler.h
1 //===-- llvm/MC/MCObjectDisassembler.h --------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
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.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_MC_MCOBJECTDISASSEMBLER_H
16 #define LLVM_MC_MCOBJECTDISASSEMBLER_H
17
18 #include "llvm/ADT/ArrayRef.h"
19 #include "llvm/ADT/StringRef.h"
20 #include "llvm/Support/DataTypes.h"
21
22 namespace llvm {
23
24 namespace object {
25   class ObjectFile;
26   class MachOObjectFile;
27 }
28
29 class MCBasicBlock;
30 class MCDisassembler;
31 class MCFunction;
32 class MCInstrAnalysis;
33 class MCModule;
34 class MCObjectSymbolizer;
35
36 /// \brief Disassemble an ObjectFile to an MCModule and MCFunctions.
37 /// This class builds on MCDisassembler to disassemble whole sections, creating
38 /// MCAtom (MCTextAtom for disassembled sections and MCDataAtom for raw data).
39 /// It can also be used to create a control flow graph consisting of MCFunctions
40 /// and MCBasicBlocks.
41 class MCObjectDisassembler {
42 public:
43   MCObjectDisassembler(const object::ObjectFile &Obj,
44                        const MCDisassembler &Dis,
45                        const MCInstrAnalysis &MIA);
46   virtual ~MCObjectDisassembler() {}
47
48   /// \brief Build an MCModule, creating atoms and optionally functions.
49   /// \param withCFG Also build a CFG by adding MCFunctions to the Module.
50   /// If withCFG is false, the MCModule built only contains atoms, representing
51   /// what was found in the object file. If withCFG is true, MCFunctions are
52   /// created, containing MCBasicBlocks. All text atoms are split to form basic
53   /// block atoms, which then each back an MCBasicBlock.
54   MCModule *buildModule(bool withCFG = false);
55
56   MCModule *buildEmptyModule();
57
58   /// \brief Set the symbolizer to use to get information on external functions.
59   /// Note that this isn't used to do instruction-level symbolization (that is,
60   /// plugged into MCDisassembler), but to symbolize function call targets.
61   void setSymbolizer(MCObjectSymbolizer *ObjectSymbolizer) {
62     MOS = ObjectSymbolizer;
63   }
64
65   /// \brief Get the effective address of the entrypoint, or 0 if there is none.
66   virtual uint64_t getEntrypoint();
67
68   /// \name Get the addresses of static constructors/destructors in the object.
69   /// The caller is expected to know how to interpret the addresses;
70   /// for example, Mach-O init functions expect 5 arguments, not for ELF.
71   /// The addresses are original object file load addresses, not effective.
72   /// @{
73   virtual ArrayRef<uint64_t> getStaticInitFunctions();
74   virtual ArrayRef<uint64_t> getStaticExitFunctions();
75   /// @}
76
77   /// \name Translation between effective and objectfile load address.
78   /// @{
79   /// \brief Compute the effective load address, from an objectfile virtual
80   /// address. This is implemented in a format-specific way, to take into
81   /// account things like PIE/ASLR when doing dynamic disassembly.
82   /// For example, on Mach-O this would be done by adding the VM addr slide,
83   /// on glibc ELF by keeping a map between segment load addresses, filled
84   /// using dl_iterate_phdr, etc..
85   /// In most static situations and in the default impl., this returns \p Addr.
86   virtual uint64_t getEffectiveLoadAddr(uint64_t Addr);
87
88   /// \brief Compute the original load address, as specified in the objectfile.
89   /// This is the inverse of getEffectiveLoadAddr.
90   virtual uint64_t getOriginalLoadAddr(uint64_t EffectiveAddr);
91   /// @}
92
93 protected:
94   const object::ObjectFile &Obj;
95   const MCDisassembler &Dis;
96   const MCInstrAnalysis &MIA;
97   MCObjectSymbolizer *MOS;
98
99 private:
100   /// \brief Fill \p Module by creating an atom for each section.
101   /// This could be made much smarter, using information like symbols, but also
102   /// format-specific features, like mach-o function_start or data_in_code LCs.
103   void buildSectionAtoms(MCModule *Module);
104
105   /// \brief Enrich \p Module with a CFG consisting of MCFunctions.
106   /// \param Module An MCModule returned by buildModule, with no CFG.
107   /// NOTE: Each MCBasicBlock in a MCFunction is backed by a single MCTextAtom.
108   /// When the CFG is built, contiguous instructions that were previously in a
109   /// single MCTextAtom will be split in multiple basic block atoms.
110   void buildCFG(MCModule *Module);
111 };
112
113 class MCMachOObjectDisassembler : public MCObjectDisassembler {
114   const object::MachOObjectFile &MOOF;
115
116   uint64_t VMAddrSlide;
117   uint64_t HeaderLoadAddress;
118
119   // __DATA;__mod_init_func support.
120   llvm::StringRef ModInitContents;
121   // __DATA;__mod_exit_func support.
122   llvm::StringRef ModExitContents;
123
124 public:
125   /// \brief Construct a Mach-O specific object disassembler.
126   /// \param VMAddrSlide The virtual address slide applied by dyld.
127   /// \param HeaderLoadAddress The load address of the mach_header for this
128   /// object.
129   MCMachOObjectDisassembler(const object::MachOObjectFile &MOOF,
130                             const MCDisassembler &Dis,
131                             const MCInstrAnalysis &MIA, uint64_t VMAddrSlide,
132                             uint64_t HeaderLoadAddress);
133
134 protected:
135   uint64_t getEffectiveLoadAddr(uint64_t Addr) LLVM_OVERRIDE;
136   uint64_t getOriginalLoadAddr(uint64_t EffectiveAddr) LLVM_OVERRIDE;
137   uint64_t getEntrypoint() LLVM_OVERRIDE;
138
139   ArrayRef<uint64_t> getStaticInitFunctions() LLVM_OVERRIDE;
140   ArrayRef<uint64_t> getStaticExitFunctions() LLVM_OVERRIDE;
141 };
142
143 }
144
145 #endif