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