This is an archive of the discontinued LLVM Phabricator instance.

[lld-macho] Prevent assertions for aliases to weak_def_can_be_hidden symbols
ClosedPublic

Authored by paulkirth on Jan 5 2023, 12:47 PM.

Details

Summary

In https://reviews.llvm.org/D137982 we found that on Mach-O private
aliases could trigger an assert in lld when the aliasee was a
weak_def_can_be_hidden symbol.

This appears to be incorrect, and should be allowed in Mach-O.
Disallowing this behavior is also inconsistent with how ld64 handles
a private alias to weak_def_can_be_hidden symbols.

This patch removes the assert and tests that LLD handles such aliases
gracefully.

Diff Detail

Event Timeline

paulkirth created this revision.Jan 5 2023, 12:47 PM
Herald added projects: Restricted Project, Restricted Project. · View Herald TranscriptJan 5 2023, 12:47 PM
Herald added a reviewer: Restricted Project. · View Herald Transcript
paulkirth requested review of this revision.Jan 5 2023, 12:47 PM
Herald added a project: Restricted Project. · View Herald TranscriptJan 5 2023, 12:47 PM
paulkirth added inline comments.Jan 5 2023, 1:59 PM
lld/test/MachO/weak-def-can-be-hidden.s
142

oops, will remove whitespace changes.

paulkirth updated this revision to Diff 486697.Jan 5 2023, 3:35 PM

Remove newlines in test file

int3 added a subscriber: int3.Jan 6 2023, 10:42 AM

Hm interesting. I'm actually wondering if we are handling these aliases correctly in general. I wasn't aware that there were section-based aliases; alias-symbols.s only tests the non-section-based types.

Hm interesting. I'm actually wondering if we are handling these aliases correctly in general.

That's actually one of the reasons I made the patch. I'm not the most familiar w/ Mach-O semantics, but I believe its perfectly fine to have an alias to a weak or weak_def_can_be_hidden symbol. I wanted to double check with the experts that 1) that assumption is correct, and 2) if there is broader problem than just a missing case in an assertion.

I wasn't aware that there were section-based aliases; alias-symbols.s only tests the non-section-based types.

hmm, you know I just did the simple thing and added the test here since it used weak_def_can_be_hidden already. Do you think we should move this test into alias.s or maybe somewhere else?

int3 added a comment.Jan 6 2023, 12:00 PM

Okay so I looked into this a bit more and I don't think there are actually section-based aliases. Creating an alias to a Defined symbol (i.e. defined in the current object file) causes llvm-mc to emit a regular non-aliasing section symbol. Your sym.n_type & N_INDR check is not doing what you think it is -- n_type is not a set of disjoint flags, but a bunch of overlapping values. sym.n_type & N_INDR happens to be true for n_type == N_SECT, but that doesn't mean n_type is an alias.

So I think we should just delete the assertion, and keep the test as-is (with an update to the comment)

lld/test/MachO/weak-def-can-be-hidden.s
119

maybe something like "an alias is the only way to get llvm-mc to set .weak_def_can_be_hidden on an already-hidden symbol. Verify that LLD can handle these double-hidden symbols gracefully"

int3 added a comment.Jan 6 2023, 12:00 PM

(I verified the above by looking at the output of llvm-readobj --syms)

Your sym.n_type & N_INDR check is not doing what you think it is -- n_type is not a set of disjoint flags, but a bunch of overlapping values. sym.n_type & N_INDR happens to be true for n_type == N_SECT, but that doesn't mean n_type is an alias.

Thanks for pointing out the subtlety here. I was definitely under the wrong impression.

So I think we should just delete the assertion, and keep the test as-is (with an update to the comment)

Sounds good. I'll update the patch with those suggestions.

paulkirth updated this revision to Diff 486985.Jan 6 2023, 1:57 PM
paulkirth retitled this revision from [lld] Prevent assertions for aliases to weak_def_can_be_hidden symbols to [lld-macho] Prevent assertions for aliases to weak_def_can_be_hidden symbols.
paulkirth edited the summary of this revision. (Show Details)

Address comments

  • remove assert
  • update comment in test file
paulkirth marked an inline comment as done.Jan 6 2023, 1:58 PM
int3 accepted this revision.Jan 6 2023, 10:18 PM

Thank you!

lld/test/MachO/weak-def-can-be-hidden.s
120
This revision is now accepted and ready to land.Jan 6 2023, 10:18 PM