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

The compiler bug for data address refer #205

Open
Princess-of-Sleeping opened this issue May 16, 2022 · 0 comments
Open

The compiler bug for data address refer #205

Princess-of-Sleeping opened this issue May 16, 2022 · 0 comments
Labels

Comments

@Princess-of-Sleeping
Copy link
Contributor

Princess-of-Sleeping commented May 16, 2022

Since there is no division instruction in ARM, the compiler tries to resolve it with a function, but when the function is defined with a function pointer, the compiler issues bl to the address of the function pointer in the data address. so it crashes by Prefetch abort excption.

Compile environment

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wl,-q -Wall -O2 -fno-inline")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-rtti -fno-exceptions")

set_target_properties(${PROJECT_NAME}
  PROPERTIES LINK_FLAGS "-nostdlib"
  COMPILE_FLAGS "-D__PSP2_USER__"
)

example

// 0x81000000 for text address

uint64_t test_div(uint64_t a, uint64_t b){
	return a / b;
}


// 0x81001000 for text address
// __aeabi_uldivmod was get function pointer by somehow
uint64_t (* __aeabi_uldivmod)(uint64_t a, uint64_t b);

Compiling this (^) code will give you the following assembly

test_div:
	push {lr}
	bl #0x81001000
	// and some...
	pop {pc}

But the correct code is:

test_div:
	push {ip, lr}
	movw ip, #:lower16:__aeabi_uldivmod
	movt ip, #:upper16:__aeabi_uldivmod
	ldr ip, [ip]
	blx ip
	// and some...
	pop {pc}

So for some reason we have to devise a definition for the compiler function.

Like

uint64_t __attribute__((noinline, naked)) __aeabi_uldivmod(uint64_t a, uint64_t b){
	__asm__ volatile (
		"push {ip, lr}\n"
		"movw ip, #:lower16:___aeabi_uldivmod\n"
		"movt ip, #:upper16:___aeabi_uldivmod\n"
		"ldr ip, [ip]\n"
		"blx ip\n"
		"pop {ip, pc}\n"
		:
		:
		:
	);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant