[llvm] [llvm-objcopy] Add llvm-objcopy option --set-visibility-sym (PR #80872)
Ilia Kuklin via llvm-commits
llvm-commits at lists.llvm.org
Thu Feb 8 09:31:54 PST 2024
https://github.com/kuilpd updated https://github.com/llvm/llvm-project/pull/80872
>From af5f188a7c4d16f1c9d7c2d8d4785d5a24e9ef00 Mon Sep 17 00:00:00 2001
From: Ilia Kuklin <ikuklin at accesssoftek.com>
Date: Tue, 6 Feb 2024 20:59:40 +0500
Subject: [PATCH 1/9] Add llvm-objcopy option --set-visibility-sym
---
llvm/include/llvm/ObjCopy/ELF/ELFConfig.h | 4 +
llvm/lib/ObjCopy/ELF/ELFObjcopy.cpp | 3 +
.../llvm-objcopy/ELF/set-visibility.test | 139 ++++++++++++++++++
llvm/tools/llvm-objcopy/ObjcopyOptions.cpp | 43 ++++++
llvm/tools/llvm-objcopy/ObjcopyOpts.td | 11 ++
5 files changed, 200 insertions(+)
create mode 100644 llvm/test/tools/llvm-objcopy/ELF/set-visibility.test
diff --git a/llvm/include/llvm/ObjCopy/ELF/ELFConfig.h b/llvm/include/llvm/ObjCopy/ELF/ELFConfig.h
index d77cb69b159db6..b3efa3205e3766 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,9 @@ namespace objcopy {
struct ELFConfig {
uint8_t NewSymbolVisibility = (uint8_t)ELF::STV_DEFAULT;
+ NameMatcher SymbolsToSetVisibility;
+ uint8_t SetVisibilityType = ELF::STV_DEFAULT;
+
// 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 b6d77d17bae36c..b768b7c5d05d7b 100644
--- a/llvm/lib/ObjCopy/ELF/ELFObjcopy.cpp
+++ b/llvm/lib/ObjCopy/ELF/ELFObjcopy.cpp
@@ -290,6 +290,9 @@ static Error updateAndRemoveSymbols(const CommonConfig &Config,
return Error::success();
Obj.SymbolTable->updateSymbols([&](Symbol &Sym) {
+ if (ELFConfig.SymbolsToSetVisibility.matches(Sym.Name) &&
+ Sym.getShndx() != SHN_UNDEF)
+ Sym.Visibility = ELFConfig.SetVisibilityType;
// 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/set-visibility.test b/llvm/test/tools/llvm-objcopy/ELF/set-visibility.test
new file mode 100644
index 00000000000000..bc1c92021c4fe3
--- /dev/null
+++ b/llvm/test/tools/llvm-objcopy/ELF/set-visibility.test
@@ -0,0 +1,139 @@
+
+# RUN: yaml2obj %s -o %t.o
+# RUN: echo '.*' > %t.symbols.regex
+
+# RUN: cp %t.o %t0.o
+# RUN: llvm-objcopy %t0.o --set-visibility-syms=%t.symbols.regex=default --regex
+# RUN: llvm-readelf -s %t0.o | FileCheck %s --check-prefix=DEF
+# DEF-DAG: LOCAL DEFAULT 1 default_local
+# DEF-DAG: LOCAL DEFAULT 1 internal_local
+# DEF-DAG: LOCAL DEFAULT 1 hidden_local
+# DEF-DAG: LOCAL DEFAULT 1 protected_local
+# DEF-DAG: GLOBAL DEFAULT 1 default_global
+# DEF-DAG: WEAK DEFAULT 1 default_weak
+# DEF-DAG: GLOBAL DEFAULT 1 internal_global
+# DEF-DAG: WEAK DEFAULT 1 internal_weak
+# DEF-DAG: GLOBAL DEFAULT 1 hidden_global
+# DEF-DAG: WEAK DEFAULT 1 hidden_weak
+# DEF-DAG: GLOBAL DEFAULT 1 protected_global
+# DEF-DAG: WEAK DEFAULT 1 protected_weak
+
+# RUN: cp %t.o %t0.o
+# RUN: llvm-objcopy %t0.o --set-visibility-syms=%t.symbols.regex=hidden --regex
+# RUN: llvm-readelf -s %t0.o | FileCheck %s --check-prefix=HID
+# HID-DAG: LOCAL HIDDEN 1 default_local
+# HID-DAG: LOCAL HIDDEN 1 internal_local
+# HID-DAG: LOCAL HIDDEN 1 hidden_local
+# HID-DAG: LOCAL HIDDEN 1 protected_local
+# HID-DAG: GLOBAL HIDDEN 1 default_global
+# HID-DAG: WEAK HIDDEN 1 default_weak
+# HID-DAG: GLOBAL HIDDEN 1 internal_global
+# HID-DAG: WEAK HIDDEN 1 internal_weak
+# HID-DAG: GLOBAL HIDDEN 1 hidden_global
+# HID-DAG: WEAK HIDDEN 1 hidden_weak
+# HID-DAG: GLOBAL HIDDEN 1 protected_global
+# HID-DAG: WEAK HIDDEN 1 protected_weak
+
+# RUN: cp %t.o %t0.o
+# RUN: llvm-objcopy %t0.o --set-visibility-syms=%t.symbols.regex=protected --regex
+# RUN: llvm-readelf -s %t0.o | FileCheck %s --check-prefix=PRO
+# PRO-DAG: LOCAL PROTECTED 1 default_local
+# PRO-DAG: LOCAL PROTECTED 1 internal_local
+# PRO-DAG: LOCAL PROTECTED 1 hidden_local
+# PRO-DAG: LOCAL PROTECTED 1 protected_local
+# PRO-DAG: GLOBAL PROTECTED 1 default_global
+# PRO-DAG: WEAK PROTECTED 1 default_weak
+# PRO-DAG: GLOBAL PROTECTED 1 internal_global
+# PRO-DAG: WEAK PROTECTED 1 internal_weak
+# PRO-DAG: GLOBAL PROTECTED 1 hidden_global
+# PRO-DAG: WEAK PROTECTED 1 hidden_weak
+# PRO-DAG: GLOBAL PROTECTED 1 protected_global
+# PRO-DAG: WEAK PROTECTED 1 protected_weak
+
+# RUN: cp %t.o %t0.o
+# RUN: llvm-objcopy %t0.o --set-visibility-syms=%t.symbols.regex=internal --regex
+# RUN: llvm-readelf -s %t0.o | FileCheck %s --check-prefix=INT
+# INT-DAG: LOCAL INTERNAL 1 default_local
+# INT-DAG: LOCAL INTERNAL 1 internal_local
+# INT-DAG: LOCAL INTERNAL 1 hidden_local
+# INT-DAG: LOCAL INTERNAL 1 protected_local
+# INT-DAG: GLOBAL INTERNAL 1 default_global
+# INT-DAG: WEAK INTERNAL 1 default_weak
+# INT-DAG: GLOBAL INTERNAL 1 internal_global
+# INT-DAG: WEAK INTERNAL 1 internal_weak
+# INT-DAG: GLOBAL INTERNAL 1 hidden_global
+# INT-DAG: WEAK INTERNAL 1 hidden_weak
+# INT-DAG: GLOBAL INTERNAL 1 protected_global
+# INT-DAG: WEAK INTERNAL 1 protected_weak
+
+# RUN: cp %t.o %t0.o
+# RUN: llvm-objcopy %t0.o --set-visibility-sym=hidden_global=default
+# RUN: llvm-objcopy %t0.o --set-visibility-sym=default_global=hidden
+# RUN: llvm-objcopy %t0.o --set-visibility-sym=default_local=protected
+# RUN: llvm-objcopy %t0.o --set-visibility-sym=protected_global=internal
+# RUN: llvm-readelf -s %t0.o | FileCheck %s --check-prefix=SYM
+# SYM-DAG: LOCAL PROTECTED 1 default_local
+# SYM-DAG: GLOBAL HIDDEN 1 default_global
+# SYM-DAG: GLOBAL DEFAULT 1 hidden_global
+# SYM-DAG: GLOBAL INTERNAL 1 protected_global
+
+!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: default_local
+ Section: .text
+ Binding: STB_LOCAL
+ - Name: default_global
+ Section: .text
+ Binding: STB_GLOBAL
+ - Name: default_weak
+ Section: .text
+ Binding: STB_WEAK
+ - Name: internal_local
+ Section: .text
+ Binding: STB_LOCAL
+ Other: [ STV_INTERNAL ]
+ - Name: internal_global
+ Section: .text
+ Binding: STB_GLOBAL
+ Other: [ STV_INTERNAL ]
+ - Name: internal_weak
+ Section: .text
+ Binding: STB_WEAK
+ Other: [ STV_INTERNAL ]
+ - Name: hidden_local
+ Section: .text
+ Binding: STB_LOCAL
+ Other: [ STV_HIDDEN ]
+ - Name: hidden_global
+ Section: .text
+ Binding: STB_GLOBAL
+ Other: [ STV_HIDDEN ]
+ - Name: hidden_weak
+ Section: .text
+ Binding: STB_WEAK
+ Other: [ STV_HIDDEN ]
+ - Name: protected_local
+ Section: .text
+ Binding: STB_LOCAL
+ Other: [ STV_PROTECTED ]
+ - Name: protected_global
+ Section: .text
+ Binding: STB_GLOBAL
+ Other: [ STV_PROTECTED ]
+ - Name: protected_weak
+ Section: .text
+ Binding: STB_WEAK
+ Other: [ STV_PROTECTED ]
+ - Name: ignored_name
+ Section: .text
+ Binding: STB_GLOBAL
+ Other: [ STV_INTERNAL ]
diff --git a/llvm/tools/llvm-objcopy/ObjcopyOptions.cpp b/llvm/tools/llvm-objcopy/ObjcopyOptions.cpp
index f15307181fad61..9fb7d3997039e6 100644
--- a/llvm/tools/llvm-objcopy/ObjcopyOptions.cpp
+++ b/llvm/tools/llvm-objcopy/ObjcopyOptions.cpp
@@ -254,6 +254,21 @@ parseSetSectionFlagValue(StringRef FlagValue) {
return SFU;
}
+static Expected<uint8_t> parseVisibilityType(StringRef VisType) {
+ const uint8_t Invalid = 0xff;
+ uint8_t type = StringSwitch<uint8_t>(VisType)
+ .Case("default", ELF::STV_DEFAULT)
+ .Case("hidden", ELF::STV_HIDDEN)
+ .Case("internal", ELF::STV_INTERNAL)
+ .Case("protected", ELF::STV_PROTECTED)
+ .Default(Invalid);
+ if (type == Invalid)
+ return createStringError(errc::invalid_argument,
+ "'%s' is not a valid symbol visibility",
+ VisType.str().c_str());
+ return type;
+}
+
namespace {
struct TargetInfo {
FileFormat Format;
@@ -962,6 +977,34 @@ objcopy::parseObjcopyOptions(ArrayRef<const char *> RawArgsArr,
Config.SymbolsToAdd.push_back(*SymInfo);
}
+ for (auto *Arg : InputArgs.filtered(OBJCOPY_set_visibility_sym)) {
+ if (!StringRef(Arg->getValue()).contains('='))
+ return createStringError(errc::invalid_argument,
+ "bad format for --set-visibility-sym");
+ auto SymAndVis = StringRef(Arg->getValue()).split('=');
+ Expected<uint8_t> Type = parseVisibilityType(SymAndVis.second);
+ if (!Type)
+ return Type.takeError();
+ ELFConfig.SetVisibilityType = Type.get();
+ if (Error E =
+ ELFConfig.SymbolsToSetVisibility.addMatcher(NameOrPattern::create(
+ SymAndVis.first, SymbolMatchStyle, ErrorCallback)))
+ return std::move(E);
+ }
+ for (auto *Arg : InputArgs.filtered(OBJCOPY_set_visibility_syms)) {
+ if (!StringRef(Arg->getValue()).contains('='))
+ return createStringError(errc::invalid_argument,
+ "bad format for --set-visibility-syms");
+ auto FileAndVis = StringRef(Arg->getValue()).split('=');
+ Expected<uint8_t> Type = parseVisibilityType(FileAndVis.second);
+ if (!Type)
+ return Type.takeError();
+ ELFConfig.SetVisibilityType = Type.get();
+ if (Error E = addSymbolsFromFile(ELFConfig.SymbolsToSetVisibility, DC.Alloc,
+ FileAndVis.first, SymbolMatchStyle,
+ ErrorCallback))
+ return std::move(E);
+ }
ELFConfig.AllowBrokenLinks = InputArgs.hasArg(OBJCOPY_allow_broken_links);
diff --git a/llvm/tools/llvm-objcopy/ObjcopyOpts.td b/llvm/tools/llvm-objcopy/ObjcopyOpts.td
index ead8cd28d38779..d872316df5190f 100644
--- a/llvm/tools/llvm-objcopy/ObjcopyOpts.td
+++ b/llvm/tools/llvm-objcopy/ObjcopyOpts.td
@@ -93,6 +93,17 @@ defm set_section_type
"Set the type of section <section> to the integer <type>">,
MetaVarName<"section=type">;
+defm set_visibility_sym
+ : Eq<"set-visibility-sym",
+ "Change the visibility of a symbol to the specified type">,
+ MetaVarName<"symbol=visibility_type">;
+defm set_visibility_syms
+ : Eq<"set-visibility-syms",
+ "Reads a list of symbols from <filename> and changes their "
+ "visibility to the specified type. Visibility types: default, "
+ "internal, hidden, protected">,
+ MetaVarName<"filename=visibility_type">;
+
def S : Flag<["-"], "S">,
Alias<strip_all>,
HelpText<"Alias for --strip-all">;
>From cc05728dd3d0b05cffdcc2e1e194b9898d30666f Mon Sep 17 00:00:00 2001
From: Ilia Kuklin <ikuklin at accesssoftek.com>
Date: Wed, 7 Feb 2024 00:19:24 +0500
Subject: [PATCH 2/9] Change --set-visibility-sym test check order and variable
---
llvm/test/tools/llvm-objcopy/ELF/set-visibility.test | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/llvm/test/tools/llvm-objcopy/ELF/set-visibility.test b/llvm/test/tools/llvm-objcopy/ELF/set-visibility.test
index bc1c92021c4fe3..008981b0e06c77 100644
--- a/llvm/test/tools/llvm-objcopy/ELF/set-visibility.test
+++ b/llvm/test/tools/llvm-objcopy/ELF/set-visibility.test
@@ -69,12 +69,12 @@
# RUN: cp %t.o %t0.o
# RUN: llvm-objcopy %t0.o --set-visibility-sym=hidden_global=default
# RUN: llvm-objcopy %t0.o --set-visibility-sym=default_global=hidden
-# RUN: llvm-objcopy %t0.o --set-visibility-sym=default_local=protected
+# RUN: llvm-objcopy %t0.o --set-visibility-sym=internal_global=protected
# RUN: llvm-objcopy %t0.o --set-visibility-sym=protected_global=internal
# RUN: llvm-readelf -s %t0.o | FileCheck %s --check-prefix=SYM
-# SYM-DAG: LOCAL PROTECTED 1 default_local
-# SYM-DAG: GLOBAL HIDDEN 1 default_global
# SYM-DAG: GLOBAL DEFAULT 1 hidden_global
+# SYM-DAG: GLOBAL HIDDEN 1 default_global
+# SYM-DAG: GLOBAL PROTECTED 1 internal_global
# SYM-DAG: GLOBAL INTERNAL 1 protected_global
!ELF
>From 97b1e2d5236d6c23da52199e4f78035d7c2515c5 Mon Sep 17 00:00:00 2001
From: Ilia Kuklin <ikuklin at accesssoftek.com>
Date: Thu, 8 Feb 2024 22:23:18 +0500
Subject: [PATCH 3/9] Move setting the visibility to after localizing hidden
symbols
---
llvm/lib/ObjCopy/ELF/ELFObjcopy.cpp | 7 ++++---
1 file changed, 4 insertions(+), 3 deletions(-)
diff --git a/llvm/lib/ObjCopy/ELF/ELFObjcopy.cpp b/llvm/lib/ObjCopy/ELF/ELFObjcopy.cpp
index b768b7c5d05d7b..500306c5b70476 100644
--- a/llvm/lib/ObjCopy/ELF/ELFObjcopy.cpp
+++ b/llvm/lib/ObjCopy/ELF/ELFObjcopy.cpp
@@ -290,9 +290,6 @@ static Error updateAndRemoveSymbols(const CommonConfig &Config,
return Error::success();
Obj.SymbolTable->updateSymbols([&](Symbol &Sym) {
- if (ELFConfig.SymbolsToSetVisibility.matches(Sym.Name) &&
- Sym.getShndx() != SHN_UNDEF)
- Sym.Visibility = ELFConfig.SetVisibilityType;
// 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 &&
@@ -301,6 +298,10 @@ static Error updateAndRemoveSymbols(const CommonConfig &Config,
Config.SymbolsToLocalize.matches(Sym.Name)))
Sym.Binding = STB_LOCAL;
+ if (ELFConfig.SymbolsToSetVisibility.matches(Sym.Name) &&
+ Sym.getShndx() != SHN_UNDEF)
+ Sym.Visibility = ELFConfig.SetVisibilityType;
+
// Note: these two globalize flags have very similar names but different
// meanings:
//
>From d3104d156d5afbfdfbf08cba3389e450b6eed250 Mon Sep 17 00:00:00 2001
From: Ilia Kuklin <ikuklin at accesssoftek.com>
Date: Thu, 8 Feb 2024 22:25:56 +0500
Subject: [PATCH 4/9] Change code formatting in when parsing the options
---
llvm/tools/llvm-objcopy/ObjcopyOptions.cpp | 16 ++++++++--------
1 file changed, 8 insertions(+), 8 deletions(-)
diff --git a/llvm/tools/llvm-objcopy/ObjcopyOptions.cpp b/llvm/tools/llvm-objcopy/ObjcopyOptions.cpp
index 9fb7d3997039e6..9d995c79d22b30 100644
--- a/llvm/tools/llvm-objcopy/ObjcopyOptions.cpp
+++ b/llvm/tools/llvm-objcopy/ObjcopyOptions.cpp
@@ -981,27 +981,27 @@ objcopy::parseObjcopyOptions(ArrayRef<const char *> RawArgsArr,
if (!StringRef(Arg->getValue()).contains('='))
return createStringError(errc::invalid_argument,
"bad format for --set-visibility-sym");
- auto SymAndVis = StringRef(Arg->getValue()).split('=');
- Expected<uint8_t> Type = parseVisibilityType(SymAndVis.second);
+ auto [Sym, Visibility] = StringRef(Arg->getValue()).split('=');
+ Expected<uint8_t> Type = parseVisibilityType(Visibility);
if (!Type)
return Type.takeError();
- ELFConfig.SetVisibilityType = Type.get();
+ ELFConfig.SetVisibilityType = *Type;
if (Error E =
ELFConfig.SymbolsToSetVisibility.addMatcher(NameOrPattern::create(
- SymAndVis.first, SymbolMatchStyle, ErrorCallback)))
+ Sym, SymbolMatchStyle, ErrorCallback)))
return std::move(E);
}
for (auto *Arg : InputArgs.filtered(OBJCOPY_set_visibility_syms)) {
if (!StringRef(Arg->getValue()).contains('='))
return createStringError(errc::invalid_argument,
"bad format for --set-visibility-syms");
- auto FileAndVis = StringRef(Arg->getValue()).split('=');
- Expected<uint8_t> Type = parseVisibilityType(FileAndVis.second);
+ auto [File, Visibility] = StringRef(Arg->getValue()).split('=');
+ Expected<uint8_t> Type = parseVisibilityType(Visibility);
if (!Type)
return Type.takeError();
- ELFConfig.SetVisibilityType = Type.get();
+ ELFConfig.SetVisibilityType = *Type;
if (Error E = addSymbolsFromFile(ELFConfig.SymbolsToSetVisibility, DC.Alloc,
- FileAndVis.first, SymbolMatchStyle,
+ File, SymbolMatchStyle,
ErrorCallback))
return std::move(E);
}
>From 524b13f284a91c565bfcb1d262134148d37b2836 Mon Sep 17 00:00:00 2001
From: Ilia Kuklin <ikuklin at accesssoftek.com>
Date: Thu, 8 Feb 2024 22:26:54 +0500
Subject: [PATCH 5/9] Add checks for multiple uses of setting visibility
---
llvm/tools/llvm-objcopy/ObjcopyOptions.cpp | 6 ++++++
1 file changed, 6 insertions(+)
diff --git a/llvm/tools/llvm-objcopy/ObjcopyOptions.cpp b/llvm/tools/llvm-objcopy/ObjcopyOptions.cpp
index 9d995c79d22b30..a6f8c8cf7b48ef 100644
--- a/llvm/tools/llvm-objcopy/ObjcopyOptions.cpp
+++ b/llvm/tools/llvm-objcopy/ObjcopyOptions.cpp
@@ -978,6 +978,9 @@ objcopy::parseObjcopyOptions(ArrayRef<const char *> RawArgsArr,
Config.SymbolsToAdd.push_back(*SymInfo);
}
for (auto *Arg : InputArgs.filtered(OBJCOPY_set_visibility_sym)) {
+ if (!ELFConfig.SymbolsToSetVisibility.empty())
+ return createStringError(errc::invalid_argument,
+ "multiple specifications of visibility type");
if (!StringRef(Arg->getValue()).contains('='))
return createStringError(errc::invalid_argument,
"bad format for --set-visibility-sym");
@@ -992,6 +995,9 @@ objcopy::parseObjcopyOptions(ArrayRef<const char *> RawArgsArr,
return std::move(E);
}
for (auto *Arg : InputArgs.filtered(OBJCOPY_set_visibility_syms)) {
+ if (!ELFConfig.SymbolsToSetVisibility.empty())
+ return createStringError(errc::invalid_argument,
+ "multiple specifications of visibility type");
if (!StringRef(Arg->getValue()).contains('='))
return createStringError(errc::invalid_argument,
"bad format for --set-visibility-syms");
>From 9ec88f20a28d9612b5f80cf1fa2e522e67f981c3 Mon Sep 17 00:00:00 2001
From: Ilia Kuklin <ikuklin at accesssoftek.com>
Date: Thu, 8 Feb 2024 22:28:07 +0500
Subject: [PATCH 6/9] Adjust and separate tests of setting visibility
functionality
---
.../llvm-objcopy/ELF/set-visibility-sym.test | 106 ++++++++++++
.../llvm-objcopy/ELF/set-visibility-syms.test | 155 ++++++++++++++++++
2 files changed, 261 insertions(+)
create mode 100644 llvm/test/tools/llvm-objcopy/ELF/set-visibility-sym.test
create mode 100644 llvm/test/tools/llvm-objcopy/ELF/set-visibility-syms.test
diff --git a/llvm/test/tools/llvm-objcopy/ELF/set-visibility-sym.test b/llvm/test/tools/llvm-objcopy/ELF/set-visibility-sym.test
new file mode 100644
index 00000000000000..0cae042914589c
--- /dev/null
+++ b/llvm/test/tools/llvm-objcopy/ELF/set-visibility-sym.test
@@ -0,0 +1,106 @@
+
+# RUN: yaml2obj %s -o %t.o
+
+# Check if the visibility of a single symbol is set correctly,
+# and none of other symbols are affected
+# RUN: cp %t.o %t0.o
+# RUN: llvm-objcopy %t0.o --set-visibility-sym=default_local=hidden
+# RUN: llvm-objcopy %t0.o --set-visibility-sym=internal_local=protected
+# RUN: llvm-objcopy %t0.o --set-visibility-sym=hidden_local=internal
+# RUN: llvm-objcopy %t0.o --set-visibility-sym=protected_local=default
+# RUN: llvm-readelf -s %t0.o | FileCheck %s --check-prefix=SINGLE
+
+# SINGLE-DAG: HIDDEN 1 default_local
+# SINGLE-DAG: PROTECTED 1 internal_local
+# SINGLE-DAG: INTERNAL 1 hidden_local
+# SINGLE-DAG: DEFAULT 1 protected_local
+# Unaffected symbols:
+# SINGLE-DAG: DEFAULT 1 default_global
+# SINGLE-DAG: DEFAULT 1 default_weak
+# SINGLE-DAG: INTERNAL 1 internal_global
+# SINGLE-DAG: INTERNAL 1 internal_weak
+# SINGLE-DAG: HIDDEN 1 hidden_global
+# SINGLE-DAG: HIDDEN 1 hidden_weak
+# SINGLE-DAG: PROTECTED 1 protected_global
+# SINGLE-DAG: PROTECTED 1 protected_weak
+
+
+# Check if the visibility of symbols specified by a regex are set correctly,
+# and none of other symbols are affected
+# RUN: llvm-objcopy %t.o %t1.o --set-visibility-sym=.*_local=hidden --regex
+# RUN: llvm-readelf -s %t1.o | FileCheck %s --check-prefix=REGEX
+
+# REGEX-DAG: HIDDEN 1 default_local
+# REGEX-DAG: HIDDEN 1 internal_local
+# REGEX-DAG: HIDDEN 1 hidden_local
+# REGEX-DAG: HIDDEN 1 protected_local
+# Unaffected symbols:
+# REGEX-DAG: DEFAULT 1 default_global
+# REGEX-DAG: DEFAULT 1 default_weak
+# REGEX-DAG: INTERNAL 1 internal_global
+# REGEX-DAG: INTERNAL 1 internal_weak
+# REGEX-DAG: HIDDEN 1 hidden_global
+# REGEX-DAG: HIDDEN 1 hidden_weak
+# REGEX-DAG: PROTECTED 1 protected_global
+# REGEX-DAG: PROTECTED 1 protected_weak
+
+
+!ELF
+FileHeader:
+ Class: ELFCLASS64
+ Data: ELFDATA2LSB
+ Type: ET_REL
+ Machine: EM_X86_64
+Sections:
+ - Name: .text
+ Type: SHT_PROGBITS
+Symbols:
+ - Name: default_local
+ Section: .text
+ Binding: STB_LOCAL
+ - Name: default_global
+ Section: .text
+ Binding: STB_GLOBAL
+ - Name: default_weak
+ Section: .text
+ Binding: STB_WEAK
+ - Name: internal_local
+ Section: .text
+ Binding: STB_LOCAL
+ Other: [ STV_INTERNAL ]
+ - Name: internal_global
+ Section: .text
+ Binding: STB_GLOBAL
+ Other: [ STV_INTERNAL ]
+ - Name: internal_weak
+ Section: .text
+ Binding: STB_WEAK
+ Other: [ STV_INTERNAL ]
+ - Name: hidden_local
+ Section: .text
+ Binding: STB_LOCAL
+ Other: [ STV_HIDDEN ]
+ - Name: hidden_global
+ Section: .text
+ Binding: STB_GLOBAL
+ Other: [ STV_HIDDEN ]
+ - Name: hidden_weak
+ Section: .text
+ Binding: STB_WEAK
+ Other: [ STV_HIDDEN ]
+ - Name: protected_local
+ Section: .text
+ Binding: STB_LOCAL
+ Other: [ STV_PROTECTED ]
+ - Name: protected_global
+ Section: .text
+ Binding: STB_GLOBAL
+ Other: [ STV_PROTECTED ]
+ - Name: protected_weak
+ Section: .text
+ Binding: STB_WEAK
+ Other: [ STV_PROTECTED ]
+ - Name: ignored_name
+ Section: .text
+ Binding: STB_GLOBAL
+ Other: [ STV_INTERNAL ]
diff --git a/llvm/test/tools/llvm-objcopy/ELF/set-visibility-syms.test b/llvm/test/tools/llvm-objcopy/ELF/set-visibility-syms.test
new file mode 100644
index 00000000000000..d3fe8ca8229e38
--- /dev/null
+++ b/llvm/test/tools/llvm-objcopy/ELF/set-visibility-syms.test
@@ -0,0 +1,155 @@
+
+# RUN: yaml2obj %s -o %t.o
+# RUN: echo '.*' > %t.symbols.regex
+
+# Check if the visibility of all symbols is properly set to DEFAULT
+# RUN: llvm-objcopy %t.o %t0.o --set-visibility-syms=%t.symbols.regex=default --regex
+# RUN: llvm-readelf -s %t0.o | FileCheck %s --check-prefix=DEF
+
+# DEF-DAG: DEFAULT 1 default_local
+# DEF-DAG: DEFAULT 1 internal_local
+# DEF-DAG: DEFAULT 1 hidden_local
+# DEF-DAG: DEFAULT 1 protected_local
+# DEF-DAG: DEFAULT 1 default_global
+# DEF-DAG: DEFAULT 1 default_weak
+# DEF-DAG: DEFAULT 1 internal_global
+# DEF-DAG: DEFAULT 1 internal_weak
+# DEF-DAG: DEFAULT 1 hidden_global
+# DEF-DAG: DEFAULT 1 hidden_weak
+# DEF-DAG: DEFAULT 1 protected_global
+# DEF-DAG: DEFAULT 1 protected_weak
+
+
+# Check if the visibility of all symbols is properly set to HIDDEN
+# RUN: llvm-objcopy %t.o %t1.o --set-visibility-syms=%t.symbols.regex=hidden --regex
+# RUN: llvm-readelf -s %t1.o | FileCheck %s --check-prefix=HID
+
+# HID-DAG: HIDDEN 1 default_local
+# HID-DAG: HIDDEN 1 internal_local
+# HID-DAG: HIDDEN 1 hidden_local
+# HID-DAG: HIDDEN 1 protected_local
+# HID-DAG: HIDDEN 1 default_global
+# HID-DAG: HIDDEN 1 default_weak
+# HID-DAG: HIDDEN 1 internal_global
+# HID-DAG: HIDDEN 1 internal_weak
+# HID-DAG: HIDDEN 1 hidden_global
+# HID-DAG: HIDDEN 1 hidden_weak
+# HID-DAG: HIDDEN 1 protected_global
+# HID-DAG: HIDDEN 1 protected_weak
+
+
+# Check if the visibility of all symbols is properly set to PROTECTED
+# RUN: llvm-objcopy %t.o %t2.o --set-visibility-syms=%t.symbols.regex=protected --regex
+# RUN: llvm-readelf -s %t2.o | FileCheck %s --check-prefix=PRO
+
+# PRO-DAG: PROTECTED 1 default_local
+# PRO-DAG: PROTECTED 1 internal_local
+# PRO-DAG: PROTECTED 1 hidden_local
+# PRO-DAG: PROTECTED 1 protected_local
+# PRO-DAG: PROTECTED 1 default_global
+# PRO-DAG: PROTECTED 1 default_weak
+# PRO-DAG: PROTECTED 1 internal_global
+# PRO-DAG: PROTECTED 1 internal_weak
+# PRO-DAG: PROTECTED 1 hidden_global
+# PRO-DAG: PROTECTED 1 hidden_weak
+# PRO-DAG: PROTECTED 1 protected_global
+# PRO-DAG: PROTECTED 1 protected_weak
+
+
+# Check if the visibility of all symbols is properly set to INTERNAL
+# RUN: llvm-objcopy %t.o %t3.o --set-visibility-syms=%t.symbols.regex=internal --regex
+# RUN: llvm-readelf -s %t3.o | FileCheck %s --check-prefix=INT
+
+# INT-DAG: INTERNAL 1 default_local
+# INT-DAG: INTERNAL 1 internal_local
+# INT-DAG: INTERNAL 1 hidden_local
+# INT-DAG: INTERNAL 1 protected_local
+# INT-DAG: INTERNAL 1 default_global
+# INT-DAG: INTERNAL 1 default_weak
+# INT-DAG: INTERNAL 1 internal_global
+# INT-DAG: INTERNAL 1 internal_weak
+# INT-DAG: INTERNAL 1 hidden_global
+# INT-DAG: INTERNAL 1 hidden_weak
+# INT-DAG: INTERNAL 1 protected_global
+# INT-DAG: INTERNAL 1 protected_weak
+
+
+# Check if setting the visibility of certain symbols that were read from
+# a file does not affect other symbols
+# RUN: echo -e "default_local\ninternal_local" > %t.symbol.list
+# RUN: llvm-objcopy %t.o %t3.o --set-visibility-syms=%t.symbol.list=hidden
+# RUN: llvm-readelf -s %t3.o | FileCheck %s --check-prefix=FILE
+
+# FILE-DAG: HIDDEN 1 default_local
+# FILE-DAG: HIDDEN 1 internal_local
+# Unaffected symbols:
+# FILE-DAG: HIDDEN 1 hidden_local
+# FILE-DAG: PROTECTED 1 protected_local
+# FILE-DAG: DEFAULT 1 default_global
+# FILE-DAG: DEFAULT 1 default_weak
+# FILE-DAG: INTERNAL 1 internal_global
+# FILE-DAG: INTERNAL 1 internal_weak
+# FILE-DAG: HIDDEN 1 hidden_global
+# FILE-DAG: HIDDEN 1 hidden_weak
+# FILE-DAG: PROTECTED 1 protected_global
+# FILE-DAG: PROTECTED 1 protected_weak
+
+!ELF
+FileHeader:
+ Class: ELFCLASS64
+ Data: ELFDATA2LSB
+ Type: ET_REL
+ Machine: EM_X86_64
+Sections:
+ - Name: .text
+ Type: SHT_PROGBITS
+Symbols:
+ - Name: default_local
+ Section: .text
+ Binding: STB_LOCAL
+ - Name: default_global
+ Section: .text
+ Binding: STB_GLOBAL
+ - Name: default_weak
+ Section: .text
+ Binding: STB_WEAK
+ - Name: internal_local
+ Section: .text
+ Binding: STB_LOCAL
+ Other: [ STV_INTERNAL ]
+ - Name: internal_global
+ Section: .text
+ Binding: STB_GLOBAL
+ Other: [ STV_INTERNAL ]
+ - Name: internal_weak
+ Section: .text
+ Binding: STB_WEAK
+ Other: [ STV_INTERNAL ]
+ - Name: hidden_local
+ Section: .text
+ Binding: STB_LOCAL
+ Other: [ STV_HIDDEN ]
+ - Name: hidden_global
+ Section: .text
+ Binding: STB_GLOBAL
+ Other: [ STV_HIDDEN ]
+ - Name: hidden_weak
+ Section: .text
+ Binding: STB_WEAK
+ Other: [ STV_HIDDEN ]
+ - Name: protected_local
+ Section: .text
+ Binding: STB_LOCAL
+ Other: [ STV_PROTECTED ]
+ - Name: protected_global
+ Section: .text
+ Binding: STB_GLOBAL
+ Other: [ STV_PROTECTED ]
+ - Name: protected_weak
+ Section: .text
+ Binding: STB_WEAK
+ Other: [ STV_PROTECTED ]
+ - Name: ignored_name
+ Section: .text
+ Binding: STB_GLOBAL
+ Other: [ STV_INTERNAL ]
>From 450e1f622c446651059cdb62127ed6356b23b6ef Mon Sep 17 00:00:00 2001
From: Ilia Kuklin <ikuklin at accesssoftek.com>
Date: Thu, 8 Feb 2024 22:28:59 +0500
Subject: [PATCH 7/9] Add a test for checking various error cases
---
.../ELF/set-visibility-check-errors.test | 43 +++++++++++++++++++
1 file changed, 43 insertions(+)
create mode 100644 llvm/test/tools/llvm-objcopy/ELF/set-visibility-check-errors.test
diff --git a/llvm/test/tools/llvm-objcopy/ELF/set-visibility-check-errors.test b/llvm/test/tools/llvm-objcopy/ELF/set-visibility-check-errors.test
new file mode 100644
index 00000000000000..2555d727f031fe
--- /dev/null
+++ b/llvm/test/tools/llvm-objcopy/ELF/set-visibility-check-errors.test
@@ -0,0 +1,43 @@
+
+# RUN: yaml2obj %s -o %t.o
+# RUN: echo 'foo' > %t.symbols
+
+# Check if passing an invalid visibility type generates an error message
+# RUN: not llvm-objcopy %t.o --set-visibility-syms=%t.symbols=invalid-type --regex 2>&1 | FileCheck %s --check-prefix=TYPE
+# RUN: not llvm-objcopy %t.o --set-visibility-sym=foo=invalid-type --regex 2>&1 | FileCheck %s --check-prefix=TYPE
+# TYPE: error: 'invalid-type' is not a valid symbol visibility
+
+# Check if using the option multiple times generates an error message
+# RUN: not llvm-objcopy %t.o --set-visibility-syms=%t.symbols=hidden \
+# RUN: --set-visibility-syms=%t.symbols=default \
+# RUN: --regex 2>&1 | FileCheck %s --check-prefix=MULTIPLE
+# RUN: not llvm-objcopy %t.o --set-visibility-sym=foo=hidden \
+# RUN: --set-visibility-sym=bar=default \
+# RUN: --regex 2>&1 | FileCheck %s --check-prefix=MULTIPLE
+# RUN: not llvm-objcopy %t.o --set-visibility-syms=%t.symbols=hidden \
+# RUN: --set-visibility-sym=bar=default \
+# RUN: --regex 2>&1 | FileCheck %s --check-prefix=MULTIPLE
+# MULTIPLE: error: multiple specifications of visibility type
+
+# Check if not using the proper format with character '=' generates an error
+# RUN: not llvm-objcopy %t.o --set-visibility-syms=%t.symbols,hidden 2>&1 | FileCheck %s --check-prefix=FORMAT
+# RUN: not llvm-objcopy %t.o --set-visibility-sym=foo default 2>&1 | FileCheck %s --check-prefix=FORMAT
+# FORMAT: error: bad format for --set-visibility-sym
+
+# Check if using an invalid symbol pattern generates an error
+# RUN: echo '*.' > %t.symbols.regex
+# RUN: not llvm-objcopy %t.o --set-visibility-syms=%t.symbols.regex=hidden --regex 2>&1 | FileCheck %s --check-prefix=SYMBOL
+# RUN: not llvm-objcopy %t.o --set-visibility-sym=*.=default --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 --set-visibility-syms=no_file=hidden 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
>From 20a8c43baa85fb2b0733362928c1a3cbe7edc614 Mon Sep 17 00:00:00 2001
From: Ilia Kuklin <ikuklin at accesssoftek.com>
Date: Thu, 8 Feb 2024 22:29:59 +0500
Subject: [PATCH 8/9] Add a test for ingoring SHN_UNDEF symbols
---
.../ELF/set-visibility-undef-symbol.test | 19 +++++++++++++++++++
1 file changed, 19 insertions(+)
create mode 100644 llvm/test/tools/llvm-objcopy/ELF/set-visibility-undef-symbol.test
diff --git a/llvm/test/tools/llvm-objcopy/ELF/set-visibility-undef-symbol.test b/llvm/test/tools/llvm-objcopy/ELF/set-visibility-undef-symbol.test
new file mode 100644
index 00000000000000..e7a22a09351b3a
--- /dev/null
+++ b/llvm/test/tools/llvm-objcopy/ELF/set-visibility-undef-symbol.test
@@ -0,0 +1,19 @@
+
+# Check if symbols with SHN_UNDEF index are ignored when setting visibility
+
+# RUN: yaml2obj %s -o %t.o
+# RUN: llvm-objcopy %t.o --set-visibility-sym=foo=hidden
+# RUN: llvm-readelf -s %t.o | FileCheck %s
+
+# CHECK: DEFAULT UND foo
+
+!ELF
+FileHeader:
+ Class: ELFCLASS64
+ Data: ELFDATA2LSB
+ Type: ET_REL
+ Machine: EM_X86_64
+Symbols:
+ - Name: foo
+ Binding: STB_LOCAL
+ Index: SHN_UNDEF
>From f6b659b986d51f12e283174c6a2cc90c06549ee7 Mon Sep 17 00:00:00 2001
From: Ilia Kuklin <ikuklin at accesssoftek.com>
Date: Thu, 8 Feb 2024 22:30:55 +0500
Subject: [PATCH 9/9] Update the llvm-objcopy documentation
---
llvm/docs/CommandGuide/llvm-objcopy.rst | 9 +++++++++
1 file changed, 9 insertions(+)
diff --git a/llvm/docs/CommandGuide/llvm-objcopy.rst b/llvm/docs/CommandGuide/llvm-objcopy.rst
index 6e13cd94b92fda..f150fdd7ec51ac 100644
--- a/llvm/docs/CommandGuide/llvm-objcopy.rst
+++ b/llvm/docs/CommandGuide/llvm-objcopy.rst
@@ -450,6 +450,15 @@ them.
Set the start address of the output to ``<addr>``. Overrides any previously
specified :option:`--change-start` or :option:`--adjust-start` options.
+.. option:: --set-visibility-sym <symbol>=<visibility_type>
+
+ Change the visibility of a symbol to the specified type.
+
+.. option:: --set-visibility-syms <filename>=<visibility_type>
+
+ Reads a list of symbols from <filename> and changes their visibility to the
+ specified type. Visibility types: default, internal, hidden, protected.
+
.. option:: --split-dwo <dwo-file>
Equivalent to running :program:`llvm-objcopy` with :option:`--extract-dwo` and
More information about the llvm-commits
mailing list