Skip to content

Conversation

omajid
Copy link
Member

@omajid omajid commented Sep 10, 2020

We give a list of arguments to the linker which includes a list of static objects we want to be linked together and a set of system-wide native libraries they should be dynamically linked against.

Some build environments, such as Fedora, use the --as-needed linker flag. This makes the linker pay attention to the order in which libraries appear and remove uneeded libararies.

When the linker sees this:

ld --as-needed -lz staticobject.a

It works from left to right and sees that nothing so far depends on libz. The linker removes libz from the set of objects being linked. Then it links staticobject.a, which needs symbols from libz. The linker then complains that it contains an undefined reference.

We can fix that by changing the order so that dependencies appear last in the linker command line:

ld --as-needed staticobject.a -lz

This makes the linker link staticobject.a against libz correctly.

We give a list of arguments to the linker which includes a list of
static objects we want to be linked together and a set of system-wide
native libraries they should be dynamically linked against.

Some build environments, such as Fedora, use the `--as-needed` linker
flag. This makes the linker pay attention to the order in which
libraries appear and remove uneeded libararies.

When the linker sees this:

    ld --as-needed -lz staticobject.a

It works from left to right and sess that nothing so far depends on
libz. The linker removes libz from the set of objects being linked. Then
it links staticobject.a, which needs symbols from libz. The linker then
complains that it contains an undefined reference.

We can fix that by changing the order so that dependencies appear last
in the linker command line:

    ld --as-needed staticobject.a -lz

This makes the linker link staticobject.a against libz correctly.
@Dotnet-GitSync-Bot
Copy link
Collaborator

I couldn't figure out the best area label to add to this PR. If you have write-permissions please help me learn by adding exactly one area label.

@omajid
Copy link
Member Author

omajid commented Sep 10, 2020

I found this bug when trying to build .NET 5.0 Preview 8 (using source-build) on Fedora 32.

cc @dleeapho

@omajid
Copy link
Member Author

omajid commented Sep 17, 2020

@janvorli Can you help me find some reviewers? I have a fix for #41768 (see dotnet/source-build#1755) that touches these lines too and I want to get this merged before doing a PR for that.

@janvorli
Copy link
Member

@omajid I'll review it. I am sorry for missing this PR before.

@omajid
Copy link
Member Author

omajid commented Sep 17, 2020

Thanks!

Copy link
Member

@janvorli janvorli left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thank you!

@janvorli janvorli merged commit 0ed3f33 into dotnet:master Sep 17, 2020
@ghost ghost locked as resolved and limited conversation to collaborators Dec 7, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants