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 

using prx to load values/store values (exports not working)

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



Joined: 20 Oct 2008
Posts: 355

PostPosted: Sat May 23, 2009 1:04 am    Post subject: using prx to load values/store values (exports not working) Reply with quote

i want my initconf prx to do all the parsing for my main and then be unloaded but just getting started i cant figure out why it wont hold varaibles and pass them to main when i call the exports i get nothing here is the prx and its makefile

initconf.cpp
Code:
#include <pspkernel.h>
#include <stdio.h>
#include <libconfig.hh>
#include <libconfig.h>
#include <iostream>
#include <fstream>
#include <sys/unistd.h>
#include <cstdlib>
#include <oslib/oslib.h> //use nothing from here except values
using namespace std;
using namespace libconfig;

#include "../main/global.h"
#include "initconf.h"

PSP_MODULE_INFO("initconf", 0x0006, 1, 1);
PSP_HEAP_SIZE_KB(1500);

Config cfg;

bool loaded = false;
bool finished = false;
bool created = false;

void check_finished()
{
   while(!finished)
   {
      //just wait till main is finished loading config
   }
}

bool file_exist(string src)
{
   bool r = false;
   ifstream file;
   file.open(src.c_str());
   if(file.is_open())
      r = true;
   else
      r = false;
   file.close();
   return r;
}

int main(int argc, char * argv[])
{
   if(argc < 2)
   {
      loaded = false;
      finished = true;
      return 0;
   }
   
   try
   {
      if(file_exist(argv[1]))
      {
         cfg.readFile(argv[1]);
         loaded = true;
      }
   }
   catch(FileIOException &e)
   {
      loaded = false;
      debugerr("FileIOException",__LINE__,__FILE__,__FUNCTION__);
      
      if(file_exist(argv[1]))
      {
         string renamed = "~";
         renamed += argv[1];
         if(file_exist(renamed))
            remove(renamed.c_str());
         rename(argv[1],renamed.c_str());
         cfg.writeFile(argv[1]);
      }
   }
   catch(ParseException &e)
   {
      loaded = false;
      char err[1000];
      sprintf(err,"ParseException: what = %s, error = %s, line = %d",e.what(),e.getError(),e.getLine());
      debugerr(err,__LINE__,__FILE__,__FUNCTION__);
      
      if(file_exist(argv[1]))
      {
         string renamed = "~";
         renamed += argv[1];
         if(file_exist(renamed))
            remove(renamed.c_str());
         rename(argv[1],renamed.c_str());
         cfg.writeFile(argv[1]);
      }
   }
   finished = true;
   return 0;
}
bool initconf_enableHome()
{
   check_finished();
   bool enableHome = false;
   if(!cfg.lookupValue("enableHome",enableHome))
      enableHome = true;
   return enableHome;
}
int initconf_getPixelFormat()
{
   check_finished();
   int bpp = 0;
   if(!cfg.lookupValue("bits_per_pixel",bpp))
      bpp = 16;
   switch(bpp)
   {
      case 32: return OSL_PF_8888;
      case 16: return OSL_PF_5650;
      case 15: return OSL_PF_5551;
      default: return OSL_PF_4444;
   }
   return OSL_PF_4444;
}


makefile
Code:
PSPSDK=$(shell psp-config --pspsdk-path)

TARGET = initconf
OBJS = main.o ../main/main_exp.o

CFLAGS = -O2 -G0 -Wall
CXXFLAGS = $(CFLAGS) -fexceptions -fno-rtti
ASFLAGS = $(CFLAGS)

LDFLAGS =
#-mno-crt0 -nostartfiles
INCS = -I/usr/local/pspdev/include \
      -I/usr/local/pspdev/psp/include \
      -I/usr/local/pspdev/psp/sdk/include

CFLAGS := $(INCS)

INCDIR =
LIBDIR =

