1 //===- MachOObjcopy.cpp -----------------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #include "MachOObjcopy.h"
10 #include "../CopyConfig.h"
11 #include "MachOReader.h"
12 #include "MachOWriter.h"
13 #include "llvm/Support/Errc.h"
14 #include "llvm/Support/Error.h"
20 using namespace object
;
22 static Error
handleArgs(const CopyConfig
&Config
, Object
&Obj
) {
23 if (Config
.AllowBrokenLinks
|| !Config
.BuildIdLinkDir
.empty() ||
24 Config
.BuildIdLinkInput
|| Config
.BuildIdLinkOutput
||
25 !Config
.SplitDWO
.empty() || !Config
.SymbolsPrefix
.empty() ||
26 !Config
.AllocSectionsPrefix
.empty() || !Config
.AddSection
.empty() ||
27 !Config
.DumpSection
.empty() || !Config
.KeepSection
.empty() ||
28 !Config
.OnlySection
.empty() || !Config
.SymbolsToGlobalize
.empty() ||
29 !Config
.SymbolsToKeep
.empty() || !Config
.SymbolsToLocalize
.empty() ||
30 !Config
.SymbolsToWeaken
.empty() || !Config
.SymbolsToKeepGlobal
.empty() ||
31 !Config
.SectionsToRename
.empty() || !Config
.SymbolsToRename
.empty() ||
32 !Config
.UnneededSymbolsToRemove
.empty() ||
33 !Config
.SetSectionFlags
.empty() || !Config
.ToRemove
.empty() ||
34 Config
.ExtractDWO
|| Config
.KeepFileSymbols
|| Config
.LocalizeHidden
||
35 Config
.PreserveDates
|| Config
.StripDWO
|| Config
.StripNonAlloc
||
36 Config
.StripSections
|| Config
.Weaken
|| Config
.DecompressDebugSections
||
37 Config
.StripDebug
|| Config
.StripNonAlloc
|| Config
.StripSections
||
38 Config
.StripUnneeded
|| Config
.DiscardMode
!= DiscardType::None
||
39 !Config
.SymbolsToAdd
.empty() || Config
.EntryExpr
) {
40 return createStringError(llvm::errc::invalid_argument
,
41 "option not supported by llvm-objcopy for MachO");
44 return Error::success();
47 Error
executeObjcopyOnBinary(const CopyConfig
&Config
,
48 object::MachOObjectFile
&In
, Buffer
&Out
) {
49 MachOReader
Reader(In
);
50 std::unique_ptr
<Object
> O
= Reader
.create();
52 return createFileError(
54 createStringError(object_error::parse_failed
,
55 "unable to deserialize MachO object"));
57 if (Error E
= handleArgs(Config
, *O
))
58 return createFileError(Config
.InputFilename
, std::move(E
));
60 MachOWriter
Writer(*O
, In
.is64Bit(), In
.isLittleEndian(), Out
);
61 if (auto E
= Writer
.finalize())
63 return Writer
.write();
66 } // end namespace macho
67 } // end namespace objcopy
68 } // end namespace llvm