Skip to content

Commit 6321089

Browse files
author
jim
committed
[PATCH] Add PS2Dis MAP file serializer to prxtool
http://forums.ps2dev.org/viewtopic.php?t=6629
1 parent 9c8cc8c commit 6321089

File tree

5 files changed

+340
-16
lines changed

5 files changed

+340
-16
lines changed

Makefile.am

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ prxtool_SOURCES = \
1717
SerializePrx.C \
1818
SerializePrxToIdc.C \
1919
SerializePrxToXml.C \
20+
SerializePrxToMap.C \
2021
$(TINYXML)/tinyxml.cpp \
2122
$(TINYXML)/tinyxmlparser.cpp \
2223
$(TINYXML)/tinystr.cpp \
@@ -33,6 +34,7 @@ noinst_HEADERS = \
3334
SerializePrx.h \
3435
SerializePrxToIdc.h \
3536
SerializePrxToXml.h \
37+
SerializePrxToMap.h \
3638
VirtualMem.h \
3739
$(TINYXML)/tinystr.h \
3840
$(TINYXML)/tinyxml.h

SerializePrxToMap.C

Lines changed: 273 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,273 @@
1+
/***************************************************************
2+
* PRXTool : Utility for PSP executables.
3+
* (c) TyRaNiD 2k5
4+
*
5+
* SerializePrxToMap.C - Implementation of a class to serialize
6+
* a loaded PRX file to a PS2DIS Map file.
7+
***************************************************************/
8+
9+
#include <stdio.h>
10+
#include "SerializePrxToMap.h"
11+
12+
/* Build a name from a base and extention */
13+
static const char *BuildName(const char* base, const char *ext)
14+
{
15+
static char str_export[512];
16+
17+
snprintf(str_export, sizeof(str_export), "%s_%s", base, ext);
18+
19+
return str_export;
20+
}
21+
22+
static void PrintOffset(FILE *fp, unsigned int addr)
23+
{
24+
fprintf(fp, "%08x:\n", addr);
25+
}
26+
27+
static void PrintComment(FILE *fp, const char *text)
28+
{
29+
fprintf(fp, "# %s\n", text);
30+
}
31+
32+
CSerializePrxToMap::CSerializePrxToMap(FILE *fpOut)
33+
{
34+
m_fpOut = fpOut;
35+
}
36+
37+
CSerializePrxToMap::~CSerializePrxToMap()
38+
{
39+
fflush(m_fpOut);
40+
}
41+
42+
bool CSerializePrxToMap::StartFile()
43+
{
44+
return true;
45+
}
46+
47+
bool CSerializePrxToMap::EndFile()
48+
{
49+
/* Do nothing */
50+
return true;
51+
}
52+
53+
bool CSerializePrxToMap::StartPrx(const char* szFilename, const PspModule *mod, u32 iSMask)
54+
{
55+
int i;
56+
u32 addr;
57+
58+
PrintComment(m_fpOut, "Generated by prxtool");
59+
PrintComment(m_fpOut, "Make sure to \"Load From Address 0xA0\" to skip the ELF header");
60+
PrintComment(m_fpOut, "Make sure to load the module as plain binary, not as ELF");
61+
fprintf(m_fpOut, "# File: %s\n", szFilename);
62+
63+
addr = mod->addr;
64+
65+
PrintOffset(m_fpOut, addr);
66+
fprintf(m_fpOut, ".word\t_module_flags\n");
67+
fprintf(m_fpOut, ".byte\t_module_name\n");
68+
for(i=0; i < (sizeof(mod->name)-2); i++)
69+
fprintf(m_fpOut, ".byte\n");
70+
fprintf(m_fpOut, ".word\t_module_gp\n");
71+
fprintf(m_fpOut, ".word\t_module_exports\n");
72+
fprintf(m_fpOut, ".word\t_module_exp_end\n");
73+
fprintf(m_fpOut, ".word\t_module_imports\n");
74+
fprintf(m_fpOut, ".word\t_module_imp_end\n");
75+
76+
return true;
77+
}
78+
79+
bool CSerializePrxToMap::EndPrx()
80+
{
81+
/* Do nothing */
82+
return true;
83+
}
84+
85+
bool CSerializePrxToMap::StartSects()
86+
{
87+
/* Do nothing */
88+
return true;
89+
}
90+
91+
bool CSerializePrxToMap::SerializeSect(int num, ElfSection &sect)
92+
{
93+
u32 shFlags;
94+
u32 shType;
95+
u32 shAddr;
96+
u32 shSize;
97+
const char *pName;
98+
99+
shFlags = sect.iFlags;
100+
shType = sect.iType;
101+
shAddr = sect.iAddr;
102+
shSize = sect.iSize;
103+
pName = sect.szName;
104+
105+
/* Check if the section is loadable */
106+
if((shFlags & SHF_ALLOC) && ((shType == SHT_PROGBITS) || (shType == SHT_NOBITS)))
107+
{
108+
PrintOffset(m_fpOut, shAddr);
109+
fprintf(m_fpOut, ".word\t%s\t;", pName);
110+
111+
if(shFlags & SHF_EXECINSTR)
112+
{
113+
fprintf(m_fpOut, " SEG_CODE");
114+
}
115+
else
116+
{
117+
if(shType == SHT_NOBITS)
118+
{
119+
fprintf(m_fpOut, " SEG_BSS");
120+
}
121+
else
122+
{
123+
fprintf(m_fpOut, " SEG_DATA");
124+
}
125+
}
126+
127+
fprintf(m_fpOut, " 0x%08x - 0x%08x\n", shAddr, shAddr + shSize);
128+
}
129+
130+
return true;
131+
}
132+
133+
bool CSerializePrxToMap::EndSects()
134+
{
135+
return true;
136+
}
137+
138+
bool CSerializePrxToMap::StartImports()
139+
{
140+
return true;
141+
}
142+
143+
bool CSerializePrxToMap::SerializeImport(int num, const PspLibImport *imp)
144+
{
145+
char str_import[128];
146+
int iLoop;
147+
u32 addr;
148+
snprintf(str_import, sizeof(str_import), "import%d", num);
149+
150+
addr = imp->addr;
151+
152+
if(imp->stub.name != 0)
153+
{
154+
PrintOffset(m_fpOut, addr);
155+
fprintf(m_fpOut, ".word\t%s\t; %s\n", imp->name, BuildName(str_import, "name"));
156+
}
157+
else
158+
{
159+
PrintOffset(m_fpOut, addr);
160+
fprintf(m_fpOut, ".word\t%s\t; %s\n", str_import, BuildName(str_import, "name"));
161+
}
162+
163+
fprintf(m_fpOut, ".word\t%s\n", BuildName(str_import, "flags"));
164+
fprintf(m_fpOut, ".word\t%s\n", BuildName(str_import, "counts"));
165+
fprintf(m_fpOut, ".word\t%s\n", BuildName(str_import, "nids"));
166+
fprintf(m_fpOut, ".word\t%s\n", BuildName(str_import, "funcs"));
167+
168+
for(iLoop = 0; iLoop < imp->f_count; iLoop++)
169+
{
170+
PrintOffset(m_fpOut, imp->funcs[iLoop].nid_addr);
171+
fprintf(m_fpOut, ".word\t%s\t; NID %08x\n", BuildName(str_import, imp->funcs[iLoop].name), imp->funcs[iLoop].nid);
172+
173+
PrintOffset(m_fpOut, imp->funcs[iLoop].addr);
174+
fprintf(m_fpOut, ".code\t%s\n", imp->funcs[iLoop].name);
175+
}
176+
177+
for(iLoop = 0; iLoop < imp->v_count; iLoop++)
178+
{
179+
180+
PrintOffset(m_fpOut, imp->funcs[iLoop].nid_addr);
181+
fprintf(m_fpOut, ".word\t%s\t; NID %08x\n", BuildName(str_import, imp->vars[iLoop].name), imp->vars[iLoop].nid);
182+
183+
PrintOffset(m_fpOut, imp->vars[iLoop].nid_addr + ((imp->v_count + imp->f_count) * 4));
184+
fprintf(m_fpOut, ".word\t%s\n", imp->vars[iLoop].name);
185+
}
186+
187+
return true;
188+
}
189+
190+
bool CSerializePrxToMap::EndImports()
191+
{
192+
return true;
193+
}
194+
195+
bool CSerializePrxToMap::StartExports()
196+
{
197+
return true;
198+
}
199+
200+
bool CSerializePrxToMap::SerializeExport(int num, const PspLibExport *exp)
201+
{
202+
char str_export[128];
203+
int iLoop;
204+
u32 addr;
205+
snprintf(str_export, sizeof(str_export), "export_%d", num);
206+
207+
addr = exp->addr;
208+
209+
if(exp->stub.name != 0)
210+
{
211+
PrintOffset(m_fpOut, addr);
212+
fprintf(m_fpOut, ".word\t%s\t; %s\n", exp->name, BuildName(str_export, "name"));
213+
}
214+
else
215+
{
216+
PrintOffset(m_fpOut, addr);
217+
fprintf(m_fpOut, ".word\t%s\t; %s\n", str_export, BuildName(str_export, "name"));
218+
}
219+
220+
fprintf(m_fpOut, ".word\t%s\n", BuildName(str_export, "flags"));
221+
fprintf(m_fpOut, ".word\t%s\n", BuildName(str_export, "counts"));
222+
fprintf(m_fpOut, ".word\t%s\n", BuildName(str_export, "exports"));
223+
224+
for(iLoop = 0; iLoop < exp->f_count; iLoop++)
225+
{
226+
PrintOffset(m_fpOut, exp->funcs[iLoop].nid_addr);
227+
fprintf(m_fpOut, ".word\t%s\t; NID %08x\n", BuildName(str_export, exp->funcs[iLoop].name), exp->funcs[iLoop].nid);
228+
229+
PrintOffset(m_fpOut, exp->funcs[iLoop].nid_addr + ((exp->v_count + exp->f_count) * 4));
230+
fprintf(m_fpOut, ".word\n");
231+
232+
PrintOffset(m_fpOut, exp->funcs[iLoop].addr);
233+
fprintf(m_fpOut, ".code\t%s\n", exp->funcs[iLoop].name);
234+
}
235+
236+
for(iLoop = 0; iLoop < exp->v_count; iLoop++)
237+
{
238+
PrintOffset(m_fpOut, exp->funcs[iLoop].nid_addr);
239+
fprintf(m_fpOut, ".word\t%s\t; NID %08x\n", BuildName(str_export, exp->vars[iLoop].name), exp->vars[iLoop].nid);
240+
241+
PrintOffset(m_fpOut, exp->vars[iLoop].nid_addr + ((exp->v_count + exp->f_count) * 4));
242+
fprintf(m_fpOut, ".word\t%s\n", exp->vars[iLoop].name);
243+
}
244+
245+
return true;
246+
}
247+
248+
bool CSerializePrxToMap::EndExports()
249+
{
250+
return true;
251+
}
252+
253+
bool CSerializePrxToMap::StartRelocs()
254+
{
255+
return true;
256+
}
257+
258+
bool CSerializePrxToMap::SerializeReloc(int count, const ElfReloc *rel)
259+
{
260+
ElfSection *pDataSect, *pTextSect;
261+
262+
pDataSect = m_currPrx->ElfFindSection(".data");
263+
pTextSect = m_currPrx->ElfFindSection(".text");
264+
265+
fprintf(stderr, "Reloc count %d, %s, data %p, text %p\n", count, rel->secname, pDataSect, pTextSect);
266+
return true;
267+
}
268+
269+
bool CSerializePrxToMap::EndRelocs()
270+
{
271+
return true;
272+
}
273+

