MC CFG: Add MCObjectDisassembler support for entrypoint + static ctors.
[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 }
27
28 class MCBasicBlock;
29 class MCDisassembler;
30 class MCFunction;
31 class MCInstrAnalysis;
32 class MCModule;
33
34 /// \brief Disassemble an ObjectFile to an MCModule and MCFunctions.
35 /// This class builds on MCDisassembler to disassemble whole sections, creating
36 /// MCAtom (MCTextAtom for disassembled sections and MCDataAtom for raw data).
37 /// It can also be used to create a control flow graph consisting of MCFunctions
38 /// and MCBasicBlocks.
39 class MCObjectDisassembler {
40 public:
41   MCObjectDisassembler(const object::ObjectFile &Obj,
42                        const MCDisassembler &Dis,
43                        const MCInstrAnalysis &MIA);
44   virtual ~MCObjectDisassembler() {}
45
46   /// \brief Build an MCModule, creating atoms and optionally functions.
47   /// \param withCFG Also build a CFG by adding MCFunctions to the Module.
48   /// If withCFG is false, the MCModule built only contains atoms, representing
49   /// what was found in the object file. If withCFG is true, MCFunctions are
50   /// created, containing MCBasicBlocks. All text atoms are split to form basic
51   /// block atoms, which then each back an MCBasicBlock.
52   MCModule *buildModule(bool withCFG = false);
53
54   MCModule *buildEmptyModule();
55
56   /// \brief Get the effective address of the entrypoint, or 0 if there is none.
57   virtual uint64_t getEntrypoint();
58
59   /// \name Get the addresses of static constructors/destructors in the object.
60   /// The caller is expected to know how to interpret the addresses;
61   /// for example, Mach-O init functions expect 5 arguments, not for ELF.
62   /// The addresses are original object file load addresses, not effective.
63   /// @{
64   virtual ArrayRef<uint64_t> getStaticInitFunctions();
65   virtual ArrayRef<uint64_t> getStaticExitFunctions();
66   /// @}
67
68 protected:
69   const object::ObjectFile &Obj;
70   const MCDisassembler &Dis;
71   const MCInstrAnalysis &MIA;
72
73 private:
74   /// \brief Fill \p Module by creating an atom for each section.
75   /// This could be made much smarter, using information like symbols, but also
76   /// format-specific features, like mach-o function_start or data_in_code LCs.
77   void buildSectionAtoms(MCModule *Module);
78
79   /// \brief Enrich \p Module with a CFG consisting of MCFunctions.
80   /// \param Module An MCModule returned by buildModule, with no CFG.
81   /// NOTE: Each MCBasicBlock in a MCFunction is backed by a single MCTextAtom.
82   /// When the CFG is built, contiguous instructions that were previously in a
83   /// single MCTextAtom will be split in multiple basic block atoms.
84   void buildCFG(MCModule *Module);
85 };
86
87 }
88
89 #endif