forked from pspdev/prxtool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.C
1093 lines (966 loc) · 26.6 KB
/
main.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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***************************************************************
* PRXTool : Utility for PSP executables.
* (c) TyRaNiD 2k5
*
* main.C - Main function for PRXTool
***************************************************************/
#include <stdio.h>
#include <ctype.h>
#include <unistd.h>
#include <cassert>
#include <sys/stat.h>
#include "SerializePrxToIdc.h"
#include "SerializePrxToXml.h"
#include "SerializePrxToMap.h"
#include "ProcessPrx.h"
#include "output.h"
#include "getargs.h"
#define PRXTOOL_VERSION "1.1"
enum OutputMode
{
OUTPUT_NONE = 0,
OUTPUT_IDC = 1,
OUTPUT_MAP = 2,
OUTPUT_XML = 3,
OUTPUT_ELF = 4,
OUTPUT_STUB = 6,
OUTPUT_DEP = 7,
OUTPUT_MOD = 8,
OUTPUT_PSTUB = 9,
OUTPUT_IMPEXP = 10,
OUTPUT_SYMBOLS = 11,
OUTPUT_DISASM = 12,
OUTPUT_XMLDB = 13,
OUTPUT_ENT = 14,
};
static char **g_ppInfiles;
static int g_iInFiles;
static char *g_pOutfile;
static char *g_pNamefile;
static char *g_pFuncfile;
static bool g_blDebug;
static OutputMode g_outputMode;
static u32 g_iSMask;
static int g_newstubs;
static u32 g_dwBase;
static const char *g_disopts = "";
static char g_namepath[PATH_MAX];
static char g_funcpath[PATH_MAX];
static bool g_loadbin = false;
static bool g_xmlOutput = false;
static bool g_aliasOutput = false;
static const char *g_pDbTitle;
static unsigned int g_database = 0;
int do_serialize(const char *arg)
{
int i;
i = 0;
g_iSMask = 0;
while(arg[i])
{
switch(tolower(arg[i]))
{
case 'i' : g_iSMask |= SERIALIZE_IMPORTS;
break;
case 'x' : g_iSMask |= SERIALIZE_EXPORTS;
break;
case 'r' : g_iSMask |= SERIALIZE_RELOCS;
break;
case 's' : g_iSMask |= SERIALIZE_SECTIONS;
break;
case 'l' : g_iSMask |= SERIALIZE_DOSYSLIB;
break;
default: COutput::Printf(LEVEL_WARNING,
"Unknown serialize option '%c'\n",
tolower(arg[i]));
return 0;
};
i++;
}
return 1;
}
int do_xmldb(const char *arg)
{
g_pDbTitle = arg;
g_outputMode = OUTPUT_XMLDB;
return 1;
}
static struct ArgEntry cmd_options[] = {
{"output", 'o', ARG_TYPE_STR, ARG_OPT_REQUIRED, (void*) &g_pOutfile, 0,
"outfile : Outputfile. If not specified uses stdout"},
{"idcout", 'c', ARG_TYPE_INT, ARG_OPT_NONE, (void*) &g_outputMode, OUTPUT_IDC,
" : Output an IDC file (default)"},
{"mapout", 'a', ARG_TYPE_INT, ARG_OPT_NONE, (void*) &g_outputMode, OUTPUT_MAP,
" : Output a MAP file"},
{"xmlout", 'x', ARG_TYPE_INT, ARG_OPT_NONE, (void*) &g_outputMode, OUTPUT_XML,
" : Output an XML file"},
{"elfout", 'e', ARG_TYPE_INT, ARG_OPT_NONE, (void*) &g_outputMode, OUTPUT_ELF,
" : Output an ELF from a PRX"},
{"debug", 'd', ARG_TYPE_BOOL, ARG_OPT_NONE, (void*) &g_blDebug, true,
" : Enable debug mode"},
{"serial", 's', ARG_TYPE_FUNC, ARG_OPT_REQUIRED, (void*) &do_serialize, 0,
"ixrsl : Specify what to serialize (Imports,Exports,Relocs,Sections,SyslibExp)"},
{"xmlfile", 'n', ARG_TYPE_STR, ARG_OPT_REQUIRED, (void*) &g_pNamefile, 0,
"imp.xml : Specify a XML file containing the NID tables"},
{"xmldis", 'g', ARG_TYPE_BOOL, ARG_OPT_NONE, (void*) &g_xmlOutput, true,
" : Enable XML disassembly output mode"},
{"xmldb", 'w', ARG_TYPE_FUNC, ARG_OPT_REQUIRED, (void*) &do_xmldb, 0,
"title : Output the PRX(es) as an XML database disassembly with a title" },
{"stubs", 't', ARG_TYPE_INT, ARG_OPT_NONE, (void*) &g_outputMode, OUTPUT_STUB,
" : Emit stub files for the XML file passed on the command line"},
{"prxstubs", 'u', ARG_TYPE_INT, ARG_OPT_NONE, (void*) &g_outputMode, OUTPUT_PSTUB,
" : Emit stub files based on the exports of the specified PRX files" },
{"newstubs", 'k', ARG_TYPE_BOOL, ARG_OPT_NONE, (void*) &g_newstubs, true,
" : Emit new style stubs for the SDK"},
{"depends", 'q', ARG_TYPE_INT, ARG_OPT_NONE, (void*) &g_outputMode, OUTPUT_DEP,
" : Print PRX dependencies. (Should have loaded an XML file to be useful"},
{"modinfo", 'm', ARG_TYPE_INT, ARG_OPT_NONE, (void*) &g_outputMode, OUTPUT_MOD,
" : Print the module and library information to screen"},
{"impexp", 'f', ARG_TYPE_INT, ARG_OPT_NONE, (void*) &g_outputMode, OUTPUT_IMPEXP,
" : Print the imports and exports of a prx"},
{"exports", 'p', ARG_TYPE_INT, ARG_OPT_NONE, (void*) &g_outputMode, OUTPUT_ENT,
" : Output an export file (.exp)"},
{"disasm", 'w', ARG_TYPE_INT, ARG_OPT_NONE, (void*) &g_outputMode, OUTPUT_DISASM,
" : Disasm the executable sections of the files (if more than one file output name is automatic)"},
{"disopts", 'i', ARG_TYPE_STR, ARG_OPT_REQUIRED, (void*) &g_disopts, 0,
"opts : Specify options for disassembler"},
{"binary", 'b', ARG_TYPE_BOOL, ARG_OPT_NONE, (void*) &g_loadbin, true,
" : Load the file as binary for disassembly"},
{"database", 'l', ARG_TYPE_INT, ARG_OPT_REQUIRED, (void*) &g_database, 0,
" : Specify the offset of the data section in the file for binary disassembly"},
{"reloc", 'r', ARG_TYPE_INT, ARG_OPT_REQUIRED, (void*) &g_dwBase, 0,
"addr : Relocate the PRX to a different address"},
{"symbols", 'y', ARG_TYPE_INT, ARG_OPT_NONE, (void*) &g_outputMode, OUTPUT_SYMBOLS,
"Output a symbol file based on the input file"},
{"funcs", 'z', ARG_TYPE_STR, ARG_OPT_REQUIRED, (void*) &g_pFuncfile, 0,
" : Specify a functions file for disassembly"},
{"alias", 'A', ARG_TYPE_BOOL, ARG_OPT_NONE, (void*) &g_aliasOutput, true,
" : Print aliases when using -f mode" },
};
void DoOutput(OutputLevel level, const char *str)
{
switch(level)
{
case LEVEL_INFO: fprintf(stderr, "%s", str);
break;
case LEVEL_WARNING: fprintf(stderr, "Warning: %s", str);
break;
case LEVEL_ERROR: fprintf(stderr, "Error: %s", str);
break;
case LEVEL_DEBUG: fprintf(stderr, "Debug: %s", str);
break;
default: fprintf(stderr, "Unknown Level: %s", str);
break;
};
}
void init_arguments()
{
const char *home;
g_ppInfiles = NULL;
g_iInFiles = 0;
g_pOutfile = NULL;
g_blDebug = false;
g_outputMode = OUTPUT_IDC;
g_iSMask = SERIALIZE_ALL & ~SERIALIZE_SECTIONS;
g_newstubs = 0;
g_dwBase = 0;
memset(g_namepath, 0, sizeof(g_namepath));
memset(g_funcpath, 0, sizeof(g_funcpath));
home = getenv("HOME");
if(home)
{
struct stat s;
snprintf(g_namepath, sizeof(g_namepath), "%s/.prxtool/psplibdoc.xml", home);
if(stat(g_namepath, &s) == 0)
{
g_pNamefile = g_namepath;
}
snprintf(g_funcpath, sizeof(g_funcpath), "%s/.prxtool/functions.txt", home);
if(stat(g_funcpath, &s) == 0)
{
g_pFuncfile = g_funcpath;
}
}
}
int process_args(int argc, char **argv)
{
init_arguments();
g_ppInfiles = GetArgs(&argc, argv, cmd_options, ARG_COUNT(cmd_options));
if((g_ppInfiles) && (argc > 0))
{
g_iInFiles = argc;
}
else
{
return 0;
}
return 1;
}
void print_help()
{
unsigned int i;
COutput::Printf(LEVEL_INFO, "Usage: prxtool [options...] file\n");
COutput::Printf(LEVEL_INFO, "Options:\n");
for(i = 0; i < ARG_COUNT(cmd_options); i++)
{
if(cmd_options[i].help)
{
COutput::Printf(LEVEL_INFO, "--%-10s -%c %s\n", cmd_options[i].full, cmd_options[i].ch, cmd_options[i].help);
}
}
COutput::Printf(LEVEL_INFO, "\n");
COutput::Printf(LEVEL_INFO, "Disassembler Options:\n");
COutput::Printf(LEVEL_INFO, "x - Print immediates all in hex (not just appropriate ones\n");
COutput::Printf(LEVEL_INFO, "d - When combined with 'x' prints the hex as signed\n");
COutput::Printf(LEVEL_INFO, "r - Print CPU registers using rN format rather than mnemonics (i.e. $a0)\n");
COutput::Printf(LEVEL_INFO, "s - Print the PC as a symbol if possible\n");
COutput::Printf(LEVEL_INFO, "m - Disable macro instructions (e.g. nop, beqz etc.\n");
COutput::Printf(LEVEL_INFO, "w - Indicate PC, opcode information goes after the instruction disasm\n");
}
void output_elf(const char *file, FILE *out_fp)
{
CProcessPrx prx(g_dwBase);
COutput::Printf(LEVEL_INFO, "Loading %s\n", file);
if(prx.LoadFromFile(file) == false)
{
COutput::Puts(LEVEL_ERROR, "Couldn't load prx file structures\n");
}
else
{
if(prx.PrxToElf(out_fp) == false)
{
COutput::Puts(LEVEL_ERROR, "Failed to create a fixed up ELF\n");
}
}
}
int compare_symbols(const void *left, const void *right)
{
ElfSymbol *pLeft, *pRight;
pLeft = (ElfSymbol *) left;
pRight = (ElfSymbol *) right;
return ((int) pLeft->value) - ((int) pRight->value);
}
void output_symbols(const char *file, FILE *out_fp)
{
CProcessPrx prx(g_dwBase);
COutput::Printf(LEVEL_INFO, "Loading %s\n", file);
if(prx.LoadFromFile(file) == false)
{
COutput::Puts(LEVEL_ERROR, "Couldn't load elf file structures");
}
else
{
ElfSymbol *pSymbols;
ElfSymbol *pSymCopy;
SymfileHeader fileHead;
int iSymCount;
int iSymCopyCount;
int iStrSize;
int iStrPos;
pSymbols = prx.GetSymbols(iSymCount);
if(pSymbols != NULL)
{
SAFE_ALLOC(pSymCopy, ElfSymbol[iSymCount]);
if(pSymCopy)
{
iSymCopyCount = 0;
iStrSize = 0;
iStrPos = 0;
/* Calculate the sizes */
for(int i = 0; i < iSymCount; i++)
{
int type;
type = ELF32_ST_TYPE(pSymbols[i].info);
if(((type == STT_FUNC) || (type == STT_OBJECT)) && (strlen(pSymbols[i].symname) > 0))
{
memcpy(&pSymCopy[iSymCopyCount], &pSymbols[i], sizeof(ElfSymbol));
iSymCopyCount++;
iStrSize += strlen(pSymbols[i].symname) + 1;
}
}
COutput::Printf(LEVEL_DEBUG, "Removed %d symbols, leaving %d\n", iSymCount - iSymCopyCount, iSymCopyCount);
COutput::Printf(LEVEL_DEBUG, "String size %d\n", iSymCount - iSymCopyCount, iSymCopyCount);
qsort(pSymCopy, iSymCopyCount, sizeof(ElfSymbol), compare_symbols);
memcpy(fileHead.magic, SYMFILE_MAGIC, 4);
memcpy(fileHead.modname, prx.GetModuleInfo()->name, PSP_MODULE_MAX_NAME);
SW(fileHead.symcount, iSymCopyCount);
SW(fileHead.strstart, sizeof(fileHead) + (sizeof(SymfileEntry)*iSymCopyCount));
SW(fileHead.strsize, iStrSize);
fwrite(&fileHead, 1, sizeof(fileHead), out_fp);
for(int i = 0; i < iSymCopyCount; i++)
{
SymfileEntry sym;
SW(sym.name, iStrPos);
SW(sym.addr, pSymCopy[i].value);
SW(sym.size, pSymCopy[i].size);
iStrPos += strlen(pSymCopy[i].symname)+1;
fwrite(&sym, 1, sizeof(sym), out_fp);
}
/* Write out string table */
for(int i = 0; i < iSymCopyCount; i++)
{
fwrite(pSymCopy[i].symname, 1, strlen(pSymCopy[i].symname)+1, out_fp);
}
delete pSymCopy;
}
else
{
COutput::Puts(LEVEL_ERROR, "Could not allocate memory for symbol copy\n");
}
}
else
{
COutput::Puts(LEVEL_ERROR, "No symbols available");
}
}
}
void output_disasm(const char *file, FILE *out_fp, CNidMgr *nids)
{
CProcessPrx prx(g_dwBase);
bool blRet;
COutput::Printf(LEVEL_INFO, "Loading %s\n", file);
prx.SetNidMgr(nids);
if(g_loadbin)
{
blRet = prx.LoadFromBinFile(file, g_database);
}
else
{
blRet = prx.LoadFromFile(file);
}
if(g_xmlOutput)
{
prx.SetXmlDump();
}
if(blRet == false)
{
COutput::Puts(LEVEL_ERROR, "Couldn't load elf file structures");
}
else
{
prx.Dump(out_fp, g_disopts);
}
}
void output_xmldb(const char *file, FILE *out_fp, CNidMgr *nids)
{
CProcessPrx prx(g_dwBase);
bool blRet;
COutput::Printf(LEVEL_INFO, "Loading %s\n", file);
prx.SetNidMgr(nids);
if(g_loadbin)
{
blRet = prx.LoadFromBinFile(file, g_database);
}
else
{
blRet = prx.LoadFromFile(file);
}
if(blRet == false)
{
COutput::Puts(LEVEL_ERROR, "Couldn't load elf file structures");
}
else
{
prx.DumpXML(out_fp, g_disopts);
}
}
void serialize_file(const char *file, CSerializePrx *pSer, CNidMgr *pNids)
{
CProcessPrx prx(g_dwBase);
assert(pSer != NULL);
prx.SetNidMgr(pNids);
COutput::Printf(LEVEL_INFO, "Loading %s\n", file);
if(prx.LoadFromFile(file) == false)
{
COutput::Puts(LEVEL_ERROR, "Couldn't load prx file structures\n");
}
else
{
pSer->SerializePrx(prx, g_iSMask);
}
}
void output_mods(const char *file, CNidMgr *pNids)
{
CProcessPrx prx(g_dwBase);
prx.SetNidMgr(pNids);
if(prx.LoadFromFile(file) == false)
{
COutput::Puts(LEVEL_ERROR, "Couldn't load prx file structures\n");
}
else
{
PspModule *pMod;
PspLibExport *pExport;
PspLibImport *pImport;
int count;
pMod = prx.GetModuleInfo();
COutput::Puts(LEVEL_INFO, "Module information\n");
COutput::Printf(LEVEL_INFO, "Name: %s\n", pMod->name);
COutput::Printf(LEVEL_INFO, "Attrib: %04X\n", pMod->info.flags & 0xFFFF);
COutput::Printf(LEVEL_INFO, "Version: %d.%d\n",
(pMod->info.flags >> 24) & 0xFF, (pMod->info.flags >> 16) & 0xFF);
COutput::Printf(LEVEL_INFO, "GP: %08X\n", pMod->info.gp);
COutput::Printf(LEVEL_INFO, "\nExports:\n");
pExport = pMod->exp_head;
count = 0;
while(pExport != NULL)
{
COutput::Printf(LEVEL_INFO, "Export %d, Name %s, Functions %d, Variables %d, flags %08X\n",
count++, pExport->name, pExport->f_count, pExport->v_count, pExport->stub.flags);
pExport = pExport->next;
}
COutput::Printf(LEVEL_INFO, "\nImports:\n");
pImport = pMod->imp_head;
count = 0;
while(pImport != NULL)
{
COutput::Printf(LEVEL_INFO, "Import %d, Name %s, Functions %d, Variables %d, flags %08X\n",
count++, pImport->name, pImport->f_count, pImport->v_count, pImport->stub.flags);
pImport = pImport->next;
}
}
}
void output_importexport(const char *file, CNidMgr *pNids)
{
CProcessPrx prx(g_dwBase);
int iLoop;
prx.SetNidMgr(pNids);
if(prx.LoadFromFile(file) == false)
{
COutput::Puts(LEVEL_ERROR, "Couldn't load prx file structures\n");
}
else
{
PspModule *pMod;
PspLibExport *pExport;
PspLibImport *pImport;
int count;
pMod = prx.GetModuleInfo();
COutput::Puts(LEVEL_INFO, "Module information\n");
COutput::Printf(LEVEL_INFO, "Name: %s\n", pMod->name);
COutput::Printf(LEVEL_INFO, "Attrib: %04X\n", pMod->info.flags & 0xFFFF);
COutput::Printf(LEVEL_INFO, "Version: %d.%d\n",
(pMod->info.flags >> 24) & 0xFF, (pMod->info.flags >> 16) & 0xFF);
COutput::Printf(LEVEL_INFO, "GP: %08X\n", pMod->info.gp);
COutput::Printf(LEVEL_INFO, "\nExports:\n");
pExport = pMod->exp_head;
count = 0;
while(pExport != NULL)
{
COutput::Printf(LEVEL_INFO, "Export %d, Name %s, Functions %d, Variables %d, flags %08X\n",
count++, pExport->name, pExport->f_count, pExport->v_count, pExport->stub.flags);
if(pExport->f_count > 0)
{
COutput::Printf(LEVEL_INFO, "Functions:\n");
for(iLoop = 0; iLoop < pExport->f_count; iLoop++)
{
COutput::Printf(LEVEL_INFO, "0x%08X [0x%08X] - %s", pExport->funcs[iLoop].nid,
pExport->funcs[iLoop].addr, pExport->funcs[iLoop].name);
if(g_aliasOutput)
{
SymbolEntry *pSym;
pSym = prx.GetSymbolEntryFromAddr(pExport->funcs[iLoop].addr);
if((pSym) && (pSym->alias.size() > 0))
{
if(strcmp(pSym->name.c_str(), pExport->funcs[iLoop].name))
{
COutput::Printf(LEVEL_INFO, " => %s", pSym->name.c_str());
}
else
{
COutput::Printf(LEVEL_INFO, " => %s", pSym->alias[0].c_str());
}
}
}
COutput::Printf(LEVEL_INFO, "\n");
}
}
if(pExport->v_count > 0)
{
COutput::Printf(LEVEL_INFO, "Variables:\n");
for(iLoop = 0; iLoop < pExport->v_count; iLoop++)
{
COutput::Printf(LEVEL_INFO, "0x%08X [0x%08X] - %s\n", pExport->vars[iLoop].nid,
pExport->vars[iLoop].addr, pExport->vars[iLoop].name);
}
}
pExport = pExport->next;
}
COutput::Printf(LEVEL_INFO, "\nImports:\n");
pImport = pMod->imp_head;
count = 0;
while(pImport != NULL)
{
COutput::Printf(LEVEL_INFO, "Import %d, Name %s, Functions %d, Variables %d, flags %08X\n",
count++, pImport->name, pImport->f_count, pImport->v_count, pImport->stub.flags);
if(pImport->f_count > 0)
{
COutput::Printf(LEVEL_INFO, "Functions:\n");
for(iLoop = 0; iLoop < pImport->f_count; iLoop++)
{
COutput::Printf(LEVEL_INFO, "0x%08X [0x%08X] - %s\n",
pImport->funcs[iLoop].nid, pImport->funcs[iLoop].addr,
pImport->funcs[iLoop].name);
}
}
if(pImport->v_count > 0)
{
COutput::Printf(LEVEL_INFO, "Variables:\n");
for(iLoop = 0; iLoop < pImport->v_count; iLoop++)
{
COutput::Printf(LEVEL_INFO, "0x%08X [0x%08X] - %s\n",
pImport->vars[iLoop].nid, pImport->vars[iLoop].addr,
pImport->vars[iLoop].name);
}
}
pImport = pImport->next;
}
}
}
void output_deps(const char *file, CNidMgr *pNids)
{
CProcessPrx prx(g_dwBase);
prx.SetNidMgr(pNids);
if(prx.LoadFromFile(file) == false)
{
COutput::Puts(LEVEL_ERROR, "Couldn't load prx file structures\n");
}
else
{
PspLibImport *pHead;
char path[PATH_MAX];
int i;
i = 0;
COutput::Printf(LEVEL_INFO, "Dependancy list for %s\n", file);
pHead = prx.GetImports();
while(pHead != NULL)
{
if(strlen(pHead->file) > 0)
{
strcpy(path, pHead->file);
}
else
{
snprintf(path, PATH_MAX, "Unknown (%s)", pHead->name);
}
COutput::Printf(LEVEL_INFO, "Dependancy %d for %s: %s\n", i++, pHead->name, path);
pHead = pHead->next;
}
}
}
void write_stub(const char *szDirectory, PspLibExport *pExp, CProcessPrx *pPrx)
{
char szPath[MAXPATH];
FILE *fp;
COutput::Printf(LEVEL_DEBUG, "Library %s\n", pExp->name);
if(pExp->v_count != 0)
{
COutput::Printf(LEVEL_WARNING, "%s: Stub output does not currently support variables\n", pExp->name);
}
strcpy(szPath, szDirectory);
strcat(szPath, pExp->name);
strcat(szPath, ".S");
fp = fopen(szPath, "w");
if(fp != NULL)
{
fprintf(fp, "\t.set noreorder\n\n");
fprintf(fp, "#include \"pspstub.s\"\n\n");
fprintf(fp, "\tSTUB_START\t\"%s\",0x%08X,0x%08X\n", pExp->name, pExp->stub.flags, (pExp->f_count << 16) | 5);
for(int i = 0; i < pExp->f_count; i++)
{
SymbolEntry *pSym;
if(pPrx)
{
pSym = pPrx->GetSymbolEntryFromAddr(pExp->funcs[i].addr);
}
else
{
pSym = NULL;
}
if((g_aliasOutput) && (pSym) && (pSym->alias.size() > 0))
{
if(strcmp(pSym->name.c_str(), pExp->funcs[i].name))
{
fprintf(fp, "\tSTUB_FUNC_WITH_ALIAS\t0x%08X,%s,%s\n", pExp->funcs[i].nid, pExp->funcs[i].name,
pSym->name.c_str());
}
else
{
fprintf(fp, "\tSTUB_FUNC_WITH_ALIAS\t0x%08X,%s,%s\n", pExp->funcs[i].nid, pExp->funcs[i].name,
pSym->alias[0].c_str());
}
}
else
{
fprintf(fp, "\tSTUB_FUNC\t0x%08X,%s\n", pExp->funcs[i].nid, pExp->funcs[i].name);
}
}
fprintf(fp, "\tSTUB_END\n");
fclose(fp);
}
}
void write_ent(PspLibExport *pExp, FILE *fp)
{
char szPath[MAXPATH];
COutput::Printf(LEVEL_DEBUG, "Library %s\n", pExp->name);
if(fp != NULL)
{
int i;
char *nidName = (char*)malloc(strlen(pExp->name) + 10);
fprintf(fp, "PSP_EXPORT_START(%s, 0x%04X, 0x%04X)\n", pExp->name, pExp->stub.flags & 0xFFFF, pExp->stub.flags >> 16);
for(i = 0; i < pExp->f_count; i++)
{
sprintf(nidName, "%s_%08X", pExp->name, pExp->funcs[i].nid);
if (strcmp(nidName, pExp->funcs[i].name) == 0)
fprintf(fp, "PSP_EXPORT_FUNC_NID(%s, 0x%08X)\n", pExp->funcs[i].name, pExp->funcs[i].nid);
else
fprintf(fp, "PSP_EXPORT_FUNC_HASH(%s)\n", pExp->funcs[i].name);
}
for (i = 0; i < pExp->v_count; i++)
{
sprintf(nidName, "%s_%08X", pExp->name, pExp->vars[i].nid);
if (strcmp(nidName, pExp->vars[i].name) == 0)
fprintf(fp, "PSP_EXPORT_VAR_NID(%s, 0x%08X)\n", pExp->vars[i].name, pExp->vars[i].nid);
else
fprintf(fp, "PSP_EXPORT_VAR_HASH(%s)\n", pExp->vars[i].name);
}
fprintf(fp, "PSP_EXPORT_END\n\n");
free(nidName);
}
}
void write_stub_new(const char *szDirectory, PspLibExport *pExp, CProcessPrx *pPrx)
{
char szPath[MAXPATH];
FILE *fp;
COutput::Printf(LEVEL_DEBUG, "Library %s\n", pExp->name);
if(pExp->v_count != 0)
{
COutput::Printf(LEVEL_WARNING, "%s: Stub output does not currently support variables\n", pExp->name);
}
strcpy(szPath, szDirectory);
strcat(szPath, pExp->name);
strcat(szPath, ".S");
fp = fopen(szPath, "w");
if(fp != NULL)
{
fprintf(fp, "\t.set noreorder\n\n");
fprintf(fp, "#include \"pspimport.s\"\n\n");
fprintf(fp, "// Build List\n");
fprintf(fp, "// %s_0000.o ", pExp->name);
for(int i = 0; i < pExp->f_count; i++)
{
fprintf(fp, "%s_%04d.o ", pExp->name, i + 1);
}
fprintf(fp, "\n\n");
fprintf(fp, "#ifdef F_%s_0000\n", pExp->name);
fprintf(fp, "\tIMPORT_START\t\"%s\",0x%08X\n", pExp->name, pExp->stub.flags);
fprintf(fp, "#endif\n");
for(int i = 0; i < pExp->f_count; i++)
{
SymbolEntry *pSym;
fprintf(fp, "#ifdef F_%s_%04d\n", pExp->name, i + 1);
if(pPrx)
{
pSym = pPrx->GetSymbolEntryFromAddr(pExp->funcs[i].addr);
}
else
{
pSym = NULL;
}
if((g_aliasOutput) && (pSym) && (pSym->alias.size() > 0))
{
if(strcmp(pSym->name.c_str(), pExp->funcs[i].name))
{
fprintf(fp, "\tIMPORT_FUNC_WITH_ALIAS\t\"%s\",0x%08X,%s,%s\n", pExp->name,
pExp->funcs[i].nid, pExp->funcs[i].name, pSym->name.c_str());
}
else
{
fprintf(fp, "\tIMPORT_FUNC_WITH_ALIAS\t\"%s\",0x%08X,%s,%s\n", pExp->name,
pExp->funcs[i].nid, pExp->funcs[i].name, pSym->alias[0].c_str());
}
}
else
{
fprintf(fp, "\tIMPORT_FUNC\t\"%s\",0x%08X,%s\n", pExp->name, pExp->funcs[i].nid, pExp->funcs[i].name);
}
fprintf(fp, "#endif\n");
}
fclose(fp);
}
}
void output_stubs_prx(const char *file, CNidMgr *pNids)
{
CProcessPrx prx(g_dwBase);
prx.SetNidMgr(pNids);
if(prx.LoadFromFile(file) == false)
{
COutput::Puts(LEVEL_ERROR, "Couldn't load prx file structures\n");
}
else
{
PspLibExport *pHead;
COutput::Printf(LEVEL_INFO, "Dependancy list for %s\n", file);
pHead = prx.GetExports();
while(pHead != NULL)
{
if(strcmp(pHead->name, PSP_SYSTEM_EXPORT) != 0)
{
if(g_newstubs)
{
write_stub_new("", pHead, &prx);
}
else
{
write_stub("", pHead, &prx);
}
}
pHead = pHead->next;
}
}
}
void output_ents(const char *file, CNidMgr *pNids, FILE *f)
{
CProcessPrx prx(g_dwBase);
prx.SetNidMgr(pNids);
if(prx.LoadFromFile(file) == false)
{
COutput::Puts(LEVEL_ERROR, "Couldn't load prx file structures\n");
}
else
{
PspLibExport *pHead;
COutput::Printf(LEVEL_INFO, "Dependancy list for %s\n", file);
pHead = prx.GetExports();
while(pHead != NULL)
{
write_ent(pHead, f);
pHead = pHead->next;
}
}
}
void output_stubs_xml(CNidMgr *pNids)
{
LibraryEntry *pLib = NULL;
PspLibExport *pExp = NULL;
pLib = pNids->GetLibraries();
pExp = new PspLibExport;
while(pLib != NULL)
{
/* Convery the LibraryEntry into a valid PspLibExport */
int i;
memset(pExp, 0, sizeof(PspLibExport));
strcpy(pExp->name, pLib->lib_name);
pExp->f_count = pLib->fcount;
pExp->v_count = pLib->vcount;
pExp->stub.flags = pLib->flags;
for(i = 0; i < pExp->f_count; i++)
{
pExp->funcs[i].nid = pLib->pNids[i].nid;
strcpy(pExp->funcs[i].name, pLib->pNids[i].name);
}
if(g_newstubs)
{
write_stub_new("", pExp, NULL);
}
else
{
write_stub("", pExp, NULL);
}
pLib = pLib->pNext;
}
if(pExp != NULL)
{
delete pExp;
pExp = NULL;
}
}
int main(int argc, char **argv)
{
CSerializePrx *pSer;
CNidMgr nids;
FILE *out_fp;
out_fp = stdout;
COutput::SetOutputHandler(DoOutput);
COutput::Printf(LEVEL_INFO, "PRXTool v%s : (c) TyRaNiD 2k6\n", PRXTOOL_VERSION);
COutput::Printf(LEVEL_INFO, "Built: %s %s\n", __DATE__, __TIME__);
if(process_args(argc, argv))
{
COutput::SetDebug(g_blDebug);
if(g_pOutfile != NULL)
{
switch(g_outputMode)
{
case OUTPUT_ELF :
out_fp = fopen(g_pOutfile, "wb");
break;
default:
out_fp = fopen(g_pOutfile, "wt");
break;
}
if(out_fp == NULL)
{
COutput::Printf(LEVEL_ERROR, "Couldn't open output file %s\n", g_pOutfile);
return 1;
}
}
switch(g_outputMode)
{
case OUTPUT_XML : pSer = new CSerializePrxToXml(out_fp);
break;
case OUTPUT_MAP : pSer = new CSerializePrxToMap(out_fp);
break;
case OUTPUT_IDC : pSer = new CSerializePrxToIdc(out_fp);
break;
default: pSer = NULL;
break;
};
if(g_pNamefile != NULL)
{
(void) nids.AddXmlFile(g_pNamefile);
}
if(g_pFuncfile != NULL)
{
(void) nids.AddFunctionFile(g_pFuncfile);
}
if(g_outputMode == OUTPUT_ELF)
{
output_elf(g_ppInfiles[0], out_fp);
}
else if(g_outputMode == OUTPUT_STUB)
{
CNidMgr nidData;
if(nidData.AddXmlFile(g_ppInfiles[0]))
{
output_stubs_xml(&nidData);
}
}
else if(g_outputMode == OUTPUT_DEP)
{
int iLoop;
for(iLoop = 0; iLoop < g_iInFiles; iLoop++)
{
output_deps(g_ppInfiles[iLoop], &nids);
}
}
else if(g_outputMode == OUTPUT_MOD)
{
int iLoop;
for(iLoop = 0; iLoop < g_iInFiles; iLoop++)
{
output_mods(g_ppInfiles[iLoop], &nids);
}
}
else if(g_outputMode == OUTPUT_PSTUB)
{
int iLoop;
for(iLoop = 0; iLoop < g_iInFiles; iLoop++)
{
output_stubs_prx(g_ppInfiles[iLoop], &nids);
}
}
else if(g_outputMode == OUTPUT_IMPEXP)
{
int iLoop;
for(iLoop = 0; iLoop < g_iInFiles; iLoop++)
{
output_importexport(g_ppInfiles[iLoop], &nids);
}
}
else if(g_outputMode == OUTPUT_SYMBOLS)
{
output_symbols(g_ppInfiles[0], out_fp);
}
else if(g_outputMode == OUTPUT_XMLDB)
{
int iLoop;
fprintf(out_fp, "<?xml version=\"1.0\" ?>\n");
fprintf(out_fp, "<firmware title=\"%s\">\n", g_pDbTitle);
for(iLoop = 0; iLoop < g_iInFiles; iLoop++)
{
output_xmldb(g_ppInfiles[iLoop], out_fp, &nids);
}
fprintf(out_fp, "</firmware>\n");
}
else if(g_outputMode == OUTPUT_ENT)