blob: 8c025eb3891f74e246a6b6038770ee01f033357f [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"
Jordan Rupprechtbd95a9f2019-03-28 18:27:00 +000013#include "llvm/ADT/BitmaskEnum.h"
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +000014#include "llvm/ADT/Optional.h"
15#include "llvm/ADT/SmallVector.h"
16#include "llvm/ADT/StringMap.h"
17#include "llvm/ADT/StringRef.h"
Eugene Leviant51c1f642019-02-25 14:12:41 +000018#include "llvm/Object/ELFTypes.h"
Jordan Rupprecht5745c5f2019-02-04 18:38:00 +000019#include "llvm/Support/Allocator.h"
Jordan Rupprechtad29d292019-02-21 17:05:19 +000020#include "llvm/Support/Error.h"
Eugene Leviantf324f6d2019-02-06 11:00:07 +000021#include "llvm/Support/Regex.h"
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +000022// Necessary for llvm::DebugCompressionType::None
23#include "llvm/Target/TargetOptions.h"
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +000024#include <vector>
25
26namespace llvm {
27namespace objcopy {
28
29// This type keeps track of the machine info for various architectures. This
30// lets us map architecture names to ELF types and the e_machine value of the
31// ELF file.
32struct MachineInfo {
Jordan Rupprechtb0b65ca2019-04-17 07:42:31 +000033 MachineInfo(uint16_t EM, uint8_t ABI, bool Is64, bool IsLittle)
34 : EMachine(EM), OSABI(ABI), Is64Bit(Is64), IsLittleEndian(IsLittle) {}
35 // Alternative constructor that defaults to NONE for OSABI.
36 MachineInfo(uint16_t EM, bool Is64, bool IsLittle)
37 : MachineInfo(EM, ELF::ELFOSABI_NONE, Is64, IsLittle) {}
38 // Default constructor for unset fields.
39 MachineInfo() : MachineInfo(0, 0, false, false) {}
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +000040 uint16_t EMachine;
James Hendersonc040d5d2019-03-22 10:21:09 +000041 uint8_t OSABI;
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +000042 bool Is64Bit;
43 bool IsLittleEndian;
44};
45
Jordan Rupprechtbd95a9f2019-03-28 18:27:00 +000046// Flags set by --set-section-flags or --rename-section. Interpretation of these
47// is format-specific and not all flags are meaningful for all object file
48// formats. This is a bitmask; many section flags may be set.
49enum SectionFlag {
50 SecNone = 0,
51 SecAlloc = 1 << 0,
52 SecLoad = 1 << 1,
53 SecNoload = 1 << 2,
54 SecReadonly = 1 << 3,
55 SecDebug = 1 << 4,
56 SecCode = 1 << 5,
57 SecData = 1 << 6,
58 SecRom = 1 << 7,
59 SecMerge = 1 << 8,
60 SecStrings = 1 << 9,
61 SecContents = 1 << 10,
62 SecShare = 1 << 11,
63 LLVM_MARK_AS_BITMASK_ENUM(/* LargestValue = */ SecShare)
64};
65
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +000066struct SectionRename {
67 StringRef OriginalName;
68 StringRef NewName;
Jordan Rupprechtbd95a9f2019-03-28 18:27:00 +000069 Optional<SectionFlag> NewFlags;
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +000070};
71
Jordan Rupprechtc8927412019-01-29 15:05:38 +000072struct SectionFlagsUpdate {
73 StringRef Name;
Jordan Rupprechtbd95a9f2019-03-28 18:27:00 +000074 SectionFlag NewFlags;
Jordan Rupprechtc8927412019-01-29 15:05:38 +000075};
76
Jordan Rupprechtd0f7bcf2019-01-30 14:58:13 +000077enum class DiscardType {
78 None, // Default
79 All, // --discard-all (-x)
80 Locals, // --discard-locals (-X)
81};
82
Eugene Leviantf324f6d2019-02-06 11:00:07 +000083class NameOrRegex {
84 StringRef Name;
85 // Regex is shared between multiple CopyConfig instances.
86 std::shared_ptr<Regex> R;
87
88public:
89 NameOrRegex(StringRef Pattern, bool IsRegex);
90 bool operator==(StringRef S) const { return R ? R->match(S) : Name == S; }
91 bool operator!=(StringRef S) const { return !operator==(S); }
92};
93
Eugene Leviant51c1f642019-02-25 14:12:41 +000094struct NewSymbolInfo {
95 StringRef SymbolName;
96 StringRef SectionName;
97 uint64_t Value = 0;
98 uint8_t Type = ELF::STT_NOTYPE;
99 uint8_t Bind = ELF::STB_GLOBAL;
100 uint8_t Visibility = ELF::STV_DEFAULT;
101};
102
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000103// Configuration for copying/stripping a single file.
104struct CopyConfig {
105 // Main input/output options
106 StringRef InputFilename;
107 StringRef InputFormat;
108 StringRef OutputFilename;
109 StringRef OutputFormat;
110
Jordan Rupprecht70038e02019-01-07 16:59:12 +0000111 // Only applicable for --input-format=binary
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000112 MachineInfo BinaryArch;
Jordan Rupprecht70038e02019-01-07 16:59:12 +0000113 // Only applicable when --output-format!=binary (e.g. elf64-x86-64).
114 Optional<MachineInfo> OutputArch;
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000115
116 // Advanced options
117 StringRef AddGnuDebugLink;
Jake Ehrlich8ad77792018-12-03 19:49:23 +0000118 StringRef BuildIdLinkDir;
119 Optional<StringRef> BuildIdLinkInput;
120 Optional<StringRef> BuildIdLinkOutput;
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000121 StringRef SplitDWO;
122 StringRef SymbolsPrefix;
Jordan Rupprechtd0f7bcf2019-01-30 14:58:13 +0000123 DiscardType DiscardMode = DiscardType::None;
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000124
125 // Repeated options
126 std::vector<StringRef> AddSection;
127 std::vector<StringRef> DumpSection;
Eugene Leviant51c1f642019-02-25 14:12:41 +0000128 std::vector<NewSymbolInfo> SymbolsToAdd;
Eugene Leviantf324f6d2019-02-06 11:00:07 +0000129 std::vector<NameOrRegex> KeepSection;
130 std::vector<NameOrRegex> OnlySection;
131 std::vector<NameOrRegex> SymbolsToGlobalize;
132 std::vector<NameOrRegex> SymbolsToKeep;
133 std::vector<NameOrRegex> SymbolsToLocalize;
134 std::vector<NameOrRegex> SymbolsToRemove;
Eugene Leviant2db10622019-02-13 07:34:54 +0000135 std::vector<NameOrRegex> UnneededSymbolsToRemove;
Eugene Leviantf324f6d2019-02-06 11:00:07 +0000136 std::vector<NameOrRegex> SymbolsToWeaken;
137 std::vector<NameOrRegex> ToRemove;
138 std::vector<NameOrRegex> SymbolsToKeepGlobal;
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000139
140 // Map options
141 StringMap<SectionRename> SectionsToRename;
Jordan Rupprechtc8927412019-01-29 15:05:38 +0000142 StringMap<SectionFlagsUpdate> SetSectionFlags;
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000143 StringMap<StringRef> SymbolsToRename;
144
Eugene Leviant53350d02019-02-26 09:24:22 +0000145 // ELF entry point address expression. The input parameter is an entry point
146 // address in the input ELF file. The entry address in the output file is
147 // calculated with EntryExpr(input_address), when either --set-start or
148 // --change-start is used.
149 std::function<uint64_t(uint64_t)> EntryExpr;
150
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000151 // Boolean options
Jordan Rupprechtfc780bb2018-11-01 17:36:37 +0000152 bool DeterministicArchives = true;
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000153 bool ExtractDWO = false;
154 bool KeepFileSymbols = false;
155 bool LocalizeHidden = false;
156 bool OnlyKeepDebug = false;
157 bool PreserveDates = false;
158 bool StripAll = false;
159 bool StripAllGNU = false;
160 bool StripDWO = false;
161 bool StripDebug = false;
162 bool StripNonAlloc = false;
163 bool StripSections = false;
164 bool StripUnneeded = false;
165 bool Weaken = false;
166 bool DecompressDebugSections = false;
167 DebugCompressionType CompressionType = DebugCompressionType::None;
168};
169
170// Configuration for the overall invocation of this tool. When invoked as
171// objcopy, will always contain exactly one CopyConfig. When invoked as strip,
172// will contain one or more CopyConfigs.
173struct DriverConfig {
174 SmallVector<CopyConfig, 1> CopyConfigs;
Jordan Rupprecht5745c5f2019-02-04 18:38:00 +0000175 BumpPtrAllocator Alloc;
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000176};
177
178// ParseObjcopyOptions returns the config and sets the input arguments. If a
179// help flag is set then ParseObjcopyOptions will print the help messege and
180// exit.
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000181Expected<DriverConfig> parseObjcopyOptions(ArrayRef<const char *> ArgsArr);
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000182
183// ParseStripOptions returns the config and sets the input arguments. If a
184// help flag is set then ParseStripOptions will print the help messege and
185// exit.
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000186Expected<DriverConfig> parseStripOptions(ArrayRef<const char *> ArgsArr);
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000187
188} // namespace objcopy
189} // namespace llvm
190
191#endif