SerializePrxToMap.h

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/***************************************************************
2+
* PRXTool : Utility for PSP executables.
3+
* (c) TyRaNiD 2k5
4+
*
5+
* SerializePrxToMap.h - Implementation of a class to serialize
6+
* a loaded PRX file to a PS2DIS Map file.
7+
***************************************************************/
8+
9+
#ifndef __SERIALIZEPRXTOMAP_H__
10+
#define __SERIALIZEPRXTOMAP_H__
11+
12+
#include <stdio.h>
13+
#include "SerializePrx.h"
14+
15+
class CSerializePrxToMap : public CSerializePrx
16+
{
17+
FILE *m_fpOut;
18+
19+
virtual bool StartFile();
20+
virtual bool EndFile();
21+
virtual bool StartPrx(const char *szFilename, const PspModule *pMod, u32 iSMask);
22+
virtual bool EndPrx();
23+
virtual bool StartSects();
24+
virtual bool SerializeSect(int num, ElfSection &sect);
25+
virtual bool EndSects();
26+
virtual bool StartImports();
27+
virtual bool SerializeImport(int num, const PspLibImport *imp);
28+
virtual bool EndImports();
29+
virtual bool StartExports();
30+
virtual bool SerializeExport(int num, const PspLibExport *exp);
31+
virtual bool EndExports();
32+
virtual bool StartRelocs();
33+
virtual bool SerializeReloc(int count, const ElfReloc *rel);
34+
virtual bool EndRelocs();
35+
36+
public:
37+
CSerializePrxToMap(FILE *fpOut);
38+
~CSerializePrxToMap();
39+
};
40+
41+
#endif

