Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 1 | //===- CopyConfig.h -------------------------------------------------------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +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 | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #ifndef LLVM_TOOLS_LLVM_OBJCOPY_COPY_CONFIG_H |
| 10 | #define LLVM_TOOLS_LLVM_OBJCOPY_COPY_CONFIG_H |
| 11 | |
| 12 | #include "llvm/ADT/ArrayRef.h" |
| 13 | #include "llvm/ADT/Optional.h" |
| 14 | #include "llvm/ADT/SmallVector.h" |
| 15 | #include "llvm/ADT/StringMap.h" |
| 16 | #include "llvm/ADT/StringRef.h" |
Jordan Rupprecht | 5745c5f | 2019-02-04 18:38:00 +0000 | [diff] [blame] | 17 | #include "llvm/Support/Allocator.h" |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame^] | 18 | #include "llvm/Support/Error.h" |
Eugene Leviant | f324f6d | 2019-02-06 11:00:07 +0000 | [diff] [blame] | 19 | #include "llvm/Support/Regex.h" |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 20 | // Necessary for llvm::DebugCompressionType::None |
| 21 | #include "llvm/Target/TargetOptions.h" |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 22 | #include <vector> |
| 23 | |
| 24 | namespace llvm { |
| 25 | namespace objcopy { |
| 26 | |
| 27 | // This type keeps track of the machine info for various architectures. This |
| 28 | // lets us map architecture names to ELF types and the e_machine value of the |
| 29 | // ELF file. |
| 30 | struct MachineInfo { |
| 31 | uint16_t EMachine; |
| 32 | bool Is64Bit; |
| 33 | bool IsLittleEndian; |
| 34 | }; |
| 35 | |
| 36 | struct SectionRename { |
| 37 | StringRef OriginalName; |
| 38 | StringRef NewName; |
| 39 | Optional<uint64_t> NewFlags; |
| 40 | }; |
| 41 | |
Jordan Rupprecht | c892741 | 2019-01-29 15:05:38 +0000 | [diff] [blame] | 42 | struct SectionFlagsUpdate { |
| 43 | StringRef Name; |
| 44 | uint64_t NewFlags; |
| 45 | }; |
| 46 | |
Jordan Rupprecht | d0f7bcf | 2019-01-30 14:58:13 +0000 | [diff] [blame] | 47 | enum class DiscardType { |
| 48 | None, // Default |
| 49 | All, // --discard-all (-x) |
| 50 | Locals, // --discard-locals (-X) |
| 51 | }; |
| 52 | |
Eugene Leviant | f324f6d | 2019-02-06 11:00:07 +0000 | [diff] [blame] | 53 | class NameOrRegex { |
| 54 | StringRef Name; |
| 55 | // Regex is shared between multiple CopyConfig instances. |
| 56 | std::shared_ptr<Regex> R; |
| 57 | |
| 58 | public: |
| 59 | NameOrRegex(StringRef Pattern, bool IsRegex); |
| 60 | bool operator==(StringRef S) const { return R ? R->match(S) : Name == S; } |
| 61 | bool operator!=(StringRef S) const { return !operator==(S); } |
| 62 | }; |
| 63 | |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 64 | // Configuration for copying/stripping a single file. |
| 65 | struct CopyConfig { |
| 66 | // Main input/output options |
| 67 | StringRef InputFilename; |
| 68 | StringRef InputFormat; |
| 69 | StringRef OutputFilename; |
| 70 | StringRef OutputFormat; |
| 71 | |
Jordan Rupprecht | 70038e0 | 2019-01-07 16:59:12 +0000 | [diff] [blame] | 72 | // Only applicable for --input-format=binary |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 73 | MachineInfo BinaryArch; |
Jordan Rupprecht | 70038e0 | 2019-01-07 16:59:12 +0000 | [diff] [blame] | 74 | // Only applicable when --output-format!=binary (e.g. elf64-x86-64). |
| 75 | Optional<MachineInfo> OutputArch; |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 76 | |
| 77 | // Advanced options |
| 78 | StringRef AddGnuDebugLink; |
Jake Ehrlich | 8ad7779 | 2018-12-03 19:49:23 +0000 | [diff] [blame] | 79 | StringRef BuildIdLinkDir; |
| 80 | Optional<StringRef> BuildIdLinkInput; |
| 81 | Optional<StringRef> BuildIdLinkOutput; |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 82 | StringRef SplitDWO; |
| 83 | StringRef SymbolsPrefix; |
Jordan Rupprecht | d0f7bcf | 2019-01-30 14:58:13 +0000 | [diff] [blame] | 84 | DiscardType DiscardMode = DiscardType::None; |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 85 | |
| 86 | // Repeated options |
| 87 | std::vector<StringRef> AddSection; |
| 88 | std::vector<StringRef> DumpSection; |
Eugene Leviant | f324f6d | 2019-02-06 11:00:07 +0000 | [diff] [blame] | 89 | std::vector<NameOrRegex> KeepSection; |
| 90 | std::vector<NameOrRegex> OnlySection; |
| 91 | std::vector<NameOrRegex> SymbolsToGlobalize; |
| 92 | std::vector<NameOrRegex> SymbolsToKeep; |
| 93 | std::vector<NameOrRegex> SymbolsToLocalize; |
| 94 | std::vector<NameOrRegex> SymbolsToRemove; |
Eugene Leviant | 2db1062 | 2019-02-13 07:34:54 +0000 | [diff] [blame] | 95 | std::vector<NameOrRegex> UnneededSymbolsToRemove; |
Eugene Leviant | f324f6d | 2019-02-06 11:00:07 +0000 | [diff] [blame] | 96 | std::vector<NameOrRegex> SymbolsToWeaken; |
| 97 | std::vector<NameOrRegex> ToRemove; |
| 98 | std::vector<NameOrRegex> SymbolsToKeepGlobal; |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 99 | |
| 100 | // Map options |
| 101 | StringMap<SectionRename> SectionsToRename; |
Jordan Rupprecht | c892741 | 2019-01-29 15:05:38 +0000 | [diff] [blame] | 102 | StringMap<SectionFlagsUpdate> SetSectionFlags; |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 103 | StringMap<StringRef> SymbolsToRename; |
| 104 | |
| 105 | // Boolean options |
Jordan Rupprecht | fc780bb | 2018-11-01 17:36:37 +0000 | [diff] [blame] | 106 | bool DeterministicArchives = true; |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 107 | bool ExtractDWO = false; |
| 108 | bool KeepFileSymbols = false; |
| 109 | bool LocalizeHidden = false; |
| 110 | bool OnlyKeepDebug = false; |
| 111 | bool PreserveDates = false; |
| 112 | bool StripAll = false; |
| 113 | bool StripAllGNU = false; |
| 114 | bool StripDWO = false; |
| 115 | bool StripDebug = false; |
| 116 | bool StripNonAlloc = false; |
| 117 | bool StripSections = false; |
| 118 | bool StripUnneeded = false; |
| 119 | bool Weaken = false; |
| 120 | bool DecompressDebugSections = false; |
| 121 | DebugCompressionType CompressionType = DebugCompressionType::None; |
| 122 | }; |
| 123 | |
| 124 | // Configuration for the overall invocation of this tool. When invoked as |
| 125 | // objcopy, will always contain exactly one CopyConfig. When invoked as strip, |
| 126 | // will contain one or more CopyConfigs. |
| 127 | struct DriverConfig { |
| 128 | SmallVector<CopyConfig, 1> CopyConfigs; |
Jordan Rupprecht | 5745c5f | 2019-02-04 18:38:00 +0000 | [diff] [blame] | 129 | BumpPtrAllocator Alloc; |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 130 | }; |
| 131 | |
| 132 | // ParseObjcopyOptions returns the config and sets the input arguments. If a |
| 133 | // help flag is set then ParseObjcopyOptions will print the help messege and |
| 134 | // exit. |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame^] | 135 | Expected<DriverConfig> parseObjcopyOptions(ArrayRef<const char *> ArgsArr); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 136 | |
| 137 | // ParseStripOptions returns the config and sets the input arguments. If a |
| 138 | // help flag is set then ParseStripOptions will print the help messege and |
| 139 | // exit. |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame^] | 140 | Expected<DriverConfig> parseStripOptions(ArrayRef<const char *> ArgsArr); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 141 | |
| 142 | } // namespace objcopy |
| 143 | } // namespace llvm |
| 144 | |
| 145 | #endif |