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
35 /* The following definitions are ripped from MinGW W32API headers. We don't
36 use these headers directly in order to allow compilation on Linux hosts. */
38 typedef unsigned char BYTE
, *PBYTE
;
39 typedef unsigned short WORD
;
40 typedef unsigned int DWORD
;
42 typedef long LONG_PTR
;
44 #define IMAGE_NUMBEROF_DIRECTORY_ENTRIES 16
45 #define IMAGE_SIZEOF_SHORT_NAME 8
46 #define IMAGE_DOS_SIGNATURE 0x5A4D
47 #define IMAGE_NT_SIGNATURE 0x00004550
48 #define IMAGE_SCN_MEM_DISCARDABLE 0x2000000
49 #define IMAGE_SCN_MEM_NOT_PAGED 0x8000000
50 #define FIELD_OFFSET(t,f) ((LONG)(LONG_PTR)&(((t*)0)->f))
51 #define IMAGE_FIRST_SECTION(h) ((PIMAGE_SECTION_HEADER) ((unsigned long)h+FIELD_OFFSET(IMAGE_NT_HEADERS,OptionalHeader)+((PIMAGE_NT_HEADERS)(h))->FileHeader.SizeOfOptionalHeader))
52 #define IMAGE_DIRECTORY_ENTRY_EXPORT 0
55 typedef struct _IMAGE_DOS_HEADER
{
75 } IMAGE_DOS_HEADER
,*PIMAGE_DOS_HEADER
;
78 typedef struct _IMAGE_EXPORT_DIRECTORY
{
79 DWORD Characteristics
;
85 DWORD NumberOfFunctions
;
87 DWORD AddressOfFunctions
;
89 DWORD AddressOfNameOrdinals
;
90 } IMAGE_EXPORT_DIRECTORY
,*PIMAGE_EXPORT_DIRECTORY
;
91 typedef struct _IMAGE_FILE_HEADER
{
93 WORD NumberOfSections
;
95 DWORD PointerToSymbolTable
;
96 DWORD NumberOfSymbols
;
97 WORD SizeOfOptionalHeader
;
99 } IMAGE_FILE_HEADER
, *PIMAGE_FILE_HEADER
;
100 typedef struct _IMAGE_DATA_DIRECTORY
{
101 DWORD VirtualAddress
;
103 } IMAGE_DATA_DIRECTORY
,*PIMAGE_DATA_DIRECTORY
;
104 typedef struct _IMAGE_OPTIONAL_HEADER
{
106 BYTE MajorLinkerVersion
;
107 BYTE MinorLinkerVersion
;
109 DWORD SizeOfInitializedData
;
110 DWORD SizeOfUninitializedData
;
111 DWORD AddressOfEntryPoint
;
115 DWORD SectionAlignment
;
117 WORD MajorOperatingSystemVersion
;
118 WORD MinorOperatingSystemVersion
;
119 WORD MajorImageVersion
;
120 WORD MinorImageVersion
;
121 WORD MajorSubsystemVersion
;
122 WORD MinorSubsystemVersion
;
128 WORD DllCharacteristics
;
129 DWORD SizeOfStackReserve
;
130 DWORD SizeOfStackCommit
;
131 DWORD SizeOfHeapReserve
;
132 DWORD SizeOfHeapCommit
;
134 DWORD NumberOfRvaAndSizes
;
135 IMAGE_DATA_DIRECTORY DataDirectory
[IMAGE_NUMBEROF_DIRECTORY_ENTRIES
];
136 } IMAGE_OPTIONAL_HEADER
,*PIMAGE_OPTIONAL_HEADER
;
137 typedef struct _IMAGE_NT_HEADERS
{
139 IMAGE_FILE_HEADER FileHeader
;
140 IMAGE_OPTIONAL_HEADER OptionalHeader
;
141 } IMAGE_NT_HEADERS
,*PIMAGE_NT_HEADERS
;
142 typedef struct _IMAGE_SECTION_HEADER
{
143 BYTE Name
[IMAGE_SIZEOF_SHORT_NAME
];
145 DWORD PhysicalAddress
;
148 DWORD VirtualAddress
;
150 DWORD PointerToRawData
;
151 DWORD PointerToRelocations
;
152 DWORD PointerToLinenumbers
;
153 WORD NumberOfRelocations
;
154 WORD NumberOfLinenumbers
;
155 DWORD Characteristics
;
156 } IMAGE_SECTION_HEADER
,*PIMAGE_SECTION_HEADER
;
159 /* End of ripped definitions */
161 typedef struct _export_t
{
166 unsigned char *buffer
;
167 PIMAGE_DOS_HEADER dos_header
;
168 PIMAGE_NT_HEADERS nt_header
;
170 static inline WORD
dtohs(WORD in
)
172 PBYTE in_ptr
= (PBYTE
)&in
;
173 return in_ptr
[0] | (in_ptr
[1] << 8);
176 static inline WORD
htods(WORD in
)
179 PBYTE out_ptr
= (PBYTE
)&out
;
180 out_ptr
[0] = in
; out_ptr
[1] = in
>> 8;
184 static inline DWORD
dtohl(DWORD in
)
186 PBYTE in_ptr
= (PBYTE
)&in
;
187 return in_ptr
[0] | (in_ptr
[1] << 8) | (in_ptr
[2] << 16) | (in_ptr
[3] << 24);
190 static inline DWORD
htodl(DWORD in
)
193 PBYTE out_ptr
= (PBYTE
)&out
;
194 out_ptr
[0] = in
; out_ptr
[1] = in
>> 8;
195 out_ptr
[2] = in
>> 16; out_ptr
[3] = in
>> 24;
199 void *rva_to_ptr(DWORD rva
)
201 PIMAGE_SECTION_HEADER section_header
;
204 for (i
= 0, section_header
= IMAGE_FIRST_SECTION(nt_header
);
205 i
< dtohl(nt_header
->FileHeader
.NumberOfSections
);
206 i
++, section_header
++)
208 if (rva
>= dtohl(section_header
->VirtualAddress
) &&
209 rva
< dtohl(section_header
->VirtualAddress
) +
210 dtohl(section_header
->Misc
.VirtualSize
))
212 return buffer
+ rva
- dtohl(section_header
->VirtualAddress
) +
213 dtohl(section_header
->PointerToRawData
);
220 int export_compare_func(const void *a
, const void *b
)
222 const export_t
*ap
= a
;
223 const export_t
*bp
= b
;
224 char *an
= rva_to_ptr(ap
->name
);
225 char *bn
= rva_to_ptr(bp
->name
);
226 return strcmp(an
, bn
);
229 int main(int argc
, char **argv
)
233 char hdrbuf
[4] = { }, elfhdr
[4] = { '\177', 'E', 'L', 'F' };
234 PIMAGE_SECTION_HEADER section_header
;
235 PIMAGE_DATA_DIRECTORY data_dir
;
237 unsigned long checksum
;
238 int fixup_exports
= 0;
239 int fixup_sections
= 0;
242 * Process parameters.
247 printf("Usage: %s <filename> <options>\n"
249 " -sections Sets section flags for PE image.\n"
250 " -exports Sort the names in export table.\n",
255 for (i
= 2; i
< argc
; i
++)
257 if (!strcmp(argv
[i
], "-sections"))
259 else if (!strcmp(argv
[i
], "-exports"))
262 { printf("Invalid option: %s\n", argv
[i
]); return 1; }
265 if (fixup_sections
== 0 && fixup_exports
== 0)
267 printf("Nothing to do.\n");
272 * Read the whole file to memory.
275 fd_in
= open(argv
[1], O_RDONLY
| O_BINARY
);
278 printf("Can't open input file.\n");
283 * PowerPC ReactOS uses elf, so doesn't need pefixup
285 len
= read(fd_in
, hdrbuf
, sizeof(elfhdr
));
286 if (!memcmp(hdrbuf
, elfhdr
, sizeof(elfhdr
)))
289 len
= lseek(fd_in
, 0, SEEK_END
);
290 if (len
< sizeof(IMAGE_DOS_HEADER
))
293 printf("'%s' isn't a PE image (too short)\n", argv
[1]);
297 /* Lower down we overwrite the byte at len, so here, we need at least
298 * one more byte than len. We'll be guaranteed one or two now. */
299 buffer
= malloc((len
+ 2) & ~1);
303 printf("Not enough memory available.\n");
307 /* Read the whole input file into a buffer */
308 lseek(fd_in
, 0, SEEK_SET
);
309 read(fd_in
, buffer
, len
);
310 /* Here is where the block end overwrite was */
317 * Check the headers and save pointers to them.
320 dos_header
= (PIMAGE_DOS_HEADER
)buffer
;
321 nt_header
= (PIMAGE_NT_HEADERS
)(buffer
+ dtohl(dos_header
->e_lfanew
));
323 if (dtohs(dos_header
->e_magic
) != IMAGE_DOS_SIGNATURE
||
324 dtohl(nt_header
->Signature
) != IMAGE_NT_SIGNATURE
)
326 printf("'%s' isn't a PE image (bad headers)\n", argv
[1]);
333 /* Sort export directory */
334 data_dir
= &nt_header
->OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_EXPORT
];
335 if (dtohl(data_dir
->Size
) != 0)
337 PIMAGE_EXPORT_DIRECTORY export_directory
;
342 export_directory
= (PIMAGE_EXPORT_DIRECTORY
)rva_to_ptr(dtohl(data_dir
->VirtualAddress
));
343 if (export_directory
!= NULL
)
345 exports
= malloc(sizeof(export_t
) * dtohl(export_directory
->NumberOfNames
));
348 printf("Not enough memory.\n");
353 name_ptr
= (DWORD
*)rva_to_ptr(dtohl(export_directory
->AddressOfNames
));
354 ordinal_ptr
= (WORD
*)rva_to_ptr(dtohl(export_directory
->AddressOfNameOrdinals
));
356 for (i
= 0; i
< dtohl(export_directory
->NumberOfNames
); i
++)
358 exports
[i
].name
= dtohl(name_ptr
[i
]);
359 exports
[i
].ordinal
= dtohl(ordinal_ptr
[i
]);
362 qsort(exports
, dtohl(export_directory
->NumberOfNames
), sizeof(export_t
),
363 export_compare_func
);
365 for (i
= 0; i
< dtohl(export_directory
->NumberOfNames
); i
++)
367 name_ptr
[i
] = htodl(exports
[i
].name
);
368 ordinal_ptr
[i
] = htodl(exports
[i
].ordinal
);
378 /* Update section flags */
379 for (i
= 0, section_header
= IMAGE_FIRST_SECTION(nt_header
);
380 i
< dtohl(nt_header
->OptionalHeader
.NumberOfRvaAndSizes
);
381 i
++, section_header
++)
383 if (!strcmp((char*)section_header
->Name
, ".text") ||
384 !strcmp((char*)section_header
->Name
, ".data") ||
385 !strcmp((char*)section_header
->Name
, ".idata") ||
386 !strcmp((char*)section_header
->Name
, ".bss"))
388 section_header
->Characteristics
|= htodl(IMAGE_SCN_MEM_NOT_PAGED
);
389 section_header
->Characteristics
&= htodl(~IMAGE_SCN_MEM_DISCARDABLE
);
391 else if (!strcmp((char*)section_header
->Name
, "INIT"))
393 section_header
->Characteristics
|= htodl(IMAGE_SCN_MEM_DISCARDABLE
);
395 else if (!strcmp((char*)section_header
->Name
, "PAGE"))
397 section_header
->Characteristics
|= htodl(IMAGE_SCN_MEM_NOT_PAGED
);
402 /* Recalculate checksum */
403 nt_header
->OptionalHeader
.CheckSum
= 0;
405 for (i
= 0; i
< len
; i
+= 2)
407 checksum
+= *(unsigned short *)(buffer
+ i
);
408 checksum
= (checksum
+ (checksum
>> 16)) & 0xffff;
411 nt_header
->OptionalHeader
.CheckSum
= htods(checksum
);
413 /* Write the output file */
414 fd_out
= open(argv
[1], O_WRONLY
| O_BINARY
);
415 write(fd_out
, buffer
, len
);