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"
31 class MCInstrAnalysis;
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 {
41 MCObjectDisassembler(const object::ObjectFile &Obj,
42 const MCDisassembler &Dis,
43 const MCInstrAnalysis &MIA);
44 virtual ~MCObjectDisassembler() {}
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);
54 MCModule *buildEmptyModule();
56 /// \brief Get the effective address of the entrypoint, or 0 if there is none.
57 virtual uint64_t getEntrypoint();
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.
64 virtual ArrayRef<uint64_t> getStaticInitFunctions();
65 virtual ArrayRef<uint64_t> getStaticExitFunctions();
69 const object::ObjectFile &Obj;
70 const MCDisassembler &Dis;
71 const MCInstrAnalysis &MIA;
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);
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);