]>
git.proxmox.com Git - mirror_edk2.git/blob - Tools/Source/TianoTools/ZeroDebugData/ZeroDebugData.c
3 Copyright (c) 2001 - 2002 Intel Corporation. All rights reserved
4 This software and associated documentation (if any) is furnished
5 under a license and may only be used or copied in accordance
6 with the terms of the license. Except as permitted by such
7 license, no part of this software or documentation may be
8 reproduced, stored in a retrieval system, or transmitted in any
9 form or by any means without the express written consent of
17 Zero the Debug Data Fields of Portable Executable (PE) format file.
32 print usage of ZeroDebugData command
40 // GC_TODO: void - add argument and description to function comment
43 // print usage of command
45 printf ("\nUsage: ZeroDebugData <PE-File> [DebugData-File]\n");
57 read data from a specified location of file
61 offset - number of bytes from beginning of file
62 buffer - buffer used to store data
71 // set file pointer to the specified location of file
73 if (fseek (fp
, offset
, SEEK_SET
) != 0) {
74 printf ("Error: Cannot move the current location of the file.\n");
78 // read data from the file
80 if (fread (buffer
, size
, 1, fp
) != 1) {
81 printf ("Error: Cannot read data from the file.\n");
97 Zero the debug data fields of the file
102 fpData - pointer to output file that ZeroDebugData progress is written to
111 unsigned char header
[4];
112 unsigned long offset
;
113 unsigned long NumberOfRvaAndSizes
;
115 unsigned long lvalue
;
117 unsigned long Pointer
;
118 unsigned char *Buffer
;
122 // read the header of file
124 if (ReadFromFile (fp
, 0, header
, 2) != 0) {
125 printf ("Error: open image file\n");
129 // "MZ" -- the header of image file (PE)
131 if (strncmp ((char *) header
, "MZ", 2) != 0) {
132 printf ("Error: Invalid Image file.\n");
136 // At location 0x3C, the stub has the file offset to the
139 if (ReadFromFile (fp
, 0x3C, &offset
, 4) != 0) {
143 // read the header of optional
145 if (ReadFromFile (fp
, offset
, header
, 4) != 0) {
149 // "PE\0\0" -- the signature of optional header
151 if (strncmp ((char *) header
, "PE\0\0", 4) != 0) {
152 printf ("Error: Invalid PE format file.\n");
156 // Add 16 to skip COFF file header, and get to optional header.
161 // Check the magic field, 0x10B for PE32 and 0x20B for PE32+
163 if (ReadFromFile (fp
, offset
, &nvalue
, 2) != 0) {
167 // If this is PE32 image file, offset of NumberOfRvaAndSizes is 92.
170 switch (nvalue
& 0xFFFF) {
173 printf ("Info: Image is PE32. ");
178 printf ("Info: Image is PE32+. ");
182 printf ("Error: Magic value is unknown.\n");
186 // get the value of NumberOfRvaAndSizes
188 if (ReadFromFile (fp
, offset
, &NumberOfRvaAndSizes
, 4) != 0) {
189 printf ("Error: read NumberOfRvaAndSizes error.\n");
193 // printf ("Info: NumberOfRvaAndSizes = %d\n", NumberOfRvaAndSizes);
196 // Finding Debug Table, offset of Debug Table
197 // is 4 + 6 * 8 = 52.
199 if (NumberOfRvaAndSizes
>= 7) {
200 if (ReadFromFile (fp
, offset
+ 52, &lvalue
, 4) != 0) {
204 // Read the SizeOfData(16) and PointerToRawData(24)
206 if (ReadFromFile (fp
, lvalue
+ 16, &Size
, 4) != 0) {
207 printf ("error: Size = %d\n", Size
);
211 printf ("Debug data: size = %xh, ", Size
);
212 fprintf (fpData
, "Debug data: size = %xh, ", Size
);
214 if (ReadFromFile (fp
, lvalue
+ 20, &Pointer
, 4) != 0) {
215 printf ("error: LoadOffset = %xh\n", Pointer
);
219 // printf ("LoadOffset = %xh, ", Pointer);
221 fprintf (fpData
, "LoadOffset = %xh, ", Pointer
);
223 if (ReadFromFile (fp
, lvalue
+ 24, &Pointer
, 4) != 0) {
224 printf ("error: FileOffset = %xh\n", Pointer
);
228 printf ("FileOffset = %xh, ", Pointer
);
229 fprintf (fpData
, "FileOffset = %xh, \n", Pointer
);
231 if ((lvalue
!= 0) && (Pointer
!= 0)) {
235 Buffer
= malloc (Size
+ 1);
236 if (Buffer
== NULL
) {
237 printf ("Error: Cannot allocate memory.\n");
241 // set file pointer to the specified location of file
243 if (fseek (fp
, Pointer
, SEEK_SET
) != 0) {
244 printf ("Error: Cannot move the current location of the file.\n");
249 // read data from PE file
251 if (fread (Buffer
, Size
, 1, fp
) != 1) {
252 printf ("Error: Cannot read data from the file.\n");
257 // write to data file
259 for (Index
= 0; Index
< Size
;) {
260 fprintf (fpData
, "%02x ", Buffer
[Index
]);
263 if (Index
% 8 == 0) {
264 fprintf (fpData
, "\n");
268 fprintf (fpData
, "\n");
271 // zero buffer and write back to PE file
273 if (fseek (fp
, Pointer
, SEEK_SET
) != 0) {
274 printf ("Error: Cannot move the current location of the file.\n");
279 memset (Buffer
, 0, Size
);
280 if (fwrite (Buffer
, Size
, 1, fp
) != 1) {
281 perror ("Error: Cannot write zero to the file.\n");
286 // set file pointer to the specified location of file
288 if (fseek (fp
, lvalue
+ 4, SEEK_SET
) != 0) {
289 printf ("Error: Cannot move the current location of the file.\n");
294 if (fwrite (Buffer
, 4, 1, fp
) != 1) {
295 perror ("Error: Cannot write zero to the file.\n");
316 Prints the zero debug data of the PE file to the DebugData file.
317 Executes the ZeroDebugData function.
321 argc - Standard C argument, number of command line arguments.
322 argv[] - Standard C argument, array of pointers to the input files,
323 such as the PE and DebugData files.
334 char DataFile
[1024] = "";
337 // check the number of parameters
340 printf ("\nUsage: ZeroDebugData <PE-File> [DebugData-File]\n");
344 // open the DebugData file, if not exists, return
347 strcpy (DataFile
, argv
[2]);
349 strcpy (DataFile
, "DebugData.dat");
352 fpData
= fopen (DataFile
, "a+");
353 if (fpData
== NULL
) {
354 fpData
= fopen (DataFile
, "w");
355 if (fpData
== NULL
) {
356 printf ("Error: Cannot open the data file!\n");
363 fp
= fopen (argv
[1], "r+b");
365 printf ("Error: Cannot open the PE file!\n");
369 // Zero the Debug Data to the PE file
371 printf ("Zero Debug Data to file %s:\n", argv
[1]);
372 fprintf (fpData
, "\nZero Debug Data to file %s:\n", argv
[1]);
373 if ((int *) ZeroDebugData (fp
, fpData
) != 0) {
374 printf ("Error: Zero Debug Data PE file\n");
379 printf (" success\n");