forums.ps2dev.org Forum Index forums.ps2dev.org
Homebrew PS2, PSP & PS3 Development Discussions
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Returning data from a exported function...

 
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development
View previous topic :: View next topic  
Author Message
ne0h



Joined: 21 Feb 2008
Posts: 386

PostPosted: Sun Nov 09, 2008 1:03 am    Post subject: Returning data from a exported function... Reply with quote

Hi,
I'm trying to return some data from a function exported from a kernel prx and imported to a user pbp, but it crash every time!
What's the problem?
This is the exported function:
Code:
char* GetBarraInf(void)
{
    char* data="fdsfdsfds";
    return data;
}

And here I try to use it...
Code:
printf("%s",GetBarraInf());

It doesn't work, no one character is printed to screen!
Crash every time!
What I'm trying to do is export a variable ( unsigned char ) that contains a image data ( builted with bin2o )!
Back to top
View user's profile Send private message
Pirata Nervo



Joined: 09 Oct 2007
Posts: 409

PostPosted: Sun Nov 09, 2008 1:15 am    Post subject: Reply with quote

make sure you allocate pointers.
Also I always need pspSdkSetK1 when exporting functions otherwise it does not work for me
_________________

Upgrade your PSP
Back to top
View user's profile Send private message
ne0h



Joined: 21 Feb 2008
Posts: 386

PostPosted: Sun Nov 09, 2008 1:30 am    Post subject: Reply with quote

mmm, is always needed to set K1 to 0? Is not only for "special" operations, or to remove some security protections?
Thanks!
Back to top
View user's profile Send private message
ne0h



Joined: 21 Feb 2008
Posts: 386

PostPosted: Sun Nov 09, 2008 1:38 am    Post subject: Reply with quote

Doesn't work whit this! ?_?
Code:
char* GetBarraInf(void)
{
    return "asd";
}


EDIT:
Get this exception:
Quote:
Exception - Address load/inst fetch
EPC - 0889A07C
Location - ra.text + 0009607C
Cause - 10000010
Status - 20008613
BadVAddr - 88217A40

zr:00000000 at:2008FF00 v0:88217A41 v1:0889CB38
a0:88217A40 a1:0BBBFE44 a2:00000073 a3:0BBBFE40
t0:00000068 t1:0000002B t2:00000020 t3:88065244
t4:0BBBFE58 t5:00003E0C t6:0880A5FC t7:20008600
s0:00000000 s1:FFFFFFFF s2:0BBBF63C s3:0BBBF600
s4:08930000 s5:08930000 s6:088AB9E6 s7:08930000
t8:00000000 t9:00000000 k0:0BBBFF00 k1:00000000
gp:08939230 sp:0BBBF5D0 fp:88217A40 ra:0889D7C4
Back to top
View user's profile Send private message
Badboy_



Joined: 09 Aug 2008
Posts: 4

PostPosted: Sun Nov 09, 2008 2:08 am    Post subject: Reply with quote

Code:
char* GetBarraInf(void)
{
    char* data= (char*) malloc(20);
    strcpy(data, "Hello, world!");
    return data;
}

does this work?
Back to top
View user's profile Send private message
hlide



Joined: 10 Sep 2006
Posts: 750

PostPosted: Sun Nov 09, 2008 2:19 am    Post subject: Reply with quote

*sigh*, noobish question again...

do you think making a local string in a function is making this string to be... local to this function, don't you ?

didn't it ever occur to you that adding "static" before the local string would turn it into a permanent static string you can return from your function ?

how many times do you think you need to master such a trivial error ?

well, don't answer.

EDIT:

a0:88217A40 <-- hernel address doesn't work with user code. Hint : #define USER_ADDR(x) ({ (typeof(x))(((int)(x))&~0x80000000); }) ==> return USER_ADDR("blahblah");


Last edited by hlide on Sun Nov 09, 2008 2:31 am; edited 2 times in total
Back to top
View user's profile Send private message
ne0h



