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