blob: f13775c914df808ecdfd4454158e1f3011f8bba4 [file] [log] [blame]
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001//===- ObjectFile.h - File format independent object file -------*- C++ -*-===//
2//
Andrew Walbran16937d02019-10-22 13:54:20 +01003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01006//
7//===----------------------------------------------------------------------===//
8//
9// This file declares a file format independent ObjectFile class.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_OBJECT_OBJECTFILE_H
14#define LLVM_OBJECT_OBJECTFILE_H
15
16#include "llvm/ADT/StringRef.h"
17#include "llvm/ADT/Triple.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/FileSystem.h"
27#include "llvm/Support/MemoryBuffer.h"
28#include <cassert>
29#include <cstdint>
30#include <memory>
31#include <system_error>
32
33namespace llvm {
34
35class ARMAttributeParser;
36
37namespace object {
38
39class COFFObjectFile;
40class MachOObjectFile;
41class ObjectFile;
42class SectionRef;
43class SymbolRef;
44class symbol_iterator;
45class WasmObjectFile;
46
47using section_iterator = content_iterator<SectionRef>;
48
49/// This is a value type class that represents a single relocation in the list
50/// of relocations in the object file.
51class RelocationRef {
52 DataRefImpl RelocationPimpl;
53 const ObjectFile *OwningObject = nullptr;
54
55public:
56 RelocationRef() = default;
57 RelocationRef(DataRefImpl RelocationP, const ObjectFile *Owner);
58
59 bool operator==(const RelocationRef &Other) const;
60
61 void moveNext();
62
63 uint64_t getOffset() const;
64 symbol_iterator getSymbol() const;
65 uint64_t getType() const;
66
Andrew Scullcdfcccc2018-10-05 20:58:37 +010067 /// Get a string that represents the type of this relocation.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010068 ///
69 /// This is for display purposes only.
70 void getTypeName(SmallVectorImpl<char> &Result) const;
71
72 DataRefImpl getRawDataRefImpl() const;
73 const ObjectFile *getObject() const;
74};
75
76using relocation_iterator = content_iterator<RelocationRef>;
77
78/// This is a value type class that represents a single section in the list of
79/// sections in the object file.
80class SectionRef {
81 friend class SymbolRef;
82
83 DataRefImpl SectionPimpl;
84 const ObjectFile *OwningObject = nullptr;
85
86public:
87 SectionRef() = default;
88 SectionRef(DataRefImpl SectionP, const ObjectFile *Owner);
89
90 bool operator==(const SectionRef &Other) const;
91 bool operator!=(const SectionRef &Other) const;
92 bool operator<(const SectionRef &Other) const;
93
94 void moveNext();
95
96 std::error_code getName(StringRef &Result) const;
97 uint64_t getAddress() const;
98 uint64_t getIndex() const;
99 uint64_t getSize() const;
100 std::error_code getContents(StringRef &Result) const;
101
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100102 /// Get the alignment of this section as the actual value (not log 2).
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100103 uint64_t getAlignment() const;
104
105 bool isCompressed() const;
Andrew Walbran16937d02019-10-22 13:54:20 +0100106 /// Whether this section contains instructions.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100107 bool isText() const;
Andrew Walbran16937d02019-10-22 13:54:20 +0100108 /// Whether this section contains data, not instructions.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100109 bool isData() const;
Andrew Walbran16937d02019-10-22 13:54:20 +0100110 /// Whether this section contains BSS uninitialized data.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100111 bool isBSS() const;
112 bool isVirtual() const;
113 bool isBitcode() const;
114 bool isStripped() const;
115
Andrew Walbran16937d02019-10-22 13:54:20 +0100116 /// Whether this section will be placed in the text segment, according to the
117 /// Berkeley size format. This is true if the section is allocatable, and
118 /// contains either code or readonly data.
119 bool isBerkeleyText() const;
120 /// Whether this section will be placed in the data segment, according to the
121 /// Berkeley size format. This is true if the section is allocatable and
122 /// contains data (e.g. PROGBITS), but is not text.
123 bool isBerkeleyData() const;
124
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100125 bool containsSymbol(SymbolRef S) const;
126
127 relocation_iterator relocation_begin() const;
128 relocation_iterator relocation_end() const;
129 iterator_range<relocation_iterator> relocations() const {
130 return make_range(relocation_begin(), relocation_end());
131 }
132 section_iterator getRelocatedSection() const;
133
134 DataRefImpl getRawDataRefImpl() const;
135 const ObjectFile *getObject() const;
136};
137
138/// This is a value type class that represents a single symbol in the list of
139/// symbols in the object file.
140class SymbolRef : public BasicSymbolRef {
141 friend class SectionRef;
142
143public:
144 enum Type {
145 ST_Unknown, // Type not specified
146 ST_Data,
147 ST_Debug,
148 ST_File,
149 ST_Function,
150 ST_Other
151 };
152
153 SymbolRef() = default;
154 SymbolRef(DataRefImpl SymbolP, const ObjectFile *Owner);
155 SymbolRef(const BasicSymbolRef &B) : BasicSymbolRef(B) {
156 assert(isa<ObjectFile>(BasicSymbolRef::getObject()));
157 }
158
159 Expected<StringRef> getName() const;
160 /// Returns the symbol virtual address (i.e. address at which it will be
161 /// mapped).
162 Expected<uint64_t> getAddress() const;
163
164 /// Return the value of the symbol depending on the object this can be an
165 /// offset or a virtual address.
166 uint64_t getValue() const;
167
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100168 /// Get the alignment of this symbol as the actual value (not log 2).
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100169 uint32_t getAlignment() const;
170 uint64_t getCommonSize() const;
171 Expected<SymbolRef::Type> getType() const;
172
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100173 /// Get section this symbol is defined in reference to. Result is
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100174 /// end_sections() if it is undefined or is an absolute symbol.
175 Expected<section_iterator> getSection() const;
176
177 const ObjectFile *getObject() const;
178};
179
180class symbol_iterator : public basic_symbol_iterator {
181public:
182 symbol_iterator(SymbolRef Sym) : basic_symbol_iterator(Sym) {}
183 symbol_iterator(const basic_symbol_iterator &B)
184 : basic_symbol_iterator(SymbolRef(B->getRawDataRefImpl(),
185 cast<ObjectFile>(B->getObject()))) {}
186
187 const SymbolRef *operator->() const {
188 const BasicSymbolRef &P = basic_symbol_iterator::operator *();
189 return static_cast<const SymbolRef*>(&P);
190 }
191
192 const SymbolRef &operator*() const {
193 const BasicSymbolRef &P = basic_symbol_iterator::operator *();
194 return static_cast<const SymbolRef&>(P);
195 }
196};
197
198/// This class is the base class for all object file types. Concrete instances
199/// of this object are created by createObjectFile, which figures out which type
200/// to create.
201class ObjectFile : public SymbolicFile {
202 virtual void anchor();
203
204protected:
205 ObjectFile(unsigned int Type, MemoryBufferRef Source);
206
207 const uint8_t *base() const {
208 return reinterpret_cast<const uint8_t *>(Data.getBufferStart());
209 }
210
211 // These functions are for SymbolRef to call internally. The main goal of
212 // this is to allow SymbolRef::SymbolPimpl to point directly to the symbol
213 // entry in the memory mapped object file. SymbolPimpl cannot contain any
214 // virtual functions because then it could not point into the memory mapped
215 // file.
216 //
217 // Implementations assume that the DataRefImpl is valid and has not been
218 // modified externally. It's UB otherwise.
219 friend class SymbolRef;
220
221 virtual Expected<StringRef> getSymbolName(DataRefImpl Symb) const = 0;
222 std::error_code printSymbolName(raw_ostream &OS,
223 DataRefImpl Symb) const override;
224 virtual Expected<uint64_t> getSymbolAddress(DataRefImpl Symb) const = 0;
225 virtual uint64_t getSymbolValueImpl(DataRefImpl Symb) const = 0;
226 virtual uint32_t getSymbolAlignment(DataRefImpl Symb) const;
227 virtual uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const = 0;
228 virtual Expected<SymbolRef::Type> getSymbolType(DataRefImpl Symb) const = 0;
229 virtual Expected<section_iterator>
230 getSymbolSection(DataRefImpl Symb) const = 0;
231
232 // Same as above for SectionRef.
233 friend class SectionRef;
234
235 virtual void moveSectionNext(DataRefImpl &Sec) const = 0;
236 virtual std::error_code getSectionName(DataRefImpl Sec,
237 StringRef &Res) const = 0;
238 virtual uint64_t getSectionAddress(DataRefImpl Sec) const = 0;
239 virtual uint64_t getSectionIndex(DataRefImpl Sec) const = 0;
240 virtual uint64_t getSectionSize(DataRefImpl Sec) const = 0;
241 virtual std::error_code getSectionContents(DataRefImpl Sec,
242 StringRef &Res) const = 0;
243 virtual uint64_t getSectionAlignment(DataRefImpl Sec) const = 0;
244 virtual bool isSectionCompressed(DataRefImpl Sec) const = 0;
245 virtual bool isSectionText(DataRefImpl Sec) const = 0;
246 virtual bool isSectionData(DataRefImpl Sec) const = 0;
247 virtual bool isSectionBSS(DataRefImpl Sec) const = 0;
248 // A section is 'virtual' if its contents aren't present in the object image.
249 virtual bool isSectionVirtual(DataRefImpl Sec) const = 0;
250 virtual bool isSectionBitcode(DataRefImpl Sec) const;
251 virtual bool isSectionStripped(DataRefImpl Sec) const;
Andrew Walbran16937d02019-10-22 13:54:20 +0100252 virtual bool isBerkeleyText(DataRefImpl Sec) const;
253 virtual bool isBerkeleyData(DataRefImpl Sec) const;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100254 virtual relocation_iterator section_rel_begin(DataRefImpl Sec) const = 0;
255 virtual relocation_iterator section_rel_end(DataRefImpl Sec) const = 0;
256 virtual section_iterator getRelocatedSection(DataRefImpl Sec) const;
257
258 // Same as above for RelocationRef.
259 friend class RelocationRef;
260 virtual void moveRelocationNext(DataRefImpl &Rel) const = 0;
261 virtual uint64_t getRelocationOffset(DataRefImpl Rel) const = 0;
262 virtual symbol_iterator getRelocationSymbol(DataRefImpl Rel) const = 0;
263 virtual uint64_t getRelocationType(DataRefImpl Rel) const = 0;
264 virtual void getRelocationTypeName(DataRefImpl Rel,
265 SmallVectorImpl<char> &Result) const = 0;
266
267 uint64_t getSymbolValue(DataRefImpl Symb) const;
268
269public:
270 ObjectFile() = delete;
271 ObjectFile(const ObjectFile &other) = delete;
272
273 uint64_t getCommonSymbolSize(DataRefImpl Symb) const {
274 assert(getSymbolFlags(Symb) & SymbolRef::SF_Common);
275 return getCommonSymbolSizeImpl(Symb);
276 }
277
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100278 virtual std::vector<SectionRef> dynamic_relocation_sections() const {
279 return std::vector<SectionRef>();
280 }
281
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100282 using symbol_iterator_range = iterator_range<symbol_iterator>;
283 symbol_iterator_range symbols() const {
284 return symbol_iterator_range(symbol_begin(), symbol_end());
285 }
286
287 virtual section_iterator section_begin() const = 0;
288 virtual section_iterator section_end() const = 0;
289
290 using section_iterator_range = iterator_range<section_iterator>;
291 section_iterator_range sections() const {
292 return section_iterator_range(section_begin(), section_end());
293 }
294
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100295 /// The number of bytes used to represent an address in this object
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100296 /// file format.
297 virtual uint8_t getBytesInAddress() const = 0;
298
299 virtual StringRef getFileFormatName() const = 0;
300 virtual Triple::ArchType getArch() const = 0;
301 virtual SubtargetFeatures getFeatures() const = 0;
302 virtual void setARMSubArch(Triple &TheTriple) const { }
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100303 virtual Expected<uint64_t> getStartAddress() const {
304 return errorCodeToError(object_error::parse_failed);
305 };
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100306
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100307 /// Create a triple from the data in this object file.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100308 Triple makeTriple() const;
309
310 virtual std::error_code
311 getBuildAttributes(ARMAttributeParser &Attributes) const {
312 return std::error_code();
313 }
314
315 /// Maps a debug section name to a standard DWARF section name.
316 virtual StringRef mapDebugSectionName(StringRef Name) const { return Name; }
317
318 /// True if this is a relocatable object (.o/.obj).
319 virtual bool isRelocatableObject() const = 0;
320
321 /// @returns Pointer to ObjectFile subclass to handle this type of object.
322 /// @param ObjectPath The path to the object file. ObjectPath.isObject must
323 /// return true.
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100324 /// Create ObjectFile from path.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100325 static Expected<OwningBinary<ObjectFile>>
326 createObjectFile(StringRef ObjectPath);
327
328 static Expected<std::unique_ptr<ObjectFile>>
329 createObjectFile(MemoryBufferRef Object, llvm::file_magic Type);
330 static Expected<std::unique_ptr<ObjectFile>>
331 createObjectFile(MemoryBufferRef Object) {
332 return createObjectFile(Object, llvm::file_magic::unknown);
333 }
334
335 static bool classof(const Binary *v) {
336 return v->isObject();
337 }
338
339 static Expected<std::unique_ptr<COFFObjectFile>>
340 createCOFFObjectFile(MemoryBufferRef Object);
341
342 static Expected<std::unique_ptr<ObjectFile>>
343 createELFObjectFile(MemoryBufferRef Object);
344
345 static Expected<std::unique_ptr<MachOObjectFile>>
346 createMachOObjectFile(MemoryBufferRef Object,
347 uint32_t UniversalCputype = 0,
348 uint32_t UniversalIndex = 0);
349
350 static Expected<std::unique_ptr<WasmObjectFile>>
351 createWasmObjectFile(MemoryBufferRef Object);
352};
353
354// Inline function definitions.
355inline SymbolRef::SymbolRef(DataRefImpl SymbolP, const ObjectFile *Owner)
356 : BasicSymbolRef(SymbolP, Owner) {}
357
358inline Expected<StringRef> SymbolRef::getName() const {
359 return getObject()->getSymbolName(getRawDataRefImpl());
360}
361
362inline Expected<uint64_t> SymbolRef::getAddress() const {
363 return getObject()->getSymbolAddress(getRawDataRefImpl());
364}
365
366inline uint64_t SymbolRef::getValue() const {
367 return getObject()->getSymbolValue(getRawDataRefImpl());
368}
369
370inline uint32_t SymbolRef::getAlignment() const {
371 return getObject()->getSymbolAlignment(getRawDataRefImpl());
372}
373
374inline uint64_t SymbolRef::getCommonSize() const {
375 return getObject()->getCommonSymbolSize(getRawDataRefImpl());
376}
377
378inline Expected<section_iterator> SymbolRef::getSection() const {
379 return getObject()->getSymbolSection(getRawDataRefImpl());
380}
381
382inline Expected<SymbolRef::Type> SymbolRef::getType() const {
383 return getObject()->getSymbolType(getRawDataRefImpl());
384}
385
386inline const ObjectFile *SymbolRef::getObject() const {
387 const SymbolicFile *O = BasicSymbolRef::getObject();
388 return cast<ObjectFile>(O);
389}
390
391/// SectionRef
392inline SectionRef::SectionRef(DataRefImpl SectionP,
393 const ObjectFile *Owner)
394 : SectionPimpl(SectionP)
395 , OwningObject(Owner) {}
396
397inline bool SectionRef::operator==(const SectionRef &Other) const {
398 return SectionPimpl == Other.SectionPimpl;
399}
400
401inline bool SectionRef::operator!=(const SectionRef &Other) const {
402 return SectionPimpl != Other.SectionPimpl;
403}
404
405inline bool SectionRef::operator<(const SectionRef &Other) const {
406 return SectionPimpl < Other.SectionPimpl;
407}
408
409inline void SectionRef::moveNext() {
410 return OwningObject->moveSectionNext(SectionPimpl);
411}
412
413inline std::error_code SectionRef::getName(StringRef &Result) const {
414 return OwningObject->getSectionName(SectionPimpl, Result);
415}
416
417inline uint64_t SectionRef::getAddress() const {
418 return OwningObject->getSectionAddress(SectionPimpl);
419}
420
421inline uint64_t SectionRef::getIndex() const {
422 return OwningObject->getSectionIndex(SectionPimpl);
423}
424
425inline uint64_t SectionRef::getSize() const {
426 return OwningObject->getSectionSize(SectionPimpl);
427}
428
429inline std::error_code SectionRef::getContents(StringRef &Result) const {
430 return OwningObject->getSectionContents(SectionPimpl, Result);
431}
432
433inline uint64_t SectionRef::getAlignment() const {
434 return OwningObject->getSectionAlignment(SectionPimpl);
435}
436
437inline bool SectionRef::isCompressed() const {
438 return OwningObject->isSectionCompressed(SectionPimpl);
439}
440
441inline bool SectionRef::isText() const {
442 return OwningObject->isSectionText(SectionPimpl);
443}
444
445inline bool SectionRef::isData() const {
446 return OwningObject->isSectionData(SectionPimpl);
447}
448
449inline bool SectionRef::isBSS() const {
450 return OwningObject->isSectionBSS(SectionPimpl);
451}
452
453inline bool SectionRef::isVirtual() const {
454 return OwningObject->isSectionVirtual(SectionPimpl);
455}
456
457inline bool SectionRef::isBitcode() const {
458 return OwningObject->isSectionBitcode(SectionPimpl);
459}
460
461inline bool SectionRef::isStripped() const {
462 return OwningObject->isSectionStripped(SectionPimpl);
463}
464
Andrew Walbran16937d02019-10-22 13:54:20 +0100465inline bool SectionRef::isBerkeleyText() const {
466 return OwningObject->isBerkeleyText(SectionPimpl);
467}
468
469inline bool SectionRef::isBerkeleyData() const {
470 return OwningObject->isBerkeleyData(SectionPimpl);
471}
472
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100473inline relocation_iterator SectionRef::relocation_begin() const {
474 return OwningObject->section_rel_begin(SectionPimpl);
475}
476
477inline relocation_iterator SectionRef::relocation_end() const {
478 return OwningObject->section_rel_end(SectionPimpl);
479}
480
481inline section_iterator SectionRef::getRelocatedSection() const {
482 return OwningObject->getRelocatedSection(SectionPimpl);
483}
484
485inline DataRefImpl SectionRef::getRawDataRefImpl() const {
486 return SectionPimpl;
487}
488
489inline const ObjectFile *SectionRef::getObject() const {
490 return OwningObject;
491}
492
493/// RelocationRef
494inline RelocationRef::RelocationRef(DataRefImpl RelocationP,
495 const ObjectFile *Owner)
496 : RelocationPimpl(RelocationP)
497 , OwningObject(Owner) {}
498
499inline bool RelocationRef::operator==(const RelocationRef &Other) const {
500 return RelocationPimpl == Other.RelocationPimpl;
501}
502
503inline void RelocationRef::moveNext() {
504 return OwningObject->moveRelocationNext(RelocationPimpl);
505}
506
507inline uint64_t RelocationRef::getOffset() const {
508 return OwningObject->getRelocationOffset(RelocationPimpl);
509}
510
511inline symbol_iterator RelocationRef::getSymbol() const {
512 return OwningObject->getRelocationSymbol(RelocationPimpl);
513}
514
515inline uint64_t RelocationRef::getType() const {
516 return OwningObject->getRelocationType(RelocationPimpl);
517}
518
519inline void RelocationRef::getTypeName(SmallVectorImpl<char> &Result) const {
520 return OwningObject->getRelocationTypeName(RelocationPimpl, Result);
521}
522
523inline DataRefImpl RelocationRef::getRawDataRefImpl() const {
524 return RelocationPimpl;
525}
526
527inline const ObjectFile *RelocationRef::getObject() const {
528 return OwningObject;
529}
530
531} // end namespace object
532
533} // end namespace llvm
534
535#endif // LLVM_OBJECT_OBJECTFILE_H