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" |
Jordan Rupprecht | bd95a9f | 2019-03-28 18:27:00 +0000 | [diff] [blame] | 13 | #include "llvm/ADT/BitmaskEnum.h" |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 14 | #include "llvm/ADT/Optional.h" |
| 15 | #include "llvm/ADT/SmallVector.h" |
| 16 | #include "llvm/ADT/StringMap.h" |
| 17 | #include "llvm/ADT/StringRef.h" |
Eugene Leviant | 51c1f64 | 2019-02-25 14:12:41 +0000 | [diff] [blame] | 18 | #include "llvm/Object/ELFTypes.h" |
Jordan Rupprecht | 5745c5f | 2019-02-04 18:38:00 +0000 | [diff] [blame] | 19 | #include "llvm/Support/Allocator.h" |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 20 | #include "llvm/Support/Error.h" |
Eugene Leviant | f324f6d | 2019-02-06 11:00:07 +0000 | [diff] [blame] | 21 | #include "llvm/Support/Regex.h" |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 22 | // Necessary for llvm::DebugCompressionType::None |
| 23 | #include "llvm/Target/TargetOptions.h" |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 24 | #include <vector> |
| 25 | |
| 26 | namespace llvm { |
| 27 | namespace objcopy { |
| 28 | |
| 29 | // This type keeps track of the machine info for various architectures. This |
| 30 | // lets us map architecture names to ELF types and the e_machine value of the |
| 31 | // ELF file. |
| 32 | struct MachineInfo { |
| 33 | uint16_t EMachine; |
James Henderson | c040d5d | 2019-03-22 10:21:09 +0000 | [diff] [blame] | 34 | uint8_t OSABI; |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 35 | bool Is64Bit; |
| 36 | bool IsLittleEndian; |
| 37 | }; |
| 38 | |
Jordan Rupprecht | bd95a9f | 2019-03-28 18:27:00 +0000 | [diff] [blame] | 39 | // Flags set by --set-section-flags or --rename-section. Interpretation of these |
| 40 | // is format-specific and not all flags are meaningful for all object file |
| 41 | // formats. This is a bitmask; many section flags may be set. |
| 42 | enum SectionFlag { |
| 43 | SecNone = 0, |
| 44 | SecAlloc = 1 << 0, |
| 45 | SecLoad = 1 << 1, |
| 46 | SecNoload = 1 << 2, |
| 47 | SecReadonly = 1 << 3, |
| 48 | SecDebug = 1 << 4, |
| 49 | SecCode = 1 << 5, |
| 50 | SecData = 1 << 6, |
| 51 | SecRom = 1 << 7, |
| 52 | SecMerge = 1 << 8, |
| 53 | SecStrings = 1 << 9, |
| 54 | SecContents = 1 << 10, |
| 55 | SecShare = 1 << 11, |
| 56 | LLVM_MARK_AS_BITMASK_ENUM(/* LargestValue = */ SecShare) |
| 57 | }; |
| 58 | |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 59 | struct SectionRename { |
| 60 | StringRef OriginalName; |
| 61 | StringRef NewName; |
Jordan Rupprecht | bd95a9f | 2019-03-28 18:27:00 +0000 | [diff] [blame] | 62 | Optional<SectionFlag> NewFlags; |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 63 | }; |
| 64 | |
Jordan Rupprecht | c892741 | 2019-01-29 15:05:38 +0000 | [diff] [blame] | 65 | struct SectionFlagsUpdate { |
| 66 | StringRef Name; |
Jordan Rupprecht | bd95a9f | 2019-03-28 18:27:00 +0000 | [diff] [blame] | 67 | SectionFlag NewFlags; |
Jordan Rupprecht | c892741 | 2019-01-29 15:05:38 +0000 | [diff] [blame] | 68 | }; |
| 69 | |
Jordan Rupprecht | d0f7bcf | 2019-01-30 14:58:13 +0000 | [diff] [blame] | 70 | enum class DiscardType { |
| 71 | None, // Default |
| 72 | All, // --discard-all (-x) |
| 73 | Locals, // --discard-locals (-X) |
| 74 | }; |
| 75 | |
Eugene Leviant | f324f6d | 2019-02-06 11:00:07 +0000 | [diff] [blame] | 76 | class NameOrRegex { |
| 77 | StringRef Name; |
| 78 | // Regex is shared between multiple CopyConfig instances. |
| 79 | std::shared_ptr<Regex> R; |
| 80 | |
| 81 | public: |
| 82 | NameOrRegex(StringRef Pattern, bool IsRegex); |
| 83 | bool operator==(StringRef S) const { return R ? R->match(S) : Name == S; } |
| 84 | bool operator!=(StringRef S) const { return !operator==(S); } |
| 85 | }; |
| 86 | |
Eugene Leviant | 51c1f64 | 2019-02-25 14:12:41 +0000 | [diff] [blame] | 87 | struct NewSymbolInfo { |
| 88 | StringRef SymbolName; |
| 89 | StringRef SectionName; |
| 90 | uint64_t Value = 0; |
| 91 | uint8_t Type = ELF::STT_NOTYPE; |
| 92 | uint8_t Bind = ELF::STB_GLOBAL; |
| 93 | uint8_t Visibility = ELF::STV_DEFAULT; |
| 94 | }; |
| 95 | |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 96 | // Configuration for copying/stripping a single file. |
| 97 | struct CopyConfig { |
| 98 | // Main input/output options |
| 99 | StringRef InputFilename; |
| 100 | StringRef InputFormat; |
| 101 | StringRef OutputFilename; |
| 102 | StringRef OutputFormat; |
| 103 | |
Jordan Rupprecht | 70038e0 | 2019-01-07 16:59:12 +0000 | [diff] [blame] | 104 | // Only applicable for --input-format=binary |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 105 | MachineInfo BinaryArch; |
Jordan Rupprecht | 70038e0 | 2019-01-07 16:59:12 +0000 | [diff] [blame] | 106 | // Only applicable when --output-format!=binary (e.g. elf64-x86-64). |
| 107 | Optional<MachineInfo> OutputArch; |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 108 | |
| 109 | // Advanced options |
| 110 | StringRef AddGnuDebugLink; |
Jake Ehrlich | 8ad7779 | 2018-12-03 19:49:23 +0000 | [diff] [blame] | 111 | StringRef BuildIdLinkDir; |
| 112 | Optional<StringRef> BuildIdLinkInput; |
| 113 | Optional<StringRef> BuildIdLinkOutput; |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 114 | StringRef SplitDWO; |
| 115 | StringRef SymbolsPrefix; |
Jordan Rupprecht | d0f7bcf | 2019-01-30 14:58:13 +0000 | [diff] [blame] | 116 | DiscardType DiscardMode = DiscardType::None; |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 117 | |
| 118 | // Repeated options |
| 119 | std::vector<StringRef> AddSection; |
| 120 | std::vector<StringRef> DumpSection; |
Eugene Leviant | 51c1f64 | 2019-02-25 14:12:41 +0000 | [diff] [blame] | 121 | std::vector<NewSymbolInfo> SymbolsToAdd; |
Eugene Leviant | f324f6d | 2019-02-06 11:00:07 +0000 | [diff] [blame] | 122 | std::vector<NameOrRegex> KeepSection; |
| 123 | std::vector<NameOrRegex> OnlySection; |
| 124 | std::vector<NameOrRegex> SymbolsToGlobalize; |
| 125 | std::vector<NameOrRegex> SymbolsToKeep; |
| 126 | std::vector<NameOrRegex> SymbolsToLocalize; |
| 127 | std::vector<NameOrRegex> SymbolsToRemove; |
Eugene Leviant | 2db1062 | 2019-02-13 07:34:54 +0000 | [diff] [blame] | 128 | std::vector<NameOrRegex> UnneededSymbolsToRemove; |
Eugene Leviant | f324f6d | 2019-02-06 11:00:07 +0000 | [diff] [blame] | 129 | std::vector<NameOrRegex> SymbolsToWeaken; |
| 130 | std::vector<NameOrRegex> ToRemove; |
| 131 | std::vector<NameOrRegex> SymbolsToKeepGlobal; |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 132 | |
| 133 | // Map options |
| 134 | StringMap<SectionRename> SectionsToRename; |
Jordan Rupprecht | c892741 | 2019-01-29 15:05:38 +0000 | [diff] [blame] | 135 | StringMap<SectionFlagsUpdate> SetSectionFlags; |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 136 | StringMap<StringRef> SymbolsToRename; |
| 137 | |
Eugene Leviant | 53350d0 | 2019-02-26 09:24:22 +0000 | [diff] [blame] | 138 | // ELF entry point address expression. The input parameter is an entry point |
| 139 | // address in the input ELF file. The entry address in the output file is |
| 140 | // calculated with EntryExpr(input_address), when either --set-start or |
| 141 | // --change-start is used. |
| 142 | std::function<uint64_t(uint64_t)> EntryExpr; |
| 143 | |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 144 | // Boolean options |
Jordan Rupprecht | fc780bb | 2018-11-01 17:36:37 +0000 | [diff] [blame] | 145 | bool DeterministicArchives = true; |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 146 | bool ExtractDWO = false; |
| 147 | bool KeepFileSymbols = false; |
| 148 | bool LocalizeHidden = false; |
| 149 | bool OnlyKeepDebug = false; |
| 150 | bool PreserveDates = false; |
| 151 | bool StripAll = false; |
| 152 | bool StripAllGNU = false; |
| 153 | bool StripDWO = false; |
| 154 | bool StripDebug = false; |
| 155 | bool StripNonAlloc = false; |
| 156 | bool StripSections = false; |
| 157 | bool StripUnneeded = false; |
| 158 | bool Weaken = false; |
| 159 | bool DecompressDebugSections = false; |
| 160 | DebugCompressionType CompressionType = DebugCompressionType::None; |
| 161 | }; |
| 162 | |
| 163 | // Configuration for the overall invocation of this tool. When invoked as |
| 164 | // objcopy, will always contain exactly one CopyConfig. When invoked as strip, |
| 165 | // will contain one or more CopyConfigs. |
| 166 | struct DriverConfig { |
| 167 | SmallVector<CopyConfig, 1> CopyConfigs; |
Jordan Rupprecht | 5745c5f | 2019-02-04 18:38:00 +0000 | [diff] [blame] | 168 | BumpPtrAllocator Alloc; |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 169 | }; |
| 170 | |
| 171 | // ParseObjcopyOptions returns the config and sets the input arguments. If a |
| 172 | // help flag is set then ParseObjcopyOptions will print the help messege and |
| 173 | // exit. |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 174 | Expected<DriverConfig> parseObjcopyOptions(ArrayRef<const char *> ArgsArr); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 175 | |
| 176 | // ParseStripOptions returns the config and sets the input arguments. If a |
| 177 | // help flag is set then ParseStripOptions will print the help messege and |
| 178 | // exit. |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 179 | Expected<DriverConfig> parseStripOptions(ArrayRef<const char *> ArgsArr); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 180 | |
| 181 | } // namespace objcopy |
| 182 | } // namespace llvm |
| 183 | |
| 184 | #endif |