blob: f1d5cf2c10cbf011a28827d7eab5b1c3711d4388 [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"
Eugene Leviantf324f6d2019-02-06 11:00:07 +000018#include "llvm/Support/Regex.h"
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +000019// Necessary for llvm::DebugCompressionType::None
20#include "llvm/Target/TargetOptions.h"
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +000021#include <vector>
22
23namespace llvm {
24namespace objcopy {
25
26// This type keeps track of the machine info for various architectures. This
27// lets us map architecture names to ELF types and the e_machine value of the
28// ELF file.
29struct MachineInfo {
30 uint16_t EMachine;
31 bool Is64Bit;
32 bool IsLittleEndian;
33};
34
35struct SectionRename {
36 StringRef OriginalName;
37 StringRef NewName;
38 Optional<uint64_t> NewFlags;
39};
40
Jordan Rupprechtc8927412019-01-29 15:05:38 +000041struct SectionFlagsUpdate {
42 StringRef Name;
43 uint64_t NewFlags;
44};
45
Jordan Rupprechtd0f7bcf2019-01-30 14:58:13 +000046enum class DiscardType {
47 None, // Default
48 All, // --discard-all (-x)
49 Locals, // --discard-locals (-X)
50};
51
Eugene Leviantf324f6d2019-02-06 11:00:07 +000052class NameOrRegex {
53 StringRef Name;
54 // Regex is shared between multiple CopyConfig instances.
55 std::shared_ptr<Regex> R;
56
57public:
58 NameOrRegex(StringRef Pattern, bool IsRegex);
59 bool operator==(StringRef S) const { return R ? R->match(S) : Name == S; }
60 bool operator!=(StringRef S) const { return !operator==(S); }
61};
62
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +000063// Configuration for copying/stripping a single file.
64struct CopyConfig {
65 // Main input/output options
66 StringRef InputFilename;
67 StringRef InputFormat;
68 StringRef OutputFilename;
69 StringRef OutputFormat;
70
Jordan Rupprecht70038e02019-01-07 16:59:12 +000071 // Only applicable for --input-format=binary
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +000072 MachineInfo BinaryArch;
Jordan Rupprecht70038e02019-01-07 16:59:12 +000073 // Only applicable when --output-format!=binary (e.g. elf64-x86-64).
74 Optional<MachineInfo> OutputArch;
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +000075
76 // Advanced options
77 StringRef AddGnuDebugLink;
Jake Ehrlich8ad77792018-12-03 19:49:23 +000078 StringRef BuildIdLinkDir;
79 Optional<StringRef> BuildIdLinkInput;
80 Optional<StringRef> BuildIdLinkOutput;
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +000081 StringRef SplitDWO;
82 StringRef SymbolsPrefix;
Jordan Rupprechtd0f7bcf2019-01-30 14:58:13 +000083 DiscardType DiscardMode = DiscardType::None;
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +000084
85 // Repeated options
86 std::vector<StringRef> AddSection;
87 std::vector<StringRef> DumpSection;
Eugene Leviantf324f6d2019-02-06 11:00:07 +000088 std::vector<NameOrRegex> KeepSection;
89 std::vector<NameOrRegex> OnlySection;
90 std::vector<NameOrRegex> SymbolsToGlobalize;
91 std::vector<NameOrRegex> SymbolsToKeep;
92 std::vector<NameOrRegex> SymbolsToLocalize;
93 std::vector<NameOrRegex> SymbolsToRemove;
Eugene Leviant2db10622019-02-13 07:34:54 +000094 std::vector<NameOrRegex> UnneededSymbolsToRemove;
Eugene Leviantf324f6d2019-02-06 11:00:07 +000095 std::vector<NameOrRegex> SymbolsToWeaken;
96 std::vector<NameOrRegex> ToRemove;
97 std::vector<NameOrRegex> SymbolsToKeepGlobal;
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +000098
99 // Map options
100 StringMap<SectionRename> SectionsToRename;
Jordan Rupprechtc8927412019-01-29 15:05:38 +0000101 StringMap<SectionFlagsUpdate> SetSectionFlags;
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000102 StringMap<StringRef> SymbolsToRename;
103
104 // Boolean options
Jordan Rupprechtfc780bb2018-11-01 17:36:37 +0000105 bool DeterministicArchives = true;
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000106 bool ExtractDWO = false;
107 bool KeepFileSymbols = false;
108 bool LocalizeHidden = false;
109 bool OnlyKeepDebug = false;
110 bool PreserveDates = false;
111 bool StripAll = false;
112 bool StripAllGNU = false;
113 bool StripDWO = false;
114 bool StripDebug = false;
115 bool StripNonAlloc = false;
116 bool StripSections = false;
117 bool StripUnneeded = false;
118 bool Weaken = false;
119 bool DecompressDebugSections = false;
120 DebugCompressionType CompressionType = DebugCompressionType::None;
121};
122
123// Configuration for the overall invocation of this tool. When invoked as
124// objcopy, will always contain exactly one CopyConfig. When invoked as strip,
125// will contain one or more CopyConfigs.
126struct DriverConfig {
127 SmallVector<CopyConfig, 1> CopyConfigs;
Jordan Rupprecht5745c5f2019-02-04 18:38:00 +0000128 BumpPtrAllocator Alloc;
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000129};
130
131// ParseObjcopyOptions returns the config and sets the input arguments. If a
132// help flag is set then ParseObjcopyOptions will print the help messege and
133// exit.
134DriverConfig parseObjcopyOptions(ArrayRef<const char *> ArgsArr);
135
136// ParseStripOptions returns the config and sets the input arguments. If a
137// help flag is set then ParseStripOptions will print the help messege and
138// exit.
139DriverConfig parseStripOptions(ArrayRef<const char *> ArgsArr);
140
141} // namespace objcopy
142} // namespace llvm
143
144#endif