-
Notifications
You must be signed in to change notification settings - Fork 0
/
rebase.c
1526 lines (1429 loc) · 43.9 KB
/
rebase.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) 2001, 2002, 2003, 2004, 2008, 2011, 2012, 2013 Jason Tishler
*
* 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.
*
* Written by Jason Tishler <jason@tishler.net>
*
* $Id$
*/
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <limits.h>
#include <sys/types.h>
#include <sys/stat.h>
#if defined(__CYGWIN__) || defined(__MSYS__)
#include <sys/cygwin.h>
#endif
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <locale.h>
#include <getopt.h>
#include <string.h>
#include <errno.h>
#include "imagehelper.h"
#include "rebase-db.h"
BOOL save_image_info ();
BOOL load_image_info ();
BOOL merge_image_info ();
BOOL collect_image_info (const char *pathname);
void print_image_info ();
BOOL rebase (const char *pathname, ULONG64 *new_image_base, BOOL down_flag);
void parse_args (int argc, char *argv[]);
unsigned long long string_to_ulonglong (const char *string);
void usage ();
void help ();
BOOL is_rebaseable (const char *pathname);
FILE *file_list_fopen (const char *file_list);
char *file_list_fgets (char *buf, int size, FILE *file);
int file_list_fclose (FILE *file);
void version ();
#if defined(__MSYS__)
/* MSYS has no strtoull */
unsigned long long strtoull(const char *, char **, int);
#endif
#ifdef __x86_64__
WORD machine = IMAGE_FILE_MACHINE_AMD64;
#else
WORD machine = IMAGE_FILE_MACHINE_I386;
#endif
ULONG64 image_base = 0;
ULONG64 low_addr;
BOOL down_flag = FALSE;
BOOL image_info_flag = FALSE;
BOOL image_storage_flag = FALSE;
BOOL image_oblivious_flag = FALSE;
BOOL force_rebase_flag = FALSE;
ULONG offset = 0;
int args_index = 0;
BOOL verbose = FALSE;
BOOL quiet = FALSE;
const char *file_list = 0;
const char *stdin_file_list = "-";
const char *progname;
ULONG ALLOCATION_SLOT; /* Allocation granularity. */
img_info_t *img_info_list = NULL;
unsigned int img_info_size = 0;
unsigned int img_info_rebase_start = 0;
unsigned int img_info_max_size = 0;
#if !defined (__CYGWIN__) && !defined (__MSYS__)
#undef SYSCONFDIR
#define SYSCONFDIR "/../etc"
#endif
#define IMG_INFO_FILE_I386 SYSCONFDIR "/rebase.db.i386"
#define IMG_INFO_FILE_AMD64 SYSCONFDIR "/rebase.db.x86_64"
#ifdef __x86_64__
#define IMG_INFO_FILE IMG_INFO_FILE_AMD64
#else
#define IMG_INFO_FILE IMG_INFO_FILE_I386
#endif
char *DB_FILE = IMG_INFO_FILE;
char TMP_FILE[] = SYSCONFDIR "/rebase.db.XXXXXX";
char *db_file = NULL;
char *tmp_file = NULL;
#if defined(__CYGWIN__) || defined(__MSYS__)
ULONG64 cygwin_dll_image_base = 0;
ULONG cygwin_dll_image_size = 0;
#endif
#if defined(__MSYS__)
# define CYGWIN_DLL "/usr/bin/msys-1.0.dll"
#elif defined (__CYGWIN__)
# define CYGWIN_DLL "/usr/bin/cygwin1.dll"
#endif
#define LONG_PATH_MAX 32768
int
check_base_address_sanity (ULONG64 addr, BOOL at_start)
{
#if defined(__CYGWIN__) || defined(__MSYS__)
/* Sanity checks for Cygwin:
*
* - No DLLs below 0x38000000 on 32 bit, W10 1703+ rebase those on
* runtime anyway
* - No DLLs below 0x2:00000000, ever, on 64 bit.
*/
if (addr <= low_addr)
{
if (at_start)
fprintf (stderr, "%s: Invalid Baseaddress 0x%" PRIx64 ", must be > 0x%" PRIx64 "\n",
progname, (uint64_t) addr, (uint64_t) low_addr);
else
fprintf (stderr, "%s: Too many DLLs for available address space: %s\n",
progname, strerror (ENOMEM));
return -1;
}
#endif
return 0;
}
void
gen_progname (const char *arg0)
{
char *p;
p = strrchr (arg0, '/');
if (!p)
p = strrchr (arg0, '\\');
progname = p ? p + 1 : arg0;
if ((p = strrchr (progname, '.')) && !strcmp (p, ".exe"))
*p = 0;
}
int
main (int argc, char *argv[])
{
int i = 0;
SYSTEM_INFO si;
BOOL status;
setlocale (LC_ALL, "");
gen_progname (argv[0]);
parse_args (argc, argv);
GetSystemInfo (&si);
ALLOCATION_SLOT = si.dwAllocationGranularity;
/* If database support has been requested, load database. */
if (image_storage_flag)
{
if (load_image_info () < 0)
return 2;
img_info_rebase_start = img_info_size;
}
#if defined(__MSYS__)
if (machine == IMAGE_FILE_MACHINE_I386)
{
GetImageInfos64 ("/bin/msys-1.0.dll", NULL,
&cygwin_dll_image_base, &cygwin_dll_image_size);
}
#elif defined(__CYGWIN__)
if (machine == IMAGE_FILE_MACHINE_I386)
{
/* Fetch the Cygwin DLLs data to make sure that DLLs aren't rebased
into the memory area taken by the Cygwin DLL. */
GetImageInfos64 ("/bin/cygwin1.dll", NULL,
&cygwin_dll_image_base, &cygwin_dll_image_size);
/* Take the up to four shared memory areas preceeding the DLL into
account. */
cygwin_dll_image_base -= 4 * ALLOCATION_SLOT;
/* Add a slack of 8 * 64K at the end of the Cygwin DLL. This leave a
bit of room to install newer, bigger Cygwin DLLs, as well as room to
install non-optimized DLLs for debugging purposes. Otherwise the
slightest change might break fork again :-P */
cygwin_dll_image_size += 4 * ALLOCATION_SLOT + 8 * ALLOCATION_SLOT;
}
else
{
/* On x86_64 Cygwin, we want to keep free the whole 2 Gigs area in which
the Cygwin DLL resides, no matter what. */
cygwin_dll_image_base = 0x180000000L;
cygwin_dll_image_size = 0x080000000L;
}
#endif /* __CYGWIN__ */
/* Collect file list, if specified. */
if (file_list)
{
char filename[MAX_PATH + 2];
FILE *file = file_list_fopen (file_list);
if (!file)
return 2;
status = TRUE;
while (file_list_fgets (filename, MAX_PATH + 2, file))
{
if (strlen (filename) > 0)
{
status = collect_image_info (filename);
if (!status)
break;
}
}
file_list_fclose (file);
if (!status)
return 2;
}
/* Collect command line arguments. */
for (i = args_index; i < argc; i++)
{
const char *filename = argv[i];
if (strlen (filename) > 0)
{
status = collect_image_info (filename);
if (!status)
return 2;
}
}
/* Nothing to do? */
if (img_info_size == 0)
return 0;
/* Check what we have to do and do it. */
if (image_info_flag)
{
/* Print. */
print_image_info ();
}
else if (!image_storage_flag)
{
/* Rebase. */
ULONG64 new_image_base = image_base;
for (i = 0; i < img_info_size; ++i)
{
status = rebase (img_info_list[i].name, &new_image_base, down_flag);
if (!status)
return 2;
}
}
else
{
/* Rebase with database support. */
BOOL header;
if (merge_image_info () < 0)
return 2;
status = TRUE;
for (i = 0; i < img_info_size; ++i)
if (img_info_list[i].flag.needs_rebasing)
{
ULONG64 new_image_base = img_info_list[i].base;
status = rebase (img_info_list[i].name, &new_image_base, FALSE);
if (status)
img_info_list[i].flag.needs_rebasing = 0;
}
for (header = FALSE, i = 0; i < img_info_size; ++i)
if (img_info_list[i].flag.cannot_rebase == 1)
{
if (!header)
{
fputs ("\nThe following DLLs couldn't be rebased "
"because they were in use:\n", stderr);
header = TRUE;
}
fprintf (stderr, " %s\n", img_info_list[i].name);
}
for (header = FALSE, i = 0; i < img_info_size; ++i)
if (img_info_list[i].flag.needs_rebasing)
{
if (!header)
{
fputs ("\nThe following DLLs couldn't be rebased "
"due to errors:\n", stderr);
header = TRUE;
}
fprintf (stderr, " %s\n", img_info_list[i].name);
}
if (save_image_info () < 0)
return 2;
}
return 0;
}
#if !defined(__CYGWIN__) && !defined(__MSYS__)
int
mkstemp (char *name)
{
return _open (mktemp (name),
O_RDWR | O_BINARY | O_CREAT | O_EXCL | O_TRUNC | _O_SHORT_LIVED,
_S_IREAD|_S_IWRITE);
}
#endif
int
save_image_info ()
{
int i, fd;
int ret = 0;
img_info_hdr_t hdr;
/* Do not re-write the database if --oblivious is active */
if (image_oblivious_flag)
return 0;
/* Drop cannot_rebase flag and remove all DLLs for which rebasing failed
from the list before storing it in the database file. */
for (i = 0; i < img_info_size; ++i)
{
img_info_list[i].flag.cannot_rebase = 0;
if (img_info_list[i].flag.needs_rebasing)
img_info_list[i--] = img_info_list[--img_info_size];
}
/* Create a temporary file to write to. */
fd = mkstemp (tmp_file);
if (fd < 0)
{
fprintf (stderr, "%s: failed to create temporary rebase database: %s\n",
progname, strerror (errno));
return -1;
}
qsort (img_info_list, img_info_size, sizeof (img_info_t), img_info_name_cmp);
/* First write the number of entries. */
memcpy (hdr.magic, IMG_INFO_MAGIC, 4);
hdr.machine = machine;
hdr.version = IMG_INFO_VERSION;
hdr.base = image_base;
hdr.offset = offset;
hdr.down_flag = down_flag;
hdr.count = img_info_size;
if (write (fd, &hdr, sizeof (hdr)) < 0)
{
fprintf (stderr, "%s: failed to write rebase database: %s\n",
progname, strerror (errno));
ret = -1;
}
/* Write the list. */
else if (write (fd, img_info_list, img_info_size * sizeof (img_info_t)) < 0)
{
fprintf (stderr, "%s: failed to write rebase database: %s\n",
progname, strerror (errno));
ret = -1;
}
else
{
int i;
/* Write all strings. */
for (i = 0; i < img_info_size; ++i)
if (write (fd, img_info_list[i].name,
strlen (img_info_list[i].name) + 1) < 0)
{
fprintf (stderr, "%s: failed to write rebase database: %s\n",
progname, strerror (errno));
ret = -1;
break;
}
}
#if defined(__CYGWIN__) && !defined(__MSYS__)
/* fchmod is broken on msys */
fchmod (fd, 0660);
#else
chmod (tmp_file, 0660);
#endif
close (fd);
if (ret < 0)
unlink (tmp_file);
else
{
if (unlink (db_file) < 0 && errno != ENOENT)
{
fprintf (stderr,
"%s: failed to remove old rebase database file \"%s\":\n"
"%s\n"
"The new rebase database is stored in \"%s\".\n"
"Manually remove \"%s\" and rename \"%s\" to \"%s\",\n"
"otherwise the new rebase database will be unusable.\n",
progname, db_file,
strerror (errno),
tmp_file,
db_file, tmp_file, db_file);
ret = -1;
}
else if (rename (tmp_file, db_file) < 0)
{
fprintf (stderr,
"%s: failed to rename \"%s\" to \"%s\":\n"
"%s\n"
"Manually rename \"%s\" to \"%s\",\n"
"otherwise the new rebase database will be unusable.\n",
progname, tmp_file, db_file,
strerror (errno),
tmp_file, db_file);
ret = -1;
}
}
return ret;
}
int
load_image_info ()
{
int fd;
ssize_t read_ret;
int ret = 0;
int i;
img_info_hdr_t hdr;
fd = open (db_file, O_RDONLY | O_BINARY);
if (fd < 0)
{
/* It's no error if the file doesn't exist. However, in this case
the -b option is mandatory. */
if (errno == ENOENT && image_base)
return 0;
fprintf (stderr, "%s: failed to open rebase database \"%s\":\n%s\n",
progname, db_file, strerror (errno));
return -1;
}
/* First read the header. */
if ((read_ret = read (fd, &hdr, sizeof hdr)) != sizeof hdr)
{
if (read_ret < 0)
fprintf (stderr, "%s: failed to read rebase database \"%s\":\n%s\n",
progname, db_file, strerror (errno));
else
fprintf (stderr, "%s: premature end of rebase database \"%s\".\n",
progname, db_file);
close (fd);
return -1;
}
/* Check the header. */
if (memcmp (hdr.magic, IMG_INFO_MAGIC, 4) != 0)
{
fprintf (stderr, "%s: \"%s\" is not a valid rebase database.\n",
progname, db_file);
close (fd);
return -1;
}
if (hdr.machine != machine)
{
if (hdr.machine == IMAGE_FILE_MACHINE_I386)
fprintf (stderr,
"%s: \"%s\" is a database file for 32 bit DLLs but\n"
"I'm started to handle 64 bit DLLs. If you want to handle 32 bit DLLs,\n"
"use the -4 option.\n", progname, db_file);
else if (hdr.machine == IMAGE_FILE_MACHINE_AMD64)
fprintf (stderr,
"%s: \"%s\" is a database file for 64 bit DLLs but\n"
"I'm started to handle 32 bit DLLs. If you want to handle 64 bit DLLs,\n"
"use the -8 option.\n", progname, db_file);
else
fprintf (stderr, "%s: \"%s\" is a database file for a machine type\n"
"I don't know about.", progname, db_file);
close (fd);
return -1;
}
if (hdr.version != IMG_INFO_VERSION)
{
fprintf (stderr, "%s: \"%s\" is a version %u rebase database.\n"
"I can only handle versions up to %u.\n",
progname, db_file, hdr.version, (uint32_t) IMG_INFO_VERSION);
close (fd);
return -1;
}
/* If no new image base has been specified, use the one from the header. */
if (image_base == 0)
{
image_base = hdr.base;
down_flag = hdr.down_flag;
}
if (offset == 0)
offset = hdr.offset;
/* Don't enforce rebasing if address and offset are unchanged or taken from
the file anyway. */
if (image_base == hdr.base && offset == hdr.offset)
force_rebase_flag = FALSE;
img_info_size = hdr.count;
/* Allocate memory for the image list. */
if (ret == 0)
{
img_info_max_size = roundup (img_info_size, 100);
img_info_list = (img_info_t *) calloc (img_info_max_size,
sizeof (img_info_t));
if (!img_info_list)
{
fprintf (stderr, "%s: Out of memory.\n", progname);
ret = -1;
}
}
/* Now read the list. */
if (ret == 0
&& (read_ret = read (fd, img_info_list,
img_info_size * sizeof (img_info_t)))
!= img_info_size * sizeof (img_info_t))
{
if (read_ret < 0)
fprintf (stderr, "%s: failed to read rebase database \"%s\":\n%s\n",
progname, db_file, strerror (errno));
else
fprintf (stderr, "%s: premature end of rebase database \"%s\".\n",
progname, db_file);
ret = -1;
}
/* Make sure all pointers are NULL. */
if (ret == 0)
for (i = 0; i < img_info_size; ++i)
{
img_info_list[i].name = NULL;
/* Ensure that existing database entries are not touched when
* --oblivious is active, even if they are out-of sync with
* reality. */
if (image_oblivious_flag)
img_info_list[i].flag.cannot_rebase = 2;
}
/* Eventually read the strings. */
if (ret == 0)
{
for (i = 0; i < img_info_size; ++i)
{
img_info_list[i].name = (char *)
malloc (img_info_list[i].name_size);
if (!img_info_list[i].name)
{
fprintf (stderr, "%s: Out of memory.\n", progname);
ret = -1;
break;
}
if ((read_ret = read (fd, img_info_list[i].name,
img_info_list[i].name_size))
!= img_info_list[i].name_size)
{
if (read_ret < 0)
fprintf (stderr, "%s: failed to read rebase database \"%s\": "
"%s\n", progname, db_file, strerror (errno));
else
fprintf (stderr,
"%s: premature end of rebase database \"%s\".\n",
progname, db_file);
ret = -1;
break;
}
}
}
close (fd);
/* On failure, free all allocated memory and set list pointer to NULL. */
if (ret < 0)
{
for (i = 0; i < img_info_size && img_info_list[i].name; ++i)
free (img_info_list[i].name);
free (img_info_list);
img_info_list = NULL;
img_info_size = 0;
img_info_max_size = 0;
}
return ret;
}
static BOOL
set_cannot_rebase (img_info_t *img)
{
/* While --oblivious is active, cannot_rebase is set to 2 on loading
* the database entries */
if (img->flag.cannot_rebase <= 1 )
{
int fd = open (img->name, O_WRONLY);
if (fd < 0)
img->flag.cannot_rebase = 1;
else
close (fd);
}
return img->flag.cannot_rebase;
}
int
merge_image_info ()
{
int i, end;
img_info_t *match;
ULONG64 floating_image_base;
/* Sort new files from command line by name. */
qsort (img_info_list + img_info_rebase_start,
img_info_size - img_info_rebase_start, sizeof (img_info_t),
img_info_name_cmp);
/* Iterate through new files and eliminate duplicates. */
for (i = img_info_rebase_start; i + 1 < img_info_size; ++i)
if ((img_info_list[i].name_size == img_info_list[i + 1].name_size
&& !strcmp (img_info_list[i].name, img_info_list[i + 1].name))
#if defined(__CYGWIN__) || defined(__MSYS__)
|| !strcmp (img_info_list[i].name, CYGWIN_DLL)
#endif
)
{
free (img_info_list[i].name);
memmove (img_info_list + i, img_info_list + i + 1,
(img_info_size - i - 1) * sizeof (img_info_t));
--img_info_size;
--i;
}
/* Iterate through new files and see if they are already available in
existing database. */
if (img_info_rebase_start)
{
for (i = img_info_rebase_start; i < img_info_size; ++i)
{
/* First test if we can open the DLL for writing. If not, it's
probably blocked by another process. */
set_cannot_rebase (&img_info_list[i]);
match = bsearch (&img_info_list[i], img_info_list,
img_info_rebase_start, sizeof (img_info_t),
img_info_name_cmp);
if (match)
{
/* We found a match. Now test if the "new" file is actually
the old file, or if it at least fits into the memory slot
of the old file. If so, screw the new file into the old slot.
Otherwise set base to 0 to indicate that this DLL needs a new
base address. */
if (img_info_list[i].flag.cannot_rebase)
match->base = img_info_list[i].base;
else if (match->base != img_info_list[i].base
|| match->slot_size < img_info_list[i].slot_size)
{
/* Reuse the old address if possible. */
if (match->slot_size < img_info_list[i].slot_size)
{
match->base = 0;
if (verbose)
fprintf (stderr, "rebasing %s because it won't fit in it's old slot size\n", img_info_list[i].name);
}
else if (verbose)
fprintf (stderr, "rebasing %s because it's not located at it's old slot\n", img_info_list[i].name);
match->flag.needs_rebasing = 1;
}
/* Unconditionally overwrite old with new size. */
match->size = img_info_list[i].size;
match->slot_size = img_info_list[i].slot_size;
/* With an --oblivious active, the files should not
* already be in the database. Warn since the file will
* not be touched. */
if (image_oblivious_flag)
fprintf (stderr, "%s: oblivious file \"%s\" already "
"found in rebase database "
"(file and database kept unchanged).\n",
progname, img_info_list[i].name);
/* Remove new entry from array. */
free (img_info_list[i].name);
img_info_list[i--] = img_info_list[--img_info_size];
}
else if (!img_info_list[i].flag.cannot_rebase)
{
/* Not in database yet. Set base to 0 to choose a new one. */
img_info_list[i].base = 0;
if (verbose)
fprintf (stderr, "rebasing %s because not in database yet\n", img_info_list[i].name);
}
}
}
if (!img_info_rebase_start || force_rebase_flag)
{
/* No database yet or enforcing a new base address. Set base of all
DLLs to 0, if possible. */
for (i = 0; i < img_info_size; ++i)
{
/* Test DLLs already in database for writability. */
if (i < img_info_rebase_start)
set_cannot_rebase (&img_info_list[i]);
if (!img_info_list[i].flag.cannot_rebase)
{
img_info_list[i].base = 0;
if (verbose)
fprintf (stderr, "rebasing %s because forced or database missing\n", img_info_list[i].name);
}
}
img_info_rebase_start = 0;
}
/* Now sort the old part of the list by base address. */
if (img_info_rebase_start)
qsort (img_info_list, img_info_rebase_start, sizeof (img_info_t),
img_info_cmp);
/* Perform several tests on the information fetched from the database
to match with reality. */
for (i = 0; i < img_info_rebase_start; ++i)
{
ULONG64 cur_base;
ULONG cur_size, slot_size;
/* Files with the needs_rebasing or cannot_rebase flags set have been
checked already. */
if (img_info_list[i].flag.needs_rebasing
|| img_info_list[i].flag.cannot_rebase)
continue;
/* Check if the files in the old list still exist. Drop non-existant
or unaccessible files. */
if (access (img_info_list[i].name, F_OK) == -1
|| !GetImageInfos64 (img_info_list[i].name, NULL,
&cur_base, &cur_size))
{
free (img_info_list[i].name);
memmove (img_info_list + i, img_info_list + i + 1,
(img_info_size - i - 1) * sizeof (img_info_t));
--img_info_rebase_start;
--img_info_size;
continue;
}
slot_size = roundup2 (cur_size, ALLOCATION_SLOT);
if (set_cannot_rebase (&img_info_list[i]))
img_info_list[i].base = cur_base;
else
{
/* If the file has been reinstalled, try to rebase to the same address
in the first place. */
if (cur_base != img_info_list[i].base)
{
img_info_list[i].flag.needs_rebasing = 1;
if (verbose)
fprintf (stderr, "rebasing %s because it's base has changed (due to being reinstalled?)\n", img_info_list[i].name);
/* Set cur_base to the old base to simplify subsequent tests. */
cur_base = img_info_list[i].base;
}
/* However, if the DLL got bigger and doesn't fit into its slot
anymore, rebase this DLL from scratch. */
if (i + 1 < img_info_rebase_start
&& cur_base + slot_size + offset > img_info_list[i + 1].base)
{
img_info_list[i].base = 0;
if (verbose)
fprintf (stderr, "rebasing %s because it won't fit in it's old slot without overlapping next DLL\n", img_info_list[i].name);
}
/* Does the previous DLL reach into the address space of this
DLL? This happens if the previous DLL is not rebaseable. */
else if (i > 0 && cur_base < img_info_list[i - 1].base
+ img_info_list[i - 1].slot_size)
{
img_info_list[i].base = 0;
if (verbose)
fprintf (stderr, "rebasing %s because previous DLL now overlaps\n", img_info_list[i].name);
}
/* Does the file match the base address requirements? If not,
rebase from scratch. */
else if ((down_flag && cur_base + slot_size + offset > image_base)
|| (!down_flag && cur_base < image_base))
{
img_info_list[i].base = 0;
if (verbose)
fprintf (stderr, "rebasing %s because it's base address is outside the expected area\n", img_info_list[i].name);
}
}
/* Unconditionally overwrite old with new size. */
img_info_list[i].size = cur_size;
img_info_list[i].slot_size = slot_size;
/* Make sure all DLLs with base address 0 have the needs_rebasing
flag set. */
if (img_info_list[i].base == 0)
img_info_list[i].flag.needs_rebasing = 1;
}
/* The remainder of the function expects img_info_size to be > 0. */
if (img_info_size == 0)
return 0;
/* Now sort entire list by base address. The files with address 0 will
be first. */
if (!force_rebase_flag)
qsort (img_info_list, img_info_size, sizeof (img_info_t), img_info_cmp);
/* Try to fit all DLLs with base address 0 into the given list. */
/* FIXME: This loop only implements the top-down case. Implement a
bottom-up case, too, at one point. */
floating_image_base = image_base;
end = img_info_size - 1;
while (img_info_list[0].base == 0)
{
ULONG64 new_base = 0;
/* Skip trailing entries as long as there is no hole. */
while (end > 0
&& img_info_list[end].base + img_info_list[end].slot_size
+ offset >= floating_image_base)
{
floating_image_base = img_info_list[end].base;
--end;
}
/* Test if one of the DLLs with address 0 fits into the hole. */
for (i = 0; img_info_list[i].base == 0; ++i)
{
ULONG64 base = floating_image_base - img_info_list[i].slot_size
- offset;
/* Check if address is still valid */
if (check_base_address_sanity (base, FALSE))
return -1;
if (base >= img_info_list[end].base + img_info_list[end].slot_size
#if defined(__CYGWIN__) || defined(__MSYS__)
/* Don't overlap the Cygwin/MSYS DLL. */
&& (base >= cygwin_dll_image_base + cygwin_dll_image_size
|| base + img_info_list[i].slot_size <= cygwin_dll_image_base)
#endif
)
{
new_base = base;
break;
}
}
/* Found a match. Mount into list. */
if (new_base)
{
img_info_t tmp = img_info_list[i];
tmp.base = new_base;
memmove (img_info_list + i, img_info_list + i + 1,
(end - i) * sizeof (img_info_t));
img_info_list[end] = tmp;
continue;
}
/* Nothing matches. Set floating_image_base to the start of the
uppermost DLL at this point and try again. */
#if defined(__CYGWIN__) || defined(__MSYS__)
if (floating_image_base >= cygwin_dll_image_base + cygwin_dll_image_size
&& img_info_list[end].base < cygwin_dll_image_base)
floating_image_base = cygwin_dll_image_base;
else
#endif
{
floating_image_base = img_info_list[end].base;
if (--end < 0)
{
fprintf (stderr,
"%s: Too many DLLs for available address space: %s\n",
progname, strerror (ENOMEM));
return -1;
}
}
}
return 0;
}
BOOL
collect_image_info (const char *pathname)
{
BOOL ret;
WORD dll_machine;
/* Skip if file does not exist to prevent ReBaseImage() from using it's
stupid search algorithm (e.g, PATH, etc.). */
if (access (pathname, F_OK) == -1)
{
if (!quiet)
fprintf (stderr, "%s: skipped because nonexistent.\n", pathname);
return TRUE;
}
/* Skip if not rebaseable, but only if we're collecting for rebasing,
not if we're collecting for printing only. */
if (!image_info_flag && !is_rebaseable (pathname))
{
if (!quiet)
fprintf (stderr, "%s: skipped because not rebaseable\n", pathname);
return TRUE;
}
if (img_info_size >= img_info_max_size)
{
img_info_max_size += 100;
img_info_list = (img_info_t *) realloc (img_info_list,
img_info_max_size
* sizeof (img_info_t));
if (!img_info_list)
{
fprintf (stderr, "%s: Out of memory.\n", progname);
return FALSE;
}
}
ret = GetImageInfos64 (pathname, &dll_machine,
&img_info_list[img_info_size].base,
&img_info_list[img_info_size].size);
if (!ret)
{
if (!quiet)
fprintf (stderr, "%s: skipped because file info unreadable.\n",
pathname);
return TRUE;
}
/* We only support IMAGE_FILE_MACHINE_I386 and IMAGE_FILE_MACHINE_AMD64
so far. */
if (machine != IMAGE_FILE_MACHINE_I386
&& machine != IMAGE_FILE_MACHINE_AMD64)
{
if (quiet)
fprintf (stderr, "%s: is an executable for a machine type\n"
"I don't know about.", pathname);
return TRUE;
}
/* We either operate on 32 bit or 64 bit files. Never mix them. */
if (dll_machine != machine)
{
if (!quiet)
fprintf (stderr, "%s: skipped because wrong machine type.\n",
pathname);
return TRUE;
}
img_info_list[img_info_size].slot_size
= roundup2 (img_info_list[img_info_size].size, ALLOCATION_SLOT);
img_info_list[img_info_size].flag.needs_rebasing = 1;
img_info_list[img_info_size].flag.cannot_rebase = 0;
/* This back and forth from POSIX to Win32 is a way to get a full path
more thoroughly. For instance, the difference between /bin and
/usr/bin will be eliminated. */
#if defined (__MSYS__)
{
char w32_path[MAX_PATH];
char full_path[MAX_PATH];
cygwin_conv_to_full_win32_path (pathname, w32_path);
cygwin_conv_to_full_posix_path (w32_path, full_path);
img_info_list[img_info_size].name = strdup (full_path);
img_info_list[img_info_size].name_size = strlen (full_path) + 1;
}
#elif defined (__CYGWIN__)
{
PWSTR w32_path = cygwin_create_path (CCP_POSIX_TO_WIN_W, pathname);
if (!w32_path)
{
fprintf (stderr, "%s: Out of memory.\n", progname);
return FALSE;
}
img_info_list[img_info_size].name
= cygwin_create_path (CCP_WIN_W_TO_POSIX, w32_path);
if (!img_info_list[img_info_size].name)
{
fprintf (stderr, "%s: Out of memory.\n", progname);
return FALSE;
}
free (w32_path);
img_info_list[img_info_size].name_size
= strlen (img_info_list[img_info_size].name) + 1;
}
#else
{
char full_path[MAX_PATH];
GetFullPathName (pathname, MAX_PATH, full_path, NULL);
img_info_list[img_info_size].name = strdup (full_path);
img_info_list[img_info_size].name_size = strlen (full_path) + 1;
}
#endif
if (verbose)
fprintf (stderr, "rebasing %s because filename given on command line\n", img_info_list[img_info_size].name);
++img_info_size;
return TRUE;
}
void
print_image_info ()
{
unsigned int i;
/* Default name field width to longest available for 80 char display. */
int name_width = (machine == IMAGE_FILE_MACHINE_I386) ? 45 : 41;
/* Sort list by name. */
qsort (img_info_list, img_info_size, sizeof (img_info_t), img_info_name_cmp);
/* Iterate through list and eliminate duplicates. */
for (i = 0; i + 1 < img_info_size; ++i)
if (img_info_list[i].name_size == img_info_list[i + 1].name_size
&& !strcmp (img_info_list[i].name, img_info_list[i + 1].name))
{
/* Remove duplicate, but prefer one from the command line over one
from the database, because the one from the command line reflects
the reality, while the database is wishful thinking. */
if (img_info_list[i].flag.needs_rebasing == 0)
{
free (img_info_list[i].name);
memmove (img_info_list + i, img_info_list + i + 1,
(img_info_size - i - 1) * sizeof (img_info_t));
}