blob: 652c0ac095776decbd11eca7cb9b228947cb74eb [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
Alexander Shaposhnikovb5913e62019-10-17 15:12:55 -070062static LoadCommand buildRPathLoadCommand(StringRef Path) {
63 LoadCommand LC;
64 MachO::rpath_command RPathLC;
65 RPathLC.cmd = MachO::LC_RPATH;
66 RPathLC.path = sizeof(MachO::rpath_command);
67 RPathLC.cmdsize = alignTo(sizeof(MachO::rpath_command) + Path.size(), 8);
68 LC.MachOLoadCommand.rpath_command_data = RPathLC;
69 LC.Payload.assign(RPathLC.cmdsize - sizeof(MachO::rpath_command), 0);
70 std::copy(Path.begin(), Path.end(), LC.Payload.begin());
71 return LC;
72}
73
Seiya Nuta4bc71012019-05-29 22:21:12 +000074static Error handleArgs(const CopyConfig &Config, Object &Obj) {
75 if (Config.AllowBrokenLinks || !Config.BuildIdLinkDir.empty() ||
76 Config.BuildIdLinkInput || Config.BuildIdLinkOutput ||
77 !Config.SplitDWO.empty() || !Config.SymbolsPrefix.empty() ||
78 !Config.AllocSectionsPrefix.empty() || !Config.AddSection.empty() ||
79 !Config.DumpSection.empty() || !Config.KeepSection.empty() ||
Seiya Nuta7f19dd12019-10-28 15:40:37 +090080 Config.NewSymbolVisibility || !Config.SymbolsToGlobalize.empty() ||
81 !Config.SymbolsToKeep.empty() || !Config.SymbolsToLocalize.empty() ||
82 !Config.SymbolsToWeaken.empty() || !Config.SymbolsToKeepGlobal.empty() ||
83 !Config.SectionsToRename.empty() || !Config.SymbolsToRename.empty() ||
Seiya Nuta4bc71012019-05-29 22:21:12 +000084 !Config.UnneededSymbolsToRemove.empty() ||
Fangrui Song671fb342019-10-02 12:41:25 +000085 !Config.SetSectionAlignment.empty() || !Config.SetSectionFlags.empty() ||
86 !Config.ToRemove.empty() || Config.ExtractDWO || Config.KeepFileSymbols ||
Seiya Nuta9bbf2a12019-10-31 13:51:11 +090087 Config.LocalizeHidden || Config.PreserveDates || Config.StripAllGNU ||
88 Config.StripDWO || Config.StripNonAlloc || Config.StripSections ||
89 Config.Weaken || Config.DecompressDebugSections || Config.StripDebug ||
Fangrui Song671fb342019-10-02 12:41:25 +000090 Config.StripNonAlloc || Config.StripSections || Config.StripUnneeded ||
91 Config.DiscardMode != DiscardType::None || !Config.SymbolsToAdd.empty() ||
92 Config.EntryExpr) {
Seiya Nuta4bc71012019-05-29 22:21:12 +000093 return createStringError(llvm::errc::invalid_argument,
94 "option not supported by llvm-objcopy for MachO");
95 }
Seiya Nuta7f19dd12019-10-28 15:40:37 +090096 removeSections(Config, Obj);
Seiya Nuta9bbf2a12019-10-31 13:51:11 +090097
98 // Mark symbols to determine which symbols are still needed.
99 if (Config.StripAll)
100 markSymbols(Config, Obj);
101
102 removeSymbols(Config, Obj);
103
104 if (Config.StripAll)
105 for (LoadCommand &LC : Obj.LoadCommands)
106 for (Section &Sec : LC.Sections)
107 Sec.Relocations.clear();
108
Alexander Shaposhnikovb5913e62019-10-17 15:12:55 -0700109 for (StringRef RPath : Config.RPathToAdd) {
110 for (LoadCommand &LC : Obj.LoadCommands) {
111 if (LC.MachOLoadCommand.load_command_data.cmd == MachO::LC_RPATH &&
112 RPath == StringRef(reinterpret_cast<char *>(LC.Payload.data()),
113 LC.Payload.size())
114 .trim(0)) {
115 return createStringError(errc::invalid_argument,
116 "rpath " + RPath +
117 " would create a duplicate load command");
118 }
119 }
120 Obj.addLoadCommand(buildRPathLoadCommand(RPath));
121 }
Seiya Nuta4bc71012019-05-29 22:21:12 +0000122 return Error::success();
123}
124
Alexander Shaposhnikovd911ed12019-02-02 00:38:07 +0000125Error executeObjcopyOnBinary(const CopyConfig &Config,
126 object::MachOObjectFile &In, Buffer &Out) {
127 MachOReader Reader(In);
128 std::unique_ptr<Object> O = Reader.create();
Seiya Nuta4bc71012019-05-29 22:21:12 +0000129 if (!O)
130 return createFileError(
131 Config.InputFilename,
132 createStringError(object_error::parse_failed,
133 "unable to deserialize MachO object"));
134
135 if (Error E = handleArgs(Config, *O))
136 return createFileError(Config.InputFilename, std::move(E));
137
Seiya Nuta552bcb82019-08-19 21:05:31 +0000138 // TODO: Support 16KB pages which are employed in iOS arm64 binaries:
139 // https://github.com/llvm/llvm-project/commit/1bebb2832ee312d3b0316dacff457a7a29435edb
140 const uint64_t PageSize = 4096;
141
142 MachOWriter Writer(*O, In.is64Bit(), In.isLittleEndian(), PageSize, Out);
Seiya Nutab728e532019-06-08 01:22:54 +0000143 if (auto E = Writer.finalize())
144 return E;
Alexander Shaposhnikovd911ed12019-02-02 00:38:07 +0000145 return Writer.write();
146}
147
148} // end namespace macho
149} // end namespace objcopy
150} // end namespace llvm