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