]>
git.proxmox.com Git - mirror_edk2.git/blob - Tools/CCode/Source/ZeroDebugData/ZeroDebugData.c
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
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.
19 Zero the Debug Data Fields of Portable Executable (PE) format file.
33 print usage of ZeroDebugData command
41 // GC_TODO: void - add argument and description to function comment
44 // print usage of command
46 printf ("\nUsage: ZeroDebugData <PE-File> [DebugData-File]\n");
58 read data from a specified location of file
62 offset - number of bytes from beginning of file
63 buffer - buffer used to store data
72 // set file pointer to the specified location of file
74 if (fseek (fp
, offset
, SEEK_SET
) != 0) {
75 printf ("Error: Cannot move the current location of the file.\n");
79 // read data from the file
81 if (fread (buffer
, size
, 1, fp
) != 1) {
82 printf ("Error: Cannot read data from the file.\n");
98 Zero the debug data fields of the file
103 fpData - pointer to output file that ZeroDebugData progress is written to
112 unsigned char header
[4];
113 unsigned long offset
;
114 unsigned long NumberOfRvaAndSizes
;
116 unsigned long lvalue
;
118 unsigned long Pointer
;
119 unsigned char *Buffer
;
123 // read the header of file
125 if (ReadFromFile (fp
, 0, header
, 2) != 0) {
126 printf ("Error: open image file\n");
130 // "MZ" -- the header of image file (PE)
132 if (strncmp ((char *) header
, "MZ", 2) != 0) {
133 printf ("Error: Invalid Image file.\n");
137 // At location 0x3C, the stub has the file offset to the
140 if (ReadFromFile (fp
, 0x3C, &offset
, 4) != 0) {
144 // read the header of optional
146 if (ReadFromFile (fp
, offset
, header
, 4) != 0) {
150 // "PE\0\0" -- the signature of optional header
152 if (strncmp ((char *) header
, "PE\0\0", 4) != 0) {
153 printf ("Error: Invalid PE format file.\n");
157 // Add 16 to skip COFF file header, and get to optional header.
162 // Check the magic field, 0x10B for PE32 and 0x20B for PE32+
164 if (ReadFromFile (fp
, offset
, &nvalue
, 2) != 0) {
168 // If this is PE32 image file, offset of NumberOfRvaAndSizes is 92.
171 switch (nvalue
& 0xFFFF) {
174 printf ("Info: Image is PE32. ");
179 printf ("Info: Image is PE32+. ");
183 printf ("Error: Magic value is unknown.\n");
187 // get the value of NumberOfRvaAndSizes
189 if (ReadFromFile (fp
, offset
, &NumberOfRvaAndSizes
, 4) != 0) {
190 printf ("Error: read NumberOfRvaAndSizes error.\n");
194 // printf ("Info: NumberOfRvaAndSizes = %d\n", NumberOfRvaAndSizes);
197 // Finding Debug Table, offset of Debug Table
198 // is 4 + 6 * 8 = 52.
200 if (NumberOfRvaAndSizes
>= 7) {
201 if (ReadFromFile (fp
, offset
+ 52, &lvalue
, 4) != 0) {
205 // Read the SizeOfData(16) and PointerToRawData(24)
207 if (ReadFromFile (fp
, lvalue
+ 16, &Size
, 4) != 0) {
208 printf ("error: Size = %d\n", Size
);
212 printf ("Debug data: size = %xh, ", Size
);
213 fprintf (fpData
, "Debug data: size = %xh, ", Size
);
215 if (ReadFromFile (fp
, lvalue
+ 20, &Pointer
, 4) != 0) {
216 printf ("error: LoadOffset = %xh\n", Pointer
);
220 // printf ("LoadOffset = %xh, ", Pointer);
222 fprintf (fpData
, "LoadOffset = %xh, ", Pointer
);
224 if (ReadFromFile (fp
, lvalue
+ 24, &Pointer
, 4) != 0) {
225 printf ("error: FileOffset = %xh\n", Pointer
);
229 printf ("FileOffset = %xh, ", Pointer
);
230 fprintf (fpData
, "FileOffset = %xh, \n", Pointer
);
232 if ((lvalue
!= 0) && (Pointer
!= 0)) {
236 Buffer
= malloc (Size
+ 1);
237 if (Buffer
== NULL
) {
238 printf ("Error: Cannot allocate memory.\n");
242 // set file pointer to the specified location of file
244 if (fseek (fp
, Pointer
, SEEK_SET
) != 0) {
245 printf ("Error: Cannot move the current location of the file.\n");
250 // read data from PE file
252 if (fread (Buffer
, Size
, 1, fp
) != 1) {
253 printf ("Error: Cannot read data from the file.\n");
258 // write to data file
260 for (Index
= 0; Index
< Size
;) {
261 fprintf (fpData
, "%02x ", Buffer
[Index
]);
264 if (Index
% 8 == 0) {
265 fprintf (fpData
, "\n");
269 fprintf (fpData
, "\n");
272 // zero buffer and write back to PE file
274 if (fseek (fp
, Pointer
, SEEK_SET
) != 0) {
275 printf ("Error: Cannot move the current location of the file.\n");
280 memset (Buffer
, 0, Size
);
281 if (fwrite (Buffer
, Size
, 1, fp
) != 1) {
282 perror ("Error: Cannot write zero to the file.\n");
287 // set file pointer to the specified location of file
289 if (fseek (fp
, lvalue
+ 4, SEEK_SET
) != 0) {
290 printf ("Error: Cannot move the current location of the file.\n");
295 if (fwrite (Buffer
, 4, 1, fp
) != 1) {
296 perror ("Error: Cannot write zero to the file.\n");
317 Prints the zero debug data of the PE file to the DebugData file.
318 Executes the ZeroDebugData function.
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.
335 char DataFile
[1024] = "";
338 // check the number of parameters
341 printf ("\nUsage: ZeroDebugData <PE-File> [DebugData-File]\n");
345 // open the DebugData file, if not exists, return
348 strcpy (DataFile
, argv
[2]);
350 strcpy (DataFile
, "DebugData.dat");
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");
364 fp
= fopen (argv
[1], "r+b");
366 printf ("Error: Cannot open the PE file!\n");
370 // Zero the Debug Data to the PE file
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");
380 printf (" success\n");