This is an archive of the discontinued LLVM Phabricator instance.

[llvm-nm] add a new option -X to specify the type of object file llvm-nm should examine
ClosedPublic

Authored by DiggerLin on Jan 25 2022, 2:10 PM.

Details

Summary

Added a new option "-X" to specify, which type of object file should be examine.
for example ,

  1. "llvm-nm -X64 archive.a" only deal with the 64bit object files in the archive.a ,ignore the all 32bit object files in the archive.a

2 "llvm-nm -X32 xcoffobj32.o xcoffobj64.o " only deal with the 32bit object file "xcoffobj32.o" , 64bit object file "xcoffobj64.o" will be ignored

Diff Detail

Event Timeline

DiggerLin created this revision.Jan 25 2022, 2:10 PM
DiggerLin requested review of this revision.Jan 25 2022, 2:10 PM
Herald added a project: Restricted Project. · View Herald TranscriptJan 25 2022, 2:10 PM

llvm-nm is a multi-target binary. It doesn't need --triple=aarch64-linux-gnu to work with an aarch64 object file.
Can't llvm-nm infer the bit mode from the object file automatically?

So I just found https://www.ibm.com/docs/en/aix/7.1?topic=n-nm-command

If the intention is to support AIX nm -X, I think it is fine because GNU nm's manpage says -X This option is ignored for compatibility with the AIX version of nm. It takes one parameter which must be the string 32_64. The default mode of AIX nm corresponds to -X 32, which is not supported by GNU nm.
For other short options I would be concerned if there could be a potential conflict with GNU nm.

For the long option --bit-mode, I don't know whether it is necessary.

Can't llvm-nm infer the bit mode from the object file automatically?

llvm-nm also works on archives, which, on AIX, can contain both 32-bit and 64-bit members. The option performs a filtering operation in such cases.

DiggerLin added a comment.EditedJan 28 2022, 12:34 PM

So I just found https://www.ibm.com/docs/en/aix/7.1?topic=n-nm-command

If the intention is to support AIX nm -X, I think it is fine because GNU nm's manpage says -X This option is ignored for compatibility with the AIX version of nm. It takes one parameter which must be the string 32_64. The default mode of AIX nm corresponds to -X 32, which is not supported by GNU nm.
For other short options I would be concerned if there could be a potential conflict with GNU nm.

For the long option --bit-mode, I don't know whether it is necessary.

I will remove long option --bit-mode ,

Since the llvm-nm can dump a 64bit object file without any errror prompt.

bash> llvm-nm option-bit-mode.test.tmp64.o
0000000000000000 D var64

but in aix nm, if we dump symbol of 64bit object file, it will have

bash> nm option-bit-mode.test.tmp64.o
0654-210 option-bit-mode.test.tmp64.o is not valid in the current object file mode.

Use the -X option to specify the desired object mode

I think it maybe better to keep the default -X Value to "any" for llvm-nm , setting default value of -X to "any" will keep llvm-nm current behavior without -X option.

