This is an archive of the discontinued LLVM Phabricator instance.

[llvm-objcopy] [COFF] Do not patch debug entries with Repro type
ClosedPublic

Authored by pirama on Aug 2 2021, 10:48 PM.

Details

Summary

Fix an edge case missed by https://reviews.llvm.org/D78921. The Repro
debug entry (generated with the /Brepro linker flag) does not have a
debug-directory payload. Do not attempt to patch Debug entries of this
type.

Diff Detail

Event Timeline

pirama created this revision.Aug 2 2021, 10:48 PM
pirama requested review of this revision.Aug 2 2021, 10:48 PM
Herald added a project: Restricted Project. · View Herald TranscriptAug 2 2021, 10:48 PM
mstorsjo accepted this revision.Aug 2 2021, 10:54 PM

LGTM, thanks!

Do you happen to know if the same is the case for any other debug directory types, or is this the only one?

This revision is now accepted and ready to land.Aug 2 2021, 10:54 PM

Do you happen to know if the same is the case for any other debug directory types, or is this the only one?

I'm not sure if this is also the case for other debug directory types. Maybe someone familiar with Windows debug info can help? (+thakis, rnk)

pirama added a comment.Aug 3 2021, 9:03 AM

Do you happen to know if the same is the case for any other debug directory types, or is this the only one?

I'm not sure if this is also the case for other debug directory types. Maybe someone familiar with Windows debug info can help? (+thakis, rnk)

Looking at https://docs.microsoft.com/en-us/windows/win32/debug/pe-format#debug-type, we should also exclude IMAGE_DEBUG_TYPE_UNKNOWN. I think all other entries could have a payload associated with it.

pirama updated this revision to Diff 363765.Aug 3 2021, 9:08 AM

Skip IMAGE_DEBUG_TYPE_UNKNOWN debug entries as well.

mstorsjo accepted this revision.Aug 3 2021, 11:22 AM

LGTM, thanks!

I’m considering if it’d be good to change the existing code even further; if the field in the file is zero, regardless of type, we’d just leave it as is, and one error out of its nonzero but can’t be found. WDYT?

pirama added inline comments.Aug 3 2021, 1:39 PM
llvm/tools/llvm-objcopy/COFF/Writer.cpp
432

LGTM, thanks!

I’m considering if it’d be good to change the existing code even further; if the field in the file is zero, regardless of type, we’d just leave it as is, and one error out of its nonzero but can’t be found. WDYT?

Are you suggesting we remove the error check here and call virtualAddressToFileAddress only if Debug->AddressOfRawData is not 0?

I considered it but thought I didn't quite understand the connection between the conditional and the error message. I can amend the patch if you think this is a better choice.

mstorsjo added inline comments.Aug 3 2021, 2:08 PM
llvm/tools/llvm-objcopy/COFF/Writer.cpp
432

Yes, exactly, I think that's a better/more robust choice - if there's a different directory entry which is payload-less (for one reason or another) we should just ignore it; it's only an error if there's a value which we can't reasonably rebase.

And maybe it'd be even better to check PointerToRawData instead of AddressOfRawData for the main check - if the input file didn't have a raw file address to the payload, then we keep it as is (the virtual address should remain unchanged), but if there's a nonzero PointerToRawData, we need to recalculate that based on the new file layout.

pirama updated this revision to Diff 363881.Aug 3 2021, 2:31 PM

Simplify check and update expected error for one test.

mstorsjo accepted this revision.Aug 3 2021, 2:38 PM

LGTM, thanks! And sorry for the iterative detours while I thought through the issue.

jhenderson requested changes to this revision.Aug 4 2021, 12:33 AM
jhenderson added inline comments.
llvm/test/tools/llvm-objcopy/COFF/strip-brepro.test
2 ↗(On Diff #363881)

Why do you need both -S and -x? As I understand it, you only need one of them to strip the debug data.

Also, this comment needs enhancing to explain why this case is special.

5 ↗(On Diff #363881)

Shouldn't there be some check after this line to show that it's been stripped?

This revision now requires changes to proceed.Aug 4 2021, 12:33 AM
mstorsjo added inline comments.Aug 4 2021, 12:48 AM
llvm/test/tools/llvm-objcopy/COFF/strip-brepro.test
5 ↗(On Diff #363881)

I guess the intent of this test isn't to test stripping in itself, but that llvm-objcopy doesn't bail out while processing an executable with this content in it; the executable as such doesn't seem to contain anything that can be stripped out either. But making this clearer in the test comment would be good I guess.

pirama updated this revision to Diff 364171.Aug 4 2021, 10:11 AM

Rename test, expand comments and remove '-S -x' from RUN line.

pirama marked 3 inline comments as done.Aug 4 2021, 10:13 AM
pirama added inline comments.
llvm/test/tools/llvm-objcopy/COFF/strip-brepro.test
2 ↗(On Diff #363881)

Why do you need both -S and -x? As I understand it, you only need one of them to strip the debug data.

This is a carry-over from our local build. Neither -S nor -x is needed to reproduce the issue.

5 ↗(On Diff #363881)

Remove -S, -x. The test is mainly for absence of error but I can add any relevant check on the output if needed.

jhenderson added inline comments.Aug 5 2021, 12:24 AM
llvm/test/tools/llvm-objcopy/COFF/debug-entry-no-payload.test
3

"are handled correctly" usually implies that something needs to be checked, in my mind, namely that the debug directory entries look correct after the invocation of llvm-objcopy. It's this that should be checked. If on the other hand, you think it should be just checking for lack of an error, it's probably best to say just that. I'd check the debug entry though.

48

If I'm not mistaken, there are two entries here below. If so, please update this comment.

pirama updated this revision to Diff 364521.Aug 5 2021, 9:47 AM
pirama marked an inline comment as done.

Update test with revew comments.

mstorsjo accepted this revision.Aug 5 2021, 12:44 PM

Still LGTM, fwiw.

jhenderson accepted this revision.Aug 6 2021, 12:31 AM

LGTM, with two inline nits.

Also super nit: in the llvm-objcopy world, it's one space after '.' :-)

llvm/test/tools/llvm-objcopy/COFF/debug-entry-no-payload.test
14

As there are no other check prefixes used in this file, you can omit the check-prefix argument and just use CHECK instead of DEBUG-DIRS, if you want.

53–54
This revision is now accepted and ready to land.Aug 6 2021, 12:31 AM
pirama updated this revision to Diff 364813.Aug 6 2021, 8:52 AM
pirama marked an inline comment as done.

Address review comments

This revision was landed with ongoing or failed builds.Aug 6 2021, 9:23 AM
This revision was automatically updated to reflect the committed changes.
pirama added a comment.Aug 6 2021, 9:24 AM

Thanks for the reviews @mstorsjo @jhenderson.