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