LLVM 17.0.0git
Archive.h
Go to the documentation of this file.
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"
19#include "llvm/Object/Binary.h"
20#include "llvm/Support/Chrono.h"
21#include "llvm/Support/Error.h"
24#include <cassert>
25#include <cstdint>
26#include <memory>
27#include <string>
28#include <vector>
29
30namespace llvm {
31namespace object {
32
33const char ArchiveMagic[] = "!<arch>\n";
34const char ThinArchiveMagic[] = "!<thin>\n";
35const char BigArchiveMagic[] = "<bigaf>\n";
36
37class Archive;
38
40protected:
42
43public:
44 friend class Archive;
45 virtual std::unique_ptr<AbstractArchiveMemberHeader> clone() const = 0;
46 virtual ~AbstractArchiveMemberHeader() = default;
47
48 /// Get the name without looking up long names.
49 virtual Expected<StringRef> getRawName() const = 0;
50 virtual StringRef getRawAccessMode() const = 0;
51 virtual StringRef getRawLastModified() const = 0;
52 virtual StringRef getRawUID() const = 0;
53 virtual StringRef getRawGID() const = 0;
54
55 /// Get the name looking up long names.
57 virtual Expected<uint64_t> getSize() const = 0;
58 virtual uint64_t getOffset() const = 0;
59
60 /// Get next file member location.
62 virtual Expected<bool> isThin() const = 0;
63
68
69 /// Returns the size in bytes of the format-defined member header of the
70 /// concrete archive type.
71 virtual uint64_t getSizeOf() const = 0;
72
74};
75
76template <typename T>
78public:
79 CommonArchiveMemberHeader(const Archive *Parent, const T *RawHeaderPtr)
81 StringRef getRawAccessMode() const override;
82 StringRef getRawLastModified() const override;
83 StringRef getRawUID() const override;
84 StringRef getRawGID() const override;
85
86 uint64_t getOffset() const override;
87 uint64_t getSizeOf() const override { return sizeof(T); }
88
89 T const *ArMemHdr;
90};
91
93 char Name[16];
94 char LastModified[12];
95 char UID[6];
96 char GID[6];
97 char AccessMode[8];
98 char Size[10]; ///< Size of data, not including header or padding.
99 char Terminator[2];
100};
101
102class ArchiveMemberHeader : public CommonArchiveMemberHeader<UnixArMemHdrType> {
103public:
104 ArchiveMemberHeader(const Archive *Parent, const char *RawHeaderPtr,
105 uint64_t Size, Error *Err);
106
107 std::unique_ptr<AbstractArchiveMemberHeader> clone() const override {
108 return std::make_unique<ArchiveMemberHeader>(*this);
109 }
110
111 Expected<StringRef> getRawName() const override;
112
114 Expected<uint64_t> getSize() const override;
116 Expected<bool> isThin() const override;
117};
118
119// File Member Header
121 char Size[20]; // File member size in decimal
122 char NextOffset[20]; // Next member offset in decimal
123 char PrevOffset[20]; // Previous member offset in decimal
124 char LastModified[12];
125 char UID[12];
126 char GID[12];
127 char AccessMode[12];
128 char NameLen[4]; // File member name length in decimal
129 union {
130 char Name[2]; // Start of member name
131 char Terminator[2];
132 };
133};
134
135// Define file member header of AIX big archive.
137 : public CommonArchiveMemberHeader<BigArMemHdrType> {
138
139public:
140 BigArchiveMemberHeader(Archive const *Parent, const char *RawHeaderPtr,
141 uint64_t Size, Error *Err);
142 std::unique_ptr<AbstractArchiveMemberHeader> clone() const override {
143 return std::make_unique<BigArchiveMemberHeader>(*this);
144 }
145
146 Expected<StringRef> getRawName() const override;
148
150 Expected<uint64_t> getSize() const override;
153 Expected<bool> isThin() const override { return false; }
154};
155
156class Archive : public Binary {
157 virtual void anchor();
158
159public:
160 class Child {
161 friend Archive;
163
164 const Archive *Parent;
165 std::unique_ptr<AbstractArchiveMemberHeader> Header;
166 /// Includes header but not padding byte.
167 StringRef Data;
168 /// Offset from Data to the start of the file.
169 uint16_t StartOfFile;
170
171 Expected<bool> isThinMember() const;
172
173 public:
174 Child(const Archive *Parent, const char *Start, Error *Err);
175 Child(const Archive *Parent, StringRef Data, uint16_t StartOfFile);
176
177 Child(const Child &C)
178 : Parent(C.Parent), Data(C.Data), StartOfFile(C.StartOfFile) {
179 if (C.Header)
180 Header = C.Header->clone();
181 }
182
184 Parent = std::move(C.Parent);
185 Header = std::move(C.Header);
186 Data = C.Data;
187 StartOfFile = C.StartOfFile;
188 }
189
190 Child &operator=(Child &&C) noexcept {
191 if (&C == this)
192 return *this;
193
194 Parent = std::move(C.Parent);
195 Header = std::move(C.Header);
196 Data = C.Data;
197 StartOfFile = C.StartOfFile;
198
199 return *this;
200 }
201
203 if (&C == this)
204 return *this;
205
206 Parent = C.Parent;
207 if (C.Header)
208 Header = C.Header->clone();
209 Data = C.Data;
210 StartOfFile = C.StartOfFile;
211
212 return *this;
213 }
214
215 bool operator==(const Child &other) const {
216 assert(!Parent || !other.Parent || Parent == other.Parent);
217 return Data.begin() == other.Data.begin();
218 }
219
220 const Archive *getParent() const { return Parent; }
221 Expected<Child> getNext() const;
222
225 Expected<StringRef> getRawName() const { return Header->getRawName(); }
226
228 return Header->getLastModified();
229 }
230
232 return Header->getRawLastModified();
233 }
234
235 Expected<unsigned> getUID() const { return Header->getUID(); }
236 Expected<unsigned> getGID() const { return Header->getGID(); }
237
239 return Header->getAccessMode();
240 }
241
242 /// \return the size of the archive member without the header or padding.
244 /// \return the size in the archive header for this member.
246
248 uint64_t getChildOffset() const;
249 uint64_t getDataOffset() const { return getChildOffset() + StartOfFile; }
250
252
254 getAsBinary(LLVMContext *Context = nullptr) const;
255 };
256
258 Child C;
259
260 public:
261 ChildFallibleIterator() : C(Child(nullptr, nullptr, nullptr)) {}
262 ChildFallibleIterator(const Child &C) : C(C) {}
263
264 const Child *operator->() const { return &C; }
265 const Child &operator*() const { return C; }
266
267 bool operator==(const ChildFallibleIterator &other) const {
268 // Ignore errors here: If an error occurred during increment then getNext
269 // will have been set to child_end(), and the following comparison should
270 // do the right thing.
271 return C == other.C;
272 }
273
274 bool operator!=(const ChildFallibleIterator &other) const {
275 return !(*this == other);
276 }
277
279 auto NextChild = C.getNext();
280 if (!NextChild)
281 return NextChild.takeError();
282 C = std::move(*NextChild);
283 return Error::success();
284 }
285 };
286
288
289 class Symbol {
290 const Archive *Parent;
291 uint32_t SymbolIndex;
292 uint32_t StringIndex; // Extra index to the string.
293
294 public:
295 Symbol(const Archive *p, uint32_t symi, uint32_t stri)
296 : Parent(p), SymbolIndex(symi), StringIndex(stri) {}
297
298 bool operator==(const Symbol &other) const {
299 return (Parent == other.Parent) && (SymbolIndex == other.SymbolIndex);
300 }
301
302 StringRef getName() const;
304 Symbol getNext() const;
305 };
306
308 Symbol symbol;
309
310 public:
311 symbol_iterator(const Symbol &s) : symbol(s) {}
312
313 const Symbol *operator->() const { return &symbol; }
314 const Symbol &operator*() const { return symbol; }
315
316 bool operator==(const symbol_iterator &other) const {
317 return symbol == other.symbol;
318 }
319
320 bool operator!=(const symbol_iterator &other) const {
321 return !(*this == other);
322 }
323
324 symbol_iterator &operator++() { // Preincrement
325 symbol = symbol.getNext();
326 return *this;
327 }
328 };
329
330 Archive(MemoryBufferRef Source, Error &Err);
332
333 /// Size field is 10 decimal digits long
334 static const uint64_t MaxMemberSize = 9999999999;
335
337
338 Kind kind() const { return (Kind)Format; }
339 bool isThin() const { return IsThin; }
341
342 child_iterator child_begin(Error &Err, bool SkipInternal = true) const;
345 bool SkipInternal = true) const {
346 return make_range(child_begin(Err, SkipInternal), child_end());
347 }
348
353 }
354
355 static bool classof(Binary const *v) { return v->isArchive(); }
356
357 // check if a symbol is in the archive
359
360 virtual bool isEmpty() const;
361 bool hasSymbolTable() const;
365 virtual uint64_t getFirstChildOffset() const { return getArchiveMagicLen(); }
366
367 std::vector<std::unique_ptr<MemoryBuffer>> takeThinBuffers() {
368 return std::move(ThinBuffers);
369 }
370
371 std::unique_ptr<AbstractArchiveMemberHeader>
372 createArchiveMemberHeader(const char *RawHeaderPtr, uint64_t Size,
373 Error *Err) const;
374
375protected:
377 void setFirstRegular(const Child &C);
378
381
382private:
383 StringRef FirstRegularData;
384 uint16_t FirstRegularStartOfFile = -1;
385
386 unsigned Format : 3;
387 unsigned IsThin : 1;
388 mutable std::vector<std::unique_ptr<MemoryBuffer>> ThinBuffers;
389};
390
391class BigArchive : public Archive {
392public:
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
408
409public:
410 BigArchive(MemoryBufferRef Source, Error &Err);
411 uint64_t getFirstChildOffset() const override { return FirstChildOffset; }
413 bool isEmpty() const override {
414 return Data.getBufferSize() == sizeof(FixLenHdr);
415 };
416};
417
418} // end namespace object
419} // end namespace llvm
420
421#endif // LLVM_OBJECT_ARCHIVE_H
uint64_t Size
#define T
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static const char * name
Definition: SMEABIPass.cpp:49
Lightweight error class with error context and mandatory checking.
Definition: Error.h:156
static ErrorSuccess success()
Create a success value.
Definition: Error.h:330
Tagged union holding either a T or a Error.
Definition: Error.h:470
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:67
size_t getBufferSize() const
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
iterator begin() const
Definition: StringRef.h:111
A wrapper class for fallible iterators.
A range adaptor for a pair of iterators.
virtual StringRef getRawGID() const =0
virtual StringRef getRawUID() const =0
Expected< unsigned > getUID() const
Definition: Archive.cpp:396
virtual Expected< uint64_t > getSize() const =0
Expected< sys::fs::perms > getAccessMode() const
Definition: Archive.cpp:377
AbstractArchiveMemberHeader(const Archive *Parent)
Definition: Archive.h:41
Expected< unsigned > getGID() const
Definition: Archive.cpp:403
virtual uint64_t getOffset() const =0
virtual Expected< const char * > getNextChildLoc() const =0
Get next file member location.
virtual Expected< StringRef > getRawName() const =0
Get the name without looking up long names.
virtual Expected< bool > isThin() const =0
Expected< sys::TimePoint< std::chrono::seconds > > getLastModified() const
Definition: Archive.cpp:386
virtual uint64_t getSizeOf() const =0
Returns the size in bytes of the format-defined member header of the concrete archive type.
virtual StringRef getRawAccessMode() const =0
virtual std::unique_ptr< AbstractArchiveMemberHeader > clone() const =0
virtual StringRef getRawLastModified() const =0
virtual Expected< StringRef > getName(uint64_t Size) const =0
Get the name looking up long names.
std::unique_ptr< AbstractArchiveMemberHeader > clone() const override
Definition: Archive.h:107
Expected< StringRef > getName(uint64_t Size) const override
Get the name looking up long names.
Definition: Archive.cpp:237
Expected< StringRef > getRawName() const override
Get the name without looking up long names.
Definition: Archive.cpp:147
Expected< bool > isThin() const override
Definition: Archive.cpp:410
Expected< uint64_t > getSize() const override
Definition: Archive.cpp:349
Expected< const char * > getNextChildLoc() const override
Get next file member location.
Definition: Archive.cpp:418
bool operator==(const ChildFallibleIterator &other) const
Definition: Archive.h:267
bool operator!=(const ChildFallibleIterator &other) const
Definition: Archive.h:274
Expected< StringRef > getBuffer() const
Definition: Archive.cpp:564
StringRef getRawLastModified() const
Definition: Archive.h:231
Expected< Child > getNext() const
Definition: Archive.cpp:586
bool operator==(const Child &other) const
Definition: Archive.h:215
Expected< std::string > getFullName() const
Definition: Archive.cpp:546
uint64_t getChildOffset() const
Definition: Archive.cpp:617
Expected< uint64_t > getRawSize() const
Definition: Archive.cpp:540
Expected< StringRef > getName() const
Definition: Archive.cpp:624
Expected< uint64_t > getSize() const
Definition: Archive.cpp:534
Child & operator=(const Child &C)
Definition: Archive.h:202
Expected< unsigned > getGID() const
Definition: Archive.h:236
uint64_t getDataOffset() const
Definition: Archive.h:249
Expected< StringRef > getRawName() const
Definition: Archive.h:225
Expected< std::unique_ptr< Binary > > getAsBinary(LLVMContext *Context=nullptr) const
Definition: Archive.cpp:649
Expected< MemoryBufferRef > getMemoryBufferRef() const
Definition: Archive.cpp:637
Expected< unsigned > getUID() const
Definition: Archive.h:235
Child & operator=(Child &&C) noexcept
Definition: Archive.h:190
Expected< sys::fs::perms > getAccessMode() const
Definition: Archive.h:238
Expected< sys::TimePoint< std::chrono::seconds > > getLastModified() const
Definition: Archive.h:227
const Archive * getParent() const
Definition: Archive.h:220
Child(const Child &C)
Definition: Archive.h:177
Symbol(const Archive *p, uint32_t symi, uint32_t stri)
Definition: Archive.h:295
Expected< Child > getMember() const
Definition: Archive.cpp:967
bool operator==(const Symbol &other) const
Definition: Archive.h:298
StringRef getName() const
Definition: Archive.cpp:963
symbol_iterator & operator++()
Definition: Archive.h:324
const Symbol & operator*() const
Definition: Archive.h:314
bool operator!=(const symbol_iterator &other) const
Definition: Archive.h:320
const Symbol * operator->() const
Definition: Archive.h:313
bool operator==(const symbol_iterator &other) const
Definition: Archive.h:316
std::unique_ptr< AbstractArchiveMemberHeader > createArchiveMemberHeader(const char *RawHeaderPtr, uint64_t Size, Error *Err) const
Definition: Archive.cpp:676
symbol_iterator symbol_begin() const
Definition: Archive.cpp:1067
iterator_range< symbol_iterator > symbols() const
Definition: Archive.h:351
virtual uint64_t getFirstChildOffset() const
Definition: Archive.h:365
bool isThin() const
Definition: Archive.h:339
StringRef getStringTable() const
Definition: Archive.h:363
uint32_t getNumberOfSymbols() const
Definition: Archive.cpp:1129
fallible_iterator< ChildFallibleIterator > child_iterator
Definition: Archive.h:287
void setFirstRegular(const Child &C)
Definition: Archive.cpp:695
uint64_t getArchiveMagicLen() const
Definition: Archive.cpp:685
StringRef getSymbolTable() const
Definition: Archive.h:362
symbol_iterator symbol_end() const
Definition: Archive.cpp:1125
iterator_range< child_iterator > children(Error &Err, bool SkipInternal=true) const
Definition: Archive.h:344
virtual bool isEmpty() const
Definition: Archive.cpp:1164
child_iterator child_end() const
Definition: Archive.cpp:959
bool hasSymbolTable() const
Definition: Archive.cpp:1168
static bool classof(Binary const *v)
Definition: Archive.h:355
StringRef StringTable
Definition: Archive.h:380
Kind kind() const
Definition: Archive.h:338
StringRef SymbolTable
Definition: Archive.h:379
Expected< std::optional< Child > > findSym(StringRef name) const
Definition: Archive.cpp:1147
child_iterator child_begin(Error &Err, bool SkipInternal=true) const
Definition: Archive.cpp:943
static const uint64_t MaxMemberSize
Size field is 10 decimal digits long.
Definition: Archive.h:334
std::vector< std::unique_ptr< MemoryBuffer > > takeThinBuffers()
Definition: Archive.h:367
static object::Archive::Kind getDefaultKindForHost()
Definition: Archive.cpp:935
static Expected< std::unique_ptr< Archive > > create(MemoryBufferRef Source)
Definition: Archive.cpp:660
Expected< uint64_t > getSize() const override
Definition: Archive.cpp:354
Expected< StringRef > getRawName() const override
Get the name without looking up long names.
Definition: Archive.cpp:208
Expected< bool > isThin() const override
Definition: Archive.h:153
std::unique_ptr< AbstractArchiveMemberHeader > clone() const override
Definition: Archive.h:142
Expected< const char * > getNextChildLoc() const override
Get next file member location.
Definition: Archive.cpp:443
Expected< StringRef > getName(uint64_t Size) const override
Get the name looking up long names.
Definition: Archive.cpp:345
Expected< uint64_t > getRawNameSize() const
Definition: Archive.cpp:367
Expected< uint64_t > getNextOffset() const
Definition: Archive.cpp:372
bool isEmpty() const override
Definition: Archive.h:413
uint64_t getLastChildOffset() const
Definition: Archive.h:412
const FixLenHdr * ArFixLenHdr
Definition: Archive.h:405
uint64_t FirstChildOffset
Definition: Archive.h:406
uint64_t LastChildOffset
Definition: Archive.h:407
uint64_t getFirstChildOffset() const override
Definition: Archive.h:411
MemoryBufferRef Data
Definition: Binary.h:37
bool isArchive() const
Definition: Binary.h:114
StringRef getRawLastModified() const override
Definition: Archive.cpp:75
StringRef getRawUID() const override
Definition: Archive.cpp:79
CommonArchiveMemberHeader(const Archive *Parent, const T *RawHeaderPtr)
Definition: Archive.h:79
uint64_t getSizeOf() const override
Returns the size in bytes of the format-defined member header of the concrete archive type.
Definition: Archive.h:87
uint64_t getOffset() const override
Definition: Archive.cpp:87
StringRef getRawAccessMode() const override
Definition: Archive.cpp:70
StringRef getRawGID() const override
Definition: Archive.cpp:83
This provides a very simple, boring adaptor for a begin and end iterator into a range type.
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
const char ArchiveMagic[]
Definition: Archive.h:33
const char ThinArchiveMagic[]
Definition: Archive.h:34
const char BigArchiveMagic[]
Definition: Archive.h:35
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
Fixed-Length Header.
Definition: Archive.h:394
char Magic[sizeof(BigArchiveMagic) - 1]
Big archive magic string.
Definition: Archive.h:395
char FirstChildOffset[20]
Offset to first archive member.
Definition: Archive.h:400
char LastChildOffset[20]
Offset to last archive member.
Definition: Archive.h:401
char FreeOffset[20]
Offset to first mem on free list.
Definition: Archive.h:402
char GlobSym64Offset[20]
Offset global symbol table for 64-bit objects.
Definition: Archive.h:399
char MemOffset[20]
Offset to member table.
Definition: Archive.h:396
char GlobSymOffset[20]
Offset to global symbol table.
Definition: Archive.h:397
char Size[10]
Size of data, not including header or padding.
Definition: Archive.h:98