Skip to content
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

Macos cc_wrapper: Use otool to read install name #13313

Closed

Conversation

jlaxson
Copy link
Contributor

@jlaxson jlaxson commented Apr 7, 2021

When linking an executable on darwin, cc_wrapper.sh contains functionality to ensure rpath entries and shared library install names are relative and match the intended paths that will be used in downstream actions. This depends on being able to traverse symlinks from the _solibs path (like _solib_darwin_x86_64/_U_S_S_Cgenlib_Uimport___U/libgenlib.so) to the actual file (like bazel-out/darwin-opt-exec-31C2CD59/bin/libgenlib.so). This also assumes that the library has its internal install name equal to that latter path - this seems to be generally true right now, but is maybe a bit brittle.

This system breaks down when doing linking on a remote executor, because Bazel does not encode symlinks, and so the file shows up in _solib_darwin_x86_64 as a real file, and the wrapper picks the wrong install name. The resulting executable works when run directly from bazel-bin/bazel-out on a local machine, but will not work via bazel run, or when used in remote actions.

This change uses otool to read the install name of the target library, which is guaranteed to match what was embedded into the executable during link time. This result will be immune to the peculiarities of how symlinks are used and what install_name is encoded into the shared library during its creation.

@google-cla google-cla bot added the cla: yes label Apr 7, 2021
@jlaxson jlaxson force-pushed the osx-cc-wrapper-read-install-name branch from 0f7caf7 to 06bddbf Compare April 7, 2021 23:24
@aiuto aiuto requested a review from allevato October 7, 2021 21:22
@allevato
Copy link
Member

allevato commented Oct 7, 2021

I'm not terribly familiar with this script or its history; maybe @trybka has some insight.

@keith
Copy link
Member

keith commented Oct 7, 2021

note this logic will have to be duplicated in https://github.com/bazelbuild/bazel/blob/master/tools/cpp/osx_cc_wrapper.sh

@trybka
Copy link
Contributor

trybka commented Oct 7, 2021

I'm not super familiar with this TBH, but the change looks reasonable to me.

Copy link
Member

@allevato allevato left a comment

Choose a reason for hiding this comment

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

I'm not super familiar with this TBH, but the change looks reasonable to me.

I'll take it!

@trybka
Copy link
Contributor

trybka commented Oct 7, 2021

@keith's point about duplicating the changes to the other script still stand though. :-)

@jlaxson jlaxson force-pushed the osx-cc-wrapper-read-install-name branch from 06bddbf to ae64118 Compare October 8, 2021 17:30
@jlaxson
Copy link
Contributor Author

jlaxson commented Oct 8, 2021

Rebased and added the change to the non-tpl copy of the script

Copy link
Member

@keith keith left a comment

Choose a reason for hiding this comment

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

works for our use case with swift-syntax, here's the diff of otool -L in that case:

 bazel-bin/tools/deadcode/deadcode:
        /usr/lib/libc++.1.dylib (compatibility version 1.0.0, current version 905.6.0)
-       @rpath/lib_InternalSwiftSyntaxParser.dylib (compatibility version 1.0.0, current version 1300.0.31)
+       @loader_path/../../_solib_darwin_x86_64/_U@com_Ugithub_Ukeith_Uswift_Usyntax_S_S_Clibrary___U/lib_InternalSwiftSyntaxParser.dylib (compatibility version 1.0.0, current version 1300.0.31)

@keith
Copy link
Member

keith commented Jan 26, 2022

@trybka can you take another look at this one?

@trybka
Copy link
Contributor

trybka commented Jan 26, 2022

Shouldn't the OTOOL var appear in the .sh as well as the .sh.tpl file?

# Get the path of a lib inside a tool
function get_otool_path() {
# the lib path is the path of the original lib relative to the workspace
get_realpath $1 | sed 's|^.*/bazel-out/|bazel-out/|'
${OTOOL} -D "$1" | awk 'NR==2{print $1}' | sed 's|^.*/bazel-out/|bazel-out/|'
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
${OTOOL} -D "$1" | awk 'NR==2{print $1}' | sed 's|^.*/bazel-out/|bazel-out/|'
/usr/bin/otool -D "$1" | awk 'NR==2{print $1}' | sed 's|^.*/bazel-out/|bazel-out/|'

@sgowroji sgowroji added team-Rules-ObjC Issues for Objective-C maintainers awaiting-user-response Awaiting a response from the author labels Oct 19, 2022
@sgowroji
Copy link
Member

Hello @jlaxson, Could you please resolve the above branch conflicts and reply to the given review comments. Thanks!

@keith
Copy link
Member

keith commented Oct 24, 2022

I submitted a rebased version here #16542

@keertk
Copy link
Member

keertk commented Dec 7, 2022

Hi there! Thank you for contributing to the Bazel repository. We appreciate your time and effort. We're doing a clean up of old PRs and will be closing this since a new one has been submitted. Please feel free to reopen if you'd like to discuss anything further. We’ll respond as soon as we have the bandwidth/resources to do so.

@keertk keertk closed this Dec 7, 2022
@keith
Copy link
Member

keith commented Dec 7, 2022

should be fine, #16542 should be landed instead

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting-user-response Awaiting a response from the author cla: yes team-Rules-ObjC Issues for Objective-C maintainers
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants