forked from F8LEFT/SoFixer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
118 lines (104 loc) · 3.42 KB
/
main.cpp
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
#include <iostream>
#include "ElfReader.h"
#include "ElfRebuilder.h"
#include "FDebug.h"
#include <getopt.h>
const char* short_options = "hdm:s:o:";
const struct option long_options[] = {
{"help", 0, NULL, 'h'},
{"debug", 0, NULL, 'd'},
{"memso", 1, NULL, 'm'},
{"source", 1, NULL, 's'},
{"output", 1, NULL, 'o'},
{nullptr, 0, nullptr, 0}
};
void useage();
int main(int argc, char* argv[]) {
int c;
ElfReader elf_reader;
std::string source, output;
bool isValidArg = true;
while((c = getopt_long(argc, argv, short_options, long_options, nullptr)) != -1) {
switch (c) {
case 'd':
FDebug = true;
printf("Use debug mode\n");
break;
case 's':
source = optarg;
break;
case 'o':
output = optarg;
break;
case 'm': {
auto is16Bit = [](const char* c) {
auto len = strlen(c);
if(len > 2) {
if(c[0] == '0' & c[1] == 'x') return true;
}
bool is10bit = true;
for(auto i = 0; i < len; i++) {
if((c[i] > 'a' && c[i] < 'f') ||
c[i] > 'A' && c[i] < 'F') {
is10bit = false;
}
}
return !is10bit;
};
#ifndef __LP64__
auto base = strtoul(optarg, 0, is16Bit(optarg) ? 16: 10);
#else
auto base = strtoull(optarg, 0, is16Bit(optarg) ? 16: 10);
#endif
elf_reader.setDumpSoFile(true);
elf_reader.setDumpSoBaseAddr(base);
}
break;
default:
isValidArg = false;
break;
}
}
if(!isValidArg) {
useage();
return -1;
}
auto file = fopen(source.c_str(), "rb");
if(nullptr == file) {
printf("source so file cannot found!!!\n");
return -1;
}
auto fd = fileno(file);
printf("start to rebuild elf file\n");
elf_reader.setSource(source.c_str(), fd);
if(!elf_reader.Load()) {
printf("source so file is invalid\n");
return -1;
}
ElfRebuilder elf_rebuilder(&elf_reader);
if(!elf_rebuilder.Rebuild()) {
printf("error occured in rebuilding elf file\n");
return -1;
}
fclose(file);
file = fopen(output.c_str(), "wb+");
if(nullptr == file) {
printf("output so file cannot write !!!\n");
return -1;
}
fwrite(elf_rebuilder.getRebuildData(), elf_rebuilder.getRebuildSize(), 1, file);
fclose(file);
printf("Done!!!\n");
return 0;
}
void useage() {
printf("SoFixer v0.2 author F8LEFT(currwin)\n");
printf("Useage: SoFixer <option(s)> -s sourcefile -o generatefile\n");
printf(" try rebuild shdr with phdr\n");
printf(" Options are:\n");
printf(" -d --debug Show debug info\n");
printf(" -m --memso memBaseAddr(16bit format) Source file is dump from memory from address x\n");
printf(" -s --source sourceFilePath Source file path\n");
printf(" -o --output generateFilePath Generate file path\n");
printf(" -h --help Display this information\n");
}