Skip to content

Commit bd7735f

Browse files
committedJan 31, 2019
[llvm-objcopy] Skip --localize-symbol for undefined symbols
Summary: Include the symbol being defined in the list of requirements for using --localize-symbol. This is used, for example, when someone is depending on two different projects that have the same (or close enough) method defined in each library, and using "-L sym" for a conflicting symbol in one of the libraries so that the definition from the other one is used. However, the library may have internal references to the symbol, which cause program crashes when those are used, i.e.: ``` $ cat foo.c int foo() { return 5; } $ cat bar.c int foo(); int bar() { return 2 * foo(); } $ cat foo2.c int foo() { /* Safer implementation */ return 42; } $ cat main.c int bar(); int main() { __builtin_printf("bar = %d\n", bar()); return 0; } $ ar rcs libfoo.a foo.o bar.o $ ar rcs libfoo2.a foo2.o # Picks the wrong foo() impl $ clang main.o -lfoo -lfoo2 -L. -o main # Picks the right foo() impl $ objcopy -L foo libfoo.a && clang main.o -lfoo -lfoo2 -L. -o main # Links somehow, but crashes at runtime $ llvm-objcopy -L foo libfoo.a && clang main.o -lfoo -lfoo2 -L. -o main ``` Reviewers: jhenderson, alexshap, jakehehrlich, espindola Subscribers: emaste, arichardson Differential Revision: https://reviews.llvm.org/D57417 llvm-svn: 352767
1 parent 6fa5e62 commit bd7735f

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed
 

‎llvm/test/tools/llvm-objcopy/ELF/localize.test

+12
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# RUN: yaml2obj %s > %t
22
# RUN: llvm-objcopy \
33
# RUN: --localize-symbol Global \
4+
# RUN: -L GlobalUndef \
45
# RUN: -L Local \
56
# RUN: -L Weak \
67
# RUN: -L GlobalCommon \
@@ -45,6 +46,8 @@ Symbols:
4546
Size: 8
4647
Section: .text
4748
Value: 0x1010
49+
- Name: GlobalUndef
50+
Type: STT_FUNC
4851
- Name: GlobalCommon
4952
Type: STT_OBJECT
5053
Index: SHN_COMMON
@@ -89,6 +92,15 @@ Symbols:
8992
#CHECK-NEXT: Section: .text
9093
#CHECK-NEXT: }
9194
#CHECK-NEXT: Symbol {
95+
#CHECK-NEXT: Name: GlobalUndef
96+
#CHECK-NEXT: Value:
97+
#CHECK-NEXT: Size:
98+
#CHECK-NEXT: Binding: Global
99+
#CHECK-NEXT: Type: Function
100+
#CHECK-NEXT: Other:
101+
#CHECK-NEXT: Section: Undefined
102+
#CHECK-NEXT: }
103+
#CHECK-NEXT: Symbol {
92104
#CHECK-NEXT: Name: GlobalCommon
93105
#CHECK-NEXT: Value: 0x2006
94106
#CHECK-NEXT: Size: 2

‎llvm/tools/llvm-objcopy/ELF/ELFObjcopy.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,9 @@ static Error handleArgs(const CopyConfig &Config, Object &Obj,
287287
// them.
288288
if (Obj.SymbolTable) {
289289
Obj.SymbolTable->updateSymbols([&](Symbol &Sym) {
290-
if (!Sym.isCommon() &&
290+
// Common and undefined symbols don't make sense as local symbols, and can
291+
// even cause crashes if we localize those, so skip them.
292+
if (!Sym.isCommon() && Sym.getShndx() != SHN_UNDEF &&
291293
((Config.LocalizeHidden &&
292294
(Sym.Visibility == STV_HIDDEN || Sym.Visibility == STV_INTERNAL)) ||
293295
is_contained(Config.SymbolsToLocalize, Sym.Name)))

0 commit comments

Comments
 (0)
Please sign in to comment.