LLVM  3.7.0
ObjectFile.h
Go to the documentation of this file.
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"
19 #include "llvm/Support/DataTypes.h"
23 #include <cstring>
24 #include <vector>
25 
26 namespace llvm {
27 namespace object {
28 
29 class ObjectFile;
30 class COFFObjectFile;
31 class MachOObjectFile;
32 
33 class SymbolRef;
34 class symbol_iterator;
35 class SectionRef;
37 
38 /// This is a value type class that represents a single relocation in the list
39 /// of relocations in the object file.
41  DataRefImpl RelocationPimpl;
42  const ObjectFile *OwningObject;
43 
44 public:
45  RelocationRef() : OwningObject(nullptr) { }
46 
47  RelocationRef(DataRefImpl RelocationP, const ObjectFile *Owner);
48 
49  bool operator==(const RelocationRef &Other) const;
50 
51  void moveNext();
52 
53  uint64_t getOffset() const;
54  symbol_iterator getSymbol() const;
55  uint64_t getType() const;
56 
57  /// @brief Get a string that represents the type of this relocation.
58  ///
59  /// This is for display purposes only.
60  void getTypeName(SmallVectorImpl<char> &Result) const;
61 
63  const ObjectFile *getObject() const;
64 };
66 
67 /// This is a value type class that represents a single section in the list of
68 /// sections in the object file.
69 class SectionRef {
70  friend class SymbolRef;
71  DataRefImpl SectionPimpl;
72  const ObjectFile *OwningObject;
73 
74 public:
75  SectionRef() : OwningObject(nullptr) { }
76 
77  SectionRef(DataRefImpl SectionP, const ObjectFile *Owner);
78 
79  bool operator==(const SectionRef &Other) const;
80  bool operator!=(const SectionRef &Other) const;
81  bool operator<(const SectionRef &Other) const;
82 
83  void moveNext();
84 
85  std::error_code getName(StringRef &Result) const;
86  uint64_t getAddress() const;
87  uint64_t getSize() const;
88  std::error_code getContents(StringRef &Result) const;
89 
90  /// @brief Get the alignment of this section as the actual value (not log 2).
91  uint64_t getAlignment() const;
92 
93  bool isText() const;
94  bool isData() const;
95  bool isBSS() const;
96  bool isVirtual() const;
97 
98  bool containsSymbol(SymbolRef S) const;
99 
104  relocation_end());
105  }
107 
109  const ObjectFile *getObject() const;
110 };
111 
112 /// This is a value type class that represents a single symbol in the list of
113 /// symbols in the object file.
114 class SymbolRef : public BasicSymbolRef {
115  friend class SectionRef;
116 
117 public:
119 
120  enum Type {
121  ST_Unknown, // Type not specified
127  };
128 
129  SymbolRef(DataRefImpl SymbolP, const ObjectFile *Owner);
131  assert(isa<ObjectFile>(BasicSymbolRef::getObject()));
132  }
133 
134  ErrorOr<StringRef> getName() const;
135  /// Returns the symbol virtual address (i.e. address at which it will be
136  /// mapped).
138 
139  /// Return the value of the symbol depending on the object this can be an
140  /// offset or a virtual address.
141  uint64_t getValue() const;
142 
143  /// @brief Get the alignment of this symbol as the actual value (not log 2).
144  uint32_t getAlignment() const;
145  uint64_t getCommonSize() const;
146  SymbolRef::Type getType() const;
147 
148  /// @brief Get section this symbol is defined in reference to. Result is
149  /// end_sections() if it is undefined or is an absolute symbol.
150  std::error_code getSection(section_iterator &Result) const;
151 
152  const ObjectFile *getObject() const;
153 };
154 
156 public:
159  : basic_symbol_iterator(SymbolRef(B->getRawDataRefImpl(),
160  cast<ObjectFile>(B->getObject()))) {}
161 
162  const SymbolRef *operator->() const {
164  return static_cast<const SymbolRef*>(&P);
165  }
166 
167  const SymbolRef &operator*() const {
169  return static_cast<const SymbolRef&>(P);
170  }
171 };
172 
173 /// This class is the base class for all object file types. Concrete instances
174 /// of this object are created by createObjectFile, which figures out which type
175 /// to create.
176 class ObjectFile : public SymbolicFile {
177  virtual void anchor();
178  ObjectFile() = delete;
179  ObjectFile(const ObjectFile &other) = delete;
180 
181 protected:
182  ObjectFile(unsigned int Type, MemoryBufferRef Source);
183 
184  const uint8_t *base() const {
185  return reinterpret_cast<const uint8_t *>(Data.getBufferStart());
186  }
187 
188  // These functions are for SymbolRef to call internally. The main goal of
189  // this is to allow SymbolRef::SymbolPimpl to point directly to the symbol
190  // entry in the memory mapped object file. SymbolPimpl cannot contain any
191  // virtual functions because then it could not point into the memory mapped
192  // file.
193  //
194  // Implementations assume that the DataRefImpl is valid and has not been
195  // modified externally. It's UB otherwise.
196  friend class SymbolRef;
197  virtual ErrorOr<StringRef> getSymbolName(DataRefImpl Symb) const = 0;
198  std::error_code printSymbolName(raw_ostream &OS,
199  DataRefImpl Symb) const override;
200  virtual ErrorOr<uint64_t> getSymbolAddress(DataRefImpl Symb) const = 0;
201  virtual uint64_t getSymbolValueImpl(DataRefImpl Symb) const = 0;
202  virtual uint32_t getSymbolAlignment(DataRefImpl Symb) const;
203  virtual uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const = 0;
204  virtual SymbolRef::Type getSymbolType(DataRefImpl Symb) const = 0;
205  virtual std::error_code getSymbolSection(DataRefImpl Symb,
206  section_iterator &Res) const = 0;
207 
208  // Same as above for SectionRef.
209  friend class SectionRef;
210  virtual void moveSectionNext(DataRefImpl &Sec) const = 0;
211  virtual std::error_code getSectionName(DataRefImpl Sec,
212  StringRef &Res) const = 0;
213  virtual uint64_t getSectionAddress(DataRefImpl Sec) const = 0;
214  virtual uint64_t getSectionSize(DataRefImpl Sec) const = 0;
215  virtual std::error_code getSectionContents(DataRefImpl Sec,
216  StringRef &Res) const = 0;
217  virtual uint64_t getSectionAlignment(DataRefImpl Sec) const = 0;
218  virtual bool isSectionText(DataRefImpl Sec) const = 0;
219  virtual bool isSectionData(DataRefImpl Sec) const = 0;
220  virtual bool isSectionBSS(DataRefImpl Sec) const = 0;
221  // A section is 'virtual' if its contents aren't present in the object image.
222  virtual bool isSectionVirtual(DataRefImpl Sec) const = 0;
223  virtual relocation_iterator section_rel_begin(DataRefImpl Sec) const = 0;
224  virtual relocation_iterator section_rel_end(DataRefImpl Sec) const = 0;
226 
227  // Same as above for RelocationRef.
228  friend class RelocationRef;
229  virtual void moveRelocationNext(DataRefImpl &Rel) const = 0;
230  virtual uint64_t getRelocationOffset(DataRefImpl Rel) const = 0;
231  virtual symbol_iterator getRelocationSymbol(DataRefImpl Rel) const = 0;
232  virtual uint64_t getRelocationType(DataRefImpl Rel) const = 0;
233  virtual void getRelocationTypeName(DataRefImpl Rel,
234  SmallVectorImpl<char> &Result) const = 0;
235 
236  uint64_t getSymbolValue(DataRefImpl Symb) const;
237 
238 public:
239  uint64_t getCommonSymbolSize(DataRefImpl Symb) const {
240  assert(getSymbolFlags(Symb) & SymbolRef::SF_Common);
241  return getCommonSymbolSizeImpl(Symb);
242  }
243 
247  }
248 
249  virtual section_iterator section_begin() const = 0;
250  virtual section_iterator section_end() const = 0;
251 
255  }
256 
257  /// @brief The number of bytes used to represent an address in this object
258  /// file format.
259  virtual uint8_t getBytesInAddress() const = 0;
260 
261  virtual StringRef getFileFormatName() const = 0;
262  virtual /* Triple::ArchType */ unsigned getArch() const = 0;
263 
264  /// Returns platform-specific object flags, if any.
265  virtual std::error_code getPlatformFlags(unsigned &Result) const {
266  Result = 0;
268  }
269 
270  /// True if this is a relocatable object (.o/.obj).
271  virtual bool isRelocatableObject() const = 0;
272 
273  /// @returns Pointer to ObjectFile subclass to handle this type of object.
274  /// @param ObjectPath The path to the object file. ObjectPath.isObject must
275  /// return true.
276  /// @brief Create ObjectFile from path.
278  createObjectFile(StringRef ObjectPath);
279 
285  }
286 
287 
288  static inline bool classof(const Binary *v) {
289  return v->isObject();
290  }
291 
294 
297 
300 };
301 
302 // Inline function definitions.
303 inline SymbolRef::SymbolRef(DataRefImpl SymbolP, const ObjectFile *Owner)
304  : BasicSymbolRef(SymbolP, Owner) {}
305 
308 }
309 
312 }
313 
314 inline uint64_t SymbolRef::getValue() const {
316 }
317 
318 inline uint32_t SymbolRef::getAlignment() const {
320 }
321 
322 inline uint64_t SymbolRef::getCommonSize() const {
324 }
325 
326 inline std::error_code SymbolRef::getSection(section_iterator &Result) const {
327  return getObject()->getSymbolSection(getRawDataRefImpl(), Result);
328 }
329 
332 }
333 
334 inline const ObjectFile *SymbolRef::getObject() const {
336  return cast<ObjectFile>(O);
337 }
338 
339 
340 /// SectionRef
342  const ObjectFile *Owner)
343  : SectionPimpl(SectionP)
344  , OwningObject(Owner) {}
345 
346 inline bool SectionRef::operator==(const SectionRef &Other) const {
347  return SectionPimpl == Other.SectionPimpl;
348 }
349 
350 inline bool SectionRef::operator!=(const SectionRef &Other) const {
351  return SectionPimpl != Other.SectionPimpl;
352 }
353 
354 inline bool SectionRef::operator<(const SectionRef &Other) const {
355  return SectionPimpl < Other.SectionPimpl;
356 }
357 
358 inline void SectionRef::moveNext() {
359  return OwningObject->moveSectionNext(SectionPimpl);
360 }
361 
362 inline std::error_code SectionRef::getName(StringRef &Result) const {
363  return OwningObject->getSectionName(SectionPimpl, Result);
364 }
365 
366 inline uint64_t SectionRef::getAddress() const {
367  return OwningObject->getSectionAddress(SectionPimpl);
368 }
369 
370 inline uint64_t SectionRef::getSize() const {
371  return OwningObject->getSectionSize(SectionPimpl);
372 }
373 
374 inline std::error_code SectionRef::getContents(StringRef &Result) const {
375  return OwningObject->getSectionContents(SectionPimpl, Result);
376 }
377 
378 inline uint64_t SectionRef::getAlignment() const {
379  return OwningObject->getSectionAlignment(SectionPimpl);
380 }
381 
382 inline bool SectionRef::isText() const {
383  return OwningObject->isSectionText(SectionPimpl);
384 }
385 
386 inline bool SectionRef::isData() const {
387  return OwningObject->isSectionData(SectionPimpl);
388 }
389 
390 inline bool SectionRef::isBSS() const {
391  return OwningObject->isSectionBSS(SectionPimpl);
392 }
393 
394 inline bool SectionRef::isVirtual() const {
395  return OwningObject->isSectionVirtual(SectionPimpl);
396 }
397 
399  return OwningObject->section_rel_begin(SectionPimpl);
400 }
401 
403  return OwningObject->section_rel_end(SectionPimpl);
404 }
405 
407  return OwningObject->getRelocatedSection(SectionPimpl);
408 }
409 
411  return SectionPimpl;
412 }
413 
414 inline const ObjectFile *SectionRef::getObject() const {
415  return OwningObject;
416 }
417 
418 /// RelocationRef
420  const ObjectFile *Owner)
421  : RelocationPimpl(RelocationP)
422  , OwningObject(Owner) {}
423 
424 inline bool RelocationRef::operator==(const RelocationRef &Other) const {
425  return RelocationPimpl == Other.RelocationPimpl;
426 }
427 
428 inline void RelocationRef::moveNext() {
429  return OwningObject->moveRelocationNext(RelocationPimpl);
430 }
431 
432 inline uint64_t RelocationRef::getOffset() const {
433  return OwningObject->getRelocationOffset(RelocationPimpl);
434 }
435 
437  return OwningObject->getRelocationSymbol(RelocationPimpl);
438 }
439 
440 inline uint64_t RelocationRef::getType() const {
441  return OwningObject->getRelocationType(RelocationPimpl);
442 }
443 
445  return OwningObject->getRelocationTypeName(RelocationPimpl, Result);
446 }
447 
449  return RelocationPimpl;
450 }
451 
452 inline const ObjectFile *RelocationRef::getObject() const {
453  return OwningObject;
454 }
455 
456 
457 } // end namespace object
458 } // end namespace llvm
459 
460 #endif
static ErrorOr< std::unique_ptr< MachOObjectFile > > createMachOObjectFile(MemoryBufferRef Object)
Represents either an error or a value T.
Definition: ErrorOr.h:82
iterator_range< symbol_iterator > symbol_iterator_range
Definition: ObjectFile.h:244
iterator_range< section_iterator > section_iterator_range
Definition: ObjectFile.h:252
DataRefImpl getRawDataRefImpl() const
Definition: SymbolicFile.h:188
static ErrorOr< std::unique_ptr< ObjectFile > > createObjectFile(MemoryBufferRef Object)
Definition: ObjectFile.h:283
const ObjectFile * getObject() const
Definition: ObjectFile.h:452
const uint8_t * base() const
Definition: ObjectFile.h:184
static std::error_code getObject(const T *&Obj, MemoryBufferRef M, const void *Ptr, const uint64_t Size=sizeof(T))
uint32_t getAlignment() const
Get the alignment of this symbol as the actual value (not log 2).
Definition: ObjectFile.h:318
symbol_iterator_range symbols() const
Definition: ObjectFile.h:245
bool isData() const
Definition: ObjectFile.h:386
static ErrorOr< std::unique_ptr< COFFObjectFile > > createCOFFObjectFile(MemoryBufferRef Object)
virtual bool isSectionBSS(DataRefImpl Sec) const =0
This class is the base class for all object file types.
Definition: ObjectFile.h:176
uint64_t getOffset() const
Definition: ObjectFile.h:432
const char * getBufferStart() const
Definition: MemoryBuffer.h:161
virtual uint64_t getRelocationOffset(DataRefImpl Rel) const =0
virtual std::error_code getSectionName(DataRefImpl Sec, StringRef &Res) const =0
static ErrorOr< std::unique_ptr< ObjectFile > > createELFObjectFile(MemoryBufferRef Object)
DataRefImpl getRawDataRefImpl() const
Definition: ObjectFile.h:410
section_iterator getRelocatedSection() const
Definition: ObjectFile.h:406
virtual unsigned getArch() const =0
uint64_t getCommonSize() const
Definition: ObjectFile.h:322
virtual relocation_iterator section_rel_end(DataRefImpl Sec) const =0
virtual std::error_code getSymbolSection(DataRefImpl Symb, section_iterator &Res) const =0
bool isVirtual() const
Definition: ObjectFile.h:394
virtual ErrorOr< uint64_t > getSymbolAddress(DataRefImpl Symb) const =0
virtual std::error_code getSectionContents(DataRefImpl Sec, StringRef &Res) const =0
void getTypeName(SmallVectorImpl< char > &Result) const
Get a string that represents the type of this relocation.
Definition: ObjectFile.h:444
This is a value type class that represents a single relocation in the list of relocations in the obje...
Definition: ObjectFile.h:40
SymbolRef::Type getType() const
Definition: ObjectFile.h:330
ELFYAML::ELF_STO Other
Definition: ELFYAML.cpp:591
virtual relocation_iterator section_rel_begin(DataRefImpl Sec) const =0
bool containsSymbol(SymbolRef S) const
Definition: ObjectFile.cpp:31
basic_symbol_iterator symbol_begin() const
Definition: SymbolicFile.h:136
virtual uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const =0
std::error_code getName(StringRef &Result) const
Definition: ObjectFile.h:362
virtual SymbolRef::Type getSymbolType(DataRefImpl Symb) const =0
virtual uint64_t getSectionAlignment(DataRefImpl Sec) const =0
const SymbolRef * operator->() const
Definition: ObjectFile.h:162
std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type cast(const Y &Val)
Definition: Casting.h:222
uint64_t getAlignment() const
Get the alignment of this section as the actual value (not log 2).
Definition: ObjectFile.h:378
content_iterator< SectionRef > section_iterator
Definition: ObjectFile.h:35
virtual uint32_t getSymbolFlags(DataRefImpl Symb) const =0
#define P(N)
virtual uint8_t getBytesInAddress() const =0
The number of bytes used to represent an address in this object file format.
bool isObject() const
Definition: Binary.h:88
virtual void moveRelocationNext(DataRefImpl &Rel) const =0
relocation_iterator relocation_begin() const
Definition: ObjectFile.h:398
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:45
iterator_range< relocation_iterator > relocations() const
Definition: ObjectFile.h:102
bool operator==(const RelocationRef &Other) const
Definition: ObjectFile.h:424
symbol_iterator(const basic_symbol_iterator &B)
Definition: ObjectFile.h:158
bool isText() const
Definition: ObjectFile.h:382
symbol_iterator getSymbol() const
Definition: ObjectFile.h:436
uint64_t getSize() const
Definition: ObjectFile.h:370
virtual std::error_code getPlatformFlags(unsigned &Result) const
Returns platform-specific object flags, if any.
Definition: ObjectFile.h:265
section_iterator_range sections() const
Definition: ObjectFile.h:253
content_iterator< RelocationRef > relocation_iterator
Definition: ObjectFile.h:65
uint64_t getAddress() const
Definition: ObjectFile.h:366
const ObjectFile * getObject() const
Definition: ObjectFile.h:414
basic_symbol_iterator symbol_end() const
Definition: SymbolicFile.h:139
SymbolRef(const BasicSymbolRef &B)
Definition: ObjectFile.h:130
std::error_code printSymbolName(raw_ostream &OS, DataRefImpl Symb) const override
Definition: ObjectFile.cpp:47
virtual section_iterator section_begin() const =0
const ObjectFile * getObject() const
Definition: ObjectFile.h:334
virtual uint64_t getSectionAddress(DataRefImpl Sec) const =0
virtual symbol_iterator getRelocationSymbol(DataRefImpl Rel) const =0
A range adaptor for a pair of iterators.
virtual section_iterator getRelocatedSection(DataRefImpl Sec) const
Definition: ObjectFile.cpp:58
MemoryBufferRef Data
Definition: Binary.h:37
relocation_iterator relocation_end() const
Definition: ObjectFile.h:402
virtual bool isSectionText(DataRefImpl Sec) const =0
std::error_code getContents(StringRef &Result) const
Definition: ObjectFile.h:374
This is a value type class that represents a single symbol in the list of symbols in the object file...
Definition: ObjectFile.h:114
static bool classof(const Binary *v)
Definition: ObjectFile.h:288
uint64_t getType() const
Definition: ObjectFile.h:440
ErrorOr< StringRef > getName() const
Definition: ObjectFile.h:306
virtual void moveSectionNext(DataRefImpl &Sec) const =0
file_magic - An "enum class" enumeration of file types based on magic (the first N bytes of the file)...
Definition: FileSystem.h:224
virtual section_iterator section_end() const =0
virtual bool isSectionData(DataRefImpl Sec) const =0
uint64_t getSymbolValue(DataRefImpl Symb) const
Definition: ObjectFile.cpp:38
const SymbolicFile * getObject() const
Definition: SymbolicFile.h:192
This is a value type class that represents a single symbol in the list of symbols in the object file...
Definition: SymbolicFile.h:78
virtual uint64_t getSymbolValueImpl(DataRefImpl Symb) const =0
const SymbolRef & operator*() const
Definition: ObjectFile.h:167
virtual bool isRelocatableObject() const =0
True if this is a relocatable object (.o/.obj).
uint64_t getCommonSymbolSize(DataRefImpl Symb) const
Definition: ObjectFile.h:239
const content_type & operator*() const
Definition: SymbolicFile.h:58
bool operator==(const SectionRef &Other) const
Definition: ObjectFile.h:346
bool operator<(const SectionRef &Other) const
Definition: ObjectFile.h:354
virtual ErrorOr< StringRef > getSymbolName(DataRefImpl Symb) const =0
virtual bool isSectionVirtual(DataRefImpl Sec) const =0
virtual uint64_t getSectionSize(DataRefImpl Sec) const =0
virtual void getRelocationTypeName(DataRefImpl Rel, SmallVectorImpl< char > &Result) const =0
symbol_iterator(SymbolRef Sym)
Definition: ObjectFile.h:157
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:38
uint64_t getValue() const
Return the value of the symbol depending on the object this can be an offset or a virtual address...
Definition: ObjectFile.h:314
static ErrorOr< OwningBinary< ObjectFile > > createObjectFile(StringRef ObjectPath)
Create ObjectFile from path.
Definition: ObjectFile.cpp:102
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:40
virtual StringRef getFileFormatName() const =0
ErrorOr< uint64_t > getAddress() const
Returns the symbol virtual address (i.e.
Definition: ObjectFile.h:310
virtual uint64_t getRelocationType(DataRefImpl Rel) const =0
std::error_code getSection(section_iterator &Result) const
Get section this symbol is defined in reference to.
Definition: ObjectFile.h:326
bool operator!=(const SectionRef &Other) const
Definition: ObjectFile.h:350
This is a value type class that represents a single section in the list of sections in the object fil...
Definition: ObjectFile.h:69
virtual uint32_t getSymbolAlignment(DataRefImpl Symb) const
Definition: ObjectFile.cpp:56
DataRefImpl getRawDataRefImpl() const
Definition: ObjectFile.h:448