1 //===- CopyConfig.h -------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_TOOLS_LLVM_OBJCOPY_COPY_CONFIG_H
10 #define LLVM_TOOLS_LLVM_OBJCOPY_COPY_CONFIG_H
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"
29 enum class FileFormat
{
36 // This type keeps track of the machine info for various architectures. This
37 // lets us map architecture names to ELF types and the e_machine value of the
40 MachineInfo(uint16_t EM
, uint8_t ABI
, bool Is64
, bool IsLittle
)
41 : EMachine(EM
), OSABI(ABI
), Is64Bit(Is64
), IsLittleEndian(IsLittle
) {}
42 // Alternative constructor that defaults to NONE for OSABI.
43 MachineInfo(uint16_t EM
, bool Is64
, bool IsLittle
)
44 : MachineInfo(EM
, ELF::ELFOSABI_NONE
, Is64
, IsLittle
) {}
45 // Default constructor for unset fields.
46 MachineInfo() : MachineInfo(0, 0, false, false) {}
53 // Flags set by --set-section-flags or --rename-section. Interpretation of these
54 // is format-specific and not all flags are meaningful for all object file
55 // formats. This is a bitmask; many section flags may be set.
68 SecContents
= 1 << 10,
70 LLVM_MARK_AS_BITMASK_ENUM(/* LargestValue = */ SecShare
)
73 struct SectionRename
{
74 StringRef OriginalName
;
76 Optional
<SectionFlag
> NewFlags
;
79 struct SectionFlagsUpdate
{
84 enum class DiscardType
{
86 All
, // --discard-all (-x)
87 Locals
, // --discard-locals (-X)
92 // Regex is shared between multiple CopyConfig instances.
93 std::shared_ptr
<Regex
> R
;
96 NameOrRegex(StringRef Pattern
, bool IsRegex
);
97 bool operator==(StringRef S
) const { return R
? R
->match(S
) : Name
== S
; }
98 bool operator!=(StringRef S
) const { return !operator==(S
); }
101 // Matcher that checks symbol or section names against the command line flags
102 // provided for that option.
104 std::vector
<NameOrRegex
> Matchers
;
107 void addMatcher(NameOrRegex Matcher
) {
108 Matchers
.push_back(std::move(Matcher
));
110 bool matches(StringRef S
) const { return is_contained(Matchers
, S
); }
111 bool empty() const { return Matchers
.empty(); }
114 struct NewSymbolInfo
{
115 StringRef SymbolName
;
116 StringRef SectionName
;
118 uint8_t Type
= ELF::STT_NOTYPE
;
119 uint8_t Bind
= ELF::STB_GLOBAL
;
120 uint8_t Visibility
= ELF::STV_DEFAULT
;
123 // Configuration for copying/stripping a single file.
125 // Main input/output options
126 StringRef InputFilename
;
127 FileFormat InputFormat
;
128 StringRef OutputFilename
;
129 FileFormat OutputFormat
;
131 // Only applicable for --input-format=binary
132 MachineInfo BinaryArch
;
133 // Only applicable when --output-format!=binary (e.g. elf64-x86-64).
134 Optional
<MachineInfo
> OutputArch
;
137 StringRef AddGnuDebugLink
;
138 // Cached gnu_debuglink's target CRC
139 uint32_t GnuDebugLinkCRC32
;
140 StringRef BuildIdLinkDir
;
141 Optional
<StringRef
> BuildIdLinkInput
;
142 Optional
<StringRef
> BuildIdLinkOutput
;
143 Optional
<StringRef
> ExtractPartition
;
145 StringRef SymbolsPrefix
;
146 StringRef AllocSectionsPrefix
;
147 DiscardType DiscardMode
= DiscardType::None
;
148 Optional
<uint8_t> NewSymbolVisibility
;
151 std::vector
<StringRef
> AddSection
;
152 std::vector
<StringRef
> DumpSection
;
153 std::vector
<NewSymbolInfo
> SymbolsToAdd
;
156 NameMatcher KeepSection
;
157 NameMatcher OnlySection
;
158 NameMatcher ToRemove
;
161 NameMatcher SymbolsToGlobalize
;
162 NameMatcher SymbolsToKeep
;
163 NameMatcher SymbolsToLocalize
;
164 NameMatcher SymbolsToRemove
;
165 NameMatcher UnneededSymbolsToRemove
;
166 NameMatcher SymbolsToWeaken
;
167 NameMatcher SymbolsToKeepGlobal
;
170 StringMap
<SectionRename
> SectionsToRename
;
171 StringMap
<SectionFlagsUpdate
> SetSectionFlags
;
172 StringMap
<StringRef
> SymbolsToRename
;
174 // ELF entry point address expression. The input parameter is an entry point
175 // address in the input ELF file. The entry address in the output file is
176 // calculated with EntryExpr(input_address), when either --set-start or
177 // --change-start is used.
178 std::function
<uint64_t(uint64_t)> EntryExpr
;
181 bool AllowBrokenLinks
= false;
182 bool DeterministicArchives
= true;
183 bool ExtractDWO
= false;
184 bool ExtractMainPartition
= false;
185 bool KeepFileSymbols
= false;
186 bool LocalizeHidden
= false;
187 bool OnlyKeepDebug
= false;
188 bool PreserveDates
= false;
189 bool StripAll
= false;
190 bool StripAllGNU
= false;
191 bool StripDWO
= false;
192 bool StripDebug
= false;
193 bool StripNonAlloc
= false;
194 bool StripSections
= false;
195 bool StripUnneeded
= false;
197 bool DecompressDebugSections
= false;
198 DebugCompressionType CompressionType
= DebugCompressionType::None
;
201 // Configuration for the overall invocation of this tool. When invoked as
202 // objcopy, will always contain exactly one CopyConfig. When invoked as strip,
203 // will contain one or more CopyConfigs.
204 struct DriverConfig
{
205 SmallVector
<CopyConfig
, 1> CopyConfigs
;
206 BumpPtrAllocator Alloc
;
209 // ParseObjcopyOptions returns the config and sets the input arguments. If a
210 // help flag is set then ParseObjcopyOptions will print the help messege and
212 Expected
<DriverConfig
> parseObjcopyOptions(ArrayRef
<const char *> ArgsArr
);
214 // ParseStripOptions returns the config and sets the input arguments. If a
215 // help flag is set then ParseStripOptions will print the help messege and
216 // exit. ErrorCallback is used to handle recoverable errors. An Error returned
217 // by the callback aborts the parsing and is then returned by this function.
218 Expected
<DriverConfig
>
219 parseStripOptions(ArrayRef
<const char *> ArgsArr
,
220 std::function
<Error(Error
)> ErrorCallback
);
222 } // namespace objcopy