STDLIBS = -losl -mp3 -lmikmod -lpng -lz
STDLIBS += -lpspctrl -lpspumd -lpsprtc -lpsppower
STDLIBS += -lpspgu -lpspgum -lpsphprm -lpspaudiolib -lpspaudio
STDLIBS += -lmikmod -lm -lc -lstdc++ -lpspmpeg -lpsprtc
STDLIBS += -lpspaudiocodec -lpspatrac3

YOURLIBS = -lconfig++ -lconfig

LIBS = $(YOURLIBS) $(STDLIBS)

BUILD_PRX = 1
PRX_EXPORTS = exports.exp

include ../locations.mak
include $(PSPSDK)/lib/build.mak

s_file:   $(shell psp-build-exports -s exports.exp)

install:
   mkdir -p $(INITCONF_DIR)
   cp -f initconf.prx $(INITCONF_DIR)
Back to top
View user's profile Send private message MSN Messenger
J.F.



Joined: 22 Feb 2004
Posts: 2906

PostPosted: Sat May 23, 2009 5:53 am    Post subject: Reply with quote

You need to do a search as that has been discussed before. From my recollection, only functions are supported by the SDK, not variables. So if you wish to access variables, you'll need to make accessor functions. So instead of

y = x;

you need

y = GetX();
Back to top
View user's profile Send private message AIM Address
coolkehon



Joined: 20 Oct 2008
Posts: 355

PostPosted: Sat May 23, 2009 8:10 am    Post subject: Reply with quote

no what i'm saying there are accessor functions initconf_getXXX but its not giving me the values i always get a negative number and it doesnt return a value
Back to top
View user's profile Send private message MSN Messenger
J.F.



Joined: 22 Feb 2004
Posts: 2906

PostPosted: Sat May 23, 2009 9:26 am    Post subject: Reply with quote

I see, that's the prx source above... compare that with the example I made for someone else...

Code:

/*
 * PSP Software Development Kit - http://www.pspdev.org
 * -----------------------------------------------------------------------
 * Licensed under the BSD license, see LICENSE in PSPSDK root for details.
 *
 * main.c - Basic Kernel PRX template
 *
 * Copyright (c) 2005 Marcus R. Brown <mrbrown@ocgnet.org>
 * Copyright (c) 2005 James Forshaw <tyranid@gmail.com>
 * Copyright (c) 2005 John Kelley <ps2dev@kelley.ca>
 *
 * $Id$
 * $HeadURL$
 */
#include <pspkernel.h>
#include <stdio.h>
#include <oslib/oslib.h>

PSP_MODULE_INFO("testlib", 0x0006, 1, 1);
PSP_NO_CREATE_MAIN_THREAD();


extern "C"
{
void startDrawing()
{
   static int x = 0, y = 0;

   oslInit(0);

   oslInitGfx(OSL_PF_8888,1);

   //Make sure default analog handler is disabled!
   oslSetKeyAnalogToDPad(0);

   oslDebug("prx loaded and osl inited from main");

   while(!osl_quit)
   {
      oslReadKeys();
      if (osl_pad.held.up)
         y = (y==0) ? 171 : y-1;
      if (osl_pad.held.down)
         y = (y==172) ? 0 : y+1;
      if (osl_pad.held.left)
         x = (x==0) ? 369 : x-1;
      if (osl_pad.held.right)
         x = (x==370) ? 0 : x+1;

      oslStartDrawing();
      oslCls();
      oslDrawFillRect(x,y,x+100,y+100,RGB(255,0,0));
      oslEndDrawing();
      oslEndFrame();
      oslSyncFrame();
   }
}

/* Entry point */
int module_start(SceSize args, void *argp)
{
   return 0;
}

/* Module stop entry */
int module_stop(SceSize args, void *argp)
{
   return 0;
}

}


First, I'm not sure the PSP can export c++ functions. You'd have to talk to moonlight about that. As far as I know, it's c only. Hence the extern "C" {}.

Then we don't see the export file either. My example for the above was

Code:
# Define the exports for the prx
PSP_BEGIN_EXPORTS

