-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathobjfix.c
115 lines (90 loc) · 3 KB
/
objfix.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
/*
Copyright (c) 1998-2005 Charles Bilyue.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later
version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <stdio.h>
#include <string.h>
int open_error(const char* filename, const char* mode)
{
printf("Failure opening %s for %s\n", filename, mode);
return 1;
}
int main(int argc, char** argv)
{
int c;
int section_count;
FILE *in, *out;
unsigned char section_header[40];
if (argc < 2 || argc > 3) {
printf("Fixes MS Win32 object files to be compatible with the incorrect\n");
printf(" implementation in MinGW32.\n");
printf("Usage: objfix infile [outfile]\n");
return 1;
}
in = fopen(argv[1], (argc == 2 ? "rb+" : "rb"));
if (!in)
return open_error(argv[1], (argc == 2 ? "read" : "update"));
if (argc == 3) {
out = fopen(argv[2], "wb");
if (!out)
return open_error(argv[1], (argc == 2 ? "read" : "update"));
} else {
out = NULL;
}
if (out) {
fputc(fgetc(in), out);
fputc(fgetc(in), out);
fputc(section_count = fgetc(in), out);
fputc(c = fgetc(in), out);
section_count += c << 8;
for (c = 4; c < 0x14; c++) {
fputc(fgetc(in), out);
}
for (c = 0; c < section_count; c++) {
fread(section_header, 1, 40, in);
if (!strncmp(section_header, ".bss", 8)) {
memcpy(section_header + 8, section_header + 16, 4);
memset(section_header + 16, 0, 4);
} else {
memset(section_header + 8, 0, 4);
}
fwrite(section_header, 1, 40, out);
}
while ((c = fgetc(in)) != EOF) {
fputc(c, out);
}
} else {
fgetc(in);
fgetc(in);
section_count = fgetc(in);
section_count += fgetc(in) << 8;
fseek(in, 0x14, SEEK_SET);
for (c = 0; c < section_count; c++) {
fread(section_header, 1, 40, in);
fseek(in, -40, SEEK_CUR);
if (!strncmp(section_header, ".bss", 8)) {
memcpy(section_header + 8, section_header + 16, 4);
memset(section_header + 16, 0, 4);
} else {
memset(section_header + 8, 0, 4);
}
fwrite(section_header, 1, 40, in);
fseek(in, 0, SEEK_CUR);
}
}
fclose(in);
if (out)
fclose(out);
return 0;
}