LLVM 24.0.0git
ArchiveWriter.cpp
Go to the documentation of this file.
1//===- ArchiveWriter.cpp - ar File Format implementation --------*- C++ -*-===//
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 writeArchive function.
10//
11//===----------------------------------------------------------------------===//
12
14#include "llvm/ADT/ArrayRef.h"
15#include "llvm/ADT/StringMap.h"
16#include "llvm/ADT/StringRef.h"
18#include "llvm/IR/LLVMContext.h"
19#include "llvm/Object/Archive.h"
20#include "llvm/Object/COFF.h"
22#include "llvm/Object/Error.h"
25#include "llvm/Object/MachO.h"
31#include "llvm/Support/Errc.h"
33#include "llvm/Support/Format.h"
35#include "llvm/Support/Path.h"
38
39#include <cerrno>
40#include <map>
41
42#if !defined(_MSC_VER) && !defined(__MINGW32__)
43#include <unistd.h>
44#else
45#include <io.h>
46#endif
47
48using namespace llvm;
49using namespace llvm::object;
50
51struct SymMap {
52 bool UseECMap = false;
53 std::map<std::string, uint16_t> Map;
54 std::map<std::string, uint16_t> ECMap;
55};
56
58 : Buf(MemoryBuffer::getMemBuffer(BufRef, false)),
59 MemberName(BufRef.getBufferIdentifier()) {}
60
62 auto MemBufferRef = this->Buf->getMemBufferRef();
65
66 if (OptionalObject) {
67 if (isa<object::MachOObjectFile>(**OptionalObject))
69 if (isa<object::XCOFFObjectFile>(**OptionalObject))
71 if (isa<object::COFFObjectFile>(**OptionalObject) ||
72 isa<object::COFFImportFile>(**OptionalObject))
74 if (isa<object::GOFFObjectFile>(**OptionalObject))
77 }
78
79 // Squelch the error in case we had a non-object file.
80 consumeError(OptionalObject.takeError());
81
82 // If we're adding a bitcode file to the archive, detect the Archive kind
83 // based on the target triple.
84 LLVMContext Context;
85 if (identify_magic(MemBufferRef.getBuffer()) == file_magic::bitcode) {
87 MemBufferRef, file_magic::bitcode, &Context)) {
88 auto &IRObject = cast<object::IRObjectFile>(**ObjOrErr);
89 auto TargetTriple = Triple(IRObject.getTargetTriple());
91 } else {
92 // Squelch the error in case this was not a SymbolicFile.
93 consumeError(ObjOrErr.takeError());
94 }
95 }
96
98}
99
102 bool Deterministic) {
104 if (!BufOrErr)
105 return BufOrErr.takeError();
106
108 M.Buf = MemoryBuffer::getMemBuffer(*BufOrErr, false);
109 M.MemberName = M.Buf->getBufferIdentifier();
110 if (!Deterministic) {
111 auto ModTimeOrErr = OldMember.getLastModified();
112 if (!ModTimeOrErr)
113 return ModTimeOrErr.takeError();
114 M.ModTime = ModTimeOrErr.get();
115 Expected<unsigned> UIDOrErr = OldMember.getUID();
116 if (!UIDOrErr)
117 return UIDOrErr.takeError();
118 M.UID = UIDOrErr.get();
119 Expected<unsigned> GIDOrErr = OldMember.getGID();
120 if (!GIDOrErr)
121 return GIDOrErr.takeError();
122 M.GID = GIDOrErr.get();
123 Expected<sys::fs::perms> AccessModeOrErr = OldMember.getAccessMode();
124 if (!AccessModeOrErr)
125 return AccessModeOrErr.takeError();
126 M.Perms = AccessModeOrErr.get();
127 }
128 return std::move(M);
129}
130
132 bool Deterministic) {
134 auto FDOrErr = sys::fs::openNativeFileForRead(FileName);
135 if (!FDOrErr)
136 return FDOrErr.takeError();
137 sys::fs::file_t FD = *FDOrErr;
139
140 if (auto EC = sys::fs::status(FD, Status))
141 return errorCodeToError(EC);
142
143 // Opening a directory doesn't make sense. Let it fail.
144 // Linux cannot open directories with open(2), although
145 // cygwin and *bsd can.
148
149 ErrorOr<std::unique_ptr<MemoryBuffer>> MemberBufferOrErr =
150 MemoryBuffer::getOpenFile(FD, FileName, Status.getSize(), false);
151 if (!MemberBufferOrErr)
152 return errorCodeToError(MemberBufferOrErr.getError());
153
154 if (auto EC = sys::fs::closeFile(FD))
155 return errorCodeToError(EC);
156
158 M.Buf = std::move(*MemberBufferOrErr);
159 M.MemberName = M.Buf->getBufferIdentifier();
160 if (!Deterministic) {
161 M.ModTime = std::chrono::time_point_cast<std::chrono::seconds>(
162 Status.getLastModificationTime());
163 M.UID = Status.getUser();
164 M.GID = Status.getGroup();
165 M.Perms = Status.permissions();
166 }
167 return std::move(M);
168}
169
170template <typename T>
171static void printWithSpacePadding(raw_ostream &OS, T Data, unsigned Size) {
172 uint64_t OldPos = OS.tell();
173 OS << Data;
174 unsigned SizeSoFar = OS.tell() - OldPos;
175 assert(SizeSoFar <= Size && "Data doesn't fit in Size");
176 OS.indent(Size - SizeSoFar);
177}
178
183
187
191
195
197 switch (Kind) {
203 return false;
207 return true;
208 }
209 llvm_unreachable("not supported for writting");
210}
211
212template <class T>
218
219template <class T> static void printLE(raw_ostream &Out, T Val) {
221}
222
225 unsigned UID, unsigned GID, unsigned Perms, uint64_t Size) {
226 printWithSpacePadding(Out, sys::toTimeT(ModTime), 12);
227
228 // The format has only 6 chars for uid and gid. Truncate if the provided
229 // values don't fit.
230 printWithSpacePadding(Out, UID % 1000000, 6);
231 printWithSpacePadding(Out, GID % 1000000, 6);
232
233 printWithSpacePadding(Out, format("%o", Perms), 8);
234 printWithSpacePadding(Out, Size, 10);
235 Out << "`\n";
236}
237
238static void
241 unsigned UID, unsigned GID, unsigned Perms,
242 uint64_t Size) {
243 printWithSpacePadding(Out, Twine(Name) + "/", 16);
244 printRestOfMemberHeader(Out, ModTime, UID, GID, Perms, Size);
245}
246
247static void
250 unsigned UID, unsigned GID, unsigned Perms, uint64_t Size) {
251 uint64_t PosAfterHeader = Pos + 60 + Name.size();
252 // Pad so that even 64 bit object files are aligned.
253 unsigned Pad = offsetToAlignment(PosAfterHeader, Align(8));
254 unsigned NameWithPadding = Name.size() + Pad;
255 printWithSpacePadding(Out, Twine("#1/") + Twine(NameWithPadding), 16);
256 printRestOfMemberHeader(Out, ModTime, UID, GID, Perms,
257 NameWithPadding + Size);
258 Out << Name;
259 while (Pad--)
260 Out.write(uint8_t(0));
261}
262
263static void
266 unsigned UID, unsigned GID, unsigned Perms,
267 uint64_t Size) {
268 std::string AHeader;
269 raw_string_ostream AOut(AHeader);
270 if (Name.size() <= 16) {
271 printWithSpacePadding(AOut, Twine(Name), 16);
272 printRestOfMemberHeader(AOut, ModTime, UID, GID, Perms, Size);
273 } else {
274 // z/OS ar stores the exact name length inline with no extra alignment
275 // padding, unlike the BSD format which pads to an 8-byte boundary.
276 printWithSpacePadding(AOut, Twine("#1/") + Twine(Name.size()), 16);
277 printRestOfMemberHeader(AOut, ModTime, UID, GID, Perms, Name.size() + Size);
278 AOut << Name;
279 }
280 SmallString<256> EHeader;
281 if (std::error_code EC = ConverterEBCDIC::convertToEBCDIC(AHeader, EHeader))
283 Twine("failed to convert z/OS member header to EBCDIC: ") +
284 EC.message());
285 Out << EHeader.str();
286}
287
288static void
291 unsigned UID, unsigned GID, unsigned Perms,
292 uint64_t Size, uint64_t PrevOffset,
293 uint64_t NextOffset) {
294 unsigned NameLen = Name.size();
295
296 printWithSpacePadding(Out, Size, 20); // File member size
297 printWithSpacePadding(Out, NextOffset, 20); // Next member header offset
298 printWithSpacePadding(Out, PrevOffset, 20); // Previous member header offset
299 printWithSpacePadding(Out, sys::toTimeT(ModTime), 12); // File member date
300 // The big archive format has 12 chars for uid and gid.
301 printWithSpacePadding(Out, UID % 1000000000000, 12); // UID
302 printWithSpacePadding(Out, GID % 1000000000000, 12); // GID
303 printWithSpacePadding(Out, format("%o", Perms), 12); // Permission
304 printWithSpacePadding(Out, NameLen, 4); // Name length
305 if (NameLen) {
306 printWithSpacePadding(Out, Name, NameLen); // Name
307 if (NameLen % 2)
308 Out.write(uint8_t(0)); // Null byte padding
309 }
310 Out << "`\n"; // Terminator
311}
312
313static bool useStringTable(bool Thin, StringRef Name) {
314 return Thin || Name.size() >= 16 || Name.contains('/');
315}
316
318 switch (Kind) {
324 return false;
328 return true;
329 }
330 llvm_unreachable("not supported for writting");
331}
332
333static void
336 bool Thin, const NewArchiveMember &M, StringRef MemberName,
338 if (isBSDLike(Kind))
339 return printBSDMemberHeader(Out, Pos, MemberName, ModTime, M.UID, M.GID,
340 M.Perms, Size);
341 if (isZOSArchive(Kind))
342 return printZOSMemberHeader(Out, MemberName, ModTime, M.UID, M.GID, M.Perms,
343 Size);
344 if (!useStringTable(Thin, MemberName))
345 return printGNUSmallMemberHeader(Out, MemberName, ModTime, M.UID, M.GID,
346 M.Perms, Size);
347 Out << '/';
348 uint64_t NamePos;
349 if (Thin) {
350 NamePos = StringTable.tell();
351 StringTable << MemberName << "/\n";
352 } else {
353 auto Insertion = MemberNames.insert({MemberName, uint64_t(0)});
354 if (Insertion.second) {
355 Insertion.first->second = StringTable.tell();
356 StringTable << MemberName;
357 if (isCOFFArchive(Kind))
358 StringTable << '\0';
359 else
360 StringTable << "/\n";
361 }
362 NamePos = Insertion.first->second;
363 }
364 printWithSpacePadding(Out, NamePos, 15);
365 printRestOfMemberHeader(Out, ModTime, M.UID, M.GID, M.Perms, Size);
366}
367
368namespace {
369struct MemberData {
370 std::vector<unsigned> Symbols;
371 // z/OS archive attribute bits per symbol. Entry i of SymbolAttrs corresponds
372 // to Symbols[i]. These attributes are empty for non-z/OS archives.
373 std::vector<uint32_t> SymbolAttrs;
374 std::string Header;
375 StringRef Data;
376 StringRef Padding;
377 uint64_t PreHeadPadSize = 0;
378 std::unique_ptr<SymbolicFile> SymFile = nullptr;
379 std::string HybridName = "";
380 std::unique_ptr<MemoryBuffer> NativeBuf = nullptr;
381};
382} // namespace
383
384static MemberData computeStringTable(StringRef Names) {
385 unsigned Size = Names.size();
386 unsigned Pad = offsetToAlignment(Size, Align(2));
387 std::string Header;
388 raw_string_ostream Out(Header);
389 printWithSpacePadding(Out, "//", 48);
390 printWithSpacePadding(Out, Size + Pad, 10);
391 Out << "`\n";
392 return {{}, {}, std::move(Header), Names, Pad ? "\n" : ""};
393}
394
395static sys::TimePoint<std::chrono::seconds> now(bool Deterministic) {
396 using namespace std::chrono;
397
398 if (!Deterministic)
399 return time_point_cast<seconds>(system_clock::now());
401}
402
404 Expected<uint32_t> SymFlagsOrErr = S.getFlags();
405 if (!SymFlagsOrErr)
406 // TODO: Actually report errors helpfully.
407 report_fatal_error(SymFlagsOrErr.takeError());
408 if (*SymFlagsOrErr & object::SymbolRef::SF_FormatSpecific)
409 return false;
410 if (!(*SymFlagsOrErr & object::SymbolRef::SF_Global))
411 return false;
412 if (*SymFlagsOrErr & object::SymbolRef::SF_Undefined)
413 return false;
414 return true;
415}
416
418 uint64_t Val) {
419 if (is64BitKind(Kind))
420 print<uint64_t>(Out, Kind, Val);
421 else
422 print<uint32_t>(Out, Kind, Val);
423}
424
426 uint64_t NumSyms, uint64_t OffsetSize,
427 uint64_t StringTableSize,
428 uint32_t *Padding = nullptr) {
429 assert((OffsetSize == 4 || OffsetSize == 8) && "Unsupported OffsetSize");
430 uint64_t Size = OffsetSize; // Number of entries
431 // Each symbol table entry consists of a member offset.
432 // For BSD, each entry also includes a string table offset.
433 // For z/OS, each entry instead also includes a flag field.
435 Size += NumSyms * OffsetSize * 2; // Table
436 else
437 Size += NumSyms * OffsetSize; // Table
438 if (isBSDLike(Kind))
439 Size += OffsetSize; // byte count
440 Size += StringTableSize;
441 // ld64 expects the members to be 8-byte aligned for 64-bit content and at
442 // least 4-byte aligned for 32-bit content. Opt for the larger encoding
443 // uniformly.
444 // We do this for all bsd formats because it simplifies aligning members.
445 // For the big archive format, the symbol table is the last member, so there
446 // is no need to align.
448 ? 0
450
451 Size += Pad;
452 if (Padding)
453 *Padding = Pad;
454 return Size;
455}
456
458 uint32_t *Padding = nullptr) {
459 uint64_t Size = sizeof(uint32_t) * 2; // Number of symbols and objects entries
460 Size += NumObj * sizeof(uint32_t); // Offset table
461
462 for (auto S : SymMap.Map)
463 Size += sizeof(uint16_t) + S.first.length() + 1;
464
466 Size += Pad;
467 if (Padding)
468 *Padding = Pad;
469 return Size;
470}
471
473 uint32_t *Padding = nullptr) {
474 uint64_t Size = sizeof(uint32_t); // Number of symbols
475
476 for (auto S : SymMap.ECMap)
477 Size += sizeof(uint16_t) + S.first.length() + 1;
478
480 Size += Pad;
481 if (Padding)
482 *Padding = Pad;
483 return Size;
484}
485
487 bool Deterministic, uint64_t Size,
488 uint64_t PrevMemberOffset = 0,
489 uint64_t NextMemberOffset = 0) {
490 if (isBSDLike(Kind)) {
491 const char *Name = is64BitKind(Kind) ? "__.SYMDEF_64" : "__.SYMDEF";
492 printBSDMemberHeader(Out, Out.tell(), Name, now(Deterministic), 0, 0, 0,
493 Size);
494 } else if (isAIXBigArchive(Kind)) {
495 printBigArchiveMemberHeader(Out, "", now(Deterministic), 0, 0, 0, Size,
496 PrevMemberOffset, NextMemberOffset);
497 } else if (isZOSArchive(Kind)) {
498 const char *Name = "__.SYMDEF";
499 printZOSMemberHeader(Out, Name, now(Deterministic), 0, 0, 0, Size);
500 } else {
501 const char *Name = is64BitKind(Kind) ? "/SYM64" : "";
502 printGNUSmallMemberHeader(Out, Name, now(Deterministic), 0, 0, 0, Size);
503 }
504}
505
507 uint64_t NumMembers,
508 uint64_t StringMemberSize, uint64_t NumSyms,
509 uint64_t SymNamesSize, SymMap *SymMap) {
510 uint32_t OffsetSize = is64BitKind(Kind) ? 8 : 4;
511 uint64_t SymtabSize =
512 computeSymbolTableSize(Kind, NumSyms, OffsetSize, SymNamesSize);
513 auto computeSymbolTableHeaderSize = [=] {
514 SmallString<0> TmpBuf;
515 raw_svector_ostream Tmp(TmpBuf);
516 writeSymbolTableHeader(Tmp, Kind, true, SymtabSize);
517 return TmpBuf.size();
518 };
519 uint32_t HeaderSize = computeSymbolTableHeaderSize();
520 uint64_t Size = strlen("!<arch>\n") + HeaderSize + SymtabSize;
521
522 if (SymMap) {
523 Size += HeaderSize + computeSymbolMapSize(NumMembers, *SymMap);
524 if (SymMap->ECMap.size())
525 Size += HeaderSize + computeECSymbolsSize(*SymMap);
526 }
527
528 return Size + StringMemberSize;
529}
530
535 // Don't attempt to read non-symbolic file types.
537 return nullptr;
538 if (Type == file_magic::bitcode) {
540 Buf, file_magic::bitcode, &Context);
541 // An error reading a bitcode file most likely indicates that the file
542 // was created by a compiler from the future. Normally we don't try to
543 // implement forwards compatibility for bitcode files, but when creating an
544 // archive we can implement best-effort forwards compatibility by treating
545 // the file as a blob and not creating symbol index entries for it. lld and
546 // mold ignore the archive symbol index, so provided that you use one of
547 // these linkers, LTO will work as long as lld or the gold plugin is newer
548 // than the compiler. We only ignore errors if the archive format is one
549 // that is supported by a linker that is known to ignore the index,
550 // otherwise there's no chance of this working so we may as well error out.
551 // We print a warning on read failure so that users of linkers that rely on
552 // the symbol index can diagnose the issue.
553 //
554 // This is the same behavior as GNU ar when the linker plugin returns an
555 // error when reading the input file. If the bitcode file is actually
556 // malformed, it will be diagnosed at link time.
557 if (!ObjOrErr) {
558 switch (Kind) {
562 Warn(ObjOrErr.takeError());
563 return nullptr;
569 return ObjOrErr.takeError();
570 }
571 }
572 return std::move(*ObjOrErr);
573 } else {
574 auto ObjOrErr = object::SymbolicFile::createSymbolicFile(Buf);
575 if (!ObjOrErr)
576 return ObjOrErr.takeError();
577 return std::move(*ObjOrErr);
578 }
579}
580
581static bool is64BitSymbolicFile(const SymbolicFile *SymObj) {
582 return SymObj != nullptr ? SymObj->is64Bit() : false;
583}
584
585// Log2 of PAGESIZE(4096) on an AIX system.
586static const uint32_t Log2OfAIXPageSize = 12;
587
588// In the AIX big archive format, since the data content follows the member file
589// name, if the name ends on an odd byte, an extra byte will be added for
590// padding. This ensures that the data within the member file starts at an even
591// byte.
593
594template <typename AuxiliaryHeader>
595uint16_t getAuxMaxAlignment(uint16_t AuxHeaderSize, AuxiliaryHeader *AuxHeader,
596 uint16_t Log2OfMaxAlign) {
597 // If the member doesn't have an auxiliary header, it isn't a loadable object
598 // and so it just needs aligning at the minimum value.
599 if (AuxHeader == nullptr)
601
602 // If the auxiliary header does not have both MaxAlignOfData and
603 // MaxAlignOfText field, it is not a loadable shared object file, so align at
604 // the minimum value. The 'ModuleType' member is located right after
605 // 'MaxAlignOfData' in the AuxiliaryHeader.
606 if (AuxHeaderSize < offsetof(AuxiliaryHeader, ModuleType))
608
609 // If the XCOFF object file does not have a loader section, it is not
610 // loadable, so align at the minimum value.
611 if (AuxHeader->SecNumOfLoader == 0)
613
614 // The content of the loadable member file needs to be aligned at MAX(maximum
615 // alignment of .text, maximum alignment of .data) if there are both fields.
616 // If the desired alignment is > PAGESIZE, 32-bit members are aligned on a
617 // word boundary, while 64-bit members are aligned on a PAGESIZE(2^12=4096)
618 // boundary.
619 uint16_t Log2OfAlign =
620 std::max(AuxHeader->MaxAlignOfText, AuxHeader->MaxAlignOfData);
621 return 1 << (Log2OfAlign > Log2OfAIXPageSize ? Log2OfMaxAlign : Log2OfAlign);
622}
623
624// AIX big archives may contain shared object members. The AIX OS requires these
625// members to be aligned if they are 64-bit and recommends it for 32-bit
626// members. This ensures that when these members are loaded they are aligned in
627// memory.
630 if (!XCOFFObj)
632
633 // If the desired alignment is > PAGESIZE, 32-bit members are aligned on a
634 // word boundary, while 64-bit members are aligned on a PAGESIZE boundary.
635 return XCOFFObj->is64Bit()
637 XCOFFObj->auxiliaryHeader64(),
640 XCOFFObj->auxiliaryHeader32(), 2);
641}
642
644 bool Deterministic, ArrayRef<MemberData> Members,
645 StringRef StringTable, uint64_t MembersOffset,
646 unsigned NumSyms, uint64_t PrevMemberOffset = 0,
647 uint64_t NextMemberOffset = 0,
648 bool Is64Bit = false) {
649 // We don't write a symbol table on an archive with no members -- except on
650 // Darwin, where the linker will abort unless the archive has a symbol table.
651 if (StringTable.empty() && !isDarwin(Kind) && !isCOFFArchive(Kind))
652 return;
653
654 uint64_t OffsetSize = is64BitKind(Kind) ? 8 : 4;
655 uint32_t Pad;
656 uint64_t Size = computeSymbolTableSize(Kind, NumSyms, OffsetSize,
657 StringTable.size(), &Pad);
658
659 // Padding size is not included in the Size field of the z/OS symbol table
660 // header.
661 int64_t HeaderSize = Size;
662 if (isZOSArchive(Kind))
663 HeaderSize -= Pad;
664
665 writeSymbolTableHeader(Out, Kind, Deterministic, HeaderSize, PrevMemberOffset,
666 NextMemberOffset);
667
668 if (isBSDLike(Kind))
669 printNBits(Out, Kind, NumSyms * 2 * OffsetSize);
670 else
671 printNBits(Out, Kind, NumSyms);
672
673 uint64_t Pos = MembersOffset;
674 for (const MemberData &M : Members) {
675 if (isAIXBigArchive(Kind)) {
676 Pos += M.PreHeadPadSize;
677 if (is64BitSymbolicFile(M.SymFile.get()) != Is64Bit) {
678 Pos += M.Header.size() + M.Data.size() + M.Padding.size();
679 continue;
680 }
681 }
682
683 assert((!isZOSArchive(Kind) || M.Symbols.size() == M.SymbolAttrs.size()) &&
684 "Incorrect number of symbol attributes!");
685 for (size_t I = 0, E = M.Symbols.size(); I != E; ++I) {
686 if (isBSDLike(Kind))
687 printNBits(Out, Kind, M.Symbols[I]);
688 printNBits(Out, Kind, Pos); // member offset
689 if (isZOSArchive(Kind))
690 printNBits(Out, Kind, M.SymbolAttrs[I]); // symbol attributes
691 }
692 Pos += M.Header.size() + M.Data.size() + M.Padding.size();
693 }
694
695 if (isBSDLike(Kind))
696 // byte count of the string table
698 if (isZOSArchive(Kind)) {
699 SmallString<256> EStringTable;
700 if (std::error_code EC =
703 Twine("failed to convert z/OS symbol table to EBCDIC: ") +
704 EC.message());
705 Out << EStringTable.str();
706 } else {
707 Out << StringTable;
708 }
709
710 while (Pad--)
711 Out.write(uint8_t(0));
712}
713
715 bool Deterministic, ArrayRef<MemberData> Members,
716 SymMap &SymMap, uint64_t MembersOffset) {
717 uint32_t Pad;
718 uint64_t Size = computeSymbolMapSize(Members.size(), SymMap, &Pad);
719 writeSymbolTableHeader(Out, Kind, Deterministic, Size, 0);
720
721 uint32_t Pos = MembersOffset;
722
723 printLE<uint32_t>(Out, Members.size());
724 for (const MemberData &M : Members) {
725 printLE(Out, Pos); // member offset
726 Pos += M.Header.size() + M.Data.size() + M.Padding.size();
727 }
728
729 printLE<uint32_t>(Out, SymMap.Map.size());
730
731 for (auto S : SymMap.Map)
732 printLE(Out, S.second);
733 for (auto S : SymMap.Map)
734 Out << S.first << '\0';
735
736 while (Pad--)
737 Out.write(uint8_t(0));
738}
739
741 bool Deterministic, ArrayRef<MemberData> Members,
742 SymMap &SymMap) {
743 uint32_t Pad;
745 printGNUSmallMemberHeader(Out, "/<ECSYMBOLS>", now(Deterministic), 0, 0, 0,
746 Size);
747
748 printLE<uint32_t>(Out, SymMap.ECMap.size());
749
750 for (auto S : SymMap.ECMap)
751 printLE(Out, S.second);
752 for (auto S : SymMap.ECMap)
753 Out << S.first << '\0';
754 while (Pad--)
755 Out.write(uint8_t(0));
756}
757
759 if (Obj.isCOFF())
760 return cast<llvm::object::COFFObjectFile>(&Obj)->getMachine() !=
762
763 if (Obj.isCOFFImportFile())
764 return cast<llvm::object::COFFImportFile>(&Obj)->getMachine() !=
766
767 if (Obj.isIR()) {
768 Expected<std::string> TripleStr =
769 getBitcodeTargetTriple(Obj.getMemoryBufferRef());
770 if (!TripleStr)
771 return false;
772 Triple T(std::move(*TripleStr));
773 return T.isWindowsArm64EC() || T.getArch() == Triple::x86_64;
774 }
775
776 return false;
777}
778
780 if (Obj.isCOFF())
781 return COFF::isAnyArm64(cast<COFFObjectFile>(&Obj)->getMachine());
782
783 if (Obj.isCOFFImportFile())
784 return COFF::isAnyArm64(cast<COFFImportFile>(&Obj)->getMachine());
785
786 if (Obj.isIR()) {
787 Expected<std::string> TripleStr =
788 getBitcodeTargetTriple(Obj.getMemoryBufferRef());
789 if (!TripleStr)
790 return false;
791 Triple T(std::move(*TripleStr));
792 return T.isOSWindows() && T.getArch() == Triple::aarch64;
793 }
794
795 return false;
796}
797
799 return Name.starts_with(ImportDescriptorPrefix) ||
801 (Name.starts_with(NullThunkDataPrefix) &&
802 Name.ends_with(NullThunkDataSuffix));
803}
804
806 uint16_t Index,
807 raw_ostream &SymNames,
808 SymMap *SymMap) {
809 std::vector<unsigned> Ret;
810
811 if (Obj == nullptr)
812 return Ret;
813
814 std::map<std::string, uint16_t> *Map = nullptr;
815 if (SymMap)
816 Map = SymMap->UseECMap && isECObject(*Obj) ? &SymMap->ECMap : &SymMap->Map;
817
818 for (const object::BasicSymbolRef &S : Obj->symbols()) {
819 if (!isArchiveSymbol(S))
820 continue;
821 if (Map) {
822 std::string Name;
823 raw_string_ostream NameStream(Name);
824 if (Error E = S.printName(NameStream))
825 return std::move(E);
826 if (!Map->try_emplace(Name, Index).second)
827 continue; // ignore duplicated symbol
828 if (Map == &SymMap->Map) {
829 Ret.push_back(SymNames.tell());
830 SymNames << Name << '\0';
831 // If EC is enabled, then the import descriptors are NOT put into EC
832 // objects so we need to copy them to the EC map manually.
833 if (SymMap->UseECMap && isImportDescriptor(Name))
834 SymMap->ECMap[Name] = Index;
835 }
836 } else {
837 Ret.push_back(SymNames.tell());
838 if (Error E = S.printName(SymNames))
839 return std::move(E);
840 SymNames << '\0';
841 }
842 }
843 return Ret;
844}
845
848 object::Archive::Kind Kind, bool Thin, bool Deterministic,
849 SymtabWritingMode NeedSymbols, SymMap *SymMap,
850 LLVMContext &Context, ArrayRef<NewArchiveMember> NewMembers,
851 std::optional<bool> IsEC, function_ref<void(Error)> Warn) {
852 static char PaddingData[8] = {'\n', '\n', '\n', '\n', '\n', '\n', '\n', '\n'};
853 static char ZOSPaddingData[8] = {0x15, 0x15, 0x15, 0x15,
854 0x15, 0x15, 0x15, 0x15}; // EBCDIC newlines.
855 uint64_t Pos =
857
858 std::vector<MemberData> Ret;
859 bool HasObject = false;
860
861 // Deduplicate long member names in the string table and reuse earlier name
862 // offsets. This especially saves space for COFF Import libraries where all
863 // members have the same name.
864 StringMap<uint64_t> MemberNames;
865
866 // UniqueTimestamps is a special case to improve debugging on Darwin:
867 //
868 // The Darwin linker does not link debug info into the final
869 // binary. Instead, it emits entries of type N_OSO in the output
870 // binary's symbol table, containing references to the linked-in
871 // object files. Using that reference, the debugger can read the
872 // debug data directly from the object files. Alternatively, an
873 // invocation of 'dsymutil' will link the debug data from the object
874 // files into a dSYM bundle, which can be loaded by the debugger,
875 // instead of the object files.
876 //
877 // For an object file, the N_OSO entries contain the absolute path
878 // path to the file, and the file's timestamp. For an object
879 // included in an archive, the path is formatted like
880 // "/absolute/path/to/archive.a(member.o)", and the timestamp is the
881 // archive member's timestamp, rather than the archive's timestamp.
882 //
883 // However, this doesn't always uniquely identify an object within
884 // an archive -- an archive file can have multiple entries with the
885 // same filename. (This will happen commonly if the original object
886 // files started in different directories.) The only way they get
887 // distinguished, then, is via the timestamp. But this process is
888 // unable to find the correct object file in the archive when there
889 // are two files of the same name and timestamp.
890 //
891 // Additionally, timestamp==0 is treated specially, and causes the
892 // timestamp to be ignored as a match criteria.
893 //
894 // That will "usually" work out okay when creating an archive not in
895 // deterministic timestamp mode, because the objects will probably
896 // have been created at different timestamps.
897 //
898 // To ameliorate this problem, in deterministic archive mode (which
899 // is the default), on Darwin we will emit a unique non-zero
900 // timestamp for each entry with a duplicated name. This is still
901 // deterministic: the only thing affecting that timestamp is the
902 // order of the files in the resultant archive.
903 //
904 // See also the functions that handle the lookup:
905 // in lldb: ObjectContainerBSDArchive::Archive::FindObject()
906 // in llvm/tools/dsymutil: BinaryHolder::GetArchiveMemberBuffers().
907 bool UniqueTimestamps = Deterministic && isDarwin(Kind);
908 std::map<StringRef, unsigned> FilenameCount;
909 if (UniqueTimestamps) {
910 for (const NewArchiveMember &M : NewMembers)
911 FilenameCount[M.MemberName]++;
912 for (auto &Entry : FilenameCount)
913 Entry.second = Entry.second > 1 ? 1 : 0;
914 }
915
916 uint32_t LastZosObjIndex =
917 UINT_MAX; // Only set when writing symbol table in z/OS archive.
918
919 for (const NewArchiveMember &M : NewMembers) {
920 MemberData &D = Ret.emplace_back();
921 D.Data = M.Buf->getBuffer();
922
923 if (NeedSymbols != SymtabWritingMode::NoSymtab || isAIXBigArchive(Kind)) {
925 M.Buf->getMemBufferRef(), Context, Kind, [&](Error Err) {
926 Warn(createFileError(M.MemberName, std::move(Err)));
927 });
928 if (!SymFileOrErr)
929 return createFileError(M.MemberName, SymFileOrErr.takeError());
930 D.SymFile = std::move(*SymFileOrErr);
931
932 if (SymMap && D.SymFile.get()) {
933 auto COFFObj = dyn_cast<COFFObjectFile>(D.SymFile.get());
934 std::optional<MemoryBufferRef> HybridView;
935 if (COFFObj && (HybridView = COFFObj->findHybridObjectSection())) {
936 // Strip the hybrid section.
937 D.NativeBuf = COFFObj->stripHybridSection();
938 D.Data = D.NativeBuf->getBuffer();
939
940 // Create a separate archive member for the hybrid ARM64X object.
941 MemberData &ECData = Ret.emplace_back();
942 ECData.Data = HybridView->getBuffer();
943
944 SymFileOrErr =
945 getSymbolicFile(*HybridView, Context, Kind, [&](Error Err) {
946 Warn(createFileError(M.MemberName, std::move(Err)));
947 });
948 if (!SymFileOrErr)
949 return createFileError(M.MemberName, SymFileOrErr.takeError());
950 ECData.SymFile = std::move(*SymFileOrErr);
951
952 // Use obj.arm64ec subdirectory for the hybrid object name.
953 size_t Pos = M.MemberName.find_last_of("/\\");
954 Pos = Pos == StringRef::npos ? 0 : Pos + 1;
955 ECData.HybridName = (M.MemberName.substr(0, Pos) + "obj.arm64ec/" +
956 M.MemberName.substr(Pos))
957 .str();
958 }
959 }
960
961 if (isZOSArchive(Kind) && D.SymFile.get())
962 LastZosObjIndex = Ret.size() - 1;
963 }
964 }
965
966 if (SymMap) {
967 if (IsEC) {
968 SymMap->UseECMap = *IsEC;
969 } else {
970 // When IsEC is not specified by the caller, use it when we have both
971 // any ARM64 object (ARM64 or ARM64EC) and any EC object (ARM64EC or
972 // AMD64). This may be a single ARM64EC object, but may also be separate
973 // ARM64 and AMD64 objects.
974 bool HaveArm64 = false, HaveEC = false;
975 for (const MemberData &D : Ret) {
976 if (!D.SymFile)
977 continue;
978 if (!HaveArm64)
979 HaveArm64 = isAnyArm64COFF(*D.SymFile);
980 if (!HaveEC)
981 HaveEC = isECObject(*D.SymFile);
982 if (HaveArm64 && HaveEC) {
983 SymMap->UseECMap = true;
984 break;
985 }
986 }
987 }
988 }
989
990 // The big archive format needs to know the offset of the previous member
991 // header.
992 uint64_t PrevOffset = 0;
993 uint64_t NextMemHeadPadSize = 0;
994
995 for (uint32_t Index = 0, MemberIndex = 0; Index < Ret.size(); ++Index) {
996 MemberData &D = Ret[Index];
997 const NewArchiveMember *M = &NewMembers[MemberIndex];
998 // Native COFF members (resulting from stripping a hybrid object section)
999 // are followed by an extracted hybrid object member, using the same
1000 // NewArchiveMember.
1001 if (!D.NativeBuf.get())
1002 ++MemberIndex;
1003 raw_string_ostream Out(D.Header);
1004
1005 uint64_t Size = D.Data.size();
1006 if (Thin)
1007 D.Data = "";
1008
1009 // ld64 expects the members to be 8-byte aligned for 64-bit content and at
1010 // least 4-byte aligned for 32-bit content. Opt for the larger encoding
1011 // uniformly. This matches the behaviour with cctools and ensures that ld64
1012 // is happy with archives that we generate.
1013 unsigned MemberPadding =
1014 isDarwin(Kind) ? offsetToAlignment(D.Data.size(), Align(8)) : 0;
1015
1016 StringRef MemberName = D.HybridName.size() ? D.HybridName : M->MemberName;
1017
1018 // z/OS stores long member names inline using their exact byte length.
1019 // Include the inline name when computing alignment.
1020 uint64_t PaddingBase = D.Data.size() + MemberPadding;
1021 if (isZOSArchive(Kind) && MemberName.size() > 16)
1022 PaddingBase += MemberName.size();
1023 unsigned TailPadding = offsetToAlignment(PaddingBase, Align(2));
1024 D.Padding = StringRef(isZOSArchive(Kind) ? ZOSPaddingData : PaddingData,
1025 MemberPadding + TailPadding);
1026
1028 if (UniqueTimestamps)
1029 // Increment timestamp for each file of a given name.
1030 ModTime = sys::toTimePoint(FilenameCount[MemberName]++);
1031 else
1032 ModTime = M->ModTime;
1033
1034 Size += MemberPadding;
1036 std::string StringMsg =
1037 "File " + MemberName.str() + " exceeds size limit";
1039 std::move(StringMsg), object::object_error::parse_failed);
1040 }
1041
1042 // In the big archive file format, we need to calculate and include the next
1043 // member offset and previous member offset in the file member header.
1044 if (isAIXBigArchive(Kind)) {
1045 uint64_t OffsetToMemData =
1046 Pos + sizeof(object::BigArMemHdrType) + alignTo(MemberName.size(), 2);
1047
1048 if (Index == 0)
1049 NextMemHeadPadSize =
1050 alignToPowerOf2(OffsetToMemData,
1051 getMemberAlignment(D.SymFile.get())) -
1052 OffsetToMemData;
1053
1054 D.PreHeadPadSize = NextMemHeadPadSize;
1055 Pos += D.PreHeadPadSize;
1056 uint64_t NextOffset = Pos + sizeof(object::BigArMemHdrType) +
1057 alignTo(MemberName.size(), 2) + alignTo(Size, 2);
1058
1059 // If there is another member file after this, we need to calculate the
1060 // padding before the header.
1061 if (Index + 1 != Ret.size()) {
1062 uint64_t OffsetToNextMemData =
1063 NextOffset + sizeof(object::BigArMemHdrType) +
1064 alignTo(NewMembers[MemberIndex].MemberName.size(), 2);
1065 NextMemHeadPadSize =
1066 alignToPowerOf2(OffsetToNextMemData,
1067 getMemberAlignment(Ret[Index + 1].SymFile.get())) -
1068 OffsetToNextMemData;
1069 NextOffset += NextMemHeadPadSize;
1070 }
1071 printBigArchiveMemberHeader(Out, MemberName, ModTime, M->UID, M->GID,
1072 M->Perms, Size, PrevOffset, NextOffset);
1073 PrevOffset = Pos;
1074 } else {
1075 printMemberHeader(Out, Pos, StringTable, MemberNames, Kind, Thin, *M,
1076 MemberName, ModTime, Size);
1077 }
1078
1079 if (NeedSymbols != SymtabWritingMode::NoSymtab) {
1080 Expected<std::vector<unsigned>> SymbolsOrErr =
1081 getSymbols(D.SymFile.get(), Index + 1, SymNames, SymMap);
1082 if (!SymbolsOrErr)
1083 return createFileError(MemberName, SymbolsOrErr.takeError());
1084 D.Symbols = std::move(*SymbolsOrErr);
1085 // For z/OS, populate SymbolAttrs in lockstep with Symbols so that
1086 // writeSymbolTable() can emit the per-symbol attribute word.
1087 if (isZOSArchive(Kind)) {
1088 auto *GOFFObj = dyn_cast_or_null<GOFFObjectFile>(D.SymFile.get());
1089 if (GOFFObj) {
1090 for (object::BasicSymbolRef S : GOFFObj->symbols()) {
1091 if (!isArchiveSymbol(S))
1092 continue;
1093 D.SymbolAttrs.push_back(
1094 GOFFObj->getZOSSymbolArchiveAttributes(S.getRawDataRefImpl()));
1095 }
1096 } else {
1097 // For non-GOFF symbolic files (e.g. bitcode/IR), there is no z/OS
1098 // archive attribute data available. Pad SymbolAttrs to stay in sync
1099 // with Symbols.
1100 D.SymbolAttrs.resize(D.Symbols.size());
1101 }
1102 }
1103 if (D.SymFile)
1104 HasObject = true;
1105 // On z/OS, when there are no symbols, add a dummy blank symbol
1106 // into the symbol table. This is done since the z/OS binder:
1107 // - emits an error if there is no symbol table in the archive
1108 // - emits an error if the symbol table has 0 symbols
1109 // - should not find any references to a blank symbol
1110 if (isZOSArchive(Kind) && (LastZosObjIndex == Index) &&
1111 (SymNames.tell() == 0)) {
1112 D.Symbols.push_back(0);
1113 D.SymbolAttrs.push_back(0);
1114 SymNames << ' ' << '\0';
1115 }
1116 }
1117
1118 Pos += D.Header.size() + D.Data.size() + D.Padding.size();
1119 }
1120 // If there are no symbols, emit an empty symbol table, to satisfy Solaris
1121 // tools, older versions of which expect a symbol table in a non-empty
1122 // archive, regardless of whether there are any symbols in it.
1123 if (HasObject && SymNames.tell() == 0 && !isCOFFArchive(Kind))
1124 SymNames << '\0' << '\0' << '\0';
1125 return std::move(Ret);
1126}
1127
1128namespace llvm {
1129
1131 SmallString<128> Ret = P;
1132 std::error_code Err = sys::fs::make_absolute(Ret);
1133 if (Err)
1134 return Err;
1135 sys::path::remove_dots(Ret, /*removedotdot*/ true);
1136 return Ret;
1137}
1138
1139// Compute the relative path from From to To.
1141 ErrorOr<SmallString<128>> PathToOrErr = canonicalizePath(To);
1142 ErrorOr<SmallString<128>> DirFromOrErr = canonicalizePath(From);
1143 if (!PathToOrErr || !DirFromOrErr)
1145
1146 const SmallString<128> &PathTo = *PathToOrErr;
1147 const SmallString<128> &DirFrom = sys::path::parent_path(*DirFromOrErr);
1148
1149 // Can't construct a relative path between different roots
1150 if (sys::path::root_name(PathTo) != sys::path::root_name(DirFrom))
1151 return sys::path::convert_to_slash(PathTo);
1152
1153 // Skip common prefixes
1154 auto FromTo =
1155 std::mismatch(sys::path::begin(DirFrom), sys::path::end(DirFrom),
1156 sys::path::begin(PathTo));
1157 auto FromI = FromTo.first;
1158 auto ToI = FromTo.second;
1159
1160 // Construct relative path
1161 SmallString<128> Relative;
1162 for (auto FromE = sys::path::end(DirFrom); FromI != FromE; ++FromI)
1164
1165 for (auto ToE = sys::path::end(PathTo); ToI != ToE; ++ToI)
1167
1168 return std::string(Relative);
1169}
1170
1172 ArrayRef<NewArchiveMember> NewMembers,
1173 SymtabWritingMode WriteSymtab,
1174 object::Archive::Kind Kind, bool Deterministic,
1175 bool Thin, std::optional<bool> IsEC,
1176 function_ref<void(Error)> Warn) {
1177 assert((!Thin || !isBSDLike(Kind)) && "Only the gnu format has a thin mode");
1178
1179 SmallString<0> SymNamesBuf;
1180 raw_svector_ostream SymNames(SymNamesBuf);
1181 SmallString<0> StringTableBuf;
1182 raw_svector_ostream StringTable(StringTableBuf);
1183 SymMap SymMap;
1184 bool ShouldWriteSymtab = WriteSymtab != SymtabWritingMode::NoSymtab;
1185
1186 // COFF symbol map uses 16-bit indexes, so we can't use it if there are too
1187 // many members. COFF format also requires symbol table presence, so use
1188 // GNU format when NoSymtab is requested.
1189 if (isCOFFArchive(Kind) && (NewMembers.size() > 0xfffe || !ShouldWriteSymtab))
1191
1192 // In the scenario when LLVMContext is populated SymbolicFile will contain a
1193 // reference to it, thus SymbolicFile should be destroyed first.
1194 LLVMContext Context;
1195
1197 StringTable, SymNames, Kind, Thin, Deterministic, WriteSymtab,
1198 isCOFFArchive(Kind) ? &SymMap : nullptr, Context, NewMembers, IsEC, Warn);
1199 if (Error E = DataOrErr.takeError())
1200 return E;
1201 std::vector<MemberData> &Data = *DataOrErr;
1202
1203 uint64_t StringTableSize = 0;
1204 MemberData StringTableMember;
1205 if (!StringTableBuf.empty() && !isAIXBigArchive(Kind)) {
1206 StringTableMember = computeStringTable(StringTableBuf);
1207 StringTableSize = StringTableMember.Header.size() +
1208 StringTableMember.Data.size() +
1209 StringTableMember.Padding.size();
1210 }
1211
1212 // We would like to detect if we need to switch to a 64-bit symbol table.
1213 uint64_t LastMemberEndOffset = 0;
1214 uint64_t LastMemberHeaderOffset = 0;
1215 uint64_t NumSyms = 0;
1216 uint64_t NumSyms32 = 0; // Store symbol number of 32-bit member files.
1217
1218 for (const auto &M : Data) {
1219 // Record the start of the member's offset
1220 LastMemberEndOffset += M.PreHeadPadSize;
1221 LastMemberHeaderOffset = LastMemberEndOffset;
1222 // Account for the size of each part associated with the member.
1223 LastMemberEndOffset += M.Header.size() + M.Data.size() + M.Padding.size();
1224 NumSyms += M.Symbols.size();
1225
1226 // AIX big archive files may contain two global symbol tables. The
1227 // first global symbol table locates 32-bit file members that define global
1228 // symbols; the second global symbol table does the same for 64-bit file
1229 // members. As a big archive can have both 32-bit and 64-bit file members,
1230 // we need to know the number of symbols in each symbol table individually.
1231 if (isAIXBigArchive(Kind) && ShouldWriteSymtab) {
1232 if (!is64BitSymbolicFile(M.SymFile.get()))
1233 NumSyms32 += M.Symbols.size();
1234 }
1235 }
1236
1237 std::optional<uint64_t> HeadersSize;
1238
1239 // The symbol table is put at the end of the big archive file. The symbol
1240 // table is at the start of the archive file for other archive formats.
1241 if (ShouldWriteSymtab && !is64BitKind(Kind)) {
1242 // We assume 32-bit offsets to see if 32-bit symbols are possible or not.
1243 HeadersSize = computeHeadersSize(Kind, Data.size(), StringTableSize,
1244 NumSyms, SymNamesBuf.size(),
1245 isCOFFArchive(Kind) ? &SymMap : nullptr);
1246
1247 // The SYM64 format is used when an archive's member offsets are larger than
1248 // 32-bits can hold. The need for this shift in format is detected by
1249 // writeArchive. To test this we need to generate a file with a member that
1250 // has an offset larger than 32-bits but this demands a very slow test. To
1251 // speed the test up we use this environment variable to pretend like the
1252 // cutoff happens before 32-bits and instead happens at some much smaller
1253 // value.
1254 uint64_t Sym64Threshold = 1ULL << 32;
1255 const char *Sym64Env = std::getenv("SYM64_THRESHOLD");
1256 if (Sym64Env)
1257 StringRef(Sym64Env).getAsInteger(10, Sym64Threshold);
1258
1259 // If LastMemberHeaderOffset isn't going to fit in a 32-bit varible we need
1260 // to switch to 64-bit. Note that the file can be larger than 4GB as long as
1261 // the last member starts before the 4GB offset.
1262 if (*HeadersSize + LastMemberHeaderOffset >= Sym64Threshold) {
1263 switch (Kind) {
1265 // COFF format has no 64-bit version, so we use GNU64 instead.
1266 if (!SymMap.Map.empty() && !SymMap.ECMap.empty())
1267 // Only the COFF format supports the ECSYMBOLS section, so don’t use
1268 // GNU64 when two symbol maps are required.
1270 "Archive is too large: ARM64X does not support archives larger "
1271 "than 4GB");
1272 // Since this changes the headers, we need to recalculate everything.
1273 return writeArchiveToStream(Out, NewMembers, WriteSymtab,
1274 object::Archive::K_GNU64, Deterministic,
1275 Thin, IsEC, Warn);
1278 break;
1279 default:
1281 break;
1282 }
1283 HeadersSize.reset();
1284 }
1285 }
1286
1287 if (Thin)
1288 Out << "!<thin>\n";
1289 else if (isAIXBigArchive(Kind))
1290 Out << "<bigaf>\n";
1291 else if (isZOSArchive(Kind))
1292 Out << ZOSArchiveMagic;
1293 else
1294 Out << "!<arch>\n";
1295
1296 if (!isAIXBigArchive(Kind)) {
1297 if (ShouldWriteSymtab) {
1298 if (!HeadersSize)
1299 HeadersSize = computeHeadersSize(
1300 Kind, Data.size(), StringTableSize, NumSyms, SymNamesBuf.size(),
1301 isCOFFArchive(Kind) ? &SymMap : nullptr);
1302 writeSymbolTable(Out, Kind, Deterministic, Data, SymNamesBuf,
1303 *HeadersSize, NumSyms);
1304
1305 if (isCOFFArchive(Kind))
1306 writeSymbolMap(Out, Kind, Deterministic, Data, SymMap, *HeadersSize);
1307 }
1308
1309 if (StringTableSize)
1310 Out << StringTableMember.Header << StringTableMember.Data
1311 << StringTableMember.Padding;
1312
1313 if (ShouldWriteSymtab && SymMap.ECMap.size())
1314 writeECSymbols(Out, Kind, Deterministic, Data, SymMap);
1315
1316 for (const MemberData &M : Data)
1317 Out << M.Header << M.Data << M.Padding;
1318 } else {
1319 HeadersSize = sizeof(object::BigArchive::FixLenHdr);
1320 LastMemberEndOffset += *HeadersSize;
1321 LastMemberHeaderOffset += *HeadersSize;
1322
1323 // For the big archive (AIX) format, compute a table of member names and
1324 // offsets, used in the member table.
1325 uint64_t MemberTableNameStrTblSize = 0;
1326 std::vector<size_t> MemberOffsets;
1327 std::vector<StringRef> MemberNames;
1328 // Loop across object to find offset and names.
1329 uint64_t MemberEndOffset = sizeof(object::BigArchive::FixLenHdr);
1330 for (size_t I = 0, Size = NewMembers.size(); I != Size; ++I) {
1331 const NewArchiveMember &Member = NewMembers[I];
1332 MemberTableNameStrTblSize += Member.MemberName.size() + 1;
1333 MemberEndOffset += Data[I].PreHeadPadSize;
1334 MemberOffsets.push_back(MemberEndOffset);
1335 MemberNames.push_back(Member.MemberName);
1336 // File member name ended with "`\n". The length is included in
1337 // BigArMemHdrType.
1338 MemberEndOffset += sizeof(object::BigArMemHdrType) +
1339 alignTo(Data[I].Data.size(), 2) +
1340 alignTo(Member.MemberName.size(), 2);
1341 }
1342
1343 // AIX member table size.
1344 uint64_t MemberTableSize = 20 + // Number of members field
1345 20 * MemberOffsets.size() +
1346 MemberTableNameStrTblSize;
1347
1348 SmallString<0> SymNamesBuf32;
1349 SmallString<0> SymNamesBuf64;
1350 raw_svector_ostream SymNames32(SymNamesBuf32);
1351 raw_svector_ostream SymNames64(SymNamesBuf64);
1352
1353 if (ShouldWriteSymtab && NumSyms)
1354 // Generate the symbol names for the members.
1355 for (const auto &M : Data) {
1357 M.SymFile.get(), 0,
1358 is64BitSymbolicFile(M.SymFile.get()) ? SymNames64 : SymNames32,
1359 nullptr);
1360 if (!SymbolsOrErr)
1361 return SymbolsOrErr.takeError();
1362 }
1363
1364 uint64_t MemberTableEndOffset =
1365 LastMemberEndOffset +
1366 alignTo(sizeof(object::BigArMemHdrType) + MemberTableSize, 2);
1367
1368 // In AIX OS, The 'GlobSymOffset' field in the fixed-length header contains
1369 // the offset to the 32-bit global symbol table, and the 'GlobSym64Offset'
1370 // contains the offset to the 64-bit global symbol table.
1371 uint64_t GlobalSymbolOffset =
1372 (ShouldWriteSymtab &&
1373 (WriteSymtab != SymtabWritingMode::BigArchive64) && NumSyms32 > 0)
1374 ? MemberTableEndOffset
1375 : 0;
1376
1377 uint64_t GlobalSymbolOffset64 = 0;
1378 uint64_t NumSyms64 = NumSyms - NumSyms32;
1379 if (ShouldWriteSymtab && (WriteSymtab != SymtabWritingMode::BigArchive32) &&
1380 NumSyms64 > 0) {
1381 if (GlobalSymbolOffset == 0)
1382 GlobalSymbolOffset64 = MemberTableEndOffset;
1383 else
1384 // If there is a global symbol table for 32-bit members,
1385 // the 64-bit global symbol table is after the 32-bit one.
1386 GlobalSymbolOffset64 =
1387 GlobalSymbolOffset + sizeof(object::BigArMemHdrType) +
1388 (NumSyms32 + 1) * 8 + alignTo(SymNamesBuf32.size(), 2);
1389 }
1390
1391 // Fixed Sized Header.
1392 printWithSpacePadding(Out, NewMembers.size() ? LastMemberEndOffset : 0,
1393 20); // Offset to member table
1394 // If there are no file members in the archive, there will be no global
1395 // symbol table.
1396 printWithSpacePadding(Out, GlobalSymbolOffset, 20);
1397 printWithSpacePadding(Out, GlobalSymbolOffset64, 20);
1399 NewMembers.size()
1401 Data[0].PreHeadPadSize
1402 : 0,
1403 20); // Offset to first archive member
1404 printWithSpacePadding(Out, NewMembers.size() ? LastMemberHeaderOffset : 0,
1405 20); // Offset to last archive member
1407 Out, 0,
1408 20); // Offset to first member of free list - Not supported yet
1409
1410 for (const MemberData &M : Data) {
1411 Out << std::string(M.PreHeadPadSize, '\0');
1412 Out << M.Header << M.Data;
1413 if (M.Data.size() % 2)
1414 Out << '\0';
1415 }
1416
1417 if (NewMembers.size()) {
1418 // Member table.
1419 printBigArchiveMemberHeader(Out, "", sys::toTimePoint(0), 0, 0, 0,
1420 MemberTableSize, LastMemberHeaderOffset,
1421 GlobalSymbolOffset ? GlobalSymbolOffset
1422 : GlobalSymbolOffset64);
1423 printWithSpacePadding(Out, MemberOffsets.size(), 20); // Number of members
1424 for (uint64_t MemberOffset : MemberOffsets)
1425 printWithSpacePadding(Out, MemberOffset,
1426 20); // Offset to member file header.
1427 for (StringRef MemberName : MemberNames)
1428 Out << MemberName << '\0'; // Member file name, null byte padding.
1429
1430 if (MemberTableNameStrTblSize % 2)
1431 Out << '\0'; // Name table must be tail padded to an even number of
1432 // bytes.
1433
1434 if (ShouldWriteSymtab) {
1435 // Write global symbol table for 32-bit file members.
1436 if (GlobalSymbolOffset) {
1437 writeSymbolTable(Out, Kind, Deterministic, Data, SymNamesBuf32,
1438 *HeadersSize, NumSyms32, LastMemberEndOffset,
1439 GlobalSymbolOffset64);
1440 // Add padding between the symbol tables, if needed.
1441 if (GlobalSymbolOffset64 && (SymNamesBuf32.size() % 2))
1442 Out << '\0';
1443 }
1444
1445 // Write global symbol table for 64-bit file members.
1446 if (GlobalSymbolOffset64)
1447 writeSymbolTable(Out, Kind, Deterministic, Data, SymNamesBuf64,
1448 *HeadersSize, NumSyms64,
1449 GlobalSymbolOffset ? GlobalSymbolOffset
1450 : LastMemberEndOffset,
1451 0, true);
1452 }
1453 }
1454 }
1455 Out.flush();
1456 return Error::success();
1457}
1458
1460 llvm::logAllUnhandledErrors(std::move(Err), llvm::errs(), "warning: ");
1461}
1462
1465 bool Deterministic, bool Thin,
1466 std::unique_ptr<MemoryBuffer> OldArchiveBuf,
1467 std::optional<bool> IsEC, function_ref<void(Error)> Warn) {
1469 sys::fs::TempFile::create(ArcName + ".temp-archive-%%%%%%%.a");
1470 if (!Temp)
1471 return Temp.takeError();
1472 raw_fd_ostream Out(Temp->FD, false);
1473
1474 if (Error E = writeArchiveToStream(Out, NewMembers, WriteSymtab, Kind,
1475 Deterministic, Thin, IsEC, Warn)) {
1476 if (Error DiscardError = Temp->discard())
1477 return joinErrors(std::move(E), std::move(DiscardError));
1478 return E;
1479 }
1480
1481 // At this point, we no longer need whatever backing memory
1482 // was used to generate the NewMembers. On Windows, this buffer
1483 // could be a mapped view of the file we want to replace (if
1484 // we're updating an existing archive, say). In that case, the
1485 // rename would still succeed, but it would leave behind a
1486 // temporary file (actually the original file renamed) because
1487 // a file cannot be deleted while there's a handle open on it,
1488 // only renamed. So by freeing this buffer, this ensures that
1489 // the last open handle on the destination file, if any, is
1490 // closed before we attempt to rename.
1491 OldArchiveBuf.reset();
1492
1493 return Temp->keep(ArcName);
1494}
1495
1499 bool Deterministic, bool Thin,
1500 function_ref<void(Error)> Warn) {
1501 SmallVector<char, 0> ArchiveBufferVector;
1502 raw_svector_ostream ArchiveStream(ArchiveBufferVector);
1503
1504 if (Error E =
1505 writeArchiveToStream(ArchiveStream, NewMembers, WriteSymtab, Kind,
1506 Deterministic, Thin, std::nullopt, Warn))
1507 return std::move(E);
1508
1509 return std::make_unique<SmallVectorMemoryBuffer>(
1510 std::move(ArchiveBufferVector), /*RequiresNullTerminator=*/false);
1511}
1512
1513} // namespace llvm
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file defines the StringMap class.
unsigned uint64_t
static void printZOSMemberHeader(raw_ostream &Out, StringRef Name, const sys::TimePoint< std::chrono::seconds > &ModTime, unsigned UID, unsigned GID, unsigned Perms, uint64_t Size)
static void printNBits(raw_ostream &Out, object::Archive::Kind Kind, uint64_t Val)
bool isImportDescriptor(StringRef Name)
static sys::TimePoint< std::chrono::seconds > now(bool Deterministic)
static bool isDarwin(object::Archive::Kind Kind)
static uint64_t computeECSymbolsSize(SymMap &SymMap, uint32_t *Padding=nullptr)
static Expected< std::vector< unsigned > > getSymbols(SymbolicFile *Obj, uint16_t Index, raw_ostream &SymNames, SymMap *SymMap)
static bool is64BitSymbolicFile(const SymbolicFile *SymObj)
static void printMemberHeader(raw_ostream &Out, uint64_t Pos, raw_ostream &StringTable, StringMap< uint64_t > &MemberNames, object::Archive::Kind Kind, bool Thin, const NewArchiveMember &M, StringRef MemberName, sys::TimePoint< std::chrono::seconds > ModTime, uint64_t Size)
static uint64_t computeHeadersSize(object::Archive::Kind Kind, uint64_t NumMembers, uint64_t StringMemberSize, uint64_t NumSyms, uint64_t SymNamesSize, SymMap *SymMap)
static bool isBSDLike(object::Archive::Kind Kind)
static void printBSDMemberHeader(raw_ostream &Out, uint64_t Pos, StringRef Name, const sys::TimePoint< std::chrono::seconds > &ModTime, unsigned UID, unsigned GID, unsigned Perms, uint64_t Size)
static void writeSymbolTable(raw_ostream &Out, object::Archive::Kind Kind, bool Deterministic, ArrayRef< MemberData > Members, StringRef StringTable, uint64_t MembersOffset, unsigned NumSyms, uint64_t PrevMemberOffset=0, uint64_t NextMemberOffset=0, bool Is64Bit=false)
static const uint32_t MinBigArchiveMemDataAlign
static void writeSymbolMap(raw_ostream &Out, object::Archive::Kind Kind, bool Deterministic, ArrayRef< MemberData > Members, SymMap &SymMap, uint64_t MembersOffset)
static MemberData computeStringTable(StringRef Names)
uint16_t getAuxMaxAlignment(uint16_t AuxHeaderSize, AuxiliaryHeader *AuxHeader, uint16_t Log2OfMaxAlign)
static const uint32_t Log2OfAIXPageSize
static bool isZOSArchive(object::Archive::Kind Kind)
static bool isECObject(object::SymbolicFile &Obj)
static Expected< std::unique_ptr< SymbolicFile > > getSymbolicFile(MemoryBufferRef Buf, LLVMContext &Context, object::Archive::Kind Kind, function_ref< void(Error)> Warn)
static bool isAIXBigArchive(object::Archive::Kind Kind)
static void writeSymbolTableHeader(raw_ostream &Out, object::Archive::Kind Kind, bool Deterministic, uint64_t Size, uint64_t PrevMemberOffset=0, uint64_t NextMemberOffset=0)
static void printRestOfMemberHeader(raw_ostream &Out, const sys::TimePoint< std::chrono::seconds > &ModTime, unsigned UID, unsigned GID, unsigned Perms, uint64_t Size)
static uint64_t computeSymbolTableSize(object::Archive::Kind Kind, uint64_t NumSyms, uint64_t OffsetSize, uint64_t StringTableSize, uint32_t *Padding=nullptr)
static bool isArchiveSymbol(const object::BasicSymbolRef &S)
static bool isCOFFArchive(object::Archive::Kind Kind)
static Expected< std::vector< MemberData > > computeMemberData(raw_ostream &StringTable, raw_ostream &SymNames, object::Archive::Kind Kind, bool Thin, bool Deterministic, SymtabWritingMode NeedSymbols, SymMap *SymMap, LLVMContext &Context, ArrayRef< NewArchiveMember > NewMembers, std::optional< bool > IsEC, function_ref< void(Error)> Warn)
static void writeECSymbols(raw_ostream &Out, object::Archive::Kind Kind, bool Deterministic, ArrayRef< MemberData > Members, SymMap &SymMap)
static uint64_t computeSymbolMapSize(uint64_t NumObj, SymMap &SymMap, uint32_t *Padding=nullptr)
static void printLE(raw_ostream &Out, T Val)
static void printGNUSmallMemberHeader(raw_ostream &Out, StringRef Name, const sys::TimePoint< std::chrono::seconds > &ModTime, unsigned UID, unsigned GID, unsigned Perms, uint64_t Size)
static bool useStringTable(bool Thin, StringRef Name)
static bool is64BitKind(object::Archive::Kind Kind)
static uint32_t getMemberAlignment(SymbolicFile *SymObj)
static void printBigArchiveMemberHeader(raw_ostream &Out, StringRef Name, const sys::TimePoint< std::chrono::seconds > &ModTime, unsigned UID, unsigned GID, unsigned Perms, uint64_t Size, uint64_t PrevOffset, uint64_t NextOffset)
static void printWithSpacePadding(raw_ostream &OS, T Data, unsigned Size)
static bool isAnyArm64COFF(object::SymbolicFile &Obj)
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
#define offsetof(TYPE, MEMBER)
#define I(x, y, z)
Definition MD5.cpp:57
#define T
#define P(N)
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
size_t size() const
Get the array size.
Definition ArrayRef.h:141
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
StringRef getBuffer() const
This interface provides simple read-only access to a block of memory, and provides simple methods for...
static ErrorOr< std::unique_ptr< MemoryBuffer > > getOpenFile(sys::fs::file_t FD, const Twine &Filename, uint64_t FileSize, bool RequiresNullTerminator=true, bool IsVolatile=false, std::optional< Align > Alignment=std::nullopt)
Given an already-open file descriptor, read the file and return a MemoryBuffer.
static std::unique_ptr< MemoryBuffer > getMemBuffer(StringRef InputData, StringRef BufferName="", bool RequiresNullTerminator=true)
Open the specified memory range as a MemoryBuffer.
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition SmallString.h:26
StringRef str() const
Explicit conversion to StringRef.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringMap - This is an unconventional map that is specialized for handling keys that are "strings",...
Definition StringMap.h:129
bool insert(MapEntryTy *KeyValue)
insert - Insert the specified key/value pair into the map.
Definition StringMap.h:311
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
std::string str() const
Get the contents as an std::string.
Definition StringRef.h:222
constexpr size_t size() const
Get the string size.
Definition StringRef.h:144
A table of densely packed, null-terminated strings indexed by offset.
Definition StringTable.h:34
constexpr size_t size() const
Returns the byte size of the table.
Triple - Helper class for working with autoconf configuration names.
Definition Triple.h:48
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
An efficient, type-erasing, non-owning reference to a callable.
Expected< unsigned > getGID() const
Definition Archive.h:281
LLVM_ABI Expected< MemoryBufferRef > getMemoryBufferRef() const
Definition Archive.cpp:762
Expected< unsigned > getUID() const
Definition Archive.h:280
Expected< sys::fs::perms > getAccessMode() const
Definition Archive.h:283
Expected< sys::TimePoint< std::chrono::seconds > > getLastModified() const
Definition Archive.h:272
static object::Archive::Kind getDefaultKind()
Definition Archive.cpp:1110
static object::Archive::Kind getDefaultKindForTriple(const Triple &T)
Definition Archive.cpp:1098
static const uint64_t MaxMemberSize
Size field is 10 decimal digits long.
Definition Archive.h:396
This is a value type class that represents a single symbol in the list of symbols in the object file.
Expected< uint32_t > getFlags() const
Get symbol flags (bitwise OR of SymbolRef::Flags)
static Expected< OwningBinary< ObjectFile > > createObjectFile(StringRef ObjectPath)
static Expected< std::unique_ptr< SymbolicFile > > createSymbolicFile(MemoryBufferRef Object, llvm::file_magic Type, LLVMContext *Context, bool InitContent=true)
virtual bool is64Bit() const =0
static bool isSymbolicFile(file_magic Type, const LLVMContext *Context)
const XCOFFAuxiliaryHeader32 * auxiliaryHeader32() const
const XCOFFFileHeader64 * fileHeader64() const
const XCOFFFileHeader32 * fileHeader32() const
const XCOFFAuxiliaryHeader64 * auxiliaryHeader64() const
A raw_ostream that writes to a file descriptor.
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
uint64_t tell() const
tell - Return the current offset with the file.
raw_ostream & indent(unsigned NumSpaces)
indent - Insert 'NumSpaces' spaces.
A raw_ostream that writes to an std::string.
A raw_ostream that writes to an SmallVector or SmallString.
static LLVM_ABI Expected< TempFile > create(const Twine &Model, unsigned Mode=all_read|all_write, OpenFlags ExtraFlags=OF_None)
This creates a temporary file with createUniqueFile and schedules it for deletion with sys::RemoveFil...
Represents the result of a call to sys::fs::status().
Definition FileSystem.h:222
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ IMAGE_FILE_MACHINE_ARM64
Definition COFF.h:101
bool isAnyArm64(T Machine)
Definition COFF.h:130
LLVM_ABI std::error_code convertToEBCDIC(StringRef Source, SmallVectorImpl< char > &Result)
constexpr std::string_view NullImportDescriptorSymbolName
const char ZOSArchiveMagic[]
Definition Archive.h:37
constexpr std::string_view NullThunkDataPrefix
constexpr std::string_view NullThunkDataSuffix
constexpr std::string_view ImportDescriptorPrefix
void write(void *memory, value_type value, endianness endian)
Write a value to memory with a particular endianness.
Definition Endian.h:82
LLVM_ABI std::error_code closeFile(file_t &F)
Close the file object.
LLVM_ABI const file_t kInvalidFile
LLVM_ABI Expected< file_t > openNativeFileForRead(const Twine &Name, OpenFlags Flags=OF_None, SmallVectorImpl< char > *RealPath=nullptr)
Opens the file with the given name in a read-only mode, returning its open file descriptor.
LLVM_ABI std::error_code make_absolute(SmallVectorImpl< char > &path)
Make path an absolute path.
Definition Path.cpp:979
LLVM_ABI std::error_code status(const Twine &path, file_status &result, bool follow=true)
Get file status as if by POSIX stat().
LLVM_ABI const_iterator begin(StringRef path LLVM_LIFETIME_BOUND, Style style=Style::native)
Get begin iterator over path.
Definition Path.cpp:237
LLVM_ABI bool remove_dots(SmallVectorImpl< char > &path, bool remove_dot_dot=false, Style style=Style::native)
Remove '.
Definition Path.cpp:779
LLVM_ABI StringRef parent_path(StringRef path LLVM_LIFETIME_BOUND, Style style=Style::native)
Get parent path.
Definition Path.cpp:478
LLVM_ABI std::string convert_to_slash(StringRef path, Style style=Style::native)
Replaces backslashes with slashes if Windows.
Definition Path.cpp:585
LLVM_ABI StringRef root_name(StringRef path LLVM_LIFETIME_BOUND, Style style=Style::native)
Get root name.
Definition Path.cpp:384
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:467
LLVM_ABI const_iterator end(StringRef path LLVM_LIFETIME_BOUND)
Get end iterator over path.
Definition Path.cpp:246
std::chrono::time_point< std::chrono::system_clock, D > TimePoint
A time point on the system clock.
Definition Chrono.h:34
TimePoint< std::chrono::seconds > toTimePoint(std::time_t T)
Convert a std::time_t to a TimePoint.
Definition Chrono.h:65
std::time_t toTimeT(TimePoint<> TP)
Convert a TimePoint to std::time_t.
Definition Chrono.h:50
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI file_magic identify_magic(StringRef magic)
Identify the type of a binary file based on how magical it is.
Definition Magic.cpp:33
LLVM_ABI void logAllUnhandledErrors(Error E, raw_ostream &OS, Twine ErrorBanner={})
Log all errors (if any) in E to OS.
Definition Error.cpp:61
LLVM_ABI Expected< std::unique_ptr< MemoryBuffer > > writeArchiveToBuffer(ArrayRef< NewArchiveMember > NewMembers, SymtabWritingMode WriteSymtab, object::Archive::Kind Kind, bool Deterministic, bool Thin, function_ref< void(Error)> Warn=warnToStderr)
Error createFileError(const Twine &F, Error E)
Concatenate a source file path and/or name with an Error.
Definition Error.h:1415
Printable print(const GCNRegPressure &RP, const GCNSubtarget *ST=nullptr, unsigned DynamicVGPRBlockSize=0)
LLVM_ABI Error writeArchive(StringRef ArcName, ArrayRef< NewArchiveMember > NewMembers, SymtabWritingMode WriteSymtab, object::Archive::Kind Kind, bool Deterministic, bool Thin, std::unique_ptr< MemoryBuffer > OldArchiveBuf=nullptr, std::optional< bool > IsEC=std::nullopt, function_ref< void(Error)> Warn=warnToStderr)
std::error_code make_error_code(BitcodeError E)
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
LLVM_ABI Expected< std::string > getBitcodeTargetTriple(MemoryBufferRef Buffer)
Read the header of the specified bitcode buffer and extract just the triple information.
@ is_a_directory
Definition Errc.h:59
auto dyn_cast_or_null(const Y &Val)
Definition Casting.h:753
Error joinErrors(Error E1, Error E2)
Concatenate errors.
Definition Error.h:442
constexpr T alignToPowerOf2(U Value, V Align)
Will overflow only if result is not representable in T.
Definition MathExtras.h:488
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
Definition Error.cpp:163
constexpr uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
Definition Alignment.h:144
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
format_object< Ts... > format(const char *Fmt, const Ts &... Vals)
These are helper functions used to produce formatted output.
Definition Format.h:102
Error make_error(ArgTs &&... Args)
Make a Error instance representing failure using the given error info type.
Definition Error.h:340
uint64_t offsetToAlignment(uint64_t Value, Align Alignment)
Returns the offset to the next integer (mod 2**64) that is greater than or equal to Value and is a mu...
Definition Alignment.h:186
LLVM_ABI raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
LLVM_ABI void warnToStderr(Error Err)
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
LLVM_ABI Expected< std::string > computeArchiveRelativePath(StringRef From, StringRef To)
LLVM_ABI Error errorCodeToError(std::error_code EC)
Helper for converting an std::error_code to a Error.
Definition Error.cpp:107
static ErrorOr< SmallString< 128 > > canonicalizePath(StringRef P)
std::error_code errnoAsErrorCode()
Helper to get errno as an std::error_code.
Definition Error.h:1256
LLVM_ABI Error writeArchiveToStream(raw_ostream &Out, ArrayRef< NewArchiveMember > NewMembers, SymtabWritingMode WriteSymtab, object::Archive::Kind Kind, bool Deterministic, bool Thin, std::optional< bool > IsEC=std::nullopt, function_ref< void(Error)> Warn=warnToStderr)
SymtabWritingMode
void consumeError(Error Err)
Consume a Error without doing anything.
Definition Error.h:1106
std::map< std::string, uint16_t > ECMap
bool UseECMap
std::map< std::string, uint16_t > Map
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39
LLVM_ABI object::Archive::Kind detectKindFromObject() const
static LLVM_ABI Expected< NewArchiveMember > getFile(StringRef FileName, bool Deterministic)
static LLVM_ABI Expected< NewArchiveMember > getOldMember(const object::Archive::Child &OldMember, bool Deterministic)
std::unique_ptr< MemoryBuffer > Buf
file_magic - An "enum class" enumeration of file types based on magic (the first N bytes of the file)...
Definition Magic.h:21
@ bitcode
Bitcode file.
Definition Magic.h:24