source: trunk/src/binutils/bfd/cofflink.c@ 106

Last change on this file since 106 was 10, checked in by bird, 22 years ago

Initial revision

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