LLVM 18.0.0git
Archive.cpp
Go to the documentation of this file.
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"
15#include "llvm/ADT/StringRef.h"
16#include "llvm/ADT/Twine.h"
17#include "llvm/Object/Binary.h"
18#include "llvm/Object/Error.h"
19#include "llvm/Support/Chrono.h"
20#include "llvm/Support/Endian.h"
22#include "llvm/Support/Error.h"
27#include "llvm/Support/Path.h"
30#include <algorithm>
31#include <cassert>
32#include <cstddef>
33#include <cstdint>
34#include <memory>
35#include <string>
36#include <system_error>
37
38using namespace llvm;
39using namespace object;
40using namespace llvm::support::endian;
41
42void Archive::anchor() {}
43
45 std::string StringMsg = "truncated or malformed archive (" + Msg.str() + ")";
46 return make_error<GenericBinaryError>(std::move(StringMsg),
48}
49
50static Error
52 const char *RawHeaderPtr, uint64_t Size) {
53 StringRef Msg("remaining size of archive too small for next archive "
54 "member header ");
55
56 Expected<StringRef> NameOrErr = ArMemHeader->getName(Size);
57 if (NameOrErr)
58 return malformedError(Msg + "for " + *NameOrErr);
59
60 consumeError(NameOrErr.takeError());
61 uint64_t Offset = RawHeaderPtr - ArMemHeader->Parent->getData().data();
62 return malformedError(Msg + "at offset " + Twine(Offset));
63}
64
65template <class T, std::size_t N>
67 return StringRef(Field, N).rtrim(" ");
68}
69
70template <class T>
72 return getFieldRawString(ArMemHdr->AccessMode);
73}
74
75template <class T>
77 return getFieldRawString(ArMemHdr->LastModified);
78}
79
81 return getFieldRawString(ArMemHdr->UID);
85 return getFieldRawString(ArMemHdr->GID);
87
89 return reinterpret_cast<const char *>(ArMemHdr) - Parent->getData().data();
90}
91
94
96 const char *RawHeaderPtr,
97 uint64_t Size, Error *Err)
99 Parent, reinterpret_cast<const UnixArMemHdrType *>(RawHeaderPtr)) {
100 if (RawHeaderPtr == nullptr)
101 return;
102 ErrorAsOutParameter ErrAsOutParam(Err);
103
104 if (Size < getSizeOf()) {
105 *Err = createMemberHeaderParseError(this, RawHeaderPtr, Size);
106 return;
107 }
108 if (ArMemHdr->Terminator[0] != '`' || ArMemHdr->Terminator[1] != '\n') {
109 if (Err) {
110 std::string Buf;
114 OS.flush();
115 std::string Msg("terminator characters in archive member \"" + Buf +
116 "\" not the correct \"`\\n\" values for the archive "
117 "member header ");
118 Expected<StringRef> NameOrErr = getName(Size);
119 if (!NameOrErr) {
120 consumeError(NameOrErr.takeError());
121 uint64_t Offset = RawHeaderPtr - Parent->getData().data();
122 *Err = malformedError(Msg + "at offset " + Twine(Offset));
123 } else
124 *Err = malformedError(Msg + "for " + NameOrErr.get());
125 }
126 return;
127 }
128}
129
131 const char *RawHeaderPtr,
132 uint64_t Size, Error *Err)
134 Parent, reinterpret_cast<const BigArMemHdrType *>(RawHeaderPtr)) {
135 if (RawHeaderPtr == nullptr)
136 return;
137 ErrorAsOutParameter ErrAsOutParam(Err);
138
139 if (RawHeaderPtr + getSizeOf() >= Parent->getData().end()) {
140 if (Err)
141 *Err = malformedError("malformed AIX big archive: remaining buffer is "
142 "unable to contain next archive member");
143 return;
144 }
145
146 if (Size < getSizeOf()) {
147 Error SubErr = createMemberHeaderParseError(this, RawHeaderPtr, Size);
148 if (Err)
149 *Err = std::move(SubErr);
150 }
151}
152
153// This gets the raw name from the ArMemHdr->Name field and checks that it is
154// valid for the kind of archive. If it is not valid it returns an Error.
156 char EndCond;
157 auto Kind = Parent->kind();
159 if (ArMemHdr->Name[0] == ' ') {
161 reinterpret_cast<const char *>(ArMemHdr) - Parent->getData().data();
162 return malformedError("name contains a leading space for archive member "
163 "header at offset " +
164 Twine(Offset));
165 }
166 EndCond = ' ';
167 } else if (ArMemHdr->Name[0] == '/' || ArMemHdr->Name[0] == '#')
168 EndCond = ' ';
169 else
170 EndCond = '/';
172 StringRef(ArMemHdr->Name, sizeof(ArMemHdr->Name)).find(EndCond);
173 if (end == StringRef::npos)
174 end = sizeof(ArMemHdr->Name);
175 assert(end <= sizeof(ArMemHdr->Name) && end > 0);
176 // Don't include the EndCond if there is one.
177 return StringRef(ArMemHdr->Name, end);
178}
179
181getArchiveMemberDecField(Twine FieldName, const StringRef RawField,
182 const Archive *Parent,
183 const AbstractArchiveMemberHeader *MemHeader) {
185 if (RawField.getAsInteger(10, Value)) {
186 uint64_t Offset = MemHeader->getOffset();
187 return malformedError("characters in " + FieldName +
188 " field in archive member header are not "
189 "all decimal numbers: '" +
190 RawField +
191 "' for the archive "
192 "member header at offset " +
193 Twine(Offset));
194 }
195 return Value;
196}
197
199getArchiveMemberOctField(Twine FieldName, const StringRef RawField,
200 const Archive *Parent,
201 const AbstractArchiveMemberHeader *MemHeader) {
203 if (RawField.getAsInteger(8, Value)) {
204 uint64_t Offset = MemHeader->getOffset();
205 return malformedError("characters in " + FieldName +
206 " field in archive member header are not "
207 "all octal numbers: '" +
208 RawField +
209 "' for the archive "
210 "member header at offset " +
211 Twine(Offset));
212 }
213 return Value;
214}
215
218 "NameLen", getFieldRawString(ArMemHdr->NameLen), Parent, this);
219 if (!NameLenOrErr)
220 // TODO: Out-of-line.
221 return NameLenOrErr.takeError();
222 uint64_t NameLen = NameLenOrErr.get();
223
224 // If the name length is odd, pad with '\0' to get an even length. After
225 // padding, there is the name terminator "`\n".
226 uint64_t NameLenWithPadding = alignTo(NameLen, 2);
227 StringRef NameTerminator = "`\n";
228 StringRef NameStringWithNameTerminator =
229 StringRef(ArMemHdr->Name, NameLenWithPadding + NameTerminator.size());
230 if (!NameStringWithNameTerminator.endswith(NameTerminator)) {
232 reinterpret_cast<const char *>(ArMemHdr->Name + NameLenWithPadding) -
233 Parent->getData().data();
234 // TODO: Out-of-line.
235 return malformedError(
236 "name does not have name terminator \"`\\n\" for archive member"
237 "header at offset " +
238 Twine(Offset));
239 }
240 return StringRef(ArMemHdr->Name, NameLen);
241}
242
243// member including the header, so the size of any name following the header
244// is checked to make sure it does not overflow.
246
247 // This can be called from the ArchiveMemberHeader constructor when the
248 // archive header is truncated to produce an error message with the name.
249 // Make sure the name field is not truncated.
250 if (Size < offsetof(UnixArMemHdrType, Name) + sizeof(ArMemHdr->Name)) {
251 uint64_t ArchiveOffset =
252 reinterpret_cast<const char *>(ArMemHdr) - Parent->getData().data();
253 return malformedError("archive header truncated before the name field "
254 "for archive member header at offset " +
255 Twine(ArchiveOffset));
256 }
257
258 // The raw name itself can be invalid.
259 Expected<StringRef> NameOrErr = getRawName();
260 if (!NameOrErr)
261 return NameOrErr.takeError();
262 StringRef Name = NameOrErr.get();
263
264 // Check if it's a special name.
265 if (Name[0] == '/') {
266 if (Name.size() == 1) // Linker member.
267 return Name;
268 if (Name.size() == 2 && Name[1] == '/') // String table.
269 return Name;
270 // System libraries from the Windows SDK for Windows 11 contain this symbol.
271 // It looks like a CFG guard: we just skip it for now.
272 if (Name.equals("/<XFGHASHMAP>/"))
273 return Name;
274 // Some libraries (e.g., arm64rt.lib) from the Windows WDK
275 // (version 10.0.22000.0) contain this undocumented special member.
276 if (Name.equals("/<ECSYMBOLS>/"))
277 return Name;
278 // It's a long name.
279 // Get the string table offset.
280 std::size_t StringOffset;
281 if (Name.substr(1).rtrim(' ').getAsInteger(10, StringOffset)) {
282 std::string Buf;
284 OS.write_escaped(Name.substr(1).rtrim(' '));
285 OS.flush();
286 uint64_t ArchiveOffset =
287 reinterpret_cast<const char *>(ArMemHdr) - Parent->getData().data();
288 return malformedError("long name offset characters after the '/' are "
289 "not all decimal numbers: '" +
290 Buf + "' for archive member header at offset " +
291 Twine(ArchiveOffset));
292 }
293
294 // Verify it.
295 if (StringOffset >= Parent->getStringTable().size()) {
296 uint64_t ArchiveOffset =
297 reinterpret_cast<const char *>(ArMemHdr) - Parent->getData().data();
298 return malformedError("long name offset " + Twine(StringOffset) +
299 " past the end of the string table for archive "
300 "member header at offset " +
301 Twine(ArchiveOffset));
302 }
303
304 // GNU long file names end with a "/\n".
305 if (Parent->kind() == Archive::K_GNU ||
307 size_t End = Parent->getStringTable().find('\n', /*From=*/StringOffset);
308 if (End == StringRef::npos || End < 1 ||
309 Parent->getStringTable()[End - 1] != '/') {
310 return malformedError("string table at long name offset " +
311 Twine(StringOffset) + "not terminated");
312 }
313 return Parent->getStringTable().slice(StringOffset, End - 1);
314 }
315 return Parent->getStringTable().begin() + StringOffset;
316 }
317
318 if (Name.startswith("#1/")) {
319 uint64_t NameLength;
320 if (Name.substr(3).rtrim(' ').getAsInteger(10, NameLength)) {
321 std::string Buf;
323 OS.write_escaped(Name.substr(3).rtrim(' '));
324 OS.flush();
325 uint64_t ArchiveOffset =
326 reinterpret_cast<const char *>(ArMemHdr) - Parent->getData().data();
327 return malformedError("long name length characters after the #1/ are "
328 "not all decimal numbers: '" +
329 Buf + "' for archive member header at offset " +
330 Twine(ArchiveOffset));
331 }
332 if (getSizeOf() + NameLength > Size) {
333 uint64_t ArchiveOffset =
334 reinterpret_cast<const char *>(ArMemHdr) - Parent->getData().data();
335 return malformedError("long name length: " + Twine(NameLength) +
336 " extends past the end of the member or archive "
337 "for archive member header at offset " +
338 Twine(ArchiveOffset));
339 }
340 return StringRef(reinterpret_cast<const char *>(ArMemHdr) + getSizeOf(),
341 NameLength)
342 .rtrim('\0');
343 }
344
345 // It is not a long name so trim the blanks at the end of the name.
346 if (Name[Name.size() - 1] != '/')
347 return Name.rtrim(' ');
348
349 // It's a simple name.
350 return Name.drop_back(1);
351}
352
354 return getRawName();
355}
356
359 Parent, this);
360}
361
364 "size", getFieldRawString(ArMemHdr->Size), Parent, this);
365 if (!SizeOrErr)
366 return SizeOrErr.takeError();
367
368 Expected<uint64_t> NameLenOrErr = getRawNameSize();
369 if (!NameLenOrErr)
370 return NameLenOrErr.takeError();
371
372 return *SizeOrErr + alignTo(*NameLenOrErr, 2);
373}
374
377 "NameLen", getFieldRawString(ArMemHdr->NameLen), Parent, this);
378}
379
382 "NextOffset", getFieldRawString(ArMemHdr->NextOffset), Parent, this);
383}
384
386 Expected<uint64_t> AccessModeOrErr =
387 getArchiveMemberOctField("AccessMode", getRawAccessMode(), Parent, this);
388 if (!AccessModeOrErr)
389 return AccessModeOrErr.takeError();
390 return static_cast<sys::fs::perms>(*AccessModeOrErr);
391}
392
396 "LastModified", getRawLastModified(), Parent, this);
397
398 if (!SecondsOrErr)
399 return SecondsOrErr.takeError();
400
401 return sys::toTimePoint(*SecondsOrErr);
402}
403
406 if (User.empty())
407 return 0;
408 return getArchiveMemberDecField("UID", User, Parent, this);
409}
410
412 StringRef Group = getRawGID();
413 if (Group.empty())
414 return 0;
415 return getArchiveMemberDecField("GID", Group, Parent, this);
416}
417
419 Expected<StringRef> NameOrErr = getRawName();
420 if (!NameOrErr)
421 return NameOrErr.takeError();
422 StringRef Name = NameOrErr.get();
423 return Parent->isThin() && Name != "/" && Name != "//" && Name != "/SYM64/";
424}
425
428 Expected<bool> isThinOrErr = isThin();
429 if (!isThinOrErr)
430 return isThinOrErr.takeError();
431
432 bool isThin = isThinOrErr.get();
433 if (!isThin) {
434 Expected<uint64_t> MemberSize = getSize();
435 if (!MemberSize)
436 return MemberSize.takeError();
437
438 Size += MemberSize.get();
439 }
440
441 // If Size is odd, add 1 to make it even.
442 const char *NextLoc =
443 reinterpret_cast<const char *>(ArMemHdr) + alignTo(Size, 2);
444
445 if (NextLoc == Parent->getMemoryBufferRef().getBufferEnd())
446 return nullptr;
447
448 return NextLoc;
449}
450
452 if (getOffset() ==
453 static_cast<const BigArchive *>(Parent)->getLastChildOffset())
454 return nullptr;
455
456 Expected<uint64_t> NextOffsetOrErr = getNextOffset();
457 if (!NextOffsetOrErr)
458 return NextOffsetOrErr.takeError();
459 return Parent->getData().data() + NextOffsetOrErr.get();
460}
461
463 uint16_t StartOfFile)
464 : Parent(Parent), Data(Data), StartOfFile(StartOfFile) {
465 Header = Parent->createArchiveMemberHeader(Data.data(), Data.size(), nullptr);
466}
467
468Archive::Child::Child(const Archive *Parent, const char *Start, Error *Err)
469 : Parent(Parent) {
470 if (!Start) {
471 Header = nullptr;
472 StartOfFile = -1;
473 return;
474 }
475
476 Header = Parent->createArchiveMemberHeader(
477 Start,
478 Parent ? Parent->getData().size() - (Start - Parent->getData().data())
479 : 0,
480 Err);
481
482 // If we are pointed to real data, Start is not a nullptr, then there must be
483 // a non-null Err pointer available to report malformed data on. Only in
484 // the case sentinel value is being constructed is Err is permitted to be a
485 // nullptr.
486 assert(Err && "Err can't be nullptr if Start is not a nullptr");
487
488 ErrorAsOutParameter ErrAsOutParam(Err);
489
490 // If there was an error in the construction of the Header
491 // then just return with the error now set.
492 if (*Err)
493 return;
494
495 uint64_t Size = Header->getSizeOf();
496 Data = StringRef(Start, Size);
497 Expected<bool> isThinOrErr = isThinMember();
498 if (!isThinOrErr) {
499 *Err = isThinOrErr.takeError();
500 return;
501 }
502 bool isThin = isThinOrErr.get();
503 if (!isThin) {
504 Expected<uint64_t> MemberSize = getRawSize();
505 if (!MemberSize) {
506 *Err = MemberSize.takeError();
507 return;
508 }
509 Size += MemberSize.get();
510 Data = StringRef(Start, Size);
511 }
512
513 // Setup StartOfFile and PaddingBytes.
514 StartOfFile = Header->getSizeOf();
515 // Don't include attached name.
516 Expected<StringRef> NameOrErr = getRawName();
517 if (!NameOrErr) {
518 *Err = NameOrErr.takeError();
519 return;
520 }
521 StringRef Name = NameOrErr.get();
522
523 if (Parent->kind() == Archive::K_AIXBIG) {
524 // The actual start of the file is after the name and any necessary
525 // even-alignment padding.
526 StartOfFile += ((Name.size() + 1) >> 1) << 1;
527 } else if (Name.startswith("#1/")) {
529 StringRef RawNameSize = Name.substr(3).rtrim(' ');
530 if (RawNameSize.getAsInteger(10, NameSize)) {
531 uint64_t Offset = Start - Parent->getData().data();
532 *Err = malformedError("long name length characters after the #1/ are "
533 "not all decimal numbers: '" +
534 RawNameSize +
535 "' for archive member header at offset " +
536 Twine(Offset));
537 return;
538 }
539 StartOfFile += NameSize;
540 }
541}
542
544 if (Parent->IsThin)
545 return Header->getSize();
546 return Data.size() - StartOfFile;
547}
548
550 return Header->getSize();
551}
552
553Expected<bool> Archive::Child::isThinMember() const { return Header->isThin(); }
554
556 Expected<bool> isThin = isThinMember();
557 if (!isThin)
558 return isThin.takeError();
559 assert(isThin.get());
560 Expected<StringRef> NameOrErr = getName();
561 if (!NameOrErr)
562 return NameOrErr.takeError();
563 StringRef Name = *NameOrErr;
565 return std::string(Name);
566
568 Parent->getMemoryBufferRef().getBufferIdentifier());
569 sys::path::append(FullName, Name);
570 return std::string(FullName.str());
571}
572
574 Expected<bool> isThinOrErr = isThinMember();
575 if (!isThinOrErr)
576 return isThinOrErr.takeError();
577 bool isThin = isThinOrErr.get();
578 if (!isThin) {
580 if (!Size)
581 return Size.takeError();
582 return StringRef(Data.data() + StartOfFile, Size.get());
583 }
584 Expected<std::string> FullNameOrErr = getFullName();
585 if (!FullNameOrErr)
586 return FullNameOrErr.takeError();
587 const std::string &FullName = *FullNameOrErr;
589 if (std::error_code EC = Buf.getError())
590 return errorCodeToError(EC);
591 Parent->ThinBuffers.push_back(std::move(*Buf));
592 return Parent->ThinBuffers.back()->getBuffer();
593}
594
596 Expected<const char *> NextLocOrErr = Header->getNextChildLoc();
597 if (!NextLocOrErr)
598 return NextLocOrErr.takeError();
599
600 const char *NextLoc = *NextLocOrErr;
601
602 // Check to see if this is at the end of the archive.
603 if (NextLoc == nullptr)
604 return Child(nullptr, nullptr, nullptr);
605
606 // Check to see if this is past the end of the archive.
607 if (NextLoc > Parent->Data.getBufferEnd()) {
608 std::string Msg("offset to next archive member past the end of the archive "
609 "after member ");
610 Expected<StringRef> NameOrErr = getName();
611 if (!NameOrErr) {
612 consumeError(NameOrErr.takeError());
613 uint64_t Offset = Data.data() - Parent->getData().data();
614 return malformedError(Msg + "at offset " + Twine(Offset));
615 } else
616 return malformedError(Msg + NameOrErr.get());
617 }
618
619 Error Err = Error::success();
620 Child Ret(Parent, NextLoc, &Err);
621 if (Err)
622 return std::move(Err);
623 return Ret;
624}
625
627 const char *a = Parent->Data.getBuffer().data();
628 const char *c = Data.data();
629 uint64_t offset = c - a;
630 return offset;
631}
632
634 Expected<uint64_t> RawSizeOrErr = getRawSize();
635 if (!RawSizeOrErr)
636 return RawSizeOrErr.takeError();
637 uint64_t RawSize = RawSizeOrErr.get();
638 Expected<StringRef> NameOrErr =
639 Header->getName(Header->getSizeOf() + RawSize);
640 if (!NameOrErr)
641 return NameOrErr.takeError();
642 StringRef Name = NameOrErr.get();
643 return Name;
644}
645
647 Expected<StringRef> NameOrErr = getName();
648 if (!NameOrErr)
649 return NameOrErr.takeError();
650 StringRef Name = NameOrErr.get();
651 Expected<StringRef> Buf = getBuffer();
652 if (!Buf)
653 return createFileError(Name, Buf.takeError());
654 return MemoryBufferRef(*Buf, Name);
655}
656
660 if (!BuffOrErr)
661 return BuffOrErr.takeError();
662
663 auto BinaryOrErr = createBinary(BuffOrErr.get(), Context);
664 if (BinaryOrErr)
665 return std::move(*BinaryOrErr);
666 return BinaryOrErr.takeError();
667}
668
670 Error Err = Error::success();
671 std::unique_ptr<Archive> Ret;
672 StringRef Buffer = Source.getBuffer();
673
674 if (Buffer.startswith(BigArchiveMagic))
675 Ret = std::make_unique<BigArchive>(Source, Err);
676 else
677 Ret = std::make_unique<Archive>(Source, Err);
678
679 if (Err)
680 return std::move(Err);
681 return std::move(Ret);
682}
683
684std::unique_ptr<AbstractArchiveMemberHeader>
686 Error *Err) const {
687 ErrorAsOutParameter ErrAsOutParam(Err);
688 if (kind() != K_AIXBIG)
689 return std::make_unique<ArchiveMemberHeader>(this, RawHeaderPtr, Size, Err);
690 return std::make_unique<BigArchiveMemberHeader>(this, RawHeaderPtr, Size,
691 Err);
692}
693
695 if (isThin())
696 return sizeof(ThinArchiveMagic) - 1;
697
698 if (Kind() == K_AIXBIG)
699 return sizeof(BigArchiveMagic) - 1;
700
701 return sizeof(ArchiveMagic) - 1;
702}
703
705 FirstRegularData = C.Data;
706 FirstRegularStartOfFile = C.StartOfFile;
707}
708
710 : Binary(Binary::ID_Archive, Source) {
711 ErrorAsOutParameter ErrAsOutParam(&Err);
712 StringRef Buffer = Data.getBuffer();
713 // Check for sufficient magic.
714 if (Buffer.startswith(ThinArchiveMagic)) {
715 IsThin = true;
716 } else if (Buffer.startswith(ArchiveMagic)) {
717 IsThin = false;
718 } else if (Buffer.startswith(BigArchiveMagic)) {
719 Format = K_AIXBIG;
720 IsThin = false;
721 return;
722 } else {
723 Err = make_error<GenericBinaryError>("file too small to be an archive",
725 return;
726 }
727
728 // Make sure Format is initialized before any call to
729 // ArchiveMemberHeader::getName() is made. This could be a valid empty
730 // archive which is the same in all formats. So claiming it to be gnu to is
731 // fine if not totally correct before we look for a string table or table of
732 // contents.
733 Format = K_GNU;
734
735 // Get the special members.
736 child_iterator I = child_begin(Err, false);
737 if (Err)
738 return;
740
741 // See if this is a valid empty archive and if so return.
742 if (I == E) {
743 Err = Error::success();
744 return;
745 }
746 const Child *C = &*I;
747
748 auto Increment = [&]() {
749 ++I;
750 if (Err)
751 return true;
752 C = &*I;
753 return false;
754 };
755
756 Expected<StringRef> NameOrErr = C->getRawName();
757 if (!NameOrErr) {
758 Err = NameOrErr.takeError();
759 return;
760 }
761 StringRef Name = NameOrErr.get();
762
763 // Below is the pattern that is used to figure out the archive format
764 // GNU archive format
765 // First member : / (may exist, if it exists, points to the symbol table )
766 // Second member : // (may exist, if it exists, points to the string table)
767 // Note : The string table is used if the filename exceeds 15 characters
768 // BSD archive format
769 // First member : __.SYMDEF or "__.SYMDEF SORTED" (the symbol table)
770 // There is no string table, if the filename exceeds 15 characters or has a
771 // embedded space, the filename has #1/<size>, The size represents the size
772 // of the filename that needs to be read after the archive header
773 // COFF archive format
774 // First member : /
775 // Second member : / (provides a directory of symbols)
776 // Third member : // (may exist, if it exists, contains the string table)
777 // Note: Microsoft PE/COFF Spec 8.3 says that the third member is present
778 // even if the string table is empty. However, lib.exe does not in fact
779 // seem to create the third member if there's no member whose filename
780 // exceeds 15 characters. So the third member is optional.
781
782 if (Name == "__.SYMDEF" || Name == "__.SYMDEF_64") {
783 if (Name == "__.SYMDEF")
784 Format = K_BSD;
785 else // Name == "__.SYMDEF_64"
786 Format = K_DARWIN64;
787 // We know that the symbol table is not an external file, but we still must
788 // check any Expected<> return value.
789 Expected<StringRef> BufOrErr = C->getBuffer();
790 if (!BufOrErr) {
791 Err = BufOrErr.takeError();
792 return;
793 }
794 SymbolTable = BufOrErr.get();
795 if (Increment())
796 return;
798
799 Err = Error::success();
800 return;
801 }
802
803 if (Name.startswith("#1/")) {
804 Format = K_BSD;
805 // We know this is BSD, so getName will work since there is no string table.
806 Expected<StringRef> NameOrErr = C->getName();
807 if (!NameOrErr) {
808 Err = NameOrErr.takeError();
809 return;
810 }
811 Name = NameOrErr.get();
812 if (Name == "__.SYMDEF SORTED" || Name == "__.SYMDEF") {
813 // We know that the symbol table is not an external file, but we still
814 // must check any Expected<> return value.
815 Expected<StringRef> BufOrErr = C->getBuffer();
816 if (!BufOrErr) {
817 Err = BufOrErr.takeError();
818 return;
819 }
820 SymbolTable = BufOrErr.get();
821 if (Increment())
822 return;
823 } else if (Name == "__.SYMDEF_64 SORTED" || Name == "__.SYMDEF_64") {
824 Format = K_DARWIN64;
825 // We know that the symbol table is not an external file, but we still
826 // must check any Expected<> return value.
827 Expected<StringRef> BufOrErr = C->getBuffer();
828 if (!BufOrErr) {
829 Err = BufOrErr.takeError();
830 return;
831 }
832 SymbolTable = BufOrErr.get();
833 if (Increment())
834 return;
835 }
837 return;
838 }
839
840 // MIPS 64-bit ELF archives use a special format of a symbol table.
841 // This format is marked by `ar_name` field equals to "/SYM64/".
842 // For detailed description see page 96 in the following document:
843 // http://techpubs.sgi.com/library/manuals/4000/007-4658-001/pdf/007-4658-001.pdf
844
845 bool has64SymTable = false;
846 if (Name == "/" || Name == "/SYM64/") {
847 // We know that the symbol table is not an external file, but we still
848 // must check any Expected<> return value.
849 Expected<StringRef> BufOrErr = C->getBuffer();
850 if (!BufOrErr) {
851 Err = BufOrErr.takeError();
852 return;
853 }
854 SymbolTable = BufOrErr.get();
855 if (Name == "/SYM64/")
856 has64SymTable = true;
857
858 if (Increment())
859 return;
860 if (I == E) {
861 Err = Error::success();
862 return;
863 }
864 Expected<StringRef> NameOrErr = C->getRawName();
865 if (!NameOrErr) {
866 Err = NameOrErr.takeError();
867 return;
868 }
869 Name = NameOrErr.get();
870 }
871
872 if (Name == "//") {
873 Format = has64SymTable ? K_GNU64 : K_GNU;
874 // The string table is never an external member, but we still
875 // must check any Expected<> return value.
876 Expected<StringRef> BufOrErr = C->getBuffer();
877 if (!BufOrErr) {
878 Err = BufOrErr.takeError();
879 return;
880 }
881 StringTable = BufOrErr.get();
882 if (Increment())
883 return;
885 Err = Error::success();
886 return;
887 }
888
889 if (Name[0] != '/') {
890 Format = has64SymTable ? K_GNU64 : K_GNU;
892 Err = Error::success();
893 return;
894 }
895
896 if (Name != "/") {
898 return;
899 }
900
901 Format = K_COFF;
902 // We know that the symbol table is not an external file, but we still
903 // must check any Expected<> return value.
904 Expected<StringRef> BufOrErr = C->getBuffer();
905 if (!BufOrErr) {
906 Err = BufOrErr.takeError();
907 return;
908 }
909 SymbolTable = BufOrErr.get();
910
911 if (Increment())
912 return;
913
914 if (I == E) {
916 Err = Error::success();
917 return;
918 }
919
920 NameOrErr = C->getRawName();
921 if (!NameOrErr) {
922 Err = NameOrErr.takeError();
923 return;
924 }
925 Name = NameOrErr.get();
926
927 if (Name == "//") {
928 // The string table is never an external member, but we still
929 // must check any Expected<> return value.
930 Expected<StringRef> BufOrErr = C->getBuffer();
931 if (!BufOrErr) {
932 Err = BufOrErr.takeError();
933 return;
934 }
935 StringTable = BufOrErr.get();
936 if (Increment())
937 return;
938
939 if (I == E) {
941 Err = Error::success();
942 return;
943 }
944
945 NameOrErr = C->getRawName();
946 if (!NameOrErr) {
947 Err = NameOrErr.takeError();
948 return;
949 }
950 Name = NameOrErr.get();
951 }
952
953 if (Name == "/<ECSYMBOLS>/") {
954 // ARM64EC-aware libraries contain an additional special member with
955 // an EC symbol map after the string table. Its format is similar to a
956 // regular symbol map, except it doesn't contain member offsets. Its indexes
957 // refer to member offsets from the regular symbol table instead.
958 Expected<StringRef> BufOrErr = C->getBuffer();
959 if (!BufOrErr) {
960 Err = BufOrErr.takeError();
961 return;
962 }
963 ECSymbolTable = BufOrErr.get();
964 if (Increment())
965 return;
966 }
967
969 Err = Error::success();
970}
971
973 Triple HostTriple(sys::getProcessTriple());
974 return HostTriple.isOSDarwin()
976 : (HostTriple.isOSAIX() ? object::Archive::K_AIXBIG
978}
979
981 bool SkipInternal) const {
982 if (isEmpty())
983 return child_end();
984
985 if (SkipInternal)
986 return child_iterator::itr(
987 Child(this, FirstRegularData, FirstRegularStartOfFile), Err);
988
989 const char *Loc = Data.getBufferStart() + getFirstChildOffset();
990 Child C(this, Loc, &Err);
991 if (Err)
992 return child_end();
993 return child_iterator::itr(C, Err);
994}
995
997 return child_iterator::end(Child(nullptr, nullptr, nullptr));
998}
999
1001 // Symbols use SymbolCount..SymbolCount+getNumberOfECSymbols() for EC symbol
1002 // indexes.
1003 uint32_t SymbolCount = Parent->getNumberOfSymbols();
1004 return SymbolCount <= SymbolIndex &&
1005 SymbolIndex < SymbolCount + Parent->getNumberOfECSymbols();
1006}
1007
1009 if (isECSymbol())
1010 return Parent->ECSymbolTable.begin() + StringIndex;
1011 return Parent->getSymbolTable().begin() + StringIndex;
1012}
1013
1015 const char *Buf = Parent->getSymbolTable().begin();
1016 const char *Offsets = Buf;
1017 if (Parent->kind() == K_GNU64 || Parent->kind() == K_DARWIN64 ||
1018 Parent->kind() == K_AIXBIG)
1019 Offsets += sizeof(uint64_t);
1020 else
1021 Offsets += sizeof(uint32_t);
1022 uint64_t Offset = 0;
1023 if (Parent->kind() == K_GNU) {
1024 Offset = read32be(Offsets + SymbolIndex * 4);
1025 } else if (Parent->kind() == K_GNU64 || Parent->kind() == K_AIXBIG) {
1026 Offset = read64be(Offsets + SymbolIndex * 8);
1027 } else if (Parent->kind() == K_BSD) {
1028 // The SymbolIndex is an index into the ranlib structs that start at
1029 // Offsets (the first uint32_t is the number of bytes of the ranlib
1030 // structs). The ranlib structs are a pair of uint32_t's the first
1031 // being a string table offset and the second being the offset into
1032 // the archive of the member that defines the symbol. Which is what
1033 // is needed here.
1034 Offset = read32le(Offsets + SymbolIndex * 8 + 4);
1035 } else if (Parent->kind() == K_DARWIN64) {
1036 // The SymbolIndex is an index into the ranlib_64 structs that start at
1037 // Offsets (the first uint64_t is the number of bytes of the ranlib_64
1038 // structs). The ranlib_64 structs are a pair of uint64_t's the first
1039 // being a string table offset and the second being the offset into
1040 // the archive of the member that defines the symbol. Which is what
1041 // is needed here.
1042 Offset = read64le(Offsets + SymbolIndex * 16 + 8);
1043 } else {
1044 // Skip offsets.
1045 uint32_t MemberCount = read32le(Buf);
1046 Buf += MemberCount * 4 + 4;
1047
1048 uint32_t SymbolCount = read32le(Buf);
1049 uint16_t OffsetIndex;
1050 if (SymbolIndex < SymbolCount) {
1051 // Skip SymbolCount to get to the indices table.
1052 const char *Indices = Buf + 4;
1053
1054 // Get the index of the offset in the file member offset table for this
1055 // symbol.
1056 OffsetIndex = read16le(Indices + SymbolIndex * 2);
1057 } else if (isECSymbol()) {
1058 // Skip SymbolCount to get to the indices table.
1059 const char *Indices = Parent->ECSymbolTable.begin() + 4;
1060
1061 // Get the index of the offset in the file member offset table for this
1062 // symbol.
1063 OffsetIndex = read16le(Indices + (SymbolIndex - SymbolCount) * 2);
1064 } else {
1066 }
1067 // Subtract 1 since OffsetIndex is 1 based.
1068 --OffsetIndex;
1069
1070 if (OffsetIndex >= MemberCount)
1072
1073 Offset = read32le(Offsets + OffsetIndex * 4);
1074 }
1075
1076 const char *Loc = Parent->getData().begin() + Offset;
1077 Error Err = Error::success();
1078 Child C(Parent, Loc, &Err);
1079 if (Err)
1080 return std::move(Err);
1081 return C;
1082}
1083
1085 Symbol t(*this);
1086 if (Parent->kind() == K_BSD) {
1087 // t.StringIndex is an offset from the start of the __.SYMDEF or
1088 // "__.SYMDEF SORTED" member into the string table for the ranlib
1089 // struct indexed by t.SymbolIndex . To change t.StringIndex to the
1090 // offset in the string table for t.SymbolIndex+1 we subtract the
1091 // its offset from the start of the string table for t.SymbolIndex
1092 // and add the offset of the string table for t.SymbolIndex+1.
1093
1094 // The __.SYMDEF or "__.SYMDEF SORTED" member starts with a uint32_t
1095 // which is the number of bytes of ranlib structs that follow. The ranlib
1096 // structs are a pair of uint32_t's the first being a string table offset
1097 // and the second being the offset into the archive of the member that
1098 // define the symbol. After that the next uint32_t is the byte count of
1099 // the string table followed by the string table.
1100 const char *Buf = Parent->getSymbolTable().begin();
1101 uint32_t RanlibCount = 0;
1102 RanlibCount = read32le(Buf) / 8;
1103 // If t.SymbolIndex + 1 will be past the count of symbols (the RanlibCount)
1104 // don't change the t.StringIndex as we don't want to reference a ranlib
1105 // past RanlibCount.
1106 if (t.SymbolIndex + 1 < RanlibCount) {
1107 const char *Ranlibs = Buf + 4;
1108 uint32_t CurRanStrx = 0;
1109 uint32_t NextRanStrx = 0;
1110 CurRanStrx = read32le(Ranlibs + t.SymbolIndex * 8);
1111 NextRanStrx = read32le(Ranlibs + (t.SymbolIndex + 1) * 8);
1112 t.StringIndex -= CurRanStrx;
1113 t.StringIndex += NextRanStrx;
1114 }
1115 } else if (t.isECSymbol()) {
1116 // Go to one past next null.
1117 t.StringIndex = Parent->ECSymbolTable.find('\0', t.StringIndex) + 1;
1118 } else {
1119 // Go to one past next null.
1120 t.StringIndex = Parent->getSymbolTable().find('\0', t.StringIndex) + 1;
1121 }
1122 ++t.SymbolIndex;
1123 return t;
1124}
1125
1127 if (!hasSymbolTable())
1128 return symbol_iterator(Symbol(this, 0, 0));
1129
1130 const char *buf = getSymbolTable().begin();
1131 if (kind() == K_GNU) {
1132 uint32_t symbol_count = 0;
1133 symbol_count = read32be(buf);
1134 buf += sizeof(uint32_t) + (symbol_count * (sizeof(uint32_t)));
1135 } else if (kind() == K_GNU64) {
1136 uint64_t symbol_count = read64be(buf);
1137 buf += sizeof(uint64_t) + (symbol_count * (sizeof(uint64_t)));
1138 } else if (kind() == K_BSD) {
1139 // The __.SYMDEF or "__.SYMDEF SORTED" member starts with a uint32_t
1140 // which is the number of bytes of ranlib structs that follow. The ranlib
1141 // structs are a pair of uint32_t's the first being a string table offset
1142 // and the second being the offset into the archive of the member that
1143 // define the symbol. After that the next uint32_t is the byte count of
1144 // the string table followed by the string table.
1145 uint32_t ranlib_count = 0;
1146 ranlib_count = read32le(buf) / 8;
1147 const char *ranlibs = buf + 4;
1148 uint32_t ran_strx = 0;
1149 ran_strx = read32le(ranlibs);
1150 buf += sizeof(uint32_t) + (ranlib_count * (2 * (sizeof(uint32_t))));
1151 // Skip the byte count of the string table.
1152 buf += sizeof(uint32_t);
1153 buf += ran_strx;
1154 } else if (kind() == K_DARWIN64) {
1155 // The __.SYMDEF_64 or "__.SYMDEF_64 SORTED" member starts with a uint64_t
1156 // which is the number of bytes of ranlib_64 structs that follow. The
1157 // ranlib_64 structs are a pair of uint64_t's the first being a string
1158 // table offset and the second being the offset into the archive of the
1159 // member that define the symbol. After that the next uint64_t is the byte
1160 // count of the string table followed by the string table.
1161 uint64_t ranlib_count = 0;
1162 ranlib_count = read64le(buf) / 16;
1163 const char *ranlibs = buf + 8;
1164 uint64_t ran_strx = 0;
1165 ran_strx = read64le(ranlibs);
1166 buf += sizeof(uint64_t) + (ranlib_count * (2 * (sizeof(uint64_t))));
1167 // Skip the byte count of the string table.
1168 buf += sizeof(uint64_t);
1169 buf += ran_strx;
1170 } else if (kind() == K_AIXBIG) {
1171 buf = getStringTable().begin();
1172 } else {
1173 uint32_t member_count = 0;
1174 uint32_t symbol_count = 0;
1175 member_count = read32le(buf);
1176 buf += 4 + (member_count * 4); // Skip offsets.
1177 symbol_count = read32le(buf);
1178 buf += 4 + (symbol_count * 2); // Skip indices.
1179 }
1180 uint32_t string_start_offset = buf - getSymbolTable().begin();
1181 return symbol_iterator(Symbol(this, 0, string_start_offset));
1182}
1183
1185 return symbol_iterator(Symbol(this, getNumberOfSymbols(), 0));
1186}
1187
1189 uint32_t Count = 0;
1190
1191 // Validate EC symbol table.
1192 if (!ECSymbolTable.empty()) {
1193 if (ECSymbolTable.size() < sizeof(uint32_t))
1194 return malformedError("invalid EC symbols size (" +
1195 Twine(ECSymbolTable.size()) + ")");
1196 if (SymbolTable.size() < sizeof(uint32_t))
1197 return malformedError("invalid symbols size (" +
1198 Twine(ECSymbolTable.size()) + ")");
1199
1200 Count = read32le(ECSymbolTable.begin());
1201 size_t StringIndex = sizeof(uint32_t) + Count * sizeof(uint16_t);
1202 if (ECSymbolTable.size() < StringIndex)
1203 return malformedError("invalid EC symbols size. Size was " +
1204 Twine(ECSymbolTable.size()) + ", but expected " +
1205 Twine(StringIndex));
1206
1207 uint32_t MemberCount = read32le(SymbolTable.begin());
1208 const char *Indexes = ECSymbolTable.begin() + sizeof(uint32_t);
1209
1210 for (uint32_t i = 0; i < Count; ++i) {
1211 uint16_t Index = read16le(Indexes + i * sizeof(uint16_t));
1212 if (!Index)
1213 return malformedError("invalid EC symbol index 0");
1214 if (Index > MemberCount)
1215 return malformedError("invalid EC symbol index " + Twine(Index) +
1216 " is larger than member count " +
1217 Twine(MemberCount));
1218
1219 StringIndex = ECSymbolTable.find('\0', StringIndex);
1220 if (StringIndex == StringRef::npos)
1221 return malformedError("malformed EC symbol names: not null-terminated");
1222 ++StringIndex;
1223 }
1224 }
1225
1226 uint32_t SymbolCount = getNumberOfSymbols();
1227 return make_range(
1228 symbol_iterator(Symbol(this, SymbolCount,
1229 sizeof(uint32_t) + Count * sizeof(uint16_t))),
1230 symbol_iterator(Symbol(this, SymbolCount + Count, 0)));
1231}
1232
1234 if (!hasSymbolTable())
1235 return 0;
1236 const char *buf = getSymbolTable().begin();
1237 if (kind() == K_GNU)
1238 return read32be(buf);
1239 if (kind() == K_GNU64 || kind() == K_AIXBIG)
1240 return read64be(buf);
1241 if (kind() == K_BSD)
1242 return read32le(buf) / 8;
1243 if (kind() == K_DARWIN64)
1244 return read64le(buf) / 16;
1245 uint32_t member_count = 0;
1246 member_count = read32le(buf);
1247 buf += 4 + (member_count * 4); // Skip offsets.
1248 return read32le(buf);
1249}
1250
1252 if (ECSymbolTable.size() < sizeof(uint32_t))
1253 return 0;
1254 return read32le(ECSymbolTable.begin());
1255}
1256
1260
1261 for (; bs != es; ++bs) {
1262 StringRef SymName = bs->getName();
1263 if (SymName == name) {
1264 if (auto MemberOrErr = bs->getMember())
1265 return Child(*MemberOrErr);
1266 else
1267 return MemberOrErr.takeError();
1268 }
1269 }
1270 return std::nullopt;
1271}
1272
1273// Returns true if archive file contains no member file.
1274bool Archive::isEmpty() const {
1276}
1277
1278bool Archive::hasSymbolTable() const { return !SymbolTable.empty(); }
1279
1281 uint64_t GlobalSymtabOffset,
1282 const char *&GlobalSymtabLoc,
1283 uint64_t &Size, const char *BitMessage) {
1284 uint64_t BufferSize = Data.getBufferSize();
1285 uint64_t GlobalSymtabContentOffset =
1286 GlobalSymtabOffset + sizeof(BigArMemHdrType);
1287 if (GlobalSymtabContentOffset > BufferSize)
1288 return malformedError(
1289 Twine(BitMessage) + " global symbol table header at offset 0x" +
1290 Twine::utohexstr(GlobalSymtabOffset) + " and size 0x" +
1292 " goes past the end of file");
1293
1294 GlobalSymtabLoc = Data.getBufferStart() + GlobalSymtabOffset;
1295 const BigArMemHdrType *GlobalSymHdr =
1296 reinterpret_cast<const BigArMemHdrType *>(GlobalSymtabLoc);
1297 StringRef RawOffset = getFieldRawString(GlobalSymHdr->Size);
1298 if (RawOffset.getAsInteger(10, Size))
1299 return malformedError(Twine(BitMessage) + " global symbol table size \"" +
1300 RawOffset + "\" is not a number");
1301
1302 if (GlobalSymtabContentOffset + Size > BufferSize)
1303 return malformedError(
1304 Twine(BitMessage) + " global symbol table content at offset 0x" +
1305 Twine::utohexstr(GlobalSymtabContentOffset) + " and size 0x" +
1306 Twine::utohexstr(Size) + " goes past the end of file");
1307
1308 return Error::success();
1309}
1310
1316};
1317
1318static void
1320 const char *GlobalSymtabLoc, uint64_t Size) {
1321 // In a big archive, a global symbol table contains the following information:
1322 // - The number of symbols.
1323 // - The array of offsets into the archive file. The length is eight
1324 // times the number of symbols.
1325 // - The name-string table. The size is:
1326 // Size-(8*(the number of symbols + 1)).
1327
1329 StringRef(GlobalSymtabLoc + sizeof(BigArMemHdrType), Size);
1330 uint64_t SymNum = read64be(GlobalSymtabLoc + sizeof(BigArMemHdrType));
1331 StringRef SymbolOffsetTable = StringRef(SymbolTable.data() + 8, 8 * SymNum);
1332 unsigned SymOffsetsSize = 8 * (SymNum + 1);
1333 uint64_t SymbolTableStringSize = Size - SymOffsetsSize;
1335 StringRef(SymbolTable.data() + SymOffsetsSize, SymbolTableStringSize);
1336 SymtabInfos.push_back({SymNum, SymbolTable, SymbolOffsetTable, StringTable});
1337}
1338
1340 : Archive(Source, Err) {
1341 ErrorAsOutParameter ErrAsOutParam(&Err);
1342 StringRef Buffer = Data.getBuffer();
1343 ArFixLenHdr = reinterpret_cast<const FixLenHdr *>(Buffer.data());
1344 uint64_t BufferSize = Data.getBufferSize();
1345
1346 if (BufferSize < sizeof(FixLenHdr)) {
1347 Err = malformedError("malformed AIX big archive: incomplete fixed length "
1348 "header, the archive is only" +
1349 Twine(BufferSize) + " byte(s)");
1350 return;
1351 }
1352
1354 if (RawOffset.getAsInteger(10, FirstChildOffset))
1355 // TODO: Out-of-line.
1356 Err = malformedError("malformed AIX big archive: first member offset \"" +
1357 RawOffset + "\" is not a number");
1358
1360 if (RawOffset.getAsInteger(10, LastChildOffset))
1361 // TODO: Out-of-line.
1362 Err = malformedError("malformed AIX big archive: last member offset \"" +
1363 RawOffset + "\" is not a number");
1364
1365 uint64_t GlobSymtab32Offset = 0;
1367 if (RawOffset.getAsInteger(10, GlobSymtab32Offset)) {
1368 Err = malformedError("global symbol table "
1369 "offset of 32-bit members \"" +
1370 RawOffset + "\" is not a number");
1371 return;
1372 }
1373
1374 uint64_t GlobSymtab64Offset = 0;
1376 if (RawOffset.getAsInteger(10, GlobSymtab64Offset)) {
1377 Err = malformedError("global symbol table "
1378 "offset of 64-bit members\"" +
1379 RawOffset + "\" is not a number");
1380 return;
1381 }
1382
1383 const char *GlobSymtab32Loc = nullptr;
1384 const char *GlobSymtab64Loc = nullptr;
1385 uint64_t GlobSymtab32Size = 0;
1386 uint64_t GlobSymtab64Size = 0;
1387 const MemoryBufferRef &MemBuffRef = getMemoryBufferRef();
1388
1389 if (GlobSymtab32Offset) {
1390 Err =
1391 getGlobalSymtabLocAndSize(MemBuffRef, GlobSymtab32Offset,
1392 GlobSymtab32Loc, GlobSymtab32Size, "32-bit");
1393 if (Err)
1394 return;
1395
1396 Has32BitGlobalSymtab = true;
1397 }
1398
1399 if (GlobSymtab64Offset) {
1400 Err =
1401 getGlobalSymtabLocAndSize(MemBuffRef, GlobSymtab64Offset,
1402 GlobSymtab64Loc, GlobSymtab64Size, "64-bit");
1403 if (Err)
1404 return;
1405
1406 Has64BitGlobalSymtab = true;
1407 }
1408
1410
1411 if (GlobSymtab32Offset)
1412 appendGlobalSymbolTableInfo(SymtabInfos, GlobSymtab32Loc, GlobSymtab32Size);
1413 if (GlobSymtab64Offset)
1414 appendGlobalSymbolTableInfo(SymtabInfos, GlobSymtab64Loc, GlobSymtab64Size);
1415
1416 if (SymtabInfos.size() == 1) {
1417 SymbolTable = SymtabInfos[0].SymbolTable;
1418 StringTable = SymtabInfos[0].StringTable;
1419 } else if (SymtabInfos.size() == 2) {
1420 // In order to let the Archive::Symbol::getNext() work for both 32-bit and
1421 // 64-bit global symbol tables, we need to merge them into a single table.
1423 uint64_t SymNum = SymtabInfos[0].SymNum + SymtabInfos[1].SymNum;
1424 write(Out, SymNum, support::big);
1425 // Merge symbol offset.
1426 Out << SymtabInfos[0].SymbolOffsetTable;
1427 Out << SymtabInfos[1].SymbolOffsetTable;
1428 // Merge string table.
1429 Out << SymtabInfos[0].StringTable;
1430 Out << SymtabInfos[1].StringTable;
1432 // The size of the symbol offset to the member file is 8 bytes.
1433 StringTable = StringRef(SymbolTable.begin() + (SymNum + 1) * 8,
1434 SymtabInfos[0].StringTable.size() +
1435 SymtabInfos[1].StringTable.size());
1436 }
1437
1438 child_iterator I = child_begin(Err, false);
1439 if (Err)
1440 return;
1442 if (I == E) {
1443 Err = Error::success();
1444 return;
1445 }
1447 Err = Error::success();
1448}
aarch64 promote const
#define offsetof(TYPE, MEMBER)
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
std::string Name
uint64_t Size
bool End
Definition: ELF_riscv.cpp:469
Provides ErrorOr<T> smart pointer.
#define I(x, y, z)
Definition: MD5.cpp:58
LLVMContext & Context
static void appendGlobalSymbolTableInfo(SmallVector< GlobalSymtabInfo > &SymtabInfos, const char *GlobalSymtabLoc, uint64_t Size)
Definition: Archive.cpp:1319
static Error getGlobalSymtabLocAndSize(const MemoryBufferRef &Data, uint64_t GlobalSymtabOffset, const char *&GlobalSymtabLoc, uint64_t &Size, const char *BitMessage)
Definition: Archive.cpp:1280
static Error malformedError(Twine Msg)
Definition: Archive.cpp:44
StringRef getFieldRawString(const T(&Field)[N])
Definition: Archive.cpp:66
Expected< uint64_t > getArchiveMemberOctField(Twine FieldName, const StringRef RawField, const Archive *Parent, const AbstractArchiveMemberHeader *MemHeader)
Definition: Archive.cpp:199
Expected< uint64_t > getArchiveMemberDecField(Twine FieldName, const StringRef RawField, const Archive *Parent, const AbstractArchiveMemberHeader *MemHeader)
Definition: Archive.cpp:181
static Error createMemberHeaderParseError(const AbstractArchiveMemberHeader *ArMemHeader, const char *RawHeaderPtr, uint64_t Size)
Definition: Archive.cpp:51
static StringRef getName(Value *V)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static const char * name
Definition: SMEABIPass.cpp:49
raw_pwrite_stream & OS
This file defines the SmallString class.
static unsigned getSize(unsigned Kind)
Helper for Errors used as out-parameters.
Definition: Error.h:1102
Represents either an error or a value T.
Definition: ErrorOr.h:56
std::error_code getError() const
Definition: ErrorOr.h:152
Lightweight error class with error context and mandatory checking.
Definition: Error.h:160
static ErrorSuccess success()
Create a success value.
Definition: Error.h:334
Tagged union holding either a T or a Error.
Definition: Error.h:474
Error takeError()
Take ownership of the stored error.
Definition: Error.h:601
reference get()
Returns a reference to the stored T value.
Definition: Error.h:571
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:67
size_t getBufferSize() const
const char * getBufferStart() const
StringRef getBuffer() const
const char * getBufferEnd() const
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFile(const Twine &Filename, bool IsText=false, bool RequiresNullTerminator=true, bool IsVolatile=false, std::optional< Align > Alignment=std::nullopt)
Open the specified file as a MemoryBuffer, returning a new MemoryBuffer if successful,...
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition: SmallString.h:26
StringRef str() const
Explicit conversion to StringRef.
Definition: SmallString.h:261
size_t size() const
Definition: SmallVector.h:91
void push_back(const T &Elt)
Definition: SmallVector.h:416
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1200
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
bool getAsInteger(unsigned Radix, T &Result) const
Parse the current string as an integer of the specified radix.
Definition: StringRef.h:474
constexpr bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:134
iterator begin() const
Definition: StringRef.h:111
size_t size_type
Definition: StringRef.h:56
StringRef slice(size_t Start, size_t End) const
Return a reference to the substring from [Start, End).
Definition: StringRef.h:688
constexpr size_t size() const
size - Get the string size.
Definition: StringRef.h:137
bool startswith(StringRef Prefix) const
Definition: StringRef.h:261
iterator end() const
Definition: StringRef.h:113
StringRef rtrim(char Char) const
Return string with consecutive Char characters starting from the right removed.
Definition: StringRef.h:807
size_t find(char C, size_t From=0) const
Search for the first character C in the string.
Definition: StringRef.h:301
bool endswith(StringRef Suffix) const
Definition: StringRef.h:280
static constexpr size_t npos
Definition: StringRef.h:52
const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition: StringRef.h:131
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
bool isOSAIX() const
Tests whether the OS is AIX.
Definition: Triple.h:670
bool isOSDarwin() const
Is this a "Darwin" OS (macOS, iOS, tvOS, watchOS, or DriverKit).
Definition: Triple.h:518
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
static Twine utohexstr(const uint64_t &Val)
Definition: Twine.h:416
LLVM Value Representation.
Definition: Value.h:74
A wrapper class for fallible iterators.
static fallible_iterator end(Underlying I)
Construct a fallible iterator that can be used as an end-of-range value.
static fallible_iterator itr(Underlying I, Error &Err)
Construct a fallible iterator that cannot be used as an end-of-range value.
virtual StringRef getRawGID() const =0
virtual StringRef getRawUID() const =0
Expected< unsigned > getUID() const
Definition: Archive.cpp:404
Expected< sys::fs::perms > getAccessMode() const
Definition: Archive.cpp:385
Expected< unsigned > getGID() const
Definition: Archive.cpp:411
virtual uint64_t getOffset() const =0
Expected< sys::TimePoint< std::chrono::seconds > > getLastModified() const
Definition: Archive.cpp:394
virtual StringRef getRawAccessMode() const =0
virtual StringRef getRawLastModified() const =0
virtual Expected< StringRef > getName(uint64_t Size) const =0
Get the name looking up long names.
ArchiveMemberHeader(const Archive *Parent, const char *RawHeaderPtr, uint64_t Size, Error *Err)
Definition: Archive.cpp:95
Expected< StringRef > getName(uint64_t Size) const override
Get the name looking up long names.
Definition: Archive.cpp:245
Expected< StringRef > getRawName() const override
Get the name without looking up long names.
Definition: Archive.cpp:155
Expected< bool > isThin() const override
Definition: Archive.cpp:418
Expected< uint64_t > getSize() const override
Definition: Archive.cpp:357
Expected< const char * > getNextChildLoc() const override
Get next file member location.
Definition: Archive.cpp:426
Expected< StringRef > getBuffer() const
Definition: Archive.cpp:573
Expected< Child > getNext() const
Definition: Archive.cpp:595
Expected< std::string > getFullName() const
Definition: Archive.cpp:555
uint64_t getChildOffset() const
Definition: Archive.cpp:626
Expected< uint64_t > getRawSize() const
Definition: Archive.cpp:549
Expected< StringRef > getName() const
Definition: Archive.cpp:633
Expected< uint64_t > getSize() const
Definition: Archive.cpp:543
Expected< StringRef > getRawName() const
Definition: Archive.h:225
Expected< std::unique_ptr< Binary > > getAsBinary(LLVMContext *Context=nullptr) const
Definition: Archive.cpp:658
Expected< MemoryBufferRef > getMemoryBufferRef() const
Definition: Archive.cpp:646
Child(const Archive *Parent, const char *Start, Error *Err)
Definition: Archive.cpp:468
Expected< Child > getMember() const
Definition: Archive.cpp:1014
StringRef getName() const
Definition: Archive.cpp:1008
std::unique_ptr< AbstractArchiveMemberHeader > createArchiveMemberHeader(const char *RawHeaderPtr, uint64_t Size, Error *Err) const
Definition: Archive.cpp:685
symbol_iterator symbol_begin() const
Definition: Archive.cpp:1126
virtual uint64_t getFirstChildOffset() const
Definition: Archive.h:369
bool isThin() const
Definition: Archive.h:340
StringRef getStringTable() const
Definition: Archive.h:366
uint32_t getNumberOfSymbols() const
Definition: Archive.cpp:1233
uint32_t getNumberOfECSymbols() const
Definition: Archive.cpp:1251
void setFirstRegular(const Child &C)
Definition: Archive.cpp:704
uint64_t getArchiveMagicLen() const
Definition: Archive.cpp:694
StringRef getSymbolTable() const
Definition: Archive.h:365
StringRef ECSymbolTable
Definition: Archive.h:384
symbol_iterator symbol_end() const
Definition: Archive.cpp:1184
virtual bool isEmpty() const
Definition: Archive.cpp:1274
child_iterator child_end() const
Definition: Archive.cpp:996
bool hasSymbolTable() const
Definition: Archive.cpp:1278
StringRef StringTable
Definition: Archive.h:385
Archive(MemoryBufferRef Source, Error &Err)
Definition: Archive.cpp:709
Kind kind() const
Definition: Archive.h:339
StringRef SymbolTable
Definition: Archive.h:383
Expected< iterator_range< symbol_iterator > > ec_symbols() const
Definition: Archive.cpp:1188
Expected< std::optional< Child > > findSym(StringRef name) const
Definition: Archive.cpp:1257
child_iterator child_begin(Error &Err, bool SkipInternal=true) const
Definition: Archive.cpp:980
static object::Archive::Kind getDefaultKindForHost()
Definition: Archive.cpp:972
static Expected< std::unique_ptr< Archive > > create(MemoryBufferRef Source)
Definition: Archive.cpp:669
Expected< uint64_t > getSize() const override
Definition: Archive.cpp:362
Expected< StringRef > getRawName() const override
Get the name without looking up long names.
Definition: Archive.cpp:216
Expected< const char * > getNextChildLoc() const override
Get next file member location.
Definition: Archive.cpp:451
Expected< StringRef > getName(uint64_t Size) const override
Get the name looking up long names.
Definition: Archive.cpp:353
Expected< uint64_t > getRawNameSize() const
Definition: Archive.cpp:375
Expected< uint64_t > getNextOffset() const
Definition: Archive.cpp:380
BigArchiveMemberHeader(Archive const *Parent, const char *RawHeaderPtr, uint64_t Size, Error *Err)
Definition: Archive.cpp:130
std::string MergedGlobalSymtabBuf
Definition: Archive.h:413
const FixLenHdr * ArFixLenHdr
Definition: Archive.h:410
uint64_t FirstChildOffset
Definition: Archive.h:411
uint64_t LastChildOffset
Definition: Archive.h:412
BigArchive(MemoryBufferRef Source, Error &Err)
Definition: Archive.cpp:1339
MemoryBufferRef Data
Definition: Binary.h:37
StringRef getData() const
Definition: Binary.cpp:39
MemoryBufferRef getMemoryBufferRef() const
Definition: Binary.cpp:43
StringRef getRawLastModified() const override
Definition: Archive.cpp:76
StringRef getRawUID() const override
Definition: Archive.cpp:80
uint64_t getOffset() const override
Definition: Archive.cpp:88
StringRef getRawAccessMode() const override
Definition: Archive.cpp:71
StringRef getRawGID() const override
Definition: Archive.cpp:84
raw_ostream & write_escaped(StringRef Str, bool UseHexEscapes=false)
Output Str, turning '\', '\t', ' ', '"', and anything that doesn't satisfy llvm::isPrint into an esca...
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:642
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
constexpr size_t NameSize
Definition: XCOFF.h:29
const char ArchiveMagic[]
Definition: Archive.h:33
const char ThinArchiveMagic[]
Definition: Archive.h:34
const char BigArchiveMagic[]
Definition: Archive.h:35
Expected< std::unique_ptr< Binary > > createBinary(MemoryBufferRef Source, LLVMContext *Context=nullptr, bool InitContent=true)
Create a Binary from Source, autodetecting the file type.
Definition: Binary.cpp:45
uint64_t read64le(const void *P)
Definition: Endian.h:382
uint16_t read16le(const void *P)
Definition: Endian.h:376
uint64_t read64be(const void *P)
Definition: Endian.h:387
uint32_t read32be(const void *P)
Definition: Endian.h:386
uint32_t read32le(const void *P)
Definition: Endian.h:379
StringRef parent_path(StringRef path, Style style=Style::native)
Get parent path.
Definition: Path.cpp:469
bool is_absolute(const Twine &path, Style style=Style::native)
Is path absolute?
Definition: Path.cpp:673
void append(SmallVectorImpl< char > &path, const Twine &a, const Twine &b="", const Twine &c="", const Twine &d="")
Append to path.
Definition: Path.cpp:458
TimePoint< std::chrono::seconds > toTimePoint(std::time_t T)
Convert a std::time_t to a TimePoint.
Definition: Chrono.h:45
std::string getProcessTriple()
getProcessTriple() - Return an appropriate target triple for generating code to be loaded into the cu...
Definition: Host.cpp:1945
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:440
Error createFileError(const Twine &F, Error E)
Concatenate a source file path and/or name with an Error.
Definition: Error.h:1325
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
Error write(MCStreamer &Out, ArrayRef< std::string > Inputs, bool ContinueOnCuIndexOverflow)
Definition: DWP.cpp:585
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
Definition: Alignment.h:155
Error errorCodeToError(std::error_code EC)
Helper for converting an std::error_code to a Error.
Definition: Error.cpp:103
void consumeError(Error Err)
Consume a Error without doing anything.
Definition: Error.h:1041
#define N
StringRef StringTable
Definition: Archive.cpp:1315
StringRef SymbolTable
Definition: Archive.cpp:1313
uint64_t SymNum
Definition: Archive.cpp:1312
StringRef SymbolOffsetTable
Definition: Archive.cpp:1314
Fixed-Length Header.
Definition: Archive.h:399
char FirstChildOffset[20]
Offset to first archive member.
Definition: Archive.h:405
char LastChildOffset[20]
Offset to last archive member.
Definition: Archive.h:406
char GlobSym64Offset[20]
Offset global symbol table for 64-bit objects.
Definition: Archive.h:404
char GlobSymOffset[20]
Offset to global symbol table.
Definition: Archive.h:402
char Size[10]
Size of data, not including header or padding.
Definition: Archive.h:98