]>
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.
27 #define UTILITY_NAME "GenTEImage"
28 #define UTILITY_MAJOR_VERSION 0
29 #define UTILITY_MINOR_VERSION 1
37 print version information for this utility
45 // GC_TODO: void - add argument and description to function comment
48 // print usage of command
50 printf ( "%s v%d.%d -Utility to zero the Debug Data Fields of Portable Executable (PE) format file. \n " , UTILITY_NAME
, UTILITY_MAJOR_VERSION
, UTILITY_MINOR_VERSION
);
51 printf ( "Copyright (c) 1999-2007 Intel Corporation. All rights reserved. \n " );
60 print usage of ZeroDebugData command
68 // GC_TODO: void - add argument and description to function comment
72 // print usage of command
74 printf ( " \n Usage: ZeroDebugData <PE-File> [DebugData-File] \n " );
86 read data from a specified location of file
90 offset - number of bytes from beginning of file
91 buffer - buffer used to store data
100 // set file pointer to the specified location of file
102 if ( fseek ( fp
, offset
, SEEK_SET
) != 0 ) {
103 printf ( "Error: Cannot move the current location of the file. \n " );
107 // read data from the file
109 if ( fread ( buffer
, size
, 1 , fp
) != 1 ) {
110 printf ( "Error: Cannot read data from the file. \n " );
126 Zero the debug data fields of the file
131 fpData - pointer to output file that ZeroDebugData progress is written to
140 unsigned char header
[ 4 ];
141 unsigned long offset
;
142 unsigned long NumberOfRvaAndSizes
;
144 unsigned long lvalue
;
146 unsigned long Pointer
;
147 unsigned char * Buffer
;
151 // read the header of file
153 if ( ReadFromFile ( fp
, 0 , header
, 2 ) != 0 ) {
154 printf ( "Error: open image file \n " );
158 // "MZ" -- the header of image file (PE)
160 if ( strncmp (( char *) header
, "MZ" , 2 ) != 0 ) {
161 printf ( "Error: Invalid Image file. \n " );
165 // At location 0x3C, the stub has the file offset to the
168 if ( ReadFromFile ( fp
, 0x3C , & offset
, 4 ) != 0 ) {
172 // read the header of optional
174 if ( ReadFromFile ( fp
, offset
, header
, 4 ) != 0 ) {
178 // "PE\0\0" -- the signature of optional header
180 if ( strncmp (( char *) header
, "PE\0\0" , 4 ) != 0 ) {
181 printf ( "Error: Invalid PE format file. \n " );
185 // Add 16 to skip COFF file header, and get to optional header.
190 // Check the magic field, 0x10B for PE32 and 0x20B for PE32+
192 if ( ReadFromFile ( fp
, offset
, & nvalue
, 2 ) != 0 ) {
196 // If this is PE32 image file, offset of NumberOfRvaAndSizes is 92.
199 switch ( nvalue
& 0xFFFF ) {
202 printf ( "Info: Image is PE32. " );
207 printf ( "Info: Image is PE32+. " );
211 printf ( "Error: Magic value is unknown. \n " );
215 // get the value of NumberOfRvaAndSizes
217 if ( ReadFromFile ( fp
, offset
, & NumberOfRvaAndSizes
, 4 ) != 0 ) {
218 printf ( "Error: read NumberOfRvaAndSizes error. \n " );
222 // printf ("Info: NumberOfRvaAndSizes = %d\n", NumberOfRvaAndSizes);
225 // Finding Debug Table, offset of Debug Table
226 // is 4 + 6 * 8 = 52.
228 if ( NumberOfRvaAndSizes
>= 7 ) {
229 if ( ReadFromFile ( fp
, offset
+ 52 , & lvalue
, 4 ) != 0 ) {
233 // Read the SizeOfData(16) and PointerToRawData(24)
235 if ( ReadFromFile ( fp
, lvalue
+ 16 , & Size
, 4 ) != 0 ) {
236 printf ( "error: Size = %d \n " , Size
);
240 printf ( "Debug data: size = %xh, " , Size
);
241 fprintf ( fpData
, "Debug data: size = %xh, " , Size
);
243 if ( ReadFromFile ( fp
, lvalue
+ 20 , & Pointer
, 4 ) != 0 ) {
244 printf ( "error: LoadOffset = %xh \n " , Pointer
);
248 // printf ("LoadOffset = %xh, ", Pointer);
250 fprintf ( fpData
, "LoadOffset = %xh, " , Pointer
);
252 if ( ReadFromFile ( fp
, lvalue
+ 24 , & Pointer
, 4 ) != 0 ) {
253 printf ( "error: FileOffset = %xh \n " , Pointer
);
257 printf ( "FileOffset = %xh, " , Pointer
);
258 fprintf ( fpData
, "FileOffset = %xh, \n " , Pointer
);
260 if (( lvalue
!= 0 ) && ( Pointer
!= 0 )) {
264 Buffer
= malloc ( Size
+ 1 );
265 if ( Buffer
== NULL
) {
266 printf ( "Error: Cannot allocate memory. \n " );
270 // set file pointer to the specified location of file
272 if ( fseek ( fp
, Pointer
, SEEK_SET
) != 0 ) {
273 printf ( "Error: Cannot move the current location of the file. \n " );
278 // read data from PE file
280 if ( fread ( Buffer
, Size
, 1 , fp
) != 1 ) {
281 printf ( "Error: Cannot read data from the file. \n " );
286 // write to data file
288 for ( Index
= 0 ; Index
< Size
;) {
289 fprintf ( fpData
, "%02x " , Buffer
[ Index
]);
292 if ( Index
% 8 == 0 ) {
293 fprintf ( fpData
, " \n " );
297 fprintf ( fpData
, " \n " );
300 // zero buffer and write back to PE file
302 if ( fseek ( fp
, Pointer
, SEEK_SET
) != 0 ) {
303 printf ( "Error: Cannot move the current location of the file. \n " );
308 memset ( Buffer
, 0 , Size
);
309 if ( fwrite ( Buffer
, Size
, 1 , fp
) != 1 ) {
310 perror ( "Error: Cannot write zero to the file. \n " );
315 // set file pointer to the specified location of file
317 if ( fseek ( fp
, lvalue
+ 4 , SEEK_SET
) != 0 ) {
318 printf ( "Error: Cannot move the current location of the file. \n " );
323 if ( fwrite ( Buffer
, 4 , 1 , fp
) != 1 ) {
324 perror ( "Error: Cannot write zero to the file. \n " );
345 Prints the zero debug data of the PE file to the DebugData file.
346 Executes the ZeroDebugData function.
350 argc - Standard C argument, number of command line arguments.
351 argv[] - Standard C argument, array of pointers to the input files,
352 such as the PE and DebugData files.
363 char DataFile
[ 1024 ] = "" ;
370 if (( strcmp ( argv
[ 1 ], "-h" ) == 0 ) || ( strcmp ( argv
[ 1 ], "--help" ) == 0 ) ||
371 ( strcmp ( argv
[ 1 ], "-?" ) == 0 ) || ( strcmp ( argv
[ 1 ], "/?" ) == 0 )) {
376 if (( strcmp ( argv
[ 1 ], "-V" ) == 0 ) || ( strcmp ( argv
[ 1 ], "--version" ) == 0 )) {
387 // open the DebugData file, if not exists, return
390 strcpy ( DataFile
, argv
[ 2 ]);
392 strcpy ( DataFile
, "DebugData.dat" );
395 fpData
= fopen ( DataFile
, "a+" );
396 if ( fpData
== NULL
) {
397 fpData
= fopen ( DataFile
, "w" );
398 if ( fpData
== NULL
) {
399 printf ( "Error: Cannot open the data file! \n " );
406 fp
= fopen ( argv
[ 1 ], "r+b" );
408 printf ( "Error: Cannot open the PE file! \n " );
412 // Zero the Debug Data to the PE file
414 printf ( "Zero Debug Data to file %s: \n " , argv
[ 1 ]);
415 fprintf ( fpData
, " \n Zero Debug Data to file %s: \n " , argv
[ 1 ]);
416 if (( int *) ZeroDebugData ( fp
, fpData
) != 0 ) {
417 printf ( "Error: Zero Debug Data PE file \n " );
422 printf ( " success \n " );