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