Joined: 21 Feb 2008
Posts: 386

PostPosted: Sun Nov 09, 2008 2:26 am    Post subject: Reply with quote

Doesn't work!
Got the same exception:
Code:

Exception - Address load/inst fetch
EPC       - 0889A07C
Location  - ra.text + 0009607C
Cause     - 10000010
Status    - 20008613
BadVAddr  - 8002013A

zr:00000000 at:00000000 v0:8002013B v1:0889CB38
a0:8002013A a1:0BBBFE44 a2:00000073 a3:0BBBFE40
t0:00000068 t1:0000002B t2:00000020 t3:88065244
t4:A8028454 t5:00003E0C t6:000000FC t7:880161C8
s0:00000000 s1:FFFFFFFF s2:0BBBF63C s3:0BBBF600
s4:08930000 s5:08930000 s6:088ABA06 s7:08930000
t8:00000000 t9:00000000 k0:0BBBFF00 k1:00000000
gp:08939250 sp:0BBBF5D0 fp:8002013A ra:0889D7C4


EDIT:
hlide, thanks, I'll try!
Back to top
View user's profile Send private message
hlide



Joined: 10 Sep 2006
Posts: 750

PostPosted: Sun Nov 09, 2008 2:29 am    Post subject: Reply with quote

just look at registers, some registers have kernel addresses when you are running a user code...
Back to top
View user's profile Send private message
hlide



Joined: 10 Sep 2006
Posts: 750

PostPosted: Sun Nov 09, 2008 2:31 am    Post subject: Reply with quote

replace 0x40000000 into 0x80000000 of course...
Back to top
View user's profile Send private message
ne0h



Joined: 21 Feb 2008
Posts: 386

PostPosted: Sun Nov 09, 2008 2:43 am    Post subject: Reply with quote

Tryied with this:
Code:
static char data[20];

char* GetBarraInf(void)
{
    k1 = pspSdkSetK1(0);
    strcpy(data, "Hello world!");
    pspSdkSetK1(k1);
    return data;
}

And it doesn't work, crash!
I've also tryied to make it a user prx but it crash!
Get the same exception:
Code:
Exception - Address load/inst fetch
EPC       - 0889A07C
Location  - ra.text + 0009607C
Cause     - 10000010
Status    - 20008613
BadVAddr  - 8002013A

zr:00000000 at:00000000 v0:8002013B v1:0889CB38
a0:8002013A a1:0BBBFE44 a2:00000073 a3:0BBBFE40
t0:00000068 t1:0000002B t2:00000020 t3:88065244
t4:A8028454 t5:00003E0C t6:000000FC t7:880161C8
s0:00000000 s1:FFFFFFFF s2:0BBBF63C s3:0BBBF600
s4:08930000 s5:08930000 s6:088ABA06 s7:08930000
t8:00000000 t9:00000000 k0:0BBBFF00 k1:00000000
gp:08939250 sp:0BBBF5D0 fp:8002013A ra:0889D7C4


Quote:
just look at registers, some registers have kernel addresses when you are running a user code...

Sorry, doesn't know what mean the content of registers!
Never studied it! ( Have you a tuto about the content of this? )
So kernel address are 0x80000000 like, and user 0x40000000 like this, right?
Quote:
replace 0x40000000 into 0x80000000 of course...

Sorry, but I've to change the a0 reg value?
EDIT:

nop, maybe I've understand!
Back to top
View user's profile Send private message
hlide



Joined: 10 Sep 2006
Posts: 750

PostPosted: Sun Nov 09, 2008 2:58 am    Post subject: Reply with quote

Code:

char const * GetBarraInf(void)
{
    return (char const *)(((int)"blahblah")&~0x80000000));
}
Back to top
View user's profile Send private message
ne0h



Joined: 21 Feb 2008
Posts: 386

