-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpsp-prxgen.c
1074 lines (907 loc) · 27.4 KB
/
psp-prxgen.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
/*
* PSP Software Development Kit - /~https://github.com/pspdev
* -----------------------------------------------------------------------
* Licensed under the BSD license, see LICENSE in PSPSDK root for details.
*
* psp-prxgen.c - Simple program to build a PRX file (and strip at the same time)
*
* Copyright (c) 2005 James Forshaw <tyranid@gmail.com>
*
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "types.h"
#include "elftypes.h"
#include "prxtypes.h"
/* Arrangement of ELF file after stripping
*
* ELF Header - 52 bytes
* Program Headers
* .text data
* .data data
* Section Headers
* Relocation data
* Section Header String Table
*
* When stripping the sections remove anything which isn't an allocated section or a relocation section.
* The section string section we will rebuild.
*/
static const char *g_outfile;
static const char *g_infile;
static unsigned char *g_elfdata = NULL;
static struct ElfHeader g_elfhead = {0};
static struct ElfSection *g_elfsections = NULL;
static struct ElfSection *g_modinfo = NULL;
static int g_out_sects = 2;
static int g_alloc_size = 0;
static int g_mem_size = 0;
static int g_reloc_size = 0;
static int g_str_size = 1;
/* Base addresses in the Elf */
static int g_phbase = 0;
static int g_allocbase = 0;
static int g_shbase = 0;
static int g_relocbase = 0;
static int g_shstrbase = 0;
static int g_progbits_that_have_zero_addr_size = 0;
static int g_top_addr = 0;
static int g_debugbase = 0;
static int g_debug_size = 0;
static int g_symbol_size = 0;
/* Specifies that the current usage is to the print the pspsdk path */
static int g_verbose = 0;
void print_help(void)
{
fprintf(stderr, "Usage: psp-prxgen [-v] infile.elf outfile.prx\n");
fprintf(stderr, "Options:\n");
fprintf(stderr, "-v, --verbose : Verbose output\n");
}
unsigned char *load_file(const char *file)
{
FILE *fp;
unsigned int size;
unsigned char *data = NULL;
do
{
fp = fopen(file, "rb");
if(fp != NULL)
{
(void) fseek(fp, 0, SEEK_END);
size = ftell(fp);
rewind(fp);
if(size < sizeof(Elf32_Ehdr))
{
fprintf(stderr, "Error, invalid file size\n");
fclose(fp);
break;
}
data = (unsigned char *) malloc(size);
if(data == NULL)
{
fprintf(stderr, "Error, could not allocate memory for ELF\n");
fclose(fp);
break;
}
(void) fread(data, 1, size, fp);
fclose(fp);
}
else
{
fprintf(stderr, "Error, could not find file %s\n", file);
}
}
while(0);
return data;
}
/* Validate the ELF header */
int validate_header(unsigned char *data)
{
Elf32_Ehdr *head;
int ret = 0;
head = (Elf32_Ehdr*) data;
do
{
/* Read in the header structure */
g_elfhead.iMagic = LW(head->e_magic);
g_elfhead.iClass = head->e_class;
g_elfhead.iData = head->e_data;
g_elfhead.iIdver = head->e_idver;
g_elfhead.iType = LH(head->e_type);
g_elfhead.iMachine = LH(head->e_machine);
g_elfhead.iVersion = LW(head->e_version);
g_elfhead.iEntry = LW(head->e_entry);
g_elfhead.iPhoff = LW(head->e_phoff);
g_elfhead.iShoff = LW(head->e_shoff);
g_elfhead.iFlags = LW(head->e_flags);
g_elfhead.iEhsize = LH(head->e_ehsize);
g_elfhead.iPhentsize = LH(head->e_phentsize);
g_elfhead.iPhnum = LH(head->e_phnum);
g_elfhead.iShentsize = LH(head->e_shentsize);
g_elfhead.iShnum = LH(head->e_shnum);
g_elfhead.iShstrndx = LH(head->e_shstrndx);
if(g_verbose)
{
fprintf(stderr, "Magic %08X, Class %02X, Data %02X, Idver %02X\n", g_elfhead.iMagic,
g_elfhead.iClass, g_elfhead.iData, g_elfhead.iIdver);
fprintf(stderr, "Type %04X, Machine %04X, Version %08X, Entry %08X\n", g_elfhead.iType,
g_elfhead.iMachine, g_elfhead.iVersion, g_elfhead.iEntry);
fprintf(stderr, "Phoff %08X, Shoff %08X, Flags %08X, Ehsize %08X\n", g_elfhead.iPhoff,
g_elfhead.iShoff, g_elfhead.iFlags, g_elfhead.iEhsize);
fprintf(stderr, "Phentsize %04X, Phnum %04X\n", g_elfhead.iPhentsize, g_elfhead.iPhnum);
fprintf(stderr, "Shentsize %04X, Shnum %08X, Shstrndx %04X\n", g_elfhead.iShentsize,
g_elfhead.iShnum, g_elfhead.iShstrndx);
}
if(g_elfhead.iMagic != ELF_MAGIC)
{
fprintf(stderr, "Error, invalid magic in the header\n");
break;
}
if((g_elfhead.iType != ELF_EXEC_TYPE) && (g_elfhead.iType != ELF_PRX_TYPE))
{
fprintf(stderr, "Error, not EXEC type elf\n");
break;
}
if(g_elfhead.iMachine != ELF_MACHINE_MIPS)
{
fprintf(stderr, "Error, not MIPS type ELF\n");
break;
}
if(g_elfhead.iShnum < g_elfhead.iShstrndx)
{
fprintf(stderr, "Error, number of headers is less than section string index\n");
break;
}
ret = 1;
}
while(0);
return ret;
}
/* Load sections into ram */
int load_sections(unsigned char *data)
{
int ret = 0;
int found_rel = 0;
unsigned int load_addr = 0xFFFFFFFF;
if(g_elfhead.iShnum > 0)
{
do
{
Elf32_Shdr *sect;
int i;
g_elfsections = (struct ElfSection *) malloc(sizeof(struct ElfSection) * g_elfhead.iShnum);
if(g_elfsections == NULL)
{
fprintf(stderr, "Error, could not allocate memory for sections\n");
break;
}
memset(g_elfsections, 0, sizeof(struct ElfSection) * g_elfhead.iShnum);
for(i = 0; i < g_elfhead.iShnum; i++)
{
sect = (Elf32_Shdr *) (g_elfdata + g_elfhead.iShoff + (i * g_elfhead.iShentsize));
g_elfsections[i].iName = LW(sect->sh_name);
g_elfsections[i].iType = LW(sect->sh_type);
g_elfsections[i].iAddr = LW(sect->sh_addr);
g_elfsections[i].iFlags = LW(sect->sh_flags);
g_elfsections[i].iOffset = LW(sect->sh_offset);
g_elfsections[i].iSize = LW(sect->sh_size);
g_elfsections[i].iLink = LW(sect->sh_link);
g_elfsections[i].iInfo = LW(sect->sh_info);
g_elfsections[i].iAddralign = LW(sect->sh_addralign);
g_elfsections[i].iEntsize = LW(sect->sh_entsize);
g_elfsections[i].iIndex = i;
if(g_elfsections[i].iOffset != 0)
{
g_elfsections[i].pData = g_elfdata + g_elfsections[i].iOffset;
}
if(g_elfsections[i].iFlags & SHF_ALLOC)
{
g_elfsections[i].blOutput = 1;
if(g_elfsections[i].iAddr < load_addr)
{
load_addr = g_elfsections[i].iAddr;
}
}
}
/* Okay so we have loaded all the sections, lets find relocations and fix up the names */
// .debug_info isn't getting allocated properly.
for(i = 0; i < g_elfhead.iShnum; i++)
{
// avoid including NULL section
if (i >= 1)
g_elfsections[i].blOutput = 1;
// is this true for every rel section? might cause the issue that sh_info is being set because pRef is only being set once
//if(((g_elfsections[i].iType == SHT_REL) || (g_elfsections[i].iType == SHT_PRXRELOC)) && (g_elfsections[g_elfsections[i].iInfo].iFlags & SHF_ALLOC))
if((g_elfsections[i].iType == SHT_REL) || (g_elfsections[i].iType == SHT_PRXRELOC))
{
g_elfsections[i].pRef = &g_elfsections[g_elfsections[i].iInfo];
found_rel = 1;
g_elfsections[i].blOutput = 1;
}
// give every section their names from their section table
strcpy(g_elfsections[i].szName, (char *) (g_elfsections[g_elfhead.iShstrndx].pData + g_elfsections[i].iName));
if(strcmp(g_elfsections[i].szName, PSP_MODULE_INFO_NAME) == 0)
{
g_modinfo = &g_elfsections[i];
}
else if(strcmp(g_elfsections[i].szName, PSP_MODULE_REMOVE_REL) == 0)
{
/* Don't output .rel.lib.stub relocations */
g_elfsections[i].blOutput = 0;
}
// we are already constructing a new string tab because we are moving around sections
if (strcmp(g_elfsections[i].szName, ELF_SH_STRTAB) == 0)
g_elfsections[i].blOutput = 0;
}
if(g_verbose)
{
for(i = 0; i < g_elfhead.iShnum; i++)
{
fprintf(stderr, "\nSection %d: %s\n", i, g_elfsections[i].szName);
fprintf(stderr, "Name %08X, Type %08X, Flags %08X, Addr %08X\n",
g_elfsections[i].iName, g_elfsections[i].iType,
g_elfsections[i].iFlags, g_elfsections[i].iAddr);
fprintf(stderr, "Offset %08X, Size %08X, Link %08X, Info %08X\n",
g_elfsections[i].iOffset, g_elfsections[i].iSize,
g_elfsections[i].iLink, g_elfsections[i].iInfo);
fprintf(stderr, "Addralign %08X, Entsize %08X pData %p\n",
g_elfsections[i].iAddralign, g_elfsections[i].iEntsize,
g_elfsections[i].pData);
}
fprintf(stderr, "ELF Load Base address %08X\n", load_addr);
}
if(g_modinfo == NULL)
{
fprintf(stderr, "Error, no sceModuleInfo section found\n");
break;
}
if(!found_rel)
{
fprintf(stderr, "Error, found no relocation sections\n");
break;
}
if(load_addr != 0)
{
fprintf(stderr, "Error, ELF not loaded to address 0 (%08X)\n", load_addr);
break;
}
ret = 1;
}
while(0);
}
else
{
fprintf(stderr, "Error, no sections in the ELF\n");
}
return ret;
}
int remove_weak_relocs(struct ElfSection *pReloc, struct ElfSection *pSymbol, struct ElfSection *pString)
{
int iCount;
int iMaxSymbol;
void *pNewRel = NULL;
Elf32_Rel *pInRel;
Elf32_Rel *pOutRel;
Elf32_Sym *pSymData = (Elf32_Sym *) pSymbol->pData;
char *pStrData = NULL;
int iOutput;
int i;
if(pString != NULL)
{
pStrData = (char *) pString->pData;
}
iMaxSymbol = pSymbol->iSize / sizeof(Elf32_Sym);
iCount = pReloc->iSize / sizeof(Elf32_Rel);
pNewRel = malloc(pReloc->iSize);
if(pNewRel == NULL)
{
return 0;
}
pOutRel = (Elf32_Rel *) pNewRel;
pInRel = (Elf32_Rel *) pReloc->pData;
iOutput = 0;
if(g_verbose)
{
fprintf(stderr, "[%s] Processing %d relocations, %d symbols\n", pReloc->szName, iCount, iMaxSymbol);
}
for(i = 0; i < iCount; i++)
{
int iSymbol;
iSymbol = ELF32_R_SYM(LW(pInRel->r_info));
if(g_verbose)
{
fprintf(stderr, "Relocation %d - Symbol %x\n", iOutput, iSymbol);
}
if(iSymbol >= iMaxSymbol)
{
fprintf(stderr, "Warning: Ignoring relocation as cannot find matching symbol\n");
}
else
{
if(g_verbose)
{
if(pStrData != NULL)
{
fprintf(stderr, "Symbol %d - Name %s info %x ndx %x\n", iSymbol, &pStrData[pSymData[iSymbol].st_name],
pSymData[iSymbol].st_info, pSymData[iSymbol].st_shndx);
}
else
{
fprintf(stderr, "Symbol %d - Name %d info %x ndx %x\n", iSymbol, pSymData[iSymbol].st_name,
pSymData[iSymbol].st_info, pSymData[iSymbol].st_shndx);
}
}
/* Remove PC16 relocations (unsupported by PSP, and useless) */
if(LH(pSymData[iSymbol].st_shndx) == 0 || ELF32_R_TYPE(LW(pInRel->r_info)) == R_MIPS_PC16 || ELF32_R_TYPE(LW(pInRel->r_info)) == R_MIPS_GPREL16)
{
if(g_verbose)
{
fprintf(stderr, "Deleting relocation\n");
}
}
else
{
/* We are keeping this relocation, copy it across */
*pOutRel = *pInRel;
pOutRel++;
iOutput++;
}
}
pInRel++;
}
/* If we deleted some relocations */
if(iOutput < iCount)
{
int iSize;
iSize = iOutput * sizeof(Elf32_Rel);
if(g_verbose)
{
fprintf(stderr, "Old relocation size %d, new %d\n", pReloc->iSize, iSize);
}
pReloc->iSize = iSize;
/* If size is zero then delete this section */
if(iSize == 0)
{
pReloc->blOutput = 0;
}
else
{
// Copy across the new relocation data
memcpy(pReloc->pData, pNewRel, pReloc->iSize);
}
}
free(pNewRel);
return 1;
}
/* Let's remove the weak relocations from the list */
int process_relocs(void)
{
int i;
for(i = 0; i < g_elfhead.iShnum; i++)
{
if((g_elfsections[i].blOutput) && (g_elfsections[i].iType == SHT_REL))
{
struct ElfSection *pReloc;
pReloc = &g_elfsections[i];
// TODO:
// could this cause issues for symtab?
if((pReloc->iLink < g_elfhead.iShnum) && (g_elfsections[pReloc->iLink].iType == SHT_SYMTAB))
{
struct ElfSection *pStrings = NULL;
struct ElfSection *pSymbols;
pSymbols = &g_elfsections[pReloc->iLink];
if((pSymbols->iLink < g_elfhead.iShnum) && (g_elfsections[pSymbols->iLink].iType == SHT_STRTAB))
{
pStrings = &g_elfsections[pSymbols->iLink];
}
if(!remove_weak_relocs(pReloc, pSymbols, pStrings))
{
return 0;
}
}
else
{
if(g_verbose)
{
fprintf(stderr, "Ignoring relocation section %d, invalid link number\n", i);
}
}
}
}
return 1;
}
/* Reindex the sections we are keeping */
void reindex_sections(void)
{
int i;
int sect = 1;
for(i = 0; i < g_elfhead.iShnum; i++)
{
if(g_elfsections[i].blOutput)
{
g_elfsections[i].iIndex = sect++;
}
}
}
/* Load an ELF file */
int load_elf(const char *elf)
{
int ret = 0;
do
{
g_elfdata = load_file(elf);
if(g_elfdata == NULL)
{
break;
}
if(!validate_header(g_elfdata))
{
break;
}
if(!load_sections(g_elfdata))
{
break;
}
if(!process_relocs())
{
break;
}
reindex_sections();
ret = 1;
}
while(0);
return ret;
}
int calculate_outsize(void)
{
/* out_sects starts at two for the null section and the section string table */
int out_sects = 2;
int alloc_size = 0;
int reloc_size = 0;
int mem_size = 0;
/* 1 for the NUL for the NULL section */
int str_size = 1;
int i;
int total_size_of_all_sections = 0;
int total_size_of_symbol_and_string_section = 0;
/* Calculate how big our output file needs to be */
/* We have elf header + 1 PH + allocated data + section headers + relocation data */
/* Note that the ELF should be based from 0, we use this to calculate the alloc and mem sizes */
/* Skip null section */
for(i = 1; i < g_elfhead.iShnum; i++)
{
if(g_elfsections[i].blOutput)
{
if(g_elfsections[i].iType == SHT_PROGBITS)
{
unsigned int top_addr;
top_addr = g_elfsections[i].iAddr + g_elfsections[i].iSize;
// if a section has a higher address than alloc_size, increase alloc_size
if(top_addr > alloc_size)
{
alloc_size = top_addr;
}
if(top_addr > mem_size)
{
mem_size = top_addr;
}
// some progbit sections have their address set to 0, allocate section size
// we will put them into the correct offset later
if (g_elfsections[i].iAddr == 0 && (strcmp(g_elfsections[i].szName, ".text") != 0))
{
if(g_verbose)
printf("set %s fakeRel on\n", g_elfsections[i].szName);
g_elfsections[i].fakeRel = 1;
g_progbits_that_have_zero_addr_size += g_elfsections[i].iSize;
}
str_size += strlen(g_elfsections[i].szName) + 1;
}
else if((g_elfsections[i].iType == SHT_REL) || (g_elfsections[i].iType == SHT_PRXRELOC))
{
reloc_size += g_elfsections[i].iSize;
str_size += strlen(g_elfsections[i].szName) + 1;
}
else
{
unsigned int top_addr;
top_addr = g_elfsections[i].iAddr + g_elfsections[i].iSize;
if(top_addr > mem_size)
{
mem_size = top_addr;
}
str_size += strlen(g_elfsections[i].szName) + 1;
}
if(g_verbose)
printf("%d, alloc_size: %04x\n", i, alloc_size);
if (g_elfsections[i].iType == SHT_MIPS_DWARF)
total_size_of_all_sections += g_elfsections[i].iSize;
if ((g_elfsections[i].iType == SHT_STRTAB) || (g_elfsections[i].iType == SHT_SYMTAB))
total_size_of_symbol_and_string_section += g_elfsections[i].iSize;
out_sects++;
}
}
alloc_size = (alloc_size + 3) & ~3;
mem_size = (mem_size + 3) & ~3;
str_size = (str_size + 3) & ~3;
str_size += strlen(ELF_SH_STRTAB) + 1;
if(g_verbose)
{
fprintf(stderr, "Out_sects %d, alloc_size %d, reloc_size %d, str_size %d, mem_size %d\n",
out_sects, alloc_size, reloc_size, str_size, mem_size);
printf("total_size_of_all_sections: %d\n", total_size_of_all_sections);
}
/* Save them for future use */
g_out_sects = out_sects;
g_alloc_size = alloc_size;
g_reloc_size = reloc_size;
g_mem_size = mem_size;
g_str_size = str_size;
// this probably isnt the correct size we will see
// we aren't also including section headers per dwarf section, could be that its already being allocated for
// we are also not allocating for
g_debug_size = total_size_of_all_sections;
g_symbol_size = total_size_of_symbol_and_string_section;
/* Lets build the offsets */
g_phbase = sizeof(Elf32_Ehdr);
// when we are allocating sections for dwarf & symtab etc they are getting overwritten by the base addresses
// we should actually create our own allocation base for dwarf and friends so that we aren't being overwritten
// debug_info for example contains the section of shstrtab since its defining its own base
g_allocbase = (g_phbase + sizeof(Elf32_Shdr) + 0xF) & ~0xF; /* The allocated data needs to be 16 byte aligned */
g_shbase = g_allocbase + g_alloc_size;
g_relocbase = g_shbase + (g_out_sects * sizeof(Elf32_Shdr));
g_shstrbase = g_relocbase + g_reloc_size;
g_debugbase = g_shstrbase + g_str_size;
if(g_verbose)
{
printf("PHBase %08X, AllocBase %08X, SHBase %08X\n", g_phbase, g_allocbase, g_shbase);
printf("Relocbase %08X, Shstrbase %08X\n", g_relocbase, g_shstrbase);
printf("Total size %d\n", g_shstrbase + g_str_size);
}
return (g_shstrbase + g_str_size + g_debug_size + g_symbol_size + g_progbits_that_have_zero_addr_size);
}
/* Output the ELF header */
void output_header(unsigned char *data)
{
Elf32_Ehdr *head;
head = (Elf32_Ehdr*) data;
SW(&head->e_magic, g_elfhead.iMagic);
head->e_class = g_elfhead.iClass;
head->e_data = g_elfhead.iData;
head->e_idver = g_elfhead.iIdver;
SH(&head->e_type, ELF_PRX_TYPE);
SH(&head->e_machine, g_elfhead.iMachine);
SW(&head->e_version, g_elfhead.iVersion);
SW(&head->e_entry, g_elfhead.iEntry);
SW(&head->e_phoff, g_phbase);
SW(&head->e_shoff, g_shbase);
SW(&head->e_flags, g_elfhead.iFlags);
SH(&head->e_ehsize, sizeof(Elf32_Ehdr));
SH(&head->e_phentsize, sizeof(Elf32_Phdr));
SH(&head->e_phnum, 1);
SH(&head->e_shentsize, sizeof(Elf32_Shdr));
SH(&head->e_shnum, g_out_sects);
SH(&head->e_shstrndx, g_out_sects-1);
}
/* Output the program header */
void output_ph(unsigned char *data)
{
// atleast one prgraom header has to exist in order to be loadable, because of relocation entries
Elf32_Phdr *phdr;
struct PspModuleInfo *pModinfo;
int mod_flags;
phdr = (Elf32_Phdr*) data;
pModinfo = (struct PspModuleInfo *) (g_modinfo->pData);
mod_flags = LW(pModinfo->flags);
SW(&phdr->p_type, 1);
/* Starts after the program header */
SW(&phdr->p_offset, g_allocbase);
SW(&phdr->p_vaddr, 0);
// physical address is not used like its described in the ELF documentation.
// physical address is instead set to the offset of .rodata_sceModuleInfo
// it is not the address in memory.
// any subsequent program header has the physical address set to 0.
if(mod_flags & 0x1000) /* Check if this is a kernel module */
{
SW(&phdr->p_paddr, 0x80000000 | (g_modinfo->iAddr + g_allocbase));
}
else
{
SW(&phdr->p_paddr, (g_modinfo->iAddr + g_allocbase));
}
SW(&phdr->p_filesz, g_alloc_size);
SW(&phdr->p_memsz, g_mem_size);
SW(&phdr->p_flags, 7); // read & execute ? // changed it from 5 to 7 since its 0x7 on devkit prx's
SW(&phdr->p_align, 0x10); // must be atleast aligned to 16 byte boundaries
// otherwise the kernel ELF loader will fail
}
/* Output the allocated sections */
void output_alloc(unsigned char *data)
{
for(int i = 0; i < g_elfhead.iShnum; i++)
if((g_elfsections[i].blOutput) && (g_elfsections[i].iType == SHT_PROGBITS) && !g_elfsections[i].fakeRel)
memcpy(&data[g_elfsections[i].iAddr], g_elfsections[i].pData, g_elfsections[i].iSize);
}
/* Output the section headers */
void output_sh(unsigned char *data)
{
unsigned int reloc_ofs;
unsigned int debug_ofs;
unsigned int str_ofs;
Elf32_Shdr *shdr;
int i;
shdr = (Elf32_Shdr*) data;
/* For the NULL section */
shdr++;
memset(data, 0, g_out_sects * sizeof(Elf32_Shdr));
reloc_ofs = g_relocbase;
debug_ofs = g_debugbase;
str_ofs = 1;
unsigned int index_of_symtab;
for(i=0; i < g_elfhead.iShnum; i++)
if(g_elfsections[i].iType == SHT_SYMTAB)
index_of_symtab = i-2;
for(i = 1; i < g_elfhead.iShnum; i++)
{
if(g_elfsections[i].blOutput)
{
SW(&shdr->sh_name, str_ofs);
str_ofs += strlen(g_elfsections[i].szName) + 1;
SW(&shdr->sh_flags, g_elfsections[i].iFlags);
SW(&shdr->sh_addr, g_elfsections[i].iAddr);
SW(&shdr->sh_size, g_elfsections[i].iSize);
SW(&shdr->sh_link, g_elfsections[i].iLink);
//SW(&shdr->sh_link, 0);
SW(&shdr->sh_addralign, g_elfsections[i].iAddralign);
SW(&shdr->sh_entsize, g_elfsections[i].iEntsize);
if((g_elfsections[i].iType == SHT_REL) || (g_elfsections[i].iType == SHT_PRXRELOC))
{
if (strstr(g_elfsections[i].szName, "debug") || strstr(g_elfsections[i].szName, ".rel.line"))
SW(&shdr->sh_type, SHT_REL);
else
SW(&shdr->sh_type, SHT_PRXRELOC);
// turn this off once we converted debug_info from dwarf 2 to dwarf 1
//if (strcmp(g_elfsections[i].szName, ".rel.debug_info") == 0)
// SW(&shdr->sh_type, SHT_PRXRELOC);
if (g_elfsections[i].pRef)
SW(&shdr->sh_info, g_elfsections[i].pRef->iIndex);
else
SW(&shdr->sh_info, 0);
SW(&shdr->sh_offset, reloc_ofs);
SW(&shdr->sh_link, index_of_symtab); // TODO: this is going to require some further inspection could be that its not correct
// after the prx is created since sections move in the index
reloc_ofs += g_elfsections[i].iSize;
}
else if(g_elfsections[i].iType == SHT_PROGBITS)
{
// sbss bss and comment don't have an address and are instead using offset as their address
// problem is that progbits only sets the offset to the section address
// which makes it write the content into the wrong offset
// we could make a simple hack where we check for sections that arent .text then place them at the offset
// do the same thing that we are doing with the dwarf sections
SW(&shdr->sh_type, g_elfsections[i].iType);
SW(&shdr->sh_info, 0);
SW(&shdr->sh_offset, g_allocbase + g_elfsections[i].iAddr);
if (g_elfsections[i].fakeRel)
{
SW(&shdr->sh_offset, debug_ofs);
debug_ofs += g_elfsections[i].iSize;
}
if (g_elfsections[i].iAddr == 0 && (strcmp(g_elfsections[i].szName, ".text") != 0))
{
if(g_verbose)
printf("fix this: %s, isnt being allocated properly in the prx, wrong offset!!!\n", g_elfsections[i].szName);
}
}
else if((g_elfsections[i].iType == SHT_MIPS_DWARF) || (strcmp(g_elfsections[i].szName, ".strtab") == 0))
{
SW(&shdr->sh_type, g_elfsections[i].iType);
SW(&shdr->sh_info, 0);
SW(&shdr->sh_offset, debug_ofs);
debug_ofs += g_elfsections[i].iSize;
}
// symtab seems to not be correct, both ghidra and the devkit cant seem to understand them
else if(g_elfsections[i].iType == SHT_SYMTAB)
{
SW(&shdr->sh_type, g_elfsections[i].iType);
SW(&shdr->sh_info, 0);
SW(&shdr->sh_link, i-1); // Hack!! we should do another for loop to see where strtab is at then set sh_link to it
//SW(&shdr->sh_info, g_elfsections[i].iInfo); // why is this set to 0xEE ?
// apparently the devkit prx sets iInfo to 0, no clue
SW(&shdr->sh_offset, debug_ofs);
debug_ofs += g_elfsections[i].iSize;
}
else
{
if(g_verbose)
printf("fix this: %s, isnt being allocated properly in the prx!!!\n", g_elfsections[i].szName);
SW(&shdr->sh_type, g_elfsections[i].iType);
SW(&shdr->sh_info, 0);
/* Point it to the end of the allocated section */
SW(&shdr->sh_offset, g_allocbase + g_alloc_size);
}
shdr++;
}
}
/* Fill in the shstrtab section */
SW(&shdr->sh_name, str_ofs);
SW(&shdr->sh_flags, 0);
SW(&shdr->sh_addr, 0);
SW(&shdr->sh_size, g_str_size);
SW(&shdr->sh_link, 0);
SW(&shdr->sh_addralign, 1);
SW(&shdr->sh_entsize, 0);
SW(&shdr->sh_type, SHT_STRTAB);
SW(&shdr->sh_info, 0);
SW(&shdr->sh_offset, g_shstrbase);
}
/* Output relocations */
void output_relocs(unsigned char *data)
{
int i;
unsigned char *pReloc;
pReloc = data;
for(i = 0; i < g_elfhead.iShnum; i++)
{
if (g_elfsections[i].blOutput)
{
// dont modify the dwarf relocation sections or else the devkit debugger crashes
if(strstr(g_elfsections[i].szName, ".rel.debug"))
{
memcpy(pReloc, g_elfsections[i].pData, g_elfsections[i].iSize);
pReloc += g_elfsections[i].iSize;
continue;
}
if((g_elfsections[i].iType == SHT_REL) || (g_elfsections[i].iType == SHT_PRXRELOC))
{
Elf32_Rel *rel;
int j, count;
memcpy(pReloc, g_elfsections[i].pData, g_elfsections[i].iSize);
rel = (Elf32_Rel*) pReloc;
count = g_elfsections[i].iSize / sizeof(Elf32_Rel);
for(j = 0; j < count; j++)
{
unsigned int sym;
/* Clear the top 24bits of the info */
/* Kind of a dirty trick but hey :P */
sym = LW(rel->r_info);
sym &= 0xFF;
SW(&rel->r_info, sym);
rel++;
}
pReloc += g_elfsections[i].iSize;
}
}
}
}
void output_debug(unsigned char *data)
{
int i;
unsigned char *pDebug;
pDebug = data;
for(i = 0; i < g_elfhead.iShnum; i++)
{
if (g_elfsections[i].blOutput)
{
// we don't need to do the relocation stuff on the dwarf sections
if ((g_elfsections[i].fakeRel) || (g_elfsections[i].iType == SHT_MIPS_DWARF) || (g_elfsections[i].iType == SHT_SYMTAB) || (strcmp(g_elfsections[i].szName, ".strtab") == 0))
{
memset(pDebug, 0, g_elfsections[i].iSize);
memcpy(pDebug, g_elfsections[i].pData, g_elfsections[i].iSize);
if (g_elfsections[i].iType == SHT_MIPS_DWARF)
{
if (g_verbose)
printf("%s got memset to 0\n", g_elfsections[i].szName);
memcpy(pDebug, g_elfsections[i].pData, g_elfsections[i].iSize);
}
// debug_info is in a different format properly, since the devkit properly wants it in darwf 1 and not darwf 2
// remove this later once we got it properly converted
//if (strcmp(g_elfsections[i].szName, ".debug_info") == 0)
// memset(pDebug, 0, g_elfsections[i].iSize);
if (g_elfsections[i].iType == SHT_SYMTAB)
{
memset(pDebug, 0, g_elfsections[i].iSize);
memcpy(pDebug, g_elfsections[i].pData, g_elfsections[i].iSize);
}
if (g_elfsections[i].fakeRel)
{
memset(pDebug, 0, g_elfsections[i].iSize);
memcpy(pDebug, g_elfsections[i].pData, g_elfsections[i].iSize);
}
pDebug += g_elfsections[i].iSize;
}
}
}
}
/* Output the section header string table */
void output_shstrtab(unsigned char *data)
{
int i;
char *pData;
/* For the NULL section, memory should be zeroed anyway */
memset(data, 0, g_str_size);
pData = (char *) (data + 1);
for(i = 1; i < g_elfhead.iShnum; i++)
{
if(g_elfsections[i].blOutput)
{
if(g_verbose)
{
fprintf(stderr, "String %d: %s\n", i, g_elfsections[i].szName);
}
strcpy(pData, g_elfsections[i].szName);
pData += strlen(g_elfsections[i].szName) + 1;
}
}
strcpy(pData, ELF_SH_STRTAB);
}
/* Output a stripped prx file */
int output_prx(const char *prxfile)
{
int size;
unsigned char *data;
FILE *fp;