[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:32:56 PST 2024
================
@@ -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=internal_global=protected
+# RUN: llvm-objcopy %t0.o --set-visibility-sym=protected_global=internal
----------------
kuilpd wrote:
I didn't realize it at first, but using the option multiple times would result in overwriting the set visibility, since I have only 1 place for storing the assigned visibility type, which is `uint8_t SetVisibilityType` in ELFConfig.h.
Allowing for multiple uses is a bit of a problem. I can make a `StringMap<StringRef>` just like with `SymbolsToRename` to keep a separate visibility for each symbol, but then it disables the possibility to use regexes. Same with reading symbols from file. I could keep a separate NameMatcher with a visibility paired to it, but if regexes with different visibilities overlap then it seems like an undefined behavior.
For now, I added a check to return an error if either of options is used more than once, but I'm really not sure what to do here.
https://github.com/llvm/llvm-project/pull/80872
More information about the llvm-commits
mailing list