2018-10-12 00:33:50 +02:00
|
|
|
//===- CopyConfig.h -------------------------------------------------------===//
|
|
|
|
//
|
2019-01-19 09:50:56 +01:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2018-10-12 00:33:50 +02:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_TOOLS_LLVM_OBJCOPY_COPY_CONFIG_H
|
|
|
|
#define LLVM_TOOLS_LLVM_OBJCOPY_COPY_CONFIG_H
|
|
|
|
|
|
|
|
#include "llvm/ADT/ArrayRef.h"
|
|
|
|
#include "llvm/ADT/Optional.h"
|
|
|
|
#include "llvm/ADT/SmallVector.h"
|
|
|
|
#include "llvm/ADT/StringMap.h"
|
|
|
|
#include "llvm/ADT/StringRef.h"
|
2019-02-25 15:12:41 +01:00
|
|
|
#include "llvm/Object/ELFTypes.h"
|
2019-02-04 19:38:00 +01:00
|
|
|
#include "llvm/Support/Allocator.h"
|
2019-02-21 18:05:19 +01:00
|
|
|
#include "llvm/Support/Error.h"
|
2019-02-06 12:00:07 +01:00
|
|
|
#include "llvm/Support/Regex.h"
|
2018-10-12 00:33:50 +02:00
|
|
|
// Necessary for llvm::DebugCompressionType::None
|
|
|
|
#include "llvm/Target/TargetOptions.h"
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
namespace objcopy {
|
|
|
|
|
|
|
|
// This type keeps track of the machine info for various architectures. This
|
|
|
|
// lets us map architecture names to ELF types and the e_machine value of the
|
|
|
|
// ELF file.
|
|
|
|
struct MachineInfo {
|
|
|
|
uint16_t EMachine;
|
|
|
|
bool Is64Bit;
|
|
|
|
bool IsLittleEndian;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct SectionRename {
|
|
|
|
StringRef OriginalName;
|
|
|
|
StringRef NewName;
|
|
|
|
Optional<uint64_t> NewFlags;
|
|
|
|
};
|
|
|
|
|
[llvm-objcopy] Implement --set-section-flags.
Summary:
--set-section-flags is used to change the section flags (e.g. SHF_ALLOC) for given sections. The flags allowed are the same from the existing --rename-section=.old=.new[,flags] feature.
Additionally, make sure that --set-section-flag cannot be used with --rename-section (either the source or destination), since --rename-section accepts flags. This avoids ambiguity for something like "--rename-section=.foo=.bar,alloc --set-section-flag=.bar,code".
Reviewers: jhenderson, jakehehrlich, alexshap, espindola
Reviewed By: jhenderson, jakehehrlich
Subscribers: llvm-commits, emaste, arichardson
Differential Revision: https://reviews.llvm.org/D57198
llvm-svn: 352505
2019-01-29 16:05:38 +01:00
|
|
|
struct SectionFlagsUpdate {
|
|
|
|
StringRef Name;
|
|
|
|
uint64_t NewFlags;
|
|
|
|
};
|
|
|
|
|
[llvm-objcopy] Support -X|--discard-locals.
Summary:
This adds support for the --discard-locals flag, which acts similarly to --discard-all, except it only applies to compiler-generated symbols (i.e. symbols starting with `.L` in ELF).
I am not sure about COFF local symbols: those appear to also use `.L` in most cases, but also use just `L` in other cases, so for now I am just leaving it unimplemented there.
Fixes PR36160
Reviewers: jhenderson, alexshap, jakehehrlich, mstorsjo, espindola
Reviewed By: jhenderson
Subscribers: llvm-commits, emaste, arichardson
Differential Revision: https://reviews.llvm.org/D57248
llvm-svn: 352626
2019-01-30 15:58:13 +01:00
|
|
|
enum class DiscardType {
|
|
|
|
None, // Default
|
|
|
|
All, // --discard-all (-x)
|
|
|
|
Locals, // --discard-locals (-X)
|
|
|
|
};
|
|
|
|
|
2019-02-06 12:00:07 +01:00
|
|
|
class NameOrRegex {
|
|
|
|
StringRef Name;
|
|
|
|
// Regex is shared between multiple CopyConfig instances.
|
|
|
|
std::shared_ptr<Regex> R;
|
|
|
|
|
|
|
|
public:
|
|
|
|
NameOrRegex(StringRef Pattern, bool IsRegex);
|
|
|
|
bool operator==(StringRef S) const { return R ? R->match(S) : Name == S; }
|
|
|
|
bool operator!=(StringRef S) const { return !operator==(S); }
|
|
|
|
};
|
|
|
|
|
2019-02-25 15:12:41 +01:00
|
|
|
struct NewSymbolInfo {
|
|
|
|
StringRef SymbolName;
|
|
|
|
StringRef SectionName;
|
|
|
|
uint64_t Value = 0;
|
|
|
|
uint8_t Type = ELF::STT_NOTYPE;
|
|
|
|
uint8_t Bind = ELF::STB_GLOBAL;
|
|
|
|
uint8_t Visibility = ELF::STV_DEFAULT;
|
|
|
|
};
|
|
|
|
|
2018-10-12 00:33:50 +02:00
|
|
|
// Configuration for copying/stripping a single file.
|
|
|
|
struct CopyConfig {
|
|
|
|
// Main input/output options
|
|
|
|
StringRef InputFilename;
|
|
|
|
StringRef InputFormat;
|
|
|
|
StringRef OutputFilename;
|
|
|
|
StringRef OutputFormat;
|
|
|
|
|
2019-01-07 17:59:12 +01:00
|
|
|
// Only applicable for --input-format=binary
|
2018-10-12 00:33:50 +02:00
|
|
|
MachineInfo BinaryArch;
|
2019-01-07 17:59:12 +01:00
|
|
|
// Only applicable when --output-format!=binary (e.g. elf64-x86-64).
|
|
|
|
Optional<MachineInfo> OutputArch;
|
2018-10-12 00:33:50 +02:00
|
|
|
|
|
|
|
// Advanced options
|
|
|
|
StringRef AddGnuDebugLink;
|
2018-12-03 20:49:23 +01:00
|
|
|
StringRef BuildIdLinkDir;
|
|
|
|
Optional<StringRef> BuildIdLinkInput;
|
|
|
|
Optional<StringRef> BuildIdLinkOutput;
|
2018-10-12 00:33:50 +02:00
|
|
|
StringRef SplitDWO;
|
|
|
|
StringRef SymbolsPrefix;
|
[llvm-objcopy] Support -X|--discard-locals.
Summary:
This adds support for the --discard-locals flag, which acts similarly to --discard-all, except it only applies to compiler-generated symbols (i.e. symbols starting with `.L` in ELF).
I am not sure about COFF local symbols: those appear to also use `.L` in most cases, but also use just `L` in other cases, so for now I am just leaving it unimplemented there.
Fixes PR36160
Reviewers: jhenderson, alexshap, jakehehrlich, mstorsjo, espindola
Reviewed By: jhenderson
Subscribers: llvm-commits, emaste, arichardson
Differential Revision: https://reviews.llvm.org/D57248
llvm-svn: 352626
2019-01-30 15:58:13 +01:00
|
|
|
DiscardType DiscardMode = DiscardType::None;
|
2018-10-12 00:33:50 +02:00
|
|
|
|
|
|
|
// Repeated options
|
|
|
|
std::vector<StringRef> AddSection;
|
|
|
|
std::vector<StringRef> DumpSection;
|
2019-02-25 15:12:41 +01:00
|
|
|
std::vector<NewSymbolInfo> SymbolsToAdd;
|
2019-02-06 12:00:07 +01:00
|
|
|
std::vector<NameOrRegex> KeepSection;
|
|
|
|
std::vector<NameOrRegex> OnlySection;
|
|
|
|
std::vector<NameOrRegex> SymbolsToGlobalize;
|
|
|
|
std::vector<NameOrRegex> SymbolsToKeep;
|
|
|
|
std::vector<NameOrRegex> SymbolsToLocalize;
|
|
|
|
std::vector<NameOrRegex> SymbolsToRemove;
|
2019-02-13 08:34:54 +01:00
|
|
|
std::vector<NameOrRegex> UnneededSymbolsToRemove;
|
2019-02-06 12:00:07 +01:00
|
|
|
std::vector<NameOrRegex> SymbolsToWeaken;
|
|
|
|
std::vector<NameOrRegex> ToRemove;
|
|
|
|
std::vector<NameOrRegex> SymbolsToKeepGlobal;
|
2018-10-12 00:33:50 +02:00
|
|
|
|
|
|
|
// Map options
|
|
|
|
StringMap<SectionRename> SectionsToRename;
|
[llvm-objcopy] Implement --set-section-flags.
Summary:
--set-section-flags is used to change the section flags (e.g. SHF_ALLOC) for given sections. The flags allowed are the same from the existing --rename-section=.old=.new[,flags] feature.
Additionally, make sure that --set-section-flag cannot be used with --rename-section (either the source or destination), since --rename-section accepts flags. This avoids ambiguity for something like "--rename-section=.foo=.bar,alloc --set-section-flag=.bar,code".
Reviewers: jhenderson, jakehehrlich, alexshap, espindola
Reviewed By: jhenderson, jakehehrlich
Subscribers: llvm-commits, emaste, arichardson
Differential Revision: https://reviews.llvm.org/D57198
llvm-svn: 352505
2019-01-29 16:05:38 +01:00
|
|
|
StringMap<SectionFlagsUpdate> SetSectionFlags;
|
2018-10-12 00:33:50 +02:00
|
|
|
StringMap<StringRef> SymbolsToRename;
|
|
|
|
|
2019-02-26 10:24:22 +01:00
|
|
|
// ELF entry point address expression. The input parameter is an entry point
|
|
|
|
// address in the input ELF file. The entry address in the output file is
|
|
|
|
// calculated with EntryExpr(input_address), when either --set-start or
|
|
|
|
// --change-start is used.
|
|
|
|
std::function<uint64_t(uint64_t)> EntryExpr;
|
|
|
|
|
2018-10-12 00:33:50 +02:00
|
|
|
// Boolean options
|
2018-11-01 18:36:37 +01:00
|
|
|
bool DeterministicArchives = true;
|
2018-10-12 00:33:50 +02:00
|
|
|
bool ExtractDWO = false;
|
|
|
|
bool KeepFileSymbols = false;
|
|
|
|
bool LocalizeHidden = false;
|
|
|
|
bool OnlyKeepDebug = false;
|
|
|
|
bool PreserveDates = false;
|
|
|
|
bool StripAll = false;
|
|
|
|
bool StripAllGNU = false;
|
|
|
|
bool StripDWO = false;
|
|
|
|
bool StripDebug = false;
|
|
|
|
bool StripNonAlloc = false;
|
|
|
|
bool StripSections = false;
|
|
|
|
bool StripUnneeded = false;
|
|
|
|
bool Weaken = false;
|
|
|
|
bool DecompressDebugSections = false;
|
|
|
|
DebugCompressionType CompressionType = DebugCompressionType::None;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Configuration for the overall invocation of this tool. When invoked as
|
|
|
|
// objcopy, will always contain exactly one CopyConfig. When invoked as strip,
|
|
|
|
// will contain one or more CopyConfigs.
|
|
|
|
struct DriverConfig {
|
|
|
|
SmallVector<CopyConfig, 1> CopyConfigs;
|
2019-02-04 19:38:00 +01:00
|
|
|
BumpPtrAllocator Alloc;
|
2018-10-12 00:33:50 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// ParseObjcopyOptions returns the config and sets the input arguments. If a
|
|
|
|
// help flag is set then ParseObjcopyOptions will print the help messege and
|
|
|
|
// exit.
|
2019-02-21 18:05:19 +01:00
|
|
|
Expected<DriverConfig> parseObjcopyOptions(ArrayRef<const char *> ArgsArr);
|
2018-10-12 00:33:50 +02:00
|
|
|
|
|
|
|
// ParseStripOptions returns the config and sets the input arguments. If a
|
|
|
|
// help flag is set then ParseStripOptions will print the help messege and
|
|
|
|
// exit.
|
2019-02-21 18:05:19 +01:00
|
|
|
Expected<DriverConfig> parseStripOptions(ArrayRef<const char *> ArgsArr);
|
2018-10-12 00:33:50 +02:00
|
|
|
|
|
|
|
} // namespace objcopy
|
|
|
|
} // namespace llvm
|
|
|
|
|
|
|
|
#endif
|