Skip to content

[RISCV] Basic Objdump Mapping Symbol Support #151452

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions llvm/docs/ReleaseNotes.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@ Changes to the PowerPC Backend
Changes to the RISC-V Backend
-----------------------------

* `llvm-objdump` now has basic support for switching between disassembling code
and data using mapping symbols such as `$x` and `$d`. Switching architectures
using `$x` with an architecture string suffix is not yet supported.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor nit: single blank line, please.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done


Changes to the WebAssembly Backend
----------------------------------

Expand Down
2 changes: 2 additions & 0 deletions llvm/lib/Target/RISCV/MCTargetDesc/RISCVAsmBackend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,8 @@ bool RISCVAsmBackend::writeNopData(raw_ostream &OS, uint64_t Count,
Count -= 1;
}

// TODO: emit a mapping symbol right here

if (Count % 4 == 2) {
// The canonical nop with Zca is c.nop.
OS.write(STI->hasFeature(RISCV::FeatureStdExtZca) ? "\x01\0" : "\0\0", 2);
Expand Down
20 changes: 20 additions & 0 deletions llvm/test/MC/Disassembler/RISCV/riscv-mapping-symbols.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# RUN: llvm-mc --triple=riscv32-unknown-none-elf %s -filetype=obj -o - \
# RUN: | llvm-objdump -dr - \
# RUN: | FileCheck %s
# RUN: llvm-mc --triple=riscv64-unknown-none-elf %s -filetype=obj -o - \
# RUN: | llvm-objdump -dr - \
# RUN: | FileCheck %s


# CHECK: 00000013 nop
nop

# CHECK-NEXT: 55 55 55 55 .word 0x55555555
.word 0x55555555

# CHECK-NEXT: 00 00 00 00 .word 0x00000000
# CHECK-NEXT: R_RISCV_32 foo
.word foo

# CHECK-NEXT: 00000013 nop
nop
29 changes: 0 additions & 29 deletions llvm/test/MC/RISCV/large-instructions.s

This file was deleted.

88 changes: 88 additions & 0 deletions llvm/test/MC/RISCV/large-instructions.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
# RUN: yaml2obj -o - %s \
# RUN: | llvm-objdump -d - | FileCheck %s
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# RUN: yaml2obj -o - %s \
# RUN: | llvm-objdump -d - | FileCheck %s
# RUN: yaml2obj -o %t
# RUN: llvm-objdump -d %t | FileCheck %s

This is more normal in the tests I've seen and means the intermediate object can be inspected, if there's a failure.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done


## This CHECKs objdump's handling of wide instruction encodings, and how it
## groups the instruction bytes when disassembling.
##
## This is written in YAML because using `.byte` emits the wrong mapping
## symbols.

--- !ELF
FileHeader:
Class: ELFCLASS32
Data: ELFDATA2LSB
Type: ET_REL
Machine: EM_RISCV
SectionHeaderStringTable: .strtab
Sections:
- Name: .text.six_byte
Type: SHT_PROGBITS
Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
AddressAlign: 0x1
Content: 1F0123456789
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You might want to consider ContentArray instead, but I suspect it's down to personal preference with no real advantages either way in this case.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there docs on ContentArray - I'm happy to use something less inscrutable, to be honest.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's much better, thanks.

# CHECK: 011f 4523 8967 <unknown>
- Name: .text.eight_byte
Type: SHT_PROGBITS
Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
AddressAlign: 0x1
Content: 3F0123456789ABCD
# CHECK: 4523013f cdab8967 <unknown>
- Name: .text.ten_byte
Type: SHT_PROGBITS
Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
AddressAlign: 0x1
Content: 7F0023456789ABCDEFFE
# CHECK: 007f 4523 8967 cdab feef <unknown>
- Name: .text.twelve_byte
Type: SHT_PROGBITS
Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
AddressAlign: 0x1
Content: 7F1023456789ABCDEFFEDCBA
# CHECK: 4523107f cdab8967 badcfeef <unknown>
- Name: .text.fourteen_byte
Type: SHT_PROGBITS
Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
AddressAlign: 0x1
Content: 7F2023456789ABCDEFFEDCBA9876
# CHECK: 207f 4523 8967 cdab feef badc 7698 <unknown>
- Name: .text.sixteen_byte
Type: SHT_PROGBITS
Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
AddressAlign: 0x1
Content: 7F3023456789ABCDEFFEDCBA98765432
# CHECK: 4523307f cdab8967 badcfeef 32547698 <unknown>
- Name: .text.eighteen_byte
Type: SHT_PROGBITS
Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
AddressAlign: 0x1
Content: 7F4023456789ABCDEFFEDCBA987654321012
# CHECK: 407f 4523 8967 cdab feef badc 7698 3254 1210 <unknown>
- Name: .text.twenty_byte
Type: SHT_PROGBITS
Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
AddressAlign: 0x1
Content: 7F5023456789ABCDEFFEDCBA9876543210123456
# CHECK: 4523507f cdab8967 badcfeef 32547698 56341210 <unknown>
- Name: .text.twentytwo_byte
Type: SHT_PROGBITS
Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
AddressAlign: 0x1
Content: 7F6023456789ABCDEFFEDCBA9876543210123456789A
# CHECK: 607f 4523 8967 cdab feef badc 7698 3254 1210 5634 9a78 <unknown>
- Type: SectionHeaderTable
Sections:
- Name: .strtab
- Name: .text.six_byte
- Name: .text.eight_byte
- Name: .text.ten_byte
- Name: .text.twelve_byte
- Name: .text.fourteen_byte
- Name: .text.sixteen_byte
- Name: .text.eighteen_byte
- Name: .text.twenty_byte
- Name: .text.twentytwo_byte
- Name: .symtab
Symbols:
- Name: '$x'
Section: .text.six_byte
...
13 changes: 5 additions & 8 deletions llvm/test/MC/RISCV/nop-slide.s
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,15 @@
auipc a0, 0

# CHECK-RVC-NORELAX: 0000000000000000 <.text>:
# CHECK-RVC-NORELAX-NEXT: 0: 0000 unimp
# CHECK-RVC-NORELAX-NEXT: 2: 0001 nop
# CHECK-RVC-NORELAX-NEXT: 0: 00 00 01 00 .word 0x00010000
# CHECK-RVC-NORELAX-NEXT: 4: 00000517 auipc a0, 0x0

# CHECK-RVC-RELAX: 0000000000000000 <.text>:
# CHECK-RVC-RELAX-NEXT: 0: 0001 nop
# CHECK-RVC-RELAX-NEXT: 2: 0100 addi s0, sp, 0x80
# CHECK-RVC-RELAX-NEXT: 4: 1700 addi s0, sp, 0x3a0
# CHECK-RVC-RELAX-NEXT: 6: 0005 c.nop 0x1
# CHECK-RVC-RELAX-NEXT: 8: 00 <unknown>
# CHECK-RVC-RELAX-NEXT: 2: 00 01 .short 0x0100
# CHECK-RVC-RELAX-NEXT: 4: 00 .byte 0x00
# CHECK-RVC-RELAX-NEXT: 5: 00000517 auipc a0, 0x0

# CHECK: 0000000000000000 <.text>:
# CHECK-NEXT: 0: 0000 <unknown>
# CHECK-NEXT: 2: 0000 <unknown>
# CHECK-NEXT: 0: 00 00 00 00 .word 0x00000000
# CHECK-NEXT: 4: 00000517 auipc a0, 0x0
24 changes: 12 additions & 12 deletions llvm/test/MC/RISCV/rvv/vsetvl-invalid.s
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,37 @@
# RUN: | llvm-objdump -d --mattr=+v - | FileCheck %s

# CHECK: vsetvli a1, a0, e64, m1, tu, mu
.word 0x018575d7
.insn 4, 0x018575d7

# CHECK: vsetvli a1, a0, 0x1c
.word 0x01c575d7
.insn 4, 0x01c575d7

# CHECK: vsetvli a1, a0, 0x24
.word 0x024575d7
.insn 4, 0x024575d7

# CHECK: vsetvli a1, a0, 0x29
.word 0x029575d7
.insn 4, 0x029575d7

# CHECK: vsetvli a1, a0, 0x110
.word 0x110575d7
.insn 4, 0x110575d7

# CHECK: vsetvli a1, a0, e64, mf8, tu, mu
.word 0x01d575d7
.insn 4, 0x01d575d7

# CHECK: vsetivli a1, 0x10, e8, m4, tu, mu
.word 0xc02875d7
.insn 4, 0xc02875d7

# CHECK: vsetivli a1, 0x10, 0xc
.word 0xc0c875d7
.insn 4, 0xc0c875d7

# CHECK: vsetivli a1, 0x10, 0x14
.word 0xc14875d7
.insn 4, 0xc14875d7

# CHECK: vsetivli a1, 0x10, 0x38
.word 0xc38875d7
.insn 4, 0xc38875d7

# CHECK: vsetivli a1, 0x10, 0x103
.word 0xd03875d7
.insn 4, 0xd03875d7

# CHECK: vsetivli a1, 0x10, e8, mf4, tu, mu
.word 0xc06875d7
.insn 4, 0xc06875d7
8 changes: 7 additions & 1 deletion llvm/tools/llvm-objdump/llvm-objdump.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -633,8 +633,14 @@ static bool isCSKYElf(const ObjectFile &Obj) {
return Elf && Elf->getEMachine() == ELF::EM_CSKY;
}

static bool isRISCVElf(const ObjectFile &Obj) {
const auto *Elf = dyn_cast<ELFObjectFileBase>(&Obj);
return Elf && Elf->getEMachine() == ELF::EM_RISCV;
}

static bool hasMappingSymbols(const ObjectFile &Obj) {
return isArmElf(Obj) || isAArch64Elf(Obj) || isCSKYElf(Obj);
return isArmElf(Obj) || isAArch64Elf(Obj) || isCSKYElf(Obj) ||
isRISCVElf(Obj);
}

static void printRelocation(formatted_raw_ostream &OS, StringRef FileName,
Expand Down