config.h.in

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,19 +66,19 @@
6666
/* Define to the version of this package. */
6767
#undef PACKAGE_VERSION
6868

69-
/* The size of a `char', as computed by sizeof. */
69+
/* The size of `char', as computed by sizeof. */
7070
#undef SIZEOF_CHAR
7171

72-
/* The size of a `int', as computed by sizeof. */
72+
/* The size of `int', as computed by sizeof. */
7373
#undef SIZEOF_INT
7474

75-
/* The size of a `long', as computed by sizeof. */
75+
/* The size of `long', as computed by sizeof. */
7676
#undef SIZEOF_LONG
7777

78-
/* The size of a `short', as computed by sizeof. */
78+
/* The size of `short', as computed by sizeof. */
7979
#undef SIZEOF_SHORT
8080

81-
/* The size of a `void*', as computed by sizeof. */
81+
/* The size of `void*', as computed by sizeof. */
8282
#undef SIZEOF_VOIDP
8383

8484
/* Define to 1 if you have the ANSI C header files. */
@@ -100,5 +100,5 @@
100100
#undef inline
101101
#endif
102102

103-
/* Define to `unsigned' if <sys/types.h> does not define. */
103+
/* Define to `unsigned int' if <sys/types.h> does not define. */
104104
#undef size_t

0 commit comments

Comments
 (0)