LCOV - code coverage report
Current view: top level - include/llvm/Object - ObjectFile.h (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 65 70 92.9 %
Date: 2017-09-14 15:23:50 Functions: 7 9 77.8 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===- ObjectFile.h - File format independent object file -------*- 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 declares a file format independent ObjectFile class.
      11             : //
      12             : //===----------------------------------------------------------------------===//
      13             : 
      14             : #ifndef LLVM_OBJECT_OBJECTFILE_H
      15             : #define LLVM_OBJECT_OBJECTFILE_H
      16             : 
      17             : #include "llvm/ADT/StringRef.h"
      18             : #include "llvm/ADT/iterator_range.h"
      19             : #include "llvm/BinaryFormat/Magic.h"
      20             : #include "llvm/MC/SubtargetFeature.h"
      21             : #include "llvm/Object/Binary.h"
      22             : #include "llvm/Object/Error.h"
      23             : #include "llvm/Object/SymbolicFile.h"
      24             : #include "llvm/Support/Casting.h"
      25             : #include "llvm/Support/Error.h"
      26             : #include "llvm/Support/ErrorOr.h"
      27             : #include "llvm/Support/FileSystem.h"
      28             : #include "llvm/Support/MemoryBuffer.h"
      29             : #include <cassert>
      30             : #include <cstdint>
      31             : #include <memory>
      32             : #include <system_error>
      33             : 
      34             : namespace llvm {
      35             : 
      36             : class ARMAttributeParser;
      37             : 
      38             : namespace object {
      39             : 
      40             : class COFFObjectFile;
      41             : class MachOObjectFile;
      42             : class ObjectFile;
      43             : class SectionRef;
      44             : class SymbolRef;
      45             : class symbol_iterator;
      46             : class WasmObjectFile;
      47             : 
      48             : using section_iterator = content_iterator<SectionRef>;
      49             : 
      50             : /// This is a value type class that represents a single relocation in the list
      51             : /// of relocations in the object file.
      52             : class RelocationRef {
      53             :   DataRefImpl RelocationPimpl;
      54             :   const ObjectFile *OwningObject = nullptr;
      55             : 
      56             : public:
      57         174 :   RelocationRef() = default;
      58             :   RelocationRef(DataRefImpl RelocationP, const ObjectFile *Owner);
      59             : 
      60             :   bool operator==(const RelocationRef &Other) const;
      61             : 
      62             :   void moveNext();
      63             : 
      64             :   uint64_t getOffset() const;
      65             :   symbol_iterator getSymbol() const;
      66             :   uint64_t getType() const;
      67             : 
      68             :   /// @brief Get a string that represents the type of this relocation.
      69             :   ///
      70             :   /// This is for display purposes only.
      71             :   void getTypeName(SmallVectorImpl<char> &Result) const;
      72             : 
      73             :   DataRefImpl getRawDataRefImpl() const;
      74             :   const ObjectFile *getObject() const;
      75             : };
      76             : 
      77             : using relocation_iterator = content_iterator<RelocationRef>;
      78             : 
      79             : /// This is a value type class that represents a single section in the list of
      80             : /// sections in the object file.
      81             : class SectionRef {
      82             :   friend class SymbolRef;
      83             : 
      84             :   DataRefImpl SectionPimpl;
      85             :   const ObjectFile *OwningObject = nullptr;
      86             : 
      87             : public:
      88        5670 :   SectionRef() = default;
      89             :   SectionRef(DataRefImpl SectionP, const ObjectFile *Owner);
      90             : 
      91             :   bool operator==(const SectionRef &Other) const;
      92             :   bool operator!=(const SectionRef &Other) const;
      93             :   bool operator<(const SectionRef &Other) const;
      94             : 
      95             :   void moveNext();
      96             : 
      97             :   std::error_code getName(StringRef &Result) const;
      98             :   uint64_t getAddress() const;
      99             :   uint64_t getIndex() const;
     100             :   uint64_t getSize() const;
     101             :   std::error_code getContents(StringRef &Result) const;
     102             : 
     103             :   /// @brief Get the alignment of this section as the actual value (not log 2).
     104             :   uint64_t getAlignment() const;
     105             : 
     106             :   bool isCompressed() const;
     107             :   bool isText() const;
     108             :   bool isData() const;
     109             :   bool isBSS() const;
     110             :   bool isVirtual() const;
     111             :   bool isBitcode() const;
     112             : 
     113             :   bool containsSymbol(SymbolRef S) const;
     114             : 
     115             :   relocation_iterator relocation_begin() const;
     116             :   relocation_iterator relocation_end() const;
     117        3341 :   iterator_range<relocation_iterator> relocations() const {
     118       10021 :     return make_range(relocation_begin(), relocation_end());
     119             :   }
     120             :   section_iterator getRelocatedSection() const;
     121             : 
     122             :   DataRefImpl getRawDataRefImpl() const;
     123             :   const ObjectFile *getObject() const;
     124             : };
     125             : 
     126             : /// This is a value type class that represents a single symbol in the list of
     127             : /// symbols in the object file.
     128             : class SymbolRef : public BasicSymbolRef {
     129             :   friend class SectionRef;
     130             : 
     131             : public:
     132             :   enum Type {
     133             :     ST_Unknown, // Type not specified
     134             :     ST_Data,
     135             :     ST_Debug,
     136             :     ST_File,
     137             :     ST_Function,
     138             :     ST_Other
     139             :   };
     140             : 
     141        3789 :   SymbolRef() = default;
     142             :   SymbolRef(DataRefImpl SymbolP, const ObjectFile *Owner);
     143        1823 :   SymbolRef(const BasicSymbolRef &B) : BasicSymbolRef(B) {
     144             :     assert(isa<ObjectFile>(BasicSymbolRef::getObject()));
     145             :   }
     146             : 
     147             :   Expected<StringRef> getName() const;
     148             :   /// Returns the symbol virtual address (i.e. address at which it will be
     149             :   /// mapped).
     150             :   Expected<uint64_t> getAddress() const;
     151             : 
     152             :   /// Return the value of the symbol depending on the object this can be an
     153             :   /// offset or a virtual address.
     154             :   uint64_t getValue() const;
     155             : 
     156             :   /// @brief Get the alignment of this symbol as the actual value (not log 2).
     157             :   uint32_t getAlignment() const;
     158             :   uint64_t getCommonSize() const;
     159             :   Expected<SymbolRef::Type> getType() const;
     160             : 
     161             :   /// @brief Get section this symbol is defined in reference to. Result is
     162             :   /// end_sections() if it is undefined or is an absolute symbol.
     163             :   Expected<section_iterator> getSection() const;
     164             : 
     165             :   const ObjectFile *getObject() const;
     166             : };
     167             : 
     168             : class symbol_iterator : public basic_symbol_iterator {
     169             : public:
     170       20590 :   symbol_iterator(SymbolRef Sym) : basic_symbol_iterator(Sym) {}
     171        5326 :   symbol_iterator(const basic_symbol_iterator &B)
     172       31956 :       : basic_symbol_iterator(SymbolRef(B->getRawDataRefImpl(),
     173       15978 :                                         cast<ObjectFile>(B->getObject()))) {}
     174             : 
     175             :   const SymbolRef *operator->() const {
     176       15765 :     const BasicSymbolRef &P = basic_symbol_iterator::operator *();
     177             :     return static_cast<const SymbolRef*>(&P);
     178             :   }
     179             : 
     180             :   const SymbolRef &operator*() const {
     181     8232689 :     const BasicSymbolRef &P = basic_symbol_iterator::operator *();
     182             :     return static_cast<const SymbolRef&>(P);
     183             :   }
     184             : };
     185             : 
     186             : /// This class is the base class for all object file types. Concrete instances
     187             : /// of this object are created by createObjectFile, which figures out which type
     188             : /// to create.
     189        6360 : class ObjectFile : public SymbolicFile {
     190             :   virtual void anchor();
     191             : 
     192             : protected:
     193             :   ObjectFile(unsigned int Type, MemoryBufferRef Source);
     194             : 
     195             :   const uint8_t *base() const {
     196      385478 :     return reinterpret_cast<const uint8_t *>(Data.getBufferStart());
     197             :   }
     198             : 
     199             :   // These functions are for SymbolRef to call internally. The main goal of
     200             :   // this is to allow SymbolRef::SymbolPimpl to point directly to the symbol
     201             :   // entry in the memory mapped object file. SymbolPimpl cannot contain any
     202             :   // virtual functions because then it could not point into the memory mapped
     203             :   // file.
     204             :   //
     205             :   // Implementations assume that the DataRefImpl is valid and has not been
     206             :   // modified externally. It's UB otherwise.
     207             :   friend class SymbolRef;
     208             : 
     209             :   virtual Expected<StringRef> getSymbolName(DataRefImpl Symb) const = 0;
     210             :   std::error_code printSymbolName(raw_ostream &OS,
     211             :                                   DataRefImpl Symb) const override;
     212             :   virtual Expected<uint64_t> getSymbolAddress(DataRefImpl Symb) const = 0;
     213             :   virtual uint64_t getSymbolValueImpl(DataRefImpl Symb) const = 0;
     214             :   virtual uint32_t getSymbolAlignment(DataRefImpl Symb) const;
     215             :   virtual uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const = 0;
     216             :   virtual Expected<SymbolRef::Type> getSymbolType(DataRefImpl Symb) const = 0;
     217             :   virtual Expected<section_iterator>
     218             :   getSymbolSection(DataRefImpl Symb) const = 0;
     219             : 
     220             :   // Same as above for SectionRef.
     221             :   friend class SectionRef;
     222             : 
     223             :   virtual void moveSectionNext(DataRefImpl &Sec) const = 0;
     224             :   virtual std::error_code getSectionName(DataRefImpl Sec,
     225             :                                          StringRef &Res) const = 0;
     226             :   virtual uint64_t getSectionAddress(DataRefImpl Sec) const = 0;
     227             :   virtual uint64_t getSectionIndex(DataRefImpl Sec) const = 0;
     228             :   virtual uint64_t getSectionSize(DataRefImpl Sec) const = 0;
     229             :   virtual std::error_code getSectionContents(DataRefImpl Sec,
     230             :                                              StringRef &Res) const = 0;
     231             :   virtual uint64_t getSectionAlignment(DataRefImpl Sec) const = 0;
     232             :   virtual bool isSectionCompressed(DataRefImpl Sec) const = 0;
     233             :   virtual bool isSectionText(DataRefImpl Sec) const = 0;
     234             :   virtual bool isSectionData(DataRefImpl Sec) const = 0;
     235             :   virtual bool isSectionBSS(DataRefImpl Sec) const = 0;
     236             :   // A section is 'virtual' if its contents aren't present in the object image.
     237             :   virtual bool isSectionVirtual(DataRefImpl Sec) const = 0;
     238             :   virtual bool isSectionBitcode(DataRefImpl Sec) const;
     239             :   virtual relocation_iterator section_rel_begin(DataRefImpl Sec) const = 0;
     240             :   virtual relocation_iterator section_rel_end(DataRefImpl Sec) const = 0;
     241             :   virtual section_iterator getRelocatedSection(DataRefImpl Sec) const;
     242             : 
     243             :   // Same as above for RelocationRef.
     244             :   friend class RelocationRef;
     245             :   virtual void moveRelocationNext(DataRefImpl &Rel) const = 0;
     246             :   virtual uint64_t getRelocationOffset(DataRefImpl Rel) const = 0;
     247             :   virtual symbol_iterator getRelocationSymbol(DataRefImpl Rel) const = 0;
     248             :   virtual uint64_t getRelocationType(DataRefImpl Rel) const = 0;
     249             :   virtual void getRelocationTypeName(DataRefImpl Rel,
     250             :                                      SmallVectorImpl<char> &Result) const = 0;
     251             : 
     252             :   uint64_t getSymbolValue(DataRefImpl Symb) const;
     253             : 
     254             : public:
     255             :   ObjectFile() = delete;
     256             :   ObjectFile(const ObjectFile &other) = delete;
     257             : 
     258             :   uint64_t getCommonSymbolSize(DataRefImpl Symb) const {
     259             :     assert(getSymbolFlags(Symb) & SymbolRef::SF_Common);
     260         201 :     return getCommonSymbolSizeImpl(Symb);
     261             :   }
     262             : 
     263             :   using symbol_iterator_range = iterator_range<symbol_iterator>;
     264        1659 :   symbol_iterator_range symbols() const {
     265        3318 :     return symbol_iterator_range(symbol_begin(), symbol_end());
     266             :   }
     267             : 
     268             :   virtual section_iterator section_begin() const = 0;
     269             :   virtual section_iterator section_end() const = 0;
     270             : 
     271             :   using section_iterator_range = iterator_range<section_iterator>;
     272        6397 :   section_iterator_range sections() const {
     273       12794 :     return section_iterator_range(section_begin(), section_end());
     274             :   }
     275             : 
     276             :   /// @brief The number of bytes used to represent an address in this object
     277             :   ///        file format.
     278             :   virtual uint8_t getBytesInAddress() const = 0;
     279             : 
     280             :   virtual StringRef getFileFormatName() const = 0;
     281             :   virtual /* Triple::ArchType */ unsigned getArch() const = 0;
     282             :   virtual SubtargetFeatures getFeatures() const = 0;
     283           9 :   virtual void setARMSubArch(Triple &TheTriple) const { }
     284             : 
     285             :   /// Returns platform-specific object flags, if any.
     286           0 :   virtual std::error_code getPlatformFlags(unsigned &Result) const {
     287           0 :     Result = 0;
     288           0 :     return object_error::invalid_file_type;
     289             :   }
     290             : 
     291             :   virtual std::error_code
     292           0 :     getBuildAttributes(ARMAttributeParser &Attributes) const {
     293           0 :       return std::error_code();
     294             :     }
     295             : 
     296             :   /// Maps a debug section name to a standard DWARF section name.
     297       11202 :   virtual StringRef mapDebugSectionName(StringRef Name) const { return Name; }
     298             : 
     299             :   /// True if this is a relocatable object (.o/.obj).
     300             :   virtual bool isRelocatableObject() const = 0;
     301             : 
     302             :   /// @returns Pointer to ObjectFile subclass to handle this type of object.
     303             :   /// @param ObjectPath The path to the object file. ObjectPath.isObject must
     304             :   ///        return true.
     305             :   /// @brief Create ObjectFile from path.
     306             :   static Expected<OwningBinary<ObjectFile>>
     307             :   createObjectFile(StringRef ObjectPath);
     308             : 
     309             :   static Expected<std::unique_ptr<ObjectFile>>
     310             :   createObjectFile(MemoryBufferRef Object, llvm::file_magic Type);
     311             :   static Expected<std::unique_ptr<ObjectFile>>
     312             :   createObjectFile(MemoryBufferRef Object) {
     313        1112 :     return createObjectFile(Object, llvm::file_magic::unknown);
     314             :   }
     315             : 
     316             :   static bool classof(const Binary *v) {
     317        4905 :     return v->isObject();
     318             :   }
     319             : 
     320             :   static ErrorOr<std::unique_ptr<COFFObjectFile>>
     321             :   createCOFFObjectFile(MemoryBufferRef Object);
     322             : 
     323             :   static ErrorOr<std::unique_ptr<ObjectFile>>
     324             :   createELFObjectFile(MemoryBufferRef Object);
     325             : 
     326             :   static Expected<std::unique_ptr<MachOObjectFile>>
     327             :   createMachOObjectFile(MemoryBufferRef Object,
     328             :                         uint32_t UniversalCputype = 0,
     329             :                         uint32_t UniversalIndex = 0);
     330             : 
     331             :   static Expected<std::unique_ptr<WasmObjectFile>>
     332             :   createWasmObjectFile(MemoryBufferRef Object);
     333             : };
     334             : 
     335             : // Inline function definitions.
     336             : inline SymbolRef::SymbolRef(DataRefImpl SymbolP, const ObjectFile *Owner)
     337       65358 :     : BasicSymbolRef(SymbolP, Owner) {}
     338             : 
     339             : inline Expected<StringRef> SymbolRef::getName() const {
     340    16383116 :   return getObject()->getSymbolName(getRawDataRefImpl());
     341             : }
     342             : 
     343             : inline Expected<uint64_t> SymbolRef::getAddress() const {
     344    16377402 :   return getObject()->getSymbolAddress(getRawDataRefImpl());
     345             : }
     346             : 
     347        1919 : inline uint64_t SymbolRef::getValue() const {
     348        3838 :   return getObject()->getSymbolValue(getRawDataRefImpl());
     349             : }
     350             : 
     351             : inline uint32_t SymbolRef::getAlignment() const {
     352         252 :   return getObject()->getSymbolAlignment(getRawDataRefImpl());
     353             : }
     354             : 
     355             : inline uint64_t SymbolRef::getCommonSize() const {
     356         294 :   return getObject()->getCommonSymbolSize(getRawDataRefImpl());
     357             : }
     358             : 
     359             : inline Expected<section_iterator> SymbolRef::getSection() const {
     360       20854 :   return getObject()->getSymbolSection(getRawDataRefImpl());
     361             : }
     362             : 
     363             : inline Expected<SymbolRef::Type> SymbolRef::getType() const {
     364    16222512 :   return getObject()->getSymbolType(getRawDataRefImpl());
     365             : }
     366             : 
     367             : inline const ObjectFile *SymbolRef::getObject() const {
     368    24504085 :   const SymbolicFile *O = BasicSymbolRef::getObject();
     369    24504085 :   return cast<ObjectFile>(O);
     370             : }
     371             : 
     372             : /// SectionRef
     373             : inline SectionRef::SectionRef(DataRefImpl SectionP,
     374       99992 :                               const ObjectFile *Owner)
     375             :   : SectionPimpl(SectionP)
     376       99992 :   , OwningObject(Owner) {}
     377             : 
     378             : inline bool SectionRef::operator==(const SectionRef &Other) const {
     379      394882 :   return SectionPimpl == Other.SectionPimpl;
     380             : }
     381             : 
     382             : inline bool SectionRef::operator!=(const SectionRef &Other) const {
     383           4 :   return SectionPimpl != Other.SectionPimpl;
     384             : }
     385             : 
     386             : inline bool SectionRef::operator<(const SectionRef &Other) const {
     387      171200 :   return SectionPimpl < Other.SectionPimpl;
     388             : }
     389             : 
     390             : inline void SectionRef::moveNext() {
     391      132448 :   return OwningObject->moveSectionNext(SectionPimpl);
     392             : }
     393             : 
     394             : inline std::error_code SectionRef::getName(StringRef &Result) const {
     395       59336 :   return OwningObject->getSectionName(SectionPimpl, Result);
     396             : }
     397             : 
     398             : inline uint64_t SectionRef::getAddress() const {
     399       16079 :   return OwningObject->getSectionAddress(SectionPimpl);
     400             : }
     401             : 
     402             : inline uint64_t SectionRef::getIndex() const {
     403        1756 :   return OwningObject->getSectionIndex(SectionPimpl);
     404             : }
     405             : 
     406             : inline uint64_t SectionRef::getSize() const {
     407     2821916 :   return OwningObject->getSectionSize(SectionPimpl);
     408             : }
     409             : 
     410             : inline std::error_code SectionRef::getContents(StringRef &Result) const {
     411       20506 :   return OwningObject->getSectionContents(SectionPimpl, Result);
     412             : }
     413             : 
     414             : inline uint64_t SectionRef::getAlignment() const {
     415        1800 :   return OwningObject->getSectionAlignment(SectionPimpl);
     416             : }
     417             : 
     418             : inline bool SectionRef::isCompressed() const {
     419       15528 :   return OwningObject->isSectionCompressed(SectionPimpl);
     420             : }
     421             : 
     422             : inline bool SectionRef::isText() const {
     423        9085 :   return OwningObject->isSectionText(SectionPimpl);
     424             : }
     425             : 
     426             : inline bool SectionRef::isData() const {
     427        2518 :   return OwningObject->isSectionData(SectionPimpl);
     428             : }
     429             : 
     430             : inline bool SectionRef::isBSS() const {
     431       20211 :   return OwningObject->isSectionBSS(SectionPimpl);
     432             : }
     433             : 
     434             : inline bool SectionRef::isVirtual() const {
     435       18393 :   return OwningObject->isSectionVirtual(SectionPimpl);
     436             : }
     437             : 
     438             : inline bool SectionRef::isBitcode() const {
     439       67899 :   return OwningObject->isSectionBitcode(SectionPimpl);
     440             : }
     441             : 
     442             : inline relocation_iterator SectionRef::relocation_begin() const {
     443        5128 :   return OwningObject->section_rel_begin(SectionPimpl);
     444             : }
     445             : 
     446             : inline relocation_iterator SectionRef::relocation_end() const {
     447        5128 :   return OwningObject->section_rel_end(SectionPimpl);
     448             : }
     449             : 
     450             : inline section_iterator SectionRef::getRelocatedSection() const {
     451       31616 :   return OwningObject->getRelocatedSection(SectionPimpl);
     452             : }
     453             : 
     454             : inline DataRefImpl SectionRef::getRawDataRefImpl() const {
     455       19228 :   return SectionPimpl;
     456             : }
     457             : 
     458             : inline const ObjectFile *SectionRef::getObject() const {
     459             :   return OwningObject;
     460             : }
     461             : 
     462             : /// RelocationRef
     463             : inline RelocationRef::RelocationRef(DataRefImpl RelocationP,
     464       14224 :                               const ObjectFile *Owner)
     465             :   : RelocationPimpl(RelocationP)
     466       14224 :   , OwningObject(Owner) {}
     467             : 
     468             : inline bool RelocationRef::operator==(const RelocationRef &Other) const {
     469       39534 :   return RelocationPimpl == Other.RelocationPimpl;
     470             : }
     471             : 
     472             : inline void RelocationRef::moveNext() {
     473       13320 :   return OwningObject->moveRelocationNext(RelocationPimpl);
     474             : }
     475             : 
     476             : inline uint64_t RelocationRef::getOffset() const {
     477       19333 :   return OwningObject->getRelocationOffset(RelocationPimpl);
     478             : }
     479             : 
     480             : inline symbol_iterator RelocationRef::getSymbol() const {
     481        8484 :   return OwningObject->getRelocationSymbol(RelocationPimpl);
     482             : }
     483             : 
     484             : inline uint64_t RelocationRef::getType() const {
     485        8231 :   return OwningObject->getRelocationType(RelocationPimpl);
     486             : }
     487             : 
     488             : inline void RelocationRef::getTypeName(SmallVectorImpl<char> &Result) const {
     489        1545 :   return OwningObject->getRelocationTypeName(RelocationPimpl, Result);
     490             : }
     491             : 
     492             : inline DataRefImpl RelocationRef::getRawDataRefImpl() const {
     493        9939 :   return RelocationPimpl;
     494             : }
     495             : 
     496             : inline const ObjectFile *RelocationRef::getObject() const {
     497             :   return OwningObject;
     498             : }
     499             : 
     500             : } // end namespace object
     501             : 
     502             : } // end namespace llvm
     503             : 
     504             : #endif // LLVM_OBJECT_OBJECTFILE_H

Generated by: LCOV version 1.13