Alexander Shaposhnikov | d911ed1 | 2019-02-02 00:38:07 +0000 | [diff] [blame] | 1 | //===- MachOObjcopy.cpp -----------------------------------------*- C++ -*-===// |
| 2 | // |
Chandler Carruth | 127252b | 2019-02-11 08:25:19 +0000 | [diff] [blame] | 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 |
Alexander Shaposhnikov | d911ed1 | 2019-02-02 00:38:07 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "MachOObjcopy.h" |
| 10 | #include "../CopyConfig.h" |
Alexander Shaposhnikov | d911ed1 | 2019-02-02 00:38:07 +0000 | [diff] [blame] | 11 | #include "MachOReader.h" |
| 12 | #include "MachOWriter.h" |
Seiya Nuta | 4bc7101 | 2019-05-29 22:21:12 +0000 | [diff] [blame] | 13 | #include "llvm/Support/Errc.h" |
Alexander Shaposhnikov | d911ed1 | 2019-02-02 00:38:07 +0000 | [diff] [blame] | 14 | #include "llvm/Support/Error.h" |
| 15 | |
| 16 | namespace llvm { |
| 17 | namespace objcopy { |
| 18 | namespace macho { |
| 19 | |
Seiya Nuta | 4bc7101 | 2019-05-29 22:21:12 +0000 | [diff] [blame] | 20 | using namespace object; |
Seiya Nuta | 7f19dd1 | 2019-10-28 15:40:37 +0900 | [diff] [blame] | 21 | using SectionPred = std::function<bool(const Section &Sec)>; |
| 22 | |
| 23 | static void removeSections(const CopyConfig &Config, Object &Obj) { |
| 24 | SectionPred RemovePred = [](const Section &) { return false; }; |
| 25 | |
Seiya Nuta | bc11830 | 2019-11-15 12:37:55 +0900 | [diff] [blame] | 26 | if (!Config.ToRemove.empty()) { |
| 27 | RemovePred = [&Config, RemovePred](const Section &Sec) { |
| 28 | return Config.ToRemove.matches(Sec.CanonicalName); |
| 29 | }; |
| 30 | } |
| 31 | |
Fangrui Song | 30ccee7 | 2019-11-18 15:25:04 -0800 | [diff] [blame^] | 32 | if (Config.StripAll || Config.StripDebug) { |
Seiya Nuta | 9bbf2a1 | 2019-10-31 13:51:11 +0900 | [diff] [blame] | 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 | |
Seiya Nuta | 7f19dd1 | 2019-10-28 15:40:37 +0900 | [diff] [blame] | 42 | if (!Config.OnlySection.empty()) { |
Seiya Nuta | bc11830 | 2019-11-15 12:37:55 +0900 | [diff] [blame] | 43 | // Overwrite RemovePred because --only-section takes priority. |
| 44 | RemovePred = [&Config](const Section &Sec) { |
Seiya Nuta | 7f19dd1 | 2019-10-28 15:40:37 +0900 | [diff] [blame] | 45 | return !Config.OnlySection.matches(Sec.CanonicalName); |
| 46 | }; |
| 47 | } |
| 48 | |
| 49 | return Obj.removeSections(RemovePred); |
| 50 | } |
Seiya Nuta | 4bc7101 | 2019-05-29 22:21:12 +0000 | [diff] [blame] | 51 | |
Seiya Nuta | 9bbf2a1 | 2019-10-31 13:51:11 +0900 | [diff] [blame] | 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 | |
Fangrui Song | 28a5dc7 | 2019-11-13 13:10:15 -0800 | [diff] [blame] | 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 | |
Seiya Nuta | 9bbf2a1 | 2019-10-31 13:51:11 +0900 | [diff] [blame] | 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 | |
Alexander Shaposhnikov | c54959c | 2019-11-19 23:30:52 -0800 | [diff] [blame] | 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 | |
Seiya Nuta | 4bc7101 | 2019-05-29 22:21:12 +0000 | [diff] [blame] | 87 | static Error handleArgs(const CopyConfig &Config, Object &Obj) { |
| 88 | if (Config.AllowBrokenLinks || !Config.BuildIdLinkDir.empty() || |
| 89 | Config.BuildIdLinkInput || Config.BuildIdLinkOutput || |
| 90 | !Config.SplitDWO.empty() || !Config.SymbolsPrefix.empty() || |
| 91 | !Config.AllocSectionsPrefix.empty() || !Config.AddSection.empty() || |
| 92 | !Config.DumpSection.empty() || !Config.KeepSection.empty() || |
Seiya Nuta | 7f19dd1 | 2019-10-28 15:40:37 +0900 | [diff] [blame] | 93 | Config.NewSymbolVisibility || !Config.SymbolsToGlobalize.empty() || |
| 94 | !Config.SymbolsToKeep.empty() || !Config.SymbolsToLocalize.empty() || |
| 95 | !Config.SymbolsToWeaken.empty() || !Config.SymbolsToKeepGlobal.empty() || |
Fangrui Song | 28a5dc7 | 2019-11-13 13:10:15 -0800 | [diff] [blame] | 96 | !Config.SectionsToRename.empty() || |
Seiya Nuta | 4bc7101 | 2019-05-29 22:21:12 +0000 | [diff] [blame] | 97 | !Config.UnneededSymbolsToRemove.empty() || |
Fangrui Song | 671fb34 | 2019-10-02 12:41:25 +0000 | [diff] [blame] | 98 | !Config.SetSectionAlignment.empty() || !Config.SetSectionFlags.empty() || |
Seiya Nuta | bc11830 | 2019-11-15 12:37:55 +0900 | [diff] [blame] | 99 | Config.ExtractDWO || Config.KeepFileSymbols || Config.LocalizeHidden || |
| 100 | Config.PreserveDates || Config.StripAllGNU || Config.StripDWO || |
| 101 | Config.StripNonAlloc || Config.StripSections || Config.Weaken || |
Fangrui Song | 30ccee7 | 2019-11-18 15:25:04 -0800 | [diff] [blame^] | 102 | Config.DecompressDebugSections || Config.StripNonAlloc || |
| 103 | Config.StripSections || Config.StripUnneeded || |
Fangrui Song | 671fb34 | 2019-10-02 12:41:25 +0000 | [diff] [blame] | 104 | Config.DiscardMode != DiscardType::None || !Config.SymbolsToAdd.empty() || |
| 105 | Config.EntryExpr) { |
Seiya Nuta | 4bc7101 | 2019-05-29 22:21:12 +0000 | [diff] [blame] | 106 | return createStringError(llvm::errc::invalid_argument, |
| 107 | "option not supported by llvm-objcopy for MachO"); |
| 108 | } |
Seiya Nuta | 7f19dd1 | 2019-10-28 15:40:37 +0900 | [diff] [blame] | 109 | removeSections(Config, Obj); |
Seiya Nuta | 9bbf2a1 | 2019-10-31 13:51:11 +0900 | [diff] [blame] | 110 | |
| 111 | // Mark symbols to determine which symbols are still needed. |
| 112 | if (Config.StripAll) |
| 113 | markSymbols(Config, Obj); |
| 114 | |
Fangrui Song | 28a5dc7 | 2019-11-13 13:10:15 -0800 | [diff] [blame] | 115 | updateAndRemoveSymbols(Config, Obj); |
Seiya Nuta | 9bbf2a1 | 2019-10-31 13:51:11 +0900 | [diff] [blame] | 116 | |
| 117 | if (Config.StripAll) |
| 118 | for (LoadCommand &LC : Obj.LoadCommands) |
| 119 | for (Section &Sec : LC.Sections) |
| 120 | Sec.Relocations.clear(); |
| 121 | |
Alexander Shaposhnikov | c54959c | 2019-11-19 23:30:52 -0800 | [diff] [blame] | 122 | for (StringRef RPath : Config.RPathToAdd) { |
| 123 | for (LoadCommand &LC : Obj.LoadCommands) { |
| 124 | if (LC.MachOLoadCommand.load_command_data.cmd == MachO::LC_RPATH && |
| 125 | RPath == StringRef(reinterpret_cast<char *>(LC.Payload.data()), |
| 126 | LC.Payload.size()) |
| 127 | .trim(0)) { |
| 128 | return createStringError(errc::invalid_argument, |
| 129 | "rpath " + RPath + |
| 130 | " would create a duplicate load command"); |
| 131 | } |
| 132 | } |
| 133 | Obj.addLoadCommand(buildRPathLoadCommand(RPath)); |
| 134 | } |
Seiya Nuta | 4bc7101 | 2019-05-29 22:21:12 +0000 | [diff] [blame] | 135 | return Error::success(); |
| 136 | } |
| 137 | |
Alexander Shaposhnikov | d911ed1 | 2019-02-02 00:38:07 +0000 | [diff] [blame] | 138 | Error executeObjcopyOnBinary(const CopyConfig &Config, |
| 139 | object::MachOObjectFile &In, Buffer &Out) { |
| 140 | MachOReader Reader(In); |
| 141 | std::unique_ptr<Object> O = Reader.create(); |
Seiya Nuta | 4bc7101 | 2019-05-29 22:21:12 +0000 | [diff] [blame] | 142 | if (!O) |
| 143 | return createFileError( |
| 144 | Config.InputFilename, |
| 145 | createStringError(object_error::parse_failed, |
| 146 | "unable to deserialize MachO object")); |
| 147 | |
| 148 | if (Error E = handleArgs(Config, *O)) |
| 149 | return createFileError(Config.InputFilename, std::move(E)); |
| 150 | |
Seiya Nuta | 552bcb8 | 2019-08-19 21:05:31 +0000 | [diff] [blame] | 151 | // TODO: Support 16KB pages which are employed in iOS arm64 binaries: |
| 152 | // https://github.com/llvm/llvm-project/commit/1bebb2832ee312d3b0316dacff457a7a29435edb |
| 153 | const uint64_t PageSize = 4096; |
| 154 | |
| 155 | MachOWriter Writer(*O, In.is64Bit(), In.isLittleEndian(), PageSize, Out); |
Seiya Nuta | b728e53 | 2019-06-08 01:22:54 +0000 | [diff] [blame] | 156 | if (auto E = Writer.finalize()) |
| 157 | return E; |
Alexander Shaposhnikov | d911ed1 | 2019-02-02 00:38:07 +0000 | [diff] [blame] | 158 | return Writer.write(); |
| 159 | } |
| 160 | |
| 161 | } // end namespace macho |
| 162 | } // end namespace objcopy |
| 163 | } // end namespace llvm |