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 //===----------------------------------------------------------------------===//
8
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"
15
16 namespace llvm {
17 namespace objcopy {
18 namespace macho {
19
20 using namespace object;
21 using SectionPred = std::function<bool(const Section &Sec)>;
22
removeSections(const CopyConfig & Config,Object & Obj)23 static void removeSections(const CopyConfig &Config, Object &Obj) {
24 SectionPred RemovePred = [](const Section &) { return false; };
25
26 if (!Config.ToRemove.empty()) {
27 RemovePred = [&Config, RemovePred](const Section &Sec) {
28 return Config.ToRemove.matches(Sec.CanonicalName);
29 };
30 }
31
32 if (Config.StripAll || Config.StripDebug) {
33 // Remove all debug sections.
34 RemovePred = [RemovePred](const Section &Sec) {
35 if (Sec.Segname == "__DWARF")
36 return true;
37
38 return RemovePred(Sec);
39 };
40 }
41
42 if (!Config.OnlySection.empty()) {
43 // Overwrite RemovePred because --only-section takes priority.
44 RemovePred = [&Config](const Section &Sec) {
45 return !Config.OnlySection.matches(Sec.CanonicalName);
46 };
47 }
48
49 return Obj.removeSections(RemovePred);
50 }
51
markSymbols(const CopyConfig & Config,Object & Obj)52 static void markSymbols(const CopyConfig &Config, Object &Obj) {
53 // Symbols referenced from the indirect symbol table must not be removed.
54 for (IndirectSymbolEntry &ISE : Obj.IndirectSymTable.Symbols)
55 if (ISE.Symbol)
56 (*ISE.Symbol)->Referenced = true;
57 }
58
updateAndRemoveSymbols(const CopyConfig & Config,Object & Obj)59 static void updateAndRemoveSymbols(const CopyConfig &Config, Object &Obj) {
60 for (SymbolEntry &Sym : Obj.SymTable) {
61 auto I = Config.SymbolsToRename.find(Sym.Name);
62 if (I != Config.SymbolsToRename.end())
63 Sym.Name = I->getValue();
64 }
65
66 auto RemovePred = [Config](const std::unique_ptr<SymbolEntry> &N) {
67 if (N->Referenced)
68 return false;
69 return Config.StripAll;
70 };
71
72 Obj.SymTable.removeSymbols(RemovePred);
73 }
74
buildRPathLoadCommand(StringRef Path)75 static LoadCommand buildRPathLoadCommand(StringRef Path) {
76 LoadCommand LC;
77 MachO::rpath_command RPathLC;
78 RPathLC.cmd = MachO::LC_RPATH;
79 RPathLC.path = sizeof(MachO::rpath_command);
80 RPathLC.cmdsize = alignTo(sizeof(MachO::rpath_command) + Path.size(), 8);
81 LC.MachOLoadCommand.rpath_command_data = RPathLC;
82 LC.Payload.assign(RPathLC.cmdsize - sizeof(MachO::rpath_command), 0);
83 std::copy(Path.begin(), Path.end(), LC.Payload.begin());
84 return LC;
85 }
86
dumpSectionToFile(StringRef SecName,StringRef Filename,Object & Obj)87 static Error dumpSectionToFile(StringRef SecName, StringRef Filename,
88 Object &Obj) {
89 for (LoadCommand &LC : Obj.LoadCommands)
90 for (Section &Sec : LC.Sections) {
91 if (Sec.CanonicalName == SecName) {
92 Expected<std::unique_ptr<FileOutputBuffer>> BufferOrErr =
93 FileOutputBuffer::create(Filename, Sec.Content.size());
94 if (!BufferOrErr)
95 return BufferOrErr.takeError();
96 std::unique_ptr<FileOutputBuffer> Buf = std::move(*BufferOrErr);
97 llvm::copy(Sec.Content, Buf->getBufferStart());
98
99 if (Error E = Buf->commit())
100 return E;
101 return Error::success();
102 }
103 }
104
105 return createStringError(object_error::parse_failed, "section '%s' not found",
106 SecName.str().c_str());
107 }
108
addSection(StringRef SecName,StringRef Filename,Object & Obj)109 static Error addSection(StringRef SecName, StringRef Filename, Object &Obj) {
110 ErrorOr<std::unique_ptr<MemoryBuffer>> BufOrErr =
111 MemoryBuffer::getFile(Filename);
112 if (!BufOrErr)
113 return createFileError(Filename, errorCodeToError(BufOrErr.getError()));
114 std::unique_ptr<MemoryBuffer> Buf = std::move(*BufOrErr);
115
116 std::pair<StringRef, StringRef> Pair = SecName.split(',');
117 StringRef TargetSegName = Pair.first;
118 Section Sec(TargetSegName, Pair.second);
119 Sec.Content = Obj.NewSectionsContents.save(Buf->getBuffer());
120
121 // Add the a section into an existing segment.
122 for (LoadCommand &LC : Obj.LoadCommands) {
123 Optional<StringRef> SegName = LC.getSegmentName();
124 if (SegName && SegName == TargetSegName) {
125 LC.Sections.push_back(Sec);
126 return Error::success();
127 }
128 }
129
130 // There's no segment named TargetSegName. Create a new load command and
131 // Insert a new section into it.
132 LoadCommand &NewSegment = Obj.addSegment(TargetSegName);
133 NewSegment.Sections.push_back(Sec);
134 return Error::success();
135 }
136
137 // isValidMachOCannonicalName returns success if Name is a MachO cannonical name
138 // ("<segment>,<section>") and lengths of both segment and section names are
139 // valid.
isValidMachOCannonicalName(StringRef Name)140 Error isValidMachOCannonicalName(StringRef Name) {
141 if (Name.count(',') != 1)
142 return createStringError(errc::invalid_argument,
143 "invalid section name '%s' (should be formatted "
144 "as '<segment name>,<section name>')",
145 Name.str().c_str());
146
147 std::pair<StringRef, StringRef> Pair = Name.split(',');
148 if (Pair.first.size() > 16)
149 return createStringError(errc::invalid_argument,
150 "too long segment name: '%s'",
151 Pair.first.str().c_str());
152 if (Pair.second.size() > 16)
153 return createStringError(errc::invalid_argument,
154 "too long section name: '%s'",
155 Pair.second.str().c_str());
156 return Error::success();
157 }
158
handleArgs(const CopyConfig & Config,Object & Obj)159 static Error handleArgs(const CopyConfig &Config, Object &Obj) {
160 if (Config.AllowBrokenLinks || !Config.BuildIdLinkDir.empty() ||
161 Config.BuildIdLinkInput || Config.BuildIdLinkOutput ||
162 !Config.SplitDWO.empty() || !Config.SymbolsPrefix.empty() ||
163 !Config.AllocSectionsPrefix.empty() || !Config.KeepSection.empty() ||
164 Config.NewSymbolVisibility || !Config.SymbolsToGlobalize.empty() ||
165 !Config.SymbolsToKeep.empty() || !Config.SymbolsToLocalize.empty() ||
166 !Config.SymbolsToWeaken.empty() || !Config.SymbolsToKeepGlobal.empty() ||
167 !Config.SectionsToRename.empty() ||
168 !Config.UnneededSymbolsToRemove.empty() ||
169 !Config.SetSectionAlignment.empty() || !Config.SetSectionFlags.empty() ||
170 Config.ExtractDWO || Config.KeepFileSymbols || Config.LocalizeHidden ||
171 Config.PreserveDates || Config.StripAllGNU || Config.StripDWO ||
172 Config.StripNonAlloc || Config.StripSections || Config.Weaken ||
173 Config.DecompressDebugSections || Config.StripNonAlloc ||
174 Config.StripSections || Config.StripUnneeded ||
175 Config.DiscardMode != DiscardType::None || !Config.SymbolsToAdd.empty() ||
176 Config.EntryExpr) {
177 return createStringError(llvm::errc::invalid_argument,
178 "option not supported by llvm-objcopy for MachO");
179 }
180 removeSections(Config, Obj);
181
182 // Mark symbols to determine which symbols are still needed.
183 if (Config.StripAll)
184 markSymbols(Config, Obj);
185
186 updateAndRemoveSymbols(Config, Obj);
187
188 if (Config.StripAll)
189 for (LoadCommand &LC : Obj.LoadCommands)
190 for (Section &Sec : LC.Sections)
191 Sec.Relocations.clear();
192
193 for (const StringRef &Flag : Config.DumpSection) {
194 std::pair<StringRef, StringRef> SecPair = Flag.split("=");
195 StringRef SecName = SecPair.first;
196 StringRef File = SecPair.second;
197 if (Error E = dumpSectionToFile(SecName, File, Obj))
198 return E;
199 }
200
201 for (const auto &Flag : Config.AddSection) {
202 std::pair<StringRef, StringRef> SecPair = Flag.split("=");
203 StringRef SecName = SecPair.first;
204 StringRef File = SecPair.second;
205 if (Error E = isValidMachOCannonicalName(SecName))
206 return E;
207 if (Error E = addSection(SecName, File, Obj))
208 return E;
209 }
210
211 for (StringRef RPath : Config.RPathToAdd) {
212 for (LoadCommand &LC : Obj.LoadCommands) {
213 if (LC.MachOLoadCommand.load_command_data.cmd == MachO::LC_RPATH &&
214 RPath == StringRef(reinterpret_cast<char *>(LC.Payload.data()),
215 LC.Payload.size())
216 .trim(0)) {
217 return createStringError(errc::invalid_argument,
218 "rpath " + RPath +
219 " would create a duplicate load command");
220 }
221 }
222 Obj.addLoadCommand(buildRPathLoadCommand(RPath));
223 }
224 return Error::success();
225 }
226
executeObjcopyOnBinary(const CopyConfig & Config,object::MachOObjectFile & In,Buffer & Out)227 Error executeObjcopyOnBinary(const CopyConfig &Config,
228 object::MachOObjectFile &In, Buffer &Out) {
229 MachOReader Reader(In);
230 std::unique_ptr<Object> O = Reader.create();
231 if (!O)
232 return createFileError(
233 Config.InputFilename,
234 createStringError(object_error::parse_failed,
235 "unable to deserialize MachO object"));
236
237 if (Error E = handleArgs(Config, *O))
238 return createFileError(Config.InputFilename, std::move(E));
239
240 // TODO: Support 16KB pages which are employed in iOS arm64 binaries:
241 // https://github.com/llvm/llvm-project/commit/1bebb2832ee312d3b0316dacff457a7a29435edb
242 const uint64_t PageSize = 4096;
243
244 MachOWriter Writer(*O, In.is64Bit(), In.isLittleEndian(), PageSize, Out);
245 if (auto E = Writer.finalize())
246 return E;
247 return Writer.write();
248 }
249
250 } // end namespace macho
251 } // end namespace objcopy
252 } // end namespace llvm
253