ObjectFile.h revision dedd6ee135f886e219035b2cf5957ce492f90ed1
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_OBJECT_FILE_H 15#define LLVM_OBJECT_OBJECT_FILE_H 16 17#include "llvm/Object/Binary.h" 18#include "llvm/ADT/StringRef.h" 19#include "llvm/Support/DataTypes.h" 20#include "llvm/Support/ErrorHandling.h" 21#include "llvm/Support/MemoryBuffer.h" 22#include <cstring> 23 24namespace llvm { 25namespace object { 26 27class ObjectFile; 28 29union DataRefImpl { 30 struct { 31 uint32_t a, b; 32 } d; 33 uintptr_t p; 34}; 35 36static bool operator ==(const DataRefImpl &a, const DataRefImpl &b) { 37 // Check bitwise identical. This is the only legal way to compare a union w/o 38 // knowing which member is in use. 39 return std::memcmp(&a, &b, sizeof(DataRefImpl)) == 0; 40} 41 42class RelocationRef { 43 DataRefImpl RelocationPimpl; 44 const ObjectFile *OwningObject; 45 46public: 47 RelocationRef() : OwningObject(NULL) { 48 std::memset(&RelocationPimpl, 0, sizeof(RelocationPimpl)); 49 } 50 51 RelocationRef(DataRefImpl RelocationP, const ObjectFile *Owner); 52 53 bool operator==(const RelocationRef &Other) const; 54 55 error_code getNext(RelocationRef &Result); 56}; 57 58/// SymbolRef - This is a value type class that represents a single symbol in 59/// the list of symbols in the object file. 60class SymbolRef { 61 DataRefImpl SymbolPimpl; 62 const ObjectFile *OwningObject; 63 64public: 65 SymbolRef() : OwningObject(NULL) { 66 std::memset(&SymbolPimpl, 0, sizeof(SymbolPimpl)); 67 } 68 69 SymbolRef(DataRefImpl SymbolP, const ObjectFile *Owner); 70 71 bool operator==(const SymbolRef &Other) const; 72 73 error_code getNext(SymbolRef &Result) const; 74 75 error_code getName(StringRef &Result) const; 76 error_code getAddress(uint64_t &Result) const; 77 error_code getSize(uint64_t &Result) const; 78 79 /// Returns the ascii char that should be displayed in a symbol table dump via 80 /// nm for this symbol. 81 error_code getNMTypeChar(char &Result) const; 82 83 /// Returns true for symbols that are internal to the object file format such 84 /// as section symbols. 85 error_code isInternal(bool &Result) const; 86}; 87 88/// SectionRef - This is a value type class that represents a single section in 89/// the list of sections in the object file. 90class SectionRef { 91 DataRefImpl SectionPimpl; 92 const ObjectFile *OwningObject; 93 94public: 95 SectionRef() : OwningObject(NULL) { 96 std::memset(&SectionPimpl, 0, sizeof(SectionPimpl)); 97 } 98 99 SectionRef(DataRefImpl SectionP, const ObjectFile *Owner); 100 101 bool operator==(const SectionRef &Other) const; 102 103 error_code getNext(SectionRef &Result) const; 104 105 error_code getName(StringRef &Result) const; 106 error_code getAddress(uint64_t &Result) const; 107 error_code getSize(uint64_t &Result) const; 108 error_code getContents(StringRef &Result) const; 109 110 // FIXME: Move to the normalization layer when it's created. 111 error_code isText(bool &Result) const; 112}; 113 114const uint64_t UnknownAddressOrSize = ~0ULL; 115 116/// ObjectFile - This class is the base class for all object file types. 117/// Concrete instances of this object are created by createObjectFile, which 118/// figure out which type to create. 119class ObjectFile : public Binary { 120private: 121 ObjectFile(); // = delete 122 ObjectFile(const ObjectFile &other); // = delete 123 124protected: 125 ObjectFile(unsigned int Type, MemoryBuffer *source, error_code &ec); 126 127 const uint8_t *base() const { 128 return reinterpret_cast<const uint8_t *>(Data->getBufferStart()); 129 } 130 131 // These functions are for SymbolRef to call internally. The main goal of 132 // this is to allow SymbolRef::SymbolPimpl to point directly to the symbol 133 // entry in the memory mapped object file. SymbolPimpl cannot contain any 134 // virtual functions because then it could not point into the memory mapped 135 // file. 136 // 137 // Implementations assume that the DataRefImpl is valid and has not been 138 // modified externally. It's UB otherwise. 139 friend class SymbolRef; 140 virtual error_code getSymbolNext(DataRefImpl Symb, SymbolRef &Res) const = 0; 141 virtual error_code getSymbolName(DataRefImpl Symb, StringRef &Res) const = 0; 142 virtual error_code getSymbolAddress(DataRefImpl Symb, uint64_t &Res) const =0; 143 virtual error_code getSymbolSize(DataRefImpl Symb, uint64_t &Res) const = 0; 144 virtual error_code getSymbolNMTypeChar(DataRefImpl Symb, char &Res) const = 0; 145 virtual error_code isSymbolInternal(DataRefImpl Symb, bool &Res) const = 0; 146 147 // Same as above for SectionRef. 148 friend class SectionRef; 149 virtual error_code getSectionNext(DataRefImpl Sec, SectionRef &Res) const = 0; 150 virtual error_code getSectionName(DataRefImpl Sec, StringRef &Res) const = 0; 151 virtual error_code getSectionAddress(DataRefImpl Sec, uint64_t &Res) const =0; 152 virtual error_code getSectionSize(DataRefImpl Sec, uint64_t &Res) const = 0; 153 virtual error_code getSectionContents(DataRefImpl Sec, StringRef &Res)const=0; 154 virtual error_code isSectionText(DataRefImpl Sec, bool &Res) const = 0; 155 156 157public: 158 template<class content_type> 159 class content_iterator { 160 content_type Current; 161 public: 162 content_iterator(content_type symb) 163 : Current(symb) {} 164 165 const content_type* operator->() const { 166 return &Current; 167 } 168 169 bool operator==(const content_iterator &other) const { 170 return Current == other.Current; 171 } 172 173 bool operator!=(const content_iterator &other) const { 174 return !(*this == other); 175 } 176 177 content_iterator& increment(error_code &err) { 178 content_type next; 179 if (error_code ec = Current.getNext(next)) 180 err = ec; 181 else 182 Current = next; 183 return *this; 184 } 185 }; 186 187 typedef content_iterator<SymbolRef> symbol_iterator; 188 typedef content_iterator<SectionRef> section_iterator; 189 190 virtual symbol_iterator begin_symbols() const = 0; 191 virtual symbol_iterator end_symbols() const = 0; 192 193 virtual section_iterator begin_sections() const = 0; 194 virtual section_iterator end_sections() const = 0; 195 196 /// @brief The number of bytes used to represent an address in this object 197 /// file format. 198 virtual uint8_t getBytesInAddress() const = 0; 199 200 virtual StringRef getFileFormatName() const = 0; 201 virtual /* Triple::ArchType */ unsigned getArch() const = 0; 202 203 /// @returns Pointer to ObjectFile subclass to handle this type of object. 204 /// @param ObjectPath The path to the object file. ObjectPath.isObject must 205 /// return true. 206 /// @brief Create ObjectFile from path. 207 static ObjectFile *createObjectFile(StringRef ObjectPath); 208 static ObjectFile *createObjectFile(MemoryBuffer *Object); 209 210 static inline bool classof(const Binary *v) { 211 return v->getType() >= isObject && 212 v->getType() < lastObject; 213 } 214 static inline bool classof(const ObjectFile *v) { return true; } 215 216public: 217 static ObjectFile *createCOFFObjectFile(MemoryBuffer *Object); 218 static ObjectFile *createELFObjectFile(MemoryBuffer *Object); 219 static ObjectFile *createMachOObjectFile(MemoryBuffer *Object); 220}; 221 222// Inline function definitions. 223inline SymbolRef::SymbolRef(DataRefImpl SymbolP, const ObjectFile *Owner) 224 : SymbolPimpl(SymbolP) 225 , OwningObject(Owner) {} 226 227inline bool SymbolRef::operator==(const SymbolRef &Other) const { 228 return SymbolPimpl == Other.SymbolPimpl; 229} 230 231inline error_code SymbolRef::getNext(SymbolRef &Result) const { 232 return OwningObject->getSymbolNext(SymbolPimpl, Result); 233} 234 235inline error_code SymbolRef::getName(StringRef &Result) const { 236 return OwningObject->getSymbolName(SymbolPimpl, Result); 237} 238 239inline error_code SymbolRef::getAddress(uint64_t &Result) const { 240 return OwningObject->getSymbolAddress(SymbolPimpl, Result); 241} 242 243inline error_code SymbolRef::getSize(uint64_t &Result) const { 244 return OwningObject->getSymbolSize(SymbolPimpl, Result); 245} 246 247inline error_code SymbolRef::getNMTypeChar(char &Result) const { 248 return OwningObject->getSymbolNMTypeChar(SymbolPimpl, Result); 249} 250 251inline error_code SymbolRef::isInternal(bool &Result) const { 252 return OwningObject->isSymbolInternal(SymbolPimpl, Result); 253} 254 255 256/// SectionRef 257inline SectionRef::SectionRef(DataRefImpl SectionP, 258 const ObjectFile *Owner) 259 : SectionPimpl(SectionP) 260 , OwningObject(Owner) {} 261 262inline bool SectionRef::operator==(const SectionRef &Other) const { 263 return SectionPimpl == Other.SectionPimpl; 264} 265 266inline error_code SectionRef::getNext(SectionRef &Result) const { 267 return OwningObject->getSectionNext(SectionPimpl, Result); 268} 269 270inline error_code SectionRef::getName(StringRef &Result) const { 271 return OwningObject->getSectionName(SectionPimpl, Result); 272} 273 274inline error_code SectionRef::getAddress(uint64_t &Result) const { 275 return OwningObject->getSectionAddress(SectionPimpl, Result); 276} 277 278inline error_code SectionRef::getSize(uint64_t &Result) const { 279 return OwningObject->getSectionSize(SectionPimpl, Result); 280} 281 282inline error_code SectionRef::getContents(StringRef &Result) const { 283 return OwningObject->getSectionContents(SectionPimpl, Result); 284} 285 286inline error_code SectionRef::isText(bool &Result) const { 287 return OwningObject->isSectionText(SectionPimpl, Result); 288} 289 290} // end namespace object 291} // end namespace llvm 292 293#endif 294