OSDN Git Service

9ae4270f4fe56d50beb5dca8496904f2bbc1777a
[android-x86/external-llvm.git] / tools / llvm-objcopy / CopyConfig.h
1 //===- CopyConfig.h -------------------------------------------------------===//
2 //
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
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/BitmaskEnum.h"
14 #include "llvm/ADT/Optional.h"
15 #include "llvm/ADT/SmallVector.h"
16 #include "llvm/ADT/StringMap.h"
17 #include "llvm/ADT/StringRef.h"
18 #include "llvm/Object/ELFTypes.h"
19 #include "llvm/Support/Allocator.h"
20 #include "llvm/Support/Error.h"
21 #include "llvm/Support/Regex.h"
22 // Necessary for llvm::DebugCompressionType::None
23 #include "llvm/Target/TargetOptions.h"
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   MachineInfo(uint16_t EM, uint8_t ABI, bool Is64, bool IsLittle)
34       : EMachine(EM), OSABI(ABI), Is64Bit(Is64), IsLittleEndian(IsLittle) {}
35   // Alternative constructor that defaults to NONE for OSABI.
36   MachineInfo(uint16_t EM, bool Is64, bool IsLittle)
37       : MachineInfo(EM, ELF::ELFOSABI_NONE, Is64, IsLittle) {}
38   // Default constructor for unset fields.
39   MachineInfo() : MachineInfo(0, 0, false, false) {}
40   uint16_t EMachine;
41   uint8_t OSABI;
42   bool Is64Bit;
43   bool IsLittleEndian;
44 };
45
46 // Flags set by --set-section-flags or --rename-section. Interpretation of these
47 // is format-specific and not all flags are meaningful for all object file
48 // formats. This is a bitmask; many section flags may be set.
49 enum SectionFlag {
50   SecNone = 0,
51   SecAlloc = 1 << 0,
52   SecLoad = 1 << 1,
53   SecNoload = 1 << 2,
54   SecReadonly = 1 << 3,
55   SecDebug = 1 << 4,
56   SecCode = 1 << 5,
57   SecData = 1 << 6,
58   SecRom = 1 << 7,
59   SecMerge = 1 << 8,
60   SecStrings = 1 << 9,
61   SecContents = 1 << 10,
62   SecShare = 1 << 11,
63   LLVM_MARK_AS_BITMASK_ENUM(/* LargestValue = */ SecShare)
64 };
65
66 struct SectionRename {
67   StringRef OriginalName;
68   StringRef NewName;
69   Optional<SectionFlag> NewFlags;
70 };
71
72 struct SectionFlagsUpdate {
73   StringRef Name;
74   SectionFlag NewFlags;
75 };
76
77 enum class DiscardType {
78   None,   // Default
79   All,    // --discard-all (-x)
80   Locals, // --discard-locals (-X)
81 };
82
83 class NameOrRegex {
84   StringRef Name;
85   // Regex is shared between multiple CopyConfig instances.
86   std::shared_ptr<Regex> R;
87
88 public:
89   NameOrRegex(StringRef Pattern, bool IsRegex);
90   bool operator==(StringRef S) const { return R ? R->match(S) : Name == S; }
91   bool operator!=(StringRef S) const { return !operator==(S); }
92 };
93
94 struct NewSymbolInfo {
95   StringRef SymbolName;
96   StringRef SectionName;
97   uint64_t Value = 0;
98   uint8_t Type = ELF::STT_NOTYPE;
99   uint8_t Bind = ELF::STB_GLOBAL;
100   uint8_t Visibility = ELF::STV_DEFAULT;
101 };
102
103 // Configuration for copying/stripping a single file.
104 struct CopyConfig {
105   // Main input/output options
106   StringRef InputFilename;
107   StringRef InputFormat;
108   StringRef OutputFilename;
109   StringRef OutputFormat;
110
111   // Only applicable for --input-format=binary
112   MachineInfo BinaryArch;
113   // Only applicable when --output-format!=binary (e.g. elf64-x86-64).
114   Optional<MachineInfo> OutputArch;
115
116   // Advanced options
117   StringRef AddGnuDebugLink;
118   // Cached gnu_debuglink's target CRC
119   uint32_t GnuDebugLinkCRC32;
120   StringRef BuildIdLinkDir;
121   Optional<StringRef> BuildIdLinkInput;
122   Optional<StringRef> BuildIdLinkOutput;
123   Optional<StringRef> ExtractPartition;
124   StringRef SplitDWO;
125   StringRef SymbolsPrefix;
126   StringRef AllocSectionsPrefix;
127   DiscardType DiscardMode = DiscardType::None;
128
129   // Repeated options
130   std::vector<StringRef> AddSection;
131   std::vector<StringRef> DumpSection;
132   std::vector<NewSymbolInfo> SymbolsToAdd;
133   std::vector<NameOrRegex> KeepSection;
134   std::vector<NameOrRegex> OnlySection;
135   std::vector<NameOrRegex> SymbolsToGlobalize;
136   std::vector<NameOrRegex> SymbolsToKeep;
137   std::vector<NameOrRegex> SymbolsToLocalize;
138   std::vector<NameOrRegex> SymbolsToRemove;
139   std::vector<NameOrRegex> UnneededSymbolsToRemove;
140   std::vector<NameOrRegex> SymbolsToWeaken;
141   std::vector<NameOrRegex> ToRemove;
142   std::vector<NameOrRegex> SymbolsToKeepGlobal;
143
144   // Map options
145   StringMap<SectionRename> SectionsToRename;
146   StringMap<SectionFlagsUpdate> SetSectionFlags;
147   StringMap<StringRef> SymbolsToRename;
148
149   // ELF entry point address expression. The input parameter is an entry point
150   // address in the input ELF file. The entry address in the output file is
151   // calculated with EntryExpr(input_address), when either --set-start or
152   // --change-start is used.
153   std::function<uint64_t(uint64_t)> EntryExpr;
154
155   // Boolean options
156   bool AllowBrokenLinks = false;
157   bool DeterministicArchives = true;
158   bool ExtractDWO = false;
159   bool ExtractMainPartition = false;
160   bool KeepFileSymbols = false;
161   bool LocalizeHidden = false;
162   bool OnlyKeepDebug = false;
163   bool PreserveDates = false;
164   bool StripAll = false;
165   bool StripAllGNU = false;
166   bool StripDWO = false;
167   bool StripDebug = false;
168   bool StripNonAlloc = false;
169   bool StripSections = false;
170   bool StripUnneeded = false;
171   bool Weaken = false;
172   bool DecompressDebugSections = false;
173   DebugCompressionType CompressionType = DebugCompressionType::None;
174 };
175
176 // Configuration for the overall invocation of this tool. When invoked as
177 // objcopy, will always contain exactly one CopyConfig. When invoked as strip,
178 // will contain one or more CopyConfigs.
179 struct DriverConfig {
180   SmallVector<CopyConfig, 1> CopyConfigs;
181   BumpPtrAllocator Alloc;
182 };
183
184 // ParseObjcopyOptions returns the config and sets the input arguments. If a
185 // help flag is set then ParseObjcopyOptions will print the help messege and
186 // exit.
187 Expected<DriverConfig> parseObjcopyOptions(ArrayRef<const char *> ArgsArr);
188
189 // ParseStripOptions returns the config and sets the input arguments. If a
190 // help flag is set then ParseStripOptions will print the help messege and
191 // exit. ErrorCallback is used to handle recoverable errors. An Error returned
192 // by the callback aborts the parsing and is then returned by this function.
193 Expected<DriverConfig>
194 parseStripOptions(ArrayRef<const char *> ArgsArr,
195                   std::function<Error(Error)> ErrorCallback);
196
197 } // namespace objcopy
198 } // namespace llvm
199
200 #endif