This is an archive of the discontinued LLVM Phabricator instance.

[RISCV] Support RISC-V ELF attribute section in llvm-readobj
ClosedPublic

Authored by HsiangKai on Mar 8 2020, 7:49 PM.

Details

Summary

Enable llvm-readobj to handle RISC-V ELF attribute section.

Diff Detail

Event Timeline

HsiangKai created this revision.Mar 8 2020, 7:49 PM

This patch is depended on D74023.

MaskRay added inline comments.Mar 8 2020, 9:51 PM
llvm/test/tools/llvm-readobj/ELF/RISCV/unknown-arch-attr.test
44 ↗(On Diff #249023)

unittests/Support/RISCVAttributeParser.cpp is more suitable for such tests. See unittests/Support/ARMAttributeParser.cpp for examples.

HsiangKai marked an inline comment as done.Mar 9 2020, 6:41 AM
HsiangKai added inline comments.
llvm/test/tools/llvm-readobj/ELF/RISCV/unknown-arch-attr.test
44 ↗(On Diff #249023)

In current implementation, it will ignore unrecognized extensions. It intends to skip 'x1p0' and recognizes 'm2p0' and decodes mul instruction correctly. So, I think it is appropriate to create this test case.

jhenderson added inline comments.Mar 11 2020, 2:17 AM
llvm/test/MC/RISCV/attribute-with-option.s
2

It's not clear what this comment is trying to say. Could you turn it into a complete sentence please, describing what the test is testing.

8

'##'

jhenderson added inline comments.Mar 11 2020, 2:17 AM
llvm/test/tools/llvm-readobj/ELF/RISCV/attribute.s
2

to decode -> can decode

I think the canoncial name is "RISC-V", so you should use that in comments instead of "RISCV".

llvm/test/tools/llvm-readobj/ELF/RISCV/unknown-arch-attr.test
7 ↗(On Diff #249023)

Is this test an llvm-objdump or llvm-readobj test? It looks to me like this test is testing llvm-objdump primarily, in which case it should be in the llvm-objdump directory, and maybe belongs in a separate change?

38–39 ↗(On Diff #249023)

'##'

Same below.

llvm/tools/llvm-readobj/ELFDumper.cpp
2709

Is there a test case for this line?

2709

Is there a test case for this line?

2740

This is an existing mistake in the ARM version, but we shouldn't replicate it (and perhaps you could fix it separately). Unless E is going to be a duplicate of an already-reported error, this should report the error, probably as a warning, as there's no particular reason to terminate the program.

HsiangKai marked 3 inline comments as done.Mar 12 2020, 12:47 AM
HsiangKai added inline comments.
llvm/tools/llvm-readobj/ELFDumper.cpp
2709

No, it is not related to this patch. Originally, printAttributes() is only implemented for ELFDumper<ELF32LE>. So, I check Obj->isLE() to keep original logic. In this way, it does not change the functionality of original version. So, I did not add a test case for it.

2709

No, it is not related to this patch.

2740

We could fix it separately.

HsiangKai updated this revision to Diff 249850.Mar 12 2020, 1:01 AM
jhenderson added inline comments.Mar 16 2020, 2:48 AM
llvm/test/MC/RISCV/attribute-with-option.s
2

When a user specifies an architecture extension which conflicts with an arhitecture attribute, we use the architecture attribute instead of the command line option.

5

specify the "e" extension

6

arch -> architecture
specify -> specifying
So, we -> This means we

13

RV32E. x16 -> RV32E, because x16

14

assemble. So, it -> assemble, since it

17

Check that the architecture attribute is not overridden by the command line option.

llvm/tools/llvm-readobj/ELFDumper.cpp
2709

The !Obj->isLE() line is new, so I think we need a test case that exercises this line as part of this change. You may be adding it to preserve existing behaviour, in which case, consider adding a test for it that in a prerequisite patch, to show that there is no behaviour change.

2709

Yes it is. The behaviour has gone from "not EM_ARM" to "not EM_ARM and not EM_RISCV", which means the code here is now exercised differently. Again, you could add a prerequisite test that shows that non-ARM architectures hit this. Then, in this patch, you would remove the EM_RISCV case.

2740

If you want to fix the ARM case separately, that's fine, but we shouldn't introduce the same mistake with new code for RISC-V.

  • Print out error messages if parsing attribute section error.
  • Add more tests for invalid values in attribute section.
  • Refine comments.
HsiangKai marked 2 inline comments as done.Mar 18 2020, 11:52 PM
HsiangKai added inline comments.
llvm/tools/llvm-readobj/ELFDumper.cpp
2709

I add a test, validate-attr-section.test, for it.

2740

Print error messages out and add test cases for it.

HsiangKai marked an inline comment as done.Mar 18 2020, 11:57 PM
HsiangKai added inline comments.
llvm/tools/llvm-readobj/ELFDumper.cpp
2709

I found printAttributes only called under EM_ARM and EM_RISCV. I add an assertion in printAttributes to ensure it.

jhenderson added inline comments.Mar 20 2020, 2:58 AM
llvm/test/MC/RISCV/attribute-with-option.s
7

Sorry, missed one bit in my last comment: "to specifying" -> "to specify".

18

overrided -> overridden

llvm/test/tools/llvm-readobj/ELF/RISCV/invalid-attr-size.test
6 ↗(On Diff #251292)

Here and in the other tests, I'd add the full context for this message, i.e. something like {{.*}}.o: error: invalid subsection length (or whatever it is).

llvm/test/tools/llvm-readobj/ELF/RISCV/invalid-attr-subsection-size.test
1 ↗(On Diff #251292)

It's entirely up to you, but you might want to fold all these "invalid-attr" tests into a single test file, with lots of yaml docs. I'm happy either way.

I am a bit confused by "invalid-attr-size.test" and "invalid-attr-subsection-size.test" given that the former appears to emit an error to do with the subsection size, whilst the latter is an error to do with the attribute size itself. Did you switch them around by accident?

llvm/test/tools/llvm-readobj/ELF/RISCV/invalid-attr-vendor.test
6 ↗(On Diff #251292)

Probably should be "vendor name" without the hyphen (unless it is called "vendor-name" in the section specification).

llvm/test/tools/llvm-readobj/ELF/RISCV/validate-attr-section.test
2

"attribute section printing"

llvm/tools/llvm-readobj/ELFDumper.cpp
2734–2735

Can we remove this TODO (or at least part of it)?

HsiangKai updated this revision to Diff 252301.Mar 24 2020, 6:35 AM
HsiangKai updated this revision to Diff 252563.Mar 25 2020, 6:52 AM

Latest veresion basically looks fine. One suggestion and one question remaining from me.

llvm/test/tools/llvm-readobj/ELF/RISCV/invalid-attr-section-size.test
7

If you want to be more precise with your check (I don't think it matters too much, but it might be good), you can use something like FileCheck -DFILE=%t and change the pattern here to error: '[[FILE]].{{64|32]}.o':.

Same would apply in other tests.

19

Is the size 32-bits even for ELF64?

HsiangKai marked an inline comment as done.Mar 26 2020, 2:57 AM
HsiangKai added inline comments.
llvm/test/tools/llvm-readobj/ELF/RISCV/invalid-attr-section-size.test
19
HsiangKai updated this revision to Diff 252800.Mar 26 2020, 4:26 AM
jhenderson accepted this revision.Mar 27 2020, 1:56 AM

LGTM. Please wait for @MaskRay to approve too, since I don't know very much about the details here.

This revision is now accepted and ready to land.Mar 27 2020, 1:56 AM
MaskRay added inline comments.Mar 28 2020, 5:57 PM
llvm/lib/Support/ELFAttributeParser.cpp
132

I use vendor-name because

<format-version>
[ <section-length> "vendor-name"
      [ <file-tag> <size> <attribute>*
      | <section-tag> <size> <section-number>* 0 <attribute>*
      | <symbol-tag> <size> <symbol-number>* 0 <attribute>*
      ]+
]*

I hope this change can be dropped.

150

subsection is not very appropriate. The original attribute seems to capture the collection (file/section/symbol) better.

220

subsection->section is fine, but length->size is not appropriate.

MaskRay requested changes to this revision.Mar 28 2020, 5:57 PM
This revision now requires changes to proceed.Mar 28 2020, 5:57 PM
MaskRay added a comment.EditedMar 28 2020, 6:01 PM

Consider grouping some llvm-readobj/ELF/RISCV/invalid-*.test tests together. We can make Content a template.

--- !ELF
FileHeader:
  Class:   ELFCLASS[[BITS]]
  Data:    ELFDATA2LSB
  Type:    ET_REL
  Machine: EM_RISCV
Sections:
  - Name:    .riscv.attributes
    Type:    SHT_RISCV_ATTRIBUTES
    Content: [[CONTENT]]

Actually, if we decide to use llvm/unittests a canonical place to test attributes, we should delete most duplicate tests in test/llvm-readobj/ELF/RISCV. The minimum test that ensures llvm-redobj forwards RISCVAttributeParser errors is sufficient.

MaskRay added inline comments.Mar 28 2020, 6:11 PM
llvm/test/tools/llvm-readobj/ELF/RISCV/invalid-attr-version.test
7

These diagnostics should start with error: or warning:

HsiangKai updated this revision to Diff 253582.Mar 30 2020, 6:43 AM
MaskRay added inline comments.Mar 30 2020, 3:43 PM
llvm/test/tools/llvm-readobj/ELF/RISCV/attribute.s
2

This should probably go to test/MC/RISCV or test/MC/ELF/RISCV. This is more about a test for assembly.

llvm/test/tools/llvm-readobj/ELF/RISCV/invalid-attr-section-size.test
2

We can have one single invalid-*.text. You can find plent of --docnum= examples in llvm-readobj/ELF/

As I mentioned, we don't need many tests testing invalid section contents. We just need to show llvm-readobj forwards RISCVAttributeParser diagnostics, which are already tested by llvm/unittests/

llvm/test/tools/llvm-readobj/ELF/RISCV/validate-attr-section.test
2

This can be merged into section-types.test, I think.

HsiangKai marked 3 inline comments as done.Mar 30 2020, 8:14 PM
HsiangKai added inline comments.
llvm/test/tools/llvm-readobj/ELF/RISCV/attribute.s
2

It is not used to test assembly. It is used to test readelf/readelf to decode attribute section correctly.

llvm/test/tools/llvm-readobj/ELF/RISCV/invalid-attr-section-size.test
2

That's why I removed other invalid-attr*.test.

invalid-attr-section-size.test is used to test the error messages are handled by llvm-readobj. As I mentioned in the test case comments.
invalid-attr-version.test is used to test the format version is checked in llvm-readobj.

They are two different test cases.

llvm/test/tools/llvm-readobj/ELF/RISCV/validate-attr-section.test
2

These two test cases are used for different purpose. I prefer to keep these two test cases.

MaskRay accepted this revision.Mar 30 2020, 10:40 PM

Thanks!

This revision is now accepted and ready to land.Mar 30 2020, 10:40 PM
jhenderson requested changes to this revision.Mar 31 2020, 12:53 AM

Sorry, I spotted the error handling is not using llvm-readobj's style. Please fix it.

llvm/test/tools/llvm-readobj/ELF/RISCV/invalid-attr-version.test
2

Let's add a top-level comment to this file explaining why this error exists.

llvm/tools/llvm-readobj/ELFDumper.cpp
2725–2729

Please use llvm-readobj's reportError function rather than hand-rolling your own error handling.

This revision now requires changes to proceed.Mar 31 2020, 12:53 AM
HsiangKai updated this revision to Diff 254096.Apr 1 2020, 12:09 AM

Use reportError() to handle error messages.

jhenderson added inline comments.Apr 1 2020, 12:34 AM
llvm/test/tools/llvm-readobj/ELF/RISCV/invalid-attr-version.test
1

Delete "ELF" - the test is in the ELF directory, so it's obvious that this is testing ELF.

2

section -> sections

llvm/tools/llvm-readobj/ELFDumper.cpp
2726

Thinking about this, this should probably be reportWarning. In general, we don't want the tool to stop processing other options as soon as it sees something bad, as it might well be able to perform those options without any problems.

HsiangKai updated this revision to Diff 254125.Apr 1 2020, 2:15 AM
jhenderson accepted this revision.Apr 1 2020, 2:28 AM

LGTM, with two more reportError -> reportWarning changes.

llvm/tools/llvm-readobj/ELFDumper.cpp
2738

As with the case you've modified, please change this to reportWarning and then return/continue as appropriate.

2741

Ditto.

This revision is now accepted and ready to land.Apr 1 2020, 2:28 AM
HsiangKai updated this revision to Diff 254168.Apr 1 2020, 5:03 AM
jhenderson accepted this revision.Apr 1 2020, 5:32 AM
This revision was automatically updated to reflect the committed changes.
Via Web