This is an archive of the discontinued LLVM Phabricator instance.

[LLD] [COFF] Warn about pseudo relocations that are too narrow
ClosedPublic

Authored by mstorsjo on Jul 8 2023, 2:59 PM.

Details

Summary

In 64 bit mode, any references to symbols that might end up autoimported
must be made via full 64 bit pointers (usually in .refptr stubs
generated by the compiler).

If referenced via e.g. a 32 bit rip relative offset, it might work
as long as DLLs are loaded close together in the 64 bit address
space, but will fail surprisingly later if they happen to be loaded
further apart. Any cases of that happening is usually a toolchain
error, and the sooner we can warn about it, the easier it is to diagnose.

The warning message is a bit odd; normally all lld warnings/errors
start with lower case, but here the error message consists of two
full sentences. Style suggestions for how to handle that are
welcome.

Diff Detail

Event Timeline

mstorsjo created this revision.Jul 8 2023, 2:59 PM
Herald added a project: Restricted Project. · View Herald TranscriptJul 8 2023, 2:59 PM
mstorsjo requested review of this revision.Jul 8 2023, 2:59 PM
Herald added a project: Restricted Project. · View Herald TranscriptJul 8 2023, 2:59 PM

Sounds like a good diagnostic to me. For the warning message perhaps it can be changed a bit to avoid using a full stop?

runtime pseudo relocation in [...] against symbol [...] is too narrow (only 32 bits wide); this will fail at runtime depending on memory layout

I also think it can use a slightly stronger tone than "can fail", maybe even worth being an error by default. With HEASLR being the default nowadays, 32-bit pseudo relocations seems like a recipe for failure, is it not?

Sounds like a good diagnostic to me. For the warning message perhaps it can be changed a bit to avoid using a full stop?

runtime pseudo relocation in [...] against symbol [...] is too narrow (only 32 bits wide); this will fail at runtime depending on memory layout

Nice, thanks!

I also think it can use a slightly stronger tone than "can fail", maybe even worth being an error by default.

Yes, generally whenever you hit this, you have a serious issue anyway. On one hand, I'm worried of breaking cases where things actually do happen to work (for whatever reason - consider things like cygwin, with lots of loader/memory layout trickery - might be doing things differently?). On the other hand, a warning that doesn't break a build is very easily overlooked.

Wrt these issues, we've slowly progressed towards detecting them earlier. Initially, if you struck this, you'd have a strange runtime crash which was mysterious to debug. Nowadays, the mingw-w64 runtime errors out if the actual target symbol is too far away, with a cryptic error message (only giving addresses). With this in place, we'd at least have the symbol name more accessible if someone tries to dig in.

So I'm somewhat undecided between just making it a hard error right away, and merging it as a warning and maybe considering strengthening to an error later.

With HEASLR being the default nowadays, 32-bit pseudo relocations seems like a recipe for failure, is it not?

Not necessarily; while ASLR does affect the load addresses, I think it primarily affects the base addresses where things are loaded, while most DLLs probably still are loaded close together. Liu Hao struck this issue in May (although in a build linked with binutils, so I'm unsure if HEASLR was enabled there or not), and in that case, the mingw-w64 loader only errored out occasionally, while it mostly worked out ok.

mstorsjo updated this revision to Diff 538801.Jul 10 2023, 1:32 PM

Adjusted the warning message to avoid splitting it into two separate sentences.

mati865 accepted this revision.Jul 10 2023, 1:56 PM

I'm in favor of the warning. I don't know LLVM process for adding new errors for code that was previously accepted (even if wrongly) but IMO new errors should be introduced in a way that doesn't surprisingly break downstreams.
So I'd be okay if this becomes a warning in next release (N) and an error/hard error (don't think LLD threats them differently though) in release N+1 or N+2.

ASs for the tone, dunno, I think even with HEASLR user can be lucky enough to have it working.

This revision is now accepted and ready to land.Jul 10 2023, 1:56 PM