12c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha//===-- llvm/MC/MCObjectSymbolizer.h --------------------------------------===//
22c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha//
32c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha//                     The LLVM Compiler Infrastructure
42c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha//
52c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha// This file is distributed under the University of Illinois Open Source
62c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha// License. See LICENSE.TXT for details.
72c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha//
82c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha//===----------------------------------------------------------------------===//
92c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha//
102c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha// This file declares the MCObjectSymbolizer class, an MCSymbolizer that is
112c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha// backed by an object::ObjectFile.
122c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha//
132c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha//===----------------------------------------------------------------------===//
142c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha
152c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha#ifndef LLVM_MC_MCOBJECTSYMBOLIZER_H
162c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha#define LLVM_MC_MCOBJECTSYMBOLIZER_H
172c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha
182c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha#include "llvm/ADT/DenseMap.h"
192c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha#include "llvm/MC/MCSymbolizer.h"
202c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha#include "llvm/Object/ObjectFile.h"
21b54d29735af9ddabce268c817ccc2ab3eb54d719Ahmed Bougacha#include <vector>
222c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha
232c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougachanamespace llvm {
242c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha
252c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougachaclass MCExpr;
262c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougachaclass MCInst;
272c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougachaclass MCRelocationInfo;
282c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougachaclass raw_ostream;
292c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha
302c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha/// \brief An ObjectFile-backed symbolizer.
312c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougachaclass MCObjectSymbolizer : public MCSymbolizer {
322c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougachaprotected:
332c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha  const object::ObjectFile *Obj;
342c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha
352c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha  // Map a load address to the first relocation that applies there. As far as I
362c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha  // know, if there are several relocations at the exact same address, they are
372c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha  // related and the others can be determined from the first that was found in
382c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha  // the relocation table. For instance, on x86-64 mach-o, a SUBTRACTOR
392c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha  // relocation (referencing the minuend symbol) is followed by an UNSIGNED
402c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha  // relocation (referencing the subtrahend symbol).
41cdef37a9d8d559042fe43d8ae91d4b65f281df69Ahmed Bougacha  const object::RelocationRef *findRelocationAt(uint64_t Addr);
42cdef37a9d8d559042fe43d8ae91d4b65f281df69Ahmed Bougacha  const object::SectionRef *findSectionContaining(uint64_t Addr);
43b54d29735af9ddabce268c817ccc2ab3eb54d719Ahmed Bougacha
4436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  MCObjectSymbolizer(MCContext &Ctx, std::unique_ptr<MCRelocationInfo> RelInfo,
452c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha                     const object::ObjectFile *Obj);
462c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha
472c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougachapublic:
482c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha  /// \name Overridden MCSymbolizer methods:
492c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha  /// @{
502c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha  bool tryAddingSymbolicOperand(MCInst &MI, raw_ostream &cStream,
51cdef37a9d8d559042fe43d8ae91d4b65f281df69Ahmed Bougacha                                int64_t Value, uint64_t Address,
52cdef37a9d8d559042fe43d8ae91d4b65f281df69Ahmed Bougacha                                bool IsBranch, uint64_t Offset,
5336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines                                uint64_t InstSize) override;
542c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha
552c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha  void tryAddingPcLoadReferenceComment(raw_ostream &cStream,
5636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines                                       int64_t Value,
5736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines                                       uint64_t Address) override;
582c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha  /// @}
592c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha
609bfc0626c02e449dd321a71a09f005ac8239e921Ahmed Bougacha  /// \brief Look for an external function symbol at \p Addr.
619bfc0626c02e449dd321a71a09f005ac8239e921Ahmed Bougacha  /// (References through the ELF PLT, Mach-O stubs, and similar).
629bfc0626c02e449dd321a71a09f005ac8239e921Ahmed Bougacha  /// \returns An MCExpr representing the external symbol, or 0 if not found.
639bfc0626c02e449dd321a71a09f005ac8239e921Ahmed Bougacha  virtual StringRef findExternalFunctionAt(uint64_t Addr);
649bfc0626c02e449dd321a71a09f005ac8239e921Ahmed Bougacha
652c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha  /// \brief Create an object symbolizer for \p Obj.
662c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha  static MCObjectSymbolizer *
6736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  createObjectSymbolizer(MCContext &Ctx,
6836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines                         std::unique_ptr<MCRelocationInfo> RelInfo,
6936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines                         const object::ObjectFile *Obj);
70cdef37a9d8d559042fe43d8ae91d4b65f281df69Ahmed Bougacha
71cdef37a9d8d559042fe43d8ae91d4b65f281df69Ahmed Bougachaprivate:
72cdef37a9d8d559042fe43d8ae91d4b65f281df69Ahmed Bougacha  typedef DenseMap<uint64_t, object::RelocationRef> AddrToRelocMap;
73cdef37a9d8d559042fe43d8ae91d4b65f281df69Ahmed Bougacha  typedef std::vector<object::SectionRef> SortedSectionList;
74cdef37a9d8d559042fe43d8ae91d4b65f281df69Ahmed Bougacha  SortedSectionList SortedSections;
75cdef37a9d8d559042fe43d8ae91d4b65f281df69Ahmed Bougacha  AddrToRelocMap AddrToReloc;
76cdef37a9d8d559042fe43d8ae91d4b65f281df69Ahmed Bougacha
77cdef37a9d8d559042fe43d8ae91d4b65f281df69Ahmed Bougacha  void buildSectionList();
78cdef37a9d8d559042fe43d8ae91d4b65f281df69Ahmed Bougacha  void buildRelocationByAddrMap();
792c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha};
802c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha
812c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha}
822c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha
832c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha#endif
84