Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/include/llvm/Object/Archive.h
Line
Count
Source (jump to first uncovered line)
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
class Archive;
36
37
class ArchiveMemberHeader {
38
public:
39
  friend class Archive;
40
41
  ArchiveMemberHeader(Archive const *Parent, const char *RawHeaderPtr,
42
                      uint64_t Size, Error *Err);
43
  // ArchiveMemberHeader() = default;
44
45
  /// Get the name without looking up long names.
46
  Expected<StringRef> getRawName() const;
47
48
  /// Get the name looking up long names.
49
  Expected<StringRef> getName(uint64_t Size) const;
50
51
  Expected<uint64_t> getSize() const;
52
53
  Expected<sys::fs::perms> getAccessMode() const;
54
  Expected<sys::TimePoint<std::chrono::seconds>> getLastModified() const;
55
56
  StringRef getRawLastModified() const {
57
    return StringRef(ArMemHdr->LastModified,
58
                     sizeof(ArMemHdr->LastModified)).rtrim(' ');
59
  }
60
61
  Expected<unsigned> getUID() const;
62
  Expected<unsigned> getGID() const;
63
64
  // This returns the size of the private struct ArMemHdrType
65
11.8k
  uint64_t getSizeOf() const {
66
11.8k
    return sizeof(ArMemHdrType);
67
11.8k
  }
68
69
private:
70
  struct ArMemHdrType {
71
    char Name[16];
72
    char LastModified[12];
73
    char UID[6];
74
    char GID[6];
75
    char AccessMode[8];
76
    char Size[10]; ///< Size of data, not including header or padding.
77
    char Terminator[2];
78
  };
79
  Archive const *Parent;
80
  ArMemHdrType const *ArMemHdr;
81
};
82
83
class Archive : public Binary {
84
  virtual void anchor();
85
86
public:
87
  class Child {
88
    friend Archive;
89
    friend ArchiveMemberHeader;
90
91
    const Archive *Parent;
92
    ArchiveMemberHeader Header;
93
    /// Includes header but not padding byte.
94
    StringRef Data;
95
    /// Offset from Data to the start of the file.
96
    uint16_t StartOfFile;
97
98
    Expected<bool> isThinMember() const;
99
100
  public:
101
    Child(const Archive *Parent, const char *Start, Error *Err);
102
    Child(const Archive *Parent, StringRef Data, uint16_t StartOfFile);
103
104
3.64k
    bool operator ==(const Child &other) const {
105
3.64k
      assert(!Parent || !other.Parent || Parent == other.Parent);
106
3.64k
      return Data.begin() == other.Data.begin();
107
3.64k
    }
108
109
    const Archive *getParent() const { return Parent; }
110
    Expected<Child> getNext() const;
111
112
    Expected<StringRef> getName() const;
113
    Expected<std::string> getFullName() const;
114
5.00k
    Expected<StringRef> getRawName() const { return Header.getRawName(); }
115
116
    Expected<sys::TimePoint<std::chrono::seconds>> getLastModified() const {
117
      return Header.getLastModified();
118
    }
119
120
    StringRef getRawLastModified() const {
121
      return Header.getRawLastModified();
122
    }
123
124
    Expected<unsigned> getUID() const { return Header.getUID(); }
125
    Expected<unsigned> getGID() const { return Header.getGID(); }
126
127
    Expected<sys::fs::perms> getAccessMode() const {
128
      return Header.getAccessMode();
129
    }
130
131
    /// \return the size of the archive member without the header or padding.
132
    Expected<uint64_t> getSize() const;
133
    /// \return the size in the archive header for this member.
134
    Expected<uint64_t> getRawSize() const;
135
136
    Expected<StringRef> getBuffer() const;
137
    uint64_t getChildOffset() const;
138
139
    Expected<MemoryBufferRef> getMemoryBufferRef() const;
140
141
    Expected<std::unique_ptr<Binary>>
142
    getAsBinary(LLVMContext *Context = nullptr) const;
143
  };
144
145
  class ChildFallibleIterator {
146
    Child C;
147
148
  public:
149
0
    ChildFallibleIterator() : C(Child(nullptr, nullptr, nullptr)) {}
150
3.05k
    ChildFallibleIterator(const Child &C) : C(C) {}
151
152
0
    const Child *operator->() const { return &C; }
153
3.67k
    const Child &operator*() const { return C; }
154
155
3.64k
    bool operator==(const ChildFallibleIterator &other) const {
156
3.64k
      // Ignore errors here: If an error occurred during increment then getNext
157
3.64k
      // will have been set to child_end(), and the following comparison should
158
3.64k
      // do the right thing.
159
3.64k
      return C == other.C;
160
3.64k
    }
161
162
0
    bool operator!=(const ChildFallibleIterator &other) const {
163
0
      return !(*this == other);
164
0
    }
165
166
2.76k
    Error inc() {
167
2.76k
      auto NextChild = C.getNext();
168
2.76k
      if (!NextChild)
169
4
        return NextChild.takeError();
170
2.76k
      C = std::move(*NextChild);
171
2.76k
      return Error::success();
172
2.76k
    }
173
  };
174
175
  using child_iterator = fallible_iterator<ChildFallibleIterator>;
176
177
  class Symbol {
178
    const Archive *Parent;
179
    uint32_t SymbolIndex;
180
    uint32_t StringIndex; // Extra index to the string.
181
182
  public:
183
    Symbol(const Archive *p, uint32_t symi, uint32_t stri)
184
      : Parent(p)
185
      , SymbolIndex(symi)
186
616
      , StringIndex(stri) {}
187
188
1.34k
    bool operator ==(const Symbol &other) const {
189
1.34k
      return (Parent == other.Parent) && (SymbolIndex == other.SymbolIndex);
190
1.34k
    }
191
192
    StringRef getName() const;
193
    Expected<Child> getMember() const;
194
    Symbol getNext() const;
195
  };
196
197
  class symbol_iterator {
198
    Symbol symbol;
199
200
  public:
201
616
    symbol_iterator(const Symbol &s) : symbol(s) {}
202
203
390
    const Symbol *operator->() const { return &symbol; }
204
    const Symbol &operator*() const { return symbol; }
205
206
1.34k
    bool operator==(const symbol_iterator &other) const {
207
1.34k
      return symbol == other.symbol;
208
1.34k
    }
209
210
1.34k
    bool operator!=(const symbol_iterator &other) const {
211
1.34k
      return !(*this == other);
212
1.34k
    }
213
214
982
    symbol_iterator& operator++() {  // Preincrement
215
982
      symbol = symbol.getNext();
216
982
      return *this;
217
982
    }
218
  };
219
220
  Archive(MemoryBufferRef Source, Error &Err);
221
  static Expected<std::unique_ptr<Archive>> create(MemoryBufferRef Source);
222
223
  /// Size field is 10 decimal digits long
224
  static const uint64_t MaxMemberSize = 9999999999;
225
226
  enum Kind {
227
    K_GNU,
228
    K_GNU64,
229
    K_BSD,
230
    K_DARWIN,
231
    K_DARWIN64,
232
    K_COFF
233
  };
234
235
19.1k
  Kind kind() const { return (Kind)Format; }
236
1.11k
  bool isThin() const { return IsThin; }
237
238
  child_iterator child_begin(Error &Err, bool SkipInternal = true) const;
239
  child_iterator child_end() const;
240
  iterator_range<child_iterator> children(Error &Err,
241
637
                                          bool SkipInternal = true) const {
242
637
    return make_range(child_begin(Err, SkipInternal), child_end());
243
637
  }
244
245
  symbol_iterator symbol_begin() const;
246
  symbol_iterator symbol_end() const;
247
  iterator_range<symbol_iterator> symbols() const {
248
    return make_range(symbol_begin(), symbol_end());
249
  }
250
251
  // Cast methods.
252
8.49k
  static bool classof(Binary const *v) {
253
8.49k
    return v->isArchive();
254
8.49k
  }
255
256
  // check if a symbol is in the archive
257
  Expected<Optional<Child>> findSym(StringRef name) const;
258
259
  bool isEmpty() const;
260
  bool hasSymbolTable() const;
261
3.87k
  StringRef getSymbolTable() const { return SymbolTable; }
262
4.16k
  StringRef getStringTable() const { return StringTable; }
263
  uint32_t getNumberOfSymbols() const;
264
265
34
  std::vector<std::unique_ptr<MemoryBuffer>> takeThinBuffers() {
266
34
    return std::move(ThinBuffers);
267
34
  }
268
269
private:
270
  StringRef SymbolTable;
271
  StringRef StringTable;
272
273
  StringRef FirstRegularData;
274
  uint16_t FirstRegularStartOfFile = -1;
275
  void setFirstRegular(const Child &C);
276
277
  unsigned Format : 3;
278
  unsigned IsThin : 1;
279
  mutable std::vector<std::unique_ptr<MemoryBuffer>> ThinBuffers;
280
};
281
282
} // end namespace object
283
} // end namespace llvm
284
285
#endif // LLVM_OBJECT_ARCHIVE_H