This is an archive of the discontinued LLVM Phabricator instance.

[llvm-objcopy] Implement the PE-COFF specific --subsystem option
ClosedPublic

Authored by mstorsjo on Jan 3 2022, 1:55 PM.

Details

Summary

This implements the parsing of the highly PE-COFF specific option
in ConfigManager.cpp, setting Optional<> values in COFFConfig, which
then are used in COFFObjcopy.

This should fix https://github.com/mstorsjo/llvm-mingw/issues/239.

Diff Detail

Event Timeline

mstorsjo created this revision.Jan 3 2022, 1:55 PM
mstorsjo requested review of this revision.Jan 3 2022, 1:55 PM
Herald added a project: Restricted Project. · View Herald TranscriptJan 3 2022, 1:55 PM
Herald added a subscriber: MaskRay. · View Herald Transcript
MaskRay added inline comments.Jan 4 2022, 2:34 PM
llvm/tools/llvm-objcopy/ConfigManager.cpp
758

The diagnostic should be tested.

ELF/ has relatively better testing. See ELF/add-section.test for an example that various cases are tested.

762

A version without . should be tested.

766

The diagnostic should be tested.

mstorsjo added inline comments.Jan 4 2022, 2:38 PM
llvm/tools/llvm-objcopy/ConfigManager.cpp
598

On the topic of testing diagnostics... Here (in the get*Config() format specific methods) we do error out if a common option has been set, that isn't implemented for that format. But what if options in one of the different format specific structs have been set, e.g. an option set in ELF, but we end up calling getMachOConfig() - then we don't error out at all.

Should we add a separate bool ELFOptionsSet (and similar for all other formats) which we could check for in the get*Config() methods, so we error out cleanly in the same way, if format specific options are set for the wrong format?

MaskRay added inline comments.Jan 4 2022, 2:45 PM
llvm/tools/llvm-objcopy/ConfigManager.cpp
598

Personally I think no action item is needed for you on this one:)

The diagnostic is untested... I remember that when we add recent new binary-format-specific options, we don't bother add tests for other binary formats. I would not bother adding a diagnostic for non-COFF if I am to implement the feature:)

This part may use some refactoring which can make such tests less necessary, but that will be out of the scope of this patch.

mstorsjo updated this revision to Diff 397410.Jan 4 2022, 2:45 PM

Improve test coverage; test for various error cases, test for specifying only a major version.

mstorsjo added inline comments.Jan 4 2022, 2:48 PM
llvm/tools/llvm-objcopy/ConfigManager.cpp
598

Yeah it's out of scope for this patch as we don't have it so far, but more as a general question whether we should have it.

Sure, these diagnostics are untested, but I still think they're kinda important (so maybe we should add at least some, but not exhaustive, tests for it?)

But maybe the common options are more important to bail out on, rather than format specific options which one doesn't end up using for incorrect formats quite as easily, contrary to wanting to use unimplemented features.

avl added inline comments.Jan 5 2022, 3:38 AM
llvm/tools/llvm-objcopy/ConfigManager.cpp
598

It was a design decision that warning message should be displayed for common but not yet implemented option and warning message should NOT be displayed for format specific option set with other formats: https://reviews.llvm.org/D102277#2772972 . That way, using of format specific options when multiple inputs in different formats are specified - would be handled without error messages.

llvm/tools/llvm-objcopy/ObjcopyOpts.td
110

we do not have similar description for other format specific options : "[COFF only]". It looks useful, but probably it would be better to make it as a separate patch for all format specific options.

mstorsjo added inline comments.Jan 5 2022, 4:27 AM
llvm/tools/llvm-objcopy/ConfigManager.cpp
598

Ok, fair enough.

llvm/tools/llvm-objcopy/ObjcopyOpts.td
110

Ok, I'll remove it from here.

mstorsjo updated this revision to Diff 397525.Jan 5 2022, 4:30 AM
mstorsjo marked 6 inline comments as done.

Removed the "COFF only" annotation from the new option.

Out of time today, but will look at this tomorrow.

Please update the llvm-objcopy documentation too.

llvm/tools/llvm-objcopy/COFF/COFFObjcopy.cpp
264

New error messages should use lower-case for their first letter, according to the coding standard.

I don't think I see a test case for this though?

269–270

If I'm reading things right, there's no test for an explicit minor version.

llvm/tools/llvm-objcopy/ConfigManager.cpp
738

Probably should be spelled out rather than using auto? In particular, I believe it might be a pointer, so this needs clarifying.

