forked from bsdjhb/gdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpe-dll.c
3636 lines (3087 loc) · 97.5 KB
/
pe-dll.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
/* Routines to help build PEI-format DLLs (Win32 etc)
Copyright (C) 1998-2019 Free Software Foundation, Inc.
Written by DJ Delorie <dj@cygnus.com>
This file is part of the GNU Binutils.
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 3 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, write to the Free Software
Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
MA 02110-1301, USA. */
#include "sysdep.h"
#include "bfd.h"
#include "bfdlink.h"
#include "libiberty.h"
#include "filenames.h"
#include "safe-ctype.h"
#include "ctf-api.h"
#include <time.h>
#include "ld.h"
#include "ldexp.h"
#include "ldlang.h"
#include "ldwrite.h"
#include "ldmisc.h"
#include <ldgram.h>
#include "ldmain.h"
#include "ldfile.h"
#include "ldemul.h"
#include "coff/internal.h"
#include "../bfd/libcoff.h"
#include "deffile.h"
#ifdef pe_use_x86_64
#define PE_IDATA4_SIZE 8
#define PE_IDATA5_SIZE 8
#include "pep-dll.h"
#undef AOUTSZ
#define AOUTSZ PEPAOUTSZ
#define PEAOUTHDR PEPAOUTHDR
#else
#include "pe-dll.h"
#endif
#ifndef PE_IDATA4_SIZE
#define PE_IDATA4_SIZE 4
#endif
#ifndef PE_IDATA5_SIZE
#define PE_IDATA5_SIZE 4
#endif
/* This file turns a regular Windows PE image into a DLL. Because of
the complexity of this operation, it has been broken down into a
number of separate modules which are all called by the main function
at the end of this file. This function is not re-entrant and is
normally only called once, so static variables are used to reduce
the number of parameters and return values required.
See also: ld/emultempl/pe.em and ld/emultempl/pep.em. */
/* Auto-import feature by Paul Sokolovsky
Quick facts:
1. With this feature on, DLL clients can import variables from DLL
without any concern from their side (for example, without any source
code modifications).
2. This is done completely in bounds of the PE specification (to be fair,
there's a place where it pokes nose out of, but in practice it works).
So, resulting module can be used with any other PE compiler/linker.
3. Auto-import is fully compatible with standard import method and they
can be mixed together.
4. Overheads: space: 8 bytes per imported symbol, plus 20 for each
reference to it; load time: negligible; virtual/physical memory: should be
less than effect of DLL relocation, and I sincerely hope it doesn't affect
DLL sharability (too much).
Idea
The obvious and only way to get rid of dllimport insanity is to make client
access variable directly in the DLL, bypassing extra dereference. I.e.,
whenever client contains something like
mov dll_var,%eax,
address of dll_var in the command should be relocated to point into loaded
DLL. The aim is to make OS loader do so, and than make ld help with that.
Import section of PE made following way: there's a vector of structures
each describing imports from particular DLL. Each such structure points
to two other parallel vectors: one holding imported names, and one which
will hold address of corresponding imported name. So, the solution is
de-vectorize these structures, making import locations be sparse and
pointing directly into code. Before continuing, it is worth a note that,
while authors strives to make PE act ELF-like, there're some other people
make ELF act PE-like: elfvector, ;-) .
Implementation
For each reference of data symbol to be imported from DLL (to set of which
belong symbols with name <sym>, if __imp_<sym> is found in implib), the
import fixup entry is generated. That entry is of type
IMAGE_IMPORT_DESCRIPTOR and stored in .idata$2 subsection. Each
fixup entry contains pointer to symbol's address within .text section
(marked with __fuN_<sym> symbol, where N is integer), pointer to DLL name
(so, DLL name is referenced by multiple entries), and pointer to symbol
name thunk. Symbol name thunk is singleton vector (__nm_th_<symbol>)
pointing to IMAGE_IMPORT_BY_NAME structure (__nm_<symbol>) directly
containing imported name. Here comes that "on the edge" problem mentioned
above: PE specification rambles that name vector (OriginalFirstThunk)
should run in parallel with addresses vector (FirstThunk), i.e. that they
should have same number of elements and terminated with zero. We violate
this, since FirstThunk points directly into machine code. But in practice,
OS loader implemented the sane way: it goes through OriginalFirstThunk and
puts addresses to FirstThunk, not something else. It once again should be
noted that dll and symbol name structures are reused across fixup entries
and should be there anyway to support standard import stuff, so sustained
overhead is 20 bytes per reference. Other question is whether having several
IMAGE_IMPORT_DESCRIPTORS for the same DLL is possible. Answer is yes, it is
done even by native compiler/linker (libth32's functions are in fact reside
in windows9x kernel32.dll, so if you use it, you have two
IMAGE_IMPORT_DESCRIPTORS for kernel32.dll). Yet other question is whether
referencing the same PE structures several times is valid. The answer is why
not, prohibiting that (detecting violation) would require more work on
behalf of loader than not doing it.
See also: ld/emultempl/pe.em and ld/emultempl/pep.em. */
static void add_bfd_to_link (bfd *, const char *, struct bfd_link_info *);
/* For emultempl/pe.em. */
def_file * pe_def_file = 0;
int pe_dll_export_everything = 0;
int pe_dll_exclude_all_symbols = 0;
int pe_dll_do_default_excludes = 1;
int pe_dll_kill_ats = 0;
int pe_dll_stdcall_aliases = 0;
int pe_dll_warn_dup_exports = 0;
int pe_dll_compat_implib = 0;
int pe_dll_extra_pe_debug = 0;
int pe_use_nul_prefixed_import_tables = 0;
int pe_use_coff_long_section_names = -1;
int pe_leading_underscore = -1;
/* Static variables and types. */
static bfd_vma image_base;
static bfd *filler_bfd;
static struct bfd_section *edata_s, *reloc_s;
static unsigned char *edata_d, *reloc_d;
static size_t edata_sz, reloc_sz;
static int runtime_pseudo_relocs_created = 0;
static bfd_boolean runtime_pseudp_reloc_v2_init = FALSE;
typedef struct
{
const char *name;
int len;
}
autofilter_entry_type;
typedef struct
{
const char *target_name;
const char *object_target;
unsigned int imagebase_reloc;
int pe_arch;
int bfd_arch;
bfd_boolean underscored;
const autofilter_entry_type* autofilter_symbollist;
}
pe_details_type;
static const autofilter_entry_type autofilter_symbollist_generic[] =
{
{ STRING_COMMA_LEN ("_NULL_IMPORT_DESCRIPTOR") },
/* Entry point symbols. */
{ STRING_COMMA_LEN ("DllMain") },
{ STRING_COMMA_LEN ("DllMainCRTStartup") },
{ STRING_COMMA_LEN ("_DllMainCRTStartup") },
/* Runtime pseudo-reloc. */
{ STRING_COMMA_LEN ("_pei386_runtime_relocator") },
{ STRING_COMMA_LEN ("do_pseudo_reloc") },
{ NULL, 0 }
};
static const autofilter_entry_type autofilter_symbollist_i386[] =
{
{ STRING_COMMA_LEN ("_NULL_IMPORT_DESCRIPTOR") },
/* Entry point symbols, and entry hooks. */
{ STRING_COMMA_LEN ("cygwin_crt0") },
#ifdef pe_use_x86_64
{ STRING_COMMA_LEN ("DllMain") },
{ STRING_COMMA_LEN ("DllEntryPoint") },
{ STRING_COMMA_LEN ("DllMainCRTStartup") },
{ STRING_COMMA_LEN ("_cygwin_dll_entry") },
{ STRING_COMMA_LEN ("_cygwin_crt0_common") },
{ STRING_COMMA_LEN ("_cygwin_noncygwin_dll_entry") },
#else
{ STRING_COMMA_LEN ("DllMain@12") },
{ STRING_COMMA_LEN ("DllEntryPoint@0") },
{ STRING_COMMA_LEN ("DllMainCRTStartup@12") },
{ STRING_COMMA_LEN ("_cygwin_dll_entry@12") },
{ STRING_COMMA_LEN ("_cygwin_crt0_common@8") },
{ STRING_COMMA_LEN ("_cygwin_noncygwin_dll_entry@12") },
{ STRING_COMMA_LEN ("cygwin_attach_dll") },
#endif
{ STRING_COMMA_LEN ("cygwin_premain0") },
{ STRING_COMMA_LEN ("cygwin_premain1") },
{ STRING_COMMA_LEN ("cygwin_premain2") },
{ STRING_COMMA_LEN ("cygwin_premain3") },
/* Runtime pseudo-reloc. */
{ STRING_COMMA_LEN ("_pei386_runtime_relocator") },
{ STRING_COMMA_LEN ("do_pseudo_reloc") },
/* Global vars that should not be exported. */
{ STRING_COMMA_LEN ("impure_ptr") },
{ STRING_COMMA_LEN ("_impure_ptr") },
{ STRING_COMMA_LEN ("_fmode") },
{ STRING_COMMA_LEN ("environ") },
{ STRING_COMMA_LEN ("__dso_handle") },
{ NULL, 0 }
};
#define PE_ARCH_i386 1
#define PE_ARCH_sh 2
#define PE_ARCH_mips 3
#define PE_ARCH_arm 4
#define PE_ARCH_arm_wince 5
/* Don't make it constant as underscore mode gets possibly overriden
by target or -(no-)leading-underscore option. */
static pe_details_type pe_detail_list[] =
{
{
#ifdef pe_use_x86_64
"pei-x86-64",
"pe-x86-64",
3 /* R_IMAGEBASE */,
#else
"pei-i386",
"pe-i386",
7 /* R_IMAGEBASE */,
#endif
PE_ARCH_i386,
bfd_arch_i386,
#ifdef pe_use_x86_64
FALSE,
#else
TRUE,
#endif
autofilter_symbollist_i386
},
#ifdef pe_use_x86_64
{
"pei-x86-64",
"pe-bigobj-x86-64",
3 /* R_IMAGEBASE */,
PE_ARCH_i386,
bfd_arch_i386,
FALSE,
autofilter_symbollist_i386
},
#endif
{
"pei-shl",
"pe-shl",
16 /* R_SH_IMAGEBASE */,
PE_ARCH_sh,
bfd_arch_sh,
TRUE,
autofilter_symbollist_generic
},
{
"pei-mips",
"pe-mips",
34 /* MIPS_R_RVA */,
PE_ARCH_mips,
bfd_arch_mips,
FALSE,
autofilter_symbollist_generic
},
{
"pei-arm-little",
"pe-arm-little",
11 /* ARM_RVA32 */,
PE_ARCH_arm,
bfd_arch_arm,
TRUE,
autofilter_symbollist_generic
},
{
"pei-arm-wince-little",
"pe-arm-wince-little",
2, /* ARM_RVA32 on Windows CE, see bfd/coff-arm.c. */
PE_ARCH_arm_wince,
bfd_arch_arm,
FALSE,
autofilter_symbollist_generic
},
{ NULL, NULL, 0, 0, 0, FALSE, NULL }
};
static const pe_details_type *pe_details;
/* Do not specify library suffix explicitly, to allow for dllized versions. */
static const autofilter_entry_type autofilter_liblist[] =
{
{ STRING_COMMA_LEN ("libcegcc") },
{ STRING_COMMA_LEN ("libcygwin") },
{ STRING_COMMA_LEN ("libgcc") },
{ STRING_COMMA_LEN ("libgcc_s") },
{ STRING_COMMA_LEN ("libstdc++") },
{ STRING_COMMA_LEN ("libmingw32") },
{ STRING_COMMA_LEN ("libmingwex") },
{ STRING_COMMA_LEN ("libg2c") },
{ STRING_COMMA_LEN ("libsupc++") },
{ STRING_COMMA_LEN ("libobjc") },
{ STRING_COMMA_LEN ("libgcj") },
{ STRING_COMMA_LEN ("libmsvcrt") },
{ STRING_COMMA_LEN ("libmsvcrt-os") },
{ STRING_COMMA_LEN ("libucrtbase") },
{ NULL, 0 }
};
/* Regardless of the suffix issue mentioned above, we must ensure that
we do not falsely match on a leading substring, such as when libtool
builds libstdc++ as a DLL using libsupc++convenience.a as an intermediate.
This routine ensures that the leading part of the name matches and that
it is followed by only an optional version suffix and a file extension,
returning zero if so or -1 if not. */
static int libnamencmp (const char *libname, const autofilter_entry_type *afptr)
{
if (filename_ncmp (libname, afptr->name, afptr->len))
return -1;
libname += afptr->len;
/* Be liberal in interpreting what counts as a version suffix; we
accept anything that has a dash to separate it from the name and
begins with a digit. */
if (libname[0] == '-')
{
if (!ISDIGIT (*++libname))
return -1;
/* Ensure the filename has an extension. */
while (*++libname != '.')
if (!*libname)
return -1;
}
else if (libname[0] != '.')
return -1;
return 0;
}
static const autofilter_entry_type autofilter_objlist[] =
{
{ STRING_COMMA_LEN ("crt0.o") },
{ STRING_COMMA_LEN ("crt1.o") },
{ STRING_COMMA_LEN ("crt2.o") },
{ STRING_COMMA_LEN ("dllcrt1.o") },
{ STRING_COMMA_LEN ("dllcrt2.o") },
{ STRING_COMMA_LEN ("gcrt0.o") },
{ STRING_COMMA_LEN ("gcrt1.o") },
{ STRING_COMMA_LEN ("gcrt2.o") },
{ STRING_COMMA_LEN ("crtbegin.o") },
{ STRING_COMMA_LEN ("crtend.o") },
{ NULL, 0 }
};
static const autofilter_entry_type autofilter_symbolprefixlist[] =
{
/* _imp_ is treated specially, as it is always underscored. */
/* { STRING_COMMA_LEN ("_imp_") }, */
/* Don't export some c++ symbols. */
{ STRING_COMMA_LEN ("__rtti_") },
{ STRING_COMMA_LEN ("__builtin_") },
/* Don't re-export auto-imported symbols. */
{ STRING_COMMA_LEN ("__nm_") },
/* Don't export symbols specifying internal DLL layout. */
{ STRING_COMMA_LEN ("_head_") },
{ STRING_COMMA_LEN ("_IMPORT_DESCRIPTOR_") },
/* Don't export section labels or artificial symbols
(eg ".weak.foo". */
{ STRING_COMMA_LEN (".") },
{ NULL, 0 }
};
static const autofilter_entry_type autofilter_symbolsuffixlist[] =
{
{ STRING_COMMA_LEN ("_iname") },
{ STRING_COMMA_LEN ("_NULL_THUNK_DATA") },
{ NULL, 0 }
};
#define U(str) (pe_details->underscored ? "_" str : str)
void
pe_dll_id_target (const char *target)
{
int i;
for (i = 0; pe_detail_list[i].target_name; i++)
if (strcmp (pe_detail_list[i].target_name, target) == 0
|| strcmp (pe_detail_list[i].object_target, target) == 0)
{
int u = pe_leading_underscore; /* Underscoring mode. -1 for use default. */
if (u == -1)
bfd_get_target_info (target, NULL, NULL, &u, NULL);
if (u == -1)
abort ();
pe_detail_list[i].underscored = (u != 0 ? TRUE : FALSE);
pe_details = pe_detail_list + i;
pe_leading_underscore = (u != 0 ? 1 : 0);
return;
}
einfo (_("%X%P: unsupported PEI architecture: %s\n"), target);
exit (1);
}
/* Helper functions for qsort. Relocs must be sorted so that we can write
them out by pages. */
typedef struct
{
bfd_vma vma;
char type;
short extra;
int idx;
}
reloc_data_type;
static int
reloc_sort (const void *va, const void *vb)
{
const reloc_data_type *a = (const reloc_data_type *) va;
const reloc_data_type *b = (const reloc_data_type *) vb;
if (a->vma > b->vma)
return 1;
if (a->vma < b->vma)
return -1;
if (a->idx > b->idx)
return 1;
if (a->idx < b->idx)
return -1;
return 0;
}
static int
pe_export_sort (const void *va, const void *vb)
{
const def_file_export *a = va;
const def_file_export *b = vb;
char *an = a->name;
char *bn = b->name;
if (a->its_name)
an = a->its_name;
if (b->its_name)
bn = b->its_name;
return strcmp (an, bn);
}
/* Read and process the .DEF file. */
/* These correspond to the entries in pe_def_file->exports[]. I use
exported_symbol_sections[i] to tag whether or not the symbol was
defined, since we can't export symbols we don't have. */
static bfd_vma *exported_symbol_offsets;
static struct bfd_section **exported_symbol_sections;
static int export_table_size;
static int count_exported;
static int count_exported_byname;
static int count_with_ordinals;
static const char *dll_name;
static int min_ordinal, max_ordinal;
static int *exported_symbols;
typedef struct exclude_list_struct
{
char *string;
struct exclude_list_struct *next;
exclude_type type;
}
exclude_list_struct;
static struct exclude_list_struct *excludes = 0;
void
pe_dll_add_excludes (const char *new_excludes, const exclude_type type)
{
char *local_copy;
char *exclude_string;
local_copy = xstrdup (new_excludes);
exclude_string = strtok (local_copy, ",:");
for (; exclude_string; exclude_string = strtok (NULL, ",:"))
{
struct exclude_list_struct *new_exclude;
new_exclude = xmalloc (sizeof (struct exclude_list_struct));
new_exclude->string = xmalloc (strlen (exclude_string) + 1);
strcpy (new_exclude->string, exclude_string);
new_exclude->type = type;
new_exclude->next = excludes;
excludes = new_exclude;
}
free (local_copy);
}
static bfd_boolean
is_import (const char* n)
{
return (CONST_STRNEQ (n, "__imp_"));
}
/* abfd is a bfd containing n (or NULL)
It can be used for contextual checks. */
static int
auto_export (bfd *abfd, def_file *d, const char *n)
{
def_file_export key;
struct exclude_list_struct *ex;
const autofilter_entry_type *afptr;
const char * libname = NULL;
if (abfd && abfd->my_archive)
libname = lbasename (abfd->my_archive->filename);
key.name = key.its_name = (char *) n;
/* Return false if n is in the d->exports table. */
if (bsearch (&key, d->exports, d->num_exports,
sizeof (pe_def_file->exports[0]), pe_export_sort))
return 0;
if (pe_dll_do_default_excludes)
{
const char * p;
int len;
if (pe_dll_extra_pe_debug)
printf ("considering exporting: %s, abfd=%p, abfd->my_arc=%p\n",
n, abfd, abfd->my_archive);
/* First of all, make context checks:
Don't export anything from standard libs. */
if (libname)
{
afptr = autofilter_liblist;
while (afptr->name)
{
if (libnamencmp (libname, afptr) == 0 )
return 0;
afptr++;
}
}
/* Next, exclude symbols from certain startup objects. */
if (abfd && (p = lbasename (abfd->filename)))
{
afptr = autofilter_objlist;
while (afptr->name)
{
if (strcmp (p, afptr->name) == 0)
return 0;
afptr++;
}
}
/* Don't try to blindly exclude all symbols
that begin with '__'; this was tried and
it is too restrictive. Instead we have
a target specific list to use: */
afptr = pe_details->autofilter_symbollist;
while (afptr->name)
{
if (strcmp (n, afptr->name) == 0)
return 0;
afptr++;
}
/* Next, exclude symbols starting with ... */
afptr = autofilter_symbolprefixlist;
while (afptr->name)
{
if (strncmp (n, afptr->name, afptr->len) == 0)
return 0;
afptr++;
}
/* Finally, exclude symbols ending with ... */
len = strlen (n);
afptr = autofilter_symbolsuffixlist;
while (afptr->name)
{
if ((len >= afptr->len)
/* Add 1 to insure match with trailing '\0'. */
&& strncmp (n + len - afptr->len, afptr->name,
afptr->len + 1) == 0)
return 0;
afptr++;
}
}
for (ex = excludes; ex; ex = ex->next)
{
if (ex->type == EXCLUDELIBS)
{
if (libname
&& ((filename_cmp (libname, ex->string) == 0)
|| (strcasecmp ("ALL", ex->string) == 0)))
return 0;
}
else if (ex->type == EXCLUDEFORIMPLIB)
{
if (filename_cmp (abfd->filename, ex->string) == 0)
return 0;
}
else if (strcmp (n, ex->string) == 0)
return 0;
}
return 1;
}
static void
process_def_file_and_drectve (bfd *abfd ATTRIBUTE_UNUSED, struct bfd_link_info *info)
{
int i, j;
struct bfd_link_hash_entry *blhe;
bfd *b;
struct bfd_section *s;
def_file_export *e = 0;
bfd_boolean resort_needed;
if (!pe_def_file)
pe_def_file = def_file_empty ();
/* First, run around to all the objects looking for the .drectve
sections, and push those into the def file too. */
for (b = info->input_bfds; b; b = b->link.next)
{
s = bfd_get_section_by_name (b, ".drectve");
if (s)
{
long size = s->size;
char *buf = xmalloc (size);
bfd_get_section_contents (b, s, buf, 0, size);
def_file_add_directive (pe_def_file, buf, size);
free (buf);
}
}
/* Process aligned common symbol information from the
.drectve sections now; common symbol allocation is
done before final link, so it will be too late to
process them in process_embedded_commands() called
from _bfd_coff_link_input_bfd(). */
if (pe_def_file->aligncomms)
{
def_file_aligncomm *ac = pe_def_file->aligncomms;
while (ac)
{
struct coff_link_hash_entry *sym_hash;
sym_hash = coff_link_hash_lookup (coff_hash_table (info),
ac->symbol_name, FALSE, FALSE, FALSE);
if (sym_hash && sym_hash->root.type == bfd_link_hash_common
&& sym_hash->root.u.c.p->alignment_power < (unsigned) ac->alignment)
{
sym_hash->root.u.c.p->alignment_power = (unsigned) ac->alignment;
}
ac = ac->next;
}
}
/* If we are building an executable and there is nothing
to export, we do not build an export table at all. */
if (bfd_link_executable (info) && pe_def_file->num_exports == 0
&& (!pe_dll_export_everything || pe_dll_exclude_all_symbols))
return;
/* Now, maybe export everything else the default way. */
if ((pe_dll_export_everything || pe_def_file->num_exports == 0)
&& !pe_dll_exclude_all_symbols)
{
for (b = info->input_bfds; b; b = b->link.next)
{
asymbol **symbols;
int nsyms;
if (!bfd_generic_link_read_symbols (b))
{
einfo (_("%F%P: %pB: could not read symbols: %E\n"), b);
return;
}
symbols = bfd_get_outsymbols (b);
nsyms = bfd_get_symcount (b);
for (j = 0; j < nsyms; j++)
{
/* We should export symbols which are either global or not
anything at all. (.bss data is the latter)
We should not export undefined symbols. */
bfd_boolean would_export
= (symbols[j]->section != bfd_und_section_ptr
&& ((symbols[j]->flags & BSF_GLOBAL)
|| (symbols[j]->flags == 0)));
if (link_info.version_info && would_export)
would_export
= !bfd_hide_sym_by_version (link_info.version_info,
symbols[j]->name);
if (would_export)
{
const char *sn = symbols[j]->name;
/* We should not re-export imported stuff. */
{
char *name;
if (is_import (sn))
continue;
name = xmalloc (strlen ("__imp_") + strlen (sn) + 1);
sprintf (name, "%s%s", "__imp_", sn);
blhe = bfd_link_hash_lookup (info->hash, name,
FALSE, FALSE, FALSE);
free (name);
if (blhe && blhe->type == bfd_link_hash_defined)
continue;
}
if (pe_details->underscored && *sn == '_')
sn++;
if (auto_export (b, pe_def_file, sn))
{
int is_dup = 0;
def_file_export *p;
p = def_file_add_export (pe_def_file, sn, 0, -1,
NULL, &is_dup);
/* Fill data flag properly, from dlltool.c. */
if (!is_dup)
p->flag_data = !(symbols[j]->flags & BSF_FUNCTION);
}
}
}
}
}
#undef NE
#define NE pe_def_file->num_exports
/* Don't create an empty export table. */
if (NE == 0)
return;
resort_needed = FALSE;
/* Canonicalize the export list. */
if (pe_dll_kill_ats)
{
for (i = 0; i < NE; i++)
{
/* Check for fastcall/stdcall-decoration, but ignore
C++ mangled names. */
if (pe_def_file->exports[i].name[0] != '?'
&& strchr (pe_def_file->exports[i].name, '@'))
{
/* This will preserve internal_name, which may have been
pointing to the same memory as name, or might not
have. */
int lead_at = (*pe_def_file->exports[i].name == '@');
char *tmp = xstrdup (pe_def_file->exports[i].name + lead_at);
char *tmp_at = strrchr (tmp, '@');
if (tmp_at)
*tmp_at = 0;
else
einfo (_("%X%P: cannot export %s: invalid export name\n"),
pe_def_file->exports[i].name);
pe_def_file->exports[i].name = tmp;
resort_needed = TRUE;
}
}
}
/* Re-sort the exports table as we have possibly changed the order
by removing leading @. */
if (resort_needed)
qsort (pe_def_file->exports, NE, sizeof (pe_def_file->exports[0]),
pe_export_sort);
if (pe_dll_stdcall_aliases)
{
for (i = 0; i < NE; i++)
{
if (is_import (pe_def_file->exports[i].name))
continue;
if (strchr (pe_def_file->exports[i].name, '@'))
{
int is_dup = 1;
int lead_at = (*pe_def_file->exports[i].name == '@');
char *tmp = xstrdup (pe_def_file->exports[i].name + lead_at);
*(strchr (tmp, '@')) = 0;
if (auto_export (NULL, pe_def_file, tmp))
def_file_add_export (pe_def_file, tmp,
pe_def_file->exports[i].internal_name,
-1, NULL, &is_dup);
if (is_dup)
free (tmp);
}
}
}
/* Convenience, but watch out for it changing. */
e = pe_def_file->exports;
for (i = 0, j = 0; i < NE; i++)
{
if (i > 0 && strcmp (e[i].name, e[i - 1].name) == 0)
{
/* This is a duplicate. */
if (e[j - 1].ordinal != -1
&& e[i].ordinal != -1
&& e[j - 1].ordinal != e[i].ordinal)
{
if (pe_dll_warn_dup_exports)
/* xgettext:c-format */
einfo (_("%X%P: error, duplicate EXPORT with ordinals: %s (%d vs %d)\n"),
e[j - 1].name, e[j - 1].ordinal, e[i].ordinal);
}
else
{
if (pe_dll_warn_dup_exports)
/* xgettext:c-format */
einfo (_("%P: warning, duplicate EXPORT: %s\n"),
e[j - 1].name);
}
if (e[i].ordinal != -1)
e[j - 1].ordinal = e[i].ordinal;
e[j - 1].flag_private |= e[i].flag_private;
e[j - 1].flag_constant |= e[i].flag_constant;
e[j - 1].flag_noname |= e[i].flag_noname;
e[j - 1].flag_data |= e[i].flag_data;
if (e[i].name)
free (e[i].name);
if (e[i].internal_name)
free (e[i].internal_name);
if (e[i].its_name)
free (e[i].its_name);
}
else
{
if (i != j)
e[j] = e[i];
j++;
}
}
pe_def_file->num_exports = j; /* == NE */
exported_symbol_offsets = xmalloc (NE * sizeof (bfd_vma));
exported_symbol_sections = xmalloc (NE * sizeof (struct bfd_section *));
memset (exported_symbol_sections, 0, NE * sizeof (struct bfd_section *));
max_ordinal = 0;
min_ordinal = 65536;
count_exported = 0;
count_exported_byname = 0;
count_with_ordinals = 0;
for (i = 0; i < NE; i++)
{
char *int_name = pe_def_file->exports[i].internal_name;
char *name;
/* PR 19803: Make sure that any exported symbol does not get garbage collected. */
lang_add_gc_name (int_name);
name = xmalloc (strlen (int_name) + 2);
if (pe_details->underscored && int_name[0] != '@')
{
*name = '_';
strcpy (name + 1, int_name);
/* PR 19803: The alias must be preserved as well. */
lang_add_gc_name (xstrdup (name));
}
else
strcpy (name, int_name);
blhe = bfd_link_hash_lookup (info->hash,
name,
FALSE, FALSE, TRUE);
if (blhe
&& (blhe->type == bfd_link_hash_defined
|| (blhe->type == bfd_link_hash_common)))
{
count_exported++;
if (!pe_def_file->exports[i].flag_noname)
count_exported_byname++;
/* Only fill in the sections. The actual offsets are computed
in fill_exported_offsets() after common symbols are laid
out. */
if (blhe->type == bfd_link_hash_defined)
exported_symbol_sections[i] = blhe->u.def.section;
else
exported_symbol_sections[i] = blhe->u.c.p->section;
if (pe_def_file->exports[i].ordinal != -1)
{
if (max_ordinal < pe_def_file->exports[i].ordinal)
max_ordinal = pe_def_file->exports[i].ordinal;
if (min_ordinal > pe_def_file->exports[i].ordinal)
min_ordinal = pe_def_file->exports[i].ordinal;
count_with_ordinals++;
}
}
/* Check for forward exports. These are indicated in DEF files by an
export directive of the form NAME1 = MODULE-NAME.EXTERNAL-NAME
but we must take care not to be fooled when the user wants to export
a symbol that actually really has a dot in it, so we only check
for them here, after real defined symbols have already been matched. */
else if (strchr (int_name, '.'))
{
count_exported++;
if (!pe_def_file->exports[i].flag_noname)
count_exported_byname++;
pe_def_file->exports[i].flag_forward = 1;
if (pe_def_file->exports[i].ordinal != -1)
{
if (max_ordinal < pe_def_file->exports[i].ordinal)
max_ordinal = pe_def_file->exports[i].ordinal;
if (min_ordinal > pe_def_file->exports[i].ordinal)
min_ordinal = pe_def_file->exports[i].ordinal;
count_with_ordinals++;
}
}
else if (blhe && blhe->type == bfd_link_hash_undefined)
{
/* xgettext:c-format */
einfo (_("%X%P: cannot export %s: symbol not defined\n"),
int_name);
}
else if (blhe)
{
/* xgettext:c-format */
einfo (_("%X%P: cannot export %s: symbol wrong type (%d vs %d)\n"),
int_name,
blhe->type, bfd_link_hash_defined);
}
else
{
/* xgettext:c-format */
einfo (_("%X%P: cannot export %s: symbol not found\n"),
int_name);
}
free (name);
}