PostPosted: Sun Nov 09, 2008 3:03 am    Post subject: Reply with quote

No, I haven't undestrand!
I've writed this:
Code:

#define USER_ADDR(x) ({ (typeof(x))(((int)(x))&~0x80000000); })
   
char* GetBarraInf(void);
intraFontPrintf(ltn8, 20, 20, "%s", USER_ADDR(GetBarraInf()));

But it crash! Get Bus data error...
Code:

Exception - Bus error (data)
EPC       - 0889A084
Location  - ra.text + 00096084
Cause     - 1000001C
Status    - 20008613
BadVAddr  - 01044400

zr:00000000 at:00000000 v0:0002013B v1:0889CB40
a0:0002013A a1:0BBBFE44 a2:00000073 a3:0BBBFE40
t0:00000068 t1:0000002B t2:00000020 t3:88065244
t4:A8028454 t5:00003E0C t6:000000FC t7:880161C8
s0:00000000 s1:FFFFFFFF s2:0BBBF63C s3:0BBBF600
s4:08930000 s5:08930000 s6:088ABA16 s7:08C80000
t8:00000000 t9:00000000 k0:0BBBFF00 k1:00000000
gp:08939260 sp:0BBBF5D0 fp:0002013A ra:0889D7CC

Now I haven't a kernel address, right?
Anyway I've another problem...
Back to top
View user's profile Send private message
ne0h



Joined: 21 Feb 2008
Posts: 386

PostPosted: Sun Nov 09, 2008 3:10 am    Post subject: Reply with quote

oh my god! I got another kernel address! It crash...
Code:

Exception - Address load/inst fetch
EPC       - 0889A068
Location  - ra.text + 00096068
Cause     - 10000010
Status    - 20008613
BadVAddr  - 8002013A

zr:00000000 at:00000000 v0:8002013B v1:0889CB24
a0:8002013A a1:0BBBFE44 a2:00000073 a3:0BBBFE40
t0:00000068 t1:0000002B t2:00000020 t3:88065244
t4:A8028454 t5:00003E0C t6:000000FC t7:880161C8
s0:00000000 s1:FFFFFFFF s2:0BBBF63C s3:0BBBF600
s4:08930000 s5:08930000 s6:088AB9E6 s7:08930000
t8:00000000 t9:00000000 k0:0BBBFF00 k1:00000000
gp:08939230 sp:0BBBF5D0 fp:8002013A ra:0889D7B0


I'm sorry...

This is the code now:
Code:

static char data[20];

char const * GetBarraInf(void)
{
    strcpy(data, "Hello world!");
    return (char const *)(((int)data)&~0x80000000);
}
Back to top
View user's profile Send private message
hlide



Joined: 10 Sep 2006
Posts: 750

PostPosted: Sun Nov 09, 2008 3:41 am    Post subject: Reply with quote

Look at BadVAddr values they are abnormal...
Back to top
View user's profile Send private message
ne0h



Joined: 21 Feb 2008
Posts: 386

PostPosted: Sun Nov 09, 2008 5:33 am    Post subject: Reply with quote

hlide, a question:
EPC, Cause, Status and BadVAddr what means?
To work I've to change the variable addres?
But if I change the address the pointed memory is the same?
Or the kernel mode has a different mode to point a specified memory than the user mode?
Back to top
View user's profile Send private message
ne0h



Joined: 21 Feb 2008
Posts: 386

PostPosted: Sun Nov 09, 2008 5:51 am    Post subject: Reply with quote

Wow... Finally resolved!
Code:
static char* data = NULL;

char const * GetBarraInf(void)
{
    data = (char*)malloc(20);
    strcpy(data, "Hello World!");
    return (char const *)(((int)data)&~0x80000000);
}

And before I've commented the loadmodule function, sorry! :(
But the 8002013A of
BadVAddr is == SCE_KERNEL_ERROR_LIBRARY_NOT_YET_LINKED ?
Back to top
View user's profile Send private message
hlide