# These four lines are mandatory (although you can add other functions like module_stop)
# syslib is a psynonym for the single mandatory export.

PSP_EXPORT_START(syslib, 0, 0x8000)
PSP_EXPORT_FUNC_HASH(module_start)
PSP_EXPORT_FUNC_HASH(module_stop)
PSP_EXPORT_VAR_HASH(module_info)
PSP_EXPORT_END

# Export our function
# flags = 0x0001 => direct jmp
PSP_EXPORT_START(testlib, 0, 0x0001)
PSP_EXPORT_FUNC(startDrawing)
PSP_EXPORT_END

PSP_END_EXPORTS


With the makefile being

Code:
TARGET = testlib
OBJS = main.o

CFLAGS = -O2 -G0 -Wall
CXXFLAGS = $(CFLAGS) -fno-exceptions -fno-rtti
ASFLAGS = $(CFLAGS)

LDFLAGS =
LDFLAGS += -mno-crt0 -nostartfiles

INCDIR =
LIBDIR =

STDLIBS = -losl -mp3 -lmikmod -lpng -lz
STDLIBS += -lpspctrl -lpspumd -lpsprtc -lpsppower
STDLIBS += -lpspgu -lpspaudiolib -lpspaudio
STDLIBS += -lmikmod -lm -lc -lstdc++ -lpspmpeg -lpsprtc
STDLIBS += -lpspaudiocodec -lpspatrac3
YOURLIBS =

LIBS = $(STDLIBS) $(YOURLIBS)

BUILD_PRX = 1
PRX_EXPORTS = exports.exp

PSPSDK=$(shell psp-config --pspsdk-path)
include $(PSPSDK)/lib/build.mak


And then to use the prx, the main.c file was

Code:
#include <pspkernel.h>
#include <pspsdk.h>
#include <pspctrl.h>
#include <pspdebug.h>
#include <pspdisplay.h>

#include <string.h>
#include <stdio.h>
#include <stdlib.h>


PSP_MODULE_INFO("KJA PDA MAIN", PSP_MODULE_USER ,1,1);
PSP_MAIN_THREAD_ATTR(THREAD_ATTR_USER | THREAD_ATTR_VFPU);
PSP_HEAP_SIZE_KB(2500);

extern "C"
{
void startDrawing();
}

int main(int argc, char *argv[])
{
   pspDebugScreenInit();
   pspDebugScreenSetBackColor(0xFF000000);
   pspDebugScreenSetTextColor(0xFFFFFFFF);

   SceUID modid = pspSdkLoadStartModule("testlib.prx", PSP_MEMORY_PARTITION_USER);
   if (modid < 0)
   {
      pspDebugScreenPrintf("Error 0x%08X loading/starting testlib.prx.\n", modid);
        sceKernelDelayThread(5*1000*1000);
      sceKernelExitGame();
   }

   startDrawing();

   sceKernelExitGame();
   return 0;   // never reaches here - just to supress warning
}


Notice how the function from the prx is prototyped

Code:
extern "C"
{
void startDrawing();
}
Back to top
View user's profile Send private message AIM Address
coolkehon



Joined: 20 Oct 2008
Posts: 355

PostPosted: Sat May 23, 2009 9:36 am    Post subject: Reply with quote

lol that person was me and the exports is here and here is the initconf.h they are externed

exports.exp
Code:
# Define the exports for the prx
PSP_BEGIN_EXPORTS

# These four lines are mandatory (although you can add other functions like module_stop)
# syslib is a psynonym for the single mandatory export.

PSP_EXPORT_START(syslib, 0, 0x8000)
PSP_EXPORT_FUNC_HASH(module_start)
PSP_EXPORT_VAR_HASH(module_info)
PSP_EXPORT_END

# Export our function
# flags = 0x0001 => direct jmp
PSP_EXPORT_START(initconf, 0, 0x0001)
PSP_EXPORT_FUNC(initconf_enableHome)
PSP_EXPORT_FUNC(initconf_getPixelFormat)
PSP_EXPORT_END

