This is an archive of the discontinued LLVM Phabricator instance.

[llvm-objcopy] Add support for '--' for delimiting options from input/output files
ClosedPublic

Authored by sdmitriev on May 17 2021, 10:07 PM.

Details

Summary

This will allow to use llvm-objcopy with file names that begin with dashes.

Diff Detail

Event Timeline

sdmitriev created this revision.May 17 2021, 10:07 PM
sdmitriev requested review of this revision.May 17 2021, 10:07 PM
Herald added a project: Restricted Project. · View Herald TranscriptMay 17 2021, 10:07 PM
sdmitriev edited the summary of this revision. (Show Details)May 17 2021, 11:14 PM
jhenderson requested changes to this revision.May 18 2021, 1:21 AM

I'm not against the functional change, but why have so many tests changed? You should have a single dedicated test that covers this, rather than arbitrarily modifying existing tests to cover the new behaviour. As this is not file format specific, it can even be put in the top-level llvm-objcopy directory, although you may want a comment saying it is testing generic behaviour, since it'll presumably need to use e.g. an ELF object to avoid the "no input file" error.

Is there a particular reason you've singled out llvm-objcopy, and not llvm-strip?

llvm/tools/llvm-objcopy/CopyConfig.cpp
478

Nit: fix this clang-tidy issue.

489

What about the case where a user does llvm-objcopy -- with no arguments or input files? What does GNU objcopy do in this case?

This revision now requires changes to proceed.May 18 2021, 1:21 AM
sdmitriev updated this revision to Diff 346086.May 18 2021, 3:03 AM
sdmitriev marked an inline comment as done.

I agree that it would be better to create a single LIT test for a file name beginning with dash, but it is not clear how to write it. I assume such test would require use of %T (directory of a %t), but according to the documentation it is deprecated now. So, I decided to follow the same strategy for testing this patch as was used in a similar llvm-rc change (D56743) - updating few arbitrary tests to use ‘--’ separator for the file names. If you have ideas how to write a test for a file name beginning with ‘-‘, please share.

Is there a particular reason you've singled out llvm-objcopy, and not llvm-strip?

No, no particular reason. I only needed this to be supported in llvm-objcopy, so I’ve changed this tool only. But I can prepare a similar change for llvm-strip as well in a separate patch.

llvm/tools/llvm-objcopy/CopyConfig.cpp
489

GNU objcopy prints help in this case. I have updated this change to match GNU objcopy behavior:

llvm-objcopy -- => print help
llvm-objcopy -- <file> => empty output

I agree that it would be better to create a single LIT test for a file name beginning with dash, but it is not clear how to write it. I assume such test would require use of %T (directory of a %t), but according to the documentation it is deprecated now. So, I decided to follow the same strategy for testing this patch as was used in a similar llvm-rc change (D56743) - updating few arbitrary tests to use ‘--’ separator for the file names. If you have ideas how to write a test for a file name beginning with ‘-‘, please share.

You can use something like mkdir %t in tests (see various cases throughout the testsuites) and then cd into that directory, where you can then use relative names, e.g. to files starting with -, without neeing %t to be involved.

Is there a particular reason you've singled out llvm-objcopy, and not llvm-strip?

No, no particular reason. I only needed this to be supported in llvm-objcopy, so I’ve changed this tool only. But I can prepare a similar change for llvm-strip as well in a separate patch.

Sounds reasonable.

llvm/tools/llvm-objcopy/CopyConfig.cpp
478

Actually, auto doesn't really gain us anything here, as the return type is not obvious from the single line here. Just make the type explicit.

sdmitriev updated this revision to Diff 346106.May 18 2021, 3:46 AM
sdmitriev marked an inline comment as done.

Addressed review comments.

jhenderson added inline comments.May 18 2021, 4:01 AM
llvm/test/tools/llvm-objcopy/objcopy-dash-dash.test
1 ↗(On Diff #346106)

Nit: in newer llvm-objcopy tests, we tend to use ## for comment markers, to make the comments stand out clearly from the test directives.

Also, no need for "objcopy" to be in the name. dash-dash.test would be sufficient. Since the functionality for llvm-strip would be nearly identical, it could live in the same test.

4–5 ↗(On Diff #346106)

I would change the commnet slightly to make it generic - after all, non-dashed files are still valid after this point too. Something like:

"Check that llvm-objcopy correctly treats paths after "--" as file paths, even if they start with dashes."

You would then have a case where the name has a dash as a leading character, and another where it doesn't. I'd make the filenames with a leading dash valid options too.

Also, I recommend you test the following cases:

  1. -- is specified with options but no input files.
  2. -- is specified with input files but no options.
8 ↗(On Diff #346106)

You don't need to check the full file properties. In fact, I'd recommend doing a run without -- and then use cmp to compare this with the output of the different test cases.

10–19 ↗(On Diff #346106)

This can be dramatically simplified. We don't need a section at all, and I believe the Machine is optional too. You can also remove excess padding, and add the --- to properly start the YAML.

sdmitriev updated this revision to Diff 346147.May 18 2021, 5:30 AM
sdmitriev marked an inline comment as done.

Addressed review comments.

jhenderson accepted this revision.May 19 2021, 12:55 AM

LGTM, with one suggestion.

llvm/test/tools/llvm-objcopy/dash-dash.test
13

I'd put the -- before test-obj here. That way, you have coverage for all positional arguments being after the --. It also removes the potential for positional arguments either side of the -- treating the first as an option (in terms of whether the lack of options is an issue).

This revision is now accepted and ready to land.May 19 2021, 12:55 AM
sdmitriev updated this revision to Diff 346355.May 19 2021, 1:06 AM

Applied code review suggestions.

sdmitriev marked an inline comment as done.May 19 2021, 1:09 AM
hubert.reinterpretcast added inline comments.
llvm/test/tools/llvm-objcopy/dash-dash.test
10

This is causing a bot failure:
https://lab.llvm.org/staging/#/builders/126/builds/421/steps/5/logs/FAIL__LLVM__dash-dash_test

If you have some cmp that requires the --, I have found that using env POSIXLY_CORRECT=1 cmp would help.

llvm/test/tools/llvm-objcopy/dash-dash.test
10

Or using cmp -- out-obj1 --only-section would work.