Joined: 10 Sep 2006
Posts: 750

PostPosted: Sun Nov 09, 2008 7:25 am    Post subject: Reply with quote

no wonder.

you don't need the malloc stuff. Just be sure the pointer is a user address.
Back to top
View user's profile Send private message
a_noob



Joined: 17 Sep 2006
Posts: 97
Location: _start: jr 0xDEADBEEF

PostPosted: Sun Nov 09, 2008 9:30 am    Post subject: Reply with quote

heh, im too late (please ignore)
_________________
Code:
.øOº'ºOø.
'ºOo.oOº'
Back to top
View user's profile Send private message AIM Address MSN Messenger
ne0h



Joined: 21 Feb 2008
Posts: 386

PostPosted: Sun Nov 09, 2008 8:23 pm    Post subject: Reply with quote

EPC, Cause and Status what means?
Back to top
View user's profile Send private message
ne0h



Joined: 21 Feb 2008
Posts: 386

PostPosted: Sun Nov 09, 2008 9:38 pm    Post subject: Reply with quote

hilde, if I doesn't use malloc it crash!
But the address is a user one...
Maybe I've do something wrong...
Code:

static char* data = "hellooo!!";

char* GetBarraInf(void)
{
    return (char*)(((int)data)&~0x80000000); // USER_ADDR(data);
}

And in the main programm:
Code:

char* GetBarraInf(void);
Init(); // pspDebugScreenInit
printf("%s", GetBarraInf()); // pspDebugScreenPrintf
sleep(2000000); // sceKernelDelayThread


The exception:
Code:

Exception - Bus error (data)
EPC       - 0889A064
Location  - ra.text + 00096064
Cause     - 1000001C
Status    - 20008613
BadVAddr  - 01044410

zr:00000000 at:2008FF00 v0:0824E205 v1:0889CB20
a0:0824E204 a1:0BBBFE48 a2:00000073 a3:0BBBFE44
t0:00000068 t1:0000002B t2:00000020 t3:880E57F0
t4:0BBBFE60 t5:00001E04 t6:0880A4AC t7:20008600
s0:00000000 s1:FFFFFFFF s2:0BBBEF4C s3:0BBBEF10
s4:DEADBEEF s5:DEADBEEF s6:088AB9E6 s7:DEADBEEF
t8:880E5818 t9:00000028 k0:0BBBFF00 k1:00000000
gp:08939230 sp:0BBBEEE0 fp:0824E204 ra:0889D7AC

The problem is not the address I think!
But 0x0824E204 is a strange address, right?
Back to top
View user's profile Send private message
ne0h



Joined: 21 Feb 2008
Posts: 386

PostPosted: Sun Nov 09, 2008 9:44 pm    Post subject: Reply with quote

Work only with this:
Code:

static char* data;

char* GetBarraInf(void)
{
    data = (char*)malloc(20);
    strcpy(data, "Hello World!!!");
    return (char*)(((int)data)&~0x80000000); // USER_ADDR(data);
}

But I can't do that, I've to return a unsiged char array...
( unsigned char data[]; <-- builted with bin2o )
Back to top
View user's profile Send private message
ne0h



Joined: 21 Feb 2008
Posts: 386

PostPosted: Sun Nov 09, 2008 11:30 pm    Post subject: Reply with quote

Resolved with this... but I have to use malloc! Without that it crash!
But I haven't found the malloc function in the kernel libc, where I can find it?
I'll built the prx with the kernel libs and libc to reduce its size!
Back to top
View user's profile Send private message
Pirata Nervo



Joined: 09 Oct 2007
Posts: 409

PostPosted: Mon Nov 10, 2008 1:54 am    Post subject: Reply with quote

search these forums for a malloc function made by HellCat I think using the sce memory functions.

He made it exactly because kernel libc does not have it
_________________

Upgrade your PSP
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development All times are GMT + 10 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group