Skip to content
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

Fixing TriCore disasm instructions #2088

Merged
merged 10 commits into from
Jul 26, 2023

Conversation

bkoppelmann
Copy link
Contributor

Hi,
while trying to integrate Capstone into QEMU, I came across some bugs.
Here are the fixes :)

Cheers,
Bastian

@XVilka
Copy link
Contributor

XVilka commented Jul 17, 2023

@imbillow take a look please when you have time

@XVilka XVilka mentioned this pull request Jul 17, 2023
24 tasks
Comment on lines 227 to 234
static void off8_fixup(MCInst *MI, uint64_t *off8)
{
switch (MCInst_getOpcode(MI)) {
case TRICORE_LD_A_sc:
case TRICORE_ST_A_sc:
case TRICORE_ST_W_sc:
case TRICORE_LD_W_sc: {
*off8 *= 4;
Copy link
Contributor

@imbillow imbillow Jul 20, 2023

Choose a reason for hiding this comment

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

Suggested change
static void off8_fixup(MCInst *MI, uint64_t *off8)
{
switch (MCInst_getOpcode(MI)) {
case TRICORE_LD_A_sc:
case TRICORE_ST_A_sc:
case TRICORE_ST_W_sc:
case TRICORE_LD_W_sc: {
*off8 *= 4;
static void const8_fixup(MCInst *MI, uint64_t *const8)
{
switch (MCInst_getOpcode(MI)) {
case TRICORE_LD_A_sc:
case TRICORE_ST_A_sc:
case TRICORE_ST_W_sc:
case TRICORE_LD_W_sc: {
*const8 *= 4;

@@ -235,6 +248,9 @@ static void print_zero_ext(MCInst *MI, int OpNum, SStream *O, unsigned n)
if (n == 4) {
off4_fixup(MI, &imm);
}
if (n == 8) {
off8_fixup(MI, &imm);
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
off8_fixup(MI, &imm);
const8_fixup(MI, &imm);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

case TRICORE_ABSS_H_rr:
case TRICORE_ABS_H_rr:
case TRICORE_ABS_B_rr:
case TRICORE_ABS_rr: {
Copy link
Contributor

Choose a reason for hiding this comment

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

I checked and found that you still haven't fully fixed it here.
Can you please double-check the following instructions?

d, s2[, s1[, n]]

ADDSC.A
ADDSC.AT
MOV.A
MOV.AA
MOV.D
CRC32.B
CRC32B.W
CRC32L.W

a

UPDFL

Copy link
Contributor Author

Choose a reason for hiding this comment

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

They have been correct before. Not sure why. These are the insns I sent to capstone as well as GNU objdump and they agree.

CODE = b'\x01\x23\x01\x16'  # addsc.a %a1,%a2,%d3,1
CODE += b'\x01\x34\x20\x26' # addsc.at %a2,%a3,%d4
CODE += b'\x01\x20\x30\x16' # mov.a %a1,%d2
CODE += b'\x01\x30\x00\x20' # mov.aa %a2,%a3
CODE += b'\x01\xb0\xc0\xa4' # mov.d %d10,%a11
CODE += b'\x4b\xbc\x60\xa0' # crc32.b %d10,%d11,%d12
CODE += b'\x4b\xbc\x30\xa0' # crc32 %d10,%d11,%d12
CODE += b'\x4b\xbc\x70\xa0' # crc32l.w %d10,%d11,%d12
CODE += b'\x4b\x0a\xc1\xa0' # updfl %d10

Copy link
Contributor

Choose a reason for hiding this comment

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

I remembered that we can also change the order of operands in the td file.

@XVilka
Copy link
Contributor

XVilka commented Jul 20, 2023

@bkoppelmann @imbillow do we have tests for instructions in question? If not - we should add in a testsuite.

@bkoppelmann
Copy link
Contributor Author

I can create tests. However, I'm not quite sure where to put them. Is suite/MC/TriCore/ the right place?

@XVilka
Copy link
Contributor

XVilka commented Jul 23, 2023

Is suite/MC/TriCore/ the right place?

Yes

Bastian Koppelmann added 10 commits July 23, 2023 17:07
when we got 2 operands we were expecting them to be one of the CACHE
insns. However, 2 operands are also used by ld/st insns with bitreverse
+r addressing mode. In that case we would print the registers in the
wrong order.
abs instruction use 's2' as the first source operand and not 's1'.
extr and extr.u in the RRRR format use 's3' as the second source operand
instead of 's2'.
For these insns destination and source are swapped compared to their ld
counterparts.
we need to multiply const8 by 4 for the SC variants of ld/sw.
@bkoppelmann
Copy link
Contributor Author

@bkoppelmann @imbillow do we have tests for instructions in question? If not - we should add in a testsuite.

Done

@XVilka
Copy link
Contributor

XVilka commented Jul 25, 2023

@imbillow take a look again please.

@XVilka
Copy link
Contributor

XVilka commented Jul 25, 2023

@kabeor this one is ready to merge. Also worth importing into v5 branch.

@kabeor
Copy link
Member

kabeor commented Jul 26, 2023

Great! Thanks for your contribution!

@kabeor kabeor merged commit 9099567 into capstone-engine:next Jul 26, 2023
11 checks passed
kabeor pushed a commit to kabeor/capstone that referenced this pull request Aug 21, 2023
kabeor added a commit that referenced this pull request Aug 21, 2023
* Add Python bindings for WASM

* Update Python bindings for m68k

* Update Python bindings for mos65xx

* Update Python bindings for x86

* Add Python bindings for SH

* Update CS_* constants in Python bindings

* Update constants from ARM auto-sync patch

* Fixing TriCore disasm instructions (#2088)

* allow absolute CMAKE_INSTALL_*DIR (#2134)

This patch fixes Capstone 5 build on NixOS.

NixOS's build infrastructure sets CMAKE_INSTALL_{LIB,INCLUDE}DIR to
absolute paths. If you append it to ${prefix}, you get the wrong path.
NixOS automatically detects it and links this issue:
NixOS/nixpkgs#144170

---------

Co-authored-by: Peace-Maker <peace-maker@wcfan.de>
Co-authored-by: Bastian Koppelmann <bkoppelmann@users.noreply.github.com>
Co-authored-by: chayleaf <chayleaf@protonmail.com>
kabeor added a commit that referenced this pull request Aug 21, 2023
* Add Python bindings for WASM

* Update Python bindings for m68k

* Update Python bindings for mos65xx

* Update Python bindings for x86

* Add Python bindings for SH

* Update CS_* constants in Python bindings

* Update constants from ARM auto-sync patch

* Fixing TriCore disasm instructions (#2088)

* allow absolute CMAKE_INSTALL_*DIR (#2134)

This patch fixes Capstone 5 build on NixOS.

NixOS's build infrastructure sets CMAKE_INSTALL_{LIB,INCLUDE}DIR to
absolute paths. If you append it to ${prefix}, you get the wrong path.
NixOS automatically detects it and links this issue:
NixOS/nixpkgs#144170

* Disable swift binding const generate

* update bindings const

* update capstone version

* update ChangeLog

---------

Co-authored-by: Peace-Maker <peace-maker@wcfan.de>
Co-authored-by: Bastian Koppelmann <bkoppelmann@users.noreply.github.com>
Co-authored-by: chayleaf <chayleaf@protonmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants