blob: 73983a175a9ad74d6e016f4fcde4ca96905cc6de [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;
Seiya Nuta7f19dd12019-10-28 15:40:37 +090021using SectionPred = std::function<bool(const Section &Sec)>;
22
23static void removeSections(const CopyConfig &Config, Object &Obj) {
24 SectionPred RemovePred = [](const Section &) { return false; };
25
26 if (!Config.OnlySection.empty()) {
27 RemovePred = [&Config, RemovePred](const Section &Sec) {
28 return !Config.OnlySection.matches(Sec.CanonicalName);
29 };
30 }
31
32 return Obj.removeSections(RemovePred);
33}
Seiya Nuta4bc71012019-05-29 22:21:12 +000034
35static Error handleArgs(const CopyConfig &Config, Object &Obj) {
36 if (Config.AllowBrokenLinks || !Config.BuildIdLinkDir.empty() ||
37 Config.BuildIdLinkInput || Config.BuildIdLinkOutput ||
38 !Config.SplitDWO.empty() || !Config.SymbolsPrefix.empty() ||
39 !Config.AllocSectionsPrefix.empty() || !Config.AddSection.empty() ||
40 !Config.DumpSection.empty() || !Config.KeepSection.empty() ||
Seiya Nuta7f19dd12019-10-28 15:40:37 +090041 Config.NewSymbolVisibility || !Config.SymbolsToGlobalize.empty() ||
42 !Config.SymbolsToKeep.empty() || !Config.SymbolsToLocalize.empty() ||
43 !Config.SymbolsToWeaken.empty() || !Config.SymbolsToKeepGlobal.empty() ||
44 !Config.SectionsToRename.empty() || !Config.SymbolsToRename.empty() ||
Seiya Nuta4bc71012019-05-29 22:21:12 +000045 !Config.UnneededSymbolsToRemove.empty() ||
Fangrui Song671fb342019-10-02 12:41:25 +000046 !Config.SetSectionAlignment.empty() || !Config.SetSectionFlags.empty() ||
47 !Config.ToRemove.empty() || Config.ExtractDWO || Config.KeepFileSymbols ||
48 Config.LocalizeHidden || Config.PreserveDates || Config.StripDWO ||
49 Config.StripNonAlloc || Config.StripSections || Config.Weaken ||
50 Config.DecompressDebugSections || Config.StripDebug ||
51 Config.StripNonAlloc || Config.StripSections || Config.StripUnneeded ||
52 Config.DiscardMode != DiscardType::None || !Config.SymbolsToAdd.empty() ||
53 Config.EntryExpr) {
Seiya Nuta4bc71012019-05-29 22:21:12 +000054 return createStringError(llvm::errc::invalid_argument,
55 "option not supported by llvm-objcopy for MachO");
56 }
57
Seiya Nuta7f19dd12019-10-28 15:40:37 +090058 removeSections(Config, Obj);
Seiya Nuta4bc71012019-05-29 22:21:12 +000059 return Error::success();
60}
61
Alexander Shaposhnikovd911ed12019-02-02 00:38:07 +000062Error executeObjcopyOnBinary(const CopyConfig &Config,
63 object::MachOObjectFile &In, Buffer &Out) {
64 MachOReader Reader(In);
65 std::unique_ptr<Object> O = Reader.create();
Seiya Nuta4bc71012019-05-29 22:21:12 +000066 if (!O)
67 return createFileError(
68 Config.InputFilename,
69 createStringError(object_error::parse_failed,
70 "unable to deserialize MachO object"));
71
72 if (Error E = handleArgs(Config, *O))
73 return createFileError(Config.InputFilename, std::move(E));
74
Seiya Nuta552bcb82019-08-19 21:05:31 +000075 // TODO: Support 16KB pages which are employed in iOS arm64 binaries:
76 // https://github.com/llvm/llvm-project/commit/1bebb2832ee312d3b0316dacff457a7a29435edb
77 const uint64_t PageSize = 4096;
78
79 MachOWriter Writer(*O, In.is64Bit(), In.isLittleEndian(), PageSize, Out);
Seiya Nutab728e532019-06-08 01:22:54 +000080 if (auto E = Writer.finalize())
81 return E;
Alexander Shaposhnikovd911ed12019-02-02 00:38:07 +000082 return Writer.write();
83}
84
85} // end namespace macho
86} // end namespace objcopy
87} // end namespace llvm