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