Skip to content

Commit b73e690

Browse files
committed
Add llvm-objcopy option --ignore-symbol
1 parent 50ed98f commit b73e690

File tree

5 files changed

+69
-0
lines changed

5 files changed

+69
-0
lines changed

llvm/include/llvm/ObjCopy/ELF/ELFConfig.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#ifndef LLVM_OBJCOPY_ELF_ELFCONFIG_H
1010
#define LLVM_OBJCOPY_ELF_ELFCONFIG_H
1111

12+
#include "llvm/ObjCopy/CommonConfig.h"
1213
#include "llvm/Object/ELFTypes.h"
1314

1415
namespace llvm {
@@ -18,6 +19,8 @@ namespace objcopy {
1819
struct ELFConfig {
1920
uint8_t NewSymbolVisibility = (uint8_t)ELF::STV_DEFAULT;
2021

22+
NameMatcher SymbolsToIgnore;
23+
2124
// ELF entry point address expression. The input parameter is an entry point
2225
// address in the input ELF file. The entry address in the output file is
2326
// calculated with EntryExpr(input_address), when either --set-start or

llvm/lib/ObjCopy/ELF/ELFObjcopy.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,8 @@ static Error updateAndRemoveSymbols(const CommonConfig &Config,
292292
return Error::success();
293293

294294
Obj.SymbolTable->updateSymbols([&](Symbol &Sym) {
295+
if (ELFConfig.SymbolsToIgnore.matches(Sym.Name))
296+
return;
295297
// Common and undefined symbols don't make sense as local symbols, and can
296298
// even cause crashes if we localize those, so skip them.
297299
if (!Sym.isCommon() && Sym.getShndx() != SHN_UNDEF &&
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
2+
# RUN: yaml2obj %s -o %t.o
3+
# RUN: echo 'foo[2-3]' > %t.ignore.regex
4+
5+
# RUN: cp %t.o %t1.o
6+
# RUN: llvm-objcopy %t1.o --localize-hidden --ignore-symbols=%t.ignore.regex --regex
7+
# RUN: llvm-readelf -s %t1.o | FileCheck %s --check-prefix=SYMS
8+
# SYMS-DAG: LOCAL HIDDEN 1 foo1
9+
# SYMS-DAG: GLOBAL HIDDEN 1 foo2
10+
# SYMS-DAG: GLOBAL HIDDEN 1 foo3
11+
12+
# RUN: cp %t.o %t1.o
13+
# RUN: llvm-objcopy %t1.o --localize-hidden --ignore-symbol=foo3
14+
# RUN: llvm-readelf -s %t1.o | FileCheck %s --check-prefix=SYM
15+
# SYM-DAG: LOCAL HIDDEN 1 foo1
16+
# SYM-DAG: LOCAL HIDDEN 1 foo2
17+
# SYM-DAG: GLOBAL HIDDEN 1 foo3
18+
19+
!ELF
20+
FileHeader:
21+
Class: ELFCLASS64
22+
Data: ELFDATA2LSB
23+
Type: ET_REL
24+
Machine: EM_X86_64
25+
Sections:
26+
- Name: .text
27+
Type: SHT_PROGBITS
28+
Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
29+
Symbols:
30+
- Name: foo1
31+
Section: .text
32+
Binding: STB_GLOBAL
33+
Other: [ STV_HIDDEN ]
34+
- Name: foo2
35+
Section: .text
36+
Binding: STB_GLOBAL
37+
Other: [ STV_HIDDEN ]
38+
- Name: foo3
39+
Section: .text
40+
Binding: STB_GLOBAL
41+
Other: [ STV_HIDDEN ]

llvm/tools/llvm-objcopy/ObjcopyOptions.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -960,6 +960,15 @@ objcopy::parseObjcopyOptions(ArrayRef<const char *> RawArgsArr,
960960
addSymbolsFromFile(Config.SymbolsToKeep, DC.Alloc, Arg->getValue(),
961961
SymbolMatchStyle, ErrorCallback))
962962
return std::move(E);
963+
for (auto *Arg : InputArgs.filtered(OBJCOPY_ignore_symbol))
964+
if (Error E = ELFConfig.SymbolsToIgnore.addMatcher(NameOrPattern::create(
965+
Arg->getValue(), SymbolMatchStyle, ErrorCallback)))
966+
return std::move(E);
967+
for (auto *Arg : InputArgs.filtered(OBJCOPY_ignore_symbols))
968+
if (Error E = addSymbolsFromFile(ELFConfig.SymbolsToIgnore, DC.Alloc,
969+
Arg->getValue(), SymbolMatchStyle,
970+
ErrorCallback))
971+
return std::move(E);
963972
for (auto *Arg : InputArgs.filtered(OBJCOPY_add_symbol)) {
964973
Expected<NewSymbolInfo> SymInfo = parseNewSymbolInfo(Arg->getValue());
965974
if (!SymInfo)

llvm/tools/llvm-objcopy/ObjcopyOpts.td

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,20 @@ defm keep_symbols
196196
"be repeated to read symbols from many files">,
197197
MetaVarName<"filename">;
198198

199+
defm ignore_symbol : Eq<"ignore-symbol", "Do not change parameters of symbol <symbol> "
200+
"when executing other options that can change the symbol's "
201+
"name, binding or visibility">,
202+
MetaVarName<"symbol">;
203+
204+
defm ignore_symbols
205+
: Eq<"ignore-symbols",
206+
"Reads a list of symbols from <filename> and runs as if "
207+
"--ignore-symbol=<symbol> is set for each one. <filename> "
208+
"contains one symbol per line and may contain comments beginning with "
209+
"'#'. Leading and trailing whitespace is stripped from each line. May "
210+
"be repeated to read symbols from many files">,
211+
MetaVarName<"filename">;
212+
199213
defm dump_section
200214
: Eq<"dump-section",
201215
"Dump contents of section named <section> into file <file>">,

0 commit comments

Comments
 (0)