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