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