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