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