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 "ELF/ELFConfig.h"
13 #include "llvm/ADT/ArrayRef.h"
14 #include "llvm/ADT/BitmaskEnum.h"
15 #include "llvm/ADT/Optional.h"
16 #include "llvm/ADT/SmallVector.h"
17 #include "llvm/ADT/StringMap.h"
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/Object/ELFTypes.h"
20 #include "llvm/Support/Allocator.h"
21 #include "llvm/Support/Error.h"
22 #include "llvm/Support/Regex.h"
23 // Necessary for llvm::DebugCompressionType::None
24 #include "llvm/Target/TargetOptions.h"
30 enum class FileFormat
{
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
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) {}
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.
69 SecContents
= 1 << 10,
71 LLVM_MARK_AS_BITMASK_ENUM(/* LargestValue = */ SecShare
)
74 struct SectionRename
{
75 StringRef OriginalName
;
77 Optional
<SectionFlag
> NewFlags
;
80 struct SectionFlagsUpdate
{
85 enum class DiscardType
{
87 All
, // --discard-all (-x)
88 Locals
, // --discard-locals (-X)
93 // Regex is shared between multiple CopyConfig instances.
94 std::shared_ptr
<Regex
> R
;
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
); }
102 // Matcher that checks symbol or section names against the command line flags
103 // provided for that option.
105 std::vector
<NameOrRegex
> Matchers
;
108 void addMatcher(NameOrRegex Matcher
) {
109 Matchers
.push_back(std::move(Matcher
));
111 bool matches(StringRef S
) const { return is_contained(Matchers
, S
); }
112 bool empty() const { return Matchers
.empty(); }
115 // Configuration for copying/stripping a single file.
117 // Format-specific options to be initialized lazily when needed.
118 Optional
<elf::ELFCopyConfig
> ELF
;
120 // Main input/output options
121 StringRef InputFilename
;
122 FileFormat InputFormat
;
123 StringRef OutputFilename
;
124 FileFormat OutputFormat
;
126 // Only applicable when --output-format!=binary (e.g. elf64-x86-64).
127 Optional
<MachineInfo
> OutputArch
;
130 StringRef AddGnuDebugLink
;
131 // Cached gnu_debuglink's target CRC
132 uint32_t GnuDebugLinkCRC32
;
133 StringRef BuildIdLinkDir
;
134 Optional
<StringRef
> BuildIdLinkInput
;
135 Optional
<StringRef
> BuildIdLinkOutput
;
136 Optional
<StringRef
> ExtractPartition
;
138 StringRef SymbolsPrefix
;
139 StringRef AllocSectionsPrefix
;
140 DiscardType DiscardMode
= DiscardType::None
;
141 Optional
<StringRef
> NewSymbolVisibility
;
144 std::vector
<StringRef
> AddSection
;
145 std::vector
<StringRef
> DumpSection
;
146 std::vector
<StringRef
> SymbolsToAdd
;
149 NameMatcher KeepSection
;
150 NameMatcher OnlySection
;
151 NameMatcher ToRemove
;
154 NameMatcher SymbolsToGlobalize
;
155 NameMatcher SymbolsToKeep
;
156 NameMatcher SymbolsToLocalize
;
157 NameMatcher SymbolsToRemove
;
158 NameMatcher UnneededSymbolsToRemove
;
159 NameMatcher SymbolsToWeaken
;
160 NameMatcher SymbolsToKeepGlobal
;
163 StringMap
<SectionRename
> SectionsToRename
;
164 StringMap
<uint64_t> SetSectionAlignment
;
165 StringMap
<SectionFlagsUpdate
> SetSectionFlags
;
166 StringMap
<StringRef
> SymbolsToRename
;
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
;
175 bool AllowBrokenLinks
= false;
176 bool DeterministicArchives
= true;
177 bool ExtractDWO
= false;
178 bool ExtractMainPartition
= false;
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;
191 bool DecompressDebugSections
= false;
192 DebugCompressionType CompressionType
= DebugCompressionType::None
;
194 // parseELFConfig performs ELF-specific command-line parsing. Fills `ELF` on
195 // success or returns an Error otherwise.
196 Error
parseELFConfig() {
198 Expected
<elf::ELFCopyConfig
> ELFConfig
= elf::parseConfig(*this);
200 return ELFConfig
.takeError();
203 return Error::success();
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
;
212 BumpPtrAllocator Alloc
;
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
218 Expected
<DriverConfig
> parseObjcopyOptions(ArrayRef
<const char *> ArgsArr
);
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
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
);
228 } // namespace objcopy