]> git.proxmox.com Git - mirror_edk2.git/blob - Tools/CCode/Source/ZeroDebugData/ZeroDebugData.c
More moves for Tool Packages
[mirror_edk2.git] / Tools / CCode / Source / ZeroDebugData / ZeroDebugData.c
1 /*++
2
3 Copyright (c) 2004-2006 Intel Corporation. All rights reserved
4 This program and the accompanying materials are licensed and made available
5 under the terms and conditions of the BSD License which accompanies this
6 distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
8
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
11
12
13 Module Name:
14
15 ZeroDebugData.c
16
17 Abstract:
18
19 Zero the Debug Data Fields of Portable Executable (PE) format file.
20
21 --*/
22
23 #include <stdio.h>
24 #include <string.h>
25 #include <stdlib.h>
26
27 void
28 PrintUsage (
29 void
30 )
31 /*++
32 Routine Description:
33 print usage of ZeroDebugData command
34
35 Arguments:
36 None
37
38 Returns:
39 None
40 --*/
41 // GC_TODO: void - add argument and description to function comment
42 {
43 //
44 // print usage of command
45 //
46 printf ("\nUsage: ZeroDebugData <PE-File> [DebugData-File]\n");
47 }
48
49 int
50 ReadFromFile (
51 FILE *fp,
52 long offset,
53 void *buffer,
54 int size
55 )
56 /*++
57 Routine Description:
58 read data from a specified location of file
59
60 Arguments:
61 fp - file pointer
62 offset - number of bytes from beginning of file
63 buffer - buffer used to store data
64 size - size of buffer
65
66 Returns:
67 = 0 - Success
68 = -1 - Failed
69 --*/
70 {
71 //
72 // set file pointer to the specified location of file
73 //
74 if (fseek (fp, offset, SEEK_SET) != 0) {
75 printf ("Error: Cannot move the current location of the file.\n");
76 return -1;
77 }
78 //
79 // read data from the file
80 //
81 if (fread (buffer, size, 1, fp) != 1) {
82 printf ("Error: Cannot read data from the file.\n");
83 return -1;
84 }
85
86 return 0;
87 }
88
89 int
90 ZeroDebugData (
91 FILE *fp,
92 FILE *fpData
93 )
94 /*++
95
96 Routine Description:
97
98 Zero the debug data fields of the file
99
100 Arguments:
101
102 fp - file pointer
103 fpData - pointer to output file that ZeroDebugData progress is written to
104
105 Returns:
106
107 = 0 - Success
108 = -1 - Failed
109
110 --*/
111 {
112 unsigned char header[4];
113 unsigned long offset;
114 unsigned long NumberOfRvaAndSizes;
115 unsigned int nvalue;
116 unsigned long lvalue;
117 unsigned long Size;
118 unsigned long Pointer;
119 unsigned char *Buffer;
120 unsigned long Index;
121
122 //
123 // read the header of file
124 //
125 if (ReadFromFile (fp, 0, header, 2) != 0) {
126 printf ("Error: open image file\n");
127 return -1;
128 }
129 //
130 // "MZ" -- the header of image file (PE)
131 //
132 if (strncmp ((char *) header, "MZ", 2) != 0) {
133 printf ("Error: Invalid Image file.\n");
134 return -1;
135 }
136 //
137 // At location 0x3C, the stub has the file offset to the
138 // PE signature.
139 //
140 if (ReadFromFile (fp, 0x3C, &offset, 4) != 0) {
141 return -1;
142 }
143 //
144 // read the header of optional
145 //
146 if (ReadFromFile (fp, offset, header, 4) != 0) {
147 return -1;
148 }
149 //
150 // "PE\0\0" -- the signature of optional header
151 //
152 if (strncmp ((char *) header, "PE\0\0", 4) != 0) {
153 printf ("Error: Invalid PE format file.\n");
154 return -1;
155 }
156 //
157 // Add 16 to skip COFF file header, and get to optional header.
158 //
159 offset += 24;
160
161 //
162 // Check the magic field, 0x10B for PE32 and 0x20B for PE32+
163 //
164 if (ReadFromFile (fp, offset, &nvalue, 2) != 0) {
165 return -1;
166 }
167 //
168 // If this is PE32 image file, offset of NumberOfRvaAndSizes is 92.
169 // Else it is 108.
170 //
171 switch (nvalue & 0xFFFF) {
172 case 0x10B:
173 offset += 92;
174 printf ("Info: Image is PE32. ");
175 break;
176
177 case 0x20B:
178 offset += 108;
179 printf ("Info: Image is PE32+. ");
180 break;
181
182 default:
183 printf ("Error: Magic value is unknown.\n");
184 return -1;
185 }
186 //
187 // get the value of NumberOfRvaAndSizes
188 //
189 if (ReadFromFile (fp, offset, &NumberOfRvaAndSizes, 4) != 0) {
190 printf ("Error: read NumberOfRvaAndSizes error.\n");
191 return -1;
192 }
193 //
194 // printf ("Info: NumberOfRvaAndSizes = %d\n", NumberOfRvaAndSizes);
195 //
196 //
197 // Finding Debug Table, offset of Debug Table
198 // is 4 + 6 * 8 = 52.
199 //
200 if (NumberOfRvaAndSizes >= 7) {
201 if (ReadFromFile (fp, offset + 52, &lvalue, 4) != 0) {
202 return -1;
203 }
204 //
205 // Read the SizeOfData(16) and PointerToRawData(24)
206 //
207 if (ReadFromFile (fp, lvalue + 16, &Size, 4) != 0) {
208 printf ("error: Size = %d\n", Size);
209 return -1;
210 }
211
212 printf ("Debug data: size = %xh, ", Size);
213 fprintf (fpData, "Debug data: size = %xh, ", Size);
214
215 if (ReadFromFile (fp, lvalue + 20, &Pointer, 4) != 0) {
216 printf ("error: LoadOffset = %xh\n", Pointer);
217 return -1;
218 }
219 //
220 // printf ("LoadOffset = %xh, ", Pointer);
221 //
222 fprintf (fpData, "LoadOffset = %xh, ", Pointer);
223
224 if (ReadFromFile (fp, lvalue + 24, &Pointer, 4) != 0) {
225 printf ("error: FileOffset = %xh\n", Pointer);
226 return -1;
227 }
228
229 printf ("FileOffset = %xh, ", Pointer);
230 fprintf (fpData, "FileOffset = %xh, \n", Pointer);
231
232 if ((lvalue != 0) && (Pointer != 0)) {
233 //
234 // prepare buffer
235 //
236 Buffer = malloc (Size + 1);
237 if (Buffer == NULL) {
238 printf ("Error: Cannot allocate memory.\n");
239 return -1;
240 }
241 //
242 // set file pointer to the specified location of file
243 //
244 if (fseek (fp, Pointer, SEEK_SET) != 0) {
245 printf ("Error: Cannot move the current location of the file.\n");
246 free (Buffer);
247 return -1;
248 }
249 //
250 // read data from PE file
251 //
252 if (fread (Buffer, Size, 1, fp) != 1) {
253 printf ("Error: Cannot read data from the file.\n");
254 free (Buffer);
255 return -1;
256 }
257 //
258 // write to data file
259 //
260 for (Index = 0; Index < Size;) {
261 fprintf (fpData, "%02x ", Buffer[Index]);
262
263 Index++;
264 if (Index % 8 == 0) {
265 fprintf (fpData, "\n");
266 }
267 }
268
269 fprintf (fpData, "\n");
270
271 //
272 // zero buffer and write back to PE file
273 //
274 if (fseek (fp, Pointer, SEEK_SET) != 0) {
275 printf ("Error: Cannot move the current location of the file.\n");
276 free (Buffer);
277 return -1;
278 }
279
280 memset (Buffer, 0, Size);
281 if (fwrite (Buffer, Size, 1, fp) != 1) {
282 perror ("Error: Cannot write zero to the file.\n");
283 free (Buffer);
284 return -1;
285 }
286 //
287 // set file pointer to the specified location of file
288 //
289 if (fseek (fp, lvalue + 4, SEEK_SET) != 0) {
290 printf ("Error: Cannot move the current location of the file.\n");
291 free (Buffer);
292 return -1;
293 }
294
295 if (fwrite (Buffer, 4, 1, fp) != 1) {
296 perror ("Error: Cannot write zero to the file.\n");
297 free (Buffer);
298 return -1;
299 }
300
301 free (Buffer);
302 }
303 }
304
305 return 0;
306 }
307
308 int
309 main (
310 int argc,
311 char *argv[]
312 )
313 /*++
314
315 Routine Description:
316
317 Prints the zero debug data of the PE file to the DebugData file.
318 Executes the ZeroDebugData function.
319
320 Arguments:
321
322 argc - Standard C argument, number of command line arguments.
323 argv[] - Standard C argument, array of pointers to the input files,
324 such as the PE and DebugData files.
325
326 Returns:
327
328 zero - success
329 nonzero - failure
330
331 --*/
332 {
333 FILE *fp;
334 FILE *fpData;
335 char DataFile[1024] = "";
336
337 //
338 // check the number of parameters
339 //
340 if (argc < 2) {
341 printf ("\nUsage: ZeroDebugData <PE-File> [DebugData-File]\n");
342 return -1;
343 }
344 //
345 // open the DebugData file, if not exists, return
346 //
347 if (argc >= 3) {
348 strcpy (DataFile, argv[2]);
349 } else {
350 strcpy (DataFile, "DebugData.dat");
351 }
352
353 fpData = fopen (DataFile, "a+");
354 if (fpData == NULL) {
355 fpData = fopen (DataFile, "w");
356 if (fpData == NULL) {
357 printf ("Error: Cannot open the data file!\n");
358 return -1;
359 }
360 }
361 //
362 // open the PE file
363 //
364 fp = fopen (argv[1], "r+b");
365 if (fp == NULL) {
366 printf ("Error: Cannot open the PE file!\n");
367 return -1;
368 }
369 //
370 // Zero the Debug Data to the PE file
371 //
372 printf ("Zero Debug Data to file %s:\n", argv[1]);
373 fprintf (fpData, "\nZero Debug Data to file %s:\n", argv[1]);
374 if ((int *) ZeroDebugData (fp, fpData) != 0) {
375 printf ("Error: Zero Debug Data PE file\n");
376 fclose (fp);
377 return -1;
378 }
379
380 printf (" success\n");
381
382 //
383 // close the PE file
384 //
385 fflush (fpData);
386 fflush (fp);
387 fclose (fpData);
388 fclose (fp);
389
390 return 0;
391 }