This is an archive of the discontinued LLVM Phabricator instance.

[lld] Fill .text section gaps with INT3 only on x86 targets.
ClosedPublic

Authored by jacek on Mar 13 2023, 10:15 AM.

Details

Summary

It doesn't make sense on ARM and using default 0 fill is compatible with MSVC.

(It's more noticeable ARM64EC targets, where additional padding mixed with alignment is used for entry thunk association, so there are more gaps).

Diff Detail

Event Timeline

jacek created this revision.Mar 13 2023, 10:15 AM
Herald added a project: Restricted Project. · View Herald TranscriptMar 13 2023, 10:15 AM
jacek requested review of this revision.Mar 13 2023, 10:15 AM
Herald added a project: Restricted Project. · View Herald TranscriptMar 13 2023, 10:15 AM
mstorsjo accepted this revision.Mar 14 2023, 3:30 AM
mstorsjo added a subscriber: efriedma.

Thanks for the patch - I've consiered doing something about this at some time. At the time, I considered filling areas with a more specific pattern, mapping to trapping instructions similar to int3, but since it requires multibyte patterns instead of filling with a single byte, I never got to doing anything about it.

So, MS link.exe fills sections with zero? Is that a specific design choice (i.e. does it fill with 0xcc on x86) or is it simply the case that link.exe doesn't try to fill sections with sentinel instructions at all?

Overall I guess this patch is fine with me - the previous pattern with 0xcc doesn't seem to have mapped to any specific useful thumb/aarch64 instruction anyway (although I guess it's more likely that the previous one would have trapped, especially on aarch64 it seems to not be recognized by the disassembler) than this plain-zeros instruction.

lld/test/COFF/gaps-fill.test
12

Is this a case where llvm-objdump prints just ... for contiguous areas with plain zeros? (I guess that's primarily what this test should be looking for - it seems a bit odd that that checked aspect is so implicit here.)

This revision is now accepted and ready to land.Mar 14 2023, 3:30 AM

Thanks for the review.

So, MS link.exe fills sections with zero? Is that a specific design choice (i.e. does it fill with 0xcc on x86) or is it simply the case that link.exe doesn't try to fill sections with sentinel instructions at all?

It seems to be a design choice: link.exe does fill with 0xcc on x64. I will add a test case for x64.

FWIW, in on ARM64EC it's a bit more complicated, because we may have both x64 and true ARM64EC code linked together in a single module. Both use the same section (although according to my testing, never the same page, thus even more potential gaps). In theory, it could make sense to use 0xcc fill for x64 parts, but that's not what link.exe does: it uses 0 to fill everything.

jacek added inline comments.Mar 14 2023, 10:57 AM
lld/test/COFF/gaps-fill.test
12

Yes, that seems to be its feature (countSkippableZeroBytes in llvm-objdump.cpp). I noticed that there is -z/--disassemble-zeroes option to disable this feature, I will change the test to use that.

jacek updated this revision to Diff 505174.Mar 14 2023, 10:59 AM
mstorsjo accepted this revision.Mar 14 2023, 1:54 PM

LGTM, thanks!

So, MS link.exe fills sections with zero? Is that a specific design choice (i.e. does it fill with 0xcc on x86) or is it simply the case that link.exe doesn't try to fill sections with sentinel instructions at all?

It seems to be a design choice: link.exe does fill with 0xcc on x64. I will add a test case for x64.

Ok, thanks for confirming!

FWIW, in on ARM64EC it's a bit more complicated, because we may have both x64 and true ARM64EC code linked together in a single module. Both use the same section (although according to my testing, never the same page, thus even more potential gaps). In theory, it could make sense to use 0xcc fill for x64 parts, but that's not what link.exe does: it uses 0 to fill everything.

Yeah that sounds more complex. I guess as you say, it could be possible to do varying filling, but that sounds quite complex, so I guess they've just haven't gone out of their way to implement that.

This revision was automatically updated to reflect the committed changes.
chapuni added inline comments.
lld/test/COFF/gaps-fill.test
39

Does it work if only aarch64 is configured?

mstorsjo added inline comments.Mar 24 2023, 12:50 AM
lld/test/COFF/gaps-fill.test
39

Right, this probably needs to have x86 added to the REQUIRES line.

mstorsjo added inline comments.Mar 24 2023, 1:31 AM
lld/test/COFF/gaps-fill.test
39

I pushed a fix for this now, in 09aa3f7bb54bf7f35809d3abfdf9f6a679ba1003.