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 | |
| 26 | if (!Config.OnlySection.empty()) { |
| 27 | RemovePred = [&Config, RemovePred](const Section &Sec) { |
| 28 | return !Config.OnlySection.matches(Sec.CanonicalName); |
| 29 | }; |
| 30 | } |
| 31 | |
| 32 | return Obj.removeSections(RemovePred); |
| 33 | } |
Seiya Nuta | 4bc7101 | 2019-05-29 22:21:12 +0000 | [diff] [blame] | 34 | |
| 35 | static Error handleArgs(const CopyConfig &Config, Object &Obj) { |
| 36 | if (Config.AllowBrokenLinks || !Config.BuildIdLinkDir.empty() || |
| 37 | Config.BuildIdLinkInput || Config.BuildIdLinkOutput || |
| 38 | !Config.SplitDWO.empty() || !Config.SymbolsPrefix.empty() || |
| 39 | !Config.AllocSectionsPrefix.empty() || !Config.AddSection.empty() || |
| 40 | !Config.DumpSection.empty() || !Config.KeepSection.empty() || |
Seiya Nuta | 7f19dd1 | 2019-10-28 15:40:37 +0900 | [diff] [blame^] | 41 | Config.NewSymbolVisibility || !Config.SymbolsToGlobalize.empty() || |
| 42 | !Config.SymbolsToKeep.empty() || !Config.SymbolsToLocalize.empty() || |
| 43 | !Config.SymbolsToWeaken.empty() || !Config.SymbolsToKeepGlobal.empty() || |
| 44 | !Config.SectionsToRename.empty() || !Config.SymbolsToRename.empty() || |
Seiya Nuta | 4bc7101 | 2019-05-29 22:21:12 +0000 | [diff] [blame] | 45 | !Config.UnneededSymbolsToRemove.empty() || |
Fangrui Song | 671fb34 | 2019-10-02 12:41:25 +0000 | [diff] [blame] | 46 | !Config.SetSectionAlignment.empty() || !Config.SetSectionFlags.empty() || |
| 47 | !Config.ToRemove.empty() || Config.ExtractDWO || Config.KeepFileSymbols || |
| 48 | Config.LocalizeHidden || Config.PreserveDates || Config.StripDWO || |
| 49 | Config.StripNonAlloc || Config.StripSections || Config.Weaken || |
| 50 | Config.DecompressDebugSections || Config.StripDebug || |
| 51 | Config.StripNonAlloc || Config.StripSections || Config.StripUnneeded || |
| 52 | Config.DiscardMode != DiscardType::None || !Config.SymbolsToAdd.empty() || |
| 53 | Config.EntryExpr) { |
Seiya Nuta | 4bc7101 | 2019-05-29 22:21:12 +0000 | [diff] [blame] | 54 | return createStringError(llvm::errc::invalid_argument, |
| 55 | "option not supported by llvm-objcopy for MachO"); |
| 56 | } |
| 57 | |
Seiya Nuta | 7f19dd1 | 2019-10-28 15:40:37 +0900 | [diff] [blame^] | 58 | removeSections(Config, Obj); |
Seiya Nuta | 4bc7101 | 2019-05-29 22:21:12 +0000 | [diff] [blame] | 59 | return Error::success(); |
| 60 | } |
| 61 | |
Alexander Shaposhnikov | d911ed1 | 2019-02-02 00:38:07 +0000 | [diff] [blame] | 62 | Error executeObjcopyOnBinary(const CopyConfig &Config, |
| 63 | object::MachOObjectFile &In, Buffer &Out) { |
| 64 | MachOReader Reader(In); |
| 65 | std::unique_ptr<Object> O = Reader.create(); |
Seiya Nuta | 4bc7101 | 2019-05-29 22:21:12 +0000 | [diff] [blame] | 66 | if (!O) |
| 67 | return createFileError( |
| 68 | Config.InputFilename, |
| 69 | createStringError(object_error::parse_failed, |
| 70 | "unable to deserialize MachO object")); |
| 71 | |
| 72 | if (Error E = handleArgs(Config, *O)) |
| 73 | return createFileError(Config.InputFilename, std::move(E)); |
| 74 | |
Seiya Nuta | 552bcb8 | 2019-08-19 21:05:31 +0000 | [diff] [blame] | 75 | // TODO: Support 16KB pages which are employed in iOS arm64 binaries: |
| 76 | // https://github.com/llvm/llvm-project/commit/1bebb2832ee312d3b0316dacff457a7a29435edb |
| 77 | const uint64_t PageSize = 4096; |
| 78 | |
| 79 | MachOWriter Writer(*O, In.is64Bit(), In.isLittleEndian(), PageSize, Out); |
Seiya Nuta | b728e53 | 2019-06-08 01:22:54 +0000 | [diff] [blame] | 80 | if (auto E = Writer.finalize()) |
| 81 | return E; |
Alexander Shaposhnikov | d911ed1 | 2019-02-02 00:38:07 +0000 | [diff] [blame] | 82 | return Writer.write(); |
| 83 | } |
| 84 | |
| 85 | } // end namespace macho |
| 86 | } // end namespace objcopy |
| 87 | } // end namespace llvm |