]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - bfd/cofflink.c
Automatic date update in version.in
[thirdparty/binutils-gdb.git] / bfd / cofflink.c
CommitLineData
252b5132 1/* COFF specific linker code.
fd67aa11 2 Copyright (C) 1994-2024 Free Software Foundation, Inc.
252b5132
RH
3 Written by Ian Lance Taylor, Cygnus Support.
4
8b956130 5 This file is part of BFD, the Binary File Descriptor library.
252b5132 6
8b956130
NC
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
cd123cb7 9 the Free Software Foundation; either version 3 of the License, or
8b956130 10 (at your option) any later version.
252b5132 11
8b956130
NC
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
252b5132 16
8b956130
NC
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
cd123cb7
NC
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
252b5132
RH
21
22/* This file contains the COFF backend linker code. */
23
252b5132 24#include "sysdep.h"
3db64b00 25#include "bfd.h"
252b5132
RH
26#include "bfdlink.h"
27#include "libbfd.h"
28#include "coff/internal.h"
29#include "libcoff.h"
a3aa4448 30#include "safe-ctype.h"
252b5132 31
0a1b45a2
AM
32static bool coff_link_add_object_symbols (bfd *, struct bfd_link_info *);
33static bool coff_link_check_archive_element
13e570f8 34 (bfd *, struct bfd_link_info *, struct bfd_link_hash_entry *, const char *,
0a1b45a2
AM
35 bool *);
36static bool coff_link_add_symbols (bfd *, struct bfd_link_info *);
252b5132 37
b34976b6 38/* Return TRUE if SYM is a weak, external symbol. */
e4202681
NC
39#define IS_WEAK_EXTERNAL(abfd, sym) \
40 ((sym).n_sclass == C_WEAKEXT \
41 || (obj_pe (abfd) && (sym).n_sclass == C_NT_WEAK))
42
b34976b6 43/* Return TRUE if SYM is an external symbol. */
e4202681
NC
44#define IS_EXTERNAL(abfd, sym) \
45 ((sym).n_sclass == C_EXT || IS_WEAK_EXTERNAL (abfd, sym))
46
b2d638c7
ILT
47/* Define macros so that the ISFCN, et. al., macros work correctly.
48 These macros are defined in include/coff/internal.h in terms of
49 N_TMASK, etc. These definitions require a user to define local
50 variables with the appropriate names, and with values from the
51 coff_data (abfd) structure. */
52
53#define N_TMASK n_tmask
54#define N_BTSHFT n_btshft
55#define N_BTMASK n_btmask
56
252b5132
RH
57/* Create an entry in a COFF linker hash table. */
58
59struct bfd_hash_entry *
8b956130
NC
60_bfd_coff_link_hash_newfunc (struct bfd_hash_entry *entry,
61 struct bfd_hash_table *table,
62 const char *string)
252b5132
RH
63{
64 struct coff_link_hash_entry *ret = (struct coff_link_hash_entry *) entry;
65
66 /* Allocate the structure if it has not already been allocated by a
67 subclass. */
68 if (ret == (struct coff_link_hash_entry *) NULL)
69 ret = ((struct coff_link_hash_entry *)
70 bfd_hash_allocate (table, sizeof (struct coff_link_hash_entry)));
71 if (ret == (struct coff_link_hash_entry *) NULL)
72 return (struct bfd_hash_entry *) ret;
73
74 /* Call the allocation method of the superclass. */
75 ret = ((struct coff_link_hash_entry *)
76 _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret,
77 table, string));
78 if (ret != (struct coff_link_hash_entry *) NULL)
79 {
80 /* Set local fields. */
81 ret->indx = -1;
82 ret->type = T_NULL;
96d56e9f 83 ret->symbol_class = C_NULL;
252b5132
RH
84 ret->numaux = 0;
85 ret->auxbfd = NULL;
86 ret->aux = NULL;
87 }
88
89 return (struct bfd_hash_entry *) ret;
90}
91
80d4e113
LB
92struct bfd_hash_entry *
93_decoration_hash_newfunc (struct bfd_hash_entry *entry,
94 struct bfd_hash_table *table,
95 const char *string)
96{
97 struct decoration_hash_entry *ret = (struct decoration_hash_entry *) entry;
98
99 /* Allocate the structure if it has not already been allocated by a
100 subclass. */
101 if (ret == NULL)
102 {
103 ret = (struct decoration_hash_entry *)
104 bfd_hash_allocate (table, sizeof (struct decoration_hash_entry));
105 if (ret == NULL)
106 return NULL;
107 }
108
109 /* Call the allocation method of the superclass. */
110 ret = (struct decoration_hash_entry *)
111 _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret, table, string);
112 if (ret != NULL)
113 {
114 ret->decorated_link = NULL;
115 }
116
117 return (struct bfd_hash_entry *) ret;
118}
119
252b5132
RH
120/* Initialize a COFF linker hash table. */
121
0a1b45a2 122bool
8b956130
NC
123_bfd_coff_link_hash_table_init (struct coff_link_hash_table *table,
124 bfd *abfd,
125 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
126 struct bfd_hash_table *,
66eb6687
AM
127 const char *),
128 unsigned int entsize)
252b5132 129{
3722b82f 130 memset (&table->stab_info, 0, sizeof (table->stab_info));
80d4e113
LB
131
132 return bfd_hash_table_init (&table->decoration_hash,
133 _decoration_hash_newfunc,
134 sizeof (struct decoration_hash_entry))
135 &&_bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
252b5132
RH
136}
137
138/* Create a COFF linker hash table. */
139
140struct bfd_link_hash_table *
8b956130 141_bfd_coff_link_hash_table_create (bfd *abfd)
252b5132
RH
142{
143 struct coff_link_hash_table *ret;
986f0783 144 size_t amt = sizeof (struct coff_link_hash_table);
252b5132 145
a50b1753 146 ret = (struct coff_link_hash_table *) bfd_malloc (amt);
252b5132
RH
147 if (ret == NULL)
148 return NULL;
8b956130 149
252b5132 150 if (! _bfd_coff_link_hash_table_init (ret, abfd,
66eb6687
AM
151 _bfd_coff_link_hash_newfunc,
152 sizeof (struct coff_link_hash_entry)))
252b5132 153 {
e2d34d7d 154 free (ret);
252b5132
RH
155 return (struct bfd_link_hash_table *) NULL;
156 }
157 return &ret->root;
158}
159
160/* Create an entry in a COFF debug merge hash table. */
161
162struct bfd_hash_entry *
8b956130
NC
163_bfd_coff_debug_merge_hash_newfunc (struct bfd_hash_entry *entry,
164 struct bfd_hash_table *table,
165 const char *string)
252b5132
RH
166{
167 struct coff_debug_merge_hash_entry *ret =
168 (struct coff_debug_merge_hash_entry *) entry;
169
170 /* Allocate the structure if it has not already been allocated by a
171 subclass. */
172 if (ret == (struct coff_debug_merge_hash_entry *) NULL)
173 ret = ((struct coff_debug_merge_hash_entry *)
174 bfd_hash_allocate (table,
175 sizeof (struct coff_debug_merge_hash_entry)));
176 if (ret == (struct coff_debug_merge_hash_entry *) NULL)
177 return (struct bfd_hash_entry *) ret;
178
179 /* Call the allocation method of the superclass. */
180 ret = ((struct coff_debug_merge_hash_entry *)
181 bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string));
182 if (ret != (struct coff_debug_merge_hash_entry *) NULL)
183 {
184 /* Set local fields. */
185 ret->types = NULL;
186 }
187
188 return (struct bfd_hash_entry *) ret;
189}
190
191/* Given a COFF BFD, add symbols to the global hash table as
192 appropriate. */
193
0a1b45a2 194bool
8b956130 195_bfd_coff_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
252b5132
RH
196{
197 switch (bfd_get_format (abfd))
198 {
199 case bfd_object:
200 return coff_link_add_object_symbols (abfd, info);
201 case bfd_archive:
8b956130
NC
202 return _bfd_generic_link_add_archive_symbols
203 (abfd, info, coff_link_check_archive_element);
252b5132
RH
204 default:
205 bfd_set_error (bfd_error_wrong_format);
0a1b45a2 206 return false;
252b5132
RH
207 }
208}
209
210/* Add symbols from a COFF object file. */
211
0a1b45a2 212static bool
8b956130 213coff_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
252b5132
RH
214{
215 if (! _bfd_coff_get_external_symbols (abfd))
0a1b45a2 216 return false;
252b5132 217 if (! coff_link_add_symbols (abfd, info))
0a1b45a2 218 return false;
252b5132 219
8b956130
NC
220 if (! info->keep_memory
221 && ! _bfd_coff_free_symbols (abfd))
0a1b45a2 222 return false;
252b5132 223
0a1b45a2 224 return true;
252b5132
RH
225}
226
8b956130
NC
227/* Check a single archive element to see if we need to include it in
228 the link. *PNEEDED is set according to whether this element is
229 needed in the link or not. This is called via
230 _bfd_generic_link_add_archive_symbols. */
231
0a1b45a2 232static bool
8b956130
NC
233coff_link_check_archive_element (bfd *abfd,
234 struct bfd_link_info *info,
13e570f8
AM
235 struct bfd_link_hash_entry *h,
236 const char *name,
0a1b45a2 237 bool *pneeded)
8b956130 238{
0a1b45a2 239 *pneeded = false;
5d3236ee 240
10556cdc
NC
241 /* PR 22369 - Skip non COFF objects in the archive. */
242 if (! bfd_family_coff (abfd))
0a1b45a2 243 return true;
10556cdc 244
13e570f8
AM
245 /* We are only interested in symbols that are currently undefined.
246 If a symbol is currently known to be common, COFF linkers do not
247 bring in an object file which defines it. */
248 if (h->type != bfd_link_hash_undefined)
0a1b45a2 249 return true;
8b956130 250
75cfe082
AM
251 /* If the archive element has already been loaded then one
252 of the symbols defined by that element might have been
253 made undefined due to being in a discarded section. */
254 if (((struct coff_link_hash_entry *) h)->indx == -3)
0a1b45a2 255 return true;
75cfe082 256
b95a0a31 257 /* Include this element? */
13e570f8 258 if (!(*info->callbacks->add_archive_element) (info, abfd, name, &abfd))
0a1b45a2
AM
259 return true;
260 *pneeded = true;
8b956130 261
0f4a61b4 262 return bfd_link_add_symbols (abfd, info);
8b956130
NC
263}
264
252b5132
RH
265/* Add all the symbols from an object file to the hash table. */
266
0a1b45a2 267static bool
8b956130
NC
268coff_link_add_symbols (bfd *abfd,
269 struct bfd_link_info *info)
252b5132 270{
b2d638c7
ILT
271 unsigned int n_tmask = coff_data (abfd)->local_n_tmask;
272 unsigned int n_btshft = coff_data (abfd)->local_n_btshft;
273 unsigned int n_btmask = coff_data (abfd)->local_n_btmask;
0a1b45a2
AM
274 bool keep_syms;
275 bool default_copy;
252b5132
RH
276 bfd_size_type symcount;
277 struct coff_link_hash_entry **sym_hash;
278 bfd_size_type symesz;
279 bfd_byte *esym;
280 bfd_byte *esym_end;
dc810e39 281 bfd_size_type amt;
252b5132 282
d63388ff
MS
283 symcount = obj_raw_syment_count (abfd);
284
285 if (symcount == 0)
0a1b45a2 286 return true; /* Nothing to do. */
d63388ff 287
252b5132
RH
288 /* Keep the symbols during this function, in case the linker needs
289 to read the generic symbols in order to report an error message. */
290 keep_syms = obj_coff_keep_syms (abfd);
0a1b45a2 291 obj_coff_keep_syms (abfd) = true;
252b5132 292
252b5132 293 if (info->keep_memory)
0a1b45a2 294 default_copy = false;
252b5132 295 else
0a1b45a2 296 default_copy = true;
252b5132 297
252b5132
RH
298 /* We keep a list of the linker hash table entries that correspond
299 to particular symbols. */
dc810e39 300 amt = symcount * sizeof (struct coff_link_hash_entry *);
a50b1753 301 sym_hash = (struct coff_link_hash_entry **) bfd_zalloc (abfd, amt);
d63388ff 302 if (sym_hash == NULL)
252b5132
RH
303 goto error_return;
304 obj_coff_sym_hashes (abfd) = sym_hash;
252b5132
RH
305
306 symesz = bfd_coff_symesz (abfd);
307 BFD_ASSERT (symesz == bfd_coff_auxesz (abfd));
308 esym = (bfd_byte *) obj_coff_external_syms (abfd);
309 esym_end = esym + symcount * symesz;
310 while (esym < esym_end)
311 {
312 struct internal_syment sym;
5d54c628 313 enum coff_symbol_classification classification;
0a1b45a2 314 bool copy;
252b5132 315
8b956130 316 bfd_coff_swap_sym_in (abfd, esym, &sym);
252b5132 317
5d54c628
ILT
318 classification = bfd_coff_classify_symbol (abfd, &sym);
319 if (classification != COFF_SYMBOL_LOCAL)
252b5132
RH
320 {
321 const char *name;
322 char buf[SYMNMLEN + 1];
323 flagword flags;
324 asection *section;
325 bfd_vma value;
0a1b45a2
AM
326 bool addit;
327 bool discarded = false;
252b5132
RH
328
329 /* This symbol is externally visible. */
330
331 name = _bfd_coff_internal_syment_name (abfd, &sym, buf);
332 if (name == NULL)
333 goto error_return;
334
335 /* We must copy the name into memory if we got it from the
07d6d2b8 336 syment itself, rather than the string table. */
252b5132
RH
337 copy = default_copy;
338 if (sym._n._n_n._n_zeroes != 0
339 || sym._n._n_n._n_offset == 0)
0a1b45a2 340 copy = true;
252b5132
RH
341
342 value = sym.n_value;
343
5d54c628 344 switch (classification)
252b5132 345 {
5d54c628
ILT
346 default:
347 abort ();
348
349 case COFF_SYMBOL_GLOBAL:
252b5132
RH
350 flags = BSF_EXPORT | BSF_GLOBAL;
351 section = coff_section_from_bfd_index (abfd, sym.n_scnum);
2219ae0b 352 if (discarded_section (section))
75cfe082 353 {
0a1b45a2 354 discarded = true;
75cfe082
AM
355 section = bfd_und_section_ptr;
356 }
2219ae0b 357 else if (! obj_pe (abfd))
252b5132 358 value -= section->vma;
c77ec726 359 break;
5d54c628
ILT
360
361 case COFF_SYMBOL_UNDEFINED:
362 flags = 0;
363 section = bfd_und_section_ptr;
364 break;
365
366 case COFF_SYMBOL_COMMON:
367 flags = BSF_GLOBAL;
368 section = bfd_com_section_ptr;
369 break;
370
371 case COFF_SYMBOL_PE_SECTION:
372 flags = BSF_SECTION_SYM | BSF_GLOBAL;
373 section = coff_section_from_bfd_index (abfd, sym.n_scnum);
2219ae0b
L
374 if (discarded_section (section))
375 section = bfd_und_section_ptr;
5d54c628 376 break;
252b5132
RH
377 }
378
e4202681 379 if (IS_WEAK_EXTERNAL (abfd, sym))
252b5132
RH
380 flags = BSF_WEAK;
381
0a1b45a2 382 addit = true;
7fd9c191
ILT
383
384 /* In the PE format, section symbols actually refer to the
07d6d2b8
AM
385 start of the output section. We handle them specially
386 here. */
7fd9c191
ILT
387 if (obj_pe (abfd) && (flags & BSF_SECTION_SYM) != 0)
388 {
389 *sym_hash = coff_link_hash_lookup (coff_hash_table (info),
0a1b45a2 390 name, false, copy, false);
7fd9c191
ILT
391 if (*sym_hash != NULL)
392 {
393 if (((*sym_hash)->coff_link_hash_flags
394 & COFF_LINK_HASH_PE_SECTION_SYMBOL) == 0
395 && (*sym_hash)->root.type != bfd_link_hash_undefined
396 && (*sym_hash)->root.type != bfd_link_hash_undefweak)
4eca0228 397 _bfd_error_handler
59d08d6c 398 (_("warning: symbol `%s' is both section and non-section"),
7fd9c191
ILT
399 name);
400
0a1b45a2 401 addit = false;
7fd9c191
ILT
402 }
403 }
404
49147fca
ILT
405 /* The Microsoft Visual C compiler does string pooling by
406 hashing the constants to an internal symbol name, and
08da05b0 407 relying on the linker comdat support to discard
49147fca
ILT
408 duplicate names. However, if one string is a literal and
409 one is a data initializer, one will end up in the .data
410 section and one will end up in the .rdata section. The
411 Microsoft linker will combine them into the .data
412 section, which seems to be wrong since it might cause the
413 literal to change.
414
415 As long as there are no external references to the
416 symbols, which there shouldn't be, we can treat the .data
417 and .rdata instances as separate symbols. The comdat
418 code in the linker will do the appropriate merging. Here
419 we avoid getting a multiple definition error for one of
420 these special symbols.
421
422 FIXME: I don't think this will work in the case where
423 there are two object files which use the constants as a
424 literal and two object files which use it as a data
425 initializer. One or the other of the second object files
426 is going to wind up with an inappropriate reference. */
427 if (obj_pe (abfd)
428 && (classification == COFF_SYMBOL_GLOBAL
429 || classification == COFF_SYMBOL_PE_SECTION)
271d0fc3 430 && coff_section_data (abfd, section) != NULL
082b7297 431 && coff_section_data (abfd, section)->comdat != NULL
08dedd66 432 && startswith (name, "??_")
082b7297 433 && strcmp (name, coff_section_data (abfd, section)->comdat->name) == 0)
49147fca
ILT
434 {
435 if (*sym_hash == NULL)
436 *sym_hash = coff_link_hash_lookup (coff_hash_table (info),
0a1b45a2 437 name, false, copy, false);
49147fca
ILT
438 if (*sym_hash != NULL
439 && (*sym_hash)->root.type == bfd_link_hash_defined
082b7297
L
440 && coff_section_data (abfd, (*sym_hash)->root.u.def.section)->comdat != NULL
441 && strcmp (coff_section_data (abfd, (*sym_hash)->root.u.def.section)->comdat->name,
442 coff_section_data (abfd, section)->comdat->name) == 0)
0a1b45a2 443 addit = false;
49147fca
ILT
444 }
445
7fd9c191
ILT
446 if (addit)
447 {
448 if (! (bfd_coff_link_add_one_symbol
449 (info, abfd, name, flags, section, value,
0a1b45a2 450 (const char *) NULL, copy, false,
7fd9c191
ILT
451 (struct bfd_link_hash_entry **) sym_hash)))
452 goto error_return;
75cfe082
AM
453
454 if (discarded)
455 (*sym_hash)->indx = -3;
7fd9c191
ILT
456 }
457
458 if (obj_pe (abfd) && (flags & BSF_SECTION_SYM) != 0)
459 (*sym_hash)->coff_link_hash_flags |=
460 COFF_LINK_HASH_PE_SECTION_SYMBOL;
252b5132 461
2820a0b7 462 /* Limit the alignment of a common symbol to the possible
07d6d2b8
AM
463 alignment of a section. There is no point to permitting
464 a higher alignment for a common symbol: we can not
465 guarantee it, and it may cause us to allocate extra space
466 in the common section. */
252b5132
RH
467 if (section == bfd_com_section_ptr
468 && (*sym_hash)->root.type == bfd_link_hash_common
469 && ((*sym_hash)->root.u.c.p->alignment_power
470 > bfd_coff_default_section_alignment_power (abfd)))
471 (*sym_hash)->root.u.c.p->alignment_power
472 = bfd_coff_default_section_alignment_power (abfd);
473
f13a99db 474 if (bfd_get_flavour (info->output_bfd) == bfd_get_flavour (abfd))
252b5132 475 {
5d3aaa74 476 /* If we don't have any symbol information currently in
07d6d2b8
AM
477 the hash table, or if we are looking at a symbol
478 definition, then update the symbol class and type in
479 the hash table. */
480 if (((*sym_hash)->symbol_class == C_NULL
481 && (*sym_hash)->type == T_NULL)
482 || sym.n_scnum != 0
483 || (sym.n_value != 0
484 && (*sym_hash)->root.type != bfd_link_hash_defined
485 && (*sym_hash)->root.type != bfd_link_hash_defweak))
486 {
487 (*sym_hash)->symbol_class = sym.n_sclass;
488 if (sym.n_type != T_NULL)
489 {
490 /* We want to warn if the type changed, but not
491 if it changed from an unspecified type.
492 Testing the whole type byte may work, but the
493 change from (e.g.) a function of unspecified
494 type to function of known type also wants to
495 skip the warning. */
496 if ((*sym_hash)->type != T_NULL
497 && (*sym_hash)->type != sym.n_type
498 && !(DTYPE ((*sym_hash)->type) == DTYPE (sym.n_type)
499 && (BTYPE ((*sym_hash)->type) == T_NULL
500 || BTYPE (sym.n_type) == T_NULL)))
4eca0228 501 _bfd_error_handler
695344c0 502 /* xgettext: c-format */
59d08d6c 503 (_("warning: type of symbol `%s' changed"
871b3ab2 504 " from %d to %d in %pB"),
c08bb8dd 505 name, (*sym_hash)->type, sym.n_type, abfd);
5d3aaa74 506
07d6d2b8
AM
507 /* We don't want to change from a meaningful
508 base type to a null one, but if we know
509 nothing, take what little we might now know. */
510 if (BTYPE (sym.n_type) != T_NULL
511 || (*sym_hash)->type == T_NULL)
5d3aaa74 512 (*sym_hash)->type = sym.n_type;
07d6d2b8
AM
513 }
514 (*sym_hash)->auxbfd = abfd;
252b5132
RH
515 if (sym.n_numaux != 0)
516 {
517 union internal_auxent *alloc;
518 unsigned int i;
519 bfd_byte *eaux;
520 union internal_auxent *iaux;
521
522 (*sym_hash)->numaux = sym.n_numaux;
523 alloc = ((union internal_auxent *)
524 bfd_hash_allocate (&info->hash->table,
525 (sym.n_numaux
526 * sizeof (*alloc))));
527 if (alloc == NULL)
528 goto error_return;
529 for (i = 0, eaux = esym + symesz, iaux = alloc;
530 i < sym.n_numaux;
531 i++, eaux += symesz, iaux++)
8b956130 532 bfd_coff_swap_aux_in (abfd, eaux, sym.n_type,
dc810e39 533 sym.n_sclass, (int) i,
8b956130 534 sym.n_numaux, iaux);
252b5132
RH
535 (*sym_hash)->aux = alloc;
536 }
537 }
538 }
5d54c628
ILT
539
540 if (classification == COFF_SYMBOL_PE_SECTION
541 && (*sym_hash)->numaux != 0)
542 {
543 /* Some PE sections (such as .bss) have a zero size in
07d6d2b8
AM
544 the section header, but a non-zero size in the AUX
545 record. Correct that here.
5d54c628
ILT
546
547 FIXME: This is not at all the right place to do this.
548 For example, it won't help objdump. This needs to be
549 done when we swap in the section header. */
5d54c628 550 BFD_ASSERT ((*sym_hash)->numaux == 1);
eea6121a
AM
551 if (section->size == 0)
552 section->size = (*sym_hash)->aux[0].x_scn.x_scnlen;
5d54c628
ILT
553
554 /* FIXME: We could test whether the section sizes
07d6d2b8
AM
555 matches the size in the aux entry, but apparently
556 that sometimes fails unexpectedly. */
5d54c628 557 }
252b5132
RH
558 }
559
560 esym += (sym.n_numaux + 1) * symesz;
561 sym_hash += sym.n_numaux + 1;
562 }
563
1049f94e 564 /* If this is a non-traditional, non-relocatable link, try to
252b5132 565 optimize the handling of any .stab/.stabstr sections. */
0e1862bb 566 if (! bfd_link_relocatable (info)
252b5132 567 && ! info->traditional_format
f13a99db 568 && bfd_get_flavour (info->output_bfd) == bfd_get_flavour (abfd)
252b5132
RH
569 && (info->strip != strip_all && info->strip != strip_debugger))
570 {
29ca8dc5 571 asection *stabstr;
252b5132 572
29ca8dc5 573 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
252b5132 574
29ca8dc5
NS
575 if (stabstr != NULL)
576 {
577 bfd_size_type string_offset = 0;
578 asection *stab;
68ffbac6 579
29ca8dc5 580 for (stab = abfd->sections; stab; stab = stab->next)
08dedd66 581 if (startswith (stab->name, ".stab")
29ca8dc5 582 && (!stab->name[5]
a3aa4448 583 || (stab->name[5] == '.' && ISDIGIT (stab->name[6]))))
252b5132
RH
584 {
585 struct coff_link_hash_table *table;
29ca8dc5
NS
586 struct coff_section_tdata *secdata
587 = coff_section_data (abfd, stab);
68ffbac6 588
252b5132
RH
589 if (secdata == NULL)
590 {
dc810e39 591 amt = sizeof (struct coff_section_tdata);
8b956130 592 stab->used_by_bfd = bfd_zalloc (abfd, amt);
252b5132
RH
593 if (stab->used_by_bfd == NULL)
594 goto error_return;
595 secdata = coff_section_data (abfd, stab);
596 }
597
598 table = coff_hash_table (info);
599
600 if (! _bfd_link_section_stabs (abfd, &table->stab_info,
601 stab, stabstr,
29ca8dc5
NS
602 &secdata->stab_info,
603 &string_offset))
252b5132
RH
604 goto error_return;
605 }
606 }
607 }
608
609 obj_coff_keep_syms (abfd) = keep_syms;
610
0a1b45a2 611 return true;
252b5132
RH
612
613 error_return:
614 obj_coff_keep_syms (abfd) = keep_syms;
0a1b45a2 615 return false;
252b5132
RH
616}
617\f
618/* Do the final link step. */
619
0a1b45a2 620bool
8b956130
NC
621_bfd_coff_final_link (bfd *abfd,
622 struct bfd_link_info *info)
252b5132
RH
623{
624 bfd_size_type symesz;
57402f1e 625 struct coff_final_link_info flaginfo;
0a1b45a2
AM
626 bool debug_merge_allocated;
627 bool long_section_names;
252b5132
RH
628 asection *o;
629 struct bfd_link_order *p;
dc810e39
AM
630 bfd_size_type max_sym_count;
631 bfd_size_type max_lineno_count;
632 bfd_size_type max_reloc_count;
633 bfd_size_type max_output_reloc_count;
634 bfd_size_type max_contents_size;
252b5132
RH
635 file_ptr rel_filepos;
636 unsigned int relsz;
637 file_ptr line_filepos;
638 unsigned int linesz;
639 bfd *sub;
640 bfd_byte *external_relocs = NULL;
641 char strbuf[STRING_SIZE_SIZE];
dc810e39 642 bfd_size_type amt;
252b5132
RH
643
644 symesz = bfd_coff_symesz (abfd);
645
57402f1e
NC
646 flaginfo.info = info;
647 flaginfo.output_bfd = abfd;
648 flaginfo.strtab = NULL;
649 flaginfo.section_info = NULL;
650 flaginfo.last_file_index = -1;
651 flaginfo.last_bf_index = -1;
652 flaginfo.internal_syms = NULL;
653 flaginfo.sec_ptrs = NULL;
654 flaginfo.sym_indices = NULL;
655 flaginfo.outsyms = NULL;
656 flaginfo.linenos = NULL;
657 flaginfo.contents = NULL;
658 flaginfo.external_relocs = NULL;
659 flaginfo.internal_relocs = NULL;
0a1b45a2
AM
660 flaginfo.global_to_static = false;
661 debug_merge_allocated = false;
252b5132
RH
662
663 coff_data (abfd)->link_info = info;
664
57402f1e
NC
665 flaginfo.strtab = _bfd_stringtab_init ();
666 if (flaginfo.strtab == NULL)
252b5132
RH
667 goto error_return;
668
57402f1e 669 if (! coff_debug_merge_hash_table_init (&flaginfo.debug_merge))
252b5132 670 goto error_return;
0a1b45a2 671 debug_merge_allocated = true;
252b5132
RH
672
673 /* Compute the file positions for all the sections. */
674 if (! abfd->output_has_begun)
675 {
676 if (! bfd_coff_compute_section_file_positions (abfd))
677 goto error_return;
678 }
679
680 /* Count the line numbers and relocation entries required for the
681 output file. Set the file positions for the relocs. */
682 rel_filepos = obj_relocbase (abfd);
683 relsz = bfd_coff_relsz (abfd);
684 max_contents_size = 0;
685 max_lineno_count = 0;
686 max_reloc_count = 0;
687
0a1b45a2 688 long_section_names = false;
252b5132
RH
689 for (o = abfd->sections; o != NULL; o = o->next)
690 {
691 o->reloc_count = 0;
692 o->lineno_count = 0;
8423293d 693 for (p = o->map_head.link_order; p != NULL; p = p->next)
252b5132
RH
694 {
695 if (p->type == bfd_indirect_link_order)
696 {
697 asection *sec;
698
699 sec = p->u.indirect.section;
700
701 /* Mark all sections which are to be included in the
702 link. This will normally be every section. We need
703 to do this so that we can identify any sections which
704 the linker has decided to not include. */
0a1b45a2 705 sec->linker_mark = true;
252b5132
RH
706
707 if (info->strip == strip_none
708 || info->strip == strip_some)
709 o->lineno_count += sec->lineno_count;
710
0e1862bb 711 if (bfd_link_relocatable (info))
252b5132
RH
712 o->reloc_count += sec->reloc_count;
713
eea6121a
AM
714 if (sec->rawsize > max_contents_size)
715 max_contents_size = sec->rawsize;
716 if (sec->size > max_contents_size)
717 max_contents_size = sec->size;
252b5132
RH
718 if (sec->lineno_count > max_lineno_count)
719 max_lineno_count = sec->lineno_count;
720 if (sec->reloc_count > max_reloc_count)
721 max_reloc_count = sec->reloc_count;
722 }
0e1862bb 723 else if (bfd_link_relocatable (info)
252b5132
RH
724 && (p->type == bfd_section_reloc_link_order
725 || p->type == bfd_symbol_reloc_link_order))
726 ++o->reloc_count;
727 }
728 if (o->reloc_count == 0)
729 o->rel_filepos = 0;
730 else
731 {
732 o->flags |= SEC_RELOC;
733 o->rel_filepos = rel_filepos;
734 rel_filepos += o->reloc_count * relsz;
e9168c1e
MM
735 /* In PE COFF, if there are at least 0xffff relocations an
736 extra relocation will be written out to encode the count. */
f717994f 737 if ((obj_pe (abfd) || obj_go32 (abfd)) && o->reloc_count >= 0xffff)
e9168c1e 738 rel_filepos += relsz;
252b5132
RH
739 }
740
741 if (bfd_coff_long_section_names (abfd)
742 && strlen (o->name) > SCNNMLEN)
743 {
744 /* This section has a long name which must go in the string
07d6d2b8
AM
745 table. This must correspond to the code in
746 coff_write_object_contents which puts the string index
747 into the s_name field of the section header. That is why
748 we pass hash as FALSE. */
0a1b45a2 749 if (_bfd_stringtab_add (flaginfo.strtab, o->name, false, false)
252b5132
RH
750 == (bfd_size_type) -1)
751 goto error_return;
0a1b45a2 752 long_section_names = true;
252b5132
RH
753 }
754 }
755
1049f94e 756 /* If doing a relocatable link, allocate space for the pointers we
252b5132 757 need to keep. */
0e1862bb 758 if (bfd_link_relocatable (info))
252b5132
RH
759 {
760 unsigned int i;
761
762 /* We use section_count + 1, rather than section_count, because
07d6d2b8 763 the target_index fields are 1 based. */
dc810e39
AM
764 amt = abfd->section_count + 1;
765 amt *= sizeof (struct coff_link_section_info);
57402f1e
NC
766 flaginfo.section_info = (struct coff_link_section_info *) bfd_malloc (amt);
767 if (flaginfo.section_info == NULL)
252b5132
RH
768 goto error_return;
769 for (i = 0; i <= abfd->section_count; i++)
770 {
57402f1e
NC
771 flaginfo.section_info[i].relocs = NULL;
772 flaginfo.section_info[i].rel_hashes = NULL;
252b5132
RH
773 }
774 }
775
776 /* We now know the size of the relocs, so we can determine the file
777 positions of the line numbers. */
778 line_filepos = rel_filepos;
779 linesz = bfd_coff_linesz (abfd);
780 max_output_reloc_count = 0;
781 for (o = abfd->sections; o != NULL; o = o->next)
782 {
783 if (o->lineno_count == 0)
784 o->line_filepos = 0;
785 else
786 {
787 o->line_filepos = line_filepos;
788 line_filepos += o->lineno_count * linesz;
789 }
790
791 if (o->reloc_count != 0)
792 {
793 /* We don't know the indices of global symbols until we have
07d6d2b8
AM
794 written out all the local symbols. For each section in
795 the output file, we keep an array of pointers to hash
796 table entries. Each entry in the array corresponds to a
797 reloc. When we find a reloc against a global symbol, we
798 set the corresponding entry in this array so that we can
799 fix up the symbol index after we have written out all the
800 local symbols.
252b5132
RH
801
802 Because of this problem, we also keep the relocs in
803 memory until the end of the link. This wastes memory,
1049f94e 804 but only when doing a relocatable link, which is not the
252b5132 805 common case. */
0e1862bb 806 BFD_ASSERT (bfd_link_relocatable (info));
dc810e39
AM
807 amt = o->reloc_count;
808 amt *= sizeof (struct internal_reloc);
57402f1e 809 flaginfo.section_info[o->target_index].relocs =
07d6d2b8 810 (struct internal_reloc *) bfd_malloc (amt);
dc810e39
AM
811 amt = o->reloc_count;
812 amt *= sizeof (struct coff_link_hash_entry *);
57402f1e 813 flaginfo.section_info[o->target_index].rel_hashes =
07d6d2b8 814 (struct coff_link_hash_entry **) bfd_malloc (amt);
57402f1e
NC
815 if (flaginfo.section_info[o->target_index].relocs == NULL
816 || flaginfo.section_info[o->target_index].rel_hashes == NULL)
252b5132
RH
817 goto error_return;
818
819 if (o->reloc_count > max_output_reloc_count)
820 max_output_reloc_count = o->reloc_count;
821 }
822
823 /* Reset the reloc and lineno counts, so that we can use them to
824 count the number of entries we have output so far. */
825 o->reloc_count = 0;
826 o->lineno_count = 0;
827 }
828
829 obj_sym_filepos (abfd) = line_filepos;
830
831 /* Figure out the largest number of symbols in an input BFD. Take
832 the opportunity to clear the output_has_begun fields of all the
833 input BFD's. */
834 max_sym_count = 0;
c72f2fb2 835 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
252b5132
RH
836 {
837 size_t sz;
838
0a1b45a2 839 sub->output_has_begun = false;
e7ebb214 840 sz = bfd_family_coff (sub) ? obj_raw_syment_count (sub) : 2;
252b5132
RH
841 if (sz > max_sym_count)
842 max_sym_count = sz;
843 }
844
845 /* Allocate some buffers used while linking. */
dc810e39 846 amt = max_sym_count * sizeof (struct internal_syment);
57402f1e 847 flaginfo.internal_syms = (struct internal_syment *) bfd_malloc (amt);
dc810e39 848 amt = max_sym_count * sizeof (asection *);
57402f1e 849 flaginfo.sec_ptrs = (asection **) bfd_malloc (amt);
dc810e39 850 amt = max_sym_count * sizeof (long);
57402f1e
NC
851 flaginfo.sym_indices = (long int *) bfd_malloc (amt);
852 flaginfo.outsyms = (bfd_byte *) bfd_malloc ((max_sym_count + 1) * symesz);
dc810e39 853 amt = max_lineno_count * bfd_coff_linesz (abfd);
57402f1e
NC
854 flaginfo.linenos = (bfd_byte *) bfd_malloc (amt);
855 flaginfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
dc810e39 856 amt = max_reloc_count * relsz;
57402f1e 857 flaginfo.external_relocs = (bfd_byte *) bfd_malloc (amt);
0e1862bb 858 if (! bfd_link_relocatable (info))
dc810e39
AM
859 {
860 amt = max_reloc_count * sizeof (struct internal_reloc);
57402f1e 861 flaginfo.internal_relocs = (struct internal_reloc *) bfd_malloc (amt);
dc810e39 862 }
57402f1e
NC
863 if ((flaginfo.internal_syms == NULL && max_sym_count > 0)
864 || (flaginfo.sec_ptrs == NULL && max_sym_count > 0)
865 || (flaginfo.sym_indices == NULL && max_sym_count > 0)
866 || flaginfo.outsyms == NULL
867 || (flaginfo.linenos == NULL && max_lineno_count > 0)
868 || (flaginfo.contents == NULL && max_contents_size > 0)
869 || (flaginfo.external_relocs == NULL && max_reloc_count > 0)
0e1862bb 870 || (! bfd_link_relocatable (info)
57402f1e 871 && flaginfo.internal_relocs == NULL
252b5132
RH
872 && max_reloc_count > 0))
873 goto error_return;
874
875 /* We now know the position of everything in the file, except that
876 we don't know the size of the symbol table and therefore we don't
877 know where the string table starts. We just build the string
878 table in memory as we go along. We process all the relocations
879 for a single input file at once. */
880 obj_raw_syment_count (abfd) = 0;
881
882 if (coff_backend_info (abfd)->_bfd_coff_start_final_link)
883 {
884 if (! bfd_coff_start_final_link (abfd, info))
885 goto error_return;
886 }
887
888 for (o = abfd->sections; o != NULL; o = o->next)
889 {
8423293d 890 for (p = o->map_head.link_order; p != NULL; p = p->next)
252b5132
RH
891 {
892 if (p->type == bfd_indirect_link_order
9bd09e22 893 && bfd_family_coff (p->u.indirect.section->owner))
252b5132
RH
894 {
895 sub = p->u.indirect.section->owner;
57402f1e 896 if (! bfd_coff_link_output_has_begun (sub, & flaginfo))
252b5132 897 {
57402f1e 898 if (! _bfd_coff_link_input_bfd (&flaginfo, sub))
252b5132 899 goto error_return;
0a1b45a2 900 sub->output_has_begun = true;
252b5132
RH
901 }
902 }
903 else if (p->type == bfd_section_reloc_link_order
904 || p->type == bfd_symbol_reloc_link_order)
905 {
57402f1e 906 if (! _bfd_coff_reloc_link_order (abfd, &flaginfo, o, p))
252b5132
RH
907 goto error_return;
908 }
909 else
910 {
911 if (! _bfd_default_link_order (abfd, info, o, p))
912 goto error_return;
913 }
914 }
915 }
916
57402f1e 917 if (flaginfo.info->strip != strip_all && flaginfo.info->discard != discard_all)
e7ebb214
JB
918 {
919 /* Add local symbols from foreign inputs. */
c72f2fb2 920 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
e7ebb214
JB
921 {
922 unsigned int i;
923
924 if (bfd_family_coff (sub) || ! bfd_get_outsymbols (sub))
925 continue;
926 for (i = 0; i < bfd_get_symcount (sub); ++i)
927 {
928 asymbol *sym = bfd_get_outsymbols (sub) [i];
929 file_ptr pos;
930 struct internal_syment isym;
e7ebb214 931 bfd_vma written = 0;
20bed3bb 932 bool rewrite = false;
e7ebb214
JB
933
934 if (! (sym->flags & BSF_LOCAL)
935 || (sym->flags & (BSF_SECTION_SYM | BSF_DEBUGGING_RELOC
936 | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC
937 | BSF_SYNTHETIC))
938 || ((sym->flags & BSF_DEBUGGING)
939 && ! (sym->flags & BSF_FILE)))
940 continue;
941
942 /* See if we are discarding symbols with this name. */
57402f1e
NC
943 if ((flaginfo.info->strip == strip_some
944 && (bfd_hash_lookup (flaginfo.info->keep_hash,
0a1b45a2 945 bfd_asymbol_name(sym), false, false)
e7ebb214 946 == NULL))
57402f1e 947 || (((flaginfo.info->discard == discard_sec_merge
e6f7f6d1 948 && (bfd_asymbol_section (sym)->flags & SEC_MERGE)
0e1862bb 949 && ! bfd_link_relocatable (flaginfo.info))
57402f1e 950 || flaginfo.info->discard == discard_l)
e7ebb214
JB
951 && bfd_is_local_label_name (sub, bfd_asymbol_name(sym))))
952 continue;
953
954 pos = obj_sym_filepos (abfd) + obj_raw_syment_count (abfd)
955 * symesz;
956 if (bfd_seek (abfd, pos, SEEK_SET) != 0)
957 goto error_return;
20bed3bb
JB
958 if (! coff_write_alien_symbol(abfd, sym, &isym, &written,
959 flaginfo.strtab,
960 !flaginfo.info->traditional_format,
961 NULL, NULL))
e7ebb214
JB
962 goto error_return;
963
e7ebb214
JB
964 if (isym.n_sclass == C_FILE)
965 {
57402f1e 966 if (flaginfo.last_file_index != -1)
e7ebb214 967 {
57402f1e
NC
968 flaginfo.last_file.n_value = obj_raw_syment_count (abfd);
969 bfd_coff_swap_sym_out (abfd, &flaginfo.last_file,
970 flaginfo.outsyms);
971 pos = obj_sym_filepos (abfd) + flaginfo.last_file_index
e7ebb214 972 * symesz;
0a1b45a2 973 rewrite = true;
e7ebb214 974 }
57402f1e
NC
975 flaginfo.last_file_index = obj_raw_syment_count (abfd);
976 flaginfo.last_file = isym;
e7ebb214
JB
977 }
978
979 if (rewrite
980 && (bfd_seek (abfd, pos, SEEK_SET) != 0
226f9f4f 981 || bfd_write (flaginfo.outsyms, symesz, abfd) != symesz))
e7ebb214
JB
982 goto error_return;
983
984 obj_raw_syment_count (abfd) += written;
985 }
986 }
987 }
988
57402f1e 989 if (! bfd_coff_final_link_postscript (abfd, & flaginfo))
252b5132 990 goto error_return;
244148ad 991
252b5132
RH
992 /* Free up the buffers used by _bfd_coff_link_input_bfd. */
993
57402f1e 994 coff_debug_merge_hash_table_free (&flaginfo.debug_merge);
0a1b45a2 995 debug_merge_allocated = false;
252b5132 996
c9594989
AM
997 free (flaginfo.internal_syms);
998 flaginfo.internal_syms = NULL;
999 free (flaginfo.sec_ptrs);
1000 flaginfo.sec_ptrs = NULL;
1001 free (flaginfo.sym_indices);
1002 flaginfo.sym_indices = NULL;
1003 free (flaginfo.linenos);
1004 flaginfo.linenos = NULL;
1005 free (flaginfo.contents);
1006 flaginfo.contents = NULL;
1007 free (flaginfo.external_relocs);
1008 flaginfo.external_relocs = NULL;
1009 free (flaginfo.internal_relocs);
1010 flaginfo.internal_relocs = NULL;
252b5132
RH
1011
1012 /* The value of the last C_FILE symbol is supposed to be the symbol
1013 index of the first external symbol. Write it out again if
1014 necessary. */
57402f1e
NC
1015 if (flaginfo.last_file_index != -1
1016 && (unsigned int) flaginfo.last_file.n_value != obj_raw_syment_count (abfd))
252b5132 1017 {
dc810e39
AM
1018 file_ptr pos;
1019
57402f1e
NC
1020 flaginfo.last_file.n_value = obj_raw_syment_count (abfd);
1021 bfd_coff_swap_sym_out (abfd, &flaginfo.last_file,
1022 flaginfo.outsyms);
dc810e39 1023
57402f1e 1024 pos = obj_sym_filepos (abfd) + flaginfo.last_file_index * symesz;
dc810e39 1025 if (bfd_seek (abfd, pos, SEEK_SET) != 0
226f9f4f 1026 || bfd_write (flaginfo.outsyms, symesz, abfd) != symesz)
0a1b45a2 1027 return false;
252b5132
RH
1028 }
1029
1030 /* If doing task linking (ld --task-link) then make a pass through the
1031 global symbols, writing out any that are defined, and making them
244148ad 1032 static. */
252b5132
RH
1033 if (info->task_link)
1034 {
0a1b45a2 1035 flaginfo.failed = false;
e92d460e 1036 coff_link_hash_traverse (coff_hash_table (info),
57402f1e
NC
1037 _bfd_coff_write_task_globals, &flaginfo);
1038 if (flaginfo.failed)
252b5132
RH
1039 goto error_return;
1040 }
1041
1042 /* Write out the global symbols. */
0a1b45a2 1043 flaginfo.failed = false;
57402f1e
NC
1044 bfd_hash_traverse (&info->hash->table, _bfd_coff_write_global_sym, &flaginfo);
1045 if (flaginfo.failed)
252b5132
RH
1046 goto error_return;
1047
1048 /* The outsyms buffer is used by _bfd_coff_write_global_sym. */
c9594989
AM
1049 free (flaginfo.outsyms);
1050 flaginfo.outsyms = NULL;
252b5132 1051
0e1862bb 1052 if (bfd_link_relocatable (info) && max_output_reloc_count > 0)
252b5132
RH
1053 {
1054 /* Now that we have written out all the global symbols, we know
1055 the symbol indices to use for relocs against them, and we can
1056 finally write out the relocs. */
dc810e39 1057 amt = max_output_reloc_count * relsz;
a50b1753 1058 external_relocs = (bfd_byte *) bfd_malloc (amt);
252b5132
RH
1059 if (external_relocs == NULL)
1060 goto error_return;
1061
1062 for (o = abfd->sections; o != NULL; o = o->next)
1063 {
1064 struct internal_reloc *irel;
1065 struct internal_reloc *irelend;
1066 struct coff_link_hash_entry **rel_hash;
1067 bfd_byte *erel;
1068
1069 if (o->reloc_count == 0)
1070 continue;
1071
57402f1e 1072 irel = flaginfo.section_info[o->target_index].relocs;
252b5132 1073 irelend = irel + o->reloc_count;
57402f1e 1074 rel_hash = flaginfo.section_info[o->target_index].rel_hashes;
252b5132
RH
1075 erel = external_relocs;
1076 for (; irel < irelend; irel++, rel_hash++, erel += relsz)
1077 {
1078 if (*rel_hash != NULL)
1079 {
1080 BFD_ASSERT ((*rel_hash)->indx >= 0);
1081 irel->r_symndx = (*rel_hash)->indx;
1082 }
8b956130 1083 bfd_coff_swap_reloc_out (abfd, irel, erel);
252b5132
RH
1084 }
1085
cd339148
NS
1086 if (bfd_seek (abfd, o->rel_filepos, SEEK_SET) != 0)
1087 goto error_return;
f717994f 1088 if ((obj_pe (abfd) || obj_go32 (abfd)) && o->reloc_count >= 0xffff)
cd339148
NS
1089 {
1090 /* In PE COFF, write the count of relocs as the first
1091 reloc. The header overflow bit will be set
1092 elsewhere. */
1093 struct internal_reloc incount;
1094 bfd_byte *excount = (bfd_byte *)bfd_malloc (relsz);
68ffbac6 1095
cd339148
NS
1096 memset (&incount, 0, sizeof (incount));
1097 incount.r_vaddr = o->reloc_count + 1;
2c3fc389 1098 bfd_coff_swap_reloc_out (abfd, &incount, excount);
226f9f4f 1099 if (bfd_write (excount, relsz, abfd) != relsz)
cd339148
NS
1100 /* We'll leak, but it's an error anyway. */
1101 goto error_return;
1102 free (excount);
1103 }
226f9f4f
AM
1104 if (bfd_write (external_relocs,
1105 (bfd_size_type) relsz * o->reloc_count, abfd)
cd339148 1106 != (bfd_size_type) relsz * o->reloc_count)
252b5132
RH
1107 goto error_return;
1108 }
1109
1110 free (external_relocs);
1111 external_relocs = NULL;
1112 }
1113
1114 /* Free up the section information. */
57402f1e 1115 if (flaginfo.section_info != NULL)
252b5132
RH
1116 {
1117 unsigned int i;
1118
1119 for (i = 0; i < abfd->section_count; i++)
1120 {
c9594989
AM
1121 free (flaginfo.section_info[i].relocs);
1122 free (flaginfo.section_info[i].rel_hashes);
252b5132 1123 }
57402f1e
NC
1124 free (flaginfo.section_info);
1125 flaginfo.section_info = NULL;
252b5132
RH
1126 }
1127
1128 /* If we have optimized stabs strings, output them. */
3722b82f 1129 if (coff_hash_table (info)->stab_info.stabstr != NULL)
252b5132
RH
1130 {
1131 if (! _bfd_write_stab_strings (abfd, &coff_hash_table (info)->stab_info))
0a1b45a2 1132 return false;
252b5132
RH
1133 }
1134
1135 /* Write out the string table. */
1136 if (obj_raw_syment_count (abfd) != 0 || long_section_names)
1137 {
dc810e39
AM
1138 file_ptr pos;
1139
1140 pos = obj_sym_filepos (abfd) + obj_raw_syment_count (abfd) * symesz;
1141 if (bfd_seek (abfd, pos, SEEK_SET) != 0)
0a1b45a2 1142 return false;
252b5132
RH
1143
1144#if STRING_SIZE_SIZE == 4
dc810e39 1145 H_PUT_32 (abfd,
57402f1e 1146 _bfd_stringtab_size (flaginfo.strtab) + STRING_SIZE_SIZE,
dc810e39 1147 strbuf);
252b5132 1148#else
dc810e39 1149 #error Change H_PUT_32 above
252b5132
RH
1150#endif
1151
226f9f4f 1152 if (bfd_write (strbuf, STRING_SIZE_SIZE, abfd) != STRING_SIZE_SIZE)
0a1b45a2 1153 return false;
252b5132 1154
57402f1e 1155 if (! _bfd_stringtab_emit (abfd, flaginfo.strtab))
0a1b45a2 1156 return false;
d71f672e 1157
0a1b45a2 1158 obj_coff_strings_written (abfd) = true;
252b5132
RH
1159 }
1160
57402f1e 1161 _bfd_stringtab_free (flaginfo.strtab);
252b5132 1162
ed48ec2e 1163 /* Setting symcount to 0 will cause write_object_contents to
252b5132 1164 not try to write out the symbols. */
ed48ec2e 1165 abfd->symcount = 0;
252b5132 1166
0a1b45a2 1167 return true;
252b5132
RH
1168
1169 error_return:
1170 if (debug_merge_allocated)
57402f1e
NC
1171 coff_debug_merge_hash_table_free (&flaginfo.debug_merge);
1172 if (flaginfo.strtab != NULL)
1173 _bfd_stringtab_free (flaginfo.strtab);
1174 if (flaginfo.section_info != NULL)
252b5132
RH
1175 {
1176 unsigned int i;
1177
1178 for (i = 0; i < abfd->section_count; i++)
1179 {
c9594989
AM
1180 free (flaginfo.section_info[i].relocs);
1181 free (flaginfo.section_info[i].rel_hashes);
252b5132 1182 }
57402f1e 1183 free (flaginfo.section_info);
252b5132 1184 }
c9594989
AM
1185 free (flaginfo.internal_syms);
1186 free (flaginfo.sec_ptrs);
1187 free (flaginfo.sym_indices);
1188 free (flaginfo.outsyms);
1189 free (flaginfo.linenos);
1190 free (flaginfo.contents);
1191 free (flaginfo.external_relocs);
1192 free (flaginfo.internal_relocs);
1193 free (external_relocs);
0a1b45a2 1194 return false;
252b5132
RH
1195}
1196
8b956130 1197/* Parse out a -heap <reserved>,<commit> line. */
252b5132
RH
1198
1199static char *
8b956130 1200dores_com (char *ptr, bfd *output_bfd, int heap)
252b5132 1201{
10df41b1 1202 if (obj_pe (output_bfd))
252b5132
RH
1203 {
1204 int val = strtoul (ptr, &ptr, 0);
8b956130 1205
252b5132 1206 if (heap)
dc810e39 1207 pe_data(output_bfd)->pe_opthdr.SizeOfHeapReserve = val;
252b5132 1208 else
dc810e39 1209 pe_data(output_bfd)->pe_opthdr.SizeOfStackReserve = val;
252b5132 1210
244148ad 1211 if (ptr[0] == ',')
252b5132 1212 {
dc810e39 1213 val = strtoul (ptr+1, &ptr, 0);
252b5132 1214 if (heap)
dc810e39 1215 pe_data(output_bfd)->pe_opthdr.SizeOfHeapCommit = val;
252b5132 1216 else
dc810e39 1217 pe_data(output_bfd)->pe_opthdr.SizeOfStackCommit = val;
252b5132
RH
1218 }
1219 }
1220 return ptr;
1221}
1222
8b956130
NC
1223static char *
1224get_name (char *ptr, char **dst)
252b5132
RH
1225{
1226 while (*ptr == ' ')
1227 ptr++;
1228 *dst = ptr;
1229 while (*ptr && *ptr != ' ')
1230 ptr++;
1231 *ptr = 0;
1232 return ptr+1;
1233}
1234
8b956130 1235/* Process any magic embedded commands in a section called .drectve. */
244148ad 1236
252b5132 1237static int
8b956130
NC
1238process_embedded_commands (bfd *output_bfd,
1239 struct bfd_link_info *info ATTRIBUTE_UNUSED,
1240 bfd *abfd)
252b5132
RH
1241{
1242 asection *sec = bfd_get_section_by_name (abfd, ".drectve");
1243 char *s;
1244 char *e;
eea6121a 1245 bfd_byte *copy;
8b956130 1246
81ff113f 1247 if (sec == NULL || (sec->flags & SEC_HAS_CONTENTS) == 0)
252b5132 1248 return 1;
244148ad 1249
eea6121a 1250 if (!bfd_malloc_and_get_section (abfd, sec, &copy))
252b5132 1251 {
c9594989 1252 free (copy);
252b5132
RH
1253 return 0;
1254 }
f075ee0c 1255 e = (char *) copy + sec->size;
8b956130 1256
f075ee0c 1257 for (s = (char *) copy; s < e ; )
252b5132 1258 {
f075ee0c 1259 if (s[0] != '-')
8b956130
NC
1260 {
1261 s++;
1262 continue;
1263 }
08dedd66 1264 if (startswith (s, "-attr"))
252b5132
RH
1265 {
1266 char *name;
1267 char *attribs;
1268 asection *asec;
252b5132
RH
1269 int loop = 1;
1270 int had_write = 0;
252b5132 1271 int had_exec= 0;
8b956130 1272
252b5132 1273 s += 5;
8b956130
NC
1274 s = get_name (s, &name);
1275 s = get_name (s, &attribs);
1276
1277 while (loop)
1278 {
1279 switch (*attribs++)
1280 {
1281 case 'W':
1282 had_write = 1;
1283 break;
1284 case 'R':
8b956130
NC
1285 break;
1286 case 'S':
8b956130
NC
1287 break;
1288 case 'X':
1289 had_exec = 1;
1290 break;
1291 default:
1292 loop = 0;
1293 }
1294 }
252b5132 1295 asec = bfd_get_section_by_name (abfd, name);
8b956130
NC
1296 if (asec)
1297 {
1298 if (had_exec)
1299 asec->flags |= SEC_CODE;
1300 if (!had_write)
1301 asec->flags |= SEC_READONLY;
1302 }
252b5132 1303 }
08dedd66 1304 else if (startswith (s, "-heap"))
0112cd26 1305 s = dores_com (s + 5, output_bfd, 1);
8b956130 1306
08dedd66 1307 else if (startswith (s, "-stack"))
0112cd26 1308 s = dores_com (s + 6, output_bfd, 0);
8b956130 1309
c1711530 1310 /* GNU extension for aligned commons. */
08dedd66 1311 else if (startswith (s, "-aligncomm:"))
c1711530
DK
1312 {
1313 /* Common symbols must be aligned on reading, as it
1314 is too late to do anything here, after they have
1315 already been allocated, so just skip the directive. */
1316 s += 11;
1317 }
1318
244148ad 1319 else
252b5132
RH
1320 s++;
1321 }
1322 free (copy);
1323 return 1;
1324}
1325
1326/* Place a marker against all symbols which are used by relocations.
1327 This marker can be picked up by the 'do we skip this symbol ?'
1328 loop in _bfd_coff_link_input_bfd() and used to prevent skipping
8b956130 1329 that symbol. */
252b5132
RH
1330
1331static void
57402f1e 1332mark_relocs (struct coff_final_link_info *flaginfo, bfd *input_bfd)
252b5132
RH
1333{
1334 asection * a;
1335
1336 if ((bfd_get_file_flags (input_bfd) & HAS_SYMS) == 0)
1337 return;
244148ad 1338
252b5132
RH
1339 for (a = input_bfd->sections; a != (asection *) NULL; a = a->next)
1340 {
1341 struct internal_reloc * internal_relocs;
1342 struct internal_reloc * irel;
1343 struct internal_reloc * irelend;
1344
a29a8af8
KT
1345 if ((a->flags & SEC_RELOC) == 0 || a->reloc_count < 1
1346 || a->linker_mark == 0)
252b5132 1347 continue;
a8fcf378
NC
1348 /* Don't mark relocs in excluded sections. */
1349 if (a->output_section == bfd_abs_section_ptr)
1350 continue;
252b5132
RH
1351
1352 /* Read in the relocs. */
1353 internal_relocs = _bfd_coff_read_internal_relocs
0a1b45a2 1354 (input_bfd, a, false,
57402f1e 1355 flaginfo->external_relocs,
0e1862bb
L
1356 bfd_link_relocatable (flaginfo->info),
1357 (bfd_link_relocatable (flaginfo->info)
57402f1e
NC
1358 ? (flaginfo->section_info[ a->output_section->target_index ].relocs + a->output_section->reloc_count)
1359 : flaginfo->internal_relocs)
252b5132 1360 );
244148ad 1361
252b5132
RH
1362 if (internal_relocs == NULL)
1363 continue;
1364
1365 irel = internal_relocs;
1366 irelend = irel + a->reloc_count;
1367
1368 /* Place a mark in the sym_indices array (whose entries have
1369 been initialised to 0) for all of the symbols that are used
1370 in the relocation table. This will then be picked up in the
8b956130 1371 skip/don't-skip pass. */
252b5132 1372 for (; irel < irelend; irel++)
06ab6faf
AM
1373 if ((unsigned long) irel->r_symndx < obj_raw_syment_count (input_bfd))
1374 flaginfo->sym_indices[irel->r_symndx] = -1;
252b5132
RH
1375 }
1376}
1377
1378/* Link an input file into the linker output file. This function
1379 handles all the sections and relocations of the input file at once. */
1380
0a1b45a2 1381bool
57402f1e 1382_bfd_coff_link_input_bfd (struct coff_final_link_info *flaginfo, bfd *input_bfd)
252b5132 1383{
b2d638c7
ILT
1384 unsigned int n_tmask = coff_data (input_bfd)->local_n_tmask;
1385 unsigned int n_btshft = coff_data (input_bfd)->local_n_btshft;
0a1b45a2 1386 bool (*adjust_symndx)
8b956130 1387 (bfd *, struct bfd_link_info *, bfd *, asection *,
0a1b45a2 1388 struct internal_reloc *, bool *);
252b5132
RH
1389 bfd *output_bfd;
1390 const char *strings;
1391 bfd_size_type syment_base;
0a1b45a2 1392 bool copy, hash;
252b5132
RH
1393 bfd_size_type isymesz;
1394 bfd_size_type osymesz;
1395 bfd_size_type linesz;
1396 bfd_byte *esym;
1397 bfd_byte *esym_end;
1398 struct internal_syment *isymp;
1399 asection **secpp;
1400 long *indexp;
1401 unsigned long output_index;
1402 bfd_byte *outsym;
1403 struct coff_link_hash_entry **sym_hash;
1404 asection *o;
1405
1406 /* Move all the symbols to the output file. */
1407
57402f1e 1408 output_bfd = flaginfo->output_bfd;
252b5132
RH
1409 strings = NULL;
1410 syment_base = obj_raw_syment_count (output_bfd);
1411 isymesz = bfd_coff_symesz (input_bfd);
1412 osymesz = bfd_coff_symesz (output_bfd);
1413 linesz = bfd_coff_linesz (input_bfd);
1414 BFD_ASSERT (linesz == bfd_coff_linesz (output_bfd));
1415
0a1b45a2 1416 copy = false;
57402f1e 1417 if (! flaginfo->info->keep_memory)
0a1b45a2
AM
1418 copy = true;
1419 hash = true;
b560e2ac 1420 if (flaginfo->info->traditional_format)
0a1b45a2 1421 hash = false;
252b5132
RH
1422
1423 if (! _bfd_coff_get_external_symbols (input_bfd))
0a1b45a2 1424 return false;
252b5132
RH
1425
1426 esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
1427 esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz;
57402f1e
NC
1428 isymp = flaginfo->internal_syms;
1429 secpp = flaginfo->sec_ptrs;
1430 indexp = flaginfo->sym_indices;
252b5132 1431 output_index = syment_base;
57402f1e 1432 outsym = flaginfo->outsyms;
252b5132 1433
10df41b1 1434 if (obj_pe (output_bfd)
57402f1e 1435 && ! process_embedded_commands (output_bfd, flaginfo->info, input_bfd))
0a1b45a2 1436 return false;
252b5132 1437
8b956130
NC
1438 /* If we are going to perform relocations and also strip/discard some
1439 symbols then we must make sure that we do not strip/discard those
1440 symbols that are going to be involved in the relocations. */
57402f1e
NC
1441 if (( flaginfo->info->strip != strip_none
1442 || flaginfo->info->discard != discard_none)
0e1862bb 1443 && bfd_link_relocatable (flaginfo->info))
252b5132 1444 {
8b956130 1445 /* Mark the symbol array as 'not-used'. */
244148ad
KH
1446 memset (indexp, 0, obj_raw_syment_count (input_bfd) * sizeof * indexp);
1447
57402f1e 1448 mark_relocs (flaginfo, input_bfd);
252b5132
RH
1449 }
1450
1451 while (esym < esym_end)
1452 {
1453 struct internal_syment isym;
5d54c628 1454 enum coff_symbol_classification classification;
0a1b45a2
AM
1455 bool skip;
1456 bool global;
1457 bool dont_skip_symbol;
252b5132
RH
1458 int add;
1459
8b956130 1460 bfd_coff_swap_sym_in (input_bfd, esym, isymp);
252b5132
RH
1461
1462 /* Make a copy of *isymp so that the relocate_section function
1463 always sees the original values. This is more reliable than
1464 always recomputing the symbol value even if we are stripping
1465 the symbol. */
1466 isym = *isymp;
1467
5d54c628
ILT
1468 classification = bfd_coff_classify_symbol (input_bfd, &isym);
1469 switch (classification)
252b5132 1470 {
5d54c628
ILT
1471 default:
1472 abort ();
1473 case COFF_SYMBOL_GLOBAL:
1474 case COFF_SYMBOL_PE_SECTION:
1475 case COFF_SYMBOL_LOCAL:
1476 *secpp = coff_section_from_bfd_index (input_bfd, isym.n_scnum);
1477 break;
1478 case COFF_SYMBOL_COMMON:
1479 *secpp = bfd_com_section_ptr;
1480 break;
1481 case COFF_SYMBOL_UNDEFINED:
1482 *secpp = bfd_und_section_ptr;
1483 break;
252b5132
RH
1484 }
1485
1486 /* Extract the flag indicating if this symbol is used by a
07d6d2b8 1487 relocation. */
57402f1e
NC
1488 if ((flaginfo->info->strip != strip_none
1489 || flaginfo->info->discard != discard_none)
0e1862bb 1490 && bfd_link_relocatable (flaginfo->info))
252b5132
RH
1491 dont_skip_symbol = *indexp;
1492 else
0a1b45a2 1493 dont_skip_symbol = false;
244148ad 1494
252b5132
RH
1495 *indexp = -1;
1496
0a1b45a2
AM
1497 skip = false;
1498 global = false;
252b5132
RH
1499 add = 1 + isym.n_numaux;
1500
1501 /* If we are stripping all symbols, we want to skip this one. */
57402f1e 1502 if (flaginfo->info->strip == strip_all && ! dont_skip_symbol)
0a1b45a2 1503 skip = true;
252b5132
RH
1504
1505 if (! skip)
1506 {
5d54c628 1507 switch (classification)
252b5132 1508 {
5d54c628
ILT
1509 default:
1510 abort ();
1511 case COFF_SYMBOL_GLOBAL:
1512 case COFF_SYMBOL_COMMON:
1513 case COFF_SYMBOL_PE_SECTION:
252b5132
RH
1514 /* This is a global symbol. Global symbols come at the
1515 end of the symbol table, so skip them for now.
1516 Locally defined function symbols, however, are an
1517 exception, and are not moved to the end. */
0a1b45a2 1518 global = true;
5d54c628 1519 if (! ISFCN (isym.n_type))
0a1b45a2 1520 skip = true;
5d54c628
ILT
1521 break;
1522
1523 case COFF_SYMBOL_UNDEFINED:
1524 /* Undefined symbols are left for the end. */
0a1b45a2
AM
1525 global = true;
1526 skip = true;
5d54c628
ILT
1527 break;
1528
1529 case COFF_SYMBOL_LOCAL:
252b5132 1530 /* This is a local symbol. Skip it if we are discarding
07d6d2b8 1531 local symbols. */
57402f1e 1532 if (flaginfo->info->discard == discard_all && ! dont_skip_symbol)
0a1b45a2 1533 skip = true;
5d54c628 1534 break;
252b5132
RH
1535 }
1536 }
1537
440c4607
NC
1538#ifndef COFF_WITH_PE
1539 /* Skip section symbols for sections which are not going to be
862517b6 1540 emitted. */
440c4607 1541 if (!skip
f0803690 1542 && !dont_skip_symbol
440c4607
NC
1543 && isym.n_sclass == C_STAT
1544 && isym.n_type == T_NULL
f0803690
AM
1545 && isym.n_numaux > 0
1546 && ((*secpp)->output_section == bfd_abs_section_ptr
1547 || bfd_section_removed_from_list (output_bfd,
1548 (*secpp)->output_section)))
0a1b45a2 1549 skip = true;
440c4607
NC
1550#endif
1551
252b5132 1552 /* If we stripping debugging symbols, and this is a debugging
07d6d2b8
AM
1553 symbol, then skip it. FIXME: gas sets the section to N_ABS
1554 for some types of debugging symbols; I don't know if this is
1555 a bug or not. In any case, we handle it here. */
252b5132 1556 if (! skip
57402f1e 1557 && flaginfo->info->strip == strip_debugger
252b5132
RH
1558 && ! dont_skip_symbol
1559 && (isym.n_scnum == N_DEBUG
1560 || (isym.n_scnum == N_ABS
1561 && (isym.n_sclass == C_AUTO
1562 || isym.n_sclass == C_REG
1563 || isym.n_sclass == C_MOS
1564 || isym.n_sclass == C_MOE
1565 || isym.n_sclass == C_MOU
1566 || isym.n_sclass == C_ARG
1567 || isym.n_sclass == C_REGPARM
1568 || isym.n_sclass == C_FIELD
1569 || isym.n_sclass == C_EOS))))
0a1b45a2 1570 skip = true;
252b5132
RH
1571
1572 /* If some symbols are stripped based on the name, work out the
1573 name and decide whether to skip this symbol. */
1574 if (! skip
57402f1e
NC
1575 && (flaginfo->info->strip == strip_some
1576 || flaginfo->info->discard == discard_l))
252b5132
RH
1577 {
1578 const char *name;
1579 char buf[SYMNMLEN + 1];
1580
1581 name = _bfd_coff_internal_syment_name (input_bfd, &isym, buf);
1582 if (name == NULL)
0a1b45a2 1583 return false;
252b5132
RH
1584
1585 if (! dont_skip_symbol
57402f1e 1586 && ((flaginfo->info->strip == strip_some
0a1b45a2
AM
1587 && (bfd_hash_lookup (flaginfo->info->keep_hash, name, false,
1588 false) == NULL))
252b5132 1589 || (! global
57402f1e 1590 && flaginfo->info->discard == discard_l
252b5132 1591 && bfd_is_local_label_name (input_bfd, name))))
0a1b45a2 1592 skip = true;
252b5132
RH
1593 }
1594
1595 /* If this is an enum, struct, or union tag, see if we have
07d6d2b8 1596 already output an identical type. */
252b5132 1597 if (! skip
b560e2ac 1598 && !flaginfo->info->traditional_format
252b5132
RH
1599 && (isym.n_sclass == C_ENTAG
1600 || isym.n_sclass == C_STRTAG
1601 || isym.n_sclass == C_UNTAG)
1602 && isym.n_numaux == 1)
1603 {
1604 const char *name;
1605 char buf[SYMNMLEN + 1];
1606 struct coff_debug_merge_hash_entry *mh;
1607 struct coff_debug_merge_type *mt;
1608 union internal_auxent aux;
1609 struct coff_debug_merge_element **epp;
1610 bfd_byte *esl, *eslend;
1611 struct internal_syment *islp;
986f0783 1612 size_t amt;
252b5132
RH
1613
1614 name = _bfd_coff_internal_syment_name (input_bfd, &isym, buf);
1615 if (name == NULL)
0a1b45a2 1616 return false;
252b5132
RH
1617
1618 /* Ignore fake names invented by compiler; treat them all as
07d6d2b8 1619 the same name. */
252b5132 1620 if (*name == '~' || *name == '.' || *name == '$'
8c8145a4
AM
1621 || (*name
1622 && *name == bfd_get_symbol_leading_char (input_bfd)
252b5132
RH
1623 && (name[1] == '~' || name[1] == '.' || name[1] == '$')))
1624 name = "";
1625
57402f1e 1626 mh = coff_debug_merge_hash_lookup (&flaginfo->debug_merge, name,
0a1b45a2 1627 true, true);
252b5132 1628 if (mh == NULL)
0a1b45a2 1629 return false;
252b5132
RH
1630
1631 /* Allocate memory to hold type information. If this turns
07d6d2b8
AM
1632 out to be a duplicate, we pass this address to
1633 bfd_release. */
dc810e39 1634 amt = sizeof (struct coff_debug_merge_type);
a50b1753 1635 mt = (struct coff_debug_merge_type *) bfd_alloc (input_bfd, amt);
252b5132 1636 if (mt == NULL)
0a1b45a2 1637 return false;
96d56e9f 1638 mt->type_class = isym.n_sclass;
252b5132
RH
1639
1640 /* Pick up the aux entry, which points to the end of the tag
07d6d2b8 1641 entries. */
8b956130 1642 bfd_coff_swap_aux_in (input_bfd, (esym + isymesz),
252b5132 1643 isym.n_type, isym.n_sclass, 0, isym.n_numaux,
8b956130 1644 &aux);
252b5132
RH
1645
1646 /* Gather the elements. */
1647 epp = &mt->elements;
1648 mt->elements = NULL;
1649 islp = isymp + 2;
1650 esl = esym + 2 * isymesz;
1651 eslend = ((bfd_byte *) obj_coff_external_syms (input_bfd)
a2c7ca15 1652 + aux.x_sym.x_fcnary.x_fcn.x_endndx.u32 * isymesz);
252b5132
RH
1653 while (esl < eslend)
1654 {
1655 const char *elename;
1656 char elebuf[SYMNMLEN + 1];
00692651 1657 char *name_copy;
252b5132 1658
8b956130 1659 bfd_coff_swap_sym_in (input_bfd, esl, islp);
252b5132 1660
dc810e39 1661 amt = sizeof (struct coff_debug_merge_element);
a50b1753 1662 *epp = (struct coff_debug_merge_element *)
07d6d2b8 1663 bfd_alloc (input_bfd, amt);
252b5132 1664 if (*epp == NULL)
0a1b45a2 1665 return false;
252b5132
RH
1666
1667 elename = _bfd_coff_internal_syment_name (input_bfd, islp,
1668 elebuf);
1669 if (elename == NULL)
0a1b45a2 1670 return false;
252b5132 1671
dc810e39 1672 amt = strlen (elename) + 1;
a50b1753 1673 name_copy = (char *) bfd_alloc (input_bfd, amt);
00692651 1674 if (name_copy == NULL)
0a1b45a2 1675 return false;
00692651 1676 strcpy (name_copy, elename);
252b5132 1677
00692651 1678 (*epp)->name = name_copy;
252b5132
RH
1679 (*epp)->type = islp->n_type;
1680 (*epp)->tagndx = 0;
1681 if (islp->n_numaux >= 1
1682 && islp->n_type != T_NULL
1683 && islp->n_sclass != C_EOS)
1684 {
1685 union internal_auxent eleaux;
1686 long indx;
1687
8b956130 1688 bfd_coff_swap_aux_in (input_bfd, (esl + isymesz),
252b5132 1689 islp->n_type, islp->n_sclass, 0,
8b956130 1690 islp->n_numaux, &eleaux);
a2c7ca15 1691 indx = eleaux.x_sym.x_tagndx.u32;
252b5132
RH
1692
1693 /* FIXME: If this tagndx entry refers to a symbol
1694 defined later in this file, we just ignore it.
1695 Handling this correctly would be tedious, and may
1696 not be required. */
252b5132
RH
1697 if (indx > 0
1698 && (indx
1699 < ((esym -
1700 (bfd_byte *) obj_coff_external_syms (input_bfd))
1701 / (long) isymesz)))
1702 {
57402f1e 1703 (*epp)->tagndx = flaginfo->sym_indices[indx];
252b5132
RH
1704 if ((*epp)->tagndx < 0)
1705 (*epp)->tagndx = 0;
1706 }
1707 }
1708 epp = &(*epp)->next;
1709 *epp = NULL;
1710
1711 esl += (islp->n_numaux + 1) * isymesz;
1712 islp += islp->n_numaux + 1;
1713 }
1714
1715 /* See if we already have a definition which matches this
07d6d2b8
AM
1716 type. We always output the type if it has no elements,
1717 for simplicity. */
252b5132 1718 if (mt->elements == NULL)
8b956130 1719 bfd_release (input_bfd, mt);
252b5132
RH
1720 else
1721 {
1722 struct coff_debug_merge_type *mtl;
1723
1724 for (mtl = mh->types; mtl != NULL; mtl = mtl->next)
1725 {
1726 struct coff_debug_merge_element *me, *mel;
1727
96d56e9f 1728 if (mtl->type_class != mt->type_class)
252b5132
RH
1729 continue;
1730
1731 for (me = mt->elements, mel = mtl->elements;
1732 me != NULL && mel != NULL;
1733 me = me->next, mel = mel->next)
1734 {
1735 if (strcmp (me->name, mel->name) != 0
1736 || me->type != mel->type
1737 || me->tagndx != mel->tagndx)
1738 break;
1739 }
1740
1741 if (me == NULL && mel == NULL)
1742 break;
1743 }
1744
1745 if (mtl == NULL || (bfd_size_type) mtl->indx >= syment_base)
1746 {
1747 /* This is the first definition of this type. */
1748 mt->indx = output_index;
1749 mt->next = mh->types;
1750 mh->types = mt;
1751 }
1752 else
1753 {
1754 /* This is a redefinition which can be merged. */
8b956130 1755 bfd_release (input_bfd, mt);
252b5132
RH
1756 *indexp = mtl->indx;
1757 add = (eslend - esym) / isymesz;
0a1b45a2 1758 skip = true;
252b5132
RH
1759 }
1760 }
1761 }
1762
1763 /* We now know whether we are to skip this symbol or not. */
1764 if (! skip)
1765 {
1766 /* Adjust the symbol in order to output it. */
1767
1768 if (isym._n._n_n._n_zeroes == 0
1769 && isym._n._n_n._n_offset != 0)
1770 {
1771 const char *name;
1772 bfd_size_type indx;
1773
1774 /* This symbol has a long name. Enter it in the string
1775 table we are building. Note that we do not check
1776 bfd_coff_symname_in_debug. That is only true for
1777 XCOFF, and XCOFF requires different linking code
1778 anyhow. */
8b956130 1779 name = _bfd_coff_internal_syment_name (input_bfd, &isym, NULL);
252b5132 1780 if (name == NULL)
0a1b45a2 1781 return false;
57402f1e 1782 indx = _bfd_stringtab_add (flaginfo->strtab, name, hash, copy);
252b5132 1783 if (indx == (bfd_size_type) -1)
0a1b45a2 1784 return false;
252b5132
RH
1785 isym._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
1786 }
1787
8add8e30 1788 switch (isym.n_sclass)
252b5132 1789 {
8add8e30
ILT
1790 case C_AUTO:
1791 case C_MOS:
1792 case C_EOS:
1793 case C_MOE:
1794 case C_MOU:
1795 case C_UNTAG:
1796 case C_STRTAG:
1797 case C_ENTAG:
1798 case C_TPDEF:
1799 case C_ARG:
1800 case C_USTATIC:
1801 case C_REG:
1802 case C_REGPARM:
1803 case C_FIELD:
1804 /* The symbol value should not be modified. */
1805 break;
1806
1807 case C_FCN:
1808 if (obj_pe (input_bfd)
7bd8862c 1809 && memcmp (isym.n_name, ".bf", sizeof ".bf") != 0
8add8e30
ILT
1810 && isym.n_scnum > 0)
1811 {
1812 /* For PE, .lf and .ef get their value left alone,
1813 while .bf gets relocated. However, they all have
1814 "real" section numbers, and need to be moved into
1815 the new section. */
1816 isym.n_scnum = (*secpp)->output_section->target_index;
1817 break;
1818 }
1819 /* Fall through. */
1820 default:
1821 case C_LABEL: /* Not completely sure about these 2 */
1822 case C_EXTDEF:
1823 case C_BLOCK:
1824 case C_EFCN:
1825 case C_NULL:
1826 case C_EXT:
1827 case C_STAT:
1828 case C_SECTION:
1829 case C_NT_WEAK:
1830 /* Compute new symbol location. */
1831 if (isym.n_scnum > 0)
1832 {
1833 isym.n_scnum = (*secpp)->output_section->target_index;
1834 isym.n_value += (*secpp)->output_offset;
1835 if (! obj_pe (input_bfd))
1836 isym.n_value -= (*secpp)->vma;
57402f1e 1837 if (! obj_pe (flaginfo->output_bfd))
8add8e30
ILT
1838 isym.n_value += (*secpp)->output_section->vma;
1839 }
1840 break;
1841
1842 case C_FILE:
1843 /* The value of a C_FILE symbol is the symbol index of
1844 the next C_FILE symbol. The value of the last C_FILE
1845 symbol is the symbol index to the first external
1846 symbol (actually, coff_renumber_symbols does not get
1847 this right--it just sets the value of the last C_FILE
1848 symbol to zero--and nobody has ever complained about
1849 it). We try to get this right, below, just before we
1850 write the symbols out, but in the general case we may
1851 have to write the symbol out twice. */
57402f1e
NC
1852 if (flaginfo->last_file_index != -1
1853 && flaginfo->last_file.n_value != (bfd_vma) output_index)
252b5132 1854 {
8add8e30 1855 /* We must correct the value of the last C_FILE
07d6d2b8 1856 entry. */
57402f1e
NC
1857 flaginfo->last_file.n_value = output_index;
1858 if ((bfd_size_type) flaginfo->last_file_index >= syment_base)
252b5132
RH
1859 {
1860 /* The last C_FILE symbol is in this input file. */
1861 bfd_coff_swap_sym_out (output_bfd,
57402f1e
NC
1862 &flaginfo->last_file,
1863 (flaginfo->outsyms
1864 + ((flaginfo->last_file_index
8b956130
NC
1865 - syment_base)
1866 * osymesz)));
252b5132
RH
1867 }
1868 else
1869 {
dc810e39
AM
1870 file_ptr pos;
1871
252b5132
RH
1872 /* We have already written out the last C_FILE
1873 symbol. We need to write it out again. We
1874 borrow *outsym temporarily. */
1875 bfd_coff_swap_sym_out (output_bfd,
57402f1e 1876 &flaginfo->last_file, outsym);
dc810e39 1877 pos = obj_sym_filepos (output_bfd);
57402f1e 1878 pos += flaginfo->last_file_index * osymesz;
dc810e39 1879 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
226f9f4f 1880 || bfd_write (outsym, osymesz, output_bfd) != osymesz)
0a1b45a2 1881 return false;
252b5132
RH
1882 }
1883 }
1884
57402f1e
NC
1885 flaginfo->last_file_index = output_index;
1886 flaginfo->last_file = isym;
8add8e30 1887 break;
252b5132
RH
1888 }
1889
1890 /* If doing task linking, convert normal global function symbols to
e4202681 1891 static functions. */
57402f1e 1892 if (flaginfo->info->task_link && IS_EXTERNAL (input_bfd, isym))
252b5132
RH
1893 isym.n_sclass = C_STAT;
1894
1895 /* Output the symbol. */
8b956130 1896 bfd_coff_swap_sym_out (output_bfd, &isym, outsym);
252b5132
RH
1897
1898 *indexp = output_index;
1899
1900 if (global)
1901 {
1902 long indx;
1903 struct coff_link_hash_entry *h;
1904
1905 indx = ((esym - (bfd_byte *) obj_coff_external_syms (input_bfd))
1906 / isymesz);
1907 h = obj_coff_sym_hashes (input_bfd)[indx];
1908 if (h == NULL)
1909 {
1910 /* This can happen if there were errors earlier in
07d6d2b8 1911 the link. */
252b5132 1912 bfd_set_error (bfd_error_bad_value);
0a1b45a2 1913 return false;
252b5132
RH
1914 }
1915 h->indx = output_index;
1916 }
1917
1918 output_index += add;
1919 outsym += add * osymesz;
1920 }
1921
1922 esym += add * isymesz;
1923 isymp += add;
1924 ++secpp;
1925 ++indexp;
1926 for (--add; add > 0; --add)
1927 {
1928 *secpp++ = NULL;
1929 *indexp++ = -1;
1930 }
1931 }
1932
1933 /* Fix up the aux entries. This must be done in a separate pass,
1934 because we don't know the correct symbol indices until we have
1935 already decided which symbols we are going to keep. */
252b5132
RH
1936 esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
1937 esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz;
57402f1e
NC
1938 isymp = flaginfo->internal_syms;
1939 indexp = flaginfo->sym_indices;
252b5132 1940 sym_hash = obj_coff_sym_hashes (input_bfd);
57402f1e 1941 outsym = flaginfo->outsyms;
8b956130 1942
252b5132
RH
1943 while (esym < esym_end)
1944 {
1945 int add;
1946
1947 add = 1 + isymp->n_numaux;
1948
1949 if ((*indexp < 0
1950 || (bfd_size_type) *indexp < syment_base)
1951 && (*sym_hash == NULL
1952 || (*sym_hash)->auxbfd != input_bfd))
1953 esym += add * isymesz;
1954 else
1955 {
1956 struct coff_link_hash_entry *h;
1957 int i;
1958
1959 h = NULL;
1960 if (*indexp < 0)
1961 {
1962 h = *sym_hash;
1963
1964 /* The m68k-motorola-sysv assembler will sometimes
07d6d2b8
AM
1965 generate two symbols with the same name, but only one
1966 will have aux entries. */
252b5132 1967 BFD_ASSERT (isymp->n_numaux == 0
cfc9dfb1 1968 || h->numaux == 0
252b5132
RH
1969 || h->numaux == isymp->n_numaux);
1970 }
1971
1972 esym += isymesz;
1973
1974 if (h == NULL)
1975 outsym += osymesz;
1976
1977 /* Handle the aux entries. This handling is based on
1978 coff_pointerize_aux. I don't know if it always correct. */
1979 for (i = 0; i < isymp->n_numaux && esym < esym_end; i++)
1980 {
1981 union internal_auxent aux;
1982 union internal_auxent *auxp;
1983
cfc9dfb1 1984 if (h != NULL && h->aux != NULL && (h->numaux > i))
252b5132
RH
1985 auxp = h->aux + i;
1986 else
1987 {
8b956130
NC
1988 bfd_coff_swap_aux_in (input_bfd, esym, isymp->n_type,
1989 isymp->n_sclass, i, isymp->n_numaux, &aux);
252b5132
RH
1990 auxp = &aux;
1991 }
1992
1993 if (isymp->n_sclass == C_FILE)
1994 {
1995 /* If this is a long filename, we must put it in the
1996 string table. */
e86fc4a5
CC
1997 if (auxp->x_file.x_n.x_n.x_zeroes == 0
1998 && auxp->x_file.x_n.x_n.x_offset != 0)
252b5132
RH
1999 {
2000 const char *filename;
2001 bfd_size_type indx;
2002
e86fc4a5 2003 BFD_ASSERT (auxp->x_file.x_n.x_n.x_offset
252b5132
RH
2004 >= STRING_SIZE_SIZE);
2005 if (strings == NULL)
2006 {
2007 strings = _bfd_coff_read_string_table (input_bfd);
2008 if (strings == NULL)
0a1b45a2 2009 return false;
252b5132 2010 }
e86fc4a5 2011 if ((bfd_size_type) auxp->x_file.x_n.x_n.x_offset >= obj_coff_strings_len (input_bfd))
5a3f568b
NC
2012 filename = _("<corrupt>");
2013 else
e86fc4a5 2014 filename = strings + auxp->x_file.x_n.x_n.x_offset;
57402f1e 2015 indx = _bfd_stringtab_add (flaginfo->strtab, filename,
252b5132
RH
2016 hash, copy);
2017 if (indx == (bfd_size_type) -1)
0a1b45a2 2018 return false;
e86fc4a5 2019 auxp->x_file.x_n.x_n.x_offset = STRING_SIZE_SIZE + indx;
252b5132
RH
2020 }
2021 }
0c429e07
NC
2022 else if ((isymp->n_sclass != C_STAT || isymp->n_type != T_NULL)
2023 && isymp->n_sclass != C_NT_WEAK)
252b5132
RH
2024 {
2025 unsigned long indx;
2026
2027 if (ISFCN (isymp->n_type)
2028 || ISTAG (isymp->n_sclass)
2029 || isymp->n_sclass == C_BLOCK
2030 || isymp->n_sclass == C_FCN)
2031 {
a2c7ca15 2032 indx = auxp->x_sym.x_fcnary.x_fcn.x_endndx.u32;
252b5132
RH
2033 if (indx > 0
2034 && indx < obj_raw_syment_count (input_bfd))
2035 {
2036 /* We look forward through the symbol for
07d6d2b8
AM
2037 the index of the next symbol we are going
2038 to include. I don't know if this is
2039 entirely right. */
57402f1e
NC
2040 while ((flaginfo->sym_indices[indx] < 0
2041 || ((bfd_size_type) flaginfo->sym_indices[indx]
252b5132
RH
2042 < syment_base))
2043 && indx < obj_raw_syment_count (input_bfd))
2044 ++indx;
2045 if (indx >= obj_raw_syment_count (input_bfd))
2046 indx = output_index;
2047 else
57402f1e 2048 indx = flaginfo->sym_indices[indx];
a2c7ca15 2049 auxp->x_sym.x_fcnary.x_fcn.x_endndx.u32 = indx;
252b5132
RH
2050 }
2051 }
2052
a2c7ca15 2053 indx = auxp->x_sym.x_tagndx.u32;
252b5132
RH
2054 if (indx > 0 && indx < obj_raw_syment_count (input_bfd))
2055 {
2056 long symindx;
2057
57402f1e 2058 symindx = flaginfo->sym_indices[indx];
252b5132 2059 if (symindx < 0)
a2c7ca15 2060 auxp->x_sym.x_tagndx.u32 = 0;
252b5132 2061 else
a2c7ca15 2062 auxp->x_sym.x_tagndx.u32 = symindx;
252b5132
RH
2063 }
2064
2065 /* The .bf symbols are supposed to be linked through
2066 the endndx field. We need to carry this list
2067 across object files. */
2068 if (i == 0
2069 && h == NULL
2070 && isymp->n_sclass == C_FCN
2071 && (isymp->_n._n_n._n_zeroes != 0
2072 || isymp->_n._n_n._n_offset == 0)
2073 && isymp->_n._n_name[0] == '.'
2074 && isymp->_n._n_name[1] == 'b'
2075 && isymp->_n._n_name[2] == 'f'
2076 && isymp->_n._n_name[3] == '\0')
2077 {
57402f1e 2078 if (flaginfo->last_bf_index != -1)
252b5132 2079 {
a2c7ca15 2080 flaginfo->last_bf.x_sym.x_fcnary.x_fcn.x_endndx.u32 =
252b5132
RH
2081 *indexp;
2082
57402f1e 2083 if ((bfd_size_type) flaginfo->last_bf_index
252b5132
RH
2084 >= syment_base)
2085 {
8b956130 2086 void *auxout;
252b5132
RH
2087
2088 /* The last .bf symbol is in this input
2089 file. This will only happen if the
2090 assembler did not set up the .bf
2091 endndx symbols correctly. */
57402f1e
NC
2092 auxout = (flaginfo->outsyms
2093 + ((flaginfo->last_bf_index
8b956130
NC
2094 - syment_base)
2095 * osymesz));
2096
252b5132 2097 bfd_coff_swap_aux_out (output_bfd,
57402f1e 2098 &flaginfo->last_bf,
252b5132
RH
2099 isymp->n_type,
2100 isymp->n_sclass,
2101 0, isymp->n_numaux,
2102 auxout);
2103 }
2104 else
2105 {
dc810e39
AM
2106 file_ptr pos;
2107
252b5132 2108 /* We have already written out the last
07d6d2b8
AM
2109 .bf aux entry. We need to write it
2110 out again. We borrow *outsym
2111 temporarily. FIXME: This case should
2112 be made faster. */
252b5132 2113 bfd_coff_swap_aux_out (output_bfd,
57402f1e 2114 &flaginfo->last_bf,
252b5132
RH
2115 isymp->n_type,
2116 isymp->n_sclass,
2117 0, isymp->n_numaux,
8b956130 2118 outsym);
dc810e39 2119 pos = obj_sym_filepos (output_bfd);
57402f1e 2120 pos += flaginfo->last_bf_index * osymesz;
dc810e39 2121 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
226f9f4f 2122 || (bfd_write (outsym, osymesz, output_bfd)
dc810e39 2123 != osymesz))
0a1b45a2 2124 return false;
252b5132
RH
2125 }
2126 }
2127
a2c7ca15 2128 if (auxp->x_sym.x_fcnary.x_fcn.x_endndx.u32 != 0)
57402f1e 2129 flaginfo->last_bf_index = -1;
252b5132
RH
2130 else
2131 {
2132 /* The endndx field of this aux entry must
07d6d2b8
AM
2133 be updated with the symbol number of the
2134 next .bf symbol. */
57402f1e
NC
2135 flaginfo->last_bf = *auxp;
2136 flaginfo->last_bf_index = (((outsym - flaginfo->outsyms)
252b5132
RH
2137 / osymesz)
2138 + syment_base);
2139 }
2140 }
2141 }
2142
2143 if (h == NULL)
2144 {
8b956130 2145 bfd_coff_swap_aux_out (output_bfd, auxp, isymp->n_type,
252b5132 2146 isymp->n_sclass, i, isymp->n_numaux,
8b956130 2147 outsym);
252b5132
RH
2148 outsym += osymesz;
2149 }
2150
2151 esym += isymesz;
2152 }
2153 }
2154
2155 indexp += add;
2156 isymp += add;
2157 sym_hash += add;
2158 }
2159
2160 /* Relocate the line numbers, unless we are stripping them. */
57402f1e
NC
2161 if (flaginfo->info->strip == strip_none
2162 || flaginfo->info->strip == strip_some)
252b5132
RH
2163 {
2164 for (o = input_bfd->sections; o != NULL; o = o->next)
2165 {
2166 bfd_vma offset;
2167 bfd_byte *eline;
2168 bfd_byte *elineend;
b545f675 2169 bfd_byte *oeline;
0a1b45a2 2170 bool skipping;
dc810e39
AM
2171 file_ptr pos;
2172 bfd_size_type amt;
252b5132
RH
2173
2174 /* FIXME: If SEC_HAS_CONTENTS is not for the section, then
2175 build_link_order in ldwrite.c will not have created a
2176 link order, which means that we will not have seen this
2177 input section in _bfd_coff_final_link, which means that
2178 we will not have allocated space for the line numbers of
2179 this section. I don't think line numbers can be
2180 meaningful for a section which does not have
2181 SEC_HAS_CONTENTS set, but, if they do, this must be
2182 changed. */
2183 if (o->lineno_count == 0
2184 || (o->output_section->flags & SEC_HAS_CONTENTS) == 0)
2185 continue;
2186
2187 if (bfd_seek (input_bfd, o->line_filepos, SEEK_SET) != 0
226f9f4f 2188 || bfd_read (flaginfo->linenos, linesz * o->lineno_count,
252b5132 2189 input_bfd) != linesz * o->lineno_count)
0a1b45a2 2190 return false;
252b5132
RH
2191
2192 offset = o->output_section->vma + o->output_offset - o->vma;
57402f1e
NC
2193 eline = flaginfo->linenos;
2194 oeline = flaginfo->linenos;
252b5132 2195 elineend = eline + linesz * o->lineno_count;
0a1b45a2 2196 skipping = false;
252b5132
RH
2197 for (; eline < elineend; eline += linesz)
2198 {
2199 struct internal_lineno iline;
2200
8b956130 2201 bfd_coff_swap_lineno_in (input_bfd, eline, &iline);
252b5132
RH
2202
2203 if (iline.l_lnno != 0)
2204 iline.l_addr.l_paddr += offset;
2205 else if (iline.l_addr.l_symndx >= 0
2206 && ((unsigned long) iline.l_addr.l_symndx
2207 < obj_raw_syment_count (input_bfd)))
2208 {
2209 long indx;
2210
57402f1e 2211 indx = flaginfo->sym_indices[iline.l_addr.l_symndx];
252b5132
RH
2212
2213 if (indx < 0)
2214 {
2215 /* These line numbers are attached to a symbol
b545f675
ILT
2216 which we are stripping. We must discard the
2217 line numbers because reading them back with
2218 no associated symbol (or associating them all
2219 with symbol #0) will fail. We can't regain
2220 the space in the output file, but at least
2221 they're dense. */
0a1b45a2 2222 skipping = true;
252b5132
RH
2223 }
2224 else
2225 {
2226 struct internal_syment is;
2227 union internal_auxent ia;
2228
2229 /* Fix up the lnnoptr field in the aux entry of
2230 the symbol. It turns out that we can't do
2231 this when we modify the symbol aux entries,
2232 because gas sometimes screws up the lnnoptr
2233 field and makes it an offset from the start
2234 of the line numbers rather than an absolute
2235 file index. */
2236 bfd_coff_swap_sym_in (output_bfd,
57402f1e 2237 (flaginfo->outsyms
8b956130
NC
2238 + ((indx - syment_base)
2239 * osymesz)), &is);
252b5132
RH
2240 if ((ISFCN (is.n_type)
2241 || is.n_sclass == C_BLOCK)
2242 && is.n_numaux >= 1)
2243 {
8b956130 2244 void *auxptr;
252b5132 2245
57402f1e 2246 auxptr = (flaginfo->outsyms
8b956130
NC
2247 + ((indx - syment_base + 1)
2248 * osymesz));
252b5132
RH
2249 bfd_coff_swap_aux_in (output_bfd, auxptr,
2250 is.n_type, is.n_sclass,
8b956130 2251 0, is.n_numaux, &ia);
252b5132
RH
2252 ia.x_sym.x_fcnary.x_fcn.x_lnnoptr =
2253 (o->output_section->line_filepos
2254 + o->output_section->lineno_count * linesz
57402f1e 2255 + eline - flaginfo->linenos);
8b956130 2256 bfd_coff_swap_aux_out (output_bfd, &ia,
252b5132
RH
2257 is.n_type, is.n_sclass, 0,
2258 is.n_numaux, auxptr);
2259 }
b545f675 2260
0a1b45a2 2261 skipping = false;
252b5132
RH
2262 }
2263
2264 iline.l_addr.l_symndx = indx;
2265 }
2266
b545f675 2267 if (!skipping)
07d6d2b8 2268 {
8b956130 2269 bfd_coff_swap_lineno_out (output_bfd, &iline, oeline);
b545f675
ILT
2270 oeline += linesz;
2271 }
252b5132
RH
2272 }
2273
dc810e39
AM
2274 pos = o->output_section->line_filepos;
2275 pos += o->output_section->lineno_count * linesz;
57402f1e 2276 amt = oeline - flaginfo->linenos;
dc810e39 2277 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
226f9f4f 2278 || bfd_write (flaginfo->linenos, amt, output_bfd) != amt)
0a1b45a2 2279 return false;
252b5132 2280
dc810e39 2281 o->output_section->lineno_count += amt / linesz;
252b5132
RH
2282 }
2283 }
2284
2285 /* If we swapped out a C_FILE symbol, guess that the next C_FILE
2286 symbol will be the first symbol in the next input file. In the
2287 normal case, this will save us from writing out the C_FILE symbol
2288 again. */
57402f1e
NC
2289 if (flaginfo->last_file_index != -1
2290 && (bfd_size_type) flaginfo->last_file_index >= syment_base)
252b5132 2291 {
57402f1e
NC
2292 flaginfo->last_file.n_value = output_index;
2293 bfd_coff_swap_sym_out (output_bfd, &flaginfo->last_file,
2294 (flaginfo->outsyms
2295 + ((flaginfo->last_file_index - syment_base)
8b956130 2296 * osymesz)));
252b5132
RH
2297 }
2298
2299 /* Write the modified symbols to the output file. */
57402f1e 2300 if (outsym > flaginfo->outsyms)
252b5132 2301 {
dc810e39
AM
2302 file_ptr pos;
2303 bfd_size_type amt;
2304
2305 pos = obj_sym_filepos (output_bfd) + syment_base * osymesz;
57402f1e 2306 amt = outsym - flaginfo->outsyms;
dc810e39 2307 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
226f9f4f 2308 || bfd_write (flaginfo->outsyms, amt, output_bfd) != amt)
0a1b45a2 2309 return false;
252b5132
RH
2310
2311 BFD_ASSERT ((obj_raw_syment_count (output_bfd)
57402f1e 2312 + (outsym - flaginfo->outsyms) / osymesz)
252b5132
RH
2313 == output_index);
2314
2315 obj_raw_syment_count (output_bfd) = output_index;
2316 }
2317
2318 /* Relocate the contents of each section. */
2319 adjust_symndx = coff_backend_info (input_bfd)->_bfd_coff_adjust_symndx;
2320 for (o = input_bfd->sections; o != NULL; o = o->next)
2321 {
2322 bfd_byte *contents;
2323 struct coff_section_tdata *secdata;
2324
2325 if (! o->linker_mark)
8b956130
NC
2326 /* This section was omitted from the link. */
2327 continue;
252b5132 2328
3722b82f
AM
2329 if ((o->flags & SEC_LINKER_CREATED) != 0)
2330 continue;
2331
252b5132 2332 if ((o->flags & SEC_HAS_CONTENTS) == 0
eea6121a 2333 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
252b5132
RH
2334 {
2335 if ((o->flags & SEC_RELOC) != 0
2336 && o->reloc_count != 0)
2337 {
4eca0228 2338 _bfd_error_handler
695344c0 2339 /* xgettext: c-format */
871b3ab2 2340 (_("%pB: relocs in section `%pA', but it has no contents"),
d003868e 2341 input_bfd, o);
252b5132 2342 bfd_set_error (bfd_error_no_contents);
0a1b45a2 2343 return false;
252b5132
RH
2344 }
2345
2346 continue;
2347 }
2348
2349 secdata = coff_section_data (input_bfd, o);
2350 if (secdata != NULL && secdata->contents != NULL)
2351 contents = secdata->contents;
2352 else
2353 {
57402f1e 2354 contents = flaginfo->contents;
a29a8af8 2355 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
0a1b45a2 2356 return false;
252b5132
RH
2357 }
2358
2359 if ((o->flags & SEC_RELOC) != 0)
2360 {
2361 int target_index;
2362 struct internal_reloc *internal_relocs;
2363 struct internal_reloc *irel;
2364
2365 /* Read in the relocs. */
2366 target_index = o->output_section->target_index;
2367 internal_relocs = (_bfd_coff_read_internal_relocs
0a1b45a2 2368 (input_bfd, o, false, flaginfo->external_relocs,
0e1862bb
L
2369 bfd_link_relocatable (flaginfo->info),
2370 (bfd_link_relocatable (flaginfo->info)
57402f1e 2371 ? (flaginfo->section_info[target_index].relocs
252b5132 2372 + o->output_section->reloc_count)
57402f1e 2373 : flaginfo->internal_relocs)));
a29a8af8
KT
2374 if (internal_relocs == NULL
2375 && o->reloc_count > 0)
0a1b45a2 2376 return false;
252b5132 2377
730035f7
DK
2378 /* Run through the relocs looking for relocs against symbols
2379 coming from discarded sections and complain about them. */
2380 irel = internal_relocs;
2381 for (; irel < &internal_relocs[o->reloc_count]; irel++)
2382 {
2383 struct coff_link_hash_entry *h;
2384 asection *ps = NULL;
2385 long symndx = irel->r_symndx;
2386 if (symndx < 0)
2387 continue;
2388 h = obj_coff_sym_hashes (input_bfd)[symndx];
2389 if (h == NULL)
2390 continue;
2391 while (h->root.type == bfd_link_hash_indirect
2392 || h->root.type == bfd_link_hash_warning)
2393 h = (struct coff_link_hash_entry *) h->root.u.i.link;
2394 if (h->root.type == bfd_link_hash_defined
2395 || h->root.type == bfd_link_hash_defweak)
2396 ps = h->root.u.def.section;
2397 if (ps == NULL)
2398 continue;
2399 /* Complain if definition comes from an excluded section. */
2400 if (ps->flags & SEC_EXCLUDE)
57402f1e 2401 (*flaginfo->info->callbacks->einfo)
695344c0 2402 /* xgettext: c-format */
871b3ab2
AM
2403 (_("%X`%s' referenced in section `%pA' of %pB: "
2404 "defined in discarded section `%pA' of %pB\n"),
730035f7
DK
2405 h->root.root.string, o, input_bfd, ps, ps->owner);
2406 }
2407
252b5132 2408 /* Call processor specific code to relocate the section
07d6d2b8 2409 contents. */
57402f1e 2410 if (! bfd_coff_relocate_section (output_bfd, flaginfo->info,
252b5132
RH
2411 input_bfd, o,
2412 contents,
2413 internal_relocs,
57402f1e
NC
2414 flaginfo->internal_syms,
2415 flaginfo->sec_ptrs))
0a1b45a2 2416 return false;
252b5132 2417
0e1862bb 2418 if (bfd_link_relocatable (flaginfo->info))
252b5132
RH
2419 {
2420 bfd_vma offset;
2421 struct internal_reloc *irelend;
2422 struct coff_link_hash_entry **rel_hash;
2423
2424 offset = o->output_section->vma + o->output_offset - o->vma;
2425 irel = internal_relocs;
2426 irelend = irel + o->reloc_count;
57402f1e 2427 rel_hash = (flaginfo->section_info[target_index].rel_hashes
252b5132
RH
2428 + o->output_section->reloc_count);
2429 for (; irel < irelend; irel++, rel_hash++)
2430 {
2431 struct coff_link_hash_entry *h;
0a1b45a2 2432 bool adjusted;
252b5132
RH
2433
2434 *rel_hash = NULL;
2435
2436 /* Adjust the reloc address and symbol index. */
252b5132
RH
2437 irel->r_vaddr += offset;
2438
2439 if (irel->r_symndx == -1)
2440 continue;
2441
2442 if (adjust_symndx)
2443 {
57402f1e 2444 if (! (*adjust_symndx) (output_bfd, flaginfo->info,
252b5132
RH
2445 input_bfd, o, irel,
2446 &adjusted))
0a1b45a2 2447 return false;
252b5132
RH
2448 if (adjusted)
2449 continue;
2450 }
2451
2452 h = obj_coff_sym_hashes (input_bfd)[irel->r_symndx];
2453 if (h != NULL)
2454 {
2455 /* This is a global symbol. */
2456 if (h->indx >= 0)
2457 irel->r_symndx = h->indx;
2458 else
2459 {
2460 /* This symbol is being written at the end
2461 of the file, and we do not yet know the
2462 symbol index. We save the pointer to the
2463 hash table entry in the rel_hash list.
2464 We set the indx field to -2 to indicate
2465 that this symbol must not be stripped. */
2466 *rel_hash = h;
2467 h->indx = -2;
2468 }
2469 }
2470 else
2471 {
2472 long indx;
2473
57402f1e 2474 indx = flaginfo->sym_indices[irel->r_symndx];
252b5132
RH
2475 if (indx != -1)
2476 irel->r_symndx = indx;
2477 else
2478 {
2479 struct internal_syment *is;
2480 const char *name;
2481 char buf[SYMNMLEN + 1];
2482
2483 /* This reloc is against a symbol we are
07d6d2b8 2484 stripping. This should have been handled
252b5132 2485 by the 'dont_skip_symbol' code in the while
244148ad 2486 loop at the top of this function. */
57402f1e 2487 is = flaginfo->internal_syms + irel->r_symndx;
252b5132
RH
2488
2489 name = (_bfd_coff_internal_syment_name
2490 (input_bfd, is, buf));
2491 if (name == NULL)
0a1b45a2 2492 return false;
252b5132 2493
1a72702b
AM
2494 (*flaginfo->info->callbacks->unattached_reloc)
2495 (flaginfo->info, name, input_bfd, o, irel->r_vaddr);
252b5132
RH
2496 }
2497 }
2498 }
2499
2500 o->output_section->reloc_count += o->reloc_count;
2501 }
2502 }
2503
2504 /* Write out the modified section contents. */
2505 if (secdata == NULL || secdata->stab_info == NULL)
2506 {
61826503 2507 file_ptr loc = (o->output_offset
bb294208 2508 * bfd_octets_per_byte (output_bfd, o));
252b5132 2509 if (! bfd_set_section_contents (output_bfd, o->output_section,
eea6121a 2510 contents, loc, o->size))
0a1b45a2 2511 return false;
252b5132
RH
2512 }
2513 else
2514 {
2515 if (! (_bfd_write_section_stabs
57402f1e 2516 (output_bfd, &coff_hash_table (flaginfo->info)->stab_info,
252b5132 2517 o, &secdata->stab_info, contents)))
0a1b45a2 2518 return false;
252b5132
RH
2519 }
2520 }
2521
57402f1e 2522 if (! flaginfo->info->keep_memory
8b956130 2523 && ! _bfd_coff_free_symbols (input_bfd))
0a1b45a2 2524 return false;
252b5132 2525
0a1b45a2 2526 return true;
252b5132
RH
2527}
2528
7686d77d 2529/* Write out a global symbol. Called via bfd_hash_traverse. */
252b5132 2530
0a1b45a2 2531bool
7686d77d 2532_bfd_coff_write_global_sym (struct bfd_hash_entry *bh, void *data)
252b5132 2533{
7686d77d 2534 struct coff_link_hash_entry *h = (struct coff_link_hash_entry *) bh;
57402f1e 2535 struct coff_final_link_info *flaginfo = (struct coff_final_link_info *) data;
252b5132
RH
2536 bfd *output_bfd;
2537 struct internal_syment isym;
2538 bfd_size_type symesz;
2539 unsigned int i;
dc810e39 2540 file_ptr pos;
252b5132 2541
57402f1e 2542 output_bfd = flaginfo->output_bfd;
252b5132 2543
e92d460e
AM
2544 if (h->root.type == bfd_link_hash_warning)
2545 {
2546 h = (struct coff_link_hash_entry *) h->root.u.i.link;
2547 if (h->root.type == bfd_link_hash_new)
0a1b45a2 2548 return true;
e92d460e
AM
2549 }
2550
252b5132 2551 if (h->indx >= 0)
0a1b45a2 2552 return true;
252b5132
RH
2553
2554 if (h->indx != -2
57402f1e
NC
2555 && (flaginfo->info->strip == strip_all
2556 || (flaginfo->info->strip == strip_some
2557 && (bfd_hash_lookup (flaginfo->info->keep_hash,
0a1b45a2 2558 h->root.root.string, false, false)
252b5132 2559 == NULL))))
0a1b45a2 2560 return true;
252b5132
RH
2561
2562 switch (h->root.type)
2563 {
2564 default:
2565 case bfd_link_hash_new:
e92d460e 2566 case bfd_link_hash_warning:
252b5132 2567 abort ();
0a1b45a2 2568 return false;
252b5132
RH
2569
2570 case bfd_link_hash_undefined:
75cfe082 2571 if (h->indx == -3)
0a1b45a2 2572 return true;
75cfe082 2573 /* Fall through. */
252b5132
RH
2574 case bfd_link_hash_undefweak:
2575 isym.n_scnum = N_UNDEF;
2576 isym.n_value = 0;
2577 break;
2578
2579 case bfd_link_hash_defined:
2580 case bfd_link_hash_defweak:
2581 {
2582 asection *sec;
2583
2584 sec = h->root.u.def.section->output_section;
2585 if (bfd_is_abs_section (sec))
2586 isym.n_scnum = N_ABS;
2587 else
2588 isym.n_scnum = sec->target_index;
2589 isym.n_value = (h->root.u.def.value
2590 + h->root.u.def.section->output_offset);
57402f1e 2591 if (! obj_pe (flaginfo->output_bfd))
252b5132 2592 isym.n_value += sec->vma;
67b0f684
JB
2593#ifdef BFD64
2594 if (isym.n_value > (bfd_vma) 0xffffffff)
2595 {
2596 if (! h->root.linker_def)
f493c217
AM
2597 _bfd_error_handler
2598 (_("%pB: stripping non-representable symbol '%s' "
2599 "(value 0x%" PRIx64 ")"),
2600 output_bfd, h->root.root.string, isym.n_value);
0a1b45a2 2601 return true;
67b0f684
JB
2602 }
2603#endif
252b5132
RH
2604 }
2605 break;
2606
2607 case bfd_link_hash_common:
2608 isym.n_scnum = N_UNDEF;
2609 isym.n_value = h->root.u.c.size;
2610 break;
2611
2612 case bfd_link_hash_indirect:
252b5132 2613 /* Just ignore these. They can't be handled anyhow. */
0a1b45a2 2614 return true;
252b5132
RH
2615 }
2616
2617 if (strlen (h->root.root.string) <= SYMNMLEN)
2618 strncpy (isym._n._n_name, h->root.root.string, SYMNMLEN);
2619 else
2620 {
0a1b45a2 2621 bool hash;
252b5132
RH
2622 bfd_size_type indx;
2623
0a1b45a2 2624 hash = true;
b560e2ac 2625 if (flaginfo->info->traditional_format)
0a1b45a2 2626 hash = false;
57402f1e 2627 indx = _bfd_stringtab_add (flaginfo->strtab, h->root.root.string, hash,
0a1b45a2 2628 false);
252b5132
RH
2629 if (indx == (bfd_size_type) -1)
2630 {
0a1b45a2
AM
2631 flaginfo->failed = true;
2632 return false;
252b5132
RH
2633 }
2634 isym._n._n_n._n_zeroes = 0;
2635 isym._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
2636 }
2637
96d56e9f 2638 isym.n_sclass = h->symbol_class;
252b5132
RH
2639 isym.n_type = h->type;
2640
2641 if (isym.n_sclass == C_NULL)
2642 isym.n_sclass = C_EXT;
2643
2644 /* If doing task linking and this is the pass where we convert
2645 defined globals to statics, then do that conversion now. If the
2646 symbol is not being converted, just ignore it and it will be
e4202681 2647 output during a later pass. */
57402f1e 2648 if (flaginfo->global_to_static)
252b5132 2649 {
e4202681 2650 if (! IS_EXTERNAL (output_bfd, isym))
0a1b45a2 2651 return true;
e4202681 2652
252b5132
RH
2653 isym.n_sclass = C_STAT;
2654 }
2655
5c4491d3 2656 /* When a weak symbol is not overridden by a strong one,
e4202681 2657 turn it into an external symbol when not building a
1049f94e 2658 shared or relocatable object. */
0e1862bb
L
2659 if (! bfd_link_pic (flaginfo->info)
2660 && ! bfd_link_relocatable (flaginfo->info)
57402f1e 2661 && IS_WEAK_EXTERNAL (flaginfo->output_bfd, isym))
e4202681
NC
2662 isym.n_sclass = C_EXT;
2663
252b5132 2664 isym.n_numaux = h->numaux;
244148ad 2665
57402f1e 2666 bfd_coff_swap_sym_out (output_bfd, &isym, flaginfo->outsyms);
252b5132
RH
2667
2668 symesz = bfd_coff_symesz (output_bfd);
2669
dc810e39
AM
2670 pos = obj_sym_filepos (output_bfd);
2671 pos += obj_raw_syment_count (output_bfd) * symesz;
2672 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
226f9f4f 2673 || bfd_write (flaginfo->outsyms, symesz, output_bfd) != symesz)
252b5132 2674 {
0a1b45a2
AM
2675 flaginfo->failed = true;
2676 return false;
252b5132
RH
2677 }
2678
2679 h->indx = obj_raw_syment_count (output_bfd);
2680
2681 ++obj_raw_syment_count (output_bfd);
2682
2c546cd8
ILT
2683 /* Write out any associated aux entries. Most of the aux entries
2684 will have been modified in _bfd_coff_link_input_bfd. We have to
2685 handle section aux entries here, now that we have the final
2686 relocation and line number counts. */
252b5132
RH
2687 for (i = 0; i < isym.n_numaux; i++)
2688 {
2c546cd8
ILT
2689 union internal_auxent *auxp;
2690
2691 auxp = h->aux + i;
2692
2693 /* Look for a section aux entry here using the same tests that
07d6d2b8 2694 coff_swap_aux_out uses. */
2c546cd8
ILT
2695 if (i == 0
2696 && (isym.n_sclass == C_STAT
2697 || isym.n_sclass == C_HIDDEN)
2698 && isym.n_type == T_NULL
2699 && (h->root.type == bfd_link_hash_defined
2700 || h->root.type == bfd_link_hash_defweak))
2701 {
2702 asection *sec;
2703
2704 sec = h->root.u.def.section->output_section;
2705 if (sec != NULL)
2706 {
eea6121a 2707 auxp->x_scn.x_scnlen = sec->size;
2c546cd8
ILT
2708
2709 /* For PE, an overflow on the final link reportedly does
07d6d2b8 2710 not matter. FIXME: Why not? */
2c546cd8
ILT
2711 if (sec->reloc_count > 0xffff
2712 && (! obj_pe (output_bfd)
0e1862bb 2713 || bfd_link_relocatable (flaginfo->info)))
4eca0228 2714 _bfd_error_handler
695344c0 2715 /* xgettext: c-format */
871b3ab2 2716 (_("%pB: %pA: reloc overflow: %#x > 0xffff"),
dae82561 2717 output_bfd, sec, sec->reloc_count);
2c546cd8
ILT
2718
2719 if (sec->lineno_count > 0xffff
2720 && (! obj_pe (output_bfd)
0e1862bb 2721 || bfd_link_relocatable (flaginfo->info)))
4eca0228 2722 _bfd_error_handler
695344c0 2723 /* xgettext: c-format */
871b3ab2 2724 (_("%pB: warning: %pA: line number overflow: %#x > 0xffff"),
dae82561 2725 output_bfd, sec, sec->lineno_count);
2c546cd8
ILT
2726
2727 auxp->x_scn.x_nreloc = sec->reloc_count;
2728 auxp->x_scn.x_nlinno = sec->lineno_count;
2729 auxp->x_scn.x_checksum = 0;
2730 auxp->x_scn.x_associated = 0;
2731 auxp->x_scn.x_comdat = 0;
2732 }
2733 }
2734
8b956130 2735 bfd_coff_swap_aux_out (output_bfd, auxp, isym.n_type,
dc810e39 2736 isym.n_sclass, (int) i, isym.n_numaux,
57402f1e 2737 flaginfo->outsyms);
226f9f4f 2738 if (bfd_write (flaginfo->outsyms, symesz, output_bfd) != symesz)
252b5132 2739 {
0a1b45a2
AM
2740 flaginfo->failed = true;
2741 return false;
252b5132
RH
2742 }
2743 ++obj_raw_syment_count (output_bfd);
2744 }
2745
0a1b45a2 2746 return true;
252b5132
RH
2747}
2748
2749/* Write out task global symbols, converting them to statics. Called
2750 via coff_link_hash_traverse. Calls bfd_coff_write_global_sym to do
244148ad 2751 the dirty work, if the symbol we are processing needs conversion. */
252b5132 2752
0a1b45a2 2753bool
8b956130 2754_bfd_coff_write_task_globals (struct coff_link_hash_entry *h, void *data)
252b5132 2755{
57402f1e 2756 struct coff_final_link_info *flaginfo = (struct coff_final_link_info *) data;
0a1b45a2
AM
2757 bool rtnval = true;
2758 bool save_global_to_static;
252b5132 2759
e92d460e
AM
2760 if (h->root.type == bfd_link_hash_warning)
2761 h = (struct coff_link_hash_entry *) h->root.u.i.link;
2762
252b5132
RH
2763 if (h->indx < 0)
2764 {
2765 switch (h->root.type)
2766 {
2767 case bfd_link_hash_defined:
2768 case bfd_link_hash_defweak:
57402f1e 2769 save_global_to_static = flaginfo->global_to_static;
0a1b45a2 2770 flaginfo->global_to_static = true;
7686d77d 2771 rtnval = _bfd_coff_write_global_sym (&h->root.root, data);
57402f1e 2772 flaginfo->global_to_static = save_global_to_static;
252b5132
RH
2773 break;
2774 default:
2775 break;
2776 }
2777 }
2778 return (rtnval);
2779}
2780
2781/* Handle a link order which is supposed to generate a reloc. */
2782
0a1b45a2 2783bool
8b956130 2784_bfd_coff_reloc_link_order (bfd *output_bfd,
57402f1e 2785 struct coff_final_link_info *flaginfo,
8b956130
NC
2786 asection *output_section,
2787 struct bfd_link_order *link_order)
252b5132
RH
2788{
2789 reloc_howto_type *howto;
2790 struct internal_reloc *irel;
2791 struct coff_link_hash_entry **rel_hash_ptr;
2792
2793 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
2794 if (howto == NULL)
2795 {
2796 bfd_set_error (bfd_error_bad_value);
0a1b45a2 2797 return false;
252b5132
RH
2798 }
2799
2800 if (link_order->u.reloc.p->addend != 0)
2801 {
2802 bfd_size_type size;
2803 bfd_byte *buf;
2804 bfd_reloc_status_type rstat;
0a1b45a2 2805 bool ok;
dc810e39 2806 file_ptr loc;
252b5132
RH
2807
2808 size = bfd_get_reloc_size (howto);
a50b1753 2809 buf = (bfd_byte *) bfd_zmalloc (size);
6346d5ca 2810 if (buf == NULL && size != 0)
0a1b45a2 2811 return false;
252b5132
RH
2812
2813 rstat = _bfd_relocate_contents (howto, output_bfd,
bb294208 2814 (bfd_vma) link_order->u.reloc.p->addend,
dc810e39 2815 buf);
252b5132
RH
2816 switch (rstat)
2817 {
2818 case bfd_reloc_ok:
2819 break;
2820 default:
2821 case bfd_reloc_outofrange:
2822 abort ();
2823 case bfd_reloc_overflow:
1a72702b
AM
2824 (*flaginfo->info->callbacks->reloc_overflow)
2825 (flaginfo->info, NULL,
2826 (link_order->type == bfd_section_reloc_link_order
fd361982 2827 ? bfd_section_name (link_order->u.reloc.p->u.section)
1a72702b
AM
2828 : link_order->u.reloc.p->u.name),
2829 howto->name, link_order->u.reloc.p->addend,
2830 (bfd *) NULL, (asection *) NULL, (bfd_vma) 0);
252b5132
RH
2831 break;
2832 }
bb294208
AM
2833 loc = link_order->offset * bfd_octets_per_byte (output_bfd,
2834 output_section);
8b956130 2835 ok = bfd_set_section_contents (output_bfd, output_section, buf,
07d6d2b8 2836 loc, size);
252b5132
RH
2837 free (buf);
2838 if (! ok)
0a1b45a2 2839 return false;
252b5132
RH
2840 }
2841
2842 /* Store the reloc information in the right place. It will get
2843 swapped and written out at the end of the final_link routine. */
57402f1e 2844 irel = (flaginfo->section_info[output_section->target_index].relocs
252b5132 2845 + output_section->reloc_count);
57402f1e 2846 rel_hash_ptr = (flaginfo->section_info[output_section->target_index].rel_hashes
252b5132
RH
2847 + output_section->reloc_count);
2848
2849 memset (irel, 0, sizeof (struct internal_reloc));
2850 *rel_hash_ptr = NULL;
2851
2852 irel->r_vaddr = output_section->vma + link_order->offset;
2853
2854 if (link_order->type == bfd_section_reloc_link_order)
2855 {
2856 /* We need to somehow locate a symbol in the right section. The
07d6d2b8
AM
2857 symbol must either have a value of zero, or we must adjust
2858 the addend by the value of the symbol. FIXME: Write this
2859 when we need it. The old linker couldn't handle this anyhow. */
252b5132
RH
2860 abort ();
2861 *rel_hash_ptr = NULL;
2862 irel->r_symndx = 0;
2863 }
2864 else
2865 {
2866 struct coff_link_hash_entry *h;
2867
2868 h = ((struct coff_link_hash_entry *)
57402f1e 2869 bfd_wrapped_link_hash_lookup (output_bfd, flaginfo->info,
252b5132 2870 link_order->u.reloc.p->u.name,
0a1b45a2 2871 false, false, true));
252b5132
RH
2872 if (h != NULL)
2873 {
2874 if (h->indx >= 0)
2875 irel->r_symndx = h->indx;
2876 else
2877 {
2878 /* Set the index to -2 to force this symbol to get
2879 written out. */
2880 h->indx = -2;
2881 *rel_hash_ptr = h;
2882 irel->r_symndx = 0;
2883 }
2884 }
2885 else
2886 {
1a72702b
AM
2887 (*flaginfo->info->callbacks->unattached_reloc)
2888 (flaginfo->info, link_order->u.reloc.p->u.name,
2889 (bfd *) NULL, (asection *) NULL, (bfd_vma) 0);
252b5132
RH
2890 irel->r_symndx = 0;
2891 }
2892 }
2893
2894 /* FIXME: Is this always right? */
2895 irel->r_type = howto->type;
2896
2897 /* r_size is only used on the RS/6000, which needs its own linker
2898 routines anyhow. r_extern is only used for ECOFF. */
2899
2900 /* FIXME: What is the right value for r_offset? Is zero OK? */
252b5132
RH
2901 ++output_section->reloc_count;
2902
0a1b45a2 2903 return true;
252b5132
RH
2904}
2905
2906/* A basic reloc handling routine which may be used by processors with
2907 simple relocs. */
2908
0a1b45a2 2909bool
8b956130
NC
2910_bfd_coff_generic_relocate_section (bfd *output_bfd,
2911 struct bfd_link_info *info,
2912 bfd *input_bfd,
2913 asection *input_section,
2914 bfd_byte *contents,
2915 struct internal_reloc *relocs,
2916 struct internal_syment *syms,
2917 asection **sections)
252b5132
RH
2918{
2919 struct internal_reloc *rel;
2920 struct internal_reloc *relend;
2921
2922 rel = relocs;
2923 relend = rel + input_section->reloc_count;
2924 for (; rel < relend; rel++)
2925 {
2926 long symndx;
2927 struct coff_link_hash_entry *h;
2928 struct internal_syment *sym;
2929 bfd_vma addend;
2930 bfd_vma val;
3a062fa1 2931 asection *sec;
252b5132
RH
2932 reloc_howto_type *howto;
2933 bfd_reloc_status_type rstat;
2934
2935 symndx = rel->r_symndx;
2936
2937 if (symndx == -1)
2938 {
2939 h = NULL;
2940 sym = NULL;
2941 }
75987f83
ILT
2942 else if (symndx < 0
2943 || (unsigned long) symndx >= obj_raw_syment_count (input_bfd))
2944 {
4eca0228 2945 _bfd_error_handler
695344c0 2946 /* xgettext: c-format */
871b3ab2 2947 (_("%pB: illegal symbol index %ld in relocs"), input_bfd, symndx);
0a1b45a2 2948 return false;
75987f83 2949 }
252b5132 2950 else
244148ad 2951 {
252b5132
RH
2952 h = obj_coff_sym_hashes (input_bfd)[symndx];
2953 sym = syms + symndx;
2954 }
2955
2956 /* COFF treats common symbols in one of two ways. Either the
07d6d2b8
AM
2957 size of the symbol is included in the section contents, or it
2958 is not. We assume that the size is not included, and force
2959 the rtype_to_howto function to adjust the addend as needed. */
252b5132
RH
2960 if (sym != NULL && sym->n_scnum != 0)
2961 addend = - sym->n_value;
2962 else
2963 addend = 0;
2964
252b5132
RH
2965 howto = bfd_coff_rtype_to_howto (input_bfd, input_section, rel, h,
2966 sym, &addend);
2967 if (howto == NULL)
0a1b45a2 2968 return false;
252b5132 2969
1049f94e 2970 /* If we are doing a relocatable link, then we can just ignore
07d6d2b8
AM
2971 a PC relative reloc that is pcrel_offset. It will already
2972 have the correct value. If this is not a relocatable link,
2973 then we should ignore the symbol value. */
252b5132
RH
2974 if (howto->pc_relative && howto->pcrel_offset)
2975 {
0e1862bb 2976 if (bfd_link_relocatable (info))
252b5132
RH
2977 continue;
2978 if (sym != NULL && sym->n_scnum != 0)
2979 addend += sym->n_value;
2980 }
2981
2982 val = 0;
3a062fa1 2983 sec = NULL;
252b5132
RH
2984 if (h == NULL)
2985 {
252b5132
RH
2986 if (symndx == -1)
2987 {
2988 sec = bfd_abs_section_ptr;
2989 val = 0;
2990 }
2991 else
2992 {
2993 sec = sections[symndx];
26c62da0
LW
2994
2995 /* PR 19623: Relocations against symbols in
38c917d8
AM
2996 the absolute sections should ignored.
2997 PR 29807: Also ignore relocs against file symbols or
2998 other such nonsense in fuzzed objects. */
2999 if (sec == NULL || bfd_is_abs_section (sec))
26c62da0
LW
3000 continue;
3001
07d6d2b8 3002 val = (sec->output_section->vma
252b5132
RH
3003 + sec->output_offset
3004 + sym->n_value);
3005 if (! obj_pe (input_bfd))
3006 val -= sec->vma;
3007 }
3008 }
3009 else
3010 {
3011 if (h->root.type == bfd_link_hash_defined
f3d8ae90 3012 /* Defined weak symbols are a GNU extension. */
252b5132
RH
3013 || h->root.type == bfd_link_hash_defweak)
3014 {
252b5132 3015 sec = h->root.u.def.section;
f3d8ae90 3016 BFD_ASSERT (sec->output_section != NULL);
252b5132
RH
3017 val = (h->root.u.def.value
3018 + sec->output_section->vma
3019 + sec->output_offset);
c87db184 3020 }
252b5132 3021
bc7a577d 3022 else if (h->root.type == bfd_link_hash_undefweak)
c87db184 3023 {
07d6d2b8 3024 if (h->symbol_class == C_NT_WEAK && h->numaux == 1)
c87db184
CF
3025 {
3026 /* See _Microsoft Portable Executable and Common Object
07d6d2b8 3027 File Format Specification_, section 5.5.3.
0c429e07
NC
3028 Note that weak symbols without aux records are a GNU
3029 extension.
3030 FIXME: All weak externals are treated as having
27198fa9
DS
3031 characteristic IMAGE_WEAK_EXTERN_SEARCH_NOLIBRARY (1).
3032 These behave as per SVR4 ABI: A library member
3033 will resolve a weak external only if a normal
3034 external causes the library member to be linked.
3035 See also linker.c: generic_link_check_archive_element. */
c87db184 3036 struct coff_link_hash_entry *h2 =
a2c7ca15
AM
3037 h->auxbfd->tdata.coff_obj_data->sym_hashes
3038 [h->aux->x_sym.x_tagndx.u32];
c87db184 3039
0c429e07
NC
3040 if (!h2 || h2->root.type == bfd_link_hash_undefined)
3041 {
3042 sec = bfd_abs_section_ptr;
3043 val = 0;
3044 }
3045 else
3046 {
3047 sec = h2->root.u.def.section;
3048 val = h2->root.u.def.value
3049 + sec->output_section->vma + sec->output_offset;
3050 }
c87db184
CF
3051 }
3052 else
07d6d2b8 3053 /* This is a GNU extension. */
c87db184
CF
3054 val = 0;
3055 }
bc7a577d 3056
0e1862bb 3057 else if (! bfd_link_relocatable (info))
6194b866
NC
3058 {
3059 (*info->callbacks->undefined_symbol)
3060 (info, h->root.root.string, input_bfd, input_section,
0a1b45a2 3061 rel->r_vaddr - input_section->vma, true);
6194b866
NC
3062 /* Stop the linker from issueing errors about truncated relocs
3063 referencing this undefined symbol by giving it an address
3064 that should be in range. */
3065 val = input_section->output_section->vma;
3066 }
252b5132
RH
3067 }
3068
3a062fa1
AM
3069 /* If the input section defining the symbol has been discarded
3070 then zero this reloc field. */
3071 if (sec != NULL && discarded_section (sec))
3072 {
3073 _bfd_clear_contents (howto, input_bfd, input_section,
0930cb30 3074 contents, rel->r_vaddr - input_section->vma);
3a062fa1
AM
3075 continue;
3076 }
3077
252b5132
RH
3078 if (info->base_file)
3079 {
244148ad 3080 /* Emit a reloc if the backend thinks it needs it. */
252b5132
RH
3081 if (sym && pe_data (output_bfd)->in_reloc_p (output_bfd, howto))
3082 {
3083 /* Relocation to a symbol in a section which isn't
3084 absolute. We output the address here to a file.
3085 This file is then read by dlltool when generating the
3086 reloc section. Note that the base file is not
d078078d
KT
3087 portable between systems. We write out a bfd_vma here,
3088 and dlltool reads in a bfd_vma. */
3089 bfd_vma addr = (rel->r_vaddr
244148ad
KH
3090 - input_section->vma
3091 + input_section->output_offset
252b5132 3092 + input_section->output_section->vma);
10df41b1 3093 if (obj_pe (output_bfd))
252b5132 3094 addr -= pe_data(output_bfd)->pe_opthdr.ImageBase;
d078078d
KT
3095 if (fwrite (&addr, 1, sizeof (bfd_vma), (FILE *) info->base_file)
3096 != sizeof (bfd_vma))
252b5132
RH
3097 {
3098 bfd_set_error (bfd_error_system_call);
0a1b45a2 3099 return false;
252b5132
RH
3100 }
3101 }
3102 }
244148ad 3103
252b5132
RH
3104 rstat = _bfd_final_link_relocate (howto, input_bfd, input_section,
3105 contents,
3106 rel->r_vaddr - input_section->vma,
3107 val, addend);
3108
3109 switch (rstat)
3110 {
3111 default:
3112 abort ();
3113 case bfd_reloc_ok:
3114 break;
3115 case bfd_reloc_outofrange:
4eca0228 3116 _bfd_error_handler
695344c0 3117 /* xgettext: c-format */
2dcf00ce
AM
3118 (_("%pB: bad reloc address %#" PRIx64 " in section `%pA'"),
3119 input_bfd, (uint64_t) rel->r_vaddr, input_section);
0a1b45a2 3120 return false;
252b5132
RH
3121 case bfd_reloc_overflow:
3122 {
74edb473
TC
3123 /* Ignore any weak undef symbols that may have overflowed. Due to
3124 PR ld/19011 the base address is now in the upper 64-bit address
3125 range. This means that when _bfd_final_link_relocate calculates
3126 the overlow it takes the distance between the symbol and the VMA
3127 which will now always overflow as 0 - 64-bit addr > 32-bit range
3128 of the relocation. This ends up creating PR ld/26659. */
3129 if (val == 0
3130 /* Reverse the hack where 4 is subtracted from the addend. */
3131 && (addend + 4) == 0
3132 && sym->n_sclass == C_NT_WEAK
3133 && bfd_coff_classify_symbol (output_bfd, sym)
3134 == COFF_SYMBOL_UNDEFINED)
3135 break;
3136
252b5132
RH
3137 const char *name;
3138 char buf[SYMNMLEN + 1];
3139
3140 if (symndx == -1)
3141 name = "*ABS*";
3142 else if (h != NULL)
dfeffb9f 3143 name = NULL;
252b5132
RH
3144 else
3145 {
3146 name = _bfd_coff_internal_syment_name (input_bfd, sym, buf);
3147 if (name == NULL)
0a1b45a2 3148 return false;
252b5132
RH
3149 }
3150
1a72702b
AM
3151 (*info->callbacks->reloc_overflow)
3152 (info, (h ? &h->root : NULL), name, howto->name,
3153 (bfd_vma) 0, input_bfd, input_section,
3154 rel->r_vaddr - input_section->vma);
252b5132
RH
3155 }
3156 }
3157 }
f3d8ae90 3158
0a1b45a2 3159 return true;
252b5132 3160}