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