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 all 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
4 changes: 4 additions & 0 deletions llvm/docs/ReleaseNotes.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@ 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 @@ -471,6 +471,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.

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

## 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
Type: SHT_PROGBITS
Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
AddressAlign: 0x1
ContentArray: [
# CHECK: 011f 4523 8967 <unknown>
0x1f, 0x01, 0x23, 0x45, 0x67, 0x89,

# CHECK: 4523013f cdab8967 <unknown>
0x3f, 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd,

# CHECK: 007f 4523 8967 cdab feef <unknown>
0x7f, 0x00, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xfe,

# CHECK: 4523107f cdab8967 badcfeef <unknown>
0x7f, 0x10, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xfe, 0xdc, 0xba,

# CHECK: 207f 4523 8967 cdab feef badc 7698 <unknown>
0x7f, 0x20, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xfe, 0xdc, 0xba, 0x98, 0x76,

# CHECK: 4523307f cdab8967 badcfeef 32547698 <unknown>
0x7f, 0x30, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32,

# CHECK: 407f 4523 8967 cdab feef badc 7698 3254 1210 <unknown>
0x7f, 0x40, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10, 0x12,

# CHECK: 4523507f cdab8967 badcfeef 32547698 56341210 <unknown>
0x7f, 0x50, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10, 0x12, 0x34, 0x56,

# CHECK: 607f 4523 8967 cdab feef badc 7698 3254 1210 5634 9a78 <unknown>
0x7f, 0x60, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10, 0x12, 0x34, 0x56, 0x78, 0x9a,
]

- Type: SectionHeaderTable
Sections:
- Name: .strtab
- Name: .symtab
- Name: .text
Symbols:
- Name: "$x"
Section: .text
Value: 0x0
...
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 @@ -636,8 +636,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