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