-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathfix-realocations.c
65 lines (51 loc) · 1.49 KB
/
fix-realocations.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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#ifndef __APPLE__
#include <malloc.h>
#else
#include <malloc/malloc.h>
#endif
#include "fix-realocations.h"
int maxelfbuffer = 16 * 1024 * 1024;
int fix_realocations() {
FILE* elf_file = fopen("data_unsigned.psp", "r+b");
if (elf_file) {
int elf_size = 0;
char* elf_buffer = malloc(maxelfbuffer);
elf_size = fread(elf_buffer, 1, maxelfbuffer, elf_file);
Elf32_Ehdr_fix* elf_hdr = (Elf32_Ehdr_fix*)&elf_buffer[0];
if (!((elf_hdr->e_ident[0] == 0x7F) &&
(strncmp((char*)&elf_hdr->e_ident[1], "ELF", 3) == 0) &&
(elf_hdr->e_type == 0xFFA0))) {
printf("Error: not a PRX\n");
fclose(elf_file);
free(elf_buffer);
return -1;
}
Elf32_Shdr_fix* sec_hdr = NULL;
Elf32_Rel* reloc = NULL;
int count = 0;
int i, j;
for (i = 0; i < elf_hdr->e_shnum; i++) {
sec_hdr = (Elf32_Shdr_fix*)&elf_buffer[elf_hdr->e_shoff + (i * sizeof(Elf32_Shdr_fix))];
if (sec_hdr->sh_type == 0x700000A0) {
int num = sec_hdr->sh_size / sizeof(Elf32_Rel);
for (j = 0; j < num; j++) {
reloc = (Elf32_Rel*)&elf_buffer[sec_hdr->sh_offset + (j * sizeof(Elf32_Rel))];
if ((reloc->r_info & 0xFF) == 7) {
reloc->r_info = (reloc->r_info & 0xFFFFFF00);
count++;
}
}
}
}
fseek(elf_file, 0, SEEK_SET);
fwrite(elf_buffer, 1, elf_size, elf_file);
free(elf_buffer);
} else {
printf("Error: cannot open file\n");
return -1;
}
return 0;
}