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 | |
Seiya Nuta | c83eefc | 2019-09-24 09:38:23 +0000 | [diff] [blame] | 12 | #include "ELF/ELFConfig.h" |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 13 | #include "llvm/ADT/ArrayRef.h" |
Jordan Rupprecht | bd95a9f | 2019-03-28 18:27:00 +0000 | [diff] [blame] | 14 | #include "llvm/ADT/BitmaskEnum.h" |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/Optional.h" |
| 16 | #include "llvm/ADT/SmallVector.h" |
| 17 | #include "llvm/ADT/StringMap.h" |
| 18 | #include "llvm/ADT/StringRef.h" |
Eugene Leviant | 51c1f64 | 2019-02-25 14:12:41 +0000 | [diff] [blame] | 19 | #include "llvm/Object/ELFTypes.h" |
Jordan Rupprecht | 5745c5f | 2019-02-04 18:38:00 +0000 | [diff] [blame] | 20 | #include "llvm/Support/Allocator.h" |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 21 | #include "llvm/Support/Error.h" |
Eugene Leviant | f324f6d | 2019-02-06 11:00:07 +0000 | [diff] [blame] | 22 | #include "llvm/Support/Regex.h" |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 23 | // Necessary for llvm::DebugCompressionType::None |
| 24 | #include "llvm/Target/TargetOptions.h" |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 25 | #include <vector> |
| 26 | |
| 27 | namespace llvm { |
| 28 | namespace objcopy { |
| 29 | |
Seiya Nuta | ecb60b7 | 2019-07-05 05:28:38 +0000 | [diff] [blame] | 30 | enum class FileFormat { |
| 31 | Unspecified, |
| 32 | ELF, |
| 33 | Binary, |
| 34 | IHex, |
| 35 | }; |
| 36 | |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 37 | // This type keeps track of the machine info for various architectures. This |
| 38 | // lets us map architecture names to ELF types and the e_machine value of the |
| 39 | // ELF file. |
| 40 | struct MachineInfo { |
Jordan Rupprecht | b0b65ca | 2019-04-17 07:42:31 +0000 | [diff] [blame] | 41 | MachineInfo(uint16_t EM, uint8_t ABI, bool Is64, bool IsLittle) |
| 42 | : EMachine(EM), OSABI(ABI), Is64Bit(Is64), IsLittleEndian(IsLittle) {} |
| 43 | // Alternative constructor that defaults to NONE for OSABI. |
| 44 | MachineInfo(uint16_t EM, bool Is64, bool IsLittle) |
| 45 | : MachineInfo(EM, ELF::ELFOSABI_NONE, Is64, IsLittle) {} |
| 46 | // Default constructor for unset fields. |
| 47 | MachineInfo() : MachineInfo(0, 0, false, false) {} |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 48 | uint16_t EMachine; |
James Henderson | c040d5d | 2019-03-22 10:21:09 +0000 | [diff] [blame] | 49 | uint8_t OSABI; |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 50 | bool Is64Bit; |
| 51 | bool IsLittleEndian; |
| 52 | }; |
| 53 | |
Jordan Rupprecht | bd95a9f | 2019-03-28 18:27:00 +0000 | [diff] [blame] | 54 | // Flags set by --set-section-flags or --rename-section. Interpretation of these |
| 55 | // is format-specific and not all flags are meaningful for all object file |
| 56 | // formats. This is a bitmask; many section flags may be set. |
| 57 | enum SectionFlag { |
| 58 | SecNone = 0, |
| 59 | SecAlloc = 1 << 0, |
| 60 | SecLoad = 1 << 1, |
| 61 | SecNoload = 1 << 2, |
| 62 | SecReadonly = 1 << 3, |
| 63 | SecDebug = 1 << 4, |
| 64 | SecCode = 1 << 5, |
| 65 | SecData = 1 << 6, |
| 66 | SecRom = 1 << 7, |
| 67 | SecMerge = 1 << 8, |
| 68 | SecStrings = 1 << 9, |
| 69 | SecContents = 1 << 10, |
| 70 | SecShare = 1 << 11, |
| 71 | LLVM_MARK_AS_BITMASK_ENUM(/* LargestValue = */ SecShare) |
| 72 | }; |
| 73 | |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 74 | struct SectionRename { |
| 75 | StringRef OriginalName; |
| 76 | StringRef NewName; |
Jordan Rupprecht | bd95a9f | 2019-03-28 18:27:00 +0000 | [diff] [blame] | 77 | Optional<SectionFlag> NewFlags; |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 78 | }; |
| 79 | |
Jordan Rupprecht | c892741 | 2019-01-29 15:05:38 +0000 | [diff] [blame] | 80 | struct SectionFlagsUpdate { |
| 81 | StringRef Name; |
Jordan Rupprecht | bd95a9f | 2019-03-28 18:27:00 +0000 | [diff] [blame] | 82 | SectionFlag NewFlags; |
Jordan Rupprecht | c892741 | 2019-01-29 15:05:38 +0000 | [diff] [blame] | 83 | }; |
| 84 | |
Jordan Rupprecht | d0f7bcf | 2019-01-30 14:58:13 +0000 | [diff] [blame] | 85 | enum class DiscardType { |
| 86 | None, // Default |
| 87 | All, // --discard-all (-x) |
| 88 | Locals, // --discard-locals (-X) |
| 89 | }; |
| 90 | |
Eugene Leviant | f324f6d | 2019-02-06 11:00:07 +0000 | [diff] [blame] | 91 | class NameOrRegex { |
| 92 | StringRef Name; |
| 93 | // Regex is shared between multiple CopyConfig instances. |
| 94 | std::shared_ptr<Regex> R; |
| 95 | |
| 96 | public: |
| 97 | NameOrRegex(StringRef Pattern, bool IsRegex); |
| 98 | bool operator==(StringRef S) const { return R ? R->match(S) : Name == S; } |
| 99 | bool operator!=(StringRef S) const { return !operator==(S); } |
| 100 | }; |
| 101 | |
Jordan Rupprecht | 6c6dd6a | 2019-08-22 19:17:50 +0000 | [diff] [blame] | 102 | // Matcher that checks symbol or section names against the command line flags |
| 103 | // provided for that option. |
| 104 | class NameMatcher { |
| 105 | std::vector<NameOrRegex> Matchers; |
| 106 | |
| 107 | public: |
| 108 | void addMatcher(NameOrRegex Matcher) { |
| 109 | Matchers.push_back(std::move(Matcher)); |
| 110 | } |
| 111 | bool matches(StringRef S) const { return is_contained(Matchers, S); } |
| 112 | bool empty() const { return Matchers.empty(); } |
| 113 | }; |
| 114 | |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 115 | // Configuration for copying/stripping a single file. |
| 116 | struct CopyConfig { |
Seiya Nuta | c83eefc | 2019-09-24 09:38:23 +0000 | [diff] [blame] | 117 | // Format-specific options to be initialized lazily when needed. |
| 118 | Optional<elf::ELFCopyConfig> ELF; |
| 119 | |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 120 | // Main input/output options |
| 121 | StringRef InputFilename; |
Seiya Nuta | ecb60b7 | 2019-07-05 05:28:38 +0000 | [diff] [blame] | 122 | FileFormat InputFormat; |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 123 | StringRef OutputFilename; |
Seiya Nuta | ecb60b7 | 2019-07-05 05:28:38 +0000 | [diff] [blame] | 124 | FileFormat OutputFormat; |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 125 | |
Jordan Rupprecht | 70038e0 | 2019-01-07 16:59:12 +0000 | [diff] [blame] | 126 | // Only applicable when --output-format!=binary (e.g. elf64-x86-64). |
| 127 | Optional<MachineInfo> OutputArch; |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 128 | |
| 129 | // Advanced options |
| 130 | StringRef AddGnuDebugLink; |
James Henderson | 9df3883 | 2019-05-14 10:59:04 +0000 | [diff] [blame] | 131 | // Cached gnu_debuglink's target CRC |
| 132 | uint32_t GnuDebugLinkCRC32; |
Jake Ehrlich | 8ad7779 | 2018-12-03 19:49:23 +0000 | [diff] [blame] | 133 | StringRef BuildIdLinkDir; |
| 134 | Optional<StringRef> BuildIdLinkInput; |
| 135 | Optional<StringRef> BuildIdLinkOutput; |
Peter Collingbourne | 8d58a98 | 2019-06-07 17:57:48 +0000 | [diff] [blame] | 136 | Optional<StringRef> ExtractPartition; |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 137 | StringRef SplitDWO; |
| 138 | StringRef SymbolsPrefix; |
James Henderson | fa11fb3 | 2019-05-08 09:49:35 +0000 | [diff] [blame] | 139 | StringRef AllocSectionsPrefix; |
Jordan Rupprecht | d0f7bcf | 2019-01-30 14:58:13 +0000 | [diff] [blame] | 140 | DiscardType DiscardMode = DiscardType::None; |
Seiya Nuta | c83eefc | 2019-09-24 09:38:23 +0000 | [diff] [blame] | 141 | Optional<StringRef> NewSymbolVisibility; |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 142 | |
| 143 | // Repeated options |
| 144 | std::vector<StringRef> AddSection; |
| 145 | std::vector<StringRef> DumpSection; |
Seiya Nuta | c83eefc | 2019-09-24 09:38:23 +0000 | [diff] [blame] | 146 | std::vector<StringRef> SymbolsToAdd; |
Jordan Rupprecht | 6c6dd6a | 2019-08-22 19:17:50 +0000 | [diff] [blame] | 147 | |
| 148 | // Section matchers |
| 149 | NameMatcher KeepSection; |
| 150 | NameMatcher OnlySection; |
| 151 | NameMatcher ToRemove; |
| 152 | |
| 153 | // Symbol matchers |
| 154 | NameMatcher SymbolsToGlobalize; |
| 155 | NameMatcher SymbolsToKeep; |
| 156 | NameMatcher SymbolsToLocalize; |
| 157 | NameMatcher SymbolsToRemove; |
| 158 | NameMatcher UnneededSymbolsToRemove; |
| 159 | NameMatcher SymbolsToWeaken; |
| 160 | NameMatcher SymbolsToKeepGlobal; |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 161 | |
| 162 | // Map options |
| 163 | StringMap<SectionRename> SectionsToRename; |
Fangrui Song | 671fb34 | 2019-10-02 12:41:25 +0000 | [diff] [blame^] | 164 | StringMap<uint64_t> SetSectionAlignment; |
Jordan Rupprecht | c892741 | 2019-01-29 15:05:38 +0000 | [diff] [blame] | 165 | StringMap<SectionFlagsUpdate> SetSectionFlags; |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 166 | StringMap<StringRef> SymbolsToRename; |
| 167 | |
Eugene Leviant | 53350d0 | 2019-02-26 09:24:22 +0000 | [diff] [blame] | 168 | // ELF entry point address expression. The input parameter is an entry point |
| 169 | // address in the input ELF file. The entry address in the output file is |
| 170 | // calculated with EntryExpr(input_address), when either --set-start or |
| 171 | // --change-start is used. |
| 172 | std::function<uint64_t(uint64_t)> EntryExpr; |
| 173 | |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 174 | // Boolean options |
James Henderson | 66a9d0f | 2019-04-18 09:13:30 +0000 | [diff] [blame] | 175 | bool AllowBrokenLinks = false; |
Jordan Rupprecht | fc780bb | 2018-11-01 17:36:37 +0000 | [diff] [blame] | 176 | bool DeterministicArchives = true; |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 177 | bool ExtractDWO = false; |
Peter Collingbourne | 8d58a98 | 2019-06-07 17:57:48 +0000 | [diff] [blame] | 178 | bool ExtractMainPartition = false; |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 179 | bool KeepFileSymbols = false; |
| 180 | bool LocalizeHidden = false; |
| 181 | bool OnlyKeepDebug = false; |
| 182 | bool PreserveDates = false; |
| 183 | bool StripAll = false; |
| 184 | bool StripAllGNU = false; |
| 185 | bool StripDWO = false; |
| 186 | bool StripDebug = false; |
| 187 | bool StripNonAlloc = false; |
| 188 | bool StripSections = false; |
| 189 | bool StripUnneeded = false; |
| 190 | bool Weaken = false; |
| 191 | bool DecompressDebugSections = false; |
| 192 | DebugCompressionType CompressionType = DebugCompressionType::None; |
Seiya Nuta | c83eefc | 2019-09-24 09:38:23 +0000 | [diff] [blame] | 193 | |
| 194 | // parseELFConfig performs ELF-specific command-line parsing. Fills `ELF` on |
| 195 | // success or returns an Error otherwise. |
| 196 | Error parseELFConfig() { |
| 197 | if (!ELF) { |
| 198 | Expected<elf::ELFCopyConfig> ELFConfig = elf::parseConfig(*this); |
| 199 | if (!ELFConfig) |
| 200 | return ELFConfig.takeError(); |
| 201 | ELF = *ELFConfig; |
| 202 | } |
| 203 | return Error::success(); |
| 204 | } |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 205 | }; |
| 206 | |
| 207 | // Configuration for the overall invocation of this tool. When invoked as |
| 208 | // objcopy, will always contain exactly one CopyConfig. When invoked as strip, |
| 209 | // will contain one or more CopyConfigs. |
| 210 | struct DriverConfig { |
| 211 | SmallVector<CopyConfig, 1> CopyConfigs; |
Jordan Rupprecht | 5745c5f | 2019-02-04 18:38:00 +0000 | [diff] [blame] | 212 | BumpPtrAllocator Alloc; |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 213 | }; |
| 214 | |
| 215 | // ParseObjcopyOptions returns the config and sets the input arguments. If a |
| 216 | // help flag is set then ParseObjcopyOptions will print the help messege and |
| 217 | // exit. |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 218 | Expected<DriverConfig> parseObjcopyOptions(ArrayRef<const char *> ArgsArr); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 219 | |
| 220 | // ParseStripOptions returns the config and sets the input arguments. If a |
| 221 | // help flag is set then ParseStripOptions will print the help messege and |
Alex Brachet | 7747700 | 2019-06-18 00:39:10 +0000 | [diff] [blame] | 222 | // exit. ErrorCallback is used to handle recoverable errors. An Error returned |
| 223 | // by the callback aborts the parsing and is then returned by this function. |
| 224 | Expected<DriverConfig> |
| 225 | parseStripOptions(ArrayRef<const char *> ArgsArr, |
| 226 | std::function<Error(Error)> ErrorCallback); |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 227 | |
| 228 | } // namespace objcopy |
| 229 | } // namespace llvm |
| 230 | |
| 231 | #endif |