blob: 7dc60bcc843502ac7052e5174a0536c25e9c20e1 [file] [log] [blame]
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +00001//===- CopyConfig.h -------------------------------------------------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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 Shaposhnikov8d0b74c2018-10-11 22:33:50 +00006//
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/Optional.h"
14#include "llvm/ADT/SmallVector.h"
15#include "llvm/ADT/StringMap.h"
16#include "llvm/ADT/StringRef.h"
Jordan Rupprecht5745c5f2019-02-04 18:38:00 +000017#include "llvm/Support/Allocator.h"
Jordan Rupprechtad29d292019-02-21 17:05:19 +000018#include "llvm/Support/Error.h"
Eugene Leviantf324f6d2019-02-06 11:00:07 +000019#include "llvm/Support/Regex.h"
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +000020// Necessary for llvm::DebugCompressionType::None
21#include "llvm/Target/TargetOptions.h"
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +000022#include <vector>
23
24namespace llvm {
25namespace objcopy {
26
27// This type keeps track of the machine info for various architectures. This
28// lets us map architecture names to ELF types and the e_machine value of the
29// ELF file.
30struct MachineInfo {
31 uint16_t EMachine;
32 bool Is64Bit;
33 bool IsLittleEndian;
34};
35
36struct SectionRename {
37 StringRef OriginalName;
38 StringRef NewName;
39 Optional<uint64_t> NewFlags;
40};
41
Jordan Rupprechtc8927412019-01-29 15:05:38 +000042struct SectionFlagsUpdate {
43 StringRef Name;
44 uint64_t NewFlags;
45};
46
Jordan Rupprechtd0f7bcf2019-01-30 14:58:13 +000047enum class DiscardType {
48 None, // Default
49 All, // --discard-all (-x)
50 Locals, // --discard-locals (-X)
51};
52
Eugene Leviantf324f6d2019-02-06 11:00:07 +000053class NameOrRegex {
54 StringRef Name;
55 // Regex is shared between multiple CopyConfig instances.
56 std::shared_ptr<Regex> R;
57
58public:
59 NameOrRegex(StringRef Pattern, bool IsRegex);
60 bool operator==(StringRef S) const { return R ? R->match(S) : Name == S; }
61 bool operator!=(StringRef S) const { return !operator==(S); }
62};
63
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +000064// Configuration for copying/stripping a single file.
65struct CopyConfig {
66 // Main input/output options
67 StringRef InputFilename;
68 StringRef InputFormat;
69 StringRef OutputFilename;
70 StringRef OutputFormat;
71
Jordan Rupprecht70038e02019-01-07 16:59:12 +000072 // Only applicable for --input-format=binary
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +000073 MachineInfo BinaryArch;
Jordan Rupprecht70038e02019-01-07 16:59:12 +000074 // Only applicable when --output-format!=binary (e.g. elf64-x86-64).
75 Optional<MachineInfo> OutputArch;
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +000076
77 // Advanced options
78 StringRef AddGnuDebugLink;
Jake Ehrlich8ad77792018-12-03 19:49:23 +000079 StringRef BuildIdLinkDir;
80 Optional<StringRef> BuildIdLinkInput;
81 Optional<StringRef> BuildIdLinkOutput;
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +000082 StringRef SplitDWO;
83 StringRef SymbolsPrefix;
Jordan Rupprechtd0f7bcf2019-01-30 14:58:13 +000084 DiscardType DiscardMode = DiscardType::None;
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +000085
86 // Repeated options
87 std::vector<StringRef> AddSection;
88 std::vector<StringRef> DumpSection;
Eugene Leviantf324f6d2019-02-06 11:00:07 +000089 std::vector<NameOrRegex> KeepSection;
90 std::vector<NameOrRegex> OnlySection;
91 std::vector<NameOrRegex> SymbolsToGlobalize;
92 std::vector<NameOrRegex> SymbolsToKeep;
93 std::vector<NameOrRegex> SymbolsToLocalize;
94 std::vector<NameOrRegex> SymbolsToRemove;
Eugene Leviant2db10622019-02-13 07:34:54 +000095 std::vector<NameOrRegex> UnneededSymbolsToRemove;
Eugene Leviantf324f6d2019-02-06 11:00:07 +000096 std::vector<NameOrRegex> SymbolsToWeaken;
97 std::vector<NameOrRegex> ToRemove;
98 std::vector<NameOrRegex> SymbolsToKeepGlobal;
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +000099
100 // Map options
101 StringMap<SectionRename> SectionsToRename;
Jordan Rupprechtc8927412019-01-29 15:05:38 +0000102 StringMap<SectionFlagsUpdate> SetSectionFlags;
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000103 StringMap<StringRef> SymbolsToRename;
104
105 // Boolean options
Jordan Rupprechtfc780bb2018-11-01 17:36:37 +0000106 bool DeterministicArchives = true;
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000107 bool ExtractDWO = false;
108 bool KeepFileSymbols = false;
109 bool LocalizeHidden = false;
110 bool OnlyKeepDebug = false;
111 bool PreserveDates = false;
112 bool StripAll = false;
113 bool StripAllGNU = false;
114 bool StripDWO = false;
115 bool StripDebug = false;
116 bool StripNonAlloc = false;
117 bool StripSections = false;
118 bool StripUnneeded = false;
119 bool Weaken = false;
120 bool DecompressDebugSections = false;
121 DebugCompressionType CompressionType = DebugCompressionType::None;
122};
123
124// Configuration for the overall invocation of this tool. When invoked as
125// objcopy, will always contain exactly one CopyConfig. When invoked as strip,
126// will contain one or more CopyConfigs.
127struct DriverConfig {
128 SmallVector<CopyConfig, 1> CopyConfigs;
Jordan Rupprecht5745c5f2019-02-04 18:38:00 +0000129 BumpPtrAllocator Alloc;
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000130};
131
132// ParseObjcopyOptions returns the config and sets the input arguments. If a
133// help flag is set then ParseObjcopyOptions will print the help messege and
134// exit.
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000135Expected<DriverConfig> parseObjcopyOptions(ArrayRef<const char *> ArgsArr);
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000136
137// ParseStripOptions returns the config and sets the input arguments. If a
138// help flag is set then ParseStripOptions will print the help messege and
139// exit.
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000140Expected<DriverConfig> parseStripOptions(ArrayRef<const char *> ArgsArr);
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000141
142} // namespace objcopy
143} // namespace llvm
144
145#endif