blob: d14354e8d067087491c6d9cdfe5452594d8942a9 [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
Seiya Nuta9bbf2a12019-10-31 13:51:11 +090026 if (Config.StripAll) {
27 // Remove all debug sections.
28 RemovePred = [RemovePred](const Section &Sec) {
29 if (Sec.Segname == "__DWARF")
30 return true;
31
32 return RemovePred(Sec);
33 };
34 }
35
Seiya Nuta7f19dd12019-10-28 15:40:37 +090036 if (!Config.OnlySection.empty()) {
37 RemovePred = [&Config, RemovePred](const Section &Sec) {
38 return !Config.OnlySection.matches(Sec.CanonicalName);
39 };
40 }
41
42 return Obj.removeSections(RemovePred);
43}
Seiya Nuta4bc71012019-05-29 22:21:12 +000044
Seiya Nuta9bbf2a12019-10-31 13:51:11 +090045static void markSymbols(const CopyConfig &Config, Object &Obj) {
46 // Symbols referenced from the indirect symbol table must not be removed.
47 for (IndirectSymbolEntry &ISE : Obj.IndirectSymTable.Symbols)
48 if (ISE.Symbol)
49 (*ISE.Symbol)->Referenced = true;
50}
51
52static void removeSymbols(const CopyConfig &Config, Object &Obj) {
53 auto RemovePred = [Config](const std::unique_ptr<SymbolEntry> &N) {
54 if (N->Referenced)
55 return false;
56 return Config.StripAll;
57 };
58
59 Obj.SymTable.removeSymbols(RemovePred);
60}
61
Seiya Nuta4bc71012019-05-29 22:21:12 +000062static Error handleArgs(const CopyConfig &Config, Object &Obj) {
63 if (Config.AllowBrokenLinks || !Config.BuildIdLinkDir.empty() ||
64 Config.BuildIdLinkInput || Config.BuildIdLinkOutput ||
65 !Config.SplitDWO.empty() || !Config.SymbolsPrefix.empty() ||
66 !Config.AllocSectionsPrefix.empty() || !Config.AddSection.empty() ||
67 !Config.DumpSection.empty() || !Config.KeepSection.empty() ||
Seiya Nuta7f19dd12019-10-28 15:40:37 +090068 Config.NewSymbolVisibility || !Config.SymbolsToGlobalize.empty() ||
69 !Config.SymbolsToKeep.empty() || !Config.SymbolsToLocalize.empty() ||
70 !Config.SymbolsToWeaken.empty() || !Config.SymbolsToKeepGlobal.empty() ||
71 !Config.SectionsToRename.empty() || !Config.SymbolsToRename.empty() ||
Seiya Nuta4bc71012019-05-29 22:21:12 +000072 !Config.UnneededSymbolsToRemove.empty() ||
Fangrui Song671fb342019-10-02 12:41:25 +000073 !Config.SetSectionAlignment.empty() || !Config.SetSectionFlags.empty() ||
74 !Config.ToRemove.empty() || Config.ExtractDWO || Config.KeepFileSymbols ||
Seiya Nuta9bbf2a12019-10-31 13:51:11 +090075 Config.LocalizeHidden || Config.PreserveDates || Config.StripAllGNU ||
76 Config.StripDWO || Config.StripNonAlloc || Config.StripSections ||
77 Config.Weaken || Config.DecompressDebugSections || Config.StripDebug ||
Fangrui Song671fb342019-10-02 12:41:25 +000078 Config.StripNonAlloc || Config.StripSections || Config.StripUnneeded ||
79 Config.DiscardMode != DiscardType::None || !Config.SymbolsToAdd.empty() ||
80 Config.EntryExpr) {
Seiya Nuta4bc71012019-05-29 22:21:12 +000081 return createStringError(llvm::errc::invalid_argument,
82 "option not supported by llvm-objcopy for MachO");
83 }
Alexander Shaposhnikov7d83c292019-11-06 17:04:04 -080084
Seiya Nuta7f19dd12019-10-28 15:40:37 +090085 removeSections(Config, Obj);
Seiya Nuta9bbf2a12019-10-31 13:51:11 +090086
87 // Mark symbols to determine which symbols are still needed.
88 if (Config.StripAll)
89 markSymbols(Config, Obj);
90
91 removeSymbols(Config, Obj);
92
93 if (Config.StripAll)
94 for (LoadCommand &LC : Obj.LoadCommands)
95 for (Section &Sec : LC.Sections)
96 Sec.Relocations.clear();
97
Seiya Nuta4bc71012019-05-29 22:21:12 +000098 return Error::success();
99}
100
Alexander Shaposhnikovd911ed12019-02-02 00:38:07 +0000101Error executeObjcopyOnBinary(const CopyConfig &Config,
102 object::MachOObjectFile &In, Buffer &Out) {
103 MachOReader Reader(In);
104 std::unique_ptr<Object> O = Reader.create();
Seiya Nuta4bc71012019-05-29 22:21:12 +0000105 if (!O)
106 return createFileError(
107 Config.InputFilename,
108 createStringError(object_error::parse_failed,
109 "unable to deserialize MachO object"));
110
111 if (Error E = handleArgs(Config, *O))
112 return createFileError(Config.InputFilename, std::move(E));
113
Seiya Nuta552bcb82019-08-19 21:05:31 +0000114 // TODO: Support 16KB pages which are employed in iOS arm64 binaries:
115 // https://github.com/llvm/llvm-project/commit/1bebb2832ee312d3b0316dacff457a7a29435edb
116 const uint64_t PageSize = 4096;
117
118 MachOWriter Writer(*O, In.is64Bit(), In.isLittleEndian(), PageSize, Out);
Seiya Nutab728e532019-06-08 01:22:54 +0000119 if (auto E = Writer.finalize())
120 return E;
Alexander Shaposhnikovd911ed12019-02-02 00:38:07 +0000121 return Writer.write();
122}
123
124} // end namespace macho
125} // end namespace objcopy
126} // end namespace llvm