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