-
Notifications
You must be signed in to change notification settings - Fork 12.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #57018 - dcreager:redundant-linker, r=alexcrichton
Keep last redundant linker flag, not first When a library (L1) is passed to the linker multiple times, this is sometimes purposeful: there might be several other libraries in the linker command (L2 and L3) that all depend on L1. You'd end up with a (simplified) linker command that looks like: ``` -l2 -l1 -l3 -l1 ``` With the previous behavior, when rustc encountered a redundant library, it would keep the first instance, and remove the later ones, resulting in: ``` -l2 -l1 -l3 ``` This can cause a linker error, because on some platforms (e.g. Linux), the linker will only include symbols from L1 that are needed *at the point it's referenced in the command line*. So if L3 depends on additional symbols from L1, which aren't needed by L2, the linker won't know to include them, and you'll end up with "undefined symbols" errors. A better behavior is to keep the *last* instance of the library: ``` -l2 -l3 -l1 ``` This ensures that all "downstream" libraries have been included in the linker command before the "upstream" library is referenced. Fixes #47989
- Loading branch information
Showing
7 changed files
with
75 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
-include ../tools.mk | ||
|
||
ifdef IS_WINDOWS | ||
all: | ||
else | ||
|
||
# rustc will remove one of the two redundant references to foo below. Depending | ||
# on which one gets removed, we'll get a linker error on SOME platforms (like | ||
# Linux). On these platforms, when a library is referenced, the linker will | ||
# only pull in the symbols needed _at that point in time_. If a later library | ||
# depends on additional symbols from the library, they will not have been pulled | ||
# in, and you'll get undefined symbols errors. | ||
# | ||
# So in this example, we need to ensure that rustc keeps the _later_ reference | ||
# to foo, and not the former one. | ||
RUSTC_FLAGS = \ | ||
-l static=bar \ | ||
-l foo \ | ||
-l static=baz \ | ||
-l foo \ | ||
-Z print-link-args | ||
|
||
all: $(call DYLIB,foo) $(call STATICLIB,bar) $(call STATICLIB,baz) | ||
$(RUSTC) $(RUSTC_FLAGS) main.rs | ||
$(call RUN,main) | ||
|
||
endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
void bar() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
extern void foo1(); | ||
extern void foo2(); | ||
|
||
void baz() { | ||
foo1(); | ||
foo2(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
void foo1() {} | ||
void foo2() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
extern "C" { | ||
fn bar(); | ||
fn baz(); | ||
} | ||
|
||
fn main() { | ||
unsafe { | ||
bar(); | ||
baz(); | ||
} | ||
} |