blob: 3fe4ab4f42f924dcbf32e4547e81f5382798fa0b [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 {
33 uint16_t EMachine;
James Hendersonc040d5d2019-03-22 10:21:09 +000034 uint8_t OSABI;
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +000035 bool Is64Bit;
36 bool IsLittleEndian;
37};
38
Jordan Rupprechtbd95a9f2019-03-28 18:27:00 +000039// Flags set by --set-section-flags or --rename-section. Interpretation of these
40// is format-specific and not all flags are meaningful for all object file
41// formats. This is a bitmask; many section flags may be set.
42enum SectionFlag {
43 SecNone = 0,
44 SecAlloc = 1 << 0,
45 SecLoad = 1 << 1,
46 SecNoload = 1 << 2,
47 SecReadonly = 1 << 3,
48 SecDebug = 1 << 4,
49 SecCode = 1 << 5,
50 SecData = 1 << 6,
51 SecRom = 1 << 7,
52 SecMerge = 1 << 8,
53 SecStrings = 1 << 9,
54 SecContents = 1 << 10,
55 SecShare = 1 << 11,
56 LLVM_MARK_AS_BITMASK_ENUM(/* LargestValue = */ SecShare)
57};
58
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +000059struct SectionRename {
60 StringRef OriginalName;
61 StringRef NewName;
Jordan Rupprechtbd95a9f2019-03-28 18:27:00 +000062 Optional<SectionFlag> NewFlags;
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +000063};
64
Jordan Rupprechtc8927412019-01-29 15:05:38 +000065struct SectionFlagsUpdate {
66 StringRef Name;
Jordan Rupprechtbd95a9f2019-03-28 18:27:00 +000067 SectionFlag NewFlags;
Jordan Rupprechtc8927412019-01-29 15:05:38 +000068};
69
Jordan Rupprechtd0f7bcf2019-01-30 14:58:13 +000070enum class DiscardType {
71 None, // Default
72 All, // --discard-all (-x)
73 Locals, // --discard-locals (-X)
74};
75
Eugene Leviantf324f6d2019-02-06 11:00:07 +000076class NameOrRegex {
77 StringRef Name;
78 // Regex is shared between multiple CopyConfig instances.
79 std::shared_ptr<Regex> R;
80
81public:
82 NameOrRegex(StringRef Pattern, bool IsRegex);
83 bool operator==(StringRef S) const { return R ? R->match(S) : Name == S; }
84 bool operator!=(StringRef S) const { return !operator==(S); }
85};
86
Eugene Leviant51c1f642019-02-25 14:12:41 +000087struct NewSymbolInfo {
88 StringRef SymbolName;
89 StringRef SectionName;
90 uint64_t Value = 0;
91 uint8_t Type = ELF::STT_NOTYPE;
92 uint8_t Bind = ELF::STB_GLOBAL;
93 uint8_t Visibility = ELF::STV_DEFAULT;
94};
95
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +000096// Configuration for copying/stripping a single file.
97struct CopyConfig {
98 // Main input/output options
99 StringRef InputFilename;
100 StringRef InputFormat;
101 StringRef OutputFilename;
102 StringRef OutputFormat;
103
Jordan Rupprecht70038e02019-01-07 16:59:12 +0000104 // Only applicable for --input-format=binary
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000105 MachineInfo BinaryArch;
Jordan Rupprecht70038e02019-01-07 16:59:12 +0000106 // Only applicable when --output-format!=binary (e.g. elf64-x86-64).
107 Optional<MachineInfo> OutputArch;
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000108
109 // Advanced options
110 StringRef AddGnuDebugLink;
Jake Ehrlich8ad77792018-12-03 19:49:23 +0000111 StringRef BuildIdLinkDir;
112 Optional<StringRef> BuildIdLinkInput;
113 Optional<StringRef> BuildIdLinkOutput;
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000114 StringRef SplitDWO;
115 StringRef SymbolsPrefix;
Jordan Rupprechtd0f7bcf2019-01-30 14:58:13 +0000116 DiscardType DiscardMode = DiscardType::None;
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000117
118 // Repeated options
119 std::vector<StringRef> AddSection;
120 std::vector<StringRef> DumpSection;
Eugene Leviant51c1f642019-02-25 14:12:41 +0000121 std::vector<NewSymbolInfo> SymbolsToAdd;
Eugene Leviantf324f6d2019-02-06 11:00:07 +0000122 std::vector<NameOrRegex> KeepSection;
123 std::vector<NameOrRegex> OnlySection;
124 std::vector<NameOrRegex> SymbolsToGlobalize;
125 std::vector<NameOrRegex> SymbolsToKeep;
126 std::vector<NameOrRegex> SymbolsToLocalize;
127 std::vector<NameOrRegex> SymbolsToRemove;
Eugene Leviant2db10622019-02-13 07:34:54 +0000128 std::vector<NameOrRegex> UnneededSymbolsToRemove;
Eugene Leviantf324f6d2019-02-06 11:00:07 +0000129 std::vector<NameOrRegex> SymbolsToWeaken;
130 std::vector<NameOrRegex> ToRemove;
131 std::vector<NameOrRegex> SymbolsToKeepGlobal;
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000132
133 // Map options
134 StringMap<SectionRename> SectionsToRename;
Jordan Rupprechtc8927412019-01-29 15:05:38 +0000135 StringMap<SectionFlagsUpdate> SetSectionFlags;
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000136 StringMap<StringRef> SymbolsToRename;
137
Eugene Leviant53350d02019-02-26 09:24:22 +0000138 // ELF entry point address expression. The input parameter is an entry point
139 // address in the input ELF file. The entry address in the output file is
140 // calculated with EntryExpr(input_address), when either --set-start or
141 // --change-start is used.
142 std::function<uint64_t(uint64_t)> EntryExpr;
143
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000144 // Boolean options
Jordan Rupprechtfc780bb2018-11-01 17:36:37 +0000145 bool DeterministicArchives = true;
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000146 bool ExtractDWO = false;
147 bool KeepFileSymbols = false;
148 bool LocalizeHidden = false;
149 bool OnlyKeepDebug = false;
150 bool PreserveDates = false;
151 bool StripAll = false;
152 bool StripAllGNU = false;
153 bool StripDWO = false;
154 bool StripDebug = false;
155 bool StripNonAlloc = false;
156 bool StripSections = false;
157 bool StripUnneeded = false;
158 bool Weaken = false;
159 bool DecompressDebugSections = false;
160 DebugCompressionType CompressionType = DebugCompressionType::None;
161};
162
163// Configuration for the overall invocation of this tool. When invoked as
164// objcopy, will always contain exactly one CopyConfig. When invoked as strip,
165// will contain one or more CopyConfigs.
166struct DriverConfig {
167 SmallVector<CopyConfig, 1> CopyConfigs;
Jordan Rupprecht5745c5f2019-02-04 18:38:00 +0000168 BumpPtrAllocator Alloc;
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000169};
170
171// ParseObjcopyOptions returns the config and sets the input arguments. If a
172// help flag is set then ParseObjcopyOptions will print the help messege and
173// exit.
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000174Expected<DriverConfig> parseObjcopyOptions(ArrayRef<const char *> ArgsArr);
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000175
176// ParseStripOptions returns the config and sets the input arguments. If a
177// help flag is set then ParseStripOptions will print the help messege and
178// exit.
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000179Expected<DriverConfig> parseStripOptions(ArrayRef<const char *> ArgsArr);
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000180
181} // namespace objcopy
182} // namespace llvm
183
184#endif