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