Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 1 | //===- ObjectFile.h - File format independent object file -------*- C++ -*-===// |
| 2 | // |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 3 | // 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 Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 6 | // |
| 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 | |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 16 | #include "llvm/ADT/DenseMapInfo.h" |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 17 | #include "llvm/ADT/StringRef.h" |
| 18 | #include "llvm/ADT/Triple.h" |
| 19 | #include "llvm/ADT/iterator_range.h" |
| 20 | #include "llvm/BinaryFormat/Magic.h" |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 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" |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 26 | #include "llvm/Support/MemoryBuffer.h" |
| 27 | #include <cassert> |
| 28 | #include <cstdint> |
| 29 | #include <memory> |
| 30 | #include <system_error> |
| 31 | |
| 32 | namespace llvm { |
| 33 | |
| 34 | class ARMAttributeParser; |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 35 | class SubtargetFeatures; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 36 | |
| 37 | namespace object { |
| 38 | |
| 39 | class COFFObjectFile; |
| 40 | class MachOObjectFile; |
| 41 | class ObjectFile; |
| 42 | class SectionRef; |
| 43 | class SymbolRef; |
| 44 | class symbol_iterator; |
| 45 | class WasmObjectFile; |
| 46 | |
| 47 | using 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. |
| 51 | class RelocationRef { |
| 52 | DataRefImpl RelocationPimpl; |
| 53 | const ObjectFile *OwningObject = nullptr; |
| 54 | |
| 55 | public: |
| 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 Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 67 | /// Get a string that represents the type of this relocation. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 68 | /// |
| 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 | |
| 76 | using 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. |
| 80 | class SectionRef { |
| 81 | friend class SymbolRef; |
| 82 | |
| 83 | DataRefImpl SectionPimpl; |
| 84 | const ObjectFile *OwningObject = nullptr; |
| 85 | |
| 86 | public: |
| 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 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 96 | Expected<StringRef> getName() const; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 97 | uint64_t getAddress() const; |
| 98 | uint64_t getIndex() const; |
| 99 | uint64_t getSize() const; |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 100 | Expected<StringRef> getContents() const; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 101 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 102 | /// Get the alignment of this section as the actual value (not log 2). |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 103 | uint64_t getAlignment() const; |
| 104 | |
| 105 | bool isCompressed() const; |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 106 | /// Whether this section contains instructions. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 107 | bool isText() const; |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 108 | /// Whether this section contains data, not instructions. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 109 | bool isData() const; |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 110 | /// Whether this section contains BSS uninitialized data. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 111 | bool isBSS() const; |
| 112 | bool isVirtual() const; |
| 113 | bool isBitcode() const; |
| 114 | bool isStripped() const; |
| 115 | |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 116 | /// 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 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 125 | /// Whether this section is a debug section. |
| 126 | bool isDebugSection(StringRef SectionName) const; |
| 127 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 128 | bool containsSymbol(SymbolRef S) const; |
| 129 | |
| 130 | relocation_iterator relocation_begin() const; |
| 131 | relocation_iterator relocation_end() const; |
| 132 | iterator_range<relocation_iterator> relocations() const { |
| 133 | return make_range(relocation_begin(), relocation_end()); |
| 134 | } |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 135 | Expected<section_iterator> getRelocatedSection() const; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 136 | |
| 137 | DataRefImpl getRawDataRefImpl() const; |
| 138 | const ObjectFile *getObject() const; |
| 139 | }; |
| 140 | |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 141 | struct SectionedAddress { |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 142 | const static uint64_t UndefSection = UINT64_MAX; |
| 143 | |
| 144 | uint64_t Address = 0; |
| 145 | uint64_t SectionIndex = UndefSection; |
| 146 | }; |
| 147 | |
| 148 | inline bool operator<(const SectionedAddress &LHS, |
| 149 | const SectionedAddress &RHS) { |
| 150 | return std::tie(LHS.SectionIndex, LHS.Address) < |
| 151 | std::tie(RHS.SectionIndex, RHS.Address); |
| 152 | } |
| 153 | |
| 154 | inline bool operator==(const SectionedAddress &LHS, |
| 155 | const SectionedAddress &RHS) { |
| 156 | return std::tie(LHS.SectionIndex, LHS.Address) == |
| 157 | std::tie(RHS.SectionIndex, RHS.Address); |
| 158 | } |
| 159 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 160 | raw_ostream &operator<<(raw_ostream &OS, const SectionedAddress &Addr); |
| 161 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 162 | /// This is a value type class that represents a single symbol in the list of |
| 163 | /// symbols in the object file. |
| 164 | class SymbolRef : public BasicSymbolRef { |
| 165 | friend class SectionRef; |
| 166 | |
| 167 | public: |
| 168 | enum Type { |
| 169 | ST_Unknown, // Type not specified |
| 170 | ST_Data, |
| 171 | ST_Debug, |
| 172 | ST_File, |
| 173 | ST_Function, |
| 174 | ST_Other |
| 175 | }; |
| 176 | |
| 177 | SymbolRef() = default; |
| 178 | SymbolRef(DataRefImpl SymbolP, const ObjectFile *Owner); |
| 179 | SymbolRef(const BasicSymbolRef &B) : BasicSymbolRef(B) { |
| 180 | assert(isa<ObjectFile>(BasicSymbolRef::getObject())); |
| 181 | } |
| 182 | |
| 183 | Expected<StringRef> getName() const; |
| 184 | /// Returns the symbol virtual address (i.e. address at which it will be |
| 185 | /// mapped). |
| 186 | Expected<uint64_t> getAddress() const; |
| 187 | |
| 188 | /// Return the value of the symbol depending on the object this can be an |
| 189 | /// offset or a virtual address. |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 190 | Expected<uint64_t> getValue() const; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 191 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 192 | /// Get the alignment of this symbol as the actual value (not log 2). |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 193 | uint32_t getAlignment() const; |
| 194 | uint64_t getCommonSize() const; |
| 195 | Expected<SymbolRef::Type> getType() const; |
| 196 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 197 | /// Get section this symbol is defined in reference to. Result is |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 198 | /// end_sections() if it is undefined or is an absolute symbol. |
| 199 | Expected<section_iterator> getSection() const; |
| 200 | |
| 201 | const ObjectFile *getObject() const; |
| 202 | }; |
| 203 | |
| 204 | class symbol_iterator : public basic_symbol_iterator { |
| 205 | public: |
| 206 | symbol_iterator(SymbolRef Sym) : basic_symbol_iterator(Sym) {} |
| 207 | symbol_iterator(const basic_symbol_iterator &B) |
| 208 | : basic_symbol_iterator(SymbolRef(B->getRawDataRefImpl(), |
| 209 | cast<ObjectFile>(B->getObject()))) {} |
| 210 | |
| 211 | const SymbolRef *operator->() const { |
| 212 | const BasicSymbolRef &P = basic_symbol_iterator::operator *(); |
| 213 | return static_cast<const SymbolRef*>(&P); |
| 214 | } |
| 215 | |
| 216 | const SymbolRef &operator*() const { |
| 217 | const BasicSymbolRef &P = basic_symbol_iterator::operator *(); |
| 218 | return static_cast<const SymbolRef&>(P); |
| 219 | } |
| 220 | }; |
| 221 | |
| 222 | /// This class is the base class for all object file types. Concrete instances |
| 223 | /// of this object are created by createObjectFile, which figures out which type |
| 224 | /// to create. |
| 225 | class ObjectFile : public SymbolicFile { |
| 226 | virtual void anchor(); |
| 227 | |
| 228 | protected: |
| 229 | ObjectFile(unsigned int Type, MemoryBufferRef Source); |
| 230 | |
| 231 | const uint8_t *base() const { |
| 232 | return reinterpret_cast<const uint8_t *>(Data.getBufferStart()); |
| 233 | } |
| 234 | |
| 235 | // These functions are for SymbolRef to call internally. The main goal of |
| 236 | // this is to allow SymbolRef::SymbolPimpl to point directly to the symbol |
| 237 | // entry in the memory mapped object file. SymbolPimpl cannot contain any |
| 238 | // virtual functions because then it could not point into the memory mapped |
| 239 | // file. |
| 240 | // |
| 241 | // Implementations assume that the DataRefImpl is valid and has not been |
| 242 | // modified externally. It's UB otherwise. |
| 243 | friend class SymbolRef; |
| 244 | |
| 245 | virtual Expected<StringRef> getSymbolName(DataRefImpl Symb) const = 0; |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 246 | Error printSymbolName(raw_ostream &OS, |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 247 | DataRefImpl Symb) const override; |
| 248 | virtual Expected<uint64_t> getSymbolAddress(DataRefImpl Symb) const = 0; |
| 249 | virtual uint64_t getSymbolValueImpl(DataRefImpl Symb) const = 0; |
| 250 | virtual uint32_t getSymbolAlignment(DataRefImpl Symb) const; |
| 251 | virtual uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const = 0; |
| 252 | virtual Expected<SymbolRef::Type> getSymbolType(DataRefImpl Symb) const = 0; |
| 253 | virtual Expected<section_iterator> |
| 254 | getSymbolSection(DataRefImpl Symb) const = 0; |
| 255 | |
| 256 | // Same as above for SectionRef. |
| 257 | friend class SectionRef; |
| 258 | |
| 259 | virtual void moveSectionNext(DataRefImpl &Sec) const = 0; |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 260 | virtual Expected<StringRef> getSectionName(DataRefImpl Sec) const = 0; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 261 | virtual uint64_t getSectionAddress(DataRefImpl Sec) const = 0; |
| 262 | virtual uint64_t getSectionIndex(DataRefImpl Sec) const = 0; |
| 263 | virtual uint64_t getSectionSize(DataRefImpl Sec) const = 0; |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 264 | virtual Expected<ArrayRef<uint8_t>> |
| 265 | getSectionContents(DataRefImpl Sec) const = 0; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 266 | virtual uint64_t getSectionAlignment(DataRefImpl Sec) const = 0; |
| 267 | virtual bool isSectionCompressed(DataRefImpl Sec) const = 0; |
| 268 | virtual bool isSectionText(DataRefImpl Sec) const = 0; |
| 269 | virtual bool isSectionData(DataRefImpl Sec) const = 0; |
| 270 | virtual bool isSectionBSS(DataRefImpl Sec) const = 0; |
| 271 | // A section is 'virtual' if its contents aren't present in the object image. |
| 272 | virtual bool isSectionVirtual(DataRefImpl Sec) const = 0; |
| 273 | virtual bool isSectionBitcode(DataRefImpl Sec) const; |
| 274 | virtual bool isSectionStripped(DataRefImpl Sec) const; |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 275 | virtual bool isBerkeleyText(DataRefImpl Sec) const; |
| 276 | virtual bool isBerkeleyData(DataRefImpl Sec) const; |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 277 | virtual bool isDebugSection(StringRef SectionName) const; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 278 | virtual relocation_iterator section_rel_begin(DataRefImpl Sec) const = 0; |
| 279 | virtual relocation_iterator section_rel_end(DataRefImpl Sec) const = 0; |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 280 | virtual Expected<section_iterator> getRelocatedSection(DataRefImpl Sec) const; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 281 | |
| 282 | // Same as above for RelocationRef. |
| 283 | friend class RelocationRef; |
| 284 | virtual void moveRelocationNext(DataRefImpl &Rel) const = 0; |
| 285 | virtual uint64_t getRelocationOffset(DataRefImpl Rel) const = 0; |
| 286 | virtual symbol_iterator getRelocationSymbol(DataRefImpl Rel) const = 0; |
| 287 | virtual uint64_t getRelocationType(DataRefImpl Rel) const = 0; |
| 288 | virtual void getRelocationTypeName(DataRefImpl Rel, |
| 289 | SmallVectorImpl<char> &Result) const = 0; |
| 290 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 291 | Expected<uint64_t> getSymbolValue(DataRefImpl Symb) const; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 292 | |
| 293 | public: |
| 294 | ObjectFile() = delete; |
| 295 | ObjectFile(const ObjectFile &other) = delete; |
| 296 | |
| 297 | uint64_t getCommonSymbolSize(DataRefImpl Symb) const { |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 298 | Expected<uint32_t> SymbolFlagsOrErr = getSymbolFlags(Symb); |
| 299 | if (!SymbolFlagsOrErr) |
| 300 | // TODO: Actually report errors helpfully. |
| 301 | report_fatal_error(SymbolFlagsOrErr.takeError()); |
| 302 | assert(*SymbolFlagsOrErr & SymbolRef::SF_Common); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 303 | return getCommonSymbolSizeImpl(Symb); |
| 304 | } |
| 305 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 306 | virtual std::vector<SectionRef> dynamic_relocation_sections() const { |
| 307 | return std::vector<SectionRef>(); |
| 308 | } |
| 309 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 310 | using symbol_iterator_range = iterator_range<symbol_iterator>; |
| 311 | symbol_iterator_range symbols() const { |
| 312 | return symbol_iterator_range(symbol_begin(), symbol_end()); |
| 313 | } |
| 314 | |
| 315 | virtual section_iterator section_begin() const = 0; |
| 316 | virtual section_iterator section_end() const = 0; |
| 317 | |
| 318 | using section_iterator_range = iterator_range<section_iterator>; |
| 319 | section_iterator_range sections() const { |
| 320 | return section_iterator_range(section_begin(), section_end()); |
| 321 | } |
| 322 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 323 | /// The number of bytes used to represent an address in this object |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 324 | /// file format. |
| 325 | virtual uint8_t getBytesInAddress() const = 0; |
| 326 | |
| 327 | virtual StringRef getFileFormatName() const = 0; |
| 328 | virtual Triple::ArchType getArch() const = 0; |
| 329 | virtual SubtargetFeatures getFeatures() const = 0; |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 330 | virtual Optional<StringRef> tryGetCPUName() const { return None; }; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 331 | virtual void setARMSubArch(Triple &TheTriple) const { } |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 332 | virtual Expected<uint64_t> getStartAddress() const { |
| 333 | return errorCodeToError(object_error::parse_failed); |
| 334 | }; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 335 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 336 | /// Create a triple from the data in this object file. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 337 | Triple makeTriple() const; |
| 338 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 339 | /// Maps a debug section name to a standard DWARF section name. |
| 340 | virtual StringRef mapDebugSectionName(StringRef Name) const { return Name; } |
| 341 | |
| 342 | /// True if this is a relocatable object (.o/.obj). |
| 343 | virtual bool isRelocatableObject() const = 0; |
| 344 | |
| 345 | /// @returns Pointer to ObjectFile subclass to handle this type of object. |
| 346 | /// @param ObjectPath The path to the object file. ObjectPath.isObject must |
| 347 | /// return true. |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 348 | /// Create ObjectFile from path. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 349 | static Expected<OwningBinary<ObjectFile>> |
| 350 | createObjectFile(StringRef ObjectPath); |
| 351 | |
| 352 | static Expected<std::unique_ptr<ObjectFile>> |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 353 | createObjectFile(MemoryBufferRef Object, llvm::file_magic Type, |
| 354 | bool InitContent = true); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 355 | static Expected<std::unique_ptr<ObjectFile>> |
| 356 | createObjectFile(MemoryBufferRef Object) { |
| 357 | return createObjectFile(Object, llvm::file_magic::unknown); |
| 358 | } |
| 359 | |
| 360 | static bool classof(const Binary *v) { |
| 361 | return v->isObject(); |
| 362 | } |
| 363 | |
| 364 | static Expected<std::unique_ptr<COFFObjectFile>> |
| 365 | createCOFFObjectFile(MemoryBufferRef Object); |
| 366 | |
| 367 | static Expected<std::unique_ptr<ObjectFile>> |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 368 | createXCOFFObjectFile(MemoryBufferRef Object, unsigned FileType); |
| 369 | |
| 370 | static Expected<std::unique_ptr<ObjectFile>> |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 371 | createELFObjectFile(MemoryBufferRef Object, bool InitContent = true); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 372 | |
| 373 | static Expected<std::unique_ptr<MachOObjectFile>> |
| 374 | createMachOObjectFile(MemoryBufferRef Object, |
| 375 | uint32_t UniversalCputype = 0, |
| 376 | uint32_t UniversalIndex = 0); |
| 377 | |
| 378 | static Expected<std::unique_ptr<WasmObjectFile>> |
| 379 | createWasmObjectFile(MemoryBufferRef Object); |
| 380 | }; |
| 381 | |
| 382 | // Inline function definitions. |
| 383 | inline SymbolRef::SymbolRef(DataRefImpl SymbolP, const ObjectFile *Owner) |
| 384 | : BasicSymbolRef(SymbolP, Owner) {} |
| 385 | |
| 386 | inline Expected<StringRef> SymbolRef::getName() const { |
| 387 | return getObject()->getSymbolName(getRawDataRefImpl()); |
| 388 | } |
| 389 | |
| 390 | inline Expected<uint64_t> SymbolRef::getAddress() const { |
| 391 | return getObject()->getSymbolAddress(getRawDataRefImpl()); |
| 392 | } |
| 393 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 394 | inline Expected<uint64_t> SymbolRef::getValue() const { |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 395 | return getObject()->getSymbolValue(getRawDataRefImpl()); |
| 396 | } |
| 397 | |
| 398 | inline uint32_t SymbolRef::getAlignment() const { |
| 399 | return getObject()->getSymbolAlignment(getRawDataRefImpl()); |
| 400 | } |
| 401 | |
| 402 | inline uint64_t SymbolRef::getCommonSize() const { |
| 403 | return getObject()->getCommonSymbolSize(getRawDataRefImpl()); |
| 404 | } |
| 405 | |
| 406 | inline Expected<section_iterator> SymbolRef::getSection() const { |
| 407 | return getObject()->getSymbolSection(getRawDataRefImpl()); |
| 408 | } |
| 409 | |
| 410 | inline Expected<SymbolRef::Type> SymbolRef::getType() const { |
| 411 | return getObject()->getSymbolType(getRawDataRefImpl()); |
| 412 | } |
| 413 | |
| 414 | inline const ObjectFile *SymbolRef::getObject() const { |
| 415 | const SymbolicFile *O = BasicSymbolRef::getObject(); |
| 416 | return cast<ObjectFile>(O); |
| 417 | } |
| 418 | |
| 419 | /// SectionRef |
| 420 | inline SectionRef::SectionRef(DataRefImpl SectionP, |
| 421 | const ObjectFile *Owner) |
| 422 | : SectionPimpl(SectionP) |
| 423 | , OwningObject(Owner) {} |
| 424 | |
| 425 | inline bool SectionRef::operator==(const SectionRef &Other) const { |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 426 | return OwningObject == Other.OwningObject && |
| 427 | SectionPimpl == Other.SectionPimpl; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | inline bool SectionRef::operator!=(const SectionRef &Other) const { |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 431 | return !(*this == Other); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | inline bool SectionRef::operator<(const SectionRef &Other) const { |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 435 | assert(OwningObject == Other.OwningObject); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 436 | return SectionPimpl < Other.SectionPimpl; |
| 437 | } |
| 438 | |
| 439 | inline void SectionRef::moveNext() { |
| 440 | return OwningObject->moveSectionNext(SectionPimpl); |
| 441 | } |
| 442 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 443 | inline Expected<StringRef> SectionRef::getName() const { |
| 444 | return OwningObject->getSectionName(SectionPimpl); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | inline uint64_t SectionRef::getAddress() const { |
| 448 | return OwningObject->getSectionAddress(SectionPimpl); |
| 449 | } |
| 450 | |
| 451 | inline uint64_t SectionRef::getIndex() const { |
| 452 | return OwningObject->getSectionIndex(SectionPimpl); |
| 453 | } |
| 454 | |
| 455 | inline uint64_t SectionRef::getSize() const { |
| 456 | return OwningObject->getSectionSize(SectionPimpl); |
| 457 | } |
| 458 | |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 459 | inline Expected<StringRef> SectionRef::getContents() const { |
| 460 | Expected<ArrayRef<uint8_t>> Res = |
| 461 | OwningObject->getSectionContents(SectionPimpl); |
| 462 | if (!Res) |
| 463 | return Res.takeError(); |
| 464 | return StringRef(reinterpret_cast<const char *>(Res->data()), Res->size()); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | inline uint64_t SectionRef::getAlignment() const { |
| 468 | return OwningObject->getSectionAlignment(SectionPimpl); |
| 469 | } |
| 470 | |
| 471 | inline bool SectionRef::isCompressed() const { |
| 472 | return OwningObject->isSectionCompressed(SectionPimpl); |
| 473 | } |
| 474 | |
| 475 | inline bool SectionRef::isText() const { |
| 476 | return OwningObject->isSectionText(SectionPimpl); |
| 477 | } |
| 478 | |
| 479 | inline bool SectionRef::isData() const { |
| 480 | return OwningObject->isSectionData(SectionPimpl); |
| 481 | } |
| 482 | |
| 483 | inline bool SectionRef::isBSS() const { |
| 484 | return OwningObject->isSectionBSS(SectionPimpl); |
| 485 | } |
| 486 | |
| 487 | inline bool SectionRef::isVirtual() const { |
| 488 | return OwningObject->isSectionVirtual(SectionPimpl); |
| 489 | } |
| 490 | |
| 491 | inline bool SectionRef::isBitcode() const { |
| 492 | return OwningObject->isSectionBitcode(SectionPimpl); |
| 493 | } |
| 494 | |
| 495 | inline bool SectionRef::isStripped() const { |
| 496 | return OwningObject->isSectionStripped(SectionPimpl); |
| 497 | } |
| 498 | |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 499 | inline bool SectionRef::isBerkeleyText() const { |
| 500 | return OwningObject->isBerkeleyText(SectionPimpl); |
| 501 | } |
| 502 | |
| 503 | inline bool SectionRef::isBerkeleyData() const { |
| 504 | return OwningObject->isBerkeleyData(SectionPimpl); |
| 505 | } |
| 506 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 507 | inline bool SectionRef::isDebugSection(StringRef SectionName) const { |
| 508 | return OwningObject->isDebugSection(SectionName); |
| 509 | } |
| 510 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 511 | inline relocation_iterator SectionRef::relocation_begin() const { |
| 512 | return OwningObject->section_rel_begin(SectionPimpl); |
| 513 | } |
| 514 | |
| 515 | inline relocation_iterator SectionRef::relocation_end() const { |
| 516 | return OwningObject->section_rel_end(SectionPimpl); |
| 517 | } |
| 518 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 519 | inline Expected<section_iterator> SectionRef::getRelocatedSection() const { |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 520 | return OwningObject->getRelocatedSection(SectionPimpl); |
| 521 | } |
| 522 | |
| 523 | inline DataRefImpl SectionRef::getRawDataRefImpl() const { |
| 524 | return SectionPimpl; |
| 525 | } |
| 526 | |
| 527 | inline const ObjectFile *SectionRef::getObject() const { |
| 528 | return OwningObject; |
| 529 | } |
| 530 | |
| 531 | /// RelocationRef |
| 532 | inline RelocationRef::RelocationRef(DataRefImpl RelocationP, |
| 533 | const ObjectFile *Owner) |
| 534 | : RelocationPimpl(RelocationP) |
| 535 | , OwningObject(Owner) {} |
| 536 | |
| 537 | inline bool RelocationRef::operator==(const RelocationRef &Other) const { |
| 538 | return RelocationPimpl == Other.RelocationPimpl; |
| 539 | } |
| 540 | |
| 541 | inline void RelocationRef::moveNext() { |
| 542 | return OwningObject->moveRelocationNext(RelocationPimpl); |
| 543 | } |
| 544 | |
| 545 | inline uint64_t RelocationRef::getOffset() const { |
| 546 | return OwningObject->getRelocationOffset(RelocationPimpl); |
| 547 | } |
| 548 | |
| 549 | inline symbol_iterator RelocationRef::getSymbol() const { |
| 550 | return OwningObject->getRelocationSymbol(RelocationPimpl); |
| 551 | } |
| 552 | |
| 553 | inline uint64_t RelocationRef::getType() const { |
| 554 | return OwningObject->getRelocationType(RelocationPimpl); |
| 555 | } |
| 556 | |
| 557 | inline void RelocationRef::getTypeName(SmallVectorImpl<char> &Result) const { |
| 558 | return OwningObject->getRelocationTypeName(RelocationPimpl, Result); |
| 559 | } |
| 560 | |
| 561 | inline DataRefImpl RelocationRef::getRawDataRefImpl() const { |
| 562 | return RelocationPimpl; |
| 563 | } |
| 564 | |
| 565 | inline const ObjectFile *RelocationRef::getObject() const { |
| 566 | return OwningObject; |
| 567 | } |
| 568 | |
| 569 | } // end namespace object |
| 570 | |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 571 | template <> struct DenseMapInfo<object::SectionRef> { |
| 572 | static bool isEqual(const object::SectionRef &A, |
| 573 | const object::SectionRef &B) { |
| 574 | return A == B; |
| 575 | } |
| 576 | static object::SectionRef getEmptyKey() { |
| 577 | return object::SectionRef({}, nullptr); |
| 578 | } |
| 579 | static object::SectionRef getTombstoneKey() { |
| 580 | object::DataRefImpl TS; |
| 581 | TS.p = (uintptr_t)-1; |
| 582 | return object::SectionRef(TS, nullptr); |
| 583 | } |
| 584 | static unsigned getHashValue(const object::SectionRef &Sec) { |
| 585 | object::DataRefImpl Raw = Sec.getRawDataRefImpl(); |
| 586 | return hash_combine(Raw.p, Raw.d.a, Raw.d.b); |
| 587 | } |
| 588 | }; |
| 589 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 590 | } // end namespace llvm |
| 591 | |
| 592 | #endif // LLVM_OBJECT_OBJECTFILE_H |