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