MaskRay added inline comments.Jan 28 2022, 1:00 PM
llvm/test/tools/llvm-nm/XCOFF/option-bit-mode.test
20 ↗(On Diff #403032)

Without FileCheck --match-full-lines, 0000000000000000 D var32 will be matched as well.

llvm/tools/llvm-nm/Opts.td
16

Ensure the space separators are consistently used: 32, 64, (default) any or 32, 64, any (default)

The leading message can be "(AIX specific) Specify the type of object file to examine". Note that other messages are imperative sentences and don't use the third-person singular. The tool is called llvm-nm, so it is unneeded to repeat llvm-nm in individual option messages.

llvm/tools/llvm-nm/llvm-nm.cpp
86

enum class

1630

Avoid a negative function/variable name which holds a boolean that tells of whether we should not do something. Use the positive form: shouldDump

if (BitMode == Any)
  return true;
return isSymbolList64Bit(Obj) ? BitMode == Bit64 : BitMode == Bit32
2173

perhaps use this message

DiggerLin marked 2 inline comments as done.
DiggerLin retitled this revision from [llvm-nm] add a new option --bit-mode to specify the type of object file llvm-nm should examine to [llvm-nm] add a new option -X to specify the type of object file llvm-nm should examine.
DiggerLin edited the summary of this revision. (Show Details)
DiggerLin marked 3 inline comments as done.Jan 31 2022, 11:41 AM
DiggerLin added inline comments.
llvm/tools/llvm-nm/llvm-nm.cpp
1630

thanks

added a new test case

DiggerLin edited the summary of this revision. (Show Details)Jan 31 2022, 12:00 PM
MaskRay added inline comments.Jan 31 2022, 7:24 PM
llvm/docs/CommandGuide/llvm-nm.rst
131

Use imperative sentences like other options

132

Align

133

Can you run ninja docs-llvm-html and inspect whether the .rst renders properly?

Ensure sphinx-build exists. Configure your build with -DLLVM_ENABLE_SPHINX=ON.

llvm/test/tools/llvm-nm/XCOFF/option-X.test
12 ↗(On Diff #404647)

If you use --implicit-check-not={{.}}, the NOT pattern should be unneeded.

25 ↗(On Diff #404647)

It's worth testing the intermediate lines between the [[FILE32]]: stanza and the [[FILE64]]: stanza, to ensure the formatting is intended.

I didn't verify:

# BOTH:        [[FILE32]]:
# BOTH-NEXT:   00000000 D var32
# BOTH-EMPTY:
# BOTH-NEXT:   [[FILE64]]:
# BOTH-NEXT:   0000000000000000 D var64
37 ↗(On Diff #404647)

replace --allow-empty usage with count 0 (there is zero line, i.e. empty)

llvm/test/tools/llvm-nm/option-X-elf-bitcode.test
6 ↗(On Diff #404647)

Because of project layering, llvm tests cannot use clang. You need to use llc to generate the test.

Consider split-file. There are quite few tests in llvm/test/tools/

llvm/tools/llvm-nm/llvm-nm.cpp
86

If Any is not that different from Bit32_64, just remove Any.

DiggerLin updated this revision to Diff 404966.Feb 1 2022, 9:20 AM
DiggerLin marked 8 inline comments as done.
DiggerLin added inline comments.Feb 1 2022, 12:10 PM
llvm/docs/CommandGuide/llvm-nm.rst
133

thanks

llvm/test/tools/llvm-nm/XCOFF/option-X.test
12 ↗(On Diff #404647)

the BIT32-NOT is for
RUN: llvm-nm -X32 %t.a | FileCheck --check-prefixes=BIT32 %s

llvm/test/tools/llvm-nm/option-X-elf-bitcode.test
6 ↗(On Diff #404647)

thanks

llvm/tools/llvm-nm/llvm-nm.cpp
86

yes, we have maybe has to support "the old pre-AIX v5 XCOFF64." later.

Added a new option "-X" to specify,which type of object file should be examined.
For example,

  1. "llvm-nm -X64 archive.a" only deals with the 64-bit object files in archive.a, ignoring all 32-bit object files.

I'm not sure what this second sentence is actually saying. Please fix it.

  1. "llvm-nm -X32 xcoffobj32.o xcoffobj64.o " only deal with the 32bit , 64bit object files input from command line
llvm/docs/CommandGuide/llvm-nm.rst
131
jhenderson added inline comments.Feb 2 2022, 12:48 AM
llvm/docs/CommandGuide/llvm-nm.rst
132

Use the proper-noun form to make it stand out that this is a specific file format and doesn't simply mean "large regular archives".

135–141

In all cases: "Processes" -> "Process"

Also "all of" -> "all"

llvm/test/tools/llvm-nm/XCOFF/option-X.test
1 ↗(On Diff #404966)

This comment can be simplified, since you are a) inside the llvm-nm testsuite, and b) inside the XCOFF subdirectory.

4–5 ↗(On Diff #404966)

Here and for all references, delete _xcoff from the file names: we're in the XCOFF subdirectory, so there's no need to provide that disambiguation.

10–12 ↗(On Diff #404966)

You should be able to do this here too. You also will need to add an extra check for a filename, I believe (which will also help show that only the desired object is dumped). Prefer the catch-all --implicit-check-not={{.}} to a pattern that could go stale if the test is modified slightly

14–17 ↗(On Diff #404966)

Same sort of comments as above.

19–21 ↗(On Diff #404966)

These lines are all very long. Split them over multiple lines as shown.

llvm/test/tools/llvm-nm/option-X-elf-bitcode.test
30–34 ↗(On Diff #404966)

Split all these lines into two lines each. Same below.

6 ↗(On Diff #404647)

You've marked as done, but you haven't used split-file to provide multiple inputs within the file instead of using echo. Please adderss this point.

llvm/tools/llvm-nm/Opts.td
16

Marked as done, but not addressed?

llvm/tools/llvm-nm/llvm-nm.cpp
1630

Obvious question: why does it only work for those kinds of files, and not COFF or Mach-O files (for example)?

2216
DiggerLin edited the summary of this revision. (Show Details)Feb 2 2022, 6:19 AM
DiggerLin marked an inline comment as done.

Added a new option "-X" to specify,which type of object file should be examined.
For example,

  1. "llvm-nm -X64 archive.a" only deals with the 64-bit object files in archive.a, ignoring all 32-bit object files.

I'm not sure what this second sentence is actually saying. Please fix it.

  1. "llvm-nm -X32 xcoffobj32.o xcoffobj64.o " only deal with the 32bit , 64bit object files input from command line
it should be 
  "llvm-nm -X32  xcoffobj32.o xcoffobj64.o " only deal with the 32bit object file "xcoffobj32.o" ,  64bit object file "xcoffobj64.o" will be ignored.
llvm/tools/llvm-nm/llvm-nm.cpp
1630

based the our requirement , we need the the option for XCOFF, llvmbit code and ELF object file, I am not sure about the whether it is COFF and Mach-O need the functionality or not, if the COFF and Mach-O need the functionality, we can a new separate patch later.

DiggerLin edited the summary of this revision. (Show Details)Feb 2 2022, 6:21 AM
DiggerLin marked 8 inline comments as done.Feb 2 2022, 10:22 AM
DiggerLin added inline comments.
llvm/docs/CommandGuide/llvm-nm.rst
132

we can create gnu archive file which contain xcoff object file, bit code, elf object file, etc with llvm-ar technically.(it is perfectly valid, just not particularly useful) .
all the llvm tools can support decode the xcoff object file in gnu archive(if there is) or elf object file in big archive(if there is) now.
so we do not need to a specific archive format here.

llvm/tools/llvm-nm/Opts.td
16

-X is not only AIX specific now. it work for llvm bit code, elf object file,xcoff object file from gnu archive and big archive or from command line input .

DiggerLin updated this revision to Diff 405400.Feb 2 2022, 12:53 PM
DiggerLin marked 6 inline comments as done.
jhenderson added inline comments.Feb 2 2022, 11:44 PM
llvm/test/tools/llvm-nm/XCOFF/option-X.test
9–15 ↗(On Diff #405400)

Possible simplification, as it removes the duplicate 00000000 D var32.

20–23 ↗(On Diff #405400)
45–48 ↗(On Diff #405400)

Indent to align.

I'd also change ARCHIVE to ARCBOTH or something equivalent, so that it is consistent with the 32-bit and 64-bit cases.

llvm/test/tools/llvm-nm/option-X-elf-bitcode.test
1 ↗(On Diff #405400)

Nit: the file format should be all caps.

It's not clear to me why this test contains test cases for two formats (ELF and bitcode), but not all formats supported by this option. In other words, could you please explain why your XCOFF case is in a separate test file to these two (and why you've kept ELF and bitcode in the same one)?

3–4 ↗(On Diff #405400)

These files don't exist. Did you forget to add them?

llvm/tools/llvm-nm/llvm-nm.cpp
1630–1631

I think you can simplify this comment, and also clarify that the option isn't fundamentally impossible with other formats, just isn't implemented.

1632–1634

It's fine not to add support for COFF and Mach-O in this patch, but something to consider is what might happen if a user attempted to use that option with an object in that format. The option would be silently ignored, but it wouldn't be clear why to them. I'd consider adding a warning here, if the option has been specified, but the input object is not a supported format.

Alternatively, just add support for all supported formats up-front.

2216

Please add a test-case for this.

DiggerLin marked 9 inline comments as done.Feb 3 2022, 1:10 PM
DiggerLin added inline comments.
llvm/test/tools/llvm-nm/option-X-elf-bitcode.test
1 ↗(On Diff #405400)

As I mention before, the -X only support for XCOFF, bitcode, ELF object file, not support for other object file in the patch.
xcoff is test on the llvm/test/tools/llvm-nm/XCOFF/option-X.test,

you suggestion moving the content of test llvm/test/tools/llvm-nm/XCOFF/option-X.test into the file too?

3–4 ↗(On Diff #405400)

the two files are in the patch "--export-symbols" ,

llvm/tools/llvm-nm/llvm-nm.cpp
1632–1634

I think we have some description in the command guide.

.. option:: -X

Specify the type of XCOFF object file, ELF object file, or IR object file input
from command line or from archive files that llvm-nm should examine.

if I change the code to

 if (!isa<XCOFFObjectFile>(Obj) && !isa<ELFObjectFileBase>(Obj) &&
      !isa<IRObjectFile>(Obj)) {
    if(BitMode !=BitModeTy::Any)
     error("-X option is currently only implemented for XCOFF, ELF, and IR object files");
    return true;
}

it will print out a warning info for each object files in a archive file, there many a lot of same warning print out. It do not look reasonable.

DiggerLin updated this revision to Diff 405763.Feb 3 2022, 1:14 PM
DiggerLin marked 3 inline comments as done.
MaskRay added inline comments.Feb 3 2022, 1:57 PM
llvm/test/tools/llvm-nm/option-X-invalid-arg.test
1 ↗(On Diff #405763)

Not necessary to use a separate file for errors. It can be placed at the end of option-X.test. I usually place error tests at the end.

I'd avoid -invalid-arg in the filename since I may test more than one types of errors and -invalid-arg restricts the types of errors the file can test.

5 ↗(On Diff #405763)
7 ↗(On Diff #405763)

It's not necessary to test the filename part.

Is there a stray colon in : : ?

llvm/tools/llvm-nm/Opts.td
16

Imperative: Specifies -> Specify

The value must be one of : => The value must be one of:

llvm/tools/llvm-nm/llvm-nm.cpp
2205–2206

Remove // XCOFF specific options. since it's no longer XCOFF specific.

jhenderson added inline comments.Feb 4 2022, 12:31 AM
llvm/test/tools/llvm-nm/option-X-elf-bitcode.test
1 ↗(On Diff #405400)

I'm suggesting one of either:

Putting the XCOFF case in this test.

OR

Splitting the bitcode and ELF cases into separate files.

I don't have a particular preference as to which.

3–4 ↗(On Diff #405400)

This patch doesn't need to rely on the --export-symbols patch though, so it shouldn't really be rebased on top of it. Preferable would be making it an independent patch, and just have the new test inputs duplicated between the two patches (obviously when landing the two patches, you only need one version of the test inputs, so the second-to-land patch won't have them in).

llvm/tools/llvm-nm/llvm-nm.cpp
1632–1634

People won't routinely look at the command guide, so documenting the limitation there is insufficient.

I do agree with your comment about not wanting to duplicate the error multiple times though. I don't have a good solution, but a small improvement would be to add the limitation to the --help text.

DiggerLin updated this revision to Diff 405982.Feb 4 2022, 8:37 AM
DiggerLin marked 6 inline comments as done.
DiggerLin added inline comments.
llvm/test/tools/llvm-nm/option-X-invalid-arg.test
7 ↗(On Diff #405763)

yes, there is ": : " output

jhenderson added inline comments.Feb 7 2022, 1:05 AM
llvm/test/tools/llvm-nm/option-X.test
8

A thought: I wonder if it would be simpler for all positive inptus to be tested in a single set of llvm-nm invocations. In other words, ratehr than having near-identical bitcode, ELF, and XCOFF cases, you could have them all combined into one, e.g.

# RUN: llvm-nm --format=just-symbols -X32 %t32.bc %t64.bc %t32.elf %t64.elf %t_xcoff.a | \
# RUN:   FileCheck %s ...

Not sure how easy it would be to craft the FileCheck line, so this may not work.

54

The Machine key probably isn't needed for this test case. You should be able to omit it.

58

Flags aren't important for this test case. Delete them.

60–61

The default binding is Local. This is sufficient for this test case, so delete the explicit binding and rename the symbol.

64

You don't need a second YAML document here. You can use -D YAML macros to configure the Class field for the specific variety you want. There are plenty of examples in other ELF tests. You don't need a unique symbol name either, though you can if you want (if you want one, I'd reuse the Class macro in the symbol name, so you don't need an additional macro).

81

No need for the r in -rf, since %t.a isn't a directory.

126

Nit: no double blank line.

131

Similar comment to the two ELF inputs. I think you can reduce duplication by using a single YAML input for XCOFF and using yaml2obj's -D option to customise it accordingly.

134–139

Do you actually need all these fields? Do any of them have defaults that are sufficient for our needs?

141–144

Why do you have two sections? Won't one do, like the ELF case?

148–153

Same comment as above: are all of these required fields, or are the defaults sufficient?

DiggerLin marked 12 inline comments as done.Feb 7 2022, 6:44 AM
DiggerLin added inline comments.
llvm/test/tools/llvm-nm/option-X.test
8

I think a separate test , it is easy to understand, and if someone want to add a new object format support support in some day, for example, MachO support, they just need to add one new test line, do not need to change current test line.

64

thanks.

134–139

thanks

141–144

thanks

148–153

thanks

DiggerLin updated this revision to Diff 406438.Feb 7 2022, 6:48 AM
DiggerLin marked 5 inline comments as done.
jhenderson added inline comments.Feb 8 2022, 12:28 AM
llvm/test/tools/llvm-nm/option-X.test
58

Ping. You've not deleted the flag field, despite this being marked as done. Please don't mark things as done unless you've uploaded a diff with the comment addressed.

59

As suggested in my earlier comment, you can reuse the class macro, to avoid needing an extra variable. The fact that the symbol is both "global" and "data" is irrelevant to the test.

60–61

Ping. You've not deleted the binding field, despite this being marked as done. Please don't mark things as done unless you've uploaded a diff with the comment addressed.

DiggerLin marked 3 inline comments as done.Feb 8 2022, 7:25 AM
DiggerLin updated this revision to Diff 406821.Feb 8 2022, 7:33 AM
DiggerLin updated this revision to Diff 406834.Feb 8 2022, 8:08 AM

reduce test case

jhenderson accepted this revision.Feb 9 2022, 1:08 AM

Looks good, barring a couple of suggestions. You might want to confirm with @MaskRay too.

llvm/test/tools/llvm-nm/option-X.test
63–65

Here and below for the 64-bit equivalent, I'd make the BIT32 prefix XCOFF32, since it's specifically testing XCOFF, and not other formats (ARC32 can stay the same if you want, or change it as you feel is appropriate).

67

FWIW, in this particular case, I'd be okay if you wanted to use a second -D option to name the variable, so that it is a little more explicit. Up to you either way, whichever you prefer.

105

I'm pretty sure the double colon is a bug, but I guess it's a bug elsewhere in the code not touched by this patch. You might want to fix it in a separate patch, if you have time.

This revision is now accepted and ready to land.Feb 9 2022, 1:08 AM
MaskRay accepted this revision.Feb 10 2022, 2:48 PM

Looks great!

llvm/tools/llvm-nm/Opts.td
16

Just to keep the binary formats sorted alphabetically.

DiggerLin marked 4 inline comments as done.Feb 15 2022, 6:48 AM
DiggerLin added inline comments.
llvm/test/tools/llvm-nm/option-X.test
63–65

thanks, I keep ARC as it is.

67

I keep as it. thanks

105

yes, I think so. I will do it when I am available.

llvm/tools/llvm-nm/Opts.td
16

thanks