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