| 1 | //===- Archive.h - ar archive file format -----------------------*- C++ -*-===// |
| 2 | // |
| 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 |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file declares the ar archive file format class. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #ifndef LLVM_OBJECT_ARCHIVE_H |
| 14 | #define LLVM_OBJECT_ARCHIVE_H |
| 15 | |
| 16 | #include "llvm/ADT/StringRef.h" |
| 17 | #include "llvm/ADT/fallible_iterator.h" |
| 18 | #include "llvm/ADT/iterator_range.h" |
| 19 | #include "llvm/Object/Binary.h" |
| 20 | #include "llvm/Support/Chrono.h" |
| 21 | #include "llvm/Support/Compiler.h" |
| 22 | #include "llvm/Support/Error.h" |
| 23 | #include "llvm/Support/FileSystem.h" |
| 24 | #include "llvm/Support/MemoryBuffer.h" |
| 25 | #include <cassert> |
| 26 | #include <cstdint> |
| 27 | #include <memory> |
| 28 | #include <string> |
| 29 | #include <vector> |
| 30 | |
| 31 | namespace llvm { |
| 32 | namespace object { |
| 33 | |
| 34 | const char ArchiveMagic[] = "!<arch>\n" ; |
| 35 | const char ThinArchiveMagic[] = "!<thin>\n" ; |
| 36 | const char BigArchiveMagic[] = "<bigaf>\n" ; |
| 37 | |
| 38 | class Archive; |
| 39 | |
| 40 | class { |
| 41 | protected: |
| 42 | (const Archive *Parent) : Parent(Parent){}; |
| 43 | |
| 44 | public: |
| 45 | friend class Archive; |
| 46 | virtual std::unique_ptr<AbstractArchiveMemberHeader> () const = 0; |
| 47 | virtual () = default; |
| 48 | |
| 49 | /// Get the name without looking up long names. |
| 50 | virtual Expected<StringRef> () const = 0; |
| 51 | virtual StringRef () const = 0; |
| 52 | virtual StringRef () const = 0; |
| 53 | virtual StringRef () const = 0; |
| 54 | virtual StringRef () const = 0; |
| 55 | |
| 56 | /// Get the name looking up long names. |
| 57 | virtual Expected<StringRef> (uint64_t Size) const = 0; |
| 58 | virtual Expected<uint64_t> () const = 0; |
| 59 | virtual uint64_t () const = 0; |
| 60 | |
| 61 | /// Get next file member location. |
| 62 | virtual Expected<const char *> () const = 0; |
| 63 | virtual Expected<bool> () const = 0; |
| 64 | |
| 65 | LLVM_ABI Expected<sys::fs::perms> () const; |
| 66 | LLVM_ABI Expected<sys::TimePoint<std::chrono::seconds>> |
| 67 | () const; |
| 68 | LLVM_ABI Expected<unsigned> () const; |
| 69 | LLVM_ABI Expected<unsigned> () const; |
| 70 | |
| 71 | /// Returns the size in bytes of the format-defined member header of the |
| 72 | /// concrete archive type. |
| 73 | virtual uint64_t () const = 0; |
| 74 | |
| 75 | const Archive *; |
| 76 | }; |
| 77 | |
| 78 | template <typename T> |
| 79 | class LLVM_ABI : public AbstractArchiveMemberHeader { |
| 80 | public: |
| 81 | (const Archive *Parent, const T *) |
| 82 | : AbstractArchiveMemberHeader(Parent), ArMemHdr(RawHeaderPtr){}; |
| 83 | StringRef () const override; |
| 84 | StringRef () const override; |
| 85 | StringRef () const override; |
| 86 | StringRef () const override; |
| 87 | |
| 88 | uint64_t () const override; |
| 89 | uint64_t () const override { return sizeof(T); } |
| 90 | |
| 91 | T const *; |
| 92 | }; |
| 93 | |
| 94 | struct UnixArMemHdrType { |
| 95 | char Name[16]; |
| 96 | char LastModified[12]; |
| 97 | char UID[6]; |
| 98 | char GID[6]; |
| 99 | char AccessMode[8]; |
| 100 | char Size[10]; ///< Size of data, not including header or padding. |
| 101 | char Terminator[2]; |
| 102 | }; |
| 103 | |
| 104 | class LLVM_ABI |
| 105 | : public CommonArchiveMemberHeader<UnixArMemHdrType> { |
| 106 | public: |
| 107 | (const Archive *Parent, const char *, |
| 108 | uint64_t Size, Error *Err); |
| 109 | |
| 110 | std::unique_ptr<AbstractArchiveMemberHeader> () const override { |
| 111 | return std::make_unique<ArchiveMemberHeader>(args: *this); |
| 112 | } |
| 113 | |
| 114 | Expected<StringRef> () const override; |
| 115 | |
| 116 | Expected<StringRef> (uint64_t Size) const override; |
| 117 | Expected<uint64_t> () const override; |
| 118 | Expected<const char *> () const override; |
| 119 | Expected<bool> () const override; |
| 120 | }; |
| 121 | |
| 122 | // File Member Header |
| 123 | struct BigArMemHdrType { |
| 124 | char Size[20]; // File member size in decimal |
| 125 | char NextOffset[20]; // Next member offset in decimal |
| 126 | char PrevOffset[20]; // Previous member offset in decimal |
| 127 | char LastModified[12]; |
| 128 | char UID[12]; |
| 129 | char GID[12]; |
| 130 | char AccessMode[12]; |
| 131 | char NameLen[4]; // File member name length in decimal |
| 132 | union { |
| 133 | char Name[2]; // Start of member name |
| 134 | char Terminator[2]; |
| 135 | }; |
| 136 | }; |
| 137 | |
| 138 | // Define file member header of AIX big archive. |
| 139 | class LLVM_ABI |
| 140 | : public CommonArchiveMemberHeader<BigArMemHdrType> { |
| 141 | |
| 142 | public: |
| 143 | (Archive const *Parent, const char *, |
| 144 | uint64_t Size, Error *Err); |
| 145 | std::unique_ptr<AbstractArchiveMemberHeader> () const override { |
| 146 | return std::make_unique<BigArchiveMemberHeader>(args: *this); |
| 147 | } |
| 148 | |
| 149 | Expected<StringRef> () const override; |
| 150 | Expected<uint64_t> () const; |
| 151 | |
| 152 | Expected<StringRef> (uint64_t Size) const override; |
| 153 | Expected<uint64_t> () const override; |
| 154 | Expected<const char *> () const override; |
| 155 | Expected<uint64_t> () const; |
| 156 | Expected<bool> () const override { return false; } |
| 157 | }; |
| 158 | |
| 159 | class LLVM_ABI Archive : public Binary { |
| 160 | virtual void anchor(); |
| 161 | |
| 162 | public: |
| 163 | class Child { |
| 164 | friend Archive; |
| 165 | friend AbstractArchiveMemberHeader; |
| 166 | |
| 167 | const Archive *Parent; |
| 168 | std::unique_ptr<AbstractArchiveMemberHeader> ; |
| 169 | /// Includes header but not padding byte. |
| 170 | StringRef Data; |
| 171 | /// Offset from Data to the start of the file. |
| 172 | uint16_t StartOfFile; |
| 173 | |
| 174 | Expected<bool> isThinMember() const; |
| 175 | |
| 176 | public: |
| 177 | LLVM_ABI Child(const Archive *Parent, const char *Start, Error *Err); |
| 178 | LLVM_ABI Child(const Archive *Parent, StringRef Data, uint16_t StartOfFile); |
| 179 | |
| 180 | Child(const Child &C) |
| 181 | : Parent(C.Parent), Data(C.Data), StartOfFile(C.StartOfFile) { |
| 182 | if (C.Header) |
| 183 | Header = C.Header->clone(); |
| 184 | } |
| 185 | |
| 186 | Child(Child &&C) { |
| 187 | Parent = std::move(C.Parent); |
| 188 | Header = std::move(C.Header); |
| 189 | Data = C.Data; |
| 190 | StartOfFile = C.StartOfFile; |
| 191 | } |
| 192 | |
| 193 | Child &operator=(Child &&C) noexcept { |
| 194 | if (&C == this) |
| 195 | return *this; |
| 196 | |
| 197 | Parent = std::move(C.Parent); |
| 198 | Header = std::move(C.Header); |
| 199 | Data = C.Data; |
| 200 | StartOfFile = C.StartOfFile; |
| 201 | |
| 202 | return *this; |
| 203 | } |
| 204 | |
| 205 | Child &operator=(const Child &C) { |
| 206 | if (&C == this) |
| 207 | return *this; |
| 208 | |
| 209 | Parent = C.Parent; |
| 210 | if (C.Header) |
| 211 | Header = C.Header->clone(); |
| 212 | Data = C.Data; |
| 213 | StartOfFile = C.StartOfFile; |
| 214 | |
| 215 | return *this; |
| 216 | } |
| 217 | |
| 218 | bool operator==(const Child &other) const { |
| 219 | assert(!Parent || !other.Parent || Parent == other.Parent); |
| 220 | return Data.begin() == other.Data.begin(); |
| 221 | } |
| 222 | |
| 223 | const Archive *getParent() const { return Parent; } |
| 224 | LLVM_ABI Expected<Child> getNext() const; |
| 225 | |
| 226 | LLVM_ABI Expected<StringRef> getName() const; |
| 227 | LLVM_ABI Expected<std::string> getFullName() const; |
| 228 | Expected<StringRef> getRawName() const { return Header->getRawName(); } |
| 229 | |
| 230 | Expected<sys::TimePoint<std::chrono::seconds>> getLastModified() const { |
| 231 | return Header->getLastModified(); |
| 232 | } |
| 233 | |
| 234 | StringRef getRawLastModified() const { |
| 235 | return Header->getRawLastModified(); |
| 236 | } |
| 237 | |
| 238 | Expected<unsigned> getUID() const { return Header->getUID(); } |
| 239 | Expected<unsigned> getGID() const { return Header->getGID(); } |
| 240 | |
| 241 | Expected<sys::fs::perms> getAccessMode() const { |
| 242 | return Header->getAccessMode(); |
| 243 | } |
| 244 | |
| 245 | /// \return the size of the archive member without the header or padding. |
| 246 | LLVM_ABI Expected<uint64_t> getSize() const; |
| 247 | /// \return the size in the archive header for this member. |
| 248 | LLVM_ABI Expected<uint64_t> getRawSize() const; |
| 249 | |
| 250 | LLVM_ABI Expected<StringRef> getBuffer() const; |
| 251 | LLVM_ABI uint64_t getChildOffset() const; |
| 252 | uint64_t getDataOffset() const { return getChildOffset() + StartOfFile; } |
| 253 | |
| 254 | LLVM_ABI Expected<MemoryBufferRef> getMemoryBufferRef() const; |
| 255 | |
| 256 | LLVM_ABI Expected<std::unique_ptr<Binary>> |
| 257 | getAsBinary(LLVMContext *Context = nullptr) const; |
| 258 | }; |
| 259 | |
| 260 | class ChildFallibleIterator { |
| 261 | Child C; |
| 262 | |
| 263 | public: |
| 264 | ChildFallibleIterator() : C(Child(nullptr, nullptr, nullptr)) {} |
| 265 | ChildFallibleIterator(const Child &C) : C(C) {} |
| 266 | |
| 267 | const Child *operator->() const { return &C; } |
| 268 | const Child &operator*() const { return C; } |
| 269 | |
| 270 | bool operator==(const ChildFallibleIterator &other) const { |
| 271 | // Ignore errors here: If an error occurred during increment then getNext |
| 272 | // will have been set to child_end(), and the following comparison should |
| 273 | // do the right thing. |
| 274 | return C == other.C; |
| 275 | } |
| 276 | |
| 277 | bool operator!=(const ChildFallibleIterator &other) const { |
| 278 | return !(*this == other); |
| 279 | } |
| 280 | |
| 281 | Error inc() { |
| 282 | auto NextChild = C.getNext(); |
| 283 | if (!NextChild) |
| 284 | return NextChild.takeError(); |
| 285 | C = std::move(*NextChild); |
| 286 | return Error::success(); |
| 287 | } |
| 288 | }; |
| 289 | |
| 290 | using child_iterator = fallible_iterator<ChildFallibleIterator>; |
| 291 | |
| 292 | class Symbol { |
| 293 | const Archive *Parent; |
| 294 | uint32_t SymbolIndex; |
| 295 | uint32_t StringIndex; // Extra index to the string. |
| 296 | |
| 297 | public: |
| 298 | Symbol(const Archive *p, uint32_t symi, uint32_t stri) |
| 299 | : Parent(p), SymbolIndex(symi), StringIndex(stri) {} |
| 300 | |
| 301 | bool operator==(const Symbol &other) const { |
| 302 | return (Parent == other.Parent) && (SymbolIndex == other.SymbolIndex); |
| 303 | } |
| 304 | |
| 305 | LLVM_ABI StringRef getName() const; |
| 306 | LLVM_ABI Expected<Child> getMember() const; |
| 307 | LLVM_ABI Symbol getNext() const; |
| 308 | LLVM_ABI bool isECSymbol() const; |
| 309 | }; |
| 310 | |
| 311 | class symbol_iterator { |
| 312 | Symbol symbol; |
| 313 | |
| 314 | public: |
| 315 | symbol_iterator(const Symbol &s) : symbol(s) {} |
| 316 | |
| 317 | const Symbol *operator->() const { return &symbol; } |
| 318 | const Symbol &operator*() const { return symbol; } |
| 319 | |
| 320 | bool operator==(const symbol_iterator &other) const { |
| 321 | return symbol == other.symbol; |
| 322 | } |
| 323 | |
| 324 | bool operator!=(const symbol_iterator &other) const { |
| 325 | return !(*this == other); |
| 326 | } |
| 327 | |
| 328 | symbol_iterator &operator++() { // Preincrement |
| 329 | symbol = symbol.getNext(); |
| 330 | return *this; |
| 331 | } |
| 332 | }; |
| 333 | |
| 334 | Archive(MemoryBufferRef Source, Error &Err); |
| 335 | static Expected<std::unique_ptr<Archive>> create(MemoryBufferRef Source); |
| 336 | |
| 337 | // Explicitly non-copyable. |
| 338 | Archive(Archive const &) = delete; |
| 339 | Archive &operator=(Archive const &) = delete; |
| 340 | |
| 341 | /// Size field is 10 decimal digits long |
| 342 | static const uint64_t MaxMemberSize = 9999999999; |
| 343 | |
| 344 | enum Kind { K_GNU, K_GNU64, K_BSD, K_DARWIN, K_DARWIN64, K_COFF, K_AIXBIG }; |
| 345 | |
| 346 | Kind kind() const { return (Kind)Format; } |
| 347 | bool isThin() const { return IsThin; } |
| 348 | static object::Archive::Kind getDefaultKind(); |
| 349 | static object::Archive::Kind getDefaultKindForTriple(const Triple &T); |
| 350 | |
| 351 | child_iterator child_begin(Error &Err, bool SkipInternal = true) const; |
| 352 | child_iterator child_end() const; |
| 353 | iterator_range<child_iterator> children(Error &Err, |
| 354 | bool SkipInternal = true) const { |
| 355 | return make_range(x: child_begin(Err, SkipInternal), y: child_end()); |
| 356 | } |
| 357 | |
| 358 | symbol_iterator symbol_begin() const; |
| 359 | symbol_iterator symbol_end() const; |
| 360 | iterator_range<symbol_iterator> symbols() const { |
| 361 | return make_range(x: symbol_begin(), y: symbol_end()); |
| 362 | } |
| 363 | |
| 364 | Expected<iterator_range<symbol_iterator>> ec_symbols() const; |
| 365 | |
| 366 | static bool classof(Binary const *v) { return v->isArchive(); } |
| 367 | |
| 368 | // check if a symbol is in the archive |
| 369 | Expected<std::optional<Child>> findSym(StringRef name) const; |
| 370 | |
| 371 | virtual bool isEmpty() const; |
| 372 | bool hasSymbolTable() const; |
| 373 | StringRef getSymbolTable() const { return SymbolTable; } |
| 374 | StringRef getStringTable() const { return StringTable; } |
| 375 | uint32_t getNumberOfSymbols() const; |
| 376 | uint32_t getNumberOfECSymbols() const; |
| 377 | virtual uint64_t getFirstChildOffset() const { return getArchiveMagicLen(); } |
| 378 | |
| 379 | std::vector<std::unique_ptr<MemoryBuffer>> takeThinBuffers() { |
| 380 | return std::move(ThinBuffers); |
| 381 | } |
| 382 | |
| 383 | std::unique_ptr<AbstractArchiveMemberHeader> |
| 384 | (const char *, uint64_t Size, |
| 385 | Error *Err) const; |
| 386 | |
| 387 | protected: |
| 388 | uint64_t getArchiveMagicLen() const; |
| 389 | void setFirstRegular(const Child &C); |
| 390 | |
| 391 | StringRef SymbolTable; |
| 392 | StringRef ECSymbolTable; |
| 393 | StringRef StringTable; |
| 394 | |
| 395 | private: |
| 396 | StringRef FirstRegularData; |
| 397 | uint16_t FirstRegularStartOfFile = -1; |
| 398 | |
| 399 | unsigned Format : 3; |
| 400 | unsigned IsThin : 1; |
| 401 | mutable std::vector<std::unique_ptr<MemoryBuffer>> ThinBuffers; |
| 402 | }; |
| 403 | |
| 404 | class BigArchive : public Archive { |
| 405 | public: |
| 406 | /// Fixed-Length Header. |
| 407 | struct FixLenHdr { |
| 408 | char Magic[sizeof(BigArchiveMagic) - 1]; ///< Big archive magic string. |
| 409 | char MemOffset[20]; ///< Offset to member table. |
| 410 | char GlobSymOffset[20]; ///< Offset to global symbol table. |
| 411 | char |
| 412 | GlobSym64Offset[20]; ///< Offset global symbol table for 64-bit objects. |
| 413 | char FirstChildOffset[20]; ///< Offset to first archive member. |
| 414 | char LastChildOffset[20]; ///< Offset to last archive member. |
| 415 | char FreeOffset[20]; ///< Offset to first mem on free list. |
| 416 | }; |
| 417 | |
| 418 | const FixLenHdr *ArFixLenHdr; |
| 419 | uint64_t FirstChildOffset = 0; |
| 420 | uint64_t LastChildOffset = 0; |
| 421 | std::string MergedGlobalSymtabBuf; |
| 422 | bool Has32BitGlobalSymtab = false; |
| 423 | bool Has64BitGlobalSymtab = false; |
| 424 | |
| 425 | public: |
| 426 | LLVM_ABI BigArchive(MemoryBufferRef Source, Error &Err); |
| 427 | uint64_t getFirstChildOffset() const override { return FirstChildOffset; } |
| 428 | uint64_t getLastChildOffset() const { return LastChildOffset; } |
| 429 | bool isEmpty() const override { return getFirstChildOffset() == 0; } |
| 430 | |
| 431 | bool has32BitGlobalSymtab() { return Has32BitGlobalSymtab; } |
| 432 | bool has64BitGlobalSymtab() { return Has64BitGlobalSymtab; } |
| 433 | }; |
| 434 | |
| 435 | } // end namespace object |
| 436 | } // end namespace llvm |
| 437 | |
| 438 | #endif // LLVM_OBJECT_ARCHIVE_H |
| 439 | |