Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/Object/Archive.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- Archive.cpp - ar File Format implementation ------------------------===//
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 defines the ArchiveObjectFile class.
10
//
11
//===----------------------------------------------------------------------===//
12
13
#include "llvm/Object/Archive.h"
14
#include "llvm/ADT/Optional.h"
15
#include "llvm/ADT/SmallString.h"
16
#include "llvm/ADT/StringRef.h"
17
#include "llvm/ADT/Twine.h"
18
#include "llvm/Object/Binary.h"
19
#include "llvm/Object/Error.h"
20
#include "llvm/Support/Chrono.h"
21
#include "llvm/Support/Endian.h"
22
#include "llvm/Support/Error.h"
23
#include "llvm/Support/ErrorOr.h"
24
#include "llvm/Support/FileSystem.h"
25
#include "llvm/Support/MemoryBuffer.h"
26
#include "llvm/Support/Path.h"
27
#include "llvm/Support/raw_ostream.h"
28
#include <algorithm>
29
#include <cassert>
30
#include <cstddef>
31
#include <cstdint>
32
#include <cstring>
33
#include <memory>
34
#include <string>
35
#include <system_error>
36
37
using namespace llvm;
38
using namespace object;
39
using namespace llvm::support::endian;
40
41
static const char *const Magic = "!<arch>\n";
42
static const char *const ThinMagic = "!<thin>\n";
43
44
0
void Archive::anchor() {}
45
46
static Error
47
18
malformedError(Twine Msg) {
48
18
  std::string StringMsg = "truncated or malformed archive (" + Msg.str() + ")";
49
18
  return make_error<GenericBinaryError>(std::move(StringMsg),
50
18
                                        object_error::parse_failed);
51
18
}
52
53
ArchiveMemberHeader::ArchiveMemberHeader(const Archive *Parent,
54
                                         const char *RawHeaderPtr,
55
                                         uint64_t Size, Error *Err)
56
    : Parent(Parent),
