/
cofflink.c
3145 lines (2761 loc) · 91.2 KB
/
cofflink.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
/* COFF specific linker code.
Copyright (C) 1994-2016 Free Software Foundation, Inc.
Written by Ian Lance Taylor, Cygnus Support.
This file is part of BFD, the Binary File Descriptor library.
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. */
/* This file contains the COFF backend linker code. */
#include "sysdep.h"
#include "bfd.h"
#include "bfdlink.h"
#include "libbfd.h"
#include "coff/internal.h"
#include "libcoff.h"
#include "safe-ctype.h"
static bfd_boolean coff_link_add_object_symbols (bfd *, struct bfd_link_info *);
static bfd_boolean coff_link_check_archive_element
(bfd *, struct bfd_link_info *, struct bfd_link_hash_entry *, const char *,
bfd_boolean *);
static bfd_boolean coff_link_add_symbols (bfd *, struct bfd_link_info *);
/* Return TRUE if SYM is a weak, external symbol. */
#define IS_WEAK_EXTERNAL(abfd, sym) \
((sym).n_sclass == C_WEAKEXT \
|| (obj_pe (abfd) && (sym).n_sclass == C_NT_WEAK))
/* Return TRUE if SYM is an external symbol. */
#define IS_EXTERNAL(abfd, sym) \
((sym).n_sclass == C_EXT || IS_WEAK_EXTERNAL (abfd, sym))
/* Define macros so that the ISFCN, et. al., macros work correctly.
These macros are defined in include/coff/internal.h in terms of
N_TMASK, etc. These definitions require a user to define local
variables with the appropriate names, and with values from the
coff_data (abfd) structure. */
#define N_TMASK n_tmask
#define N_BTSHFT n_btshft
#define N_BTMASK n_btmask
/* Create an entry in a COFF linker hash table. */
struct bfd_hash_entry *
_bfd_coff_link_hash_newfunc (struct bfd_hash_entry *entry,
struct bfd_hash_table *table,
const char *string)
{
struct coff_link_hash_entry *ret = (struct coff_link_hash_entry *) entry;
/* Allocate the structure if it has not already been allocated by a
subclass. */
if (ret == (struct coff_link_hash_entry *) NULL)
ret = ((struct coff_link_hash_entry *)
bfd_hash_allocate (table, sizeof (struct coff_link_hash_entry)));
if (ret == (struct coff_link_hash_entry *) NULL)
return (struct bfd_hash_entry *) ret;
/* Call the allocation method of the superclass. */
ret = ((struct coff_link_hash_entry *)
_bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret,
table, string));
if (ret != (struct coff_link_hash_entry *) NULL)
{
/* Set local fields. */
ret->indx = -1;
ret->type = T_NULL;
ret->symbol_class = C_NULL;
ret->numaux = 0;
ret->auxbfd = NULL;
ret->aux = NULL;
}
return (struct bfd_hash_entry *) ret;
}
/* Initialize a COFF linker hash table. */
bfd_boolean
_bfd_coff_link_hash_table_init (struct coff_link_hash_table *table,
bfd *abfd,
struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
struct bfd_hash_table *,
const char *),
unsigned int entsize)
{
memset (&table->stab_info, 0, sizeof (table->stab_info));
return _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
}
/* Create a COFF linker hash table. */
struct bfd_link_hash_table *
_bfd_coff_link_hash_table_create (bfd *abfd)
{
struct coff_link_hash_table *ret;
bfd_size_type amt = sizeof (struct coff_link_hash_table);
ret = (struct coff_link_hash_table *) bfd_malloc (amt);
if (ret == NULL)
return NULL;
if (! _bfd_coff_link_hash_table_init (ret, abfd,
_bfd_coff_link_hash_newfunc,
sizeof (struct coff_link_hash_entry)))
{
free (ret);
return (struct bfd_link_hash_table *) NULL;
}
return &ret->root;
}
/* Create an entry in a COFF debug merge hash table. */
struct bfd_hash_entry *
_bfd_coff_debug_merge_hash_newfunc (struct bfd_hash_entry *entry,
struct bfd_hash_table *table,
const char *string)
{
struct coff_debug_merge_hash_entry *ret =
(struct coff_debug_merge_hash_entry *) entry;
/* Allocate the structure if it has not already been allocated by a
subclass. */
if (ret == (struct coff_debug_merge_hash_entry *) NULL)
ret = ((struct coff_debug_merge_hash_entry *)
bfd_hash_allocate (table,
sizeof (struct coff_debug_merge_hash_entry)));
if (ret == (struct coff_debug_merge_hash_entry *) NULL)
return (struct bfd_hash_entry *) ret;
/* Call the allocation method of the superclass. */
ret = ((struct coff_debug_merge_hash_entry *)
bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string));
if (ret != (struct coff_debug_merge_hash_entry *) NULL)
{
/* Set local fields. */
ret->types = NULL;
}
return (struct bfd_hash_entry *) ret;
}
/* Given a COFF BFD, add symbols to the global hash table as
appropriate. */
bfd_boolean
_bfd_coff_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
{
switch (bfd_get_format (abfd))
{
case bfd_object:
return coff_link_add_object_symbols (abfd, info);
case bfd_archive:
return _bfd_generic_link_add_archive_symbols
(abfd, info, coff_link_check_archive_element);
default:
bfd_set_error (bfd_error_wrong_format);
return FALSE;
}
}
/* Add symbols from a COFF object file. */
static bfd_boolean
coff_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
{
if (! _bfd_coff_get_external_symbols (abfd))
return FALSE;
if (! coff_link_add_symbols (abfd, info))
return FALSE;
if (! info->keep_memory
&& ! _bfd_coff_free_symbols (abfd))
return FALSE;
return TRUE;
}
/* Check a single archive element to see if we need to include it in
the link. *PNEEDED is set according to whether this element is
needed in the link or not. This is called via
_bfd_generic_link_add_archive_symbols. */
static bfd_boolean
coff_link_check_archive_element (bfd *abfd,
struct bfd_link_info *info,
struct bfd_link_hash_entry *h,
const char *name,
bfd_boolean *pneeded)
{
*pneeded = FALSE;
/* We are only interested in symbols that are currently undefined.
If a symbol is currently known to be common, COFF linkers do not
bring in an object file which defines it. */
if (h->type != bfd_link_hash_undefined)
return TRUE;
/* Include this element? */
if (!(*info->callbacks->add_archive_element) (info, abfd, name, &abfd))
return TRUE;
*pneeded = TRUE;
return coff_link_add_object_symbols (abfd, info);
}
/* Add all the symbols from an object file to the hash table. */
static bfd_boolean
coff_link_add_symbols (bfd *abfd,
struct bfd_link_info *info)
{
unsigned int n_tmask = coff_data (abfd)->local_n_tmask;
unsigned int n_btshft = coff_data (abfd)->local_n_btshft;
unsigned int n_btmask = coff_data (abfd)->local_n_btmask;
bfd_boolean keep_syms;
bfd_boolean default_copy;
bfd_size_type symcount;
struct coff_link_hash_entry **sym_hash;
bfd_size_type symesz;
bfd_byte *esym;
bfd_byte *esym_end;
bfd_size_type amt;
symcount = obj_raw_syment_count (abfd);
if (symcount == 0)
return TRUE; /* Nothing to do. */
/* Keep the symbols during this function, in case the linker needs
to read the generic symbols in order to report an error message. */
keep_syms = obj_coff_keep_syms (abfd);
obj_coff_keep_syms (abfd) = TRUE;
if (info->keep_memory)
default_copy = FALSE;
else
default_copy = TRUE;
/* We keep a list of the linker hash table entries that correspond
to particular symbols. */
amt = symcount * sizeof (struct coff_link_hash_entry *);
sym_hash = (struct coff_link_hash_entry **) bfd_zalloc (abfd, amt);
if (sym_hash == NULL)
goto error_return;
obj_coff_sym_hashes (abfd) = sym_hash;
symesz = bfd_coff_symesz (abfd);
BFD_ASSERT (symesz == bfd_coff_auxesz (abfd));
esym = (bfd_byte *) obj_coff_external_syms (abfd);
esym_end = esym + symcount * symesz;
while (esym < esym_end)
{
struct internal_syment sym;
enum coff_symbol_classification classification;
bfd_boolean copy;
bfd_coff_swap_sym_in (abfd, esym, &sym);
classification = bfd_coff_classify_symbol (abfd, &sym);
if (classification != COFF_SYMBOL_LOCAL)
{
const char *name;
char buf[SYMNMLEN + 1];
flagword flags;
asection *section;
bfd_vma value;
bfd_boolean addit;
/* This symbol is externally visible. */
name = _bfd_coff_internal_syment_name (abfd, &sym, buf);
if (name == NULL)
goto error_return;
/* We must copy the name into memory if we got it from the
syment itself, rather than the string table. */
copy = default_copy;
if (sym._n._n_n._n_zeroes != 0
|| sym._n._n_n._n_offset == 0)
copy = TRUE;
value = sym.n_value;
switch (classification)
{
default:
abort ();
case COFF_SYMBOL_GLOBAL:
flags = BSF_EXPORT | BSF_GLOBAL;
section = coff_section_from_bfd_index (abfd, sym.n_scnum);
if (! obj_pe (abfd))
value -= section->vma;
break;
case COFF_SYMBOL_UNDEFINED:
flags = 0;
section = bfd_und_section_ptr;
break;
case COFF_SYMBOL_COMMON:
flags = BSF_GLOBAL;
section = bfd_com_section_ptr;
break;
case COFF_SYMBOL_PE_SECTION:
flags = BSF_SECTION_SYM | BSF_GLOBAL;
section = coff_section_from_bfd_index (abfd, sym.n_scnum);
break;
}
if (IS_WEAK_EXTERNAL (abfd, sym))
flags = BSF_WEAK;
addit = TRUE;
/* In the PE format, section symbols actually refer to the
start of the output section. We handle them specially
here. */
if (obj_pe (abfd) && (flags & BSF_SECTION_SYM) != 0)
{
*sym_hash = coff_link_hash_lookup (coff_hash_table (info),
name, FALSE, copy, FALSE);
if (*sym_hash != NULL)
{
if (((*sym_hash)->coff_link_hash_flags
& COFF_LINK_HASH_PE_SECTION_SYMBOL) == 0
&& (*sym_hash)->root.type != bfd_link_hash_undefined
&& (*sym_hash)->root.type != bfd_link_hash_undefweak)
(*_bfd_error_handler)
("Warning: symbol `%s' is both section and non-section",
name);
addit = FALSE;
}
}
/* The Microsoft Visual C compiler does string pooling by
hashing the constants to an internal symbol name, and
relying on the linker comdat support to discard
duplicate names. However, if one string is a literal and
one is a data initializer, one will end up in the .data
section and one will end up in the .rdata section. The
Microsoft linker will combine them into the .data
section, which seems to be wrong since it might cause the
literal to change.
As long as there are no external references to the
symbols, which there shouldn't be, we can treat the .data
and .rdata instances as separate symbols. The comdat
code in the linker will do the appropriate merging. Here
we avoid getting a multiple definition error for one of
these special symbols.
FIXME: I don't think this will work in the case where
there are two object files which use the constants as a
literal and two object files which use it as a data
initializer. One or the other of the second object files
is going to wind up with an inappropriate reference. */
if (obj_pe (abfd)
&& (classification == COFF_SYMBOL_GLOBAL
|| classification == COFF_SYMBOL_PE_SECTION)
&& coff_section_data (abfd, section) != NULL
&& coff_section_data (abfd, section)->comdat != NULL
&& CONST_STRNEQ (name, "??_")
&& strcmp (name, coff_section_data (abfd, section)->comdat->name) == 0)
{
if (*sym_hash == NULL)
*sym_hash = coff_link_hash_lookup (coff_hash_table (info),
name, FALSE, copy, FALSE);
if (*sym_hash != NULL
&& (*sym_hash)->root.type == bfd_link_hash_defined
&& coff_section_data (abfd, (*sym_hash)->root.u.def.section)->comdat != NULL
&& strcmp (coff_section_data (abfd, (*sym_hash)->root.u.def.section)->comdat->name,
coff_section_data (abfd, section)->comdat->name) == 0)
addit = FALSE;
}
if (addit)
{
if (! (bfd_coff_link_add_one_symbol
(info, abfd, name, flags, section, value,
(const char *) NULL, copy, FALSE,
(struct bfd_link_hash_entry **) sym_hash)))
goto error_return;
}
if (obj_pe (abfd) && (flags & BSF_SECTION_SYM) != 0)
(*sym_hash)->coff_link_hash_flags |=
COFF_LINK_HASH_PE_SECTION_SYMBOL;
/* Limit the alignment of a common symbol to the possible
alignment of a section. There is no point to permitting
a higher alignment for a common symbol: we can not
guarantee it, and it may cause us to allocate extra space
in the common section. */
if (section == bfd_com_section_ptr
&& (*sym_hash)->root.type == bfd_link_hash_common
&& ((*sym_hash)->root.u.c.p->alignment_power
> bfd_coff_default_section_alignment_power (abfd)))
(*sym_hash)->root.u.c.p->alignment_power
= bfd_coff_default_section_alignment_power (abfd);
if (bfd_get_flavour (info->output_bfd) == bfd_get_flavour (abfd))
{
/* If we don't have any symbol information currently in
the hash table, or if we are looking at a symbol
definition, then update the symbol class and type in
the hash table. */
if (((*sym_hash)->symbol_class == C_NULL
&& (*sym_hash)->type == T_NULL)
|| sym.n_scnum != 0
|| (sym.n_value != 0
&& (*sym_hash)->root.type != bfd_link_hash_defined
&& (*sym_hash)->root.type != bfd_link_hash_defweak))
{
(*sym_hash)->symbol_class = sym.n_sclass;
if (sym.n_type != T_NULL)
{
/* We want to warn if the type changed, but not
if it changed from an unspecified type.
Testing the whole type byte may work, but the
change from (e.g.) a function of unspecified
type to function of known type also wants to
skip the warning. */
if ((*sym_hash)->type != T_NULL
&& (*sym_hash)->type != sym.n_type
&& !(DTYPE ((*sym_hash)->type) == DTYPE (sym.n_type)
&& (BTYPE ((*sym_hash)->type) == T_NULL
|| BTYPE (sym.n_type) == T_NULL)))
(*_bfd_error_handler)
(_("Warning: type of symbol `%s' changed from %d to %d in %B"),
abfd, name, (*sym_hash)->type, sym.n_type);
/* We don't want to change from a meaningful
base type to a null one, but if we know
nothing, take what little we might now know. */
if (BTYPE (sym.n_type) != T_NULL
|| (*sym_hash)->type == T_NULL)
(*sym_hash)->type = sym.n_type;
}
(*sym_hash)->auxbfd = abfd;
if (sym.n_numaux != 0)
{
union internal_auxent *alloc;
unsigned int i;
bfd_byte *eaux;
union internal_auxent *iaux;
(*sym_hash)->numaux = sym.n_numaux;
alloc = ((union internal_auxent *)
bfd_hash_allocate (&info->hash->table,
(sym.n_numaux
* sizeof (*alloc))));
if (alloc == NULL)
goto error_return;
for (i = 0, eaux = esym + symesz, iaux = alloc;
i < sym.n_numaux;
i++, eaux += symesz, iaux++)
bfd_coff_swap_aux_in (abfd, eaux, sym.n_type,
sym.n_sclass, (int) i,
sym.n_numaux, iaux);
(*sym_hash)->aux = alloc;
}
}
}
if (classification == COFF_SYMBOL_PE_SECTION
&& (*sym_hash)->numaux != 0)
{
/* Some PE sections (such as .bss) have a zero size in
the section header, but a non-zero size in the AUX
record. Correct that here.
FIXME: This is not at all the right place to do this.
For example, it won't help objdump. This needs to be
done when we swap in the section header. */
BFD_ASSERT ((*sym_hash)->numaux == 1);
if (section->size == 0)
section->size = (*sym_hash)->aux[0].x_scn.x_scnlen;
/* FIXME: We could test whether the section sizes
matches the size in the aux entry, but apparently
that sometimes fails unexpectedly. */
}
}
esym += (sym.n_numaux + 1) * symesz;
sym_hash += sym.n_numaux + 1;
}
/* If this is a non-traditional, non-relocatable link, try to
optimize the handling of any .stab/.stabstr sections. */
if (! bfd_link_relocatable (info)
&& ! info->traditional_format
&& bfd_get_flavour (info->output_bfd) == bfd_get_flavour (abfd)
&& (info->strip != strip_all && info->strip != strip_debugger))
{
asection *stabstr;
stabstr = bfd_get_section_by_name (abfd, ".stabstr");
if (stabstr != NULL)
{
bfd_size_type string_offset = 0;
asection *stab;
for (stab = abfd->sections; stab; stab = stab->next)
if (CONST_STRNEQ (stab->name, ".stab")
&& (!stab->name[5]
|| (stab->name[5] == '.' && ISDIGIT (stab->name[6]))))
{
struct coff_link_hash_table *table;
struct coff_section_tdata *secdata
= coff_section_data (abfd, stab);
if (secdata == NULL)
{
amt = sizeof (struct coff_section_tdata);
stab->used_by_bfd = bfd_zalloc (abfd, amt);
if (stab->used_by_bfd == NULL)
goto error_return;
secdata = coff_section_data (abfd, stab);
}
table = coff_hash_table (info);
if (! _bfd_link_section_stabs (abfd, &table->stab_info,
stab, stabstr,
&secdata->stab_info,
&string_offset))
goto error_return;
}
}
}
obj_coff_keep_syms (abfd) = keep_syms;
return TRUE;
error_return:
obj_coff_keep_syms (abfd) = keep_syms;
return FALSE;
}
/* Do the final link step. */
bfd_boolean
_bfd_coff_final_link (bfd *abfd,
struct bfd_link_info *info)
{
bfd_size_type symesz;
struct coff_final_link_info flaginfo;
bfd_boolean debug_merge_allocated;
bfd_boolean long_section_names;
asection *o;
struct bfd_link_order *p;
bfd_size_type max_sym_count;
bfd_size_type max_lineno_count;
bfd_size_type max_reloc_count;
bfd_size_type max_output_reloc_count;
bfd_size_type max_contents_size;
file_ptr rel_filepos;
unsigned int relsz;
file_ptr line_filepos;
unsigned int linesz;
bfd *sub;
bfd_byte *external_relocs = NULL;
char strbuf[STRING_SIZE_SIZE];
bfd_size_type amt;
symesz = bfd_coff_symesz (abfd);
flaginfo.info = info;
flaginfo.output_bfd = abfd;
flaginfo.strtab = NULL;
flaginfo.section_info = NULL;
flaginfo.last_file_index = -1;
flaginfo.last_bf_index = -1;
flaginfo.internal_syms = NULL;
flaginfo.sec_ptrs = NULL;
flaginfo.sym_indices = NULL;
flaginfo.outsyms = NULL;
flaginfo.linenos = NULL;
flaginfo.contents = NULL;
flaginfo.external_relocs = NULL;
flaginfo.internal_relocs = NULL;
flaginfo.global_to_static = FALSE;
debug_merge_allocated = FALSE;
coff_data (abfd)->link_info = info;
flaginfo.strtab = _bfd_stringtab_init ();
if (flaginfo.strtab == NULL)
goto error_return;
if (! coff_debug_merge_hash_table_init (&flaginfo.debug_merge))
goto error_return;
debug_merge_allocated = TRUE;
/* Compute the file positions for all the sections. */
if (! abfd->output_has_begun)
{
if (! bfd_coff_compute_section_file_positions (abfd))
goto error_return;
}
/* Count the line numbers and relocation entries required for the
output file. Set the file positions for the relocs. */
rel_filepos = obj_relocbase (abfd);
relsz = bfd_coff_relsz (abfd);
max_contents_size = 0;
max_lineno_count = 0;
max_reloc_count = 0;
long_section_names = FALSE;
for (o = abfd->sections; o != NULL; o = o->next)
{
o->reloc_count = 0;
o->lineno_count = 0;
for (p = o->map_head.link_order; p != NULL; p = p->next)
{
if (p->type == bfd_indirect_link_order)
{
asection *sec;
sec = p->u.indirect.section;
/* Mark all sections which are to be included in the
link. This will normally be every section. We need
to do this so that we can identify any sections which
the linker has decided to not include. */
sec->linker_mark = TRUE;
if (info->strip == strip_none
|| info->strip == strip_some)
o->lineno_count += sec->lineno_count;
if (bfd_link_relocatable (info))
o->reloc_count += sec->reloc_count;
if (sec->rawsize > max_contents_size)
max_contents_size = sec->rawsize;
if (sec->size > max_contents_size)
max_contents_size = sec->size;
if (sec->lineno_count > max_lineno_count)
max_lineno_count = sec->lineno_count;
if (sec->reloc_count > max_reloc_count)
max_reloc_count = sec->reloc_count;
}
else if (bfd_link_relocatable (info)
&& (p->type == bfd_section_reloc_link_order
|| p->type == bfd_symbol_reloc_link_order))
++o->reloc_count;
}
if (o->reloc_count == 0)
o->rel_filepos = 0;
else
{
o->flags |= SEC_RELOC;
o->rel_filepos = rel_filepos;
rel_filepos += o->reloc_count * relsz;
/* In PE COFF, if there are at least 0xffff relocations an
extra relocation will be written out to encode the count. */
if (obj_pe (abfd) && o->reloc_count >= 0xffff)
rel_filepos += relsz;
}
if (bfd_coff_long_section_names (abfd)
&& strlen (o->name) > SCNNMLEN)
{
/* This section has a long name which must go in the string
table. This must correspond to the code in
coff_write_object_contents which puts the string index
into the s_name field of the section header. That is why
we pass hash as FALSE. */
if (_bfd_stringtab_add (flaginfo.strtab, o->name, FALSE, FALSE)
== (bfd_size_type) -1)
goto error_return;
long_section_names = TRUE;
}
}
/* If doing a relocatable link, allocate space for the pointers we
need to keep. */
if (bfd_link_relocatable (info))
{
unsigned int i;
/* We use section_count + 1, rather than section_count, because
the target_index fields are 1 based. */
amt = abfd->section_count + 1;
amt *= sizeof (struct coff_link_section_info);
flaginfo.section_info = (struct coff_link_section_info *) bfd_malloc (amt);
if (flaginfo.section_info == NULL)
goto error_return;
for (i = 0; i <= abfd->section_count; i++)
{
flaginfo.section_info[i].relocs = NULL;
flaginfo.section_info[i].rel_hashes = NULL;
}
}
/* We now know the size of the relocs, so we can determine the file
positions of the line numbers. */
line_filepos = rel_filepos;
linesz = bfd_coff_linesz (abfd);
max_output_reloc_count = 0;
for (o = abfd->sections; o != NULL; o = o->next)
{
if (o->lineno_count == 0)
o->line_filepos = 0;
else
{
o->line_filepos = line_filepos;
line_filepos += o->lineno_count * linesz;
}
if (o->reloc_count != 0)
{
/* We don't know the indices of global symbols until we have
written out all the local symbols. For each section in
the output file, we keep an array of pointers to hash
table entries. Each entry in the array corresponds to a
reloc. When we find a reloc against a global symbol, we
set the corresponding entry in this array so that we can
fix up the symbol index after we have written out all the
local symbols.
Because of this problem, we also keep the relocs in
memory until the end of the link. This wastes memory,
but only when doing a relocatable link, which is not the
common case. */
BFD_ASSERT (bfd_link_relocatable (info));
amt = o->reloc_count;
amt *= sizeof (struct internal_reloc);
flaginfo.section_info[o->target_index].relocs =
(struct internal_reloc *) bfd_malloc (amt);
amt = o->reloc_count;
amt *= sizeof (struct coff_link_hash_entry *);
flaginfo.section_info[o->target_index].rel_hashes =
(struct coff_link_hash_entry **) bfd_malloc (amt);
if (flaginfo.section_info[o->target_index].relocs == NULL
|| flaginfo.section_info[o->target_index].rel_hashes == NULL)
goto error_return;
if (o->reloc_count > max_output_reloc_count)
max_output_reloc_count = o->reloc_count;
}
/* Reset the reloc and lineno counts, so that we can use them to
count the number of entries we have output so far. */
o->reloc_count = 0;
o->lineno_count = 0;
}
obj_sym_filepos (abfd) = line_filepos;
/* Figure out the largest number of symbols in an input BFD. Take
the opportunity to clear the output_has_begun fields of all the
input BFD's. */
max_sym_count = 0;
for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
{
size_t sz;
sub->output_has_begun = FALSE;
sz = bfd_family_coff (sub) ? obj_raw_syment_count (sub) : 2;
if (sz > max_sym_count)
max_sym_count = sz;
}
/* Allocate some buffers used while linking. */
amt = max_sym_count * sizeof (struct internal_syment);
flaginfo.internal_syms = (struct internal_syment *) bfd_malloc (amt);
amt = max_sym_count * sizeof (asection *);
flaginfo.sec_ptrs = (asection **) bfd_malloc (amt);
amt = max_sym_count * sizeof (long);
flaginfo.sym_indices = (long int *) bfd_malloc (amt);
flaginfo.outsyms = (bfd_byte *) bfd_malloc ((max_sym_count + 1) * symesz);
amt = max_lineno_count * bfd_coff_linesz (abfd);
flaginfo.linenos = (bfd_byte *) bfd_malloc (amt);
flaginfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
amt = max_reloc_count * relsz;
flaginfo.external_relocs = (bfd_byte *) bfd_malloc (amt);
if (! bfd_link_relocatable (info))
{
amt = max_reloc_count * sizeof (struct internal_reloc);
flaginfo.internal_relocs = (struct internal_reloc *) bfd_malloc (amt);
}
if ((flaginfo.internal_syms == NULL && max_sym_count > 0)
|| (flaginfo.sec_ptrs == NULL && max_sym_count > 0)
|| (flaginfo.sym_indices == NULL && max_sym_count > 0)
|| flaginfo.outsyms == NULL
|| (flaginfo.linenos == NULL && max_lineno_count > 0)
|| (flaginfo.contents == NULL && max_contents_size > 0)
|| (flaginfo.external_relocs == NULL && max_reloc_count > 0)
|| (! bfd_link_relocatable (info)
&& flaginfo.internal_relocs == NULL
&& max_reloc_count > 0))
goto error_return;
/* We now know the position of everything in the file, except that
we don't know the size of the symbol table and therefore we don't
know where the string table starts. We just build the string
table in memory as we go along. We process all the relocations
for a single input file at once. */
obj_raw_syment_count (abfd) = 0;
if (coff_backend_info (abfd)->_bfd_coff_start_final_link)
{
if (! bfd_coff_start_final_link (abfd, info))
goto error_return;
}
for (o = abfd->sections; o != NULL; o = o->next)
{
for (p = o->map_head.link_order; p != NULL; p = p->next)
{
if (p->type == bfd_indirect_link_order
&& bfd_family_coff (p->u.indirect.section->owner))
{
sub = p->u.indirect.section->owner;
if (! bfd_coff_link_output_has_begun (sub, & flaginfo))
{
if (! _bfd_coff_link_input_bfd (&flaginfo, sub))
goto error_return;
sub->output_has_begun = TRUE;
}
}
else if (p->type == bfd_section_reloc_link_order
|| p->type == bfd_symbol_reloc_link_order)
{
if (! _bfd_coff_reloc_link_order (abfd, &flaginfo, o, p))
goto error_return;
}
else
{
if (! _bfd_default_link_order (abfd, info, o, p))
goto error_return;
}
}
}
if (flaginfo.info->strip != strip_all && flaginfo.info->discard != discard_all)
{
/* Add local symbols from foreign inputs. */
for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
{
unsigned int i;
if (bfd_family_coff (sub) || ! bfd_get_outsymbols (sub))
continue;
for (i = 0; i < bfd_get_symcount (sub); ++i)
{
asymbol *sym = bfd_get_outsymbols (sub) [i];
file_ptr pos;
struct internal_syment isym;
union internal_auxent iaux;
bfd_size_type string_size = 0, indx;
bfd_vma written = 0;
bfd_boolean rewrite = FALSE, hash;
if (! (sym->flags & BSF_LOCAL)
|| (sym->flags & (BSF_SECTION_SYM | BSF_DEBUGGING_RELOC
| BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC
| BSF_SYNTHETIC))
|| ((sym->flags & BSF_DEBUGGING)
&& ! (sym->flags & BSF_FILE)))
continue;
/* See if we are discarding symbols with this name. */
if ((flaginfo.info->strip == strip_some
&& (bfd_hash_lookup (flaginfo.info->keep_hash,
bfd_asymbol_name(sym), FALSE, FALSE)
== NULL))
|| (((flaginfo.info->discard == discard_sec_merge
&& (bfd_get_section (sym)->flags & SEC_MERGE)
&& ! bfd_link_relocatable (flaginfo.info))
|| flaginfo.info->discard == discard_l)
&& bfd_is_local_label_name (sub, bfd_asymbol_name(sym))))
continue;
pos = obj_sym_filepos (abfd) + obj_raw_syment_count (abfd)
* symesz;
if (bfd_seek (abfd, pos, SEEK_SET) != 0)
goto error_return;
if (! coff_write_alien_symbol(abfd, sym, &isym, &iaux, &written,
&string_size, NULL, NULL))
goto error_return;
hash = !flaginfo.info->traditional_format;
if (string_size >= 6 && isym.n_sclass == C_FILE
&& ! isym._n._n_n._n_zeroes && isym.n_numaux)
{
indx = _bfd_stringtab_add (flaginfo.strtab, ".file", hash,
FALSE);
if (indx == (bfd_size_type) -1)
goto error_return;
isym._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
bfd_coff_swap_sym_out (abfd, &isym, flaginfo.outsyms);
if (bfd_seek (abfd, pos, SEEK_SET) != 0
|| bfd_bwrite (flaginfo.outsyms, symesz,
abfd) != symesz)
goto error_return;
string_size -= 6;
}
if (string_size)
{
indx = _bfd_stringtab_add (flaginfo.strtab,
bfd_asymbol_name (sym), hash,
FALSE);
if (indx == (bfd_size_type) -1)
goto error_return;
if (isym.n_sclass != C_FILE)
{
isym._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
bfd_coff_swap_sym_out (abfd, &isym, flaginfo.outsyms);
rewrite = TRUE;
}
else
{
BFD_ASSERT (isym.n_numaux == 1);
iaux.x_file.x_n.x_offset = STRING_SIZE_SIZE + indx;
bfd_coff_swap_aux_out (abfd, &iaux, isym.n_type, C_FILE,
0, 1, flaginfo.outsyms + symesz);
if (bfd_seek (abfd, pos + symesz, SEEK_SET) != 0
|| bfd_bwrite (flaginfo.outsyms + symesz, symesz,
abfd) != symesz)
goto error_return;
}
}
if (isym.n_sclass == C_FILE)
{
if (flaginfo.last_file_index != -1)
{
flaginfo.last_file.n_value = obj_raw_syment_count (abfd);
bfd_coff_swap_sym_out (abfd, &flaginfo.last_file,
flaginfo.outsyms);
pos = obj_sym_filepos (abfd) + flaginfo.last_file_index
* symesz;
rewrite = TRUE;
}
flaginfo.last_file_index = obj_raw_syment_count (abfd);
flaginfo.last_file = isym;
}
if (rewrite
&& (bfd_seek (abfd, pos, SEEK_SET) != 0
|| bfd_bwrite (flaginfo.outsyms, symesz, abfd) != symesz))
goto error_return;
obj_raw_syment_count (abfd) += written;
}
}
}
if (! bfd_coff_final_link_postscript (abfd, & flaginfo))
goto error_return;
/* Free up the buffers used by _bfd_coff_link_input_bfd. */
coff_debug_merge_hash_table_free (&flaginfo.debug_merge);
debug_merge_allocated = FALSE;
if (flaginfo.internal_syms != NULL)
{
free (flaginfo.internal_syms);
flaginfo.internal_syms = NULL;
}
if (flaginfo.sec_ptrs != NULL)
{
free (flaginfo.sec_ptrs);
flaginfo.sec_ptrs = NULL;
}
if (flaginfo.sym_indices != NULL)
{
free (flaginfo.sym_indices);
flaginfo.sym_indices = NULL;
}