blob: 80fb18599ba3e3ea1db0b50cb48850b24d4c7cf5 [file] [log] [blame]
Alexander Shaposhnikovd911ed12019-02-02 00:38:07 +00001//===- MachOObjcopy.cpp -----------------------------------------*- C++ -*-===//
2//
Chandler Carruth127252b2019-02-11 08:25:19 +00003// 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
Alexander Shaposhnikovd911ed12019-02-02 00:38:07 +00006//
7//===----------------------------------------------------------------------===//
8
9#include "MachOObjcopy.h"
10#include "../CopyConfig.h"
Alexander Shaposhnikovd911ed12019-02-02 00:38:07 +000011#include "MachOReader.h"
12#include "MachOWriter.h"
Seiya Nuta4bc71012019-05-29 22:21:12 +000013#include "llvm/Support/Errc.h"
Alexander Shaposhnikovd911ed12019-02-02 00:38:07 +000014#include "llvm/Support/Error.h"
15
16namespace llvm {
17namespace objcopy {
18namespace macho {
19
Seiya Nuta4bc71012019-05-29 22:21:12 +000020using namespace object;
21
22static 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");
42 }
43
44 return Error::success();
45}
46
Alexander Shaposhnikovd911ed12019-02-02 00:38:07 +000047Error executeObjcopyOnBinary(const CopyConfig &Config,
48 object::MachOObjectFile &In, Buffer &Out) {
49 MachOReader Reader(In);
50 std::unique_ptr<Object> O = Reader.create();
Seiya Nuta4bc71012019-05-29 22:21:12 +000051 if (!O)
52 return createFileError(
53 Config.InputFilename,
54 createStringError(object_error::parse_failed,
55 "unable to deserialize MachO object"));
56
57 if (Error E = handleArgs(Config, *O))
58 return createFileError(Config.InputFilename, std::move(E));
59
Alexander Shaposhnikovd911ed12019-02-02 00:38:07 +000060 MachOWriter Writer(*O, In.is64Bit(), In.isLittleEndian(), Out);
61 return Writer.write();
62}
63
64} // end namespace macho
65} // end namespace objcopy
66} // end namespace llvm