3 * Copyright (C) 2005 Filip Navara
5 * The purpose of this utility is fix PE binaries generated by binutils and
6 * to manipulate flags that can't be set by binutils.
8 * Currently two features are implemented:
10 * - Setting flags on PE sections for use by drivers. The sections
11 * .text, .data, .idata, .bss are marked as non-pageable and
12 * non-discarable, section PAGE is marked as pageable and section
13 * INIT is marked as discaradable.
15 * - Sorting of export name table in executables. DLLTOOL has bug
16 * in sorting algorithm when the --kill-at flag is used. The exports
17 * are sorted in the decorated form and so the fastcall symbols are
18 * incorrectly put at the beginning of export table. This option
19 * allow to correct sort the table, so binary search can be used
32 /* The following definitions are ripped from MinGW W32API headers. We don't
33 use these headers directly in order to allow compilation on Linux hosts. */
35 typedef unsigned char BYTE
;
36 typedef unsigned short WORD
;
37 typedef unsigned int DWORD
;
40 #define IMAGE_NUMBEROF_DIRECTORY_ENTRIES 16
41 #define IMAGE_SIZEOF_SHORT_NAME 8
42 #define IMAGE_DOS_SIGNATURE 0x5A4D
43 #define IMAGE_NT_SIGNATURE 0x00004550
44 #define IMAGE_SCN_MEM_DISCARDABLE 0x2000000
45 #define IMAGE_SCN_MEM_NOT_PAGED 0x8000000
46 #define FIELD_OFFSET(t,f) ((LONG)&(((t*)0)->f))
47 #define IMAGE_FIRST_SECTION(h) ((PIMAGE_SECTION_HEADER) ((DWORD)h+FIELD_OFFSET(IMAGE_NT_HEADERS,OptionalHeader)+((PIMAGE_NT_HEADERS)(h))->FileHeader.SizeOfOptionalHeader))
48 #define IMAGE_DIRECTORY_ENTRY_EXPORT 0
51 typedef struct _IMAGE_DOS_HEADER
{
71 } IMAGE_DOS_HEADER
,*PIMAGE_DOS_HEADER
;
74 typedef struct _IMAGE_EXPORT_DIRECTORY
{
75 DWORD Characteristics
;
81 DWORD NumberOfFunctions
;
83 DWORD AddressOfFunctions
;
85 DWORD AddressOfNameOrdinals
;
86 } IMAGE_EXPORT_DIRECTORY
,*PIMAGE_EXPORT_DIRECTORY
;
87 typedef struct _IMAGE_FILE_HEADER
{
89 WORD NumberOfSections
;
91 DWORD PointerToSymbolTable
;
92 DWORD NumberOfSymbols
;
93 WORD SizeOfOptionalHeader
;
95 } IMAGE_FILE_HEADER
, *PIMAGE_FILE_HEADER
;
96 typedef struct _IMAGE_DATA_DIRECTORY
{
99 } IMAGE_DATA_DIRECTORY
,*PIMAGE_DATA_DIRECTORY
;
100 typedef struct _IMAGE_OPTIONAL_HEADER
{
102 BYTE MajorLinkerVersion
;
103 BYTE MinorLinkerVersion
;
105 DWORD SizeOfInitializedData
;
106 DWORD SizeOfUninitializedData
;
107 DWORD AddressOfEntryPoint
;
111 DWORD SectionAlignment
;
113 WORD MajorOperatingSystemVersion
;
114 WORD MinorOperatingSystemVersion
;
115 WORD MajorImageVersion
;
116 WORD MinorImageVersion
;
117 WORD MajorSubsystemVersion
;
118 WORD MinorSubsystemVersion
;
124 WORD DllCharacteristics
;
125 DWORD SizeOfStackReserve
;
126 DWORD SizeOfStackCommit
;
127 DWORD SizeOfHeapReserve
;
128 DWORD SizeOfHeapCommit
;
130 DWORD NumberOfRvaAndSizes
;
131 IMAGE_DATA_DIRECTORY DataDirectory
[IMAGE_NUMBEROF_DIRECTORY_ENTRIES
];
132 } IMAGE_OPTIONAL_HEADER
,*PIMAGE_OPTIONAL_HEADER
;
133 typedef struct _IMAGE_NT_HEADERS
{
135 IMAGE_FILE_HEADER FileHeader
;
136 IMAGE_OPTIONAL_HEADER OptionalHeader
;
137 } IMAGE_NT_HEADERS
,*PIMAGE_NT_HEADERS
;
138 typedef struct _IMAGE_SECTION_HEADER
{
139 BYTE Name
[IMAGE_SIZEOF_SHORT_NAME
];
141 DWORD PhysicalAddress
;
144 DWORD VirtualAddress
;
146 DWORD PointerToRawData
;
147 DWORD PointerToRelocations
;
148 DWORD PointerToLinenumbers
;
149 WORD NumberOfRelocations
;
150 WORD NumberOfLinenumbers
;
151 DWORD Characteristics
;
152 } IMAGE_SECTION_HEADER
,*PIMAGE_SECTION_HEADER
;
155 /* End of ripped definitions */
157 typedef struct _export_t
{
162 unsigned char *buffer
;
163 PIMAGE_DOS_HEADER dos_header
;
164 PIMAGE_NT_HEADERS nt_header
;
166 void *rva_to_ptr(DWORD rva
)
168 PIMAGE_SECTION_HEADER section_header
;
171 for (i
= 0, section_header
= IMAGE_FIRST_SECTION(nt_header
);
172 i
< nt_header
->OptionalHeader
.NumberOfRvaAndSizes
;
173 i
++, section_header
++)
175 if (rva
>= section_header
->VirtualAddress
&&
176 rva
< section_header
->VirtualAddress
+
177 section_header
->Misc
.VirtualSize
)
179 return buffer
+ rva
- section_header
->VirtualAddress
+
180 section_header
->PointerToRawData
;
187 int export_compare_func(const void *a
, const void *b
)
189 const export_t
*ap
= a
;
190 const export_t
*bp
= b
;
191 char *an
= rva_to_ptr(ap
->name
);
192 char *bn
= rva_to_ptr(bp
->name
);
193 return strcmp(an
, bn
);
196 int main(int argc
, char **argv
)
200 PIMAGE_SECTION_HEADER section_header
;
201 PIMAGE_DATA_DIRECTORY data_dir
;
203 unsigned long checksum
;
204 int fixup_exports
= 0;
205 int fixup_sections
= 0;
208 * Process parameters.
213 printf("Usage: %s <filename> <options>\n"
215 " -sections Sets section flags for PE image.\n"
216 " -exports Sort the names in export table.\n",
221 for (i
= 2; i
< argc
; i
++)
223 if (!strcmp(argv
[i
], "-sections"))
225 else if (!strcmp(argv
[i
], "-exports"))
228 { printf("Invalid option: %s\n", argv
[i
]); return 1; }
231 if (fixup_sections
== 0 && fixup_exports
== 0)
233 printf("Nothing to do.\n");
238 * Read the whole file to memory.
241 fd_in
= open(argv
[1], O_RDONLY
| O_BINARY
);
244 printf("Can't open input file.\n");
248 len
= lseek(fd_in
, 0, SEEK_END
);
249 if (len
< sizeof(IMAGE_DOS_HEADER
))
252 printf("'%s' isn't a PE image.\n", argv
[1]);
256 buffer
= malloc((len
+ 1) & ~1);
260 printf("Not enough memory available.\n");
264 /* Read the whole input file into a buffer */
265 lseek(fd_in
, 0, SEEK_SET
);
266 read(fd_in
, buffer
, len
);
273 * Check the headers and save pointers to them.
276 dos_header
= (PIMAGE_DOS_HEADER
)buffer
;
277 nt_header
= (PIMAGE_NT_HEADERS
)(buffer
+ dos_header
->e_lfanew
);
279 if (dos_header
->e_magic
!= IMAGE_DOS_SIGNATURE
||
280 nt_header
->Signature
!= IMAGE_NT_SIGNATURE
)
282 printf("'%s' isn't a PE image.\n", argv
[1]);
289 /* Sort export directory */
290 data_dir
= &nt_header
->OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_EXPORT
];
291 if (data_dir
->Size
!= 0)
293 PIMAGE_EXPORT_DIRECTORY export_directory
;
298 export_directory
= (PIMAGE_EXPORT_DIRECTORY
)rva_to_ptr(data_dir
->VirtualAddress
);
299 if (export_directory
!= NULL
)
301 exports
= malloc(sizeof(export_t
) * export_directory
->NumberOfNames
);
304 printf("Not enough memory.\n");
309 name_ptr
= (DWORD
*)rva_to_ptr(export_directory
->AddressOfNames
);
310 ordinal_ptr
= (WORD
*)rva_to_ptr(export_directory
->AddressOfNameOrdinals
);
312 for (i
= 0; i
< export_directory
->NumberOfNames
; i
++)
314 exports
[i
].name
= name_ptr
[i
];
315 exports
[i
].ordinal
= ordinal_ptr
[i
];
318 qsort(exports
, export_directory
->NumberOfNames
, sizeof(export_t
),
319 export_compare_func
);
321 for (i
= 0; i
< export_directory
->NumberOfNames
; i
++)
323 name_ptr
[i
] = exports
[i
].name
;
324 ordinal_ptr
[i
] = exports
[i
].ordinal
;
334 /* Update section flags */
335 for (i
= 0, section_header
= IMAGE_FIRST_SECTION(nt_header
);
336 i
< nt_header
->OptionalHeader
.NumberOfRvaAndSizes
;
337 i
++, section_header
++)
339 if (!strcmp(section_header
->Name
, ".text") ||
340 !strcmp(section_header
->Name
, ".data") ||
341 !strcmp(section_header
->Name
, ".idata") ||
342 !strcmp(section_header
->Name
, ".bss"))
344 section_header
->Characteristics
|= IMAGE_SCN_MEM_NOT_PAGED
;
345 section_header
->Characteristics
&= ~IMAGE_SCN_MEM_DISCARDABLE
;
347 else if (!strcmp(section_header
->Name
, "INIT"))
349 section_header
->Characteristics
|= IMAGE_SCN_MEM_DISCARDABLE
;
351 else if (!strcmp(section_header
->Name
, "PAGE"))
353 section_header
->Characteristics
|= IMAGE_SCN_MEM_NOT_PAGED
;
358 /* Recalculate checksum */
359 nt_header
->OptionalHeader
.CheckSum
= 0;
361 for (i
= 0; i
< len
; i
+= 2)
363 checksum
+= *(unsigned short *)(buffer
+ i
);
364 checksum
= (checksum
+ (checksum
>> 16)) & 0xffff;
367 nt_header
->OptionalHeader
.CheckSum
= checksum
;
369 /* Write the output file */
370 fd_out
= open(argv
[1], O_WRONLY
| O_BINARY
);
371 write(fd_out
, buffer
, len
);