Skip to content

Loongarch relative branches immediate value inconsistent with other architectures #2689

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

Closed
OBarronCS opened this issue Apr 23, 2025 · 2 comments · Fixed by #2699
Closed

Loongarch relative branches immediate value inconsistent with other architectures #2689

OBarronCS opened this issue Apr 23, 2025 · 2 comments · Fixed by #2699
Labels
bug Something is not working as it should LoongArch Arch

Comments

@OBarronCS
Copy link

Work environment

Questions Answers
Capstone module affected Loongarch32 and Loongarch64
Source of Capstone pip install capstone==6.0.0a4
Version/git commit 6.0.0 alpha 4

Relative branches in Loongarch do not add the value of the immediate to the instruction address so that the target address is encoded in .imm, as occurs in other instructions.

Actual behavior

cstool -d loongarch64 "80 0d 00 40 " 0x1000
1000  80 0d 00 40  beqz	$t0, 0xc
	ID: 117 (beqz)
	op_count: 2
		operands[0].type: REG = t0
		operands[0].access: READ
		operands[1].type: IMM = 0xc
		operands[1].access: READ
	Registers read: t0
	Groups: jump branch_relative 


cstool -d loongarch64 "00 ec 0b 54" 0x1000
1000  00 ec 0b 54  bl	0xbec
	ID: 124 (bl)
	op_count: 1
		operands[0].type: IMM = 0xbec
		operands[0].access: READ
	Registers modified: ra
	Groups: call 

Expected behavior

The expected behavior would be for the instruction address (the value 0x1000 above) to be added to the immediate, so that the immediate contains the target address, as occurs in other architectures.

An example je instruction in x86-64:

cstool -d x64 "74 4a"  0x1000
1000  74 4a                                            je	0x104c
	ID: 260 (je)
	Prefix:0x00 0x00 0x00 0x00 
	Opcode:0x74 0x00 0x00 0x00 
	rex: 0x0
	addr_size: 8
	modrm: 0x0
	disp: 0x0
	sib: 0x0
	imm_count: 1
		imms[1]: 0x104c
	op_count: 1
		operands[0].type: IMM = 0x104c
		operands[0].size: 8
	Registers read: rflags
	Registers modified: eip
	EFLAGS: TEST_ZF
	Groups: branch_relative jump 

In this example, the constant offset 0x4c is added to the offset 0x1000 so that the branch target is encoded in the .imm value, 0x104c.

@Rot127 Rot127 added bug Something is not working as it should LoongArch Arch labels Apr 24, 2025
@Rot127
Copy link
Collaborator

Rot127 commented Apr 24, 2025

cc @jiegec

jiegec added a commit to jiegec/capstone that referenced this issue May 7, 2025
To match behavior with other archs, compute absolute address instead of
relative for address operands. Fixes capstone-engine#2689.
@jiegec
Copy link
Contributor

jiegec commented May 7, 2025

Fix proposed #2699

jiegec added a commit to jiegec/capstone that referenced this issue May 9, 2025
To match behavior with other archs, compute absolute address instead of
relative for address operands. Fixes capstone-engine#2689.
kabeor pushed a commit that referenced this issue May 20, 2025
To match behavior with other archs, compute absolute address instead of
relative for address operands. Fixes #2689.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something is not working as it should LoongArch Arch
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants