LLVM  3.7.0
Archive.h
Go to the documentation of this file.
1 //===- Archive.h - ar archive file format -----------------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file declares the ar archive file format class.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_OBJECT_ARCHIVE_H
15 #define LLVM_OBJECT_ARCHIVE_H
16 
17 #include "llvm/ADT/StringRef.h"
19 #include "llvm/Object/Binary.h"
21 #include "llvm/Support/ErrorOr.h"
24 
25 namespace llvm {
26 namespace object {
28  char Name[16];
29  char LastModified[12];
30  char UID[6];
31  char GID[6];
32  char AccessMode[8];
33  char Size[10]; ///< Size of data, not including header or padding.
34  char Terminator[2];
35 
36  /// Get the name without looking up long names.
37  llvm::StringRef getName() const;
38 
39  /// Members are not larger than 4GB.
40  uint32_t getSize() const;
41 
45  return StringRef(LastModified, sizeof(LastModified)).rtrim(" ");
46  }
47  unsigned getUID() const;
48  unsigned getGID() const;
49 };
50 
51 class Archive : public Binary {
52  virtual void anchor();
53 public:
54  class Child {
55  const Archive *Parent;
56  /// \brief Includes header but not padding byte.
57  StringRef Data;
58  /// \brief Offset from Data to the start of the file.
59  uint16_t StartOfFile;
60 
61  const ArchiveMemberHeader *getHeader() const {
62  return reinterpret_cast<const ArchiveMemberHeader *>(Data.data());
63  }
64 
65  public:
66  Child(const Archive *Parent, const char *Start);
67 
68  bool operator ==(const Child &other) const {
69  assert(Parent == other.Parent);
70  return Data.begin() == other.Data.begin();
71  }
72 
73  bool operator <(const Child &other) const {
74  return Data.begin() < other.Data.begin();
75  }
76 
77  Child getNext() const;
78 
80  StringRef getRawName() const { return getHeader()->getName(); }
82  return getHeader()->getLastModified();
83  }
85  return getHeader()->getRawLastModified();
86  }
87  unsigned getUID() const { return getHeader()->getUID(); }
88  unsigned getGID() const { return getHeader()->getGID(); }
90  return getHeader()->getAccessMode();
91  }
92  /// \return the size of the archive member without the header or padding.
93  uint64_t getSize() const;
94  /// \return the size in the archive header for this member.
95  uint64_t getRawSize() const;
96 
98  uint64_t getChildOffset() const;
99 
101 
103  getAsBinary(LLVMContext *Context = nullptr) const;
104  };
105 
107  Child child;
108 
109  public:
110  child_iterator() : child(Child(nullptr, nullptr)) {}
111  child_iterator(const Child &c) : child(c) {}
112  const Child *operator->() const { return &child; }
113  const Child &operator*() const { return child; }
114 
115  bool operator==(const child_iterator &other) const {
116  return child == other.child;
117  }
118 
119  bool operator!=(const child_iterator &other) const {
120  return !(*this == other);
121  }
122 
123  bool operator<(const child_iterator &other) const {
124  return child < other.child;
125  }
126 
127  child_iterator &operator++() { // Preincrement
128  child = child.getNext();
129  return *this;
130  }
131  };
132 
133  class Symbol {
134  const Archive *Parent;
135  uint32_t SymbolIndex;
136  uint32_t StringIndex; // Extra index to the string.
137 
138  public:
139  bool operator ==(const Symbol &other) const {
140  return (Parent == other.Parent) && (SymbolIndex == other.SymbolIndex);
141  }
142 
143  Symbol(const Archive *p, uint32_t symi, uint32_t stri)
144  : Parent(p)
145  , SymbolIndex(symi)
146  , StringIndex(stri) {}
147  StringRef getName() const;
149  Symbol getNext() const;
150  };
151 
153  Symbol symbol;
154  public:
155  symbol_iterator(const Symbol &s) : symbol(s) {}
156  const Symbol *operator->() const { return &symbol; }
157  const Symbol &operator*() const { return symbol; }
158 
159  bool operator==(const symbol_iterator &other) const {
160  return symbol == other.symbol;
161  }
162 
163  bool operator!=(const symbol_iterator &other) const {
164  return !(*this == other);
165  }
166 
167  symbol_iterator& operator++() { // Preincrement
168  symbol = symbol.getNext();
169  return *this;
170  }
171  };
172 
173  Archive(MemoryBufferRef Source, std::error_code &EC);
175 
176  enum Kind {
181  };
182 
183  Kind kind() const { return (Kind)Format; }
184  bool isThin() const { return IsThin; }
185 
186  child_iterator child_begin(bool SkipInternal = true) const;
187  child_iterator child_end() const;
188  iterator_range<child_iterator> children(bool SkipInternal = true) const {
189  return iterator_range<child_iterator>(child_begin(SkipInternal),
190  child_end());
191  }
192 
194  symbol_iterator symbol_end() const;
197  }
198 
199  // Cast methods.
200  static inline bool classof(Binary const *v) {
201  return v->isArchive();
202  }
203 
204  // check if a symbol is in the archive
205  child_iterator findSym(StringRef name) const;
206 
207  bool hasSymbolTable() const;
208  child_iterator getSymbolTableChild() const { return SymbolTable; }
210  // We know that the symbol table is not an external file,
211  // so we just assert there is no error.
212  return *SymbolTable->getBuffer();
213  }
214  uint32_t getNumberOfSymbols() const;
215 
216 private:
217  child_iterator SymbolTable;
218  child_iterator StringTable;
219  child_iterator FirstRegular;
220  unsigned Format : 2;
221  unsigned IsThin : 1;
222  mutable std::vector<std::unique_ptr<MemoryBuffer>> ThinBuffers;
223 };
224 
225 }
226 }
227 
228 #endif
Represents either an error or a value T.
Definition: ErrorOr.h:82
Archive(MemoryBufferRef Source, std::error_code &EC)
Definition: Archive.cpp:230
bool operator<(const child_iterator &other) const
Definition: Archive.h:123
symbol_iterator symbol_end() const
Definition: Archive.cpp:508
ErrorOr< std::unique_ptr< Binary > > getAsBinary(LLVMContext *Context=nullptr) const
Definition: Archive.cpp:214
bool operator==(const symbol_iterator &other) const
Definition: Archive.h:159
Kind kind() const
Definition: Archive.h:183
This provides a very simple, boring adaptor for a begin and end iterator into a range type...
uint64_t getRawSize() const
Definition: Archive.cpp:115
uint64_t getChildOffset() const
Definition: Archive.cpp:151
child_iterator child_begin(bool SkipInternal=true) const
Definition: Archive.cpp:360
sys::fs::perms getAccessMode() const
Definition: Archive.h:89
uint32_t getSize() const
Members are not larger than 4GB.
Definition: Archive.cpp:46
const Symbol * operator->() const
Definition: Archive.h:156
StringRef getSymbolTable() const
Definition: Archive.h:209
sys::TimeValue getLastModified() const
Definition: Archive.h:81
bool operator==(const child_iterator &other) const
Definition: Archive.h:115
StringRef rtrim(StringRef Chars=" \t\n\v\f\r") const
Return string with consecutive characters in Chars starting from the right removed.
Definition: StringRef.h:517
const Symbol & operator*() const
Definition: Archive.h:157
iterator_range< symbol_iterator > symbols() const
Definition: Archive.h:195
ErrorOr< MemoryBufferRef > getMemoryBufferRef() const
Definition: Archive.cpp:202
bool operator!=(const symbol_iterator &other) const
Definition: Archive.h:163
char Size[10]
Size of data, not including header or padding.
Definition: Archive.h:33
const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition: StringRef.h:107
Symbol(const Archive *p, uint32_t symi, uint32_t stri)
Definition: Archive.h:143
unsigned getGID() const
Definition: Archive.h:88
ErrorOr< child_iterator > getMember() const
Definition: Archive.cpp:380
iterator begin() const
Definition: StringRef.h:90
bool hasSymbolTable() const
Definition: Archive.cpp:545
child_iterator getSymbolTableChild() const
Definition: Archive.h:208
llvm::StringRef getRawLastModified() const
Definition: Archive.h:44
bool operator==(const Child &other) const
Definition: Archive.h:68
child_iterator child_end() const
Definition: Archive.cpp:372
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:41
StringRef getName() const
Definition: Archive.cpp:376
bool operator==(const Symbol &other) const
Definition: Archive.h:139
Child(const Archive *Parent, const char *Start)
Definition: Archive.cpp:85
bool isThin() const
Definition: Archive.h:184
sys::fs::perms getAccessMode() const
Definition: Archive.cpp:53
const Child * operator->() const
Definition: Archive.h:112
unsigned getUID() const
Definition: Archive.h:87
sys::TimeValue getLastModified() const
Definition: Archive.cpp:60
const Child & operator*() const
Definition: Archive.h:113
child_iterator findSym(StringRef name) const
Definition: Archive.cpp:528
symbol_iterator symbol_begin() const
Definition: Archive.cpp:468
A range adaptor for a pair of iterators.
static ErrorOr< std::unique_ptr< Archive > > create(MemoryBufferRef Source)
Definition: Archive.cpp:222
StringRef getRawName() const
Definition: Archive.h:80
uint64_t getSize() const
Definition: Archive.cpp:109
ErrorOr< StringRef > getName() const
Definition: Archive.cpp:158
bool operator<(const Child &other) const
Definition: Archive.h:73
llvm::StringRef getName() const
Get the name without looking up long names.
Definition: Archive.cpp:31
StringRef getRawLastModified() const
Definition: Archive.h:84
uint32_t getNumberOfSymbols() const
Definition: Archive.cpp:514
Provides ErrorOr<T> smart pointer.
static const char * name
This class is used where a precise fixed point in time is required.
Definition: TimeValue.h:31
static bool classof(Binary const *v)
Definition: Archive.h:200
ErrorOr< StringRef > getBuffer() const
Definition: Archive.cpp:119
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:40
iterator_range< child_iterator > children(bool SkipInternal=true) const
Definition: Archive.h:188
bool isArchive() const
Definition: Binary.h:96
bool operator!=(const child_iterator &other) const
Definition: Archive.h:119
symbol_iterator & operator++()
Definition: Archive.h:167