[llvm] [llvm-objcopy] Add llvm-objcopy option --ignore-symbol (PR #80873)
Ilia Kuklin via llvm-commits
llvm-commits at lists.llvm.org
Mon Feb 12 07:34:14 PST 2024
https://github.com/kuilpd updated https://github.com/llvm/llvm-project/pull/80873
>From b73e69022a7e0bf1ce56f0d9c57e35eb35bba6ad Mon Sep 17 00:00:00 2001
From: Ilia Kuklin <ikuklin at accesssoftek.com>
Date: Tue, 6 Feb 2024 21:19:50 +0500
Subject: [PATCH 1/4] Add llvm-objcopy option --ignore-symbol
---
llvm/include/llvm/ObjCopy/ELF/ELFConfig.h | 3 ++
llvm/lib/ObjCopy/ELF/ELFObjcopy.cpp | 2 +
.../llvm-objcopy/ELF/ignore-symbols.test | 41 +++++++++++++++++++
llvm/tools/llvm-objcopy/ObjcopyOptions.cpp | 9 ++++
llvm/tools/llvm-objcopy/ObjcopyOpts.td | 14 +++++++
5 files changed, 69 insertions(+)
create mode 100644 llvm/test/tools/llvm-objcopy/ELF/ignore-symbols.test
diff --git a/llvm/include/llvm/ObjCopy/ELF/ELFConfig.h b/llvm/include/llvm/ObjCopy/ELF/ELFConfig.h
index d77cb69b159db6..52874ea55414d5 100644
--- a/llvm/include/llvm/ObjCopy/ELF/ELFConfig.h
+++ b/llvm/include/llvm/ObjCopy/ELF/ELFConfig.h
@@ -9,6 +9,7 @@
#ifndef LLVM_OBJCOPY_ELF_ELFCONFIG_H
#define LLVM_OBJCOPY_ELF_ELFCONFIG_H
+#include "llvm/ObjCopy/CommonConfig.h"
#include "llvm/Object/ELFTypes.h"
namespace llvm {
@@ -18,6 +19,8 @@ namespace objcopy {
struct ELFConfig {
uint8_t NewSymbolVisibility = (uint8_t)ELF::STV_DEFAULT;
+ NameMatcher SymbolsToIgnore;
+
// 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
diff --git a/llvm/lib/ObjCopy/ELF/ELFObjcopy.cpp b/llvm/lib/ObjCopy/ELF/ELFObjcopy.cpp
index 1b3a58298ec08a..056afe5560e955 100644
--- a/llvm/lib/ObjCopy/ELF/ELFObjcopy.cpp
+++ b/llvm/lib/ObjCopy/ELF/ELFObjcopy.cpp
@@ -292,6 +292,8 @@ static Error updateAndRemoveSymbols(const CommonConfig &Config,
return Error::success();
Obj.SymbolTable->updateSymbols([&](Symbol &Sym) {
+ if (ELFConfig.SymbolsToIgnore.matches(Sym.Name))
+ return;
// Common and undefined symbols don't make sense as local symbols, and can
// even cause crashes if we localize those, so skip them.
if (!Sym.isCommon() && Sym.getShndx() != SHN_UNDEF &&
diff --git a/llvm/test/tools/llvm-objcopy/ELF/ignore-symbols.test b/llvm/test/tools/llvm-objcopy/ELF/ignore-symbols.test
new file mode 100644
index 00000000000000..13a15b46038ee7
--- /dev/null
+++ b/llvm/test/tools/llvm-objcopy/ELF/ignore-symbols.test
@@ -0,0 +1,41 @@
+
+# RUN: yaml2obj %s -o %t.o
+# RUN: echo 'foo[2-3]' > %t.ignore.regex
+
+# RUN: cp %t.o %t1.o
+# RUN: llvm-objcopy %t1.o --localize-hidden --ignore-symbols=%t.ignore.regex --regex
+# RUN: llvm-readelf -s %t1.o | FileCheck %s --check-prefix=SYMS
+# SYMS-DAG: LOCAL HIDDEN 1 foo1
+# SYMS-DAG: GLOBAL HIDDEN 1 foo2
+# SYMS-DAG: GLOBAL HIDDEN 1 foo3
+
+# RUN: cp %t.o %t1.o
+# RUN: llvm-objcopy %t1.o --localize-hidden --ignore-symbol=foo3
+# RUN: llvm-readelf -s %t1.o | FileCheck %s --check-prefix=SYM
+# SYM-DAG: LOCAL HIDDEN 1 foo1
+# SYM-DAG: LOCAL HIDDEN 1 foo2
+# SYM-DAG: GLOBAL HIDDEN 1 foo3
+
+!ELF
+FileHeader:
+ Class: ELFCLASS64
+ Data: ELFDATA2LSB
+ Type: ET_REL
+ Machine: EM_X86_64
+Sections:
+ - Name: .text
+ Type: SHT_PROGBITS
+ Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
+Symbols:
+ - Name: foo1
+ Section: .text
+ Binding: STB_GLOBAL
+ Other: [ STV_HIDDEN ]
+ - Name: foo2
+ Section: .text
+ Binding: STB_GLOBAL
+ Other: [ STV_HIDDEN ]
+ - Name: foo3
+ Section: .text
+ Binding: STB_GLOBAL
+ Other: [ STV_HIDDEN ]
diff --git a/llvm/tools/llvm-objcopy/ObjcopyOptions.cpp b/llvm/tools/llvm-objcopy/ObjcopyOptions.cpp
index 9a9b631e98bcf7..84ec83db0b5e0f 100644
--- a/llvm/tools/llvm-objcopy/ObjcopyOptions.cpp
+++ b/llvm/tools/llvm-objcopy/ObjcopyOptions.cpp
@@ -960,6 +960,15 @@ objcopy::parseObjcopyOptions(ArrayRef<const char *> RawArgsArr,
addSymbolsFromFile(Config.SymbolsToKeep, DC.Alloc, Arg->getValue(),
SymbolMatchStyle, ErrorCallback))
return std::move(E);
+ for (auto *Arg : InputArgs.filtered(OBJCOPY_ignore_symbol))
+ if (Error E = ELFConfig.SymbolsToIgnore.addMatcher(NameOrPattern::create(
+ Arg->getValue(), SymbolMatchStyle, ErrorCallback)))
+ return std::move(E);
+ for (auto *Arg : InputArgs.filtered(OBJCOPY_ignore_symbols))
+ if (Error E = addSymbolsFromFile(ELFConfig.SymbolsToIgnore, DC.Alloc,
+ Arg->getValue(), SymbolMatchStyle,
+ ErrorCallback))
+ return std::move(E);
for (auto *Arg : InputArgs.filtered(OBJCOPY_add_symbol)) {
Expected<NewSymbolInfo> SymInfo = parseNewSymbolInfo(Arg->getValue());
if (!SymInfo)
diff --git a/llvm/tools/llvm-objcopy/ObjcopyOpts.td b/llvm/tools/llvm-objcopy/ObjcopyOpts.td
index bd041fabbdd7ac..a86d1459acfc59 100644
--- a/llvm/tools/llvm-objcopy/ObjcopyOpts.td
+++ b/llvm/tools/llvm-objcopy/ObjcopyOpts.td
@@ -196,6 +196,20 @@ defm keep_symbols
"be repeated to read symbols from many files">,
MetaVarName<"filename">;
+defm ignore_symbol : Eq<"ignore-symbol", "Do not change parameters of symbol <symbol> "
+ "when executing other options that can change the symbol's "
+ "name, binding or visibility">,
+ MetaVarName<"symbol">;
+
+defm ignore_symbols
+ : Eq<"ignore-symbols",
+ "Reads a list of symbols from <filename> and runs as if "
+ "--ignore-symbol=<symbol> is set for each one. <filename> "
+ "contains one symbol per line and may contain comments beginning with "
+ "'#'. Leading and trailing whitespace is stripped from each line. May "
+ "be repeated to read symbols from many files">,
+ MetaVarName<"filename">;
+
defm dump_section
: Eq<"dump-section",
"Dump contents of section named <section> into file <file>">,
>From 1a78662999ece8667dd3e7334951f82c064d6a73 Mon Sep 17 00:00:00 2001
From: Ilia Kuklin <ikuklin at accesssoftek.com>
Date: Mon, 12 Feb 2024 20:10:24 +0500
Subject: [PATCH 2/4] Adjust and expand ignore-symbols.test
---
.../llvm-objcopy/ELF/ignore-symbols.test | 44 +++++++++++++------
1 file changed, 30 insertions(+), 14 deletions(-)
diff --git a/llvm/test/tools/llvm-objcopy/ELF/ignore-symbols.test b/llvm/test/tools/llvm-objcopy/ELF/ignore-symbols.test
index 13a15b46038ee7..1d3bb81e12f589 100644
--- a/llvm/test/tools/llvm-objcopy/ELF/ignore-symbols.test
+++ b/llvm/test/tools/llvm-objcopy/ELF/ignore-symbols.test
@@ -2,19 +2,36 @@
# RUN: yaml2obj %s -o %t.o
# RUN: echo 'foo[2-3]' > %t.ignore.regex
-# RUN: cp %t.o %t1.o
-# RUN: llvm-objcopy %t1.o --localize-hidden --ignore-symbols=%t.ignore.regex --regex
-# RUN: llvm-readelf -s %t1.o | FileCheck %s --check-prefix=SYMS
-# SYMS-DAG: LOCAL HIDDEN 1 foo1
-# SYMS-DAG: GLOBAL HIDDEN 1 foo2
-# SYMS-DAG: GLOBAL HIDDEN 1 foo3
-
-# RUN: cp %t.o %t1.o
-# RUN: llvm-objcopy %t1.o --localize-hidden --ignore-symbol=foo3
-# RUN: llvm-readelf -s %t1.o | FileCheck %s --check-prefix=SYM
-# SYM-DAG: LOCAL HIDDEN 1 foo1
-# SYM-DAG: LOCAL HIDDEN 1 foo2
-# SYM-DAG: GLOBAL HIDDEN 1 foo3
+# Check --ignore-symbols functionality when changing symbol bindings
+# RUN: llvm-objcopy %t.o %t1.o --localize-hidden --ignore-symbols=%t.ignore.regex --regex
+# RUN: llvm-readelf -s %t1.o | FileCheck %s --check-prefix=LH-SYMS
+# LH-SYMS-DAG: LOCAL HIDDEN 1 foo1
+# LH-SYMS-DAG: GLOBAL HIDDEN 1 foo2
+# LH-SYMS-DAG: GLOBAL HIDDEN 1 foo3
+
+# Check --ignore-symbol functionality when changing symbol bindings
+# RUN: llvm-objcopy %t.o %t2.o --localize-hidden --ignore-symbol=foo3
+# RUN: llvm-readelf -s %t2.o | FileCheck %s --check-prefix=LH-SYM
+# LH-SYM-DAG: LOCAL HIDDEN 1 foo1
+# LH-SYM-DAG: LOCAL HIDDEN 1 foo2
+# LH-SYM-DAG: GLOBAL HIDDEN 1 foo3
+
+
+# Check --ignore-symbols functionality when changing symbol names
+# RUN: echo -e "foo1 bar1\nfoo2 bar2" > %t.renames.list
+# RUN: llvm-objcopy %t.o %t3.o --redefine-syms=%t.renames.list --ignore-symbols=%t.ignore.regex --regex
+# RUN: llvm-readelf -s %t3.o | FileCheck %s --check-prefix=RS-SYMS
+# RS-SYMS-DAG: bar1
+# RS-SYMS-DAG: foo2
+# RS-SYMS-DAG: foo3
+
+# Check --ignore-symbols functionality when changing symbol names
+# RUN: llvm-objcopy %t.o %t4.o --redefine-sym=foo1=bar1 --ignore-symbol=fo.* --regex
+# RUN: llvm-readelf -s %t4.o | FileCheck %s --check-prefix=RS-SYM
+# RS-SYM-DAG: foo1
+# RS-SYM-DAG: foo2
+# RS-SYM-DAG: foo3
+
!ELF
FileHeader:
@@ -25,7 +42,6 @@ FileHeader:
Sections:
- Name: .text
Type: SHT_PROGBITS
- Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
Symbols:
- Name: foo1
Section: .text
>From da233c7325b65301642cef2427abfee0cef25d3a Mon Sep 17 00:00:00 2001
From: Ilia Kuklin <ikuklin at accesssoftek.com>
Date: Mon, 12 Feb 2024 20:30:22 +0500
Subject: [PATCH 3/4] Update the llvm-objcopy documentation
---
llvm/docs/CommandGuide/llvm-objcopy.rst | 12 ++++++++++++
1 file changed, 12 insertions(+)
diff --git a/llvm/docs/CommandGuide/llvm-objcopy.rst b/llvm/docs/CommandGuide/llvm-objcopy.rst
index b823be9e828249..3d16c36f235f0e 100644
--- a/llvm/docs/CommandGuide/llvm-objcopy.rst
+++ b/llvm/docs/CommandGuide/llvm-objcopy.rst
@@ -346,6 +346,18 @@ them.
symbol, with leading and trailing whitespace ignored, as is anything following
a '#'. Can be specified multiple times to read names from multiple files.
+.. option:: --ignore-symbol <symbol>
+
+ Do not change parameters of symbol <symbol> when executing other options that
+ can change the symbol's name, binding or visibility
+
+.. option:: --ignore-symbols <filename>
+
+ Reads a list of symbols from <filename> and runs as if --ignore-symbol=<symbol>
+ is set for each one. <filename> contains one symbol per line and may contain
+ comments beginning with '#'. Leading and trailing whitespace is stripped from
+ each line. May be repeated to read symbols from many files.
+
.. option:: --input-target <format>, -I
Read the input as the specified format. See `SUPPORTED FORMATS`_ for a list of
>From c94c1f68152a7f939ae1dd74b58f9de09c3773be Mon Sep 17 00:00:00 2001
From: Ilia Kuklin <ikuklin at accesssoftek.com>
Date: Mon, 12 Feb 2024 20:31:58 +0500
Subject: [PATCH 4/4] Add a test for checking various error cases
---
.../ELF/ignore-symbols-check-errors.test | 19 +++++++++++++++++++
1 file changed, 19 insertions(+)
create mode 100644 llvm/test/tools/llvm-objcopy/ELF/ignore-symbols-check-errors.test
diff --git a/llvm/test/tools/llvm-objcopy/ELF/ignore-symbols-check-errors.test b/llvm/test/tools/llvm-objcopy/ELF/ignore-symbols-check-errors.test
new file mode 100644
index 00000000000000..a92fecb88b3b07
--- /dev/null
+++ b/llvm/test/tools/llvm-objcopy/ELF/ignore-symbols-check-errors.test
@@ -0,0 +1,19 @@
+
+# RUN: yaml2obj %s -o %t.o
+
+# Check if using an invalid symbol pattern generates an error
+# RUN: echo '*.' > %t.symbols.regex
+# RUN: not llvm-objcopy %t.o --ignore-symbols=%t.symbols.regex --regex 2>&1 | FileCheck %s --check-prefix=SYMBOL
+# RUN: not llvm-objcopy %t.o --ignore-symbol=*. --regex 2>&1 | FileCheck %s --check-prefix=SYMBOL
+# SYMBOL: error: cannot compile regular expression '*.': repetition-operator operand invalid
+
+# Check passing an invalid filename generates an error
+# RUN: not llvm-objcopy %t.o --ignore-symbols=no_file 2>&1 | FileCheck %s --check-prefix=FILE
+# FILE: error: 'no_file': No such file or directory
+
+!ELF
+FileHeader:
+ Class: ELFCLASS64
+ Data: ELFDATA2LSB
+ Type: ET_REL
+ Machine: EM_X86_64
More information about the llvm-commits
mailing list