LLVM 19.0.0git
Go to the documentation of this file.
1//===- Archive.cpp --------------------------------------------------------===//
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
9#include "Archive.h"
13#include "llvm/Object/Error.h"
14#include "llvm/Object/MachO.h"
18namespace llvm {
19namespace objcopy {
21using namespace llvm::object;
25 std::vector<NewArchiveMember> NewArchiveMembers;
26 Error Err = Error::success();
27 for (const Archive::Child &Child : Ar.children(Err)) {
28 Expected<StringRef> ChildNameOrErr = Child.getName();
29 if (!ChildNameOrErr)
30 return createFileError(Ar.getFileName(), ChildNameOrErr.takeError());
32 Expected<std::unique_ptr<Binary>> ChildOrErr = Child.getAsBinary();
33 if (!ChildOrErr)
34 return createFileError(Ar.getFileName() + "(" + *ChildNameOrErr + ")",
35 ChildOrErr.takeError());
38 raw_svector_ostream MemStream(Buffer);
40 if (Error E = executeObjcopyOnBinary(Config, *ChildOrErr->get(), MemStream))
41 return std::move(E);
44 Child, Config.getCommonConfig().DeterministicArchives);
45 if (!Member)
46 return createFileError(Ar.getFileName(), Member.takeError());
48 Member->Buf = std::make_unique<SmallVectorMemoryBuffer>(
49 std::move(Buffer), ChildNameOrErr.get());
50 Member->MemberName = Member->Buf->getBufferIdentifier();
51 NewArchiveMembers.push_back(std::move(*Member));
52 }
53 if (Err)
54 return createFileError(Config.getCommonConfig().InputFilename,
55 std::move(Err));
56 return std::move(NewArchiveMembers);
59// For regular archives this function simply calls llvm::writeArchive,
60// For thin archives it writes the archive file itself as well as its members.
63 SymtabWritingMode WriteSymtab,
64 object::Archive::Kind Kind, bool Deterministic,
65 bool Thin) {
66 if (Kind == object::Archive::K_BSD && !NewMembers.empty() &&
67 NewMembers.front().detectKindFromObject() == object::Archive::K_DARWIN)
70 if (Error E = writeArchive(ArcName, NewMembers, WriteSymtab, Kind,
71 Deterministic, Thin))
72 return createFileError(ArcName, std::move(E));
74 if (!Thin)
75 return Error::success();
77 for (const NewArchiveMember &Member : NewMembers) {
78 // For regular files (as is the case for deepWriteArchive),
79 // FileOutputBuffer::create will return OnDiskBuffer.
80 // OnDiskBuffer uses a temporary file and then renames it. So in reality
81 // there is no inefficiency / duplicated in-memory buffers in this case. For
82 // now in-memory buffers can not be completely avoided since
83 // NewArchiveMember still requires them even though writeArchive does not
84 // write them on disk.
86 FileOutputBuffer::create(Member.MemberName, Member.Buf->getBufferSize(),
88 if (!FB)
89 return FB.takeError();
90 std::copy(Member.Buf->getBufferStart(), Member.Buf->getBufferEnd(),
91 (*FB)->getBufferStart());
92 if (Error E = (*FB)->commit())
93 return E;
94 }
95 return Error::success();
99 const object::Archive &Ar) {
100 Expected<std::vector<NewArchiveMember>> NewArchiveMembersOrErr =
102 if (!NewArchiveMembersOrErr)
103 return NewArchiveMembersOrErr.takeError();
104 const CommonConfig &CommonConfig = Config.getCommonConfig();
105 return deepWriteArchive(CommonConfig.OutputFilename, *NewArchiveMembersOrErr,
109 Ar.isThin());
112} // end namespace objcopy
113} // end namespace llvm
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
RelaxConfig Config
Definition: ELF_riscv.cpp:506
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
const T & front() const
front - Get the first element.
Definition: ArrayRef.h:168
bool empty() const
empty - Check if the array is empty.
Definition: ArrayRef.h:160
Lightweight error class with error context and mandatory checking.
Definition: Error.h:160
static ErrorSuccess success()
Create a success value.
Definition: Error.h:337
Tagged union holding either a T or a Error.
Definition: Error.h:481
Error takeError()
Take ownership of the stored error.
Definition: Error.h:608
reference get()
Returns a reference to the stored T value.
Definition: Error.h:578
static Expected< std::unique_ptr< FileOutputBuffer > > create(StringRef FilePath, size_t Size, unsigned Flags=0)
Factory method to create an OutputBuffer object which manages a read/write buffer of the specified si...
@ F_executable
Set the 'x' bit on the resulting file.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1209
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
bool isThin() const
Definition: Archive.h:340
iterator_range< child_iterator > children(Error &Err, bool SkipInternal=true) const
Definition: Archive.h:346
bool hasSymbolTable() const
Definition: Archive.cpp:1285
Kind kind() const
Definition: Archive.h:339
StringRef getFileName() const
Definition: Binary.cpp:41
A raw_ostream that writes to an SmallVector or SmallString.
Definition: raw_ostream.h:691
static Error deepWriteArchive(StringRef ArcName, ArrayRef< NewArchiveMember > NewMembers, SymtabWritingMode WriteSymtab, object::Archive::Kind Kind, bool Deterministic, bool Thin)
Definition: Archive.cpp:61
Error executeObjcopyOnBinary(const MultiFormatConfig &Config, object::Binary &In, raw_ostream &Out)
Applies the transformations described by Config to In and writes the result into Out.
Definition: ObjCopy.cpp:38
Error executeObjcopyOnArchive(const MultiFormatConfig &Config, const object::Archive &Ar)
Applies the transformations described by Config to each member in archive Ar.
Definition: Archive.cpp:98
Expected< std::vector< NewArchiveMember > > createNewArchiveMembers(const MultiFormatConfig &Config, const Archive &Ar)
Applies the transformations described by Config to each member in archive Ar.
Definition: Archive.cpp:24
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
Error createFileError(const Twine &F, Error E)
Concatenate a source file path and/or name with an Error.
Definition: Error.h:1380
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)
Definition: ArchiveWriter.h:43
static Expected< NewArchiveMember > getOldMember(const object::Archive::Child &OldMember, bool Deterministic)