PSP_END_EXPORTS


initconf.h

Code:
/*
 * initconf.h
 *
 *  Created on: May 20, 2009
 *      Author: kurt
 */

#ifndef INITCONF_H_
#define INITCONF_H_

#ifdef __cplusplus
extern "C" {
#endif
   
   #ifndef __cplusplus
      #define bool int
   #endif
   
   bool initconf_enableHome();
   int initconf_getPixelFormat();
   
#ifdef __cplusplus
}
#endif

#endif /* INITCONF_H_ */
Back to top
View user's profile Send private message MSN Messenger
coolkehon



Joined: 20 Oct 2008
Posts: 355

PostPosted: Sat May 23, 2009 9:51 am    Post subject: Reply with quote

so also here is how the module is loaded ass you can see it accepts args

Code:
int argsc = 1;
   char * args[argsc];
   args[0] = (char *)malloc((workingDir + "/var/init.cfg").length());
   strcpy(args[0],(workingDir + "/var/init.cfg").c_str());
   
   modid = pspSdkLoadStartModuleWithArgs((workingDir + "/sys/initconf.prx").c_str(),PSP_MEMORY_PARTITION_USER,argsc,args);
   if (modid < 0)
   {
      pspDebugScreenPrintf("Error 0x%08X loading/starting %s/sys/initconf.prx.\n", modid,workingDir.c_str());
      sceKernelDelayThread(5*1000*1000);
      sceKernelExitGame();
   }
Back to top
View user's profile Send private message MSN Messenger
J.F.



Joined: 22 Feb 2004
Posts: 2906

PostPosted: Sat May 23, 2009 1:55 pm    Post subject: Reply with quote

I see you put extern "C" in the h file, but I don't see that in the cpp file.
Back to top
View user's profile Send private message AIM Address
coolkehon



Joined: 20 Oct 2008
Posts: 355

PostPosted: Sun May 24, 2009 7:58 am    Post subject: Reply with quote

it crashes the whole app and why can i not compile and export c++ functions there isnt much of a difference and also why has the pspsdk stoped being developed as much as getting upgrades such as c++ exports
Back to top
View user's profile Send private message MSN Messenger
TyRaNiD



Joined: 18 Jan 2004
Posts: 918

PostPosted: Sun May 24, 2009 8:51 am    Post subject: Reply with quote

Your problem is you don't understand how c++ works, in theory you can export static/global c++ functions no problem, if you understood what the export builder does and what c++ does. Alas you haven't bothered to try and understand.
Back to top
View user's profile Send private message
J.F.



Joined: 22 Feb 2004
Posts: 2906

PostPosted: Sun May 24, 2009 9:05 am    Post subject: Reply with quote

And I can't help with that as I don't care to learn how to export c++ as I don't care for c++. ;)
Back to top
View user's profile Send private message AIM Address
Torch



Joined: 28 May 2008
Posts: 842

PostPosted: Sun May 24, 2009 2:06 pm    Post subject: Reply with quote

Are the VSH modules' exported functions C++ too or only the internals are C++ and only C functions are exposed?
Back to top
View user's profile Send private message
SilverSpring



Joined: 27 Feb 2007
Posts: 115

PostPosted: Sun May 24, 2009 3:50 pm    Post subject: Reply with quote

No, they are C++ too. That is why some of the NID's are so difficult to crack, because of name mangling.

An example (from paf.prx):

0xDB230BE1 _ZN3paf8PhWidget7SetTextERKNS_12basic_stringIwNS_11char_traitsIwEENS_9allocatorIwEEEEi

which demangled becomes:

paf::PhWidget::SetText(paf::basic_string<wchar_t,paf::char_traits<wchar_t>,paf::allocator<wchar_t>> const&,int)


etc. etc.
_________________
PSP PRX LibDocs
Back to top
View user's profile Send private message Visit poster's website
coolkehon



Joined: 20 Oct 2008
Posts: 355

