[yaml2obj/obj2yaml] - Add support for .stack_sizes sections.
[llvm-complete.git] / tools / llvm-objcopy / MachO / MachOObjcopy.cpp
bloba52931e469fd3a86f448d00d10999d461f4af138
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 //===----------------------------------------------------------------------===//
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"
16 namespace llvm {
17 namespace objcopy {
18 namespace macho {
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.NewSymbolVisibility || !Config.OnlySection.empty() ||
29 !Config.SymbolsToGlobalize.empty() || !Config.SymbolsToKeep.empty() ||
30 !Config.SymbolsToLocalize.empty() || !Config.SymbolsToWeaken.empty() ||
31 !Config.SymbolsToKeepGlobal.empty() || !Config.SectionsToRename.empty() ||
32 !Config.SymbolsToRename.empty() ||
33 !Config.UnneededSymbolsToRemove.empty() ||
34 !Config.SetSectionFlags.empty() || !Config.ToRemove.empty() ||
35 Config.ExtractDWO || Config.KeepFileSymbols || Config.LocalizeHidden ||
36 Config.PreserveDates || Config.StripDWO || Config.StripNonAlloc ||
37 Config.StripSections || Config.Weaken || Config.DecompressDebugSections ||
38 Config.StripDebug || Config.StripNonAlloc || Config.StripSections ||
39 Config.StripUnneeded || Config.DiscardMode != DiscardType::None ||
40 !Config.SymbolsToAdd.empty() || Config.EntryExpr) {
41 return createStringError(llvm::errc::invalid_argument,
42 "option not supported by llvm-objcopy for MachO");
45 return Error::success();
48 Error executeObjcopyOnBinary(const CopyConfig &Config,
49 object::MachOObjectFile &In, Buffer &Out) {
50 MachOReader Reader(In);
51 std::unique_ptr<Object> O = Reader.create();
52 if (!O)
53 return createFileError(
54 Config.InputFilename,
55 createStringError(object_error::parse_failed,
56 "unable to deserialize MachO object"));
58 if (Error E = handleArgs(Config, *O))
59 return createFileError(Config.InputFilename, std::move(E));
61 // TODO: Support 16KB pages which are employed in iOS arm64 binaries:
62 // https://github.com/llvm/llvm-project/commit/1bebb2832ee312d3b0316dacff457a7a29435edb
63 const uint64_t PageSize = 4096;
65 MachOWriter Writer(*O, In.is64Bit(), In.isLittleEndian(), PageSize, Out);
66 if (auto E = Writer.finalize())
67 return E;
68 return Writer.write();
71 } // end namespace macho
72 } // end namespace objcopy
73 } // end namespace llvm