-
Notifications
You must be signed in to change notification settings - Fork 0
/
peflags.c
1130 lines (1042 loc) · 32.3 KB
/
peflags.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2009, 2011 Charles Wilson
* Based on rebase.c by Jason Tishler
* Significant contributions by Dave Korn
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* See the COPYING file for full license information.
*/
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <stddef.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <getopt.h>
#include <errno.h>
#include <limits.h>
#if defined (__CYGWIN__) || defined (__MSYS__)
#include <sys/mman.h>
#endif
#if defined(__MSYS__)
/* MSYS has no inttypes.h */
# define PRIu64 "llu"
# define PRIx64 "llx"
#else
# include <inttypes.h>
#endif
#include <windows.h>
#if defined(__MSYS__)
/* MSYS has no strtoull */
unsigned long long strtoull(const char *, char **, int);
#endif
/* Fix broken definitions in older w32api. */
#ifndef IMAGE_DLLCHARACTERISTICS_DYNAMIC_BASE
#define IMAGE_DLLCHARACTERISTICS_DYNAMIC_BASE IMAGE_DLL_CHARACTERISTICS_DYNAMIC_BASE
#endif
#ifndef IMAGE_DLLCHARACTERISTICS_NX_COMPAT
#define IMAGE_DLLCHARACTERISTICS_NX_COMPAT IMAGE_DLL_CHARACTERISTICS_NX_COMPAT
#endif
#ifndef IMAGE_DLLCHARACTERISTICS_FORCE_INTEGRITY
#define IMAGE_DLLCHARACTERISTICS_FORCE_INTEGRITY IMAGE_DLL_CHARACTERISTICS_FORCE_INTEGRITY
#endif
static WORD coff_characteristics_set;
static WORD coff_characteristics_clr;
static WORD coff_characteristics_show;
static WORD pe_characteristics_set;
static WORD pe_characteristics_clr;
static WORD pe_characteristics_show;
typedef struct
{
const char *pathname;
PIMAGE_DOS_HEADER dosheader;
union
{
PIMAGE_NT_HEADERS32 ntheader32;
PIMAGE_NT_HEADERS64 ntheader64;
};
BOOL is_64bit;
} pe_file;
typedef struct {
long flag;
const char * name;
size_t len;
} symbolic_flags_t;
#define CF(flag,name) { flag, #name, sizeof(#name) - 1 }
static const symbolic_flags_t coff_symbolic_flags[] = {
CF(0x0001, relocs_stripped),
CF(0x0002, executable_image),
CF(0x0004, line_nums_stripped),
CF(0x0008, local_syms_stripped),
CF(0x0010, wstrim),
CF(0x0020, bigaddr),
/*CF(0x0040, unspec_0x0040),*/
CF(0x0080, bytes_reversed_lo),
CF(0x0100, 32bit_machine),
CF(0x0200, sepdbg),
CF(0x0400, removable_run_from_swap),
CF(0x0800, net_run_from_swap),
CF(0x1000, system),
CF(0x2000, dll),
CF(0x4000, uniprocessor_only),
CF(0x8000, bytes_reversed_hi),
{0, 0, 0}
};
static const symbolic_flags_t pe_symbolic_flags[] = {
/*CF(0x0001, reserved_0x0001),*/
/*CF(0x0002, reserved_0x0002),*/
/*CF(0x0004, reserved_0x0004),*/
/*CF(0x0008, reserved_0x0008),*/
/*CF(0x0010, unspec_0x0010),*/
/*CF(0x0020, unspec_0x0020),*/
CF(0x0040, dynamicbase),
CF(0x0080, forceinteg),
CF(0x0100, nxcompat),
CF(0x0200, no-isolation),
CF(0x0400, no-seh),
CF(0x0800, no-bind),
/*CF(0x1000, reserved_0x1000),*/
CF(0x2000, wdmdriver),
/*CF(0x4000, reserved_0x4000),*/
CF(0x8000, tsaware),
{0, 0, 0}
};
enum {
SIZEOF_STACK_RESERVE = 0,
SIZEOF_STACK_COMMIT,
SIZEOF_HEAP_RESERVE,
SIZEOF_HEAP_COMMIT,
SIZEOF_CYGWIN_HEAP,
NUM_SIZEOF_VALUES /* Keep at the end */
};
typedef enum {
DONT_HANDLE = 0,
DO_READ = 1,
DO_WRITE = 2
} do_handle_t;
static do_handle_t handle_any_sizeof;
typedef struct {
ULONGLONG value;
const char *name;
const char *unit;
do_handle_t handle;
BOOL is_ulong; /* Only for 64 bit files. */
ULONG offset64;
ULONG offset32;
} sizeof_values_t;
sizeof_values_t sizeof_vals[5] = {
{ 0, "stack reserve size" , "bytes", 0, FALSE,
offsetof (IMAGE_NT_HEADERS64, OptionalHeader.SizeOfStackReserve),
offsetof (IMAGE_NT_HEADERS32, OptionalHeader.SizeOfStackReserve),
},
{ 0, "stack commit size" , "bytes", 0, FALSE,
offsetof (IMAGE_NT_HEADERS64, OptionalHeader.SizeOfStackCommit),
offsetof (IMAGE_NT_HEADERS32, OptionalHeader.SizeOfStackCommit),
},
{ 0, "Win32 heap reserve size" , "bytes", 0, FALSE,
offsetof (IMAGE_NT_HEADERS64, OptionalHeader.SizeOfHeapReserve),
offsetof (IMAGE_NT_HEADERS32, OptionalHeader.SizeOfHeapReserve),
},
{ 0, "Win32 heap commit size" , "bytes", 0, FALSE,
offsetof (IMAGE_NT_HEADERS64, OptionalHeader.SizeOfHeapCommit),
offsetof (IMAGE_NT_HEADERS32, OptionalHeader.SizeOfHeapCommit),
},
{ 0, "initial Cygwin heap size", "MB", 0, TRUE,
offsetof (IMAGE_NT_HEADERS64, OptionalHeader.LoaderFlags),
offsetof (IMAGE_NT_HEADERS32, OptionalHeader.LoaderFlags),
}
};
#define pulonglong(struct, offset) (PULONGLONG)((PBYTE)(struct)+(offset))
#define pulong(struct, offset) (PULONG)((PBYTE)(struct)+(offset))
static struct option long_options[] = {
{"dynamicbase", optional_argument, NULL, 'd'},
{"forceinteg", optional_argument, NULL, 'f'},
{"nxcompat", optional_argument, NULL, 'n'},
{"no-isolation", optional_argument, NULL, 'i'},
{"no-seh", optional_argument, NULL, 's'},
{"no-bind", optional_argument, NULL, 'b'},
{"wdmdriver", optional_argument, NULL, 'W'},
{"tsaware", optional_argument, NULL, 't'},
{"wstrim", optional_argument, NULL, 'w'},
{"bigaddr", optional_argument, NULL, 'l'},
{"sepdbg", optional_argument, NULL, 'S'},
{"stack-reserve",optional_argument, NULL, 'x'},
{"stack-commit", optional_argument, NULL, 'X'},
{"heap-reserve", optional_argument, NULL, 'y'},
{"heap-commit", optional_argument, NULL, 'Y'},
{"cygwin-heap", optional_argument, NULL, 'z'},
{"filelist", no_argument, NULL, 'T'},
{"verbose", no_argument, NULL, 'v'},
{"help", no_argument, NULL, 'h'},
{"version", no_argument, NULL, 'V'},
{NULL, no_argument, NULL, 0}
};
static const char *short_options
= "d::f::n::i::s::b::W::t::w::l::S::x::X::y::Y::z::T:vhV";
static void short_usage (FILE *f);
static void help (FILE *f);
static void version (FILE *f);
int do_mark (const char *pathname);
pe_file *pe_open (const char *path, BOOL writing);
void pe_close (pe_file *pep);
void get_and_set_sizes(const pe_file *pep);
int get_characteristics(const pe_file *pep,
WORD* coff_characteristics,
WORD* pe_characteristics);
int set_coff_characteristics(const pe_file *pep,
WORD coff_characteristics);
int set_pe_characteristics(const pe_file *pep,
WORD pe_characteristics);
static void display_flags (const char *field_name, const symbolic_flags_t *syms,
WORD show_symbolic, WORD old_flag_value,
WORD new_flag_value);
static char *symbolic_flags (const symbolic_flags_t *syms, long show, long value);
static void append_and_decorate (char **str, int is_set, const char *name, int len);
static void *xmalloc (size_t num);
#define XMALLOC(type, num) ((type *) xmalloc ((num) * sizeof(type)))
static void handle_coff_flag_option (const char *option_name,
const char *option_arg,
WORD flag_value);
static void handle_pe_flag_option (const char *option_name,
const char *option_arg,
WORD flag_value);
static void handle_num_option (const char *option_name,
const char *option_arg,
int option_index);
void parse_args (int argc, char *argv[]);
int string_to_bool (const char *string, int *value);
int string_to_ulonglong (const char *string, unsigned long long *value);
FILE *file_list_fopen (const char *file_list);
char *file_list_fgets (char *buf, int size, FILE *file);
int file_list_fclose (FILE *file);
int args_index = 0;
int verbose = 0;
const char *file_list = 0;
const char *stdin_file_list = "-";
int mark_any = 0;
int
main (int argc, char *argv[])
{
int files_attempted = 0;
int i = 0;
int ret = 0;
parse_args (argc, argv);
/* Operate on files in file list, if specified. */
if (file_list)
{
int status = 0;
char filename[MAX_PATH + 2];
FILE *file = file_list_fopen (file_list);
if (!file)
exit (2);
while (file_list_fgets (filename, MAX_PATH + 2, file))
{
files_attempted++;
if ((status = do_mark (filename)) != 0)
ret = 2;
}
file_list_fclose (file);
if (files_attempted == 0)
{
/* warn the user */
fprintf (stderr,
"Error: Could not find any filenames in %s\n",
(strcmp(file_list, stdin_file_list) == 0 ? "<stdin>" : file_list));
}
}
/* Operate on files listed as command line arguments.
* Don't reset files_attempted because we do not require
* any args if -T filelist
*/
for (i = args_index; i < argc; i++)
{
const char *filename = argv[i];
files_attempted++;
if (do_mark (filename) != 0)
ret = 2;
}
if (files_attempted == 0)
{
/* warn the user */
fprintf (stderr, "Error: no files to process\n");
short_usage (stderr);
exit (2);
}
return ret;
}
int
do_mark (const char *pathname)
{
int has_relocs;
int is_executable;
int is_dll;
WORD old_coff_characteristics;
WORD new_coff_characteristics;
WORD old_pe_characteristics;
WORD new_pe_characteristics;
/* Skip if file does not exist */
if (access (pathname, F_OK) == -1)
{
fprintf (stderr, "%s: skipped because nonexistent\n", pathname);
return 0;
}
if (mark_any)
{
/* Skip if not writable. */
if (access (pathname, W_OK) == -1)
{
fprintf (stderr, "%s: skipped because not writable\n", pathname);
return 0;
}
}
pe_file *pep = pe_open (pathname, mark_any != 0
|| handle_any_sizeof == DO_WRITE);
if (!pep)
{
fprintf (stderr,
"%s: skipped because could not open\n",
pathname);
return 0;
}
get_characteristics (pep,
&old_coff_characteristics,
&old_pe_characteristics);
get_and_set_sizes (pep);
new_coff_characteristics = old_coff_characteristics;
new_coff_characteristics |= coff_characteristics_set;
new_coff_characteristics &= ~coff_characteristics_clr;
new_pe_characteristics = old_pe_characteristics;
new_pe_characteristics |= pe_characteristics_set;
new_pe_characteristics &= ~pe_characteristics_clr;
is_executable = ((new_coff_characteristics & IMAGE_FILE_EXECUTABLE_IMAGE) > 0);
is_dll = ((new_coff_characteristics & IMAGE_FILE_DLL) > 0);
has_relocs = ((new_coff_characteristics & IMAGE_FILE_RELOCS_STRIPPED) == 0);
/* validation and warnings about things that are
problematic, but that we are not explicitly
changing */
if (!has_relocs)
{
if (verbose
&& (new_pe_characteristics & IMAGE_DLLCHARACTERISTICS_DYNAMIC_BASE)
&& (old_pe_characteristics & IMAGE_DLLCHARACTERISTICS_DYNAMIC_BASE))
{
fprintf (stderr,
"Warning: file has no relocation info but has dynbase set (%s).\n",
pathname);
}
}
if (!is_executable || is_dll)
{
if (verbose
&& (new_pe_characteristics & IMAGE_DLLCHARACTERISTICS_TERMINAL_SERVER_AWARE)
&& (old_pe_characteristics & IMAGE_DLLCHARACTERISTICS_TERMINAL_SERVER_AWARE))
{
fprintf (stderr,
"Warning: file is non-executable but has tsaware set (%s).\n",
pathname);
}
}
if (mark_any)
{
/* validation and warnings about things we are changing */
if (!has_relocs)
{
if ( (new_pe_characteristics & IMAGE_DLLCHARACTERISTICS_DYNAMIC_BASE)
&& !(old_pe_characteristics & IMAGE_DLLCHARACTERISTICS_DYNAMIC_BASE))
{
fprintf (stderr,
"Warning: setting dynbase on file with no relocation info (%s).\n",
pathname);
}
}
if (!is_executable || is_dll)
{
if ( (new_pe_characteristics & IMAGE_DLLCHARACTERISTICS_TERMINAL_SERVER_AWARE)
&& !(old_pe_characteristics & IMAGE_DLLCHARACTERISTICS_TERMINAL_SERVER_AWARE))
{
fprintf (stderr,
"Warning: setting tsaware on non-executable (%s).\n",
pathname);
}
}
/* setting */
if (new_coff_characteristics != old_coff_characteristics)
set_coff_characteristics (pep, new_coff_characteristics);
if (new_pe_characteristics != old_pe_characteristics)
set_pe_characteristics (pep, new_pe_characteristics);
}
/* Display characteristics. */
if (verbose
|| !mark_any
|| coff_characteristics_show
|| pe_characteristics_show
|| handle_any_sizeof != DONT_HANDLE)
{
BOOL printed_characteristic = FALSE;
int i;
printf ("%s: ", pathname);
if (verbose
|| (!mark_any && handle_any_sizeof == DONT_HANDLE)
|| coff_characteristics_show || pe_characteristics_show)
{
display_flags ("coff", coff_symbolic_flags,
coff_characteristics_show ?:
verbose ? old_coff_characteristics : 0,
old_coff_characteristics,
new_coff_characteristics);
display_flags ("pe", pe_symbolic_flags,
pe_characteristics_show ?:
verbose ? old_pe_characteristics : 0,
old_pe_characteristics,
new_pe_characteristics);
puts ("");
printed_characteristic = TRUE;
}
for (i = 0; i < NUM_SIZEOF_VALUES; ++i)
{
if (sizeof_vals[i].handle != DONT_HANDLE)
{
printf ("%*s%-24s: %" PRIu64 " (0x%" PRIx64 ") %s\n",
printed_characteristic ? (int) strlen (pathname) + 2
: 0, "",
sizeof_vals[i].name,
(uint64_t) sizeof_vals[i].value,
(uint64_t) sizeof_vals[i].value,
sizeof_vals[i].unit);
printed_characteristic = TRUE;
}
}
}
pe_close (pep);
return 0;
}
static void
display_flags (const char *field_name, const symbolic_flags_t *syms,
WORD show_symbolic, WORD old_flag_value,
WORD new_flag_value)
{
if (show_symbolic)
{
if (old_flag_value != new_flag_value)
{
char * old_symbolic = symbolic_flags (syms, show_symbolic, old_flag_value);
char * new_symbolic = symbolic_flags (syms, show_symbolic, new_flag_value);
printf ("%s(0x%04x%s==>0x%04x%s) ", field_name,
old_flag_value, old_symbolic,
new_flag_value, new_symbolic);
free (old_symbolic);
free (new_symbolic);
}
else
{
char * old_symbolic = symbolic_flags (syms, show_symbolic, old_flag_value);
printf ("%s(0x%04x%s) ", field_name,
old_flag_value, old_symbolic);
free (old_symbolic);
}
}
else
{
if (old_flag_value != new_flag_value)
printf ("%s(0x%04x==>0x%04x) ", field_name,
old_flag_value, new_flag_value);
else
printf ("%s(0x%04x) ", field_name, old_flag_value);
}
}
static char *
symbolic_flags (const symbolic_flags_t *syms, long show, long value)
{
int i;
char * rVal = NULL;
for (i = 0; syms[i].name; i++)
{
if (syms[i].flag & show)
{
append_and_decorate (&rVal,
(value & syms[i].flag),
syms[i].name,
syms[i].len);
}
}
if (rVal)
{
size_t len = strlen (rVal);
char *tmp = XMALLOC (char, len + 3);
*tmp = '[';
memcpy (tmp+1, rVal, len);
tmp[len+1] = ']';
tmp[len+2] = '\0';
free (rVal);
rVal = tmp;
}
return rVal;
}
static void
append_and_decorate (char **str, int is_set, const char *name, int len)
{
char *tmp;
int slen;
if (!*str)
{
*str = XMALLOC (char, len + 2);
(*str)[0] = (is_set ? '+' : '-');
memcpy ((*str)+1, name, len);
(*str)[len+1] = '\0';
return;
}
else
{
slen = strlen (*str);
tmp = XMALLOC (char, slen + 2 + len + 1);
memcpy (tmp, *str, slen);
free (*str);
*str = tmp;
tmp = *str + slen;
*tmp++ = ',';
*tmp++ = (is_set ? '+' : '-');
memcpy (tmp, name, len);
tmp[len] = '\0';
}
}
static void *
xmalloc (size_t num)
{
void *p = (void *) malloc (num);
if (!p)
{
fputs ("Memory exhausted", stderr);
exit (2);
}
return p;
}
static void
handle_num_option (const char *option_name,
const char *option_arg,
int option_index)
{
if (!option_arg)
{
if (sizeof_vals[option_index].handle == DONT_HANDLE)
sizeof_vals[option_index].handle = DO_READ;
if (handle_any_sizeof == DONT_HANDLE)
handle_any_sizeof = DO_READ;
}
else if (string_to_ulonglong (option_arg, &sizeof_vals[option_index].value)
/* 48 bit address space */
|| sizeof_vals[option_index].value > 0x0000ffffffffffffULL
/* Just a ULONG value */
|| (sizeof_vals[option_index].is_ulong
&& sizeof_vals[option_index].value > ULONG_MAX))
{
fprintf (stderr, "Invalid argument for %s: %s\n",
option_name, option_arg);
short_usage (stderr);
exit (1);
}
else
{
sizeof_vals[option_index].handle = DO_WRITE;
handle_any_sizeof = DO_WRITE;
}
}
static void
handle_pe_flag_option (const char *option_name,
const char *option_arg,
WORD flag_value)
{
int bool_value;
if (!option_arg)
{
pe_characteristics_show |= flag_value;
}
else
{
if (string_to_bool (option_arg, &bool_value) != 0)
{
fprintf (stderr, "Invalid argument for %s: %s\n",
option_name, option_arg);
short_usage (stderr);
exit (1);
}
if (bool_value)
pe_characteristics_set |= flag_value;
else
pe_characteristics_clr |= flag_value;
}
}
static void
handle_coff_flag_option (const char *option_name,
const char *option_arg,
WORD flag_value)
{
int bool_value;
if (!option_arg)
{
coff_characteristics_show |= flag_value;
}
else
{
if (string_to_bool (option_arg, &bool_value) != 0)
{
fprintf (stderr, "Invalid argument for %s: %s\n",
option_name, option_arg);
short_usage (stderr);
exit (1);
}
if (bool_value)
coff_characteristics_set |= flag_value;
else
coff_characteristics_clr |= flag_value;
}
}
void
parse_args (int argc, char *argv[])
{
int c;
while (1)
{
int option_index = 0;
c = getopt_long (argc, argv, short_options, long_options, &option_index);
if (c == -1)
break;
/* Workaround the problem that option_index is not valid if the user
specified a short option. */
if (option_index == 0 && c != long_options[0].val)
{
for (option_index = 1;
long_options[option_index].name;
++option_index)
if (long_options[option_index].val == c)
break;
}
switch (c)
{
case 'h':
help (stdout);
exit (0);
break;
case 'V':
version (stdout);
exit (0);
break;
case 'd':
handle_pe_flag_option (long_options[option_index].name,
optarg,
IMAGE_DLLCHARACTERISTICS_DYNAMIC_BASE);
break;
case 'n':
handle_pe_flag_option (long_options[option_index].name,
optarg,
IMAGE_DLLCHARACTERISTICS_NX_COMPAT);
break;
case 't':
handle_pe_flag_option (long_options[option_index].name,
optarg,
IMAGE_DLLCHARACTERISTICS_TERMINAL_SERVER_AWARE);
break;
case 'f':
handle_pe_flag_option (long_options[option_index].name,
optarg,
IMAGE_DLLCHARACTERISTICS_FORCE_INTEGRITY);
break;
case 'i':
handle_pe_flag_option (long_options[option_index].name,
optarg,
IMAGE_DLLCHARACTERISTICS_NO_ISOLATION);
break;
case 's':
handle_pe_flag_option (long_options[option_index].name,
optarg,
IMAGE_DLLCHARACTERISTICS_NO_SEH);
break;
case 'b':
handle_pe_flag_option (long_options[option_index].name,
optarg,
IMAGE_DLLCHARACTERISTICS_NO_BIND);
break;
case 'W':
handle_pe_flag_option (long_options[option_index].name,
optarg,
IMAGE_DLLCHARACTERISTICS_WDM_DRIVER);
break;
case 'w':
handle_coff_flag_option (long_options[option_index].name,
optarg,
IMAGE_FILE_AGGRESIVE_WS_TRIM);
break;
case 'l':
handle_coff_flag_option (long_options[option_index].name,
optarg,
IMAGE_FILE_LARGE_ADDRESS_AWARE);
break;
case 'S':
handle_coff_flag_option (long_options[option_index].name,
optarg,
IMAGE_FILE_DEBUG_STRIPPED);
break;
case 'x':
handle_num_option (long_options[option_index].name,
optarg,
SIZEOF_STACK_RESERVE);
break;
case 'X':
handle_num_option (long_options[option_index].name,
optarg,
SIZEOF_STACK_COMMIT);
break;
case 'y':
handle_num_option (long_options[option_index].name,
optarg,
SIZEOF_HEAP_RESERVE);
break;
case 'Y':
handle_num_option (long_options[option_index].name,
optarg,
SIZEOF_HEAP_COMMIT);
break;
case 'z':
handle_num_option (long_options[option_index].name,
optarg,
SIZEOF_CYGWIN_HEAP);
break;
case 'T':
file_list = optarg;
break;
case 'v':
verbose = TRUE;
break;
case '?':
break;
default:
short_usage (stderr);
exit (1);
break;
}
}
args_index = optind;
mark_any = pe_characteristics_set
| pe_characteristics_clr
| coff_characteristics_set
| coff_characteristics_clr;
}
int
string_to_bool (const char *string, int *value)
{
unsigned long long number = 0;
if (!string || !*string)
return 1;
if (string_to_ulonglong (string, &number) != 0)
{
size_t len = strlen (string);
if ( (len == 4 && strcasecmp (string, "true") == 0)
||(len == 3 && strcasecmp (string, "yes") == 0)
||(len == 1 && strcasecmp (string, "t") == 0)
||(len == 1 && strcasecmp (string, "y") == 0))
{
*value = TRUE;
}
else if ( (len == 5 && strcasecmp (string, "false") == 0)
||(len == 2 && strcasecmp (string, "no") == 0)
||(len == 1 && strcasecmp (string, "f") == 0)
||(len == 1 && strcasecmp (string, "n") == 0))
{
*value = FALSE;
}
else
{
return 1;
}
}
else
{
*value = (number != 0);
}
return 0;
}
int
string_to_ulonglong (const char *string, unsigned long long *value)
{
unsigned long long number = 0;
char * endp;
errno = 0;
/* null or empty input */
if (!string || !*string)
return 1;
number = strtoull (string, &endp, 0);
/* out of range */
if (ERANGE == errno)
return 1;
/* no valid numeric input */
if (endp == string)
return 1;
/* non-numeric trailing characters */
if (*endp != '\0')
return 1;
*value = number;
return 0;
}
#if !defined (__CYGWIN__) && !defined (__MSYS__)
/* Minimal mmap on files for Win32 */
#define PROT_READ 0
#define PROT_WRITE 1
#define MAP_SHARED 0
#define MAP_FAILED NULL
void *
mmap (void *addr, size_t length, int prot, int flags, int fd, off_t offset)
{
HANDLE fh = (HANDLE) _get_osfhandle (fd);
HANDLE h = CreateFileMapping (fh, NULL, prot ? PAGE_READWRITE : PAGE_READONLY,
0, 0, NULL);
if (!h)
return MAP_FAILED;
addr = MapViewOfFileEx (h, prot ? FILE_MAP_WRITE : FILE_MAP_READ,
0, 0, 4096, addr);
CloseHandle (h);
return addr;
}
int
munmap (void *addr, size_t len)
{
UnmapViewOfFile (addr);
return 0;
}
#endif
pe_file *
pe_open (const char *path, BOOL writing)
{
/* Non-threaded application. */
static pe_file pef;
int fd;
void *map;
fd = open (path, O_BINARY | (writing ? O_RDWR : O_RDONLY));
if (fd == -1)
return NULL;
map = mmap (NULL, 4096, PROT_READ | (writing ? PROT_WRITE : 0), MAP_SHARED,
fd, 0);
if (map == MAP_FAILED)
{
close (fd);
return NULL;
}
pef.pathname = path;
pef.dosheader = (PIMAGE_DOS_HEADER) map;
pef.ntheader32 = (PIMAGE_NT_HEADERS32)
((PBYTE) pef.dosheader + pef.dosheader->e_lfanew);
/* Sanity checks */
if (pef.dosheader->e_magic != 0x5a4d /* "MZ" */
|| (PBYTE) pef.ntheader32 - (PBYTE) map + sizeof *pef.ntheader32 >= 4096
|| pef.ntheader32->Signature != 0x00004550)
{
munmap (map, 4096);
close (fd);
return NULL;
}
pef.is_64bit = pef.ntheader32->OptionalHeader.Magic
== IMAGE_NT_OPTIONAL_HDR64_MAGIC;
return &pef;
}
void
pe_close (pe_file *pep)
{
if (pep)
munmap ((void *) pep->dosheader, 4096);
}
void
get_and_set_size (const pe_file *pep, sizeof_values_t *val)
{
if (val->handle == DO_READ)
{
if (!pep->is_64bit)
val->value = *pulong (pep->ntheader32, val->offset32);
else if (val->is_ulong)
val->value = *pulong (pep->ntheader64, val->offset64);
else
val->value = *pulonglong (pep->ntheader64, val->offset64);
}
else if (val->handle == DO_WRITE)
{
if ((!pep->is_64bit || val->is_ulong) && val->value >= ULONG_MAX)
{
fprintf (stderr, "%s: Skip writing %s, value too big\n",
pep->pathname, val->name);
val->handle = DONT_HANDLE;
}
else if (!pep->is_64bit)
*pulong (pep->ntheader32, val->offset32) = val->value;
else if (val->is_ulong)
*pulong (pep->ntheader64, val->offset64) = val->value;
else
*pulonglong (pep->ntheader64, val->offset64) = val->value;
}
}
void
get_and_set_sizes (const pe_file *pep)
{
int i;
for (i = 0; i < NUM_SIZEOF_VALUES; ++i)
get_and_set_size (pep, sizeof_vals + i);
}
int
get_characteristics(const pe_file *pep,
WORD* coff_characteristics,
WORD* pe_characteristics)
{
if (pep->is_64bit)
{
*coff_characteristics = pep->ntheader64->FileHeader.Characteristics;
*pe_characteristics = pep->ntheader64->OptionalHeader.DllCharacteristics;
}
else
{
*coff_characteristics = pep->ntheader32->FileHeader.Characteristics;
*pe_characteristics = pep->ntheader32->OptionalHeader.DllCharacteristics;
}
return 0;
}
int
set_coff_characteristics(const pe_file *pep,
WORD coff_characteristics)
{
if (pep->is_64bit)
pep->ntheader64->FileHeader.Characteristics = coff_characteristics;
else
pep->ntheader32->FileHeader.Characteristics = coff_characteristics;
return 0;