LLVM 20.0.0git
Archive.cpp
Go to the documentation of this file.
1//===- Archive.cpp --------------------------------------------------------===//
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#include "Archive.h"
13#include "llvm/Object/Error.h"
16
17namespace llvm {
18namespace objcopy {
19
20using namespace llvm::object;
21
24 std::vector<NewArchiveMember> NewArchiveMembers;
25 Error Err = Error::success();
26 for (const Archive::Child &Child : Ar.children(Err)) {
27 Expected<StringRef> ChildNameOrErr = Child.getName();
28 if (!ChildNameOrErr)
29 return createFileError(Ar.getFileName(), ChildNameOrErr.takeError());
30
31 Expected<std::unique_ptr<Binary>> ChildOrErr = Child.getAsBinary();
32 if (!ChildOrErr)
33 return createFileError(Ar.getFileName() + "(" + *ChildNameOrErr + ")",
34 ChildOrErr.takeError());
35
37 raw_svector_ostream MemStream(Buffer);
38
39 if (Error E = executeObjcopyOnBinary(Config, *ChildOrErr->get(), MemStream))
40 return std::move(E);
41
43 Child, Config.getCommonConfig().DeterministicArchives);
44 if (!Member)
45 return createFileError(Ar.getFileName(), Member.takeError());
46
47 Member->Buf = std::make_unique<SmallVectorMemoryBuffer>(
48 std::move(Buffer), ChildNameOrErr.get());
49 Member->MemberName = Member->Buf->getBufferIdentifier();
50 NewArchiveMembers.push_back(std::move(*Member));
51 }
52 if (Err)
53 return createFileError(Config.getCommonConfig().InputFilename,
54 std::move(Err));
55 return std::move(NewArchiveMembers);
56}
57
58// For regular archives this function simply calls llvm::writeArchive,
59// For thin archives it writes the archive file itself as well as its members.
62 SymtabWritingMode WriteSymtab,
63 object::Archive::Kind Kind, bool Deterministic,
64 bool Thin) {
65 if (Kind == object::Archive::K_BSD && !NewMembers.empty() &&
66 NewMembers.front().detectKindFromObject() == object::Archive::K_DARWIN)
68
69 if (Error E = writeArchive(ArcName, NewMembers, WriteSymtab, Kind,
70 Deterministic, Thin))
71 return createFileError(ArcName, std::move(E));
72
73 if (!Thin)
74 return Error::success();
75
76 for (const NewArchiveMember &Member : NewMembers) {
77 // For regular files (as is the case for deepWriteArchive),
78 // FileOutputBuffer::create will return OnDiskBuffer.
79 // OnDiskBuffer uses a temporary file and then renames it. So in reality
80 // there is no inefficiency / duplicated in-memory buffers in this case. For
81 // now in-memory buffers can not be completely avoided since
82 // NewArchiveMember still requires them even though writeArchive does not
83 // write them on disk.
85 FileOutputBuffer::create(Member.MemberName, Member.Buf->getBufferSize(),
87 if (!FB)
88 return FB.takeError();
89 std::copy(Member.Buf->getBufferStart(), Member.Buf->getBufferEnd(),
90 (*FB)->getBufferStart());
91 if (Error E = (*FB)->commit())
92 return E;
93 }
94 return Error::success();
95}
96
98 const object::Archive &Ar) {
99 Expected<std::vector<NewArchiveMember>> NewArchiveMembersOrErr =
101 if (!NewArchiveMembersOrErr)
102 return NewArchiveMembersOrErr.takeError();
103 const CommonConfig &CommonConfig = Config.getCommonConfig();
104 return deepWriteArchive(CommonConfig.OutputFilename, *NewArchiveMembersOrErr,
108 Ar.isThin());
109}
110
111} // end namespace objcopy
112} // 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:171
bool empty() const
empty - Check if the array is empty.
Definition: ArrayRef.h:163
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:1196
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
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:1284
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:60
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:36
Error executeObjcopyOnArchive(const MultiFormatConfig &Config, const object::Archive &Ar)
Applies the transformations described by Config to each member in archive Ar.
Definition: Archive.cpp:97
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:23
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:1385
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)
SymtabWritingMode
Definition: ArchiveWriter.h:43
static Expected< NewArchiveMember > getOldMember(const object::Archive::Child &OldMember, bool Deterministic)