57
6.30k
      ArMemHdr(reinterpret_cast<const ArMemHdrType *>(RawHeaderPtr)) {
58
6.30k
  if (RawHeaderPtr == nullptr)
59
2.15k
    return;
60
4.15k
  ErrorAsOutParameter ErrAsOutParam(Err);
61
4.15k
62
4.15k
  if (Size < sizeof(ArMemHdrType)) {
63
1
    if (Err) {
64
1
      std::string Msg("remaining size of archive too small for next archive "
65
1
                      "member header ");
66
1
      Expected<StringRef> NameOrErr = getName(Size);
67
1
      if (!NameOrErr) {
68
0
        consumeError(NameOrErr.takeError());
69
0
        uint64_t Offset = RawHeaderPtr - Parent->getData().data();
70
0
        *Err = malformedError(Msg + "at offset " + Twine(Offset));
71
0
      } else
72
1
        *Err = malformedError(Msg + "for " + NameOrErr.get());
73
1
    }
74
1
    return;
75
1
  }
76
4.15k
  if (ArMemHdr->Terminator[0] != '`' || 
ArMemHdr->Terminator[1] != '\n'4.15k
) {
77
1
    if (Err) {
78
1
      std::string Buf;
79
1
      raw_string_ostream OS(Buf);
80
1
      OS.write_escaped(StringRef(ArMemHdr->Terminator,
81
1
                                 sizeof(ArMemHdr->Terminator)));
82
1
      OS.flush();
83
1
      std::string Msg("terminator characters in archive member \"" + Buf +
84
1
                      "\" not the correct \"`\\n\" values for the archive "
85
1
                      "member header ");
86
1
      Expected<StringRef> NameOrErr = getName(Size);
87
1
      if (!NameOrErr) {
88
0
        consumeError(NameOrErr.takeError());
89
0
        uint64_t Offset = RawHeaderPtr - Parent->getData().data();
90
0
        *Err = malformedError(Msg + "at offset " + Twine(Offset));
91
0
      } else
92
1
        *Err = malformedError(Msg + "for " + NameOrErr.get());
93
1
    }
94
1
    return;
95
1
  }
96
4.15k
}
97
98
// This gets the raw name from the ArMemHdr->Name field and checks that it is
99
// valid for the kind of archive.  If it is not valid it returns an Error.
100
13.8k
Expected<StringRef> ArchiveMemberHeader::getRawName() const {
101
13.8k
  char EndCond;
102
13.8k
  auto Kind = Parent->kind();
103
13.8k
  if (Kind == Archive::K_BSD || 
Kind == Archive::K_DARWIN6411.3k
) {
104
2.57k
    if (ArMemHdr->Name[0] == ' ') {
105
1
      uint64_t Offset = reinterpret_cast<const char *>(ArMemHdr) -
106
1
                        Parent->getData().data();
107
1
      return malformedError("name contains a leading space for archive member "
108
1
                            "header at offset " + Twine(Offset));
109
1
    }
110
2.57k
    EndCond = ' ';
111
2.57k
  }
112
11.3k
  else if (ArMemHdr->Name[0] == '/' || 
ArMemHdr->Name[0] == '#'3.57k
)
113
8.59k
    EndCond = ' ';
114
2.71k
  else
115
2.71k
    EndCond = '/';
116
13.8k
  StringRef::size_type end =
117
13.8k
      StringRef(ArMemHdr->Name, sizeof(ArMemHdr->Name)).find(EndCond);
118
13.8k
  if (end == StringRef::npos)
119
107
    end = sizeof(ArMemHdr->Name);
120
13.8k
  assert(end <= sizeof(ArMemHdr->Name) && end > 0);
121
13.8k
  // Don't include the EndCond if there is one.
122
13.8k
  return StringRef(ArMemHdr->Name, end);
123
13.8k
}
124
125
// This gets the name looking up long names. Size is the size of the archive
126
// member including the header, so the size of any name following the header
127
// is checked to make sure it does not overflow.
128
2.80k
Expected<StringRef> ArchiveMemberHeader::getName(uint64_t Size) const {
129
2.80k
130
2.80k
  // This can be called from the ArchiveMemberHeader constructor when the
131
2.80k
  // archive header is truncated to produce an error message with the name.
132
2.80k
  // Make sure the name field is not truncated.
133
2.80k
  if (Size < offsetof(ArMemHdrType, Name) + sizeof(ArMemHdr->Name)) {
134
0
    uint64_t ArchiveOffset = reinterpret_cast<const char *>(ArMemHdr) -
135
0
                      Parent->getData().data();
136
0
    return malformedError("archive header truncated before the name field "
137
0
                          "for archive member header at offset " +
138
0
                          Twine(ArchiveOffset));
139
0
  }
140
2.80k
141
2.80k
  // The raw name itself can be invalid.
142
2.80k
  Expected<StringRef> NameOrErr = getRawName();
143
2.80k
  if (!NameOrErr)
144
0
    return NameOrErr.takeError();
145
2.80k
  StringRef Name = NameOrErr.get();
146
2.80k
147
2.80k
  // Check if it's a special name.
148
2.80k
  if (Name[0] == '/') {
149
1.07k
    if (Name.size() == 1) // Linker member.
150
0
      return Name;
151
1.07k
    if (Name.size() == 2 && 
Name[1] == '/'644
) // String table.
152
0
      return Name;
153
1.07k
    // It's a long name.
154
1.07k
    // Get the string table offset.
155
1.07k
    std::size_t StringOffset;
156
1.07k
    if (Name.substr(1).rtrim(' ').getAsInteger(10, StringOffset)) {
157
2
      std::string Buf;
158
2
      raw_string_ostream OS(Buf);
159
2
      OS.write_escaped(Name.substr(1).rtrim(' '));
160
2
      OS.flush();
161
2
      uint64_t ArchiveOffset = reinterpret_cast<const char *>(ArMemHdr) -
162
2
                               Parent->getData().data();
163
2
      return malformedError("long name offset characters after the '/' are "
164
2
                            "not all decimal numbers: '" + Buf + "' for "
165
2
                            "archive member header at offset " +
166
2
                            Twine(ArchiveOffset));
167
2
    }
168
1.07k
169
1.07k
    // Verify it.
170
1.07k
    if (StringOffset >= Parent->getStringTable().size()) {
171
2
      uint64_t ArchiveOffset = reinterpret_cast<const char *>(ArMemHdr) -
172
2
                               Parent->getData().data();
173
2
      return malformedError("long name offset " + Twine(StringOffset) + " past "
174
2
                            "the end of the string table for archive member "
175
2
                            "header at offset " + Twine(ArchiveOffset));
176
2
    }
177
1.07k
178
1.07k
    // GNU long file names end with a "/\n".
179
1.07k
    if (Parent->kind() == Archive::K_GNU ||
180
1.07k
        
Parent->kind() == Archive::K_GNU6474
) {
181
1.00k
      size_t End = Parent->getStringTable().find('\n', /*From=*/StringOffset);
182
1.00k
      if (End == StringRef::npos || End < 1 ||
183
1.00k
          Parent->getStringTable()[End - 1] != '/') {
184
0
        return malformedError("string table at long name offset " +
185
0
                              Twine(StringOffset) + "not terminated");
186
0
      }
187
1.00k
      return Parent->getStringTable().slice(StringOffset, End - 1);
188
1.00k
    }
189
69
    return Parent->getStringTable().begin() + StringOffset;
190
69
  }
191
1.72k
192
1.72k
  if (Name.startswith("#1/")) {
193
1.01k
    uint64_t NameLength;
194
1.01k
    if (Name.substr(3).rtrim(' ').getAsInteger(10, NameLength)) {
195
0
      std::string Buf;
196
0
      raw_string_ostream OS(Buf);
197
0
      OS.write_escaped(Name.substr(3).rtrim(' '));
198
0
      OS.flush();
199
0
      uint64_t ArchiveOffset = reinterpret_cast<const char *>(ArMemHdr) -
200
0
                        Parent->getData().data();
201
0
      return malformedError("long name length characters after the #1/ are "
202
0
                            "not all decimal numbers: '" + Buf + "' for "
203
0
                            "archive member header at offset " +
204
0
                            Twine(ArchiveOffset));
205
0
    }
206
1.01k
    if (getSizeOf() + NameLength > Size) {
207
3
      uint64_t ArchiveOffset = reinterpret_cast<const char *>(ArMemHdr) -
208
3
                        Parent->getData().data();
209
3
      return malformedError("long name length: " + Twine(NameLength) +
210
3
                            " extends past the end of the member or archive "
211
3
                            "for archive member header at offset " +
212
3
                            Twine(ArchiveOffset));
213
3
    }
214
1.00k
    return StringRef(reinterpret_cast<const char *>(ArMemHdr) + getSizeOf(),
215
1.00k
                     NameLength).rtrim('\0');
216
1.00k
  }
217
717
218
717
  // It is not a long name so trim the blanks at the end of the name.
219
717
  if (Name[Name.size() - 1] != '/')
220
717
    return Name.rtrim(' ');
221
0
222
0
  // It's a simple name.
223
0
  return Name.drop_back(1);
224
0
}
225
226
6.34k
Expected<uint64_t> ArchiveMemberHeader::getSize() const {
227
6.34k
  uint64_t Ret;
228
6.34k
  if (StringRef(ArMemHdr->Size,
229
6.34k
                sizeof(ArMemHdr->Size)).rtrim(" ").getAsInteger(10, Ret)) {
230
2
    std::string Buf;
231
2
    raw_string_ostream OS(Buf);
232
2
    OS.write_escaped(StringRef(ArMemHdr->Size,
233
2
                               sizeof(ArMemHdr->Size)).rtrim(" "));
234
2
    OS.flush();
235
2
    uint64_t Offset = reinterpret_cast<const char *>(ArMemHdr) -
236
2
                      Parent->getData().data();
237
2
    return malformedError("characters in size field in archive header are not "
238
2
                          "all decimal numbers: '" + Buf + "' for archive "
239
2
                          "member header at offset " + Twine(Offset));
240
2
  }
241
6.33k
  return Ret;
242
6.33k
}
243
244
139
Expected<sys::fs::perms> ArchiveMemberHeader::getAccessMode() const {
245
139
  unsigned Ret;
246
139
  if (StringRef(ArMemHdr->AccessMode,
247
139
                sizeof(ArMemHdr->AccessMode)).rtrim(' ').getAsInteger(8, Ret)) {
248
1
    std::string Buf;
249
1
    raw_string_ostream OS(Buf);
250
1
    OS.write_escaped(StringRef(ArMemHdr->AccessMode,
251
1
                               sizeof(ArMemHdr->AccessMode)).rtrim(" "));
252
1
    OS.flush();
253
1
    uint64_t Offset = reinterpret_cast<const char *>(ArMemHdr) -
254
1
                      Parent->getData().data();
255
1
    return malformedError("characters in AccessMode field in archive header "
256
1
                          "are not all decimal numbers: '" + Buf + "' for the "
257
1
                          "archive member header at offset " + Twine(Offset));
258
1
  }
259
138
  return static_cast<sys::fs::perms>(Ret);
260
138
}
261
262
Expected<sys::TimePoint<std::chrono::seconds>>
263
156
ArchiveMemberHeader::getLastModified() const {
264
156
  unsigned Seconds;
265
156
  if (StringRef(ArMemHdr->LastModified,
266
156
                sizeof(ArMemHdr->LastModified)).rtrim(' ')
267
156
          .getAsInteger(10, Seconds)) {
268
1
    std::string Buf;
269
1
    raw_string_ostream OS(Buf);
270
1
    OS.write_escaped(StringRef(ArMemHdr->LastModified,
271
1
                               sizeof(ArMemHdr->LastModified)).rtrim(" "));
272
1
    OS.flush();
273
1
    uint64_t Offset = reinterpret_cast<const char *>(ArMemHdr) -
274
1
                      Parent->getData().data();
275
1
    return malformedError("characters in LastModified field in archive header "
276
1
                          "are not all decimal numbers: '" + Buf + "' for the "
277
1
                          "archive member header at offset " + Twine(Offset));
278
1
  }
279
155
280
155
  return sys::toTimePoint(Seconds);
281
155
}
282
283
116
Expected<unsigned> ArchiveMemberHeader::getUID() const {
284
116
  unsigned Ret;
285
116
  StringRef User = StringRef(ArMemHdr->UID, sizeof(ArMemHdr->UID)).rtrim(' ');
286
116
  if (User.empty())
287
1
    return 0;
288
115
  if (User.getAsInteger(10, Ret)) {
289
1
    std::string Buf;
290
1
    raw_string_ostream OS(Buf);
291
1
    OS.write_escaped(User);
292
1
    OS.flush();
293
1
    uint64_t Offset = reinterpret_cast<const char *>(ArMemHdr) -
294
1
                      Parent->getData().data();
295
1
    return malformedError("characters in UID field in archive header "
296
1
                          "are not all decimal numbers: '" + Buf + "' for the "
297
1
                          "archive member header at offset " + Twine(Offset));
298
1
  }
299
114
  return Ret;
300
114
}
301
302
115
Expected<unsigned> ArchiveMemberHeader::getGID() const {
303
115
  unsigned Ret;
304
115
  StringRef Group = StringRef(ArMemHdr->GID, sizeof(ArMemHdr->GID)).rtrim(' ');
305
115
  if (Group.empty())
306
1
    return 0;
307
114
  if (Group.getAsInteger(10, Ret)) {
308
1
    std::string Buf;
309
1
    raw_string_ostream OS(Buf);
310
1
    OS.write_escaped(Group);
311
1
    OS.flush();
312
1
    uint64_t Offset = reinterpret_cast<const char *>(ArMemHdr) -
313
1
                      Parent->getData().data();
314
1
    return malformedError("characters in GID field in archive header "
315
1
                          "are not all decimal numbers: '" + Buf + "' for the "
316
1
                          "archive member header at offset " + Twine(Offset));
317
1
  }
318
113
  return Ret;
319
113
}
320
321
Archive::Child::Child(const Archive *Parent, StringRef Data,
322
                      uint16_t StartOfFile)