PostPosted: Mon May 25, 2009 12:56 am    Post subject: Reply with quote

TyRaNiD wrote:
Your problem is you don't understand how c++ works, in theory you can export static/global c++ functions no problem, if you understood what the export builder does and what c++ does. Alas you haven't bothered to try and understand.


i actually read you guide for the psp and the module tutorial yesterday and didnt learn much that i understood be it explained some stuff most of those examples are in kernel mode and this is a usermod app also what is the "constructor" for the psp application in usermode for the patches and stuff couldnt find that
Back to top
View user's profile Send private message MSN Messenger
coolkehon



Joined: 20 Oct 2008
Posts: 355

PostPosted: Mon May 25, 2009 12:58 am    Post subject: Reply with quote

SilverSpring wrote:
No, they are C++ too. That is why some of the NID's are so difficult to crack, because of name mangling.

An example (from paf.prx):

0xDB230BE1 _ZN3paf8PhWidget7SetTextERKNS_12basic_stringIwNS_11char_traitsIwEENS_9allocatorIwEEEEi

which demangled becomes:

paf::PhWidget::SetText(paf::basic_string<wchar_t,paf::char_traits<wchar_t>,paf::allocator<wchar_t>> const&,int)


etc. etc.


so how can i use c++ in an exported module

edit: here are some more details on the problem
* c++ functions will not compile unless forward declaired & externed
* when the module is loaded it will not present any error but when i try to call exports i get a return value from which should be an int -2147352262
no matter what
* if i extern the entire function in
Code:
extern "C" {int function() { do whatever; return whatever;}

it crashes the psp

here is the error when trying to compile exported function note that this is the prx itself not the app that will be using the exported function
Code:
exports.o:(.rodata.sceResident+0x1c): undefined reference to `initconf_setup'
collect2: ld returned 1 exit status
make: *** [initconf.elf] Error 1
rm exports.c
Back to top
View user's profile Send private message MSN Messenger
TyRaNiD



Joined: 18 Jan 2004
Posts: 918

PostPosted: Mon May 25, 2009 11:01 pm    Post subject: Reply with quote

For a start you really should look at what the error code is telling you. If you convert it to hex and look it up in pspkerror (or using the error command in pspsh) you find it is 0x8002013a which just so happens to be SCE_KERNEL_ERROR_LIBRARY_NOT_YET_LINKED. So for some reason you have specified the library wrong or something so the function cannot be found. Have you modified the import assembly code to ensure that it is no longer delay loaded?

As for exporting C++ functions it requires abit extra hassle but it is doable. Mangle the name for the function (e.g. build the code and use objdump to get out the mangled symbol name) then put the mangled symbol name in your exports. In theory you could even export an entire class but it probably isn't recommended :) Maybe I could have made the export process more streamlined but I guess it is too late now.
Back to top
View user's profile Send private message
coolkehon



Joined: 20 Oct 2008
Posts: 355

PostPosted: Tue May 26, 2009 1:51 am    Post subject: Reply with quote

i'm new to programming as advanced as this so it may take some explaing on your behalf of what stuff means because i learned through java the basics then learned c/c++ (self taught) then learned the psp stuff although need to (doing right now) learning the gu but i would not have thought for a second to check the number and convert it to hex and when i did convert to hex i got -0x7FFDFEC6 so how the heck did you get that error code

edit: also how would i go about this name mangling thing you speak of never heard of objdump and dont really know how to use it or mangle the name
Back to top
View user's profile Send private message MSN Messenger
TyRaNiD



Joined: 18 Jan 2004
Posts: 918

PostPosted: Tue May 26, 2009 3:04 am    Post subject: Reply with quote

The simplest way is to put the number into something like windows calc and then change to hex, you will get something like 0xFFFFFFFF8002013A, you only care about the lower 32bits. Or when you print it out of your program use unsigned hex formatting with printf (i.e. %x). Or to be technically correct add one to the number, remove the negative sign and do a bitwise not on the number, re: http://en.wikipedia.org/wiki/Twos_Compliment :)

As for the other stuff, well objdump (actually psp-objdump for the toolchain) is just a tool which can dump things about an object file. If you first build your code, then execute "psp-objdump -t yourprog.elf" (only use the elf, not the prx) it will print a list of symbols. Search for the symbol which contains the name of the function you want, it will probably look like garbage.

For example, I want to export "int exported_func(const char* str)". I compile the code and run psp-objdump to get the symbols. Searching for exported_func in the output gives me the mangled symbol "_Z13exported_funcPKc" now just put that in your exports.exp file as is and you can import functions with C++ prototype forms.
Back to top
View user's profile Send private message
coolkehon



Joined: 20 Oct 2008
Posts: 355

PostPosted: Tue May 26, 2009 4:32 am    Post subject: Reply with quote

how can i tell it to output what it prints to a text file so that i can read it because the terminal buffer isnt long enough

edit:
nvm found it
http://ubuntuforums.org/showthread.php?t=138349

wow all this time and it was that simple man well good to know

edit: lol it compiled i will test when i get time cool thanks hope it works also app that will be using compiled using the old name (initconf_setup) too
p.s: only using initconf_setup so far
Back to top
View user's profile Send private message MSN Messenger
coolkehon



Joined: 20 Oct 2008
Posts: 355

PostPosted: Tue May 26, 2009 6:34 am    Post subject: Reply with quote

ok i tryed it and it still doesnt work this time i uploaded as to not clog up the page here is the source also no one ever answered the question what is the psp's "constructor" for a user app that will enable me to install patches at startup and call functions that are kernel

direct from my site
http://kehon.izfree.com/downloads/initconf_help.zip
sendspace
http://www.sendspace.com/file/ezqih9
Back to top
View user's profile Send private message MSN Messenger
Torch



Joined: 28 May 2008
Posts: 842

PostPosted: Tue May 26, 2009 6:44 pm    Post subject: Reply with quote

Its always module_start(...) if you specify mnoctr0 and mnostartfiles.
Back to top
View user's profile Send private message
coolkehon



Joined: 20 Oct 2008
Posts: 355

PostPosted: Wed May 27, 2009 6:42 am    Post subject: Reply with quote

and if i have crto included then what
also i believe i need crto for c++ dont i
Back to top
View user's profile Send private message MSN Messenger
coolkehon



Joined: 20 Oct 2008
Posts: 355

PostPosted: Fri May 29, 2009 9:03 am    Post subject: Reply with quote

i got the prx to work with exporting c/c++ functions but it crashes when trying to return strings also what is the constructer i know someone has to know i think i saw it somewhere a long time ago it has something like __atribute__("constructor") but dont know how to use that and what exactly it is
Back to top
View user's profile Send private message MSN Messenger
Jim



Joined: 02 Jul 2005
Posts: 487
Location: Sydney

PostPosted: Fri May 29, 2009 4:41 pm    Post subject: Reply with quote

You probably want to look at the string.c_str() method. Returning C++ strings back to C land is going to be bad.

The constructor is the method that's called when someone goes
class MYOBJECT {

MYOBJECT()
{
...I am the constructor
};

~MYOBJECT()
{
...I am the destructor
};

...
}
....
int main(void)
{
MYOBJECT *instance_of_class = new MYOBJECT;
...
}

This is really basic stuff you need to know before you mess with PSP.

Jim
_________________
http://www.dbfinteractive.com
Back to top
View user's profile Send private message Visit poster's website
coolkehon



Joined: 20 Oct 2008
Posts: 355

PostPosted: Sat May 30, 2009 5:28 am    Post subject: Reply with quote

i know that and i already knew / know why strings crash its because of the allocating of memory and freeing when it gets into a another prx / module it tries to free itself in the deconstructor but the memory is inside of the prx and theorefore is not freed correctly and causes all kinds of errors when accessing memory and stuff
Back to top
View user's profile Send private message MSN Messenger
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