743–754

My gut says we need something that illustrates this mapping in a test, but I can see people arguing against that.

772

Same as MaskRay said: needs test

mstorsjo marked an inline comment as done.Jan 6 2022, 2:38 AM
mstorsjo added inline comments.
llvm/tools/llvm-objcopy/COFF/COFFObjcopy.cpp
264

Ok, making it lower case and adding a test case for it.

269–270

That’s right, it’s impossible to set only the minor version but not the major at the same time, via the options. But with the raw API, it would be possible.

llvm/tools/llvm-objcopy/ConfigManager.cpp
738

I can change this into for (const llvm::opt::Arg *Arg. Note that there’s many other identical loops with for (auto Arg : InputArgs.filtered(… within the same function, so this makes it a bit more inconsistent though.

743–754

You mean testing all of the switch values? I think it’s overkill…

772

I did add a test for this one, see line 28 in the current version of the test.

mstorsjo updated this revision to Diff 397828.Jan 6 2022, 2:52 AM
mstorsjo marked 3 inline comments as done.

Mention the new option in the docs. Lowercase an error message. Add a test for erroring out on object files. Spell out the type instead of using auto.

mstorsjo updated this revision to Diff 397830.Jan 6 2022, 2:54 AM

Fix a typo in a comment in the new test.

jhenderson added inline comments.Jan 10 2022, 12:35 AM
llvm/test/tools/llvm-objcopy/COFF/pe-fields.test
8

Should the terminology "object files" actually be "relocatable object files"? At least for ELF, al ELF files are object files, whether executable or not (I don't know if the same applies for COFF though).

Regardless, it would be good if this error message contained the offending file's name.

llvm/test/tools/llvm-objcopy/COFF/subsystem.test
26

This check should include the "foobar" in it somewhere. You might want a {{$}} at the end of this message too, since it's a strict prefix of one of the other errors (and you want to avoid a false pass, where the test is reporting a differenet error).

31

I'd prefer it if this had the explicit "major" or "minor" version. Could be achieved using FileCheck variables, i.e. is not a valid subsystem [[TYPE]] version or similar.

llvm/tools/llvm-objcopy/COFF/COFFObjcopy.cpp
263

At least elsewhere in the patch, we don't bother with the explicit llvm:: prefix here.

llvm/tools/llvm-objcopy/ConfigManager.cpp
738

I'd be okay with const auto *Arg if you prefer that for a degree of additional consistency. My main issue is the pointer-ness of Arg. Or, if you are particularly keen on the consistency, I don't mind reverting to what you had before.

mstorsjo added inline comments.Jan 10 2022, 1:26 AM
llvm/test/tools/llvm-objcopy/COFF/pe-fields.test
8

I guess relocatable object file is more correct for COFF too. I'll include the offending file name and check it in the test.

llvm/test/tools/llvm-objcopy/COFF/subsystem.test
26

Ok, I'll include the incorrect subsystem name in the test pattern. (Then it's no longer a prefix of the other messages, but I can include the {{$}} for good measure in any case.)

31

Ok. I can just split up the patterns for clarity too, and have the check include the unparseable part of the version number too.

llvm/tools/llvm-objcopy/COFF/COFFObjcopy.cpp
263

Ok, I'll leave it out here. (This file has a couple existing cases with such redundant prefixes.)

llvm/tools/llvm-objcopy/ConfigManager.cpp
738

Yeah const auto *Arg looks fine to me.

mstorsjo updated this revision to Diff 398531.Jan 10 2022, 1:27 AM
mstorsjo marked 5 inline comments as done.

Applied the suggestions.

jhenderson accepted this revision.Jan 10 2022, 1:49 AM

LGTM, with one final suggestion.

llvm/test/tools/llvm-objcopy/COFF/pe-fields.test
6–8

This is how we usually check the file names in error messages.

This revision is now accepted and ready to land.Jan 10 2022, 1:49 AM
mstorsjo added inline comments.Jan 10 2022, 2:19 AM
llvm/test/tools/llvm-objcopy/COFF/pe-fields.test
6–8

Ok, I'll try that. (I was afraid of the risk of extra fragility with potential backslashes as path separators, which can be tricky/require strict correct quoting throughout the tools. But I'll push it through the premerge testing to see that it's ok before landing.)

mstorsjo updated this revision to Diff 398545.Jan 10 2022, 2:22 AM

Check the full exact filename in the error message. Changed to print the output filename instead of the input filename, for the error.