323
    : Parent(Parent), Header(Parent, Data.data(), Data.size(), nullptr),
324
623
      Data(Data), StartOfFile(StartOfFile) {
325
623
}
326
327
Archive::Child::Child(const Archive *Parent, const char *Start, Error *Err)
328
    : Parent(Parent),
329
      Header(Parent, Start,
330
             Parent
331
               ? Parent->getData().size() - (Start - Parent->getData().data())
332
5.67k
               : 0, Err) {
333
5.67k
  if (!Start)
334
2.14k
    return;
335
3.53k
336
3.53k
  // If we are pointed to real data, Start is not a nullptr, then there must be
337
3.53k
  // a non-null Err pointer available to report malformed data on.  Only in
338
3.53k
  // the case sentinel value is being constructed is Err is permitted to be a
339
3.53k
  // nullptr.
340
3.53k
  assert(Err && "Err can't be nullptr if Start is not a nullptr");
341
3.53k
342
3.53k
  ErrorAsOutParameter ErrAsOutParam(Err);
343
3.53k
344
3.53k
  // If there was an error in the construction of the Header
345
3.53k
  // then just return with the error now set.
346
3.53k
  if (*Err)
347
2
    return;
348
3.53k
349
3.53k
  uint64_t Size = Header.getSizeOf();
350
3.53k
  Data = StringRef(Start, Size);
351
3.53k
  Expected<bool> isThinOrErr = isThinMember();
352
3.53k
  if (!isThinOrErr) {
353
1
    *Err = isThinOrErr.takeError();
354
1
    return;
355
1
  }
356
3.53k
  bool isThin = isThinOrErr.get();
357
3.53k
  if (!isThin) {
358
3.38k
    Expected<uint64_t> MemberSize = getRawSize();
359
3.38k
    if (!MemberSize) {
360
2
      *Err = MemberSize.takeError();
361
2
      return;
362
2
    }
363
3.38k
    Size += MemberSize.get();
364
3.38k
    Data = StringRef(Start, Size);
365
3.38k
  }
366
3.53k
367
3.53k
  // Setup StartOfFile and PaddingBytes.
368
3.53k
  StartOfFile = Header.getSizeOf();
369
3.52k
  // Don't include attached name.
370
3.52k
  Expected<StringRef> NameOrErr = getRawName();
371
3.52k
  if (!NameOrErr){
372
0
    *Err = NameOrErr.takeError();
373
0
    return;
374
0
  }
375
3.52k
  StringRef Name = NameOrErr.get();
376
3.52k
  if (Name.startswith("#1/")) {
377
803
    uint64_t NameSize;
378
803
    if (Name.substr(3).rtrim(' ').getAsInteger(10, NameSize)) {
379
1
      std::string Buf;
380
1
      raw_string_ostream OS(Buf);
381
1
      OS.write_escaped(Name.substr(3).rtrim(' '));
382
1
      OS.flush();
383
1
      uint64_t Offset = Start - Parent->getData().data();
384
1
      *Err = malformedError("long name length characters after the #1/ are "
385
1
                            "not all decimal numbers: '" + Buf + "' for "
386
1
                            "archive member header at offset " +
387
1
                            Twine(Offset));
388
1
      return;
389
1
    }
390
802
    StartOfFile += NameSize;
391
802
  }
392
3.52k
}
393
394
2.41k
Expected<uint64_t> Archive::Child::getSize() const {
395
2.41k
  if (Parent->IsThin) {
396
116
    Expected<uint32_t> Size = Header.getSize();
397
116
    if (!Size)
398
0
      return Size.takeError();
399
116
    return Size.get();
400
116
  }
401
2.29k
  return Data.size() - StartOfFile;
402
2.29k
}
403
404
6.22k
Expected<uint64_t> Archive::Child::getRawSize() const {
405
6.22k
  return Header.getSize();
406
6.22k
}
407
408
6.07k
Expected<bool> Archive::Child::isThinMember() const {
409
6.07k
  Expected<StringRef> NameOrErr = Header.getRawName();
410
6.07k
  if (!NameOrErr)
411
1
    return NameOrErr.takeError();
412
6.07k
  StringRef Name = NameOrErr.get();
413
6.07k
  return Parent->IsThin && 
Name != "/"551
&&
Name != "//"471
;
414
6.07k
}
415
416
103
Expected<std::string> Archive::Child::getFullName() const {
417
103
  Expected<bool> isThin = isThinMember();
418
103
  if (!isThin)
419
0
    return isThin.takeError();
420
103
  assert(isThin.get());
421
103
  Expected<StringRef> NameOrErr = getName();
422
103
  if (!NameOrErr)
423
0
    return NameOrErr.takeError();
424
103
  StringRef Name = *NameOrErr;
425
103
  if (sys::path::is_absolute(Name))
426
42
    return Name;
427
61
428
61
  SmallString<128> FullName = sys::path::parent_path(
429
61
      Parent->getMemoryBufferRef().getBufferIdentifier());
430
61
  sys::path::append(FullName, Name);
431
61
  return StringRef(FullName);
432
61
}
433
434
2.43k
Expected<StringRef> Archive::Child::getBuffer() const {
435
2.43k
  Expected<bool> isThinOrErr = isThinMember();
436
2.43k
  if (!isThinOrErr)
437
0
    return isThinOrErr.takeError();
438
2.43k
  bool isThin = isThinOrErr.get();
439
2.43k
  if (!isThin) {
440
2.35k
    Expected<uint32_t> Size = getSize();
441
2.35k
    if (!Size)
442
0
      return Size.takeError();
443
2.35k
    return StringRef(Data.data() + StartOfFile, Size.get());
444
2.35k
  }
445
79
  Expected<std::string> FullNameOrErr = getFullName();
446
79
  if (!FullNameOrErr)
447
0
    return FullNameOrErr.takeError();
448
79
  const std::string &FullName = *FullNameOrErr;
449
79
  ErrorOr<std::unique_ptr<MemoryBuffer>> Buf = MemoryBuffer::getFile(FullName);
450
79
  if (std::error_code EC = Buf.getError())
451
6
    return errorCodeToError(EC);
452
73
  Parent->ThinBuffers.push_back(std::move(*Buf));
453
73
  return Parent->ThinBuffers.back()->getBuffer();
454
73
}
455
456
2.76k
Expected<Archive::Child> Archive::Child::getNext() const {
457
2.76k
  size_t SpaceToSkip = Data.size();
458
2.76k
  // If it's odd, add 1 to make it even.
459
2.76k
  if (SpaceToSkip & 1)
460
315
    ++SpaceToSkip;
461
2.76k
462
2.76k
  const char *NextLoc = Data.data() + SpaceToSkip;
463
2.76k
464
2.76k
  // Check to see if this is at the end of the archive.
465
2.76k
  if (NextLoc == Parent->Data.getBufferEnd())
466
610
    return Child(nullptr, nullptr, nullptr);
467
2.15k
468
2.15k
  // Check to see if this is past the end of the archive.
469
2.15k
  if (NextLoc > Parent->Data.getBufferEnd()) {
470
1
    std::string Msg("offset to next archive member past the end of the archive "
471
1
                    "after member ");
472
1
    Expected<StringRef> NameOrErr = getName();
473
1
    if (!NameOrErr) {
474
0
      consumeError(NameOrErr.takeError());
475
0
      uint64_t Offset = Data.data() - Parent->getData().data();
476
0
      return malformedError(Msg + "at offset " + Twine(Offset));
477
0
    } else
478
1
      return malformedError(Msg + NameOrErr.get());
479
2.15k
  }
480
2.15k
481
2.15k
  Error Err = Error::success();
482
2.15k
  Child Ret(Parent, NextLoc, &Err);
483
2.15k
  if (Err)
484
3
    return std::move(Err);
485
2.15k
  return Ret;
486
2.15k
}
487
488
559
uint64_t Archive::Child::getChildOffset() const {
489
559
  const char *a = Parent->Data.getBuffer().data();
490
559
  const char *c = Data.data();
491
559
  uint64_t offset = c - a;
492
559
  return offset;
493
559
}
494
495
2.80k
Expected<StringRef> Archive::Child::getName() const {
496
2.80k
  Expected<uint64_t> RawSizeOrErr = getRawSize();
497
2.80k
  if (!RawSizeOrErr)
498
0
    return RawSizeOrErr.takeError();
499
2.80k
  uint64_t RawSize = RawSizeOrErr.get();
500
2.80k
  Expected<StringRef> NameOrErr = Header.getName(Header.getSizeOf() + RawSize);
501
2.80k
  if (!NameOrErr)
502
7
    return NameOrErr.takeError();
503
2.79k
  StringRef Name = NameOrErr.get();
504
2.79k
  return Name;
505
2.79k
}
506
507
1.15k
Expected<MemoryBufferRef> Archive::Child::getMemoryBufferRef() const {
508
1.15k
  Expected<StringRef> NameOrErr = getName();
509
1.15k
  if (!NameOrErr)
510
3
    return NameOrErr.takeError();
511
1.15k
  StringRef Name = NameOrErr.get();
512
1.15k
  Expected<StringRef> Buf = getBuffer();
513
1.15k
  if (!Buf)
514
5
    return createFileError(Name, Buf.takeError());
515
1.14k
  return MemoryBufferRef(*Buf, Name);
516
1.14k
}
517
518
Expected<std::unique_ptr<Binary>>
519
485
Archive::Child::getAsBinary(LLVMContext *Context) const {
520
485
  Expected<MemoryBufferRef> BuffOrErr = getMemoryBufferRef();
521
485
  if (!BuffOrErr)
522
8
    return BuffOrErr.takeError();
523
477
524
477
  auto BinaryOrErr = createBinary(BuffOrErr.get(), Context);
525
477
  if (BinaryOrErr)
526
449
    return std::move(*BinaryOrErr);
527
28
  return BinaryOrErr.takeError();
528
28
}
529
530
608
Expected<std::unique_ptr<Archive>> Archive::create(MemoryBufferRef Source) {
531
608
  Error Err = Error::success();
532
608
  std::unique_ptr<Archive> Ret(new Archive(Source, Err));
533
608
  if (Err)
534
8
    return std::move(Err);
535
600
  return std::move(Ret);
536
600
}
537
538
885
void Archive::setFirstRegular(const Child &C) {
539
885
  FirstRegularData = C.Data;
540
885
  FirstRegularStartOfFile = C.StartOfFile;
541
885
}
542
543
Archive::Archive(MemoryBufferRef Source, Error &Err)
544
896
    : Binary(Binary::ID_Archive, Source) {
545
896
  ErrorAsOutParameter ErrAsOutParam(&Err);
546
896
  StringRef Buffer = Data.getBuffer();
547
896
  // Check for sufficient magic.
548
896
  if (Buffer.startswith(ThinMagic)) {
549
70
    IsThin = true;
550
826
  } else if (Buffer.startswith(Magic)) {
551
822
    IsThin = false;
552
822
  } else {
553
4
    Err = make_error<GenericBinaryError>("File too small to be an archive",
554
4
                                         object_error::invalid_file_type);
555
4
    return;
556
4
  }
557
892
558
892
  // Make sure Format is initialized before any call to
559
892
  // ArchiveMemberHeader::getName() is made.  This could be a valid empty
560
892
  // archive which is the same in all formats.  So claiming it to be gnu to is
561
892
  // fine if not totally correct before we look for a string table or table of
562
892
  // contents.
563
892
  Format = K_GNU;
564
892
565
892
  // Get the special members.
566
892
  child_iterator I = child_begin(Err, false);
567
892
  if (Err)
568
3
    return;
569
889
  child_iterator E = child_end();
570
889
571
889
  // See if this is a valid empty archive and if so return.
572
889
  if (I == E) {
573
2
    Err = Error::success();
574
2
    return;
575
2
  }
576
887
  const Child *C = &*I;
577
887
578
1.23k
  auto Increment = [&]() {
579
1.23k
    ++I;
580
1.23k
    if (Err)
581
1
      return true;
582
1.23k
    C = &*I;
583
1.23k
    return false;
584
1.23k
  };
585
887
586
887
  Expected<StringRef> NameOrErr = C->getRawName();
587
887
  if (!NameOrErr) {
588
0
    Err = NameOrErr.takeError();
589
0
    return;
590
0
  }
591
887
  StringRef Name = NameOrErr.get();
592
887
593
887
  // Below is the pattern that is used to figure out the archive format
594
887
  // GNU archive format
595
887
  //  First member : / (may exist, if it exists, points to the symbol table )
596
887
  //  Second member : // (may exist, if it exists, points to the string table)
597
887
  //  Note : The string table is used if the filename exceeds 15 characters
598
887
  // BSD archive format
599
887
  //  First member : __.SYMDEF or "__.SYMDEF SORTED" (the symbol table)
600
887
  //  There is no string table, if the filename exceeds 15 characters or has a
601
887
  //  embedded space, the filename has #1/<size>, The size represents the size
602
887
  //  of the filename that needs to be read after the archive header
603
887
  // COFF archive format
604
887
  //  First member : /
605
887
  //  Second member : / (provides a directory of symbols)
606
887
  //  Third member : // (may exist, if it exists, contains the string table)
607
887
  //  Note: Microsoft PE/COFF Spec 8.3 says that the third member is present
608
887
  //  even if the string table is empty. However, lib.exe does not in fact
609
887
  //  seem to create the third member if there's no member whose filename
610
887
  //  exceeds 15 characters. So the third member is optional.
611
887
612
887
  if (Name == "__.SYMDEF" || Name == "__.SYMDEF_64") {
613
0
    if (Name == "__.SYMDEF")
614
0
      Format = K_BSD;
615
0
    else // Name == "__.SYMDEF_64"
616
0
      Format = K_DARWIN64;
617
0
    // We know that the symbol table is not an external file, but we still must
618
0
    // check any Expected<> return value.
619
0
    Expected<StringRef> BufOrErr = C->getBuffer();
620
0
    if (!BufOrErr) {
621
0
      Err = BufOrErr.takeError();
622
0
      return;
623
0
    }
624
0
    SymbolTable = BufOrErr.get();
625
0
    if (Increment())
626
0
      return;
627
0
    setFirstRegular(*C);
628
0
629
0
    Err = Error::success();
630
0
    return;
631
0
  }
632
887
633
887
  if (Name.startswith("#1/")) {
634
271
    Format = K_BSD;
635
271
    // We know this is BSD, so getName will work since there is no string table.
636
271
    Expected<StringRef> NameOrErr = C->getName();
637
271
    if (!NameOrErr) {
638
0
      Err = NameOrErr.takeError();
639
0
      return;
640
0
    }
641
271
    Name = NameOrErr.get();
642
271
    if (Name == "__.SYMDEF SORTED" || 
Name == "__.SYMDEF"158
) {
643
216
      // We know that the symbol table is not an external file, but we still
644
216
      // must check any Expected<> return value.
645
216
      Expected<StringRef> BufOrErr = C->getBuffer();
646
216
      if (!BufOrErr) {
647
0
        Err = BufOrErr.takeError();
648
0
        return;
649
0
      }
650
216
      SymbolTable = BufOrErr.get();
651
216
      if (Increment())
652
1
        return;
653
55
    }
654
55
    else if (Name == "__.SYMDEF_64 SORTED" || Name == "__.SYMDEF_64") {
655
2
      Format = K_DARWIN64;
656
2
      // We know that the symbol table is not an external file, but we still
657
2
      // must check any Expected<> return value.
658
2
      Expected<StringRef> BufOrErr = C->getBuffer();
659
2
      if (!BufOrErr) {
660
0
        Err = BufOrErr.takeError();
661
0
        return;
662
0
      }
663
2
      SymbolTable = BufOrErr.get();
664
2
      if (Increment())
665
0
        return;
666
270
    }
667
270
    setFirstRegular(*C);
668
270
    return;
669
270
  }
670
616
671
616
  // MIPS 64-bit ELF archives use a special format of a symbol table.
672
616
  // This format is marked by `ar_name` field equals to "/SYM64/".
673
616
  // For detailed description see page 96 in the following document:
674
616
  // http://techpubs.sgi.com/library/manuals/4000/007-4658-001/pdf/007-4658-001.pdf
675
616
676
616
  bool has64SymTable = false;
677
616
  if (Name == "/" || 
Name == "/SYM64/"81
) {
678
536
    // We know that the symbol table is not an external file, but we still
679
536
    // must check any Expected<> return value.
680
536
    Expected<StringRef> BufOrErr = C->getBuffer();
681
536
    if (!BufOrErr) {
682
0
      Err = BufOrErr.takeError();
683
0
      return;
684
0
    }
685
536
    SymbolTable = BufOrErr.get();
686
536
    if (Name == "/SYM64/")
687
1
      has64SymTable = true;
688
536
689
536
    if (Increment())
690
0
      return;
691
536
    if (I == E) {
692
1
      Err = Error::success();
693
1
      return;
694
1
    }
695
535
    Expected<StringRef> NameOrErr = C->getRawName();
696
535
    if (!NameOrErr) {
697
0
      Err = NameOrErr.takeError();
698
0
      return;
699
0
    }
700
535
    Name = NameOrErr.get();
701
535
  }
702
616
703
616
  
if (615
Name == "//"615
) {
704
418
    Format = has64SymTable ? 
K_GNU641
:
K_GNU417
;
705
418
    // The string table is never an external member, but we still
706
418
    // must check any Expected<> return value.
707
418
    Expected<StringRef> BufOrErr = C->getBuffer();
708
418
    if (!BufOrErr) {
709
0
      Err = BufOrErr.takeError();
710
0
      return;
711
0
    }
712
418
    StringTable = BufOrErr.get();
713
418
    if (Increment())
714
0
      return;
715
418
    setFirstRegular(*C);
716
418
    Err = Error::success();
717
418
    return;
718
418
  }
719
197
720
197
  if (Name[0] != '/') {
721
146
    Format = has64SymTable ? 
K_GNU640
: K_GNU;
722
146
    setFirstRegular(*C);
723
146
    Err = Error::success();
724
146
    return;
725
146
  }
726
51
727
51
  if (Name != "/") {
728
0
    Err = errorCodeToError(object_error::parse_failed);
729
0
    return;
730
0
  }
731
51
732
51
  Format = K_COFF;
733
51
  // We know that the symbol table is not an external file, but we still
734
51
  // must check any Expected<> return value.
735
51
  Expected<StringRef> BufOrErr = C->getBuffer();
736
51
  if (!BufOrErr) {
737
0
    Err = BufOrErr.takeError();
738
0
    return;
739
0
  }
740
51
  SymbolTable = BufOrErr.get();
741
51
742
51
  if (Increment())
743
0
    return;
744
51
745
51
  if (I == E) {
746
0
    setFirstRegular(*C);
747
0
    Err = Error::success();
748
0
    return;
749
0
  }
750
51
751
51
  NameOrErr = C->getRawName();
752
51
  if (!NameOrErr) {
753
0
    Err = NameOrErr.takeError();
754
0
    return;
755
0
  }
756
51
  Name = NameOrErr.get();
757
51
758
51
  if (Name == "//") {
759
8
    // The string table is never an external member, but we still
760
8
    // must check any Expected<> return value.
761
8
    Expected<StringRef> BufOrErr = C->getBuffer();
762
8
    if (!BufOrErr) {
763
0
      Err = BufOrErr.takeError();
764
0
      return;
765
0
    }
766
8
    StringTable = BufOrErr.get();
767
8
    if (Increment())
768
0
      return;
769
51
  }
770
51
771
51
  setFirstRegular(*C);
772
51
  Err = Error::success();
773
51
}
774
775
Archive::child_iterator Archive::child_begin(Error &Err,
776
1.53k
                                             bool SkipInternal) const {
777
1.53k
  if (isEmpty())
778
4
    return child_end();
779
1.52k
780
1.52k
  if (SkipInternal)
781
623
    return child_iterator::itr(
782
623
        Child(this, FirstRegularData, FirstRegularStartOfFile), Err);
783
904
784
904
  const char *Loc = Data.getBufferStart() + strlen(Magic);
785
904
  Child C(this, Loc, &Err);
786
904
  if (Err)
787
3
    return child_end();
788
901
  return child_iterator::itr(C, Err);
789
901
}
790
791
1.53k
Archive::child_iterator Archive::child_end() const {
792
1.53k
  return child_iterator::end(Child(nullptr, nullptr, nullptr));
793
1.53k
}
794
795
1.51k
StringRef Archive::Symbol::getName() const {
796
1.51k
  return Parent->getSymbolTable().begin() + StringIndex;
797
1.51k
}
798
799
477
Expected<Archive::Child> Archive::Symbol::getMember() const {
800
477
  const char *Buf = Parent->getSymbolTable().begin();
801
477
  const char *Offsets = Buf;
802
477
  if (Parent->kind() == K_GNU64 || 
Parent->kind() == K_DARWIN64474
)
803
8
    Offsets += sizeof(uint64_t);
804
469
  else
805
469
    Offsets += sizeof(uint32_t);
806
477
  uint64_t Offset = 0;
807
477
  if (Parent->kind() == K_GNU) {
808
282
    Offset = read32be(Offsets + SymbolIndex * 4);
809
282
  } else 
if (195
Parent->kind() == K_GNU64195
) {
810
3
    Offset = read64be(Offsets + SymbolIndex * 8);
811
192
  } else if (Parent->kind() == K_BSD) {
812
46
    // The SymbolIndex is an index into the ranlib structs that start at
813
46
    // Offsets (the first uint32_t is the number of bytes of the ranlib
814
46
    // structs).  The ranlib structs are a pair of uint32_t's the first
815
46
    // being a string table offset and the second being the offset into
816
46
    // the archive of the member that defines the symbol.  Which is what
817
46
    // is needed here.
818
46
    Offset = read32le(Offsets + SymbolIndex * 8 + 4);
819
146
  } else if (Parent->kind() == K_DARWIN64) {
820
5
    // The SymbolIndex is an index into the ranlib_64 structs that start at
821
5
    // Offsets (the first uint64_t is the number of bytes of the ranlib_64
822
5
    // structs).  The ranlib_64 structs are a pair of uint64_t's the first
823
5
    // being a string table offset and the second being the offset into
824
5
    // the archive of the member that defines the symbol.  Which is what
825
5
    // is needed here.
826
5
    Offset = read64le(Offsets + SymbolIndex * 16 + 8);
827
141
  } else {
828
141
    // Skip offsets.
829
141
    uint32_t MemberCount = read32le(Buf);
830
141
    Buf += MemberCount * 4 + 4;
831
141
832
141
    uint32_t SymbolCount = read32le(Buf);
833
141
    if (SymbolIndex >= SymbolCount)
834
0
      return errorCodeToError(object_error::parse_failed);
835
141
836
141
    // Skip SymbolCount to get to the indices table.
837
141
    const char *Indices = Buf + 4;
838
141
839
141
    // Get the index of the offset in the file member offset table for this
840
141
    // symbol.
841
141
    uint16_t OffsetIndex = read16le(Indices + SymbolIndex * 2);
842
141
    // Subtract 1 since OffsetIndex is 1 based.
843
141
    --OffsetIndex;
844
141
845
141
    if (OffsetIndex >= MemberCount)
846
0
      return errorCodeToError(object_error::parse_failed);
847
141
848
141
    Offset = read32le(Offsets + OffsetIndex * 4);
849
141
  }
850
477
851
477
  const char *Loc = Parent->getData().begin() + Offset;
852
477
  Error Err = Error::success();
853
477
  Child C(Parent, Loc, &Err);
854
477
  if (Err)
855
0
    return std::move(Err);
856
477
  return C;
857
477
}
858
859
982
Archive::Symbol Archive::Symbol::getNext() const {
860
982
  Symbol t(*this);
861
982
  if (Parent->kind() == K_BSD) {
862
51
    // t.StringIndex is an offset from the start of the __.SYMDEF or
863
51
    // "__.SYMDEF SORTED" member into the string table for the ranlib
864
51
    // struct indexed by t.SymbolIndex .  To change t.StringIndex to the
865
51
    // offset in the string table for t.SymbolIndex+1 we subtract the
866
51
    // its offset from the start of the string table for t.SymbolIndex
867
51
    // and add the offset of the string table for t.SymbolIndex+1.
868
51
869
51
    // The __.SYMDEF or "__.SYMDEF SORTED" member starts with a uint32_t
870
51
    // which is the number of bytes of ranlib structs that follow.  The ranlib
871
51
    // structs are a pair of uint32_t's the first being a string table offset
872
51
    // and the second being the offset into the archive of the member that
873
51
    // define the symbol. After that the next uint32_t is the byte count of
874
51
    // the string table followed by the string table.
875
51
    const char *Buf = Parent->getSymbolTable().begin();
876
51
    uint32_t RanlibCount = 0;
877
51
    RanlibCount = read32le(Buf) / 8;
878
51
    // If t.SymbolIndex + 1 will be past the count of symbols (the RanlibCount)
879
51
    // don't change the t.StringIndex as we don't want to reference a ranlib
880
51
    // past RanlibCount.
881
51
    if (t.SymbolIndex + 1 < RanlibCount) {
882
17
      const char *Ranlibs = Buf + 4;
883
17
      uint32_t CurRanStrx = 0;
884
17
      uint32_t NextRanStrx = 0;
885
17
      CurRanStrx = read32le(Ranlibs + t.SymbolIndex * 8);
886
17
      NextRanStrx = read32le(Ranlibs + (t.SymbolIndex + 1) * 8);
887
17
      t.StringIndex -= CurRanStrx;
888
17
      t.StringIndex += NextRanStrx;
889
17
    }
890
931
  } else {
891
931
    // Go to one past next null.
892
931
    t.StringIndex = Parent->getSymbolTable().find('\0', t.StringIndex) + 1;
893
931
  }
894
982
  ++t.SymbolIndex;
895
982
  return t;
896
982
}
897
898
308
Archive::symbol_iterator Archive::symbol_begin() const {
899
308
  if (!hasSymbolTable())
900
9
    return symbol_iterator(Symbol(this, 0, 0));
901
299
902
299
  const char *buf = getSymbolTable().begin();
903
299
  if (kind() == K_GNU) {
904
213
    uint32_t symbol_count = 0;
905
213
    symbol_count = read32be(buf);
906
213
    buf += sizeof(uint32_t) + (symbol_count * (sizeof(uint32_t)));
907
213
  } else 
if (86
kind() == K_GNU6486
) {
908
1
    uint64_t symbol_count = read64be(buf);
909
1
    buf += sizeof(uint64_t) + (symbol_count * (sizeof(uint64_t)));
910
85
  } else if (kind() == K_BSD) {
911
42
    // The __.SYMDEF or "__.SYMDEF SORTED" member starts with a uint32_t
912
42
    // which is the number of bytes of ranlib structs that follow.  The ranlib
913
42
    // structs are a pair of uint32_t's the first being a string table offset
914
42
    // and the second being the offset into the archive of the member that
915
42
    // define the symbol. After that the next uint32_t is the byte count of
916
42
    // the string table followed by the string table.
917
42
    uint32_t ranlib_count = 0;
918
42
    ranlib_count = read32le(buf) / 8;
919
42
    const char *ranlibs = buf + 4;
920
42
    uint32_t ran_strx = 0;
921
42
    ran_strx = read32le(ranlibs);
922
42
    buf += sizeof(uint32_t) + (ranlib_count * (2 * (sizeof(uint32_t))));
923
42
    // Skip the byte count of the string table.
924
42
    buf += sizeof(uint32_t);
925
42
    buf += ran_strx;
926
43
  } else if (kind() == K_DARWIN64) {
927
2
    // The __.SYMDEF_64 or "__.SYMDEF_64 SORTED" member starts with a uint64_t
928
2
    // which is the number of bytes of ranlib_64 structs that follow.  The
929
2
    // ranlib_64 structs are a pair of uint64_t's the first being a string
930
2
    // table offset and the second being the offset into the archive of the
931
2
    // member that define the symbol. After that the next uint64_t is the byte
932
2
    // count of the string table followed by the string table.
933
2
    uint64_t ranlib_count = 0;
934
2
    ranlib_count = read64le(buf) / 16;
935
2
    const char *ranlibs = buf + 8;
936
2
    uint64_t ran_strx = 0;
937
2
    ran_strx = read64le(ranlibs);
938
2
    buf += sizeof(uint64_t) + (ranlib_count * (2 * (sizeof(uint64_t))));
939
2
    // Skip the byte count of the string table.
940
2
    buf += sizeof(uint64_t);
941
2
    buf += ran_strx;
942
41
  } else {
943
41
    uint32_t member_count = 0;
944
41
    uint32_t symbol_count = 0;
945
41
    member_count = read32le(buf);
946
41
    buf += 4 + (member_count * 4); // Skip offsets.
947
41
    symbol_count = read32le(buf);
948
41
    buf += 4 + (symbol_count * 2); // Skip indices.
949
41
  }
950
299
  uint32_t string_start_offset = buf - getSymbolTable().begin();
951
299
  return symbol_iterator(Symbol(this, 0, string_start_offset));
952
299
}
953
954
308
Archive::symbol_iterator Archive::symbol_end() const {
955
308
  return symbol_iterator(Symbol(this, getNumberOfSymbols(), 0));
956
308
}
957
958
308
uint32_t Archive::getNumberOfSymbols() const {
959
308
  if (!hasSymbolTable())
960
9
    return 0;
961
299
  const char *buf = getSymbolTable().begin();
962
299
  if (kind() == K_GNU)
963
213
    return read32be(buf);
964
86
  if (kind() == K_GNU64)
965
1
    return read64be(buf);
966
85
  if (kind() == K_BSD)
967
42
    return read32le(buf) / 8;
968
43
  if (kind() == K_DARWIN64)
969
2
    return read64le(buf) / 16;
970
41
  uint32_t member_count = 0;
971
41
  member_count = read32le(buf);
972
41
  buf += 4 + (member_count * 4); // Skip offsets.
973
41
  return read32le(buf);
974
41
}
975
976
5
Expected<Optional<Archive::Child>> Archive::findSym(StringRef name) const {
977
5
  Archive::symbol_iterator bs = symbol_begin();
978
5
  Archive::symbol_iterator es = symbol_end();
979
5
980
9
  for (; bs != es; 
++bs4
) {
981
8
    StringRef SymName = bs->getName();
982
8
    if (SymName == name) {
983
4
      if (auto MemberOrErr = bs->getMember())
984
4
        return Child(*MemberOrErr);
985
0
      else
986
0
        return MemberOrErr.takeError();
987
4
    }
988
8
  }
989
5
  
return Optional<Child>()1
;
990
5
}
991
992
// Returns true if archive file contains no member file.
993
1.67k
bool Archive::isEmpty() const { return Data.getBufferSize() == 8; }
994
995
787
bool Archive::hasSymbolTable() const { return !SymbolTable.empty(); }