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   // Remove any empty segments if required.
262   if (!MachOConfig.EmptySegmentsToRemove.empty()) {
263     auto RemovePred = [&MachOConfig](const LoadCommand &LC) {
264       if (LC.MachOLoadCommand.load_command_data.cmd == MachO::LC_SEGMENT_64 ||
265           LC.MachOLoadCommand.load_command_data.cmd == MachO::LC_SEGMENT) {
266         return LC.Sections.empty() &&
267                MachOConfig.EmptySegmentsToRemove.contains(
268                    LC.getSegmentName().getValue());
269       }
270       return false;
271     };
272     if (Error E = Obj.removeLoadCommands(RemovePred))
273       return E;
274   }
275 
276   return Error::success();
277 }
278 
279 static Error dumpSectionToFile(StringRef SecName, StringRef Filename,
280                                Object &Obj) {
281   for (LoadCommand &LC : Obj.LoadCommands)
282     for (const std::unique_ptr<Section> &Sec : LC.Sections) {
283       if (Sec->CanonicalName == SecName) {
284         Expected<std::unique_ptr<FileOutputBuffer>> BufferOrErr =
285             FileOutputBuffer::create(Filename, Sec->Content.size());
286         if (!BufferOrErr)
287           return BufferOrErr.takeError();
288         std::unique_ptr<FileOutputBuffer> Buf = std::move(*BufferOrErr);
289         llvm::copy(Sec->Content, Buf->getBufferStart());
290 
291         if (Error E = Buf->commit())
292           return E;
293         return Error::success();
294       }
295     }
296 
297   return createStringError(object_error::parse_failed, "section '%s' not found",
298                            SecName.str().c_str());
299 }
300 
301 static Error addSection(const NewSectionInfo &NewSection, Object &Obj) {
302   std::pair<StringRef, StringRef> Pair = NewSection.SectionName.split(',');
303   StringRef TargetSegName = Pair.first;
304   Section Sec(TargetSegName, Pair.second);
305   Sec.Content =
306       Obj.NewSectionsContents.save(NewSection.SectionData->getBuffer());
307   Sec.Size = Sec.Content.size();
308 
309   // Add the a section into an existing segment.
310   for (LoadCommand &LC : Obj.LoadCommands) {
311     Optional<StringRef> SegName = LC.getSegmentName();
312     if (SegName && SegName == TargetSegName) {
313       uint64_t Addr = *LC.getSegmentVMAddr();
314       for (const std::unique_ptr<Section> &S : LC.Sections)
315         Addr = std::max(Addr, S->Addr + S->Size);
316       LC.Sections.push_back(std::make_unique<Section>(Sec));
317       LC.Sections.back()->Addr = Addr;
318       return Error::success();
319     }
320   }
321 
322   // There's no segment named TargetSegName. Create a new load command and
323   // Insert a new section into it.
324   LoadCommand &NewSegment =
325       Obj.addSegment(TargetSegName, alignTo(Sec.Size, 16384));
326   NewSegment.Sections.push_back(std::make_unique<Section>(Sec));
327   NewSegment.Sections.back()->Addr = *NewSegment.getSegmentVMAddr();
328   return Error::success();
329 }
330 
331 static Expected<Section &> findSection(StringRef SecName, Object &O) {
332   StringRef SegName;
333   std::tie(SegName, SecName) = SecName.split(",");
334   auto FoundSeg =
335       llvm::find_if(O.LoadCommands, [SegName](const LoadCommand &LC) {
336         return LC.getSegmentName() == SegName;
337       });
338   if (FoundSeg == O.LoadCommands.end())
339     return createStringError(errc::invalid_argument,
340                              "could not find segment with name '%s'",
341                              SegName.str().c_str());
342   auto FoundSec = llvm::find_if(FoundSeg->Sections,
343                                 [SecName](const std::unique_ptr<Section> &Sec) {
344                                   return Sec->Sectname == SecName;
345                                 });
346   if (FoundSec == FoundSeg->Sections.end())
347     return createStringError(errc::invalid_argument,
348                              "could not find section with name '%s'",
349                              SecName.str().c_str());
350 
351   assert(FoundSec->get()->CanonicalName == (SegName + "," + SecName).str());
352   return *FoundSec->get();
353 }
354 
355 static Error updateSection(const NewSectionInfo &NewSection, Object &O) {
356   Expected<Section &> SecToUpdateOrErr = findSection(NewSection.SectionName, O);
357 
358   if (!SecToUpdateOrErr)
359     return SecToUpdateOrErr.takeError();
360   Section &Sec = *SecToUpdateOrErr;
361 
362   if (NewSection.SectionData->getBufferSize() > Sec.Size)
363     return createStringError(
364         errc::invalid_argument,
365         "new section cannot be larger than previous section");
366   Sec.Content = O.NewSectionsContents.save(NewSection.SectionData->getBuffer());
367   Sec.Size = Sec.Content.size();
368   return Error::success();
369 }
370 
371 // isValidMachOCannonicalName returns success if Name is a MachO cannonical name
372 // ("<segment>,<section>") and lengths of both segment and section names are
373 // valid.
374 static Error isValidMachOCannonicalName(StringRef Name) {
375   if (Name.count(',') != 1)
376     return createStringError(errc::invalid_argument,
377                              "invalid section name '%s' (should be formatted "
378                              "as '<segment name>,<section name>')",
379                              Name.str().c_str());
380 
381   std::pair<StringRef, StringRef> Pair = Name.split(',');
382   if (Pair.first.size() > 16)
383     return createStringError(errc::invalid_argument,
384                              "too long segment name: '%s'",
385                              Pair.first.str().c_str());
386   if (Pair.second.size() > 16)
387     return createStringError(errc::invalid_argument,
388                              "too long section name: '%s'",
389                              Pair.second.str().c_str());
390   return Error::success();
391 }
392 
393 static Error handleArgs(const CommonConfig &Config,
394                         const MachOConfig &MachOConfig, Object &Obj) {
395   // Dump sections before add/remove for compatibility with GNU objcopy.
396   for (StringRef Flag : Config.DumpSection) {
397     StringRef SectionName;
398     StringRef FileName;
399     std::tie(SectionName, FileName) = Flag.split('=');
400     if (Error E = dumpSectionToFile(SectionName, FileName, Obj))
401       return E;
402   }
403 
404   if (Error E = removeSections(Config, Obj))
405     return E;
406 
407   // Mark symbols to determine which symbols are still needed.
408   if (Config.StripAll)
409     markSymbols(Config, Obj);
410 
411   updateAndRemoveSymbols(Config, MachOConfig, Obj);
412 
413   if (Config.StripAll)
414     for (LoadCommand &LC : Obj.LoadCommands)
415       for (std::unique_ptr<Section> &Sec : LC.Sections)
416         Sec->Relocations.clear();
417 
418   for (const NewSectionInfo &NewSection : Config.AddSection) {
419     if (Error E = isValidMachOCannonicalName(NewSection.SectionName))
420       return E;
421     if (Error E = addSection(NewSection, Obj))
422       return E;
423   }
424 
425   for (const NewSectionInfo &NewSection : Config.UpdateSection) {
426     if (Error E = isValidMachOCannonicalName(NewSection.SectionName))
427       return E;
428     if (Error E = updateSection(NewSection, Obj))
429       return E;
430   }
431 
432   if (Error E = processLoadCommands(MachOConfig, Obj))
433     return E;
434 
435   return Error::success();
436 }
437 
438 Error objcopy::macho::executeObjcopyOnBinary(const CommonConfig &Config,
439                                              const MachOConfig &MachOConfig,
440                                              object::MachOObjectFile &In,
441                                              raw_ostream &Out) {
442   MachOReader Reader(In);
443   Expected<std::unique_ptr<Object>> O = Reader.create();
444   if (!O)
445     return createFileError(Config.InputFilename, O.takeError());
446 
447   if (O->get()->Header.FileType == MachO::HeaderFileType::MH_PRELOAD)
448     return createStringError(std::errc::not_supported,
449                              "%s: MH_PRELOAD files are not supported",
450                              Config.InputFilename.str().c_str());
451 
452   if (Error E = handleArgs(Config, MachOConfig, **O))
453     return createFileError(Config.InputFilename, std::move(E));
454 
455   // Page size used for alignment of segment sizes in Mach-O executables and
456   // dynamic libraries.
457   uint64_t PageSize;
458   switch (In.getArch()) {
459   case Triple::ArchType::arm:
460   case Triple::ArchType::aarch64:
461   case Triple::ArchType::aarch64_32:
462     PageSize = 16384;
463     break;
464   default:
465     PageSize = 4096;
466   }
467 
468   MachOWriter Writer(**O, In.is64Bit(), In.isLittleEndian(),
469                      sys::path::filename(Config.OutputFilename), PageSize, Out);
470   if (auto E = Writer.finalize())
471     return E;
472   return Writer.write();
473 }
474 
475 Error objcopy::macho::executeObjcopyOnMachOUniversalBinary(
476     const MultiFormatConfig &Config, const MachOUniversalBinary &In,
477     raw_ostream &Out) {
478   SmallVector<OwningBinary<Binary>, 2> Binaries;
479   SmallVector<Slice, 2> Slices;
480   for (const auto &O : In.objects()) {
481     Expected<std::unique_ptr<Archive>> ArOrErr = O.getAsArchive();
482     if (ArOrErr) {
483       Expected<std::vector<NewArchiveMember>> NewArchiveMembersOrErr =
484           createNewArchiveMembers(Config, **ArOrErr);
485       if (!NewArchiveMembersOrErr)
486         return NewArchiveMembersOrErr.takeError();
487       auto Kind = (*ArOrErr)->kind();
488       if (Kind == object::Archive::K_BSD)
489         Kind = object::Archive::K_DARWIN;
490       Expected<std::unique_ptr<MemoryBuffer>> OutputBufferOrErr =
491           writeArchiveToBuffer(*NewArchiveMembersOrErr,
492                                (*ArOrErr)->hasSymbolTable(), Kind,
493                                Config.getCommonConfig().DeterministicArchives,
494                                (*ArOrErr)->isThin());
495       if (!OutputBufferOrErr)
496         return OutputBufferOrErr.takeError();
497       Expected<std::unique_ptr<Binary>> BinaryOrErr =
498           object::createBinary(**OutputBufferOrErr);
499       if (!BinaryOrErr)
500         return BinaryOrErr.takeError();
501       Binaries.emplace_back(std::move(*BinaryOrErr),
502                             std::move(*OutputBufferOrErr));
503       Slices.emplace_back(*cast<Archive>(Binaries.back().getBinary()),
504                           O.getCPUType(), O.getCPUSubType(),
505                           O.getArchFlagName(), O.getAlign());
506       continue;
507     }
508     // The methods getAsArchive, getAsObjectFile, getAsIRObject of the class
509     // ObjectForArch return an Error in case of the type mismatch. We need to
510     // check each in turn to see what kind of slice this is, so ignore errors
511     // produced along the way.
512     consumeError(ArOrErr.takeError());
513 
514     Expected<std::unique_ptr<MachOObjectFile>> ObjOrErr = O.getAsObjectFile();
515     if (!ObjOrErr) {
516       consumeError(ObjOrErr.takeError());
517       return createStringError(
518           std::errc::invalid_argument,
519           "slice for '%s' of the universal Mach-O binary "
520           "'%s' is not a Mach-O object or an archive",
521           O.getArchFlagName().c_str(),
522           Config.getCommonConfig().InputFilename.str().c_str());
523     }
524     std::string ArchFlagName = O.getArchFlagName();
525 
526     SmallVector<char, 0> Buffer;
527     raw_svector_ostream MemStream(Buffer);
528 
529     Expected<const MachOConfig &> MachO = Config.getMachOConfig();
530     if (!MachO)
531       return MachO.takeError();
532 
533     if (Error E = executeObjcopyOnBinary(Config.getCommonConfig(), *MachO,
534                                          **ObjOrErr, MemStream))
535       return E;
536 
537     auto MB = std::make_unique<SmallVectorMemoryBuffer>(
538         std::move(Buffer), ArchFlagName, /*RequiresNullTerminator=*/false);
539     Expected<std::unique_ptr<Binary>> BinaryOrErr = object::createBinary(*MB);
540     if (!BinaryOrErr)
541       return BinaryOrErr.takeError();
542     Binaries.emplace_back(std::move(*BinaryOrErr), std::move(MB));
543     Slices.emplace_back(*cast<MachOObjectFile>(Binaries.back().getBinary()),
544                         O.getAlign());
545   }
546 
547   if (Error Err = writeUniversalBinaryToStream(Slices, Out))
548     return Err;
549 
550   return Error::success();
551 }
552