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 "llvm/ObjCopy/MachO/MachOObjcopy.h"
10 #include "Archive.h"
11 #include "MachOReader.h"
12 #include "MachOWriter.h"
13 #include "llvm/ADT/DenseSet.h"
14 #include "llvm/ObjCopy/CommonConfig.h"
15 #include "llvm/ObjCopy/MachO/MachOConfig.h"
16 #include "llvm/ObjCopy/MultiFormatConfig.h"
17 #include "llvm/ObjCopy/ObjCopy.h"
18 #include "llvm/Object/ArchiveWriter.h"
19 #include "llvm/Object/MachOUniversal.h"
20 #include "llvm/Object/MachOUniversalWriter.h"
21 #include "llvm/Support/Errc.h"
22 #include "llvm/Support/Error.h"
23 #include "llvm/Support/FileOutputBuffer.h"
24 #include "llvm/Support/Path.h"
25 #include "llvm/Support/SmallVectorMemoryBuffer.h"
26 
27 using namespace llvm;
28 using namespace llvm::objcopy;
29 using namespace llvm::objcopy::macho;
30 using namespace llvm::object;
31 
32 using SectionPred = std::function<bool(const std::unique_ptr<Section> &Sec)>;
33 using LoadCommandPred = std::function<bool(const LoadCommand &LC)>;
34 
35 #ifndef NDEBUG
36 static bool isLoadCommandWithPayloadString(const LoadCommand &LC) {
37   // TODO: Add support for LC_REEXPORT_DYLIB, LC_LOAD_UPWARD_DYLIB and
38   // LC_LAZY_LOAD_DYLIB
39   return LC.MachOLoadCommand.load_command_data.cmd == MachO::LC_RPATH ||
40          LC.MachOLoadCommand.load_command_data.cmd == MachO::LC_ID_DYLIB ||
41          LC.MachOLoadCommand.load_command_data.cmd == MachO::LC_LOAD_DYLIB ||
42          LC.MachOLoadCommand.load_command_data.cmd == MachO::LC_LOAD_WEAK_DYLIB;
43 }
44 #endif
45 
46 static StringRef getPayloadString(const LoadCommand &LC) {
47   assert(isLoadCommandWithPayloadString(LC) &&
48          "unsupported load command encountered");
49 
50   return StringRef(reinterpret_cast<const char *>(LC.Payload.data()),
51                    LC.Payload.size())
52       .rtrim('\0');
53 }
54 
55 static Error removeSections(const CommonConfig &Config, Object &Obj) {
56   SectionPred RemovePred = [](const std::unique_ptr<Section> &) {
57     return false;
58   };
59 
60   if (!Config.ToRemove.empty()) {
61     RemovePred = [&Config, RemovePred](const std::unique_ptr<Section> &Sec) {
62       return Config.ToRemove.matches(Sec->CanonicalName);
63     };
64   }
65 
66   if (Config.StripAll || Config.StripDebug) {
67     // Remove all debug sections.
68     RemovePred = [RemovePred](const std::unique_ptr<Section> &Sec) {
69       if (Sec->Segname == "__DWARF")
70         return true;
71 
72       return RemovePred(Sec);
73     };
74   }
75 
76   if (!Config.OnlySection.empty()) {
77     // Overwrite RemovePred because --only-section takes priority.
78     RemovePred = [&Config](const std::unique_ptr<Section> &Sec) {
79       return !Config.OnlySection.matches(Sec->CanonicalName);
80     };
81   }
82 
83   return Obj.removeSections(RemovePred);
84 }
85 
86 static void markSymbols(const CommonConfig &, Object &Obj) {
87   // Symbols referenced from the indirect symbol table must not be removed.
88   for (IndirectSymbolEntry &ISE : Obj.IndirectSymTable.Symbols)
89     if (ISE.Symbol)
90       (*ISE.Symbol)->Referenced = true;
91 }
92 
93 static void updateAndRemoveSymbols(const CommonConfig &Config,
94                                    const MachOConfig &MachOConfig,
95                                    Object &Obj) {
96   for (SymbolEntry &Sym : Obj.SymTable) {
97     auto I = Config.SymbolsToRename.find(Sym.Name);
98     if (I != Config.SymbolsToRename.end())
99       Sym.Name = std::string(I->getValue());
100   }
101 
102   auto RemovePred = [&Config, &MachOConfig,
103                      &Obj](const std::unique_ptr<SymbolEntry> &N) {
104     if (N->Referenced)
105       return false;
106     if (MachOConfig.KeepUndefined && N->isUndefinedSymbol())
107       return false;
108     if (N->n_desc & MachO::REFERENCED_DYNAMICALLY)
109       return false;
110     if (Config.StripAll)
111       return true;
112     if (Config.DiscardMode == DiscardType::All && !(N->n_type & MachO::N_EXT))
113       return true;
114     // This behavior is consistent with cctools' strip.
115     if (MachOConfig.StripSwiftSymbols &&
116         (Obj.Header.Flags & MachO::MH_DYLDLINK) && Obj.SwiftVersion &&
117         *Obj.SwiftVersion && N->isSwiftSymbol())
118       return true;
119     return false;
120   };
121 
122   Obj.SymTable.removeSymbols(RemovePred);
123 }
124 
125 template <typename LCType>
126 static void updateLoadCommandPayloadString(LoadCommand &LC, StringRef S) {
127   assert(isLoadCommandWithPayloadString(LC) &&
128          "unsupported load command encountered");
129 
130   uint32_t NewCmdsize = alignTo(sizeof(LCType) + S.size() + 1, 8);
131 
132   LC.MachOLoadCommand.load_command_data.cmdsize = NewCmdsize;
133   LC.Payload.assign(NewCmdsize - sizeof(LCType), 0);
134   std::copy(S.begin(), S.end(), LC.Payload.begin());
135 }
136 
137 static LoadCommand buildRPathLoadCommand(StringRef Path) {
138   LoadCommand LC;
139   MachO::rpath_command RPathLC;
140   RPathLC.cmd = MachO::LC_RPATH;
141   RPathLC.path = sizeof(MachO::rpath_command);
142   RPathLC.cmdsize = alignTo(sizeof(MachO::rpath_command) + Path.size() + 1, 8);
143   LC.MachOLoadCommand.rpath_command_data = RPathLC;
144   LC.Payload.assign(RPathLC.cmdsize - sizeof(MachO::rpath_command), 0);
145   std::copy(Path.begin(), Path.end(), LC.Payload.begin());
146   return LC;
147 }
148 
149 static Error processLoadCommands(const MachOConfig &MachOConfig, Object &Obj) {
150   // Remove RPaths.
151   DenseSet<StringRef> RPathsToRemove(MachOConfig.RPathsToRemove.begin(),
152                                      MachOConfig.RPathsToRemove.end());
153 
154   LoadCommandPred RemovePred = [&RPathsToRemove,
155                                 &MachOConfig](const LoadCommand &LC) {
156     if (LC.MachOLoadCommand.load_command_data.cmd == MachO::LC_RPATH) {
157       // When removing all RPaths we don't need to care
158       // about what it contains
159       if (MachOConfig.RemoveAllRpaths)
160         return true;
161 
162       StringRef RPath = getPayloadString(LC);
163       if (RPathsToRemove.count(RPath)) {
164         RPathsToRemove.erase(RPath);
165         return true;
166       }
167     }
168     return false;
169   };
170 
171   if (Error E = Obj.removeLoadCommands(RemovePred))
172     return E;
173 
174   // Emit an error if the Mach-O binary does not contain an rpath path name
175   // specified in -delete_rpath.
176   for (StringRef RPath : MachOConfig.RPathsToRemove) {
177     if (RPathsToRemove.count(RPath))
178       return createStringError(errc::invalid_argument,
179                                "no LC_RPATH load command with path: %s",
180                                RPath.str().c_str());
181   }
182 
183   DenseSet<StringRef> RPaths;
184 
185   // Get all existing RPaths.
186   for (LoadCommand &LC : Obj.LoadCommands) {
187     if (LC.MachOLoadCommand.load_command_data.cmd == MachO::LC_RPATH)
188       RPaths.insert(getPayloadString(LC));
189   }
190 
191   // Throw errors for invalid RPaths.
192   for (const auto &OldNew : MachOConfig.RPathsToUpdate) {
193     StringRef Old = OldNew.getFirst();
194     StringRef New = OldNew.getSecond();
195     if (!RPaths.contains(Old))
196       return createStringError(errc::invalid_argument,
197                                "no LC_RPATH load command with path: " + Old);
198     if (RPaths.contains(New))
199       return createStringError(errc::invalid_argument,
200                                "rpath '" + New +
201                                    "' would create a duplicate load command");
202   }
203 
204   // Update load commands.
205   for (LoadCommand &LC : Obj.LoadCommands) {
206     switch (LC.MachOLoadCommand.load_command_data.cmd) {
207     case MachO::LC_ID_DYLIB:
208       if (MachOConfig.SharedLibId)
209         updateLoadCommandPayloadString<MachO::dylib_command>(
210             LC, *MachOConfig.SharedLibId);
211       break;
212 
213     case MachO::LC_RPATH: {
214       StringRef RPath = getPayloadString(LC);
215       StringRef NewRPath = MachOConfig.RPathsToUpdate.lookup(RPath);
216       if (!NewRPath.empty())
217         updateLoadCommandPayloadString<MachO::rpath_command>(LC, NewRPath);
218       break;
219     }
220 
221     // TODO: Add LC_REEXPORT_DYLIB, LC_LAZY_LOAD_DYLIB, and LC_LOAD_UPWARD_DYLIB
222     // here once llvm-objcopy supports them.
223     case MachO::LC_LOAD_DYLIB:
224     case MachO::LC_LOAD_WEAK_DYLIB:
225       StringRef InstallName = getPayloadString(LC);
226       StringRef NewInstallName =
227           MachOConfig.InstallNamesToUpdate.lookup(InstallName);
228       if (!NewInstallName.empty())
229         updateLoadCommandPayloadString<MachO::dylib_command>(LC,
230                                                              NewInstallName);
231       break;
232     }
233   }
234 
235   // Add new RPaths.
236   for (StringRef RPath : MachOConfig.RPathToAdd) {
237     if (RPaths.contains(RPath))
238       return createStringError(errc::invalid_argument,
239                                "rpath '" + RPath +
240                                    "' would create a duplicate load command");
241     RPaths.insert(RPath);
242     Obj.LoadCommands.push_back(buildRPathLoadCommand(RPath));
243   }
244 
245   for (StringRef RPath : MachOConfig.RPathToPrepend) {
246     if (RPaths.contains(RPath))
247       return createStringError(errc::invalid_argument,
248                                "rpath '" + RPath +
249                                    "' would create a duplicate load command");
250 
251     RPaths.insert(RPath);
252     Obj.LoadCommands.insert(Obj.LoadCommands.begin(),
253                             buildRPathLoadCommand(RPath));
254   }
255 
256   // Unlike appending rpaths, the indexes of subsequent load commands must
257   // be recalculated after prepending one.
258   if (!MachOConfig.RPathToPrepend.empty())
259     Obj.updateLoadCommandIndexes();
260 
261   return Error::success();
262 }
263 
264 static Error dumpSectionToFile(StringRef SecName, StringRef Filename,
265                                Object &Obj) {
266   for (LoadCommand &LC : Obj.LoadCommands)
267     for (const std::unique_ptr<Section> &Sec : LC.Sections) {
268       if (Sec->CanonicalName == SecName) {
269         Expected<std::unique_ptr<FileOutputBuffer>> BufferOrErr =
270             FileOutputBuffer::create(Filename, Sec->Content.size());
271         if (!BufferOrErr)
272           return BufferOrErr.takeError();
273         std::unique_ptr<FileOutputBuffer> Buf = std::move(*BufferOrErr);
274         llvm::copy(Sec->Content, Buf->getBufferStart());
275 
276         if (Error E = Buf->commit())
277           return E;
278         return Error::success();
279       }
280     }
281 
282   return createStringError(object_error::parse_failed, "section '%s' not found",
283                            SecName.str().c_str());
284 }
285 
286 static Error addSection(const NewSectionInfo &NewSection, Object &Obj) {
287   std::pair<StringRef, StringRef> Pair = NewSection.SectionName.split(',');
288   StringRef TargetSegName = Pair.first;
289   Section Sec(TargetSegName, Pair.second);
290   Sec.Content =
291       Obj.NewSectionsContents.save(NewSection.SectionData->getBuffer());
292   Sec.Size = Sec.Content.size();
293 
294   // Add the a section into an existing segment.
295   for (LoadCommand &LC : Obj.LoadCommands) {
296     Optional<StringRef> SegName = LC.getSegmentName();
297     if (SegName && SegName == TargetSegName) {
298       uint64_t Addr = *LC.getSegmentVMAddr();
299       for (const std::unique_ptr<Section> &S : LC.Sections)
300         Addr = std::max(Addr, S->Addr + S->Size);
301       LC.Sections.push_back(std::make_unique<Section>(Sec));
302       LC.Sections.back()->Addr = Addr;
303       return Error::success();
304     }
305   }
306 
307   // There's no segment named TargetSegName. Create a new load command and
308   // Insert a new section into it.
309   LoadCommand &NewSegment =
310       Obj.addSegment(TargetSegName, alignTo(Sec.Size, 16384));
311   NewSegment.Sections.push_back(std::make_unique<Section>(Sec));
312   NewSegment.Sections.back()->Addr = *NewSegment.getSegmentVMAddr();
313   return Error::success();
314 }
315 
316 static Expected<Section &> findSection(StringRef SecName, Object &O) {
317   StringRef SegName;
318   std::tie(SegName, SecName) = SecName.split(",");
319   auto FoundSeg =
320       llvm::find_if(O.LoadCommands, [SegName](const LoadCommand &LC) {
321         return LC.getSegmentName() == SegName;
322       });
323   if (FoundSeg == O.LoadCommands.end())
324     return createStringError(errc::invalid_argument,
325                              "could not find segment with name '%s'",
326                              SegName.str().c_str());
327   auto FoundSec = llvm::find_if(FoundSeg->Sections,
328                                 [SecName](const std::unique_ptr<Section> &Sec) {
329                                   return Sec->Sectname == SecName;
330                                 });
331   if (FoundSec == FoundSeg->Sections.end())
332     return createStringError(errc::invalid_argument,
333                              "could not find section with name '%s'",
334                              SecName.str().c_str());
335 
336   assert(FoundSec->get()->CanonicalName == (SegName + "," + SecName).str());
337   return *FoundSec->get();
338 }
339 
340 static Error updateSection(const NewSectionInfo &NewSection, Object &O) {
341   Expected<Section &> SecToUpdateOrErr = findSection(NewSection.SectionName, O);
342 
343   if (!SecToUpdateOrErr)
344     return SecToUpdateOrErr.takeError();
345   Section &Sec = *SecToUpdateOrErr;
346 
347   if (NewSection.SectionData->getBufferSize() > Sec.Size)
348     return createStringError(
349         errc::invalid_argument,
350         "new section cannot be larger than previous section");
351   Sec.Content = O.NewSectionsContents.save(NewSection.SectionData->getBuffer());
352   Sec.Size = Sec.Content.size();
353   return Error::success();
354 }
355 
356 // isValidMachOCannonicalName returns success if Name is a MachO cannonical name
357 // ("<segment>,<section>") and lengths of both segment and section names are
358 // valid.
359 static Error isValidMachOCannonicalName(StringRef Name) {
360   if (Name.count(',') != 1)
361     return createStringError(errc::invalid_argument,
362                              "invalid section name '%s' (should be formatted "
363                              "as '<segment name>,<section name>')",
364                              Name.str().c_str());
365 
366   std::pair<StringRef, StringRef> Pair = Name.split(',');
367   if (Pair.first.size() > 16)
368     return createStringError(errc::invalid_argument,
369                              "too long segment name: '%s'",
370                              Pair.first.str().c_str());
371   if (Pair.second.size() > 16)
372     return createStringError(errc::invalid_argument,
373                              "too long section name: '%s'",
374                              Pair.second.str().c_str());
375   return Error::success();
376 }
377 
378 static Error handleArgs(const CommonConfig &Config,
379                         const MachOConfig &MachOConfig, Object &Obj) {
380   // Dump sections before add/remove for compatibility with GNU objcopy.
381   for (StringRef Flag : Config.DumpSection) {
382     StringRef SectionName;
383     StringRef FileName;
384     std::tie(SectionName, FileName) = Flag.split('=');
385     if (Error E = dumpSectionToFile(SectionName, FileName, Obj))
386       return E;
387   }
388 
389   if (Error E = removeSections(Config, Obj))
390     return E;
391 
392   // Mark symbols to determine which symbols are still needed.
393   if (Config.StripAll)
394     markSymbols(Config, Obj);
395 
396   updateAndRemoveSymbols(Config, MachOConfig, Obj);
397 
398   if (Config.StripAll)
399     for (LoadCommand &LC : Obj.LoadCommands)
400       for (std::unique_ptr<Section> &Sec : LC.Sections)
401         Sec->Relocations.clear();
402 
403   for (const NewSectionInfo &NewSection : Config.AddSection) {
404     if (Error E = isValidMachOCannonicalName(NewSection.SectionName))
405       return E;
406     if (Error E = addSection(NewSection, Obj))
407       return E;
408   }
409 
410   for (const NewSectionInfo &NewSection : Config.UpdateSection) {
411     if (Error E = isValidMachOCannonicalName(NewSection.SectionName))
412       return E;
413     if (Error E = updateSection(NewSection, Obj))
414       return E;
415   }
416 
417   if (Error E = processLoadCommands(MachOConfig, Obj))
418     return E;
419 
420   return Error::success();
421 }
422 
423 Error objcopy::macho::executeObjcopyOnBinary(const CommonConfig &Config,
424                                              const MachOConfig &MachOConfig,
425                                              object::MachOObjectFile &In,
426                                              raw_ostream &Out) {
427   MachOReader Reader(In);
428   Expected<std::unique_ptr<Object>> O = Reader.create();
429   if (!O)
430     return createFileError(Config.InputFilename, O.takeError());
431 
432   if (O->get()->Header.FileType == MachO::HeaderFileType::MH_PRELOAD)
433     return createStringError(std::errc::not_supported,
434                              "%s: MH_PRELOAD files are not supported",
435                              Config.InputFilename.str().c_str());
436 
437   if (Error E = handleArgs(Config, MachOConfig, **O))
438     return createFileError(Config.InputFilename, std::move(E));
439 
440   // Page size used for alignment of segment sizes in Mach-O executables and
441   // dynamic libraries.
442   uint64_t PageSize;
443   switch (In.getArch()) {
444   case Triple::ArchType::arm:
445   case Triple::ArchType::aarch64:
446   case Triple::ArchType::aarch64_32:
447     PageSize = 16384;
448     break;
449   default:
450     PageSize = 4096;
451   }
452 
453   MachOWriter Writer(**O, In.is64Bit(), In.isLittleEndian(),
454                      sys::path::filename(Config.OutputFilename), PageSize, Out);
455   if (auto E = Writer.finalize())
456     return E;
457   return Writer.write();
458 }
459 
460 Error objcopy::macho::executeObjcopyOnMachOUniversalBinary(
461     const MultiFormatConfig &Config, const MachOUniversalBinary &In,
462     raw_ostream &Out) {
463   SmallVector<OwningBinary<Binary>, 2> Binaries;
464   SmallVector<Slice, 2> Slices;
465   for (const auto &O : In.objects()) {
466     Expected<std::unique_ptr<Archive>> ArOrErr = O.getAsArchive();
467     if (ArOrErr) {
468       Expected<std::vector<NewArchiveMember>> NewArchiveMembersOrErr =
469           createNewArchiveMembers(Config, **ArOrErr);
470       if (!NewArchiveMembersOrErr)
471         return NewArchiveMembersOrErr.takeError();
472       Expected<std::unique_ptr<MemoryBuffer>> OutputBufferOrErr =
473           writeArchiveToBuffer(*NewArchiveMembersOrErr,
474                                (*ArOrErr)->hasSymbolTable(), (*ArOrErr)->kind(),
475                                Config.getCommonConfig().DeterministicArchives,
476                                (*ArOrErr)->isThin());
477       if (!OutputBufferOrErr)
478         return OutputBufferOrErr.takeError();
479       Expected<std::unique_ptr<Binary>> BinaryOrErr =
480           object::createBinary(**OutputBufferOrErr);
481       if (!BinaryOrErr)
482         return BinaryOrErr.takeError();
483       Binaries.emplace_back(std::move(*BinaryOrErr),
484                             std::move(*OutputBufferOrErr));
485       Slices.emplace_back(*cast<Archive>(Binaries.back().getBinary()),
486                           O.getCPUType(), O.getCPUSubType(),
487                           O.getArchFlagName(), O.getAlign());
488       continue;
489     }
490     // The methods getAsArchive, getAsObjectFile, getAsIRObject of the class
491     // ObjectForArch return an Error in case of the type mismatch. We need to
492     // check each in turn to see what kind of slice this is, so ignore errors
493     // produced along the way.
494     consumeError(ArOrErr.takeError());
495 
496     Expected<std::unique_ptr<MachOObjectFile>> ObjOrErr = O.getAsObjectFile();
497     if (!ObjOrErr) {
498       consumeError(ObjOrErr.takeError());
499       return createStringError(
500           std::errc::invalid_argument,
501           "slice for '%s' of the universal Mach-O binary "
502           "'%s' is not a Mach-O object or an archive",
503           O.getArchFlagName().c_str(),
504           Config.getCommonConfig().InputFilename.str().c_str());
505     }
506     std::string ArchFlagName = O.getArchFlagName();
507 
508     SmallVector<char, 0> Buffer;
509     raw_svector_ostream MemStream(Buffer);
510 
511     Expected<const MachOConfig &> MachO = Config.getMachOConfig();
512     if (!MachO)
513       return MachO.takeError();
514 
515     if (Error E = executeObjcopyOnBinary(Config.getCommonConfig(), *MachO,
516                                          **ObjOrErr, MemStream))
517       return E;
518 
519     auto MB = std::make_unique<SmallVectorMemoryBuffer>(
520         std::move(Buffer), ArchFlagName, /*RequiresNullTerminator=*/false);
521     Expected<std::unique_ptr<Binary>> BinaryOrErr = object::createBinary(*MB);
522     if (!BinaryOrErr)
523       return BinaryOrErr.takeError();
524     Binaries.emplace_back(std::move(*BinaryOrErr), std::move(MB));
525     Slices.emplace_back(*cast<MachOObjectFile>(Binaries.back().getBinary()),
526                         O.getAlign());
527   }
528 
529   if (Error Err = writeUniversalBinaryToStream(Slices, Out))
530     return Err;
531 
532   return Error::success();
533 }
534