CopyConfig.h revision 353358
1139749Simp//===- CopyConfig.h -------------------------------------------------------===//
2116491Sharti//
3116491Sharti// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4116491Sharti// See https://llvm.org/LICENSE.txt for license information.
5116491Sharti// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6116491Sharti//
7116491Sharti//===----------------------------------------------------------------------===//
8116491Sharti
9116491Sharti#ifndef LLVM_TOOLS_LLVM_OBJCOPY_COPY_CONFIG_H
10116491Sharti#define LLVM_TOOLS_LLVM_OBJCOPY_COPY_CONFIG_H
11116491Sharti
12116491Sharti#include "llvm/ADT/ArrayRef.h"
13116491Sharti#include "llvm/ADT/BitmaskEnum.h"
14116491Sharti#include "llvm/ADT/Optional.h"
15116491Sharti#include "llvm/ADT/SmallVector.h"
16116491Sharti#include "llvm/ADT/StringMap.h"
17116491Sharti#include "llvm/ADT/StringRef.h"
18116491Sharti#include "llvm/Object/ELFTypes.h"
19116491Sharti#include "llvm/Support/Allocator.h"
20116491Sharti#include "llvm/Support/Error.h"
21116491Sharti#include "llvm/Support/Regex.h"
22116491Sharti// Necessary for llvm::DebugCompressionType::None
23116491Sharti#include "llvm/Target/TargetOptions.h"
24116491Sharti#include <vector>
25116491Sharti
26116491Shartinamespace llvm {
27116491Shartinamespace objcopy {
28116491Sharti
29116491Shartienum class FileFormat {
30116491Sharti  Unspecified,
31116491Sharti  ELF,
32116491Sharti  Binary,
33116491Sharti  IHex,
34116519Sharti};
35116519Sharti
36116519Sharti// This type keeps track of the machine info for various architectures. This
37116519Sharti// lets us map architecture names to ELF types and the e_machine value of the
38116491Sharti// ELF file.
39116491Shartistruct MachineInfo {
40116491Sharti  MachineInfo(uint16_t EM, uint8_t ABI, bool Is64, bool IsLittle)
41116491Sharti      : EMachine(EM), OSABI(ABI), Is64Bit(Is64), IsLittleEndian(IsLittle) {}
42116491Sharti  // Alternative constructor that defaults to NONE for OSABI.
43116491Sharti  MachineInfo(uint16_t EM, bool Is64, bool IsLittle)
44116491Sharti      : MachineInfo(EM, ELF::ELFOSABI_NONE, Is64, IsLittle) {}
45116491Sharti  // Default constructor for unset fields.
46116491Sharti  MachineInfo() : MachineInfo(0, 0, false, false) {}
47116491Sharti  uint16_t EMachine;
48116491Sharti  uint8_t OSABI;
49116491Sharti  bool Is64Bit;
50116491Sharti  bool IsLittleEndian;
51116491Sharti};
52116491Sharti
53116491Sharti// Flags set by --set-section-flags or --rename-section. Interpretation of these
54116491Sharti// is format-specific and not all flags are meaningful for all object file
55116491Sharti// formats. This is a bitmask; many section flags may be set.
56116491Shartienum SectionFlag {
57116491Sharti  SecNone = 0,
58116491Sharti  SecAlloc = 1 << 0,
59116491Sharti  SecLoad = 1 << 1,
60116491Sharti  SecNoload = 1 << 2,
61116491Sharti  SecReadonly = 1 << 3,
62116491Sharti  SecDebug = 1 << 4,
63116491Sharti  SecCode = 1 << 5,
64116491Sharti  SecData = 1 << 6,
65147256Sbrooks  SecRom = 1 << 7,
66116491Sharti  SecMerge = 1 << 8,
67116491Sharti  SecStrings = 1 << 9,
68116491Sharti  SecContents = 1 << 10,
69116491Sharti  SecShare = 1 << 11,
70116491Sharti  LLVM_MARK_AS_BITMASK_ENUM(/* LargestValue = */ SecShare)
71116491Sharti};
72116491Sharti
73116491Shartistruct SectionRename {
74116491Sharti  StringRef OriginalName;
75116491Sharti  StringRef NewName;
76116491Sharti  Optional<SectionFlag> NewFlags;
77119280Simp};
78119280Simp
79116491Shartistruct SectionFlagsUpdate {
80116491Sharti  StringRef Name;
81116491Sharti  SectionFlag NewFlags;
82116491Sharti};
83116491Sharti
84116491Shartienum class DiscardType {
85116491Sharti  None,   // Default
86116491Sharti  All,    // --discard-all (-x)
87116491Sharti  Locals, // --discard-locals (-X)
88116491Sharti};
89116491Sharti
90116491Sharticlass NameOrRegex {
91116491Sharti  StringRef Name;
92116491Sharti  // Regex is shared between multiple CopyConfig instances.
93116491Sharti  std::shared_ptr<Regex> R;
94116491Sharti
95116491Shartipublic:
96116491Sharti  NameOrRegex(StringRef Pattern, bool IsRegex);
97116491Sharti  bool operator==(StringRef S) const { return R ? R->match(S) : Name == S; }
98116491Sharti  bool operator!=(StringRef S) const { return !operator==(S); }
99116491Sharti};
100116491Sharti
101116491Shartistruct NewSymbolInfo {
102116491Sharti  StringRef SymbolName;
103116491Sharti  StringRef SectionName;
104116491Sharti  uint64_t Value = 0;
105116491Sharti  uint8_t Type = ELF::STT_NOTYPE;
106116491Sharti  uint8_t Bind = ELF::STB_GLOBAL;
107116491Sharti  uint8_t Visibility = ELF::STV_DEFAULT;
108116491Sharti};
109116491Sharti
110116491Sharti// Configuration for copying/stripping a single file.
111116491Shartistruct CopyConfig {
112116491Sharti  // Main input/output options
113116491Sharti  StringRef InputFilename;
114116491Sharti  FileFormat InputFormat;
115116491Sharti  StringRef OutputFilename;
116116491Sharti  FileFormat OutputFormat;
117116491Sharti
118116491Sharti  // Only applicable for --input-format=binary
119116491Sharti  MachineInfo BinaryArch;
120116491Sharti  // Only applicable when --output-format!=binary (e.g. elf64-x86-64).
121116491Sharti  Optional<MachineInfo> OutputArch;
122116491Sharti
123116491Sharti  // Advanced options
124116491Sharti  StringRef AddGnuDebugLink;
125116491Sharti  // Cached gnu_debuglink's target CRC
126116491Sharti  uint32_t GnuDebugLinkCRC32;
127116491Sharti  StringRef BuildIdLinkDir;
128116491Sharti  Optional<StringRef> BuildIdLinkInput;
129116491Sharti  Optional<StringRef> BuildIdLinkOutput;
130116491Sharti  Optional<StringRef> ExtractPartition;
131116491Sharti  StringRef SplitDWO;
132116491Sharti  StringRef SymbolsPrefix;
133116491Sharti  StringRef AllocSectionsPrefix;
134116491Sharti  DiscardType DiscardMode = DiscardType::None;
135116491Sharti
136116491Sharti  // Repeated options
137116491Sharti  std::vector<StringRef> AddSection;
138116491Sharti  std::vector<StringRef> DumpSection;
139116491Sharti  std::vector<NewSymbolInfo> SymbolsToAdd;
140116491Sharti  std::vector<NameOrRegex> KeepSection;
141116491Sharti  std::vector<NameOrRegex> OnlySection;
142116491Sharti  std::vector<NameOrRegex> SymbolsToGlobalize;
143116491Sharti  std::vector<NameOrRegex> SymbolsToKeep;
144116491Sharti  std::vector<NameOrRegex> SymbolsToLocalize;
145116491Sharti  std::vector<NameOrRegex> SymbolsToRemove;
146116491Sharti  std::vector<NameOrRegex> UnneededSymbolsToRemove;
147116491Sharti  std::vector<NameOrRegex> SymbolsToWeaken;
148116491Sharti  std::vector<NameOrRegex> ToRemove;
149116491Sharti  std::vector<NameOrRegex> SymbolsToKeepGlobal;
150116491Sharti
151116491Sharti  // Map options
152116491Sharti  StringMap<SectionRename> SectionsToRename;
153116491Sharti  StringMap<SectionFlagsUpdate> SetSectionFlags;
154116491Sharti  StringMap<StringRef> SymbolsToRename;
155116491Sharti
156116491Sharti  // ELF entry point address expression. The input parameter is an entry point
157116491Sharti  // address in the input ELF file. The entry address in the output file is
158116491Sharti  // calculated with EntryExpr(input_address), when either --set-start or
159116491Sharti  // --change-start is used.
160116491Sharti  std::function<uint64_t(uint64_t)> EntryExpr;
161116491Sharti
162116491Sharti  // Boolean options
163116491Sharti  bool AllowBrokenLinks = false;
164116491Sharti  bool DeterministicArchives = true;
165116491Sharti  bool ExtractDWO = false;
166116491Sharti  bool ExtractMainPartition = false;
167116491Sharti  bool KeepFileSymbols = false;
168116491Sharti  bool LocalizeHidden = false;
169116491Sharti  bool OnlyKeepDebug = false;
170116491Sharti  bool PreserveDates = false;
171116491Sharti  bool StripAll = false;
172116491Sharti  bool StripAllGNU = false;
173116491Sharti  bool StripDWO = false;
174116491Sharti  bool StripDebug = false;
175116491Sharti  bool StripNonAlloc = false;
176116491Sharti  bool StripSections = false;
177116491Sharti  bool StripUnneeded = false;
178116491Sharti  bool Weaken = false;
179143161Simp  bool DecompressDebugSections = false;
180116491Sharti  DebugCompressionType CompressionType = DebugCompressionType::None;
181116491Sharti};
182116491Sharti
183116491Sharti// Configuration for the overall invocation of this tool. When invoked as
184116491Sharti// objcopy, will always contain exactly one CopyConfig. When invoked as strip,
185116491Sharti// will contain one or more CopyConfigs.
186116491Shartistruct DriverConfig {
187116491Sharti  SmallVector<CopyConfig, 1> CopyConfigs;
188116491Sharti  BumpPtrAllocator Alloc;
189116491Sharti};
190116491Sharti
191116491Sharti// ParseObjcopyOptions returns the config and sets the input arguments. If a
192116491Sharti// help flag is set then ParseObjcopyOptions will print the help messege and
193116491Sharti// exit.
194116491ShartiExpected<DriverConfig> parseObjcopyOptions(ArrayRef<const char *> ArgsArr);
195116491Sharti
196116491Sharti// ParseStripOptions returns the config and sets the input arguments. If a
197116491Sharti// help flag is set then ParseStripOptions will print the help messege and
198116491Sharti// exit. ErrorCallback is used to handle recoverable errors. An Error returned
199116491Sharti// by the callback aborts the parsing and is then returned by this function.
200116491ShartiExpected<DriverConfig>
201116491ShartiparseStripOptions(ArrayRef<const char *> ArgsArr,
202116491Sharti                  std::function<Error(Error)> ErrorCallback);
203116491Sharti
204116491Sharti} // namespace objcopy
205116491Sharti} // namespace llvm
206116491Sharti
207116491Sharti#endif
208116491Sharti