-
Notifications
You must be signed in to change notification settings - Fork 2.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
DT_NEEDED for shared builds in makefile #5126
DT_NEEDED for shared builds in makefile #5126
Conversation
e34b057
to
3678bd0
Compare
The makefile build specifies -L. -lmbedx509 -lmbedcrypto flags first, and only then object files referencing symbols from those libraries. In this order the linker will not add the linked libraries to the DT_NEEDED section because they are not referenced yet (at least that happens for me on ubuntu 20.04 with the default gnu compiler tools). By first specifying the object files and then the linked libraries, we do end up with libmbedx509 and libmbedcrypto in the DT_NEEDED sections. This way running dlopen(...) on libmedtls.so just works. Note that the CMake build does this by default. Signed-off-by: Harmen Stoppels <[email protected]>
3678bd0
to
01ef723
Compare
Thanks for your contribution! After hesitating for a bit, I classified as "enhancement" rather that bug because the behaviour was as documented, but I agree it's on the line. Anyway, we can probably accept a backport to |
Thanks! I just built mbedtls on a macbook (Apple clang version 11.0.0 on Catalina), and realized this PR is not required to make it work there:
so, it lists the required dylibs already. That means that only on Linux w/ make, libmbedtls.so does not load its dependencies crypto & x509:
Just pointing this out as to me it looks more like a bug than an enhancement. But I can live with either :) |
Found in the linker's manual for the
So the object files must be specified first and the shared libraries afterwards. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The approach and resasoning of the PR seem valid to me, quoting the ld(1) manpage:
The linker will search an archive only once, at the location
where it is specified on the command line. If the archive
defines a symbol which was undefined in some object which
appeared before the archive on the command line, the linker
will include the appropriate file(s) from the archive.
However, an undefined symbol in an object appearing later on
the command line will not cause the linker to search the
archive again.See the -( option for a way to force the linker to search
archives multiple times.You may list the same archive multiple times on the command
line.This type of archive searching is standard for Unix linkers.
However, if you are using ld on AIX, note that it is
different from the behaviour of the AIX linker.
However, the whitespace change made by this PR seems to be the opposite of what you described in the PR description, deleting the whitespace where it is actually needed.
As an aside, it looks like we are not following our own advice on the library order when linking the libraries - using the reverse of what is recommended. We should probably fix that as well, but I think that can be a separate PR.
Non-regression for the fix in Mbed-TLS#5126: libmbedtls and libmbedx509 did not declare their dependencies on libmbedx509 and libmbedcrypto when built with make. Signed-off-by: Gilles Peskine <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This makes sense to me. I don't know much about dynamic linking, but I do know that we should generally link libraries in reverse dependency order, which we weren't doing.
There are a couple of minor problems in the execution.
Also, I think this requires a changelog entry.
I want to have a no-regression test, so I made one: #5131. I verified manually that my test fails before your fix and passes after your fix. We can merge the test after the fix.
I do think this is bug, and requires at least a 2.x backport and a changelog entry. I can live without a 2.16 backport since 2.16 is almost at end-of-life and anyone who was bothered by this bug has already worked around it. |
Signed-off-by: Harmen Stoppels <[email protected]>
Signed-off-by: Harmen Stoppels <[email protected]>
ba9027d
to
fcb4fb7
Compare
Anything expected from my side on this? Should the changelog entry & dlopen-test be added to this PR? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me.
Please make a similar patch for the development_2.x
branch.
I'll handle the non-regression test in my PR. However, please add a changelog entry here announcing the bug fix. This means a file in |
8178c5d
to
9afe7eb
Compare
Signed-off-by: Harmen Stoppels <[email protected]>
9afe7eb
to
3e63616
Compare
Added a changelog and a backport #5133 |
Thanks! |
Non-regression for the fix in Mbed-TLS#5126: libmbedtls and libmbedx509 did not declare their dependencies on libmbedx509 and libmbedcrypto when built with make. Signed-off-by: Gilles Peskine <[email protected]>
Non-regression for the fix in Mbed-TLS#5126: libmbedtls and libmbedx509 did not declare their dependencies on libmbedx509 and libmbedcrypto when built with make. Signed-off-by: Gilles Peskine <[email protected]>
Non-regression for the fix in Mbed-TLS#5126: libmbedtls and libmbedx509 did not declare their dependencies on libmbedx509 and libmbedcrypto when built with make. Signed-off-by: Gilles Peskine <[email protected]>
Description
The makefile build specifies
-L. -lmbedx509 -lmbedcrypto
flags first,and only then object files referencing symbols from those libraries.
In this order the linker will not add the linked libraries to the
DT_NEEDED section because they are not referenced yet (at least that
happens for me on ubuntu 20.04 with the default gnu compiler tools).
By first specifying the object files and then the linked libraries, we
do end up with libmbedx509 and libmbedcrypto in the DT_NEEDED sections.
This way running dlopen(...) on libmedtls.so just works.
Note that the CMake build does this by default.
Also fixes a bug (?) where there is no whitespace between(this was not the case)-lmbedx509$(LOCAL_LDFLAGS)
Requires Backporting
It could be seen as a bug fix, since the cmake and makefile builds are inconsistent right now.
I'd be happy if this got backported.
Todos
Steps to test or reproduce