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