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

Some compatibility improvements for linker option processing #20384

Closed
wants to merge 41 commits into from

Conversation

alexrp
Copy link
Member

@alexrp alexrp commented Jun 22, 2024

Went through and made sure that most options have all their variants handled, and added handling for a few options that were missing.

This ended up being more commits than I initially expected. I can squash them if desired.

Closes #19613.
Alleviates #18750.

References:

@alexrp alexrp marked this pull request as ready for review June 22, 2024 09:29
src/main.zig Outdated
//
// Note that this ambiguity only exists for the linker option; the Clang option is explicitly
// macOS-specific.
try force_undefined_symbols.put(arena, lookup_type, {});
Copy link
Member Author

Choose a reason for hiding this comment

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

The only other option I can think of here is to make the behavior dependent on -target. But that only works if -target comes first. I think it's also unprecedented for the option processing to be conditioned on target knowledge.

Copy link
Member

Choose a reason for hiding this comment

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

In the face of ambiguity, refuse to proceed.

Copy link
Member Author

Choose a reason for hiding this comment

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

What does that mean concretely here? If I follow that to the letter, I would have to rip out the dynamic_lookup/error handling that was already here (which I'm sure was added because someone needed it).

Copy link
Member Author

Choose a reason for hiding this comment

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

@andrewrk ping just in case you missed the question above.

Copy link
Member

Choose a reason for hiding this comment

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

I do think it should be followed to the letter, including ripping out the existing handling. However, I think it's possible to support unambiguously by lowering it as a placeholder that is resolved later when the target is known. Note this is for zig cc compatibility only; the regular zig CLI doesn't allow setting linker flags directly.

Copy link
Member Author

Choose a reason for hiding this comment

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

Ok, I'll see if I can make that strategy work first, and remove it otherwise.

src/main.zig Outdated Show resolved Hide resolved
Copy link
Member

@andrewrk andrewrk left a comment

Choose a reason for hiding this comment

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

Thanks for working on this. I think we can merge your manual work for now, however, wouldn't it be nice to parse those .td files that you linked instead of maintaining this by hand? This is already implemented for compiler options with tools/update_clang_options.zig.

src/main.zig Outdated
//
// Note that this ambiguity only exists for the linker option; the Clang option is explicitly
// macOS-specific.
try force_undefined_symbols.put(arena, lookup_type, {});
Copy link
Member

Choose a reason for hiding this comment

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

In the face of ambiguity, refuse to proceed.

src/main.zig Outdated Show resolved Hide resolved
@alexrp
Copy link
Member Author

alexrp commented Jul 20, 2024

however, wouldn't it be nice to parse those .td files that you linked instead of maintaining this by hand? This is already implemented for compiler options with tools/update_clang_options.zig.

As I got about 25% through this, that was my thought as well. 🙂 But my plate is full enough as it is, so I can't commit to that right now. Figured I may as well finish what I started, and then hopefully we can transition to something better in the future.

@alexrp
Copy link
Member Author

alexrp commented Aug 1, 2024

@andrewrk I've rebased this without the commit pertaining to the -undefined ambiguity, so that the other changes can get merged. I'll do a follow-up PR for -undefined soon.

src/main.zig Show resolved Hide resolved
@alexrp alexrp force-pushed the master branch 2 times, most recently from acf8268 to 9cd1e64 Compare August 7, 2024 15:35
MichalStrehovsky pushed a commit to dotnet/runtime that referenced this pull request Aug 8, 2024
Per @alexrp's suggestion ziglang/zig#20384 (comment).

I've tested it with bfd and lld. It should work with existing zld as well (which is currently handling `-Wl,-e,0x0`).

Fixes #106029
@alexrp
Copy link
Member Author

alexrp commented Aug 27, 2024

@andrewrk ping in case you didn't see it: #20384 (comment)

alexrp added 25 commits August 28, 2024 02:57
Unblocks scenarios like the one in ziglang#18750, but does not actually fix it.
Copy link
Member

@andrewrk andrewrk left a comment

Choose a reason for hiding this comment

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

I really don't like accumulating all these different ways of specifying the same arguments. It would be better if people standardized on one option name for everything.

So, I think this change should focus on solving the actual bug (#19613), which it doesn't seem like it tackles at all since it just eats the arg and does nothing. Neither the PR description nor the code comments include a justification for this behavior.

If you want to make it handle all of LLD's variants without a specific motivating bug report, then this code needs to use the options td files like is already done for compiler flags.

Comment on lines +2399 to +2404
} else if (mem.eql(u8, arg, "--enable-auto-image-base") or
mem.eql(u8, arg, "-enable-auto-image-base") or
mem.eql(u8, arg, "--disable-auto-image-base") or
mem.eql(u8, arg, "-disable-auto-image-base"))
{
// These are ignored by LLD, but are used by Libtool for MinGW triples.
Copy link
Member

Choose a reason for hiding this comment

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

I don't think zig should recognize options and then do nothing.

What LLD does is not really relevant here. The relevant question is, does the zig project support this functionality or not?

If the user's request cannot be satisfied, it must be an error. If the user's request is satisfied regardless of this option, it should be a warning that the option is superfluous. If the user's request is ambiguous, the flag should not be supported.

Copy link
Member Author

Choose a reason for hiding this comment

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

--enable-auto-image-base is a flag that binutils added 24 years ago for MinGW. It does a hash of the file and uses that as part of the image base value. I assume the idea was to avoid DLLs needing to be relocated when loaded. This is practically irrelevant today as all PEs produced since Windows Vista have ASLR enabled by default anyway, and Windows 10+ has Mandatory ASLR on by default, so it doesn't even care what the PE file wants.

We could implement the actual functionality behind the flag, but I'm fairly certain nobody actually cares about that anymore. The only reason the flag is remotely relevant is because it's hardcoded in Libtool, so if we don't at least accept and ignore it, it's not possible to cross-compile an Autotools project for MinGW with Zig.

Copy link
Member Author

Choose a reason for hiding this comment

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

@andrewrk given this explanation, would you consider it acceptable to recognize and ignore just --(enable,disable)-auto-image-base as a special case?

I don't really care as much about the rest of the stuff I added in this PR, and agree that it would be nice to use LLD's TableGen files like we do for Clang. But usage of --enable-auto-image-base in particular is hardcoded in Libtool when targeting MinGW, and that makes zig cc unusable for that use case. Were it any other project, I would just ask them to stop using that flag (since it's pointless linker churn nowadays due to ASLR), but given the kinds of archaic systems they insist on maintaining support for in Autotools, I don't think there's any chance whatsoever of that happening. Just ignoring the flag unblocks a real use case, and LLD having ignored it for ~7 years would indicate that no one building modern software actually cares about the effect it has in GNU ld.

@alexrp
Copy link
Member Author

alexrp commented Oct 5, 2024

Closing in favor of #21603.

@alexrp alexrp closed this Oct 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Linker flag --enable-auto-image-base is not recognized
4 participants