This is an archive of the discontinued LLVM Phabricator instance.

[llvm-readobj] Fix "Section" output when emitting relocations in JSON
ClosedPublic

Authored by paulkirth on Oct 31 2022, 10:52 AM.

Details

Summary

Prior to this patch, the JSON output would emit an invalid key from the
shared LLVM implementation. This caused llvm-readobj to output invalid
JSON. This patch introduces a small helper function to print the
relocation information differently between the LLVM and JSON formats.

Before this patch:

"Relocations": [Section (2) .rel.text {

  {
    "Relocation": {
      "Offset": 0,
      "Type": {
        "Value": "R_X86_64_NONE",
        "RawValue": 0
      },
      "Symbol": {
        "Value": "rel_0",
        "RawValue": 1
      }
    }
  },
  ...

After this patch:

"Relocations": [
   {
     "SectionIdx": 2,
     "Relocs": [
       {
         "Relocation": {
           "Offset": 0,
           "Type": {
             "Name": "R_X86_64_NONE",
             "Value": 0
           },
           "Symbol": {
             "Name": "rel_0",
             "Value": 1
           }
         }
       },
       ...

Diff Detail

Event Timeline

paulkirth created this revision.Oct 31 2022, 10:52 AM
Herald added a project: Restricted Project. · View Herald Transcript
paulkirth requested review of this revision.Oct 31 2022, 10:52 AM
Herald added a project: Restricted Project. · View Herald TranscriptOct 31 2022, 10:52 AM
jhenderson requested changes to this revision.Nov 1 2022, 1:33 AM

Please provide before and after examples, and test cases for the fix. Also, please fix the title to use "llvm-readobj" in the commit message/patch description, not simply "readobj".

This revision now requires changes to proceed.Nov 1 2022, 1:33 AM
paulkirth updated this revision to Diff 483651.Dec 16 2022, 1:25 PM
paulkirth retitled this revision from [readobj] Emit valid JSON when emitting Relocations to [llvm-readobj] Fix "Section" output when emitting relocations in JSON.
paulkirth edited the summary of this revision. (Show Details)

Rebase

  • Update summary
  • Add tests
  • Simplify implementation to avoid code duplication
paulkirth updated this revision to Diff 484087.Dec 19 2022, 2:48 PM

Rebase to fix patch application

aidengrossman added inline comments.
llvm/tools/llvm-readobj/ELFDumper.cpp
6811

If this section is already getting moved around, it might be worth it to transition to a DictScope here as well (similar to what I did in D143310, but with proper string formatting logic) to leave all the formatting in a centralized place and simplify this function.

paulkirth updated this revision to Diff 496256.Feb 9 2023, 3:19 PM

Address comments

  • Use scoped dict to simplify implementation and be more consistent
paulkirth marked an inline comment as done.Feb 9 2023, 3:21 PM
jhenderson added inline comments.Mar 3 2023, 12:44 AM
llvm/tools/llvm-readobj/ELFDumper.cpp
6808

I wonder if this would be better named printRelocationSectionInfo, since it's not printing any info about the relocations themselves.

7735

Any particular reason we don't just name this key "SectionIndex" or similar?

paulkirth added inline comments.Mar 3 2023, 9:27 AM
llvm/tools/llvm-readobj/ELFDumper.cpp
6808

That's a good point. I'll update this to reflect.

7735

No, I don't recall any a particular reason. I just went w/ the Idx suffix since it was still clear, and is a pretty common way to shorten or identify index variables. I don't have a preference here so I can change this when I address the other comments.

paulkirth updated this revision to Diff 502232.Mar 3 2023, 12:55 PM
paulkirth marked 2 inline comments as done.

Rebase, address comments, and update tests.

paulkirth updated this revision to Diff 502237.Mar 3 2023, 1:15 PM

Move test code into existing test file

paulkirth updated this revision to Diff 502326.Mar 3 2023, 6:04 PM

Fix bad rebase between two stacked diffs. I accidentally merged the Group section commit into this one, this commit should restore the correct split between the two.

paulkirth updated this revision to Diff 502700.Mar 6 2023, 10:24 AM

Fix formatting

jhenderson accepted this revision.Mar 17 2023, 1:18 AM

Looks good, with one nit.

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

Nit: blank line between this and the next function.

This revision is now accepted and ready to land.Mar 17 2023, 1:18 AM
paulkirth updated this revision to Diff 506234.Mar 17 2023, 5:07 PM
paulkirth marked an inline comment as done.

Add missing blank line between functions.

paulkirth updated this revision to Diff 506244.Mar 17 2023, 5:27 PM

Rebase and remove const qualifier from StringRef.

paulkirth updated this revision to Diff 506247.Mar 17 2023, 5:33 PM

Add back funcion removed when rebasing

paulkirth updated this revision to Diff 506249.Mar 17 2023, 5:42 PM

Add missing blank line.

This revision was landed with ongoing or failed builds.Mar 17 2023, 6:36 PM
This revision was automatically updated to reflect the committed changes.