blob: 06b3efddb5adfb4495afafa6023b64704f3b5368 [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;
James Henderson9df38832019-05-14 10:59:04 +0000118 // Cached gnu_debuglink's target CRC
119 uint32_t GnuDebugLinkCRC32;
Jake Ehrlich8ad77792018-12-03 19:49:23 +0000120 StringRef BuildIdLinkDir;
121 Optional<StringRef> BuildIdLinkInput;
122 Optional<StringRef> BuildIdLinkOutput;
Peter Collingbourne8d58a982019-06-07 17:57:48 +0000123 Optional<StringRef> ExtractPartition;
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000124 StringRef SplitDWO;
125 StringRef SymbolsPrefix;
James Hendersonfa11fb32019-05-08 09:49:35 +0000126 StringRef AllocSectionsPrefix;
Jordan Rupprechtd0f7bcf2019-01-30 14:58:13 +0000127 DiscardType DiscardMode = DiscardType::None;
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000128
129 // Repeated options
130 std::vector<StringRef> AddSection;
131 std::vector<StringRef> DumpSection;
Eugene Leviant51c1f642019-02-25 14:12:41 +0000132 std::vector<NewSymbolInfo> SymbolsToAdd;
Eugene Leviantf324f6d2019-02-06 11:00:07 +0000133 std::vector<NameOrRegex> KeepSection;
134 std::vector<NameOrRegex> OnlySection;
135 std::vector<NameOrRegex> SymbolsToGlobalize;
136 std::vector<NameOrRegex> SymbolsToKeep;
137 std::vector<NameOrRegex> SymbolsToLocalize;
138 std::vector<NameOrRegex> SymbolsToRemove;
Eugene Leviant2db10622019-02-13 07:34:54 +0000139 std::vector<NameOrRegex> UnneededSymbolsToRemove;
Eugene Leviantf324f6d2019-02-06 11:00:07 +0000140 std::vector<NameOrRegex> SymbolsToWeaken;
141 std::vector<NameOrRegex> ToRemove;
142 std::vector<NameOrRegex> SymbolsToKeepGlobal;
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000143
144 // Map options
145 StringMap<SectionRename> SectionsToRename;
Jordan Rupprechtc8927412019-01-29 15:05:38 +0000146 StringMap<SectionFlagsUpdate> SetSectionFlags;
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000147 StringMap<StringRef> SymbolsToRename;
148
Eugene Leviant53350d02019-02-26 09:24:22 +0000149 // ELF entry point address expression. The input parameter is an entry point
150 // address in the input ELF file. The entry address in the output file is
151 // calculated with EntryExpr(input_address), when either --set-start or
152 // --change-start is used.
153 std::function<uint64_t(uint64_t)> EntryExpr;
154
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000155 // Boolean options
James Henderson66a9d0f2019-04-18 09:13:30 +0000156 bool AllowBrokenLinks = false;
Jordan Rupprechtfc780bb2018-11-01 17:36:37 +0000157 bool DeterministicArchives = true;
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000158 bool ExtractDWO = false;
Peter Collingbourne8d58a982019-06-07 17:57:48 +0000159 bool ExtractMainPartition = false;
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000160 bool KeepFileSymbols = false;
161 bool LocalizeHidden = false;
162 bool OnlyKeepDebug = false;
163 bool PreserveDates = false;
164 bool StripAll = false;
165 bool StripAllGNU = false;
166 bool StripDWO = false;
167 bool StripDebug = false;
168 bool StripNonAlloc = false;
169 bool StripSections = false;
170 bool StripUnneeded = false;
171 bool Weaken = false;
172 bool DecompressDebugSections = false;
173 DebugCompressionType CompressionType = DebugCompressionType::None;
174};
175
176// Configuration for the overall invocation of this tool. When invoked as
177// objcopy, will always contain exactly one CopyConfig. When invoked as strip,
178// will contain one or more CopyConfigs.
179struct DriverConfig {
180 SmallVector<CopyConfig, 1> CopyConfigs;
Jordan Rupprecht5745c5f2019-02-04 18:38:00 +0000181 BumpPtrAllocator Alloc;
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000182};
183
184// ParseObjcopyOptions returns the config and sets the input arguments. If a
185// help flag is set then ParseObjcopyOptions will print the help messege and
186// exit.
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000187Expected<DriverConfig> parseObjcopyOptions(ArrayRef<const char *> ArgsArr);
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000188
189// ParseStripOptions returns the config and sets the input arguments. If a
190// help flag is set then ParseStripOptions will print the help messege and
191// exit.
Jordan Rupprechtad29d292019-02-21 17:05:19 +0000192Expected<DriverConfig> parseStripOptions(ArrayRef<const char *> ArgsArr);
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +0000193
194} // namespace objcopy
195} // namespace llvm
196
197#endif