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/StringRef.h"
20 #include "llvm/Support/DataTypes.h"
26 class MachOObjectFile;
32 class MCInstrAnalysis;
34 class MCObjectSymbolizer;
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 {
43 MCObjectDisassembler(const object::ObjectFile &Obj,
44 const MCDisassembler &Dis,
45 const MCInstrAnalysis &MIA);
46 virtual ~MCObjectDisassembler() {}
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);
56 MCModule *buildEmptyModule();
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;
65 /// \brief Get the effective address of the entrypoint, or 0 if there is none.
66 virtual uint64_t getEntrypoint();
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.
73 virtual ArrayRef<uint64_t> getStaticInitFunctions();
74 virtual ArrayRef<uint64_t> getStaticExitFunctions();
77 /// \name Translation between effective and objectfile load address.
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);
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);
94 const object::ObjectFile &Obj;
95 const MCDisassembler &Dis;
96 const MCInstrAnalysis &MIA;
97 MCObjectSymbolizer *MOS;
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);
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);
113 class MCMachOObjectDisassembler : public MCObjectDisassembler {
114 const object::MachOObjectFile &MOOF;
116 uint64_t VMAddrSlide;
117 uint64_t HeaderLoadAddress;
119 // __DATA;__mod_init_func support.
120 llvm::StringRef ModInitContents;
121 // __DATA;__mod_exit_func support.
122 llvm::StringRef ModExitContents;
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
129 MCMachOObjectDisassembler(const object::MachOObjectFile &MOOF,
130 const MCDisassembler &Dis,
131 const MCInstrAnalysis &MIA, uint64_t VMAddrSlide,
132 uint64_t HeaderLoadAddress);
135 uint64_t getEffectiveLoadAddr(uint64_t Addr) LLVM_OVERRIDE;
136 uint64_t getOriginalLoadAddr(uint64_t EffectiveAddr) LLVM_OVERRIDE;
137 uint64_t getEntrypoint() LLVM_OVERRIDE;
139 ArrayRef<uint64_t> getStaticInitFunctions() LLVM_OVERRIDE;
140 ArrayRef<uint64_t> getStaticExitFunctions() LLVM_OVERRIDE;