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