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

Bug the References to fstub removed by some compiling-time #226

Open
Princess-of-Sleeping opened this issue Feb 5, 2023 · 4 comments
Open

Comments

@Princess-of-Sleeping
Copy link
Contributor

Princess-of-Sleeping commented Feb 5, 2023

First of all, the psp2 kernel has code like this:

	# Simplified code for overview
	movw       r3, #0x0
	movt       r3, #0x0
	cbz        r3, LAB_810023a6
	blx        SceHdmiForDriver_3126A070

This assembly code can be originally reproduced in C code like

if(&SceHdmiForDriver_3126A070 != NULL){
	SceHdmiForDriver_3126A070(...);
}

&SceHdmiForDriver_3126A070 has movw/movt set to 0 by the SCE compiler. This matches the initial assembly code.

And if the kernel finds the SceHdmiForDriver_3126A070 export, this 0 movw/movt is resolved and replaced with a pointer to the SceHdmiForDriver_3126A070 stub.

However, even if you compile the following C code with vitasdk, these ifs are removed by the compiler.

if(&scePowerIsPowerOnline != NULL){
	scePowerIsPowerOnline();
}

Compile witth

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wl,-q -Wall ${-O0/-O1/-O2/-O3/-Os} -fno-inline -fno-rtti -fno-exceptions -std=gnu++17")

to

	push       {r4, lr}
	blx        scePowerIsPowerOnline
	...

but I don't know how to fix this bug.

@isage
Copy link
Contributor

isage commented Feb 5, 2023

Can you try

if((volatile void*)&scePowerIsPowerOnline != NULL){
	scePowerIsPowerOnline();
}

?

@Princess-of-Sleeping
Copy link
Contributor Author

image
same result with -O2

	push       {r4, lr}
	blx        scePowerIsPowerOnline
	blx        scePowerIsPowerOnline

@Princess-of-Sleeping
Copy link
Contributor Author

According to @isage arm-vita-eabi-gcc -Os -S main.c generated correct code and did not remove the assembly code corresponding to if branches in C code.

Also, scePowerIsPowerOnline was tested with the weak attribute.

Without __attribute__((weak))__ both gcc and snc remove if check on any optimization level.

gcc -Os

        push    {r3, lr}
        ldr     r3, .L6
        cbz     r3, .L2
        bl      scePowerIsPowerOnline
.L6:
        .word   scePowerIsPowerOnline

gcc -03

        push    {r3, lr}
        movw    r3, #:lower16:scePowerIsPowerOnline
        movt    r3, #:upper16:scePowerIsPowerOnline
        movs    r0, #0
        cbz     r3, .L1
        bl      scePowerIsPowerOnline

@Princess-of-Sleeping
Copy link
Contributor Author

"""Compiling""" with -O3 will output the following assembly code.

	push       {r4, lr}
	movw       r4,#0x0
	movt       r4,#0x0
	cbz        r4,LAB_810016b2
	nop.w
	cbz        r4,LAB_810016b2
	nop.w

LAB_810016b2:
	mov.w      r0, #0x2000000

@Princess-of-Sleeping Princess-of-Sleeping changed the title Bug the References to fstub removed by gcc Bug the References to fstub removed by some compiling-time Feb 5, 2023
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

No branches or pull requests

2 participants