LLVM 24.0.0git
MachOObjcopy.cpp
Go to the documentation of this file.
1//===- MachOObjcopy.cpp -----------------------------------------*- 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
10#include "Archive.h"
11#include "MachOReader.h"
12#include "MachOWriter.h"
13#include "llvm/ADT/DenseSet.h"
22#include "llvm/Support/Errc.h"
23#include "llvm/Support/Error.h"
25#include "llvm/Support/Path.h"
27
28using namespace llvm;
29using namespace llvm::objcopy;
30using namespace llvm::objcopy::macho;
31using namespace llvm::object;
32
33using SectionPred = std::function<bool(const std::unique_ptr<Section> &Sec)>;
34using LoadCommandPred = std::function<bool(const LoadCommand &LC)>;
35
36#ifndef NDEBUG
38 // TODO: Add support for LC_REEXPORT_DYLIB, LC_LOAD_UPWARD_DYLIB and
39 // LC_LAZY_LOAD_DYLIB
40 return LC.MachOLoadCommand.load_command_data.cmd == MachO::LC_RPATH ||
41 LC.MachOLoadCommand.load_command_data.cmd == MachO::LC_ID_DYLIB ||
42 LC.MachOLoadCommand.load_command_data.cmd == MachO::LC_LOAD_DYLIB ||
43 LC.MachOLoadCommand.load_command_data.cmd == MachO::LC_LOAD_WEAK_DYLIB;
44}
45#endif
46
49 "unsupported load command encountered");
50
51 return StringRef(reinterpret_cast<const char *>(LC.Payload.data()),
52 LC.Payload.size())
53 .rtrim('\0');
54}
55
56static Error removeSections(const CommonConfig &Config, Object &Obj) {
57 SectionPred RemovePred = [](const std::unique_ptr<Section> &) {
58 return false;
59 };
60
61 if (!Config.ToRemove.empty()) {
62 RemovePred = [&Config, RemovePred](const std::unique_ptr<Section> &Sec) {
63 return Config.ToRemove.matches(Sec->CanonicalName);
64 };
65 }
66
67 if (Config.StripAll || Config.StripDebug) {
68 // Remove all debug sections.
69 RemovePred = [RemovePred](const std::unique_ptr<Section> &Sec) {
70 if (Sec->Segname == "__DWARF")
71 return true;
72
73 return RemovePred(Sec);
74 };
75 }
76
77 if (!Config.OnlySection.empty()) {
78 // Overwrite RemovePred because --only-section takes priority.
79 RemovePred = [&Config](const std::unique_ptr<Section> &Sec) {
80 return !Config.OnlySection.matches(Sec->CanonicalName);
81 };
82 }
83
84 return Obj.removeSections(RemovePred);
85}
86
87static void markSymbols(const CommonConfig &Config, Object &Obj) {
88 // Symbols referenced from the indirect symbol table must not be removed.
89 for (IndirectSymbolEntry &ISE : Obj.IndirectSymTable.Symbols)
90 if (ISE.Symbol)
91 (*ISE.Symbol)->Referenced = true;
92
93 // --strip-all removes relocations, so their symbols need not be preserved.
94 if (Config.StripAll)
95 return;
96
97 for (const LoadCommand &LC : Obj.LoadCommands)
98 for (const std::unique_ptr<Section> &Sec : LC.Sections)
99 for (const RelocationInfo &R : Sec->Relocations)
100 if (R.Symbol && *R.Symbol)
101 (*R.Symbol)->Referenced = true;
102}
103
104static void updateAndRemoveSymbols(const CommonConfig &Config,
106 Object &Obj) {
107 Obj.SymTable.updateSymbols([&](SymbolEntry &Sym) {
108 if (Config.SymbolsToSkip.matches(Sym.Name))
109 return;
110
111 if (!Sym.isUndefinedSymbol() && Config.SymbolsToLocalize.matches(Sym.Name))
112 Sym.n_type &= ~MachO::N_EXT;
113
114 // Note: these two globalize flags have very similar names but different
115 // meanings:
116 //
117 // --globalize-symbol: promote a symbol to global
118 // --keep-global-symbol: all symbols except for these should be made local
119 //
120 // If --globalize-symbol is specified for a given symbol, it will be
121 // global in the output file even if it is not included via
122 // --keep-global-symbol. Because of that, make sure to check
123 // --globalize-symbol second.
124 if (!Sym.isUndefinedSymbol() && !Config.SymbolsToKeepGlobal.empty() &&
125 !Config.SymbolsToKeepGlobal.matches(Sym.Name))
126 Sym.n_type &= ~MachO::N_EXT;
127
128 if (!Sym.isUndefinedSymbol() && Config.SymbolsToGlobalize.matches(Sym.Name))
129 Sym.n_type |= MachO::N_EXT;
130
131 if (Sym.isExternalSymbol() && !Sym.isUndefinedSymbol() &&
132 (Config.Weaken || Config.SymbolsToWeaken.matches(Sym.Name)))
134
135 auto I = Config.SymbolsToRename.find(Sym.Name);
136 if (I != Config.SymbolsToRename.end())
137 Sym.Name = std::string(I->getValue());
138 });
139
140 auto RemovePred = [&Config, &MachOConfig,
141 &Obj](const std::unique_ptr<SymbolEntry> &N) {
142 if (N->Referenced)
143 return false;
144 if (MachOConfig.KeepUndefined && N->isUndefinedSymbol())
145 return false;
146 if (N->n_desc & MachO::REFERENCED_DYNAMICALLY)
147 return false;
148 if (Config.StripAll)
149 return true;
150 if (Config.DiscardMode == DiscardType::All && !(N->n_type & MachO::N_EXT))
151 return true;
152 // This behavior is consistent with cctools' strip.
153 if (Config.StripDebug && (N->n_type & MachO::N_STAB))
154 return true;
155 // This behavior is consistent with cctools' strip.
157 (Obj.Header.Flags & MachO::MH_DYLDLINK) && Obj.SwiftVersion &&
158 *Obj.SwiftVersion && N->isSwiftSymbol())
159 return true;
160 return false;
161 };
162
163 Obj.SymTable.removeSymbols(RemovePred);
164}
165
166template <typename LCType>
169 "unsupported load command encountered");
170
171 uint32_t NewCmdsize = alignToPowerOf2(sizeof(LCType) + S.size() + 1, 8);
172
173 LC.MachOLoadCommand.load_command_data.cmdsize = NewCmdsize;
174 LC.Payload.assign(NewCmdsize - sizeof(LCType), 0);
175 llvm::copy(S, LC.Payload.begin());
176}
177
179 LoadCommand LC;
180 MachO::rpath_command RPathLC;
181 RPathLC.cmd = MachO::LC_RPATH;
182 RPathLC.path = sizeof(MachO::rpath_command);
183 RPathLC.cmdsize =
184 alignToPowerOf2(sizeof(MachO::rpath_command) + Path.size() + 1, 8);
185 LC.MachOLoadCommand.rpath_command_data = RPathLC;
186 LC.Payload.assign(RPathLC.cmdsize - sizeof(MachO::rpath_command), 0);
187 llvm::copy(Path, LC.Payload.begin());
188 return LC;
189}
190
192 // Remove RPaths.
193 DenseSet<StringRef> RPathsToRemove(MachOConfig.RPathsToRemove.begin(),
195
196 LoadCommandPred RemovePred = [&RPathsToRemove,
197 &MachOConfig](const LoadCommand &LC) {
198 if (LC.MachOLoadCommand.load_command_data.cmd == MachO::LC_RPATH) {
199 // When removing all RPaths we don't need to care
200 // about what it contains
202 return true;
203
204 StringRef RPath = getPayloadString(LC);
205 if (RPathsToRemove.count(RPath)) {
206 RPathsToRemove.erase(RPath);
207 return true;
208 }
209 }
210 return false;
211 };
212
213 if (Error E = Obj.removeLoadCommands(RemovePred))
214 return E;
215
216 // Emit an error if the Mach-O binary does not contain an rpath path name
217 // specified in -delete_rpath.
218 for (StringRef RPath : MachOConfig.RPathsToRemove) {
219 if (RPathsToRemove.count(RPath))
221 "no LC_RPATH load command with path: %s",
222 RPath.str().c_str());
223 }
224
225 DenseSet<StringRef> RPaths;
226
227 // Get all existing RPaths.
228 for (LoadCommand &LC : Obj.LoadCommands) {
229 if (LC.MachOLoadCommand.load_command_data.cmd == MachO::LC_RPATH)
230 RPaths.insert(getPayloadString(LC));
231 }
232
233 // Throw errors for invalid RPaths.
234 for (const auto &OldNew : MachOConfig.RPathsToUpdate) {
235 StringRef Old = OldNew.getFirst();
236 StringRef New = OldNew.getSecond();
237 if (!RPaths.contains(Old))
239 "no LC_RPATH load command with path: " + Old);
240 if (RPaths.contains(New))
242 "rpath '" + New +
243 "' would create a duplicate load command");
244 }
245
246 // Update load commands.
247 for (LoadCommand &LC : Obj.LoadCommands) {
248 switch (LC.MachOLoadCommand.load_command_data.cmd) {
249 case MachO::LC_ID_DYLIB:
253 break;
254
255 case MachO::LC_RPATH: {
256 StringRef RPath = getPayloadString(LC);
257 StringRef NewRPath = MachOConfig.RPathsToUpdate.lookup(RPath);
258 if (!NewRPath.empty())
260 break;
261 }
262
263 // TODO: Add LC_REEXPORT_DYLIB, LC_LAZY_LOAD_DYLIB, and LC_LOAD_UPWARD_DYLIB
264 // here once llvm-objcopy supports them.
265 case MachO::LC_LOAD_DYLIB:
266 case MachO::LC_LOAD_WEAK_DYLIB:
267 StringRef InstallName = getPayloadString(LC);
268 StringRef NewInstallName =
269 MachOConfig.InstallNamesToUpdate.lookup(InstallName);
270 if (!NewInstallName.empty())
272 NewInstallName);
273 break;
274 }
275 }
276
277 // Add new RPaths.
278 for (StringRef RPath : MachOConfig.RPathToAdd) {
279 if (RPaths.contains(RPath))
281 "rpath '" + RPath +
282 "' would create a duplicate load command");
283 RPaths.insert(RPath);
284 Obj.LoadCommands.push_back(buildRPathLoadCommand(RPath));
285 }
286
287 for (StringRef RPath : MachOConfig.RPathToPrepend) {
288 if (RPaths.contains(RPath))
290 "rpath '" + RPath +
291 "' would create a duplicate load command");
292
293 RPaths.insert(RPath);
294 Obj.LoadCommands.insert(Obj.LoadCommands.begin(),
295 buildRPathLoadCommand(RPath));
296 }
297
298 // Unlike appending rpaths, the indexes of subsequent load commands must
299 // be recalculated after prepending one.
300 if (!MachOConfig.RPathToPrepend.empty())
301 Obj.updateLoadCommandIndexes();
302
303 // Remove any empty segments if required.
304 if (!MachOConfig.EmptySegmentsToRemove.empty()) {
305 auto RemovePred = [&MachOConfig](const LoadCommand &LC) {
306 if (LC.MachOLoadCommand.load_command_data.cmd == MachO::LC_SEGMENT_64 ||
307 LC.MachOLoadCommand.load_command_data.cmd == MachO::LC_SEGMENT) {
308 return LC.Sections.empty() &&
310 }
311 return false;
312 };
313 if (Error E = Obj.removeLoadCommands(RemovePred))
314 return E;
315 }
316
317 return Error::success();
318}
319
322 for (LoadCommand &LC : Obj.LoadCommands)
323 for (const std::unique_ptr<Section> &Sec : LC.Sections) {
324 if (Sec->CanonicalName == SecName) {
326 FileOutputBuffer::create(Filename, Sec->Content.size());
327 if (!BufferOrErr)
328 return createFileError(Filename, BufferOrErr.takeError());
329 std::unique_ptr<FileOutputBuffer> Buf = std::move(*BufferOrErr);
330 llvm::copy(Sec->Content, Buf->getBufferStart());
331
332 if (Error E = Buf->commit())
333 return createFileError(Filename, std::move(E));
334 return Error::success();
335 }
336 }
337
339 "section '%s' not found", SecName.str().c_str());
340}
341
342static Error addSection(const NewSectionInfo &NewSection, Object &Obj) {
343 std::pair<StringRef, StringRef> Pair = NewSection.SectionName.split(',');
344 StringRef TargetSegName = Pair.first;
345 Section Sec(TargetSegName, Pair.second);
346 Sec.Content =
347 Obj.NewSectionsContents.save(NewSection.SectionData->getBuffer());
348 Sec.Size = Sec.Content.size();
349
350 // Add a section into an existing segment load command. In MH_OBJECT files,
351 // all sections are in a single LC_SEGMENT/LC_SEGMENT_64 load command, and the
352 // segment name in each section header is the segment that the linker puts
353 // the section into. So for MH_OBJECT files, always add the section to the
354 // first segment load command, which should be the only one.
355 const bool IsObject = Obj.Header.FileType == MachO::MH_OBJECT;
356 for (LoadCommand &LC : Obj.LoadCommands) {
357 std::optional<StringRef> SegName = LC.getSegmentName();
358 if (SegName && (IsObject || SegName == TargetSegName)) {
359 uint64_t Addr = *LC.getSegmentVMAddr();
360 for (const std::unique_ptr<Section> &S : LC.Sections)
361 Addr = std::max(Addr, S->Addr + S->Size);
362 LC.Sections.push_back(std::make_unique<Section>(Sec));
363 LC.Sections.back()->Addr = Addr;
364 return Error::success();
365 }
366 }
367
368 // There's no segment named TargetSegName. Create a new load command and
369 // Insert a new section into it.
370 LoadCommand &NewSegment =
371 Obj.addSegment(TargetSegName, alignToPowerOf2(Sec.Size, 16384));
372 NewSegment.Sections.push_back(std::make_unique<Section>(Sec));
373 NewSegment.Sections.back()->Addr = *NewSegment.getSegmentVMAddr();
374 return Error::success();
375}
376
378 StringRef SegName;
379 std::tie(SegName, SecName) = SecName.split(",");
380 // For compactness, intermediate object files (MH_OBJECT) contain
381 // only one segment in which all sections are placed.
382 // The static linker places each section in the named segment when building
383 // the final product (any file that is not of type MH_OBJECT).
384 //
385 // Source:
386 // https://math-atlas.sourceforge.net/devel/assembly/MachORuntime.pdf
387 // page 57
388 if (O.Header.FileType == MachO::HeaderFileType::MH_OBJECT) {
389 for (const auto& LC : O.LoadCommands)
390 for (const auto& Sec : LC.Sections)
391 if (Sec->Segname == SegName && Sec->Sectname == SecName)
392 return *Sec;
393
394 StringRef ErrMsg = "could not find section with name '%s' in '%s' segment";
395 return createStringError(errc::invalid_argument, ErrMsg.str().c_str(),
396 SecName.str().c_str(), SegName.str().c_str());
397 }
398 auto FoundSeg =
399 llvm::find_if(O.LoadCommands, [SegName](const LoadCommand &LC) {
400 return LC.getSegmentName() == SegName;
401 });
402 if (FoundSeg == O.LoadCommands.end())
404 "could not find segment with name '%s'",
405 SegName.str().c_str());
406 auto FoundSec = llvm::find_if(FoundSeg->Sections,
407 [SecName](const std::unique_ptr<Section> &Sec) {
408 return Sec->Sectname == SecName;
409 });
410 if (FoundSec == FoundSeg->Sections.end())
412 "could not find section with name '%s'",
413 SecName.str().c_str());
414
415 assert(FoundSec->get()->CanonicalName == (SegName + "," + SecName).str());
416 return **FoundSec;
417}
418
419static Error updateSection(const NewSectionInfo &NewSection, Object &O) {
420 Expected<Section &> SecToUpdateOrErr = findSection(NewSection.SectionName, O);
421
422 if (!SecToUpdateOrErr)
423 return SecToUpdateOrErr.takeError();
424 Section &Sec = *SecToUpdateOrErr;
425
426 if (NewSection.SectionData->getBufferSize() > Sec.Size)
427 return createStringError(
429 "new section cannot be larger than previous section");
430 Sec.Content = O.NewSectionsContents.save(NewSection.SectionData->getBuffer());
431 Sec.Size = Sec.Content.size();
432 return Error::success();
433}
434
435// isValidMachOCannonicalName returns success if Name is a MachO cannonical name
436// ("<segment>,<section>") and lengths of both segment and section names are
437// valid.
439 if (Name.count(',') != 1)
441 "invalid section name '%s' (should be formatted "
442 "as '<segment name>,<section name>')",
443 Name.str().c_str());
444
445 std::pair<StringRef, StringRef> Pair = Name.split(',');
446 if (Pair.first.size() > 16)
448 "too long segment name: '%s'",
449 Pair.first.str().c_str());
450 if (Pair.second.size() > 16)
452 "too long section name: '%s'",
453 Pair.second.str().c_str());
454 return Error::success();
455}
456
457static Error handleArgs(const CommonConfig &Config,
458 const MachOConfig &MachOConfig, Object &Obj) {
459 // Dump sections before add/remove for compatibility with GNU objcopy.
460 for (StringRef Flag : Config.DumpSection) {
462 StringRef FileName;
463 std::tie(SectionName, FileName) = Flag.split('=');
464 if (Error E =
465 dumpSectionToFile(SectionName, FileName, Config.InputFilename, Obj))
466 return E;
467 }
468
469 if (Error E = removeSections(Config, Obj))
470 return createFileError(Config.InputFilename, std::move(E));
471
472 // Mark symbols to determine which symbols are still needed.
473 markSymbols(Config, Obj);
474
476
477 if (Config.StripAll)
478 for (LoadCommand &LC : Obj.LoadCommands)
479 for (std::unique_ptr<Section> &Sec : LC.Sections)
480 Sec->Relocations.clear();
481
482 for (const NewSectionInfo &NewSection : Config.AddSection) {
484 return createFileError(Config.InputFilename, std::move(E));
485 if (Error E = addSection(NewSection, Obj))
486 return createFileError(Config.InputFilename, std::move(E));
487 }
488
489 for (const NewSectionInfo &NewSection : Config.UpdateSection) {
491 return createFileError(Config.InputFilename, std::move(E));
492 if (Error E = updateSection(NewSection, Obj))
493 return createFileError(Config.InputFilename, std::move(E));
494 }
495
497 return createFileError(Config.InputFilename, std::move(E));
498
499 return Error::success();
500}
501
505 raw_ostream &Out) {
508 if (!O)
509 return createFileError(Config.InputFilename, O.takeError());
510
511 if (O->get()->Header.FileType == MachO::HeaderFileType::MH_PRELOAD)
512 return createStringError(std::errc::not_supported,
513 "%s: MH_PRELOAD files are not supported",
514 Config.InputFilename.str().c_str());
515
516 if (Error E = handleArgs(Config, MachOConfig, **O))
517 return E;
518
519 // Page size used for alignment of segment sizes in Mach-O executables and
520 // dynamic libraries.
521 uint64_t PageSize;
522 switch (In.getArch()) {
526 PageSize = 16384;
527 break;
528 default:
529 PageSize = 4096;
530 }
531
532 MachOWriter Writer(**O, In.is64Bit(), In.isLittleEndian(),
533 sys::path::filename(Config.OutputFilename), PageSize, Out);
534 if (auto E = Writer.finalize())
535 return E;
536 return Writer.write();
537}
538
540 const MultiFormatConfig &Config, const MachOUniversalBinary &In,
541 raw_ostream &Out) {
544 for (const auto &O : In.objects()) {
545 Expected<std::unique_ptr<Archive>> ArOrErr = O.getAsArchive();
546 if (ArOrErr) {
547 Expected<std::vector<NewArchiveMember>> NewArchiveMembersOrErr =
548 createNewArchiveMembers(Config, **ArOrErr);
549 if (!NewArchiveMembersOrErr)
550 return NewArchiveMembersOrErr.takeError();
551 auto Kind = (*ArOrErr)->kind();
554 Expected<std::unique_ptr<MemoryBuffer>> OutputBufferOrErr =
556 *NewArchiveMembersOrErr,
557 (*ArOrErr)->hasSymbolTable() ? SymtabWritingMode::NormalSymtab
560 (*ArOrErr)->isThin());
561 if (!OutputBufferOrErr)
562 return OutputBufferOrErr.takeError();
564 object::createBinary(**OutputBufferOrErr);
565 if (!BinaryOrErr)
566 return BinaryOrErr.takeError();
567 Binaries.emplace_back(std::move(*BinaryOrErr),
568 std::move(*OutputBufferOrErr));
569 Slices.emplace_back(*cast<Archive>(Binaries.back().getBinary()),
570 O.getCPUType(), O.getCPUSubType(),
571 O.getArchFlagName(), O.getAlign());
572 continue;
573 }
574 // The methods getAsArchive, getAsObjectFile, getAsIRObject of the class
575 // ObjectForArch return an Error in case of the type mismatch. We need to
576 // check each in turn to see what kind of slice this is, so ignore errors
577 // produced along the way.
578 consumeError(ArOrErr.takeError());
579
580 Expected<std::unique_ptr<MachOObjectFile>> ObjOrErr = O.getAsObjectFile();
581 if (!ObjOrErr) {
582 consumeError(ObjOrErr.takeError());
583 return createStringError(
584 std::errc::invalid_argument,
585 "slice for '%s' of the universal Mach-O binary "
586 "'%s' is not a Mach-O object or an archive",
587 O.getArchFlagName().c_str(),
588 Config.getCommonConfig().InputFilename.str().c_str());
589 }
590 std::string ArchFlagName = O.getArchFlagName();
591
593 raw_svector_ostream MemStream(Buffer);
594
596 if (!MachO)
597 return MachO.takeError();
598
600 **ObjOrErr, MemStream))
601 return E;
602
603 auto MB = std::make_unique<SmallVectorMemoryBuffer>(
604 std::move(Buffer), ArchFlagName, /*RequiresNullTerminator=*/false);
606 if (!BinaryOrErr)
607 return BinaryOrErr.takeError();
608 Binaries.emplace_back(std::move(*BinaryOrErr), std::move(MB));
609 Slices.emplace_back(*cast<MachOObjectFile>(Binaries.back().getBinary()),
610 O.getAlign());
611 }
612
613 if (Error Err = writeUniversalBinaryToStream(Slices, Out))
614 return Err;
615
616 return Error::success();
617}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
uint64_t uint64_t
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
This file defines the DenseSet and SmallDenseSet classes.
std::function< bool(const SectionBase &Sec)> SectionPred
#define I(x, y, z)
Definition MD5.cpp:57
static Error processLoadCommands(const MachOConfig &MachOConfig, Object &Obj)
static Expected< Section & > findSection(StringRef SecName, Object &O)
static Error isValidMachOCannonicalName(StringRef Name)
static void updateLoadCommandPayloadString(LoadCommand &LC, StringRef S)
static LoadCommand buildRPathLoadCommand(StringRef Path)
static bool isLoadCommandWithPayloadString(const LoadCommand &LC)
static void markSymbols(const CommonConfig &Config, Object &Obj)
static Error handleArgs(const CommonConfig &Config, const MachOConfig &MachOConfig, Object &Obj)
static Error removeSections(const CommonConfig &Config, Object &Obj)
std::function< bool(const LoadCommand &LC)> LoadCommandPred
static Error dumpSectionToFile(StringRef SecName, StringRef Filename, StringRef InputFilename, Object &Obj)
static Error addSection(const NewSectionInfo &NewSection, Object &Obj)
static StringRef getPayloadString(const LoadCommand &LC)
static void updateAndRemoveSymbols(const CommonConfig &Config, const MachOConfig &MachOConfig, Object &Obj)
static Error updateSection(const NewSectionInfo &NewSection, Object &O)
static cl::opt< std::string > InputFilename(cl::Positional, cl::desc("<input file>"), cl::init("-"))
static constexpr StringLiteral Filename
Implements a dense probed hash-table based set.
Definition DenseSet.h:283
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
static LLVM_ABI 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...
reference emplace_back(ArgTypes &&... Args)
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
std::pair< StringRef, StringRef > split(char Separator) const
Split into two substrings around the first occurrence of a separator character.
Definition StringRef.h:728
std::string str() const
Get the contents as an std::string.
Definition StringRef.h:222
constexpr bool empty() const
Check if the string is empty.
Definition StringRef.h:141
constexpr size_t size() const
Get the string size.
Definition StringRef.h:144
StringRef rtrim(char Char) const
Return string with consecutive Char characters starting from the right removed.
Definition StringRef.h:830
std::pair< iterator, bool > insert(const ValueT &V)
Definition DenseSet.h:210
bool contains(const_arg_type_t< ValueT > V) const
Check if the set contains the given element.
Definition DenseSet.h:183
bool erase(const ValueT &V)
Definition DenseSet.h:98
size_type count(const_arg_type_t< ValueT > V) const
Return 1 if the specified key is in the set, 0 otherwise.
Definition DenseSet.h:188
virtual Expected< const MachOConfig & > getMachOConfig() const =0
virtual const CommonConfig & getCommonConfig() const =0
bool matches(StringRef S) const
virtual Expected< std::unique_ptr< Object > > create() const =0
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
A raw_ostream that writes to an SmallVector or SmallString.
@ MH_DYLDLINK
Definition MachO.h:62
@ MH_OBJECT
Definition MachO.h:43
@ MH_PRELOAD
Definition MachO.h:47
@ N_WEAK_DEF
Definition MachO.h:342
@ REFERENCED_DYNAMICALLY
Definition MachO.h:339
LLVM_ABI Error executeObjcopyOnBinary(const CommonConfig &Config, const MachOConfig &MachOConfig, object::MachOObjectFile &In, raw_ostream &Out)
Apply the transformations described by Config and MachOConfig to In and writes the result into Out.
LLVM_ABI Error executeObjcopyOnMachOUniversalBinary(const MultiFormatConfig &Config, const object::MachOUniversalBinary &In, raw_ostream &Out)
Apply the transformations described by Config and MachOConfig to In and writes the result into Out.
Expected< std::vector< NewArchiveMember > > createNewArchiveMembers(const MultiFormatConfig &Config, const object::Archive &Ar)
Applies the transformations described by Config to each member in archive Ar.
Definition Archive.cpp:22
LLVM_ABI Error writeUniversalBinaryToStream(ArrayRef< Slice > Slices, raw_ostream &Out, FatHeaderType FatHeader=FatHeaderType::FatHeader)
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.
LLVM_ABI StringRef filename(StringRef path LLVM_LIFETIME_BOUND, Style style=Style::native)
Get filename.
Definition Path.cpp:594
This is an optimization pass for GlobalISel generic memory operations.
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
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
Definition Error.h:1321
@ invalid_argument
Definition Errc.h:56
constexpr T alignToPowerOf2(U Value, V Align)
Will overflow only if result is not representable in T.
Definition MathExtras.h:493
OutputIt copy(R &&Range, OutputIt Out)
Definition STLExtras.h:1901
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
auto find_if(R &&Range, UnaryPredicate P)
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1788
void consumeError(Error Err)
Consume a Error without doing anything.
Definition Error.h:1106
#define N
SmallVector< NewSectionInfo, 0 > UpdateSection
SmallVector< StringRef, 0 > DumpSection
SmallVector< NewSectionInfo, 0 > AddSection
StringMap< StringRef > SymbolsToRename
std::vector< StringRef > RPathToPrepend
Definition MachOConfig.h:25
DenseMap< StringRef, StringRef > InstallNamesToUpdate
Definition MachOConfig.h:27
std::optional< StringRef > SharedLibId
Definition MachOConfig.h:31
DenseSet< StringRef > EmptySegmentsToRemove
Definition MachOConfig.h:34
DenseSet< StringRef > RPathsToRemove
Definition MachOConfig.h:28
DenseMap< StringRef, StringRef > RPathsToUpdate
Definition MachOConfig.h:26
std::vector< StringRef > RPathToAdd
Definition MachOConfig.h:24
std::shared_ptr< MemoryBuffer > SectionData
std::optional< SymbolEntry * > Symbol
The Symbol referenced by this entry.
MachO::macho_load_command MachOLoadCommand
Definition MachOObject.h:82
std::optional< StringRef > getSegmentName() const
std::vector< std::unique_ptr< Section > > Sections
Definition MachOObject.h:93
std::optional< uint64_t > getSegmentVMAddr() const
std::vector< uint8_t > Payload
Definition MachOObject.h:87