Upgrade GDB from 7.4.1 to 7.6.1 on the vendor branch
[dragonfly.git] / contrib / gdb-7 / bfd / coffgen.c
CommitLineData
5796c8dc
SS
1/* Support for the generic parts of COFF, for BFD.
2 Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
3 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
4 Free Software Foundation, Inc.
5 Written by Cygnus Support.
6
7 This file is part of BFD, the Binary File Descriptor library.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
22 MA 02110-1301, USA. */
23
24/* Most of this hacked by Steve Chamberlain, sac@cygnus.com.
25 Split out of coffcode.h by Ian Taylor, ian@cygnus.com. */
26
27/* This file contains COFF code that is not dependent on any
28 particular COFF target. There is only one version of this file in
29 libbfd.a, so no target specific code may be put in here. Or, to
30 put it another way,
31
32 ********** DO NOT PUT TARGET SPECIFIC CODE IN THIS FILE **********
33
34 If you need to add some target specific behaviour, add a new hook
35 function to bfd_coff_backend_data.
36
37 Some of these functions are also called by the ECOFF routines.
38 Those functions may not use any COFF specific information, such as
39 coff_data (abfd). */
40
41#include "sysdep.h"
42#include "bfd.h"
43#include "libbfd.h"
44#include "coff/internal.h"
45#include "libcoff.h"
46
47/* Take a section header read from a coff file (in HOST byte order),
48 and make a BFD "section" out of it. This is used by ECOFF. */
49
50static bfd_boolean
51make_a_section_from_file (bfd *abfd,
52 struct internal_scnhdr *hdr,
53 unsigned int target_index)
54{
55 asection *return_section;
56 char *name;
57 bfd_boolean result = TRUE;
58 flagword flags;
59
60 name = NULL;
61
62 /* Handle long section names as in PE. On reading, we want to
63 accept long names if the format permits them at all, regardless
64 of the current state of the flag that dictates if we would generate
65 them in outputs; this construct checks if that is the case by
66 attempting to set the flag, without changing its state; the call
67 will fail for formats that do not support long names at all. */
68 if (bfd_coff_set_long_section_names (abfd, bfd_coff_long_section_names (abfd))
69 && hdr->s_name[0] == '/')
70 {
71 char buf[SCNNMLEN];
72 long strindex;
73 char *p;
74 const char *strings;
75
76 /* Flag that this BFD uses long names, even though the format might
77 expect them to be off by default. This won't directly affect the
78 format of any output BFD created from this one, but the information
79 can be used to decide what to do. */
80 bfd_coff_set_long_section_names (abfd, TRUE);
81 memcpy (buf, hdr->s_name + 1, SCNNMLEN - 1);
82 buf[SCNNMLEN - 1] = '\0';
83 strindex = strtol (buf, &p, 10);
84 if (*p == '\0' && strindex >= 0)
85 {
86 strings = _bfd_coff_read_string_table (abfd);
87 if (strings == NULL)
88 return FALSE;
89 /* FIXME: For extra safety, we should make sure that
90 strindex does not run us past the end, but right now we
91 don't know the length of the string table. */
92 strings += strindex;
93 name = (char *) bfd_alloc (abfd,
ef5ccd6c 94 (bfd_size_type) strlen (strings) + 1 + 1);
5796c8dc
SS
95 if (name == NULL)
96 return FALSE;
97 strcpy (name, strings);
98 }
99 }
100
101 if (name == NULL)
102 {
103 /* Assorted wastage to null-terminate the name, thanks AT&T! */
104 name = (char *) bfd_alloc (abfd,
ef5ccd6c 105 (bfd_size_type) sizeof (hdr->s_name) + 1 + 1);
5796c8dc
SS
106 if (name == NULL)
107 return FALSE;
108 strncpy (name, (char *) &hdr->s_name[0], sizeof (hdr->s_name));
109 name[sizeof (hdr->s_name)] = 0;
110 }
111
112 return_section = bfd_make_section_anyway (abfd, name);
113 if (return_section == NULL)
114 return FALSE;
115
116 return_section->vma = hdr->s_vaddr;
117 return_section->lma = hdr->s_paddr;
118 return_section->size = hdr->s_size;
119 return_section->filepos = hdr->s_scnptr;
120 return_section->rel_filepos = hdr->s_relptr;
121 return_section->reloc_count = hdr->s_nreloc;
122
123 bfd_coff_set_alignment_hook (abfd, return_section, hdr);
124
125 return_section->line_filepos = hdr->s_lnnoptr;
126
127 return_section->lineno_count = hdr->s_nlnno;
128 return_section->userdata = NULL;
129 return_section->next = NULL;
130 return_section->target_index = target_index;
131
132 if (! bfd_coff_styp_to_sec_flags_hook (abfd, hdr, name, return_section,
133 & flags))
134 result = FALSE;
135
136 return_section->flags = flags;
137
138 /* At least on i386-coff, the line number count for a shared library
139 section must be ignored. */
140 if ((return_section->flags & SEC_COFF_SHARED_LIBRARY) != 0)
141 return_section->lineno_count = 0;
142
143 if (hdr->s_nreloc != 0)
144 return_section->flags |= SEC_RELOC;
145 /* FIXME: should this check 'hdr->s_size > 0'. */
146 if (hdr->s_scnptr != 0)
147 return_section->flags |= SEC_HAS_CONTENTS;
148
ef5ccd6c
JM
149 /* Compress/decompress DWARF debug sections with names: .debug_* and
150 .zdebug_*, after the section flags is set. */
151 if ((flags & SEC_DEBUGGING)
152 && ((name[1] == 'd' && name[6] == '_')
153 || (name[1] == 'z' && name[7] == '_')))
154 {
155 enum { nothing, compress, decompress } action = nothing;
156 char *new_name = NULL;
157
158 if (bfd_is_section_compressed (abfd, return_section))
159 {
160 /* Compressed section. Check if we should decompress. */
161 if ((abfd->flags & BFD_DECOMPRESS))
162 action = decompress;
163 }
164 else if (!bfd_is_section_compressed (abfd, return_section))
165 {
166 /* Normal section. Check if we should compress. */
167 if ((abfd->flags & BFD_COMPRESS) && return_section->size != 0)
168 action = compress;
169 }
170
171 switch (action)
172 {
173 case nothing:
174 break;
175 case compress:
176 if (!bfd_init_section_compress_status (abfd, return_section))
177 {
178 (*_bfd_error_handler)
179 (_("%B: unable to initialize compress status for section %s"),
180 abfd, name);
181 return FALSE;
182 }
183 if (name[1] != 'z')
184 {
185 unsigned int len = strlen (name);
186
187 new_name = bfd_alloc (abfd, len + 2);
188 if (new_name == NULL)
189 return FALSE;
190 new_name[0] = '.';
191 new_name[1] = 'z';
192 memcpy (new_name + 2, name + 1, len);
193 }
194 break;
195 case decompress:
196 if (!bfd_init_section_decompress_status (abfd, return_section))
197 {
198 (*_bfd_error_handler)
199 (_("%B: unable to initialize decompress status for section %s"),
200 abfd, name);
201 return FALSE;
202 }
203 if (name[1] == 'z')
204 {
205 unsigned int len = strlen (name);
206
207 new_name = bfd_alloc (abfd, len);
208 if (new_name == NULL)
209 return FALSE;
210 new_name[0] = '.';
211 memcpy (new_name + 1, name + 2, len - 1);
212 }
213 break;
214 }
215 if (new_name != NULL)
216 bfd_rename_section (abfd, return_section, new_name);
217 }
218
5796c8dc
SS
219 return result;
220}
221
222/* Read in a COFF object and make it into a BFD. This is used by
223 ECOFF as well. */
224
225static const bfd_target *
226coff_real_object_p (bfd *abfd,
227 unsigned nscns,
228 struct internal_filehdr *internal_f,
229 struct internal_aouthdr *internal_a)
230{
231 flagword oflags = abfd->flags;
232 bfd_vma ostart = bfd_get_start_address (abfd);
233 void * tdata;
234 void * tdata_save;
235 bfd_size_type readsize; /* Length of file_info. */
236 unsigned int scnhsz;
237 char *external_sections;
238
239 if (!(internal_f->f_flags & F_RELFLG))
240 abfd->flags |= HAS_RELOC;
241 if ((internal_f->f_flags & F_EXEC))
242 abfd->flags |= EXEC_P;
243 if (!(internal_f->f_flags & F_LNNO))
244 abfd->flags |= HAS_LINENO;
245 if (!(internal_f->f_flags & F_LSYMS))
246 abfd->flags |= HAS_LOCALS;
247
248 /* FIXME: How can we set D_PAGED correctly? */
249 if ((internal_f->f_flags & F_EXEC) != 0)
250 abfd->flags |= D_PAGED;
251
252 bfd_get_symcount (abfd) = internal_f->f_nsyms;
253 if (internal_f->f_nsyms)
254 abfd->flags |= HAS_SYMS;
255
256 if (internal_a != (struct internal_aouthdr *) NULL)
257 bfd_get_start_address (abfd) = internal_a->entry;
258 else
259 bfd_get_start_address (abfd) = 0;
260
261 /* Set up the tdata area. ECOFF uses its own routine, and overrides
262 abfd->flags. */
263 tdata_save = abfd->tdata.any;
264 tdata = bfd_coff_mkobject_hook (abfd, (void *) internal_f, (void *) internal_a);
265 if (tdata == NULL)
266 goto fail2;
267
268 scnhsz = bfd_coff_scnhsz (abfd);
269 readsize = (bfd_size_type) nscns * scnhsz;
270 external_sections = (char *) bfd_alloc (abfd, readsize);
271 if (!external_sections)
272 goto fail;
273
274 if (bfd_bread ((void *) external_sections, readsize, abfd) != readsize)
275 goto fail;
276
277 /* Set the arch/mach *before* swapping in sections; section header swapping
278 may depend on arch/mach info. */
279 if (! bfd_coff_set_arch_mach_hook (abfd, (void *) internal_f))
280 goto fail;
281
282 /* Now copy data as required; construct all asections etc. */
283 if (nscns != 0)
284 {
285 unsigned int i;
286 for (i = 0; i < nscns; i++)
287 {
288 struct internal_scnhdr tmp;
289 bfd_coff_swap_scnhdr_in (abfd,
290 (void *) (external_sections + i * scnhsz),
291 (void *) & tmp);
292 if (! make_a_section_from_file (abfd, &tmp, i + 1))
293 goto fail;
294 }
295 }
296
297 return abfd->xvec;
298
299 fail:
300 bfd_release (abfd, tdata);
301 fail2:
302 abfd->tdata.any = tdata_save;
303 abfd->flags = oflags;
304 bfd_get_start_address (abfd) = ostart;
305 return (const bfd_target *) NULL;
306}
307
308/* Turn a COFF file into a BFD, but fail with bfd_error_wrong_format if it is
309 not a COFF file. This is also used by ECOFF. */
310
311const bfd_target *
312coff_object_p (bfd *abfd)
313{
314 bfd_size_type filhsz;
315 bfd_size_type aoutsz;
316 unsigned int nscns;
317 void * filehdr;
318 struct internal_filehdr internal_f;
319 struct internal_aouthdr internal_a;
320
321 /* Figure out how much to read. */
322 filhsz = bfd_coff_filhsz (abfd);
323 aoutsz = bfd_coff_aoutsz (abfd);
324
325 filehdr = bfd_alloc (abfd, filhsz);
326 if (filehdr == NULL)
327 return NULL;
328 if (bfd_bread (filehdr, filhsz, abfd) != filhsz)
329 {
330 if (bfd_get_error () != bfd_error_system_call)
331 bfd_set_error (bfd_error_wrong_format);
332 bfd_release (abfd, filehdr);
333 return NULL;
334 }
335 bfd_coff_swap_filehdr_in (abfd, filehdr, &internal_f);
336 bfd_release (abfd, filehdr);
337
338 /* The XCOFF format has two sizes for the f_opthdr. SMALL_AOUTSZ
339 (less than aoutsz) used in object files and AOUTSZ (equal to
340 aoutsz) in executables. The bfd_coff_swap_aouthdr_in function
341 expects this header to be aoutsz bytes in length, so we use that
342 value in the call to bfd_alloc below. But we must be careful to
343 only read in f_opthdr bytes in the call to bfd_bread. We should
344 also attempt to catch corrupt or non-COFF binaries with a strange
345 value for f_opthdr. */
346 if (! bfd_coff_bad_format_hook (abfd, &internal_f)
347 || internal_f.f_opthdr > aoutsz)
348 {
349 bfd_set_error (bfd_error_wrong_format);
350 return NULL;
351 }
352 nscns = internal_f.f_nscns;
353
354 if (internal_f.f_opthdr)
355 {
356 void * opthdr;
357
358 opthdr = bfd_alloc (abfd, aoutsz);
359 if (opthdr == NULL)
360 return NULL;
361 if (bfd_bread (opthdr, (bfd_size_type) internal_f.f_opthdr, abfd)
362 != internal_f.f_opthdr)
363 {
364 bfd_release (abfd, opthdr);
365 return NULL;
366 }
367 bfd_coff_swap_aouthdr_in (abfd, opthdr, (void *) &internal_a);
368 bfd_release (abfd, opthdr);
369 }
370
371 return coff_real_object_p (abfd, nscns, &internal_f,
372 (internal_f.f_opthdr != 0
373 ? &internal_a
374 : (struct internal_aouthdr *) NULL));
375}
376
377/* Get the BFD section from a COFF symbol section number. */
378
379asection *
cf7f2e2d 380coff_section_from_bfd_index (bfd *abfd, int section_index)
5796c8dc
SS
381{
382 struct bfd_section *answer = abfd->sections;
383
cf7f2e2d 384 if (section_index == N_ABS)
5796c8dc 385 return bfd_abs_section_ptr;
cf7f2e2d 386 if (section_index == N_UNDEF)
5796c8dc 387 return bfd_und_section_ptr;
cf7f2e2d 388 if (section_index == N_DEBUG)
5796c8dc
SS
389 return bfd_abs_section_ptr;
390
391 while (answer)
392 {
cf7f2e2d 393 if (answer->target_index == section_index)
5796c8dc
SS
394 return answer;
395 answer = answer->next;
396 }
397
398 /* We should not reach this point, but the SCO 3.2v4 /lib/libc_s.a
399 has a bad symbol table in biglitpow.o. */
400 return bfd_und_section_ptr;
401}
402
403/* Get the upper bound of a COFF symbol table. */
404
405long
406coff_get_symtab_upper_bound (bfd *abfd)
407{
408 if (!bfd_coff_slurp_symbol_table (abfd))
409 return -1;
410
411 return (bfd_get_symcount (abfd) + 1) * (sizeof (coff_symbol_type *));
412}
413
414/* Canonicalize a COFF symbol table. */
415
416long
417coff_canonicalize_symtab (bfd *abfd, asymbol **alocation)
418{
419 unsigned int counter;
420 coff_symbol_type *symbase;
421 coff_symbol_type **location = (coff_symbol_type **) alocation;
422
423 if (!bfd_coff_slurp_symbol_table (abfd))
424 return -1;
425
426 symbase = obj_symbols (abfd);
427 counter = bfd_get_symcount (abfd);
428 while (counter-- > 0)
429 *location++ = symbase++;
430
431 *location = NULL;
432
433 return bfd_get_symcount (abfd);
434}
435
436/* Get the name of a symbol. The caller must pass in a buffer of size
437 >= SYMNMLEN + 1. */
438
439const char *
440_bfd_coff_internal_syment_name (bfd *abfd,
441 const struct internal_syment *sym,
442 char *buf)
443{
444 /* FIXME: It's not clear this will work correctly if sizeof
445 (_n_zeroes) != 4. */
446 if (sym->_n._n_n._n_zeroes != 0
447 || sym->_n._n_n._n_offset == 0)
448 {
449 memcpy (buf, sym->_n._n_name, SYMNMLEN);
450 buf[SYMNMLEN] = '\0';
451 return buf;
452 }
453 else
454 {
455 const char *strings;
456
457 BFD_ASSERT (sym->_n._n_n._n_offset >= STRING_SIZE_SIZE);
458 strings = obj_coff_strings (abfd);
459 if (strings == NULL)
460 {
461 strings = _bfd_coff_read_string_table (abfd);
462 if (strings == NULL)
463 return NULL;
464 }
465 return strings + sym->_n._n_n._n_offset;
466 }
467}
468
469/* Read in and swap the relocs. This returns a buffer holding the
470 relocs for section SEC in file ABFD. If CACHE is TRUE and
471 INTERNAL_RELOCS is NULL, the relocs read in will be saved in case
472 the function is called again. If EXTERNAL_RELOCS is not NULL, it
473 is a buffer large enough to hold the unswapped relocs. If
474 INTERNAL_RELOCS is not NULL, it is a buffer large enough to hold
475 the swapped relocs. If REQUIRE_INTERNAL is TRUE, then the return
476 value must be INTERNAL_RELOCS. The function returns NULL on error. */
477
478struct internal_reloc *
479_bfd_coff_read_internal_relocs (bfd *abfd,
480 asection *sec,
481 bfd_boolean cache,
482 bfd_byte *external_relocs,
483 bfd_boolean require_internal,
484 struct internal_reloc *internal_relocs)
485{
486 bfd_size_type relsz;
487 bfd_byte *free_external = NULL;
488 struct internal_reloc *free_internal = NULL;
489 bfd_byte *erel;
490 bfd_byte *erel_end;
491 struct internal_reloc *irel;
492 bfd_size_type amt;
493
494 if (sec->reloc_count == 0)
495 return internal_relocs; /* Nothing to do. */
496
497 if (coff_section_data (abfd, sec) != NULL
498 && coff_section_data (abfd, sec)->relocs != NULL)
499 {
500 if (! require_internal)
501 return coff_section_data (abfd, sec)->relocs;
502 memcpy (internal_relocs, coff_section_data (abfd, sec)->relocs,
503 sec->reloc_count * sizeof (struct internal_reloc));
504 return internal_relocs;
505 }
506
507 relsz = bfd_coff_relsz (abfd);
508
509 amt = sec->reloc_count * relsz;
510 if (external_relocs == NULL)
511 {
512 free_external = (bfd_byte *) bfd_malloc (amt);
513 if (free_external == NULL)
514 goto error_return;
515 external_relocs = free_external;
516 }
517
518 if (bfd_seek (abfd, sec->rel_filepos, SEEK_SET) != 0
519 || bfd_bread (external_relocs, amt, abfd) != amt)
520 goto error_return;
521
522 if (internal_relocs == NULL)
523 {
524 amt = sec->reloc_count;
525 amt *= sizeof (struct internal_reloc);
526 free_internal = (struct internal_reloc *) bfd_malloc (amt);
527 if (free_internal == NULL)
528 goto error_return;
529 internal_relocs = free_internal;
530 }
531
532 /* Swap in the relocs. */
533 erel = external_relocs;
534 erel_end = erel + relsz * sec->reloc_count;
535 irel = internal_relocs;
536 for (; erel < erel_end; erel += relsz, irel++)
537 bfd_coff_swap_reloc_in (abfd, (void *) erel, (void *) irel);
538
539 if (free_external != NULL)
540 {
541 free (free_external);
542 free_external = NULL;
543 }
544
545 if (cache && free_internal != NULL)
546 {
547 if (coff_section_data (abfd, sec) == NULL)
548 {
549 amt = sizeof (struct coff_section_tdata);
550 sec->used_by_bfd = bfd_zalloc (abfd, amt);
551 if (sec->used_by_bfd == NULL)
552 goto error_return;
553 coff_section_data (abfd, sec)->contents = NULL;
554 }
555 coff_section_data (abfd, sec)->relocs = free_internal;
556 }
557
558 return internal_relocs;
559
560 error_return:
561 if (free_external != NULL)
562 free (free_external);
563 if (free_internal != NULL)
564 free (free_internal);
565 return NULL;
566}
567
568/* Set lineno_count for the output sections of a COFF file. */
569
570int
571coff_count_linenumbers (bfd *abfd)
572{
573 unsigned int limit = bfd_get_symcount (abfd);
574 unsigned int i;
575 int total = 0;
576 asymbol **p;
577 asection *s;
578
579 if (limit == 0)
580 {
581 /* This may be from the backend linker, in which case the
582 lineno_count in the sections is correct. */
583 for (s = abfd->sections; s != NULL; s = s->next)
584 total += s->lineno_count;
585 return total;
586 }
587
588 for (s = abfd->sections; s != NULL; s = s->next)
589 BFD_ASSERT (s->lineno_count == 0);
590
591 for (p = abfd->outsymbols, i = 0; i < limit; i++, p++)
592 {
593 asymbol *q_maybe = *p;
594
595 if (bfd_family_coff (bfd_asymbol_bfd (q_maybe)))
596 {
597 coff_symbol_type *q = coffsymbol (q_maybe);
598
599 /* The AIX 4.1 compiler can sometimes generate line numbers
600 attached to debugging symbols. We try to simply ignore
601 those here. */
602 if (q->lineno != NULL
603 && q->symbol.section->owner != NULL)
604 {
605 /* This symbol has line numbers. Increment the owning
606 section's linenumber count. */
607 alent *l = q->lineno;
608
609 do
610 {
611 asection * sec = q->symbol.section->output_section;
612
613 /* Do not try to update fields in read-only sections. */
614 if (! bfd_is_const_section (sec))
615 sec->lineno_count ++;
616
617 ++total;
618 ++l;
619 }
620 while (l->line_number != 0);
621 }
622 }
623 }
624
625 return total;
626}
627
628/* Takes a bfd and a symbol, returns a pointer to the coff specific
629 area of the symbol if there is one. */
630
631coff_symbol_type *
632coff_symbol_from (bfd *ignore_abfd ATTRIBUTE_UNUSED,
633 asymbol *symbol)
634{
635 if (!bfd_family_coff (bfd_asymbol_bfd (symbol)))
636 return (coff_symbol_type *) NULL;
637
638 if (bfd_asymbol_bfd (symbol)->tdata.coff_obj_data == (coff_data_type *) NULL)
639 return (coff_symbol_type *) NULL;
640
641 return (coff_symbol_type *) symbol;
642}
643
644static void
645fixup_symbol_value (bfd *abfd,
646 coff_symbol_type *coff_symbol_ptr,
647 struct internal_syment *syment)
648{
649 /* Normalize the symbol flags. */
ef5ccd6c 650 if (coff_symbol_ptr->symbol.section
5796c8dc
SS
651 && bfd_is_com_section (coff_symbol_ptr->symbol.section))
652 {
653 /* A common symbol is undefined with a value. */
654 syment->n_scnum = N_UNDEF;
655 syment->n_value = coff_symbol_ptr->symbol.value;
656 }
657 else if ((coff_symbol_ptr->symbol.flags & BSF_DEBUGGING) != 0
658 && (coff_symbol_ptr->symbol.flags & BSF_DEBUGGING_RELOC) == 0)
659 {
660 syment->n_value = coff_symbol_ptr->symbol.value;
661 }
662 else if (bfd_is_und_section (coff_symbol_ptr->symbol.section))
663 {
664 syment->n_scnum = N_UNDEF;
665 syment->n_value = 0;
666 }
667 /* FIXME: Do we need to handle the absolute section here? */
668 else
669 {
670 if (coff_symbol_ptr->symbol.section)
671 {
672 syment->n_scnum =
673 coff_symbol_ptr->symbol.section->output_section->target_index;
674
675 syment->n_value = (coff_symbol_ptr->symbol.value
676 + coff_symbol_ptr->symbol.section->output_offset);
677 if (! obj_pe (abfd))
678 {
679 syment->n_value += (syment->n_sclass == C_STATLAB)
680 ? coff_symbol_ptr->symbol.section->output_section->lma
681 : coff_symbol_ptr->symbol.section->output_section->vma;
682 }
683 }
684 else
685 {
686 BFD_ASSERT (0);
687 /* This can happen, but I don't know why yet (steve@cygnus.com) */
688 syment->n_scnum = N_ABS;
689 syment->n_value = coff_symbol_ptr->symbol.value;
690 }
691 }
692}
693
694/* Run through all the symbols in the symbol table and work out what
695 their indexes into the symbol table will be when output.
696
697 Coff requires that each C_FILE symbol points to the next one in the
698 chain, and that the last one points to the first external symbol. We
699 do that here too. */
700
701bfd_boolean
702coff_renumber_symbols (bfd *bfd_ptr, int *first_undef)
703{
704 unsigned int symbol_count = bfd_get_symcount (bfd_ptr);
705 asymbol **symbol_ptr_ptr = bfd_ptr->outsymbols;
706 unsigned int native_index = 0;
707 struct internal_syment *last_file = NULL;
708 unsigned int symbol_index;
709
710 /* COFF demands that undefined symbols come after all other symbols.
711 Since we don't need to impose this extra knowledge on all our
712 client programs, deal with that here. Sort the symbol table;
713 just move the undefined symbols to the end, leaving the rest
714 alone. The O'Reilly book says that defined global symbols come
715 at the end before the undefined symbols, so we do that here as
716 well. */
717 /* @@ Do we have some condition we could test for, so we don't always
718 have to do this? I don't think relocatability is quite right, but
719 I'm not certain. [raeburn:19920508.1711EST] */
720 {
721 asymbol **newsyms;
722 unsigned int i;
723 bfd_size_type amt;
724
725 amt = sizeof (asymbol *) * ((bfd_size_type) symbol_count + 1);
726 newsyms = (asymbol **) bfd_alloc (bfd_ptr, amt);
727 if (!newsyms)
728 return FALSE;
729 bfd_ptr->outsymbols = newsyms;
730 for (i = 0; i < symbol_count; i++)
731 if ((symbol_ptr_ptr[i]->flags & BSF_NOT_AT_END) != 0
732 || (!bfd_is_und_section (symbol_ptr_ptr[i]->section)
733 && !bfd_is_com_section (symbol_ptr_ptr[i]->section)
734 && ((symbol_ptr_ptr[i]->flags & BSF_FUNCTION) != 0
735 || ((symbol_ptr_ptr[i]->flags & (BSF_GLOBAL | BSF_WEAK))
736 == 0))))
737 *newsyms++ = symbol_ptr_ptr[i];
738
739 for (i = 0; i < symbol_count; i++)
740 if ((symbol_ptr_ptr[i]->flags & BSF_NOT_AT_END) == 0
741 && !bfd_is_und_section (symbol_ptr_ptr[i]->section)
742 && (bfd_is_com_section (symbol_ptr_ptr[i]->section)
743 || ((symbol_ptr_ptr[i]->flags & BSF_FUNCTION) == 0
744 && ((symbol_ptr_ptr[i]->flags & (BSF_GLOBAL | BSF_WEAK))
745 != 0))))
746 *newsyms++ = symbol_ptr_ptr[i];
747
748 *first_undef = newsyms - bfd_ptr->outsymbols;
749
750 for (i = 0; i < symbol_count; i++)
751 if ((symbol_ptr_ptr[i]->flags & BSF_NOT_AT_END) == 0
752 && bfd_is_und_section (symbol_ptr_ptr[i]->section))
753 *newsyms++ = symbol_ptr_ptr[i];
754 *newsyms = (asymbol *) NULL;
755 symbol_ptr_ptr = bfd_ptr->outsymbols;
756 }
757
758 for (symbol_index = 0; symbol_index < symbol_count; symbol_index++)
759 {
760 coff_symbol_type *coff_symbol_ptr = coff_symbol_from (bfd_ptr, symbol_ptr_ptr[symbol_index]);
761 symbol_ptr_ptr[symbol_index]->udata.i = symbol_index;
762 if (coff_symbol_ptr && coff_symbol_ptr->native)
763 {
764 combined_entry_type *s = coff_symbol_ptr->native;
765 int i;
766
767 if (s->u.syment.n_sclass == C_FILE)
768 {
769 if (last_file != NULL)
770 last_file->n_value = native_index;
771 last_file = &(s->u.syment);
772 }
773 else
774 /* Modify the symbol values according to their section and
775 type. */
776 fixup_symbol_value (bfd_ptr, coff_symbol_ptr, &(s->u.syment));
777
778 for (i = 0; i < s->u.syment.n_numaux + 1; i++)
779 s[i].offset = native_index++;
780 }
781 else
782 native_index++;
783 }
784
785 obj_conv_table_size (bfd_ptr) = native_index;
786
787 return TRUE;
788}
789
790/* Run thorough the symbol table again, and fix it so that all
791 pointers to entries are changed to the entries' index in the output
792 symbol table. */
793
794void
795coff_mangle_symbols (bfd *bfd_ptr)
796{
797 unsigned int symbol_count = bfd_get_symcount (bfd_ptr);
798 asymbol **symbol_ptr_ptr = bfd_ptr->outsymbols;
799 unsigned int symbol_index;
800
801 for (symbol_index = 0; symbol_index < symbol_count; symbol_index++)
802 {
803 coff_symbol_type *coff_symbol_ptr =
804 coff_symbol_from (bfd_ptr, symbol_ptr_ptr[symbol_index]);
805
806 if (coff_symbol_ptr && coff_symbol_ptr->native)
807 {
808 int i;
809 combined_entry_type *s = coff_symbol_ptr->native;
810
811 if (s->fix_value)
812 {
813 /* FIXME: We should use a union here. */
814 s->u.syment.n_value =
815 (bfd_hostptr_t) ((combined_entry_type *)
816 ((bfd_hostptr_t) s->u.syment.n_value))->offset;
817 s->fix_value = 0;
818 }
819 if (s->fix_line)
820 {
821 /* The value is the offset into the line number entries
822 for the symbol's section. On output, the symbol's
823 section should be N_DEBUG. */
824 s->u.syment.n_value =
825 (coff_symbol_ptr->symbol.section->output_section->line_filepos
826 + s->u.syment.n_value * bfd_coff_linesz (bfd_ptr));
827 coff_symbol_ptr->symbol.section =
828 coff_section_from_bfd_index (bfd_ptr, N_DEBUG);
829 BFD_ASSERT (coff_symbol_ptr->symbol.flags & BSF_DEBUGGING);
830 }
831 for (i = 0; i < s->u.syment.n_numaux; i++)
832 {
833 combined_entry_type *a = s + i + 1;
834 if (a->fix_tag)
835 {
836 a->u.auxent.x_sym.x_tagndx.l =
837 a->u.auxent.x_sym.x_tagndx.p->offset;
838 a->fix_tag = 0;
839 }
840 if (a->fix_end)
841 {
842 a->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l =
843 a->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p->offset;
844 a->fix_end = 0;
845 }
846 if (a->fix_scnlen)
847 {
848 a->u.auxent.x_csect.x_scnlen.l =
849 a->u.auxent.x_csect.x_scnlen.p->offset;
850 a->fix_scnlen = 0;
851 }
852 }
853 }
854 }
855}
856
857static void
858coff_fix_symbol_name (bfd *abfd,
859 asymbol *symbol,
860 combined_entry_type *native,
861 bfd_size_type *string_size_p,
862 asection **debug_string_section_p,
863 bfd_size_type *debug_string_size_p)
864{
865 unsigned int name_length;
866 union internal_auxent *auxent;
867 char *name = (char *) (symbol->name);
868
869 if (name == NULL)
870 {
871 /* COFF symbols always have names, so we'll make one up. */
872 symbol->name = "strange";
873 name = (char *) symbol->name;
874 }
875 name_length = strlen (name);
876
877 if (native->u.syment.n_sclass == C_FILE
878 && native->u.syment.n_numaux > 0)
879 {
880 unsigned int filnmlen;
881
882 if (bfd_coff_force_symnames_in_strings (abfd))
883 {
884 native->u.syment._n._n_n._n_offset =
885 (*string_size_p + STRING_SIZE_SIZE);
886 native->u.syment._n._n_n._n_zeroes = 0;
887 *string_size_p += 6; /* strlen(".file") + 1 */
888 }
889 else
890 strncpy (native->u.syment._n._n_name, ".file", SYMNMLEN);
891
892 auxent = &(native + 1)->u.auxent;
893
894 filnmlen = bfd_coff_filnmlen (abfd);
895
896 if (bfd_coff_long_filenames (abfd))
897 {
898 if (name_length <= filnmlen)
899 strncpy (auxent->x_file.x_fname, name, filnmlen);
900 else
901 {
902 auxent->x_file.x_n.x_offset = *string_size_p + STRING_SIZE_SIZE;
903 auxent->x_file.x_n.x_zeroes = 0;
904 *string_size_p += name_length + 1;
905 }
906 }
907 else
908 {
909 strncpy (auxent->x_file.x_fname, name, filnmlen);
910 if (name_length > filnmlen)
911 name[filnmlen] = '\0';
912 }
913 }
914 else
915 {
916 if (name_length <= SYMNMLEN && !bfd_coff_force_symnames_in_strings (abfd))
917 /* This name will fit into the symbol neatly. */
918 strncpy (native->u.syment._n._n_name, symbol->name, SYMNMLEN);
919
920 else if (!bfd_coff_symname_in_debug (abfd, &native->u.syment))
921 {
922 native->u.syment._n._n_n._n_offset = (*string_size_p
923 + STRING_SIZE_SIZE);
924 native->u.syment._n._n_n._n_zeroes = 0;
925 *string_size_p += name_length + 1;
926 }
927 else
928 {
929 file_ptr filepos;
930 bfd_byte buf[4];
931 int prefix_len = bfd_coff_debug_string_prefix_length (abfd);
932
933 /* This name should be written into the .debug section. For
934 some reason each name is preceded by a two byte length
935 and also followed by a null byte. FIXME: We assume that
936 the .debug section has already been created, and that it
937 is large enough. */
938 if (*debug_string_section_p == (asection *) NULL)
939 *debug_string_section_p = bfd_get_section_by_name (abfd, ".debug");
940 filepos = bfd_tell (abfd);
941 if (prefix_len == 4)
942 bfd_put_32 (abfd, (bfd_vma) (name_length + 1), buf);
943 else
944 bfd_put_16 (abfd, (bfd_vma) (name_length + 1), buf);
945
946 if (!bfd_set_section_contents (abfd,
947 *debug_string_section_p,
948 (void *) buf,
949 (file_ptr) *debug_string_size_p,
950 (bfd_size_type) prefix_len)
951 || !bfd_set_section_contents (abfd,
952 *debug_string_section_p,
953 (void *) symbol->name,
954 (file_ptr) (*debug_string_size_p
955 + prefix_len),
956 (bfd_size_type) name_length + 1))
957 abort ();
958 if (bfd_seek (abfd, filepos, SEEK_SET) != 0)
959 abort ();
960 native->u.syment._n._n_n._n_offset =
961 *debug_string_size_p + prefix_len;
962 native->u.syment._n._n_n._n_zeroes = 0;
963 *debug_string_size_p += name_length + 1 + prefix_len;
964 }
965 }
966}
967
968/* We need to keep track of the symbol index so that when we write out
969 the relocs we can get the index for a symbol. This method is a
970 hack. FIXME. */
971
972#define set_index(symbol, idx) ((symbol)->udata.i = (idx))
973
974/* Write a symbol out to a COFF file. */
975
976static bfd_boolean
977coff_write_symbol (bfd *abfd,
978 asymbol *symbol,
979 combined_entry_type *native,
980 bfd_vma *written,
981 bfd_size_type *string_size_p,
982 asection **debug_string_section_p,
983 bfd_size_type *debug_string_size_p)
984{
985 unsigned int numaux = native->u.syment.n_numaux;
986 int type = native->u.syment.n_type;
987 int n_sclass = (int) native->u.syment.n_sclass;
a45ae5f8
JM
988 asection *output_section = symbol->section->output_section
989 ? symbol->section->output_section
990 : symbol->section;
5796c8dc
SS
991 void * buf;
992 bfd_size_type symesz;
993
994 if (native->u.syment.n_sclass == C_FILE)
995 symbol->flags |= BSF_DEBUGGING;
996
997 if (symbol->flags & BSF_DEBUGGING
998 && bfd_is_abs_section (symbol->section))
999 native->u.syment.n_scnum = N_DEBUG;
1000
1001 else if (bfd_is_abs_section (symbol->section))
1002 native->u.syment.n_scnum = N_ABS;
1003
1004 else if (bfd_is_und_section (symbol->section))
1005 native->u.syment.n_scnum = N_UNDEF;
1006
1007 else
1008 native->u.syment.n_scnum =
a45ae5f8 1009 output_section->target_index;
5796c8dc
SS
1010
1011 coff_fix_symbol_name (abfd, symbol, native, string_size_p,
1012 debug_string_section_p, debug_string_size_p);
1013
1014 symesz = bfd_coff_symesz (abfd);
1015 buf = bfd_alloc (abfd, symesz);
1016 if (!buf)
1017 return FALSE;
1018 bfd_coff_swap_sym_out (abfd, &native->u.syment, buf);
1019 if (bfd_bwrite (buf, symesz, abfd) != symesz)
1020 return FALSE;
1021 bfd_release (abfd, buf);
1022
1023 if (native->u.syment.n_numaux > 0)
1024 {
1025 bfd_size_type auxesz;
1026 unsigned int j;
1027
1028 auxesz = bfd_coff_auxesz (abfd);
1029 buf = bfd_alloc (abfd, auxesz);
1030 if (!buf)
1031 return FALSE;
1032 for (j = 0; j < native->u.syment.n_numaux; j++)
1033 {
1034 bfd_coff_swap_aux_out (abfd,
1035 &((native + j + 1)->u.auxent),
1036 type, n_sclass, (int) j,
1037 native->u.syment.n_numaux,
1038 buf);
1039 if (bfd_bwrite (buf, auxesz, abfd) != auxesz)
1040 return FALSE;
1041 }
1042 bfd_release (abfd, buf);
1043 }
1044
1045 /* Store the index for use when we write out the relocs. */
1046 set_index (symbol, *written);
1047
1048 *written += numaux + 1;
1049 return TRUE;
1050}
1051
1052/* Write out a symbol to a COFF file that does not come from a COFF
1053 file originally. This symbol may have been created by the linker,
1054 or we may be linking a non COFF file to a COFF file. */
1055
a45ae5f8 1056bfd_boolean
5796c8dc
SS
1057coff_write_alien_symbol (bfd *abfd,
1058 asymbol *symbol,
a45ae5f8 1059 struct internal_syment *isym,
5796c8dc
SS
1060 bfd_vma *written,
1061 bfd_size_type *string_size_p,
1062 asection **debug_string_section_p,
1063 bfd_size_type *debug_string_size_p)
1064{
1065 combined_entry_type *native;
a45ae5f8
JM
1066 combined_entry_type dummy[2];
1067 asection *output_section = symbol->section->output_section
1068 ? symbol->section->output_section
1069 : symbol->section;
1070 struct bfd_link_info *link_info = coff_data (abfd)->link_info;
1071 bfd_boolean ret;
1072
1073 if ((!link_info || link_info->strip_discarded)
1074 && !bfd_is_abs_section (symbol->section)
1075 && symbol->section->output_section == bfd_abs_section_ptr)
1076 {
1077 symbol->name = "";
1078 if (isym != NULL)
1079 memset (isym, 0, sizeof(*isym));
1080 return TRUE;
1081 }
1082 native = dummy;
5796c8dc
SS
1083 native->u.syment.n_type = T_NULL;
1084 native->u.syment.n_flags = 0;
a45ae5f8 1085 native->u.syment.n_numaux = 0;
5796c8dc
SS
1086 if (bfd_is_und_section (symbol->section))
1087 {
1088 native->u.syment.n_scnum = N_UNDEF;
1089 native->u.syment.n_value = symbol->value;
1090 }
1091 else if (bfd_is_com_section (symbol->section))
1092 {
1093 native->u.syment.n_scnum = N_UNDEF;
1094 native->u.syment.n_value = symbol->value;
1095 }
a45ae5f8
JM
1096 else if (symbol->flags & BSF_FILE)
1097 {
1098 native->u.syment.n_scnum = N_DEBUG;
1099 native->u.syment.n_numaux = 1;
1100 }
5796c8dc
SS
1101 else if (symbol->flags & BSF_DEBUGGING)
1102 {
1103 /* There isn't much point to writing out a debugging symbol
1104 unless we are prepared to convert it into COFF debugging
1105 format. So, we just ignore them. We must clobber the symbol
1106 name to keep it from being put in the string table. */
1107 symbol->name = "";
a45ae5f8
JM
1108 if (isym != NULL)
1109 memset (isym, 0, sizeof(*isym));
5796c8dc
SS
1110 return TRUE;
1111 }
1112 else
1113 {
a45ae5f8 1114 native->u.syment.n_scnum = output_section->target_index;
5796c8dc
SS
1115 native->u.syment.n_value = (symbol->value
1116 + symbol->section->output_offset);
1117 if (! obj_pe (abfd))
a45ae5f8 1118 native->u.syment.n_value += output_section->vma;
5796c8dc
SS
1119
1120 /* Copy the any flags from the file header into the symbol.
1121 FIXME: Why? */
1122 {
1123 coff_symbol_type *c = coff_symbol_from (abfd, symbol);
1124 if (c != (coff_symbol_type *) NULL)
1125 native->u.syment.n_flags = bfd_asymbol_bfd (&c->symbol)->flags;
1126 }
1127 }
1128
1129 native->u.syment.n_type = 0;
a45ae5f8
JM
1130 if (symbol->flags & BSF_FILE)
1131 native->u.syment.n_sclass = C_FILE;
1132 else if (symbol->flags & BSF_LOCAL)
5796c8dc
SS
1133 native->u.syment.n_sclass = C_STAT;
1134 else if (symbol->flags & BSF_WEAK)
1135 native->u.syment.n_sclass = obj_pe (abfd) ? C_NT_WEAK : C_WEAKEXT;
1136 else
1137 native->u.syment.n_sclass = C_EXT;
5796c8dc 1138
a45ae5f8
JM
1139 ret = coff_write_symbol (abfd, symbol, native, written, string_size_p,
1140 debug_string_section_p, debug_string_size_p);
1141 if (isym != NULL)
1142 *isym = native->u.syment;
1143 return ret;
5796c8dc
SS
1144}
1145
1146/* Write a native symbol to a COFF file. */
1147
1148static bfd_boolean
1149coff_write_native_symbol (bfd *abfd,
1150 coff_symbol_type *symbol,
1151 bfd_vma *written,
1152 bfd_size_type *string_size_p,
1153 asection **debug_string_section_p,
1154 bfd_size_type *debug_string_size_p)
1155{
1156 combined_entry_type *native = symbol->native;
1157 alent *lineno = symbol->lineno;
a45ae5f8
JM
1158 struct bfd_link_info *link_info = coff_data (abfd)->link_info;
1159
1160 if ((!link_info || link_info->strip_discarded)
1161 && !bfd_is_abs_section (symbol->symbol.section)
1162 && symbol->symbol.section->output_section == bfd_abs_section_ptr)
1163 {
1164 symbol->symbol.name = "";
1165 return TRUE;
1166 }
5796c8dc
SS
1167
1168 /* If this symbol has an associated line number, we must store the
1169 symbol index in the line number field. We also tag the auxent to
1170 point to the right place in the lineno table. */
1171 if (lineno && !symbol->done_lineno && symbol->symbol.section->owner != NULL)
1172 {
1173 unsigned int count = 0;
1174
1175 lineno[count].u.offset = *written;
1176 if (native->u.syment.n_numaux)
1177 {
1178 union internal_auxent *a = &((native + 1)->u.auxent);
1179
1180 a->x_sym.x_fcnary.x_fcn.x_lnnoptr =
1181 symbol->symbol.section->output_section->moving_line_filepos;
1182 }
1183
1184 /* Count and relocate all other linenumbers. */
1185 count++;
1186 while (lineno[count].line_number != 0)
1187 {
1188 lineno[count].u.offset +=
1189 (symbol->symbol.section->output_section->vma
1190 + symbol->symbol.section->output_offset);
1191 count++;
1192 }
1193 symbol->done_lineno = TRUE;
1194
1195 if (! bfd_is_const_section (symbol->symbol.section->output_section))
1196 symbol->symbol.section->output_section->moving_line_filepos +=
1197 count * bfd_coff_linesz (abfd);
1198 }
1199
1200 return coff_write_symbol (abfd, &(symbol->symbol), native, written,
1201 string_size_p, debug_string_section_p,
1202 debug_string_size_p);
1203}
1204
1205static void
1206null_error_handler (const char * fmt ATTRIBUTE_UNUSED, ...)
1207{
1208}
1209
1210/* Write out the COFF symbols. */
1211
1212bfd_boolean
1213coff_write_symbols (bfd *abfd)
1214{
1215 bfd_size_type string_size;
1216 asection *debug_string_section;
1217 bfd_size_type debug_string_size;
1218 unsigned int i;
1219 unsigned int limit = bfd_get_symcount (abfd);
1220 bfd_vma written = 0;
1221 asymbol **p;
1222
1223 string_size = 0;
1224 debug_string_section = NULL;
1225 debug_string_size = 0;
1226
1227 /* If this target supports long section names, they must be put into
1228 the string table. This is supported by PE. This code must
1229 handle section names just as they are handled in
1230 coff_write_object_contents. */
1231 if (bfd_coff_long_section_names (abfd))
1232 {
1233 asection *o;
1234
1235 for (o = abfd->sections; o != NULL; o = o->next)
1236 {
1237 size_t len;
1238
1239 len = strlen (o->name);
1240 if (len > SCNNMLEN)
1241 string_size += len + 1;
1242 }
1243 }
1244
1245 /* Seek to the right place. */
1246 if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0)
1247 return FALSE;
1248
1249 /* Output all the symbols we have. */
1250 written = 0;
1251 for (p = abfd->outsymbols, i = 0; i < limit; i++, p++)
1252 {
1253 asymbol *symbol = *p;
1254 coff_symbol_type *c_symbol = coff_symbol_from (abfd, symbol);
1255
1256 if (c_symbol == (coff_symbol_type *) NULL
1257 || c_symbol->native == (combined_entry_type *) NULL)
1258 {
a45ae5f8
JM
1259 if (!coff_write_alien_symbol (abfd, symbol, NULL, &written,
1260 &string_size, &debug_string_section,
5796c8dc
SS
1261 &debug_string_size))
1262 return FALSE;
1263 }
1264 else
1265 {
1266 if (coff_backend_info (abfd)->_bfd_coff_classify_symbol != NULL)
1267 {
1268 bfd_error_handler_type current_error_handler;
1269 enum coff_symbol_classification sym_class;
1270 unsigned char *n_sclass;
1271
1272 /* Suppress error reporting by bfd_coff_classify_symbol.
1273 Error messages can be generated when we are processing a local
1274 symbol which has no associated section and we do not have to
1275 worry about this, all we need to know is that it is local. */
1276 current_error_handler = bfd_set_error_handler (null_error_handler);
1277 sym_class = bfd_coff_classify_symbol (abfd,
1278 &c_symbol->native->u.syment);
1279 (void) bfd_set_error_handler (current_error_handler);
1280
1281 n_sclass = &c_symbol->native->u.syment.n_sclass;
1282
1283 /* If the symbol class has been changed (eg objcopy/ld script/etc)
1284 we cannot retain the existing sclass from the original symbol.
1285 Weak symbols only have one valid sclass, so just set it always.
1286 If it is not local class and should be, set it C_STAT.
1287 If it is global and not classified as global, or if it is
1288 weak (which is also classified as global), set it C_EXT. */
1289
1290 if (symbol->flags & BSF_WEAK)
1291 *n_sclass = obj_pe (abfd) ? C_NT_WEAK : C_WEAKEXT;
1292 else if (symbol->flags & BSF_LOCAL && sym_class != COFF_SYMBOL_LOCAL)
1293 *n_sclass = C_STAT;
1294 else if (symbol->flags & BSF_GLOBAL
1295 && (sym_class != COFF_SYMBOL_GLOBAL
1296#ifdef COFF_WITH_PE
1297 || *n_sclass == C_NT_WEAK
1298#endif
1299 || *n_sclass == C_WEAKEXT))
1300 c_symbol->native->u.syment.n_sclass = C_EXT;
1301 }
1302
1303 if (!coff_write_native_symbol (abfd, c_symbol, &written,
1304 &string_size, &debug_string_section,
1305 &debug_string_size))
1306 return FALSE;
1307 }
1308 }
1309
1310 obj_raw_syment_count (abfd) = written;
1311
1312 /* Now write out strings. */
1313 if (string_size != 0)
1314 {
1315 unsigned int size = string_size + STRING_SIZE_SIZE;
1316 bfd_byte buffer[STRING_SIZE_SIZE];
1317
1318#if STRING_SIZE_SIZE == 4
1319 H_PUT_32 (abfd, size, buffer);
1320#else
1321 #error Change H_PUT_32
1322#endif
1323 if (bfd_bwrite ((void *) buffer, (bfd_size_type) sizeof (buffer), abfd)
1324 != sizeof (buffer))
1325 return FALSE;
1326
1327 /* Handle long section names. This code must handle section
1328 names just as they are handled in coff_write_object_contents. */
1329 if (bfd_coff_long_section_names (abfd))
1330 {
1331 asection *o;
1332
1333 for (o = abfd->sections; o != NULL; o = o->next)
1334 {
1335 size_t len;
1336
1337 len = strlen (o->name);
1338 if (len > SCNNMLEN)
1339 {
1340 if (bfd_bwrite (o->name, (bfd_size_type) (len + 1), abfd)
1341 != len + 1)
1342 return FALSE;
1343 }
1344 }
1345 }
1346
1347 for (p = abfd->outsymbols, i = 0;
1348 i < limit;
1349 i++, p++)
1350 {
1351 asymbol *q = *p;
1352 size_t name_length = strlen (q->name);
1353 coff_symbol_type *c_symbol = coff_symbol_from (abfd, q);
1354 size_t maxlen;
1355
1356 /* Figure out whether the symbol name should go in the string
1357 table. Symbol names that are short enough are stored
1358 directly in the syment structure. File names permit a
1359 different, longer, length in the syment structure. On
1360 XCOFF, some symbol names are stored in the .debug section
1361 rather than in the string table. */
1362
1363 if (c_symbol == NULL
1364 || c_symbol->native == NULL)
1365 /* This is not a COFF symbol, so it certainly is not a
1366 file name, nor does it go in the .debug section. */
1367 maxlen = bfd_coff_force_symnames_in_strings (abfd) ? 0 : SYMNMLEN;
1368
1369 else if (bfd_coff_symname_in_debug (abfd,
1370 &c_symbol->native->u.syment))
1371 /* This symbol name is in the XCOFF .debug section.
1372 Don't write it into the string table. */
1373 maxlen = name_length;
1374
1375 else if (c_symbol->native->u.syment.n_sclass == C_FILE
1376 && c_symbol->native->u.syment.n_numaux > 0)
1377 {
1378 if (bfd_coff_force_symnames_in_strings (abfd))
1379 {
1380 if (bfd_bwrite (".file", (bfd_size_type) 6, abfd) != 6)
1381 return FALSE;
1382 }
1383 maxlen = bfd_coff_filnmlen (abfd);
1384 }
1385 else
1386 maxlen = bfd_coff_force_symnames_in_strings (abfd) ? 0 : SYMNMLEN;
1387
1388 if (name_length > maxlen)
1389 {
1390 if (bfd_bwrite ((void *) (q->name), (bfd_size_type) name_length + 1,
1391 abfd) != name_length + 1)
1392 return FALSE;
1393 }
1394 }
1395 }
1396 else
1397 {
1398 /* We would normally not write anything here, but we'll write
1399 out 4 so that any stupid coff reader which tries to read the
1400 string table even when there isn't one won't croak. */
1401 unsigned int size = STRING_SIZE_SIZE;
1402 bfd_byte buffer[STRING_SIZE_SIZE];
1403
1404#if STRING_SIZE_SIZE == 4
1405 H_PUT_32 (abfd, size, buffer);
1406#else
1407 #error Change H_PUT_32
1408#endif
1409 if (bfd_bwrite ((void *) buffer, (bfd_size_type) STRING_SIZE_SIZE, abfd)
1410 != STRING_SIZE_SIZE)
1411 return FALSE;
1412 }
1413
1414 /* Make sure the .debug section was created to be the correct size.
1415 We should create it ourselves on the fly, but we don't because
1416 BFD won't let us write to any section until we know how large all
1417 the sections are. We could still do it by making another pass
1418 over the symbols. FIXME. */
1419 BFD_ASSERT (debug_string_size == 0
1420 || (debug_string_section != (asection *) NULL
1421 && (BFD_ALIGN (debug_string_size,
1422 1 << debug_string_section->alignment_power)
1423 == debug_string_section->size)));
1424
1425 return TRUE;
1426}
1427
1428bfd_boolean
1429coff_write_linenumbers (bfd *abfd)
1430{
1431 asection *s;
1432 bfd_size_type linesz;
1433 void * buff;
1434
1435 linesz = bfd_coff_linesz (abfd);
1436 buff = bfd_alloc (abfd, linesz);
1437 if (!buff)
1438 return FALSE;
1439 for (s = abfd->sections; s != (asection *) NULL; s = s->next)
1440 {
1441 if (s->lineno_count)
1442 {
1443 asymbol **q = abfd->outsymbols;
1444 if (bfd_seek (abfd, s->line_filepos, SEEK_SET) != 0)
1445 return FALSE;
1446 /* Find all the linenumbers in this section. */
1447 while (*q)
1448 {
1449 asymbol *p = *q;
1450 if (p->section->output_section == s)
1451 {
1452 alent *l =
1453 BFD_SEND (bfd_asymbol_bfd (p), _get_lineno,
1454 (bfd_asymbol_bfd (p), p));
1455 if (l)
1456 {
1457 /* Found a linenumber entry, output. */
1458 struct internal_lineno out;
1459 memset ((void *) & out, 0, sizeof (out));
1460 out.l_lnno = 0;
1461 out.l_addr.l_symndx = l->u.offset;
1462 bfd_coff_swap_lineno_out (abfd, &out, buff);
1463 if (bfd_bwrite (buff, (bfd_size_type) linesz, abfd)
1464 != linesz)
1465 return FALSE;
1466 l++;
1467 while (l->line_number)
1468 {
1469 out.l_lnno = l->line_number;
1470 out.l_addr.l_symndx = l->u.offset;
1471 bfd_coff_swap_lineno_out (abfd, &out, buff);
1472 if (bfd_bwrite (buff, (bfd_size_type) linesz, abfd)
1473 != linesz)
1474 return FALSE;
1475 l++;
1476 }
1477 }
1478 }
1479 q++;
1480 }
1481 }
1482 }
1483 bfd_release (abfd, buff);
1484 return TRUE;
1485}
1486
1487alent *
1488coff_get_lineno (bfd *ignore_abfd ATTRIBUTE_UNUSED, asymbol *symbol)
1489{
1490 return coffsymbol (symbol)->lineno;
1491}
1492
1493/* This function transforms the offsets into the symbol table into
1494 pointers to syments. */
1495
1496static void
1497coff_pointerize_aux (bfd *abfd,
1498 combined_entry_type *table_base,
1499 combined_entry_type *symbol,
1500 unsigned int indaux,
1501 combined_entry_type *auxent)
1502{
1503 unsigned int type = symbol->u.syment.n_type;
1504 unsigned int n_sclass = symbol->u.syment.n_sclass;
1505
1506 if (coff_backend_info (abfd)->_bfd_coff_pointerize_aux_hook)
1507 {
1508 if ((*coff_backend_info (abfd)->_bfd_coff_pointerize_aux_hook)
1509 (abfd, table_base, symbol, indaux, auxent))
1510 return;
1511 }
1512
1513 /* Don't bother if this is a file or a section. */
1514 if (n_sclass == C_STAT && type == T_NULL)
1515 return;
1516 if (n_sclass == C_FILE)
1517 return;
1518
1519 /* Otherwise patch up. */
1520#define N_TMASK coff_data (abfd)->local_n_tmask
1521#define N_BTSHFT coff_data (abfd)->local_n_btshft
ef5ccd6c 1522
5796c8dc
SS
1523 if ((ISFCN (type) || ISTAG (n_sclass) || n_sclass == C_BLOCK
1524 || n_sclass == C_FCN)
1525 && auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l > 0)
1526 {
1527 auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p =
1528 table_base + auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l;
1529 auxent->fix_end = 1;
1530 }
1531 /* A negative tagndx is meaningless, but the SCO 3.2v4 cc can
1532 generate one, so we must be careful to ignore it. */
1533 if (auxent->u.auxent.x_sym.x_tagndx.l > 0)
1534 {
1535 auxent->u.auxent.x_sym.x_tagndx.p =
1536 table_base + auxent->u.auxent.x_sym.x_tagndx.l;
1537 auxent->fix_tag = 1;
1538 }
1539}
1540
1541/* Allocate space for the ".debug" section, and read it.
1542 We did not read the debug section until now, because
1543 we didn't want to go to the trouble until someone needed it. */
1544
1545static char *
1546build_debug_section (bfd *abfd)
1547{
1548 char *debug_section;
1549 file_ptr position;
1550 bfd_size_type sec_size;
1551
1552 asection *sect = bfd_get_section_by_name (abfd, ".debug");
1553
1554 if (!sect)
1555 {
1556 bfd_set_error (bfd_error_no_debug_section);
1557 return NULL;
1558 }
1559
1560 sec_size = sect->size;
1561 debug_section = (char *) bfd_alloc (abfd, sec_size);
1562 if (debug_section == NULL)
1563 return NULL;
1564
1565 /* Seek to the beginning of the `.debug' section and read it.
1566 Save the current position first; it is needed by our caller.
1567 Then read debug section and reset the file pointer. */
1568
1569 position = bfd_tell (abfd);
1570 if (bfd_seek (abfd, sect->filepos, SEEK_SET) != 0
1571 || bfd_bread (debug_section, sec_size, abfd) != sec_size
1572 || bfd_seek (abfd, position, SEEK_SET) != 0)
1573 return NULL;
1574 return debug_section;
1575}
1576
1577/* Return a pointer to a malloc'd copy of 'name'. 'name' may not be
1578 \0-terminated, but will not exceed 'maxlen' characters. The copy *will*
1579 be \0-terminated. */
1580
1581static char *
1582copy_name (bfd *abfd, char *name, size_t maxlen)
1583{
1584 size_t len;
1585 char *newname;
1586
1587 for (len = 0; len < maxlen; ++len)
1588 if (name[len] == '\0')
1589 break;
1590
1591 if ((newname = (char *) bfd_alloc (abfd, (bfd_size_type) len + 1)) == NULL)
1592 return NULL;
1593
1594 strncpy (newname, name, len);
1595 newname[len] = '\0';
1596 return newname;
1597}
1598
1599/* Read in the external symbols. */
1600
1601bfd_boolean
1602_bfd_coff_get_external_symbols (bfd *abfd)
1603{
1604 bfd_size_type symesz;
1605 bfd_size_type size;
1606 void * syms;
1607
1608 if (obj_coff_external_syms (abfd) != NULL)
1609 return TRUE;
1610
1611 symesz = bfd_coff_symesz (abfd);
1612
1613 size = obj_raw_syment_count (abfd) * symesz;
1614 if (size == 0)
1615 return TRUE;
1616
1617 syms = bfd_malloc (size);
1618 if (syms == NULL)
1619 return FALSE;
1620
1621 if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0
1622 || bfd_bread (syms, size, abfd) != size)
1623 {
1624 if (syms != NULL)
1625 free (syms);
1626 return FALSE;
1627 }
1628
1629 obj_coff_external_syms (abfd) = syms;
1630
1631 return TRUE;
1632}
1633
1634/* Read in the external strings. The strings are not loaded until
1635 they are needed. This is because we have no simple way of
1636 detecting a missing string table in an archive. */
1637
1638const char *
1639_bfd_coff_read_string_table (bfd *abfd)
1640{
1641 char extstrsize[STRING_SIZE_SIZE];
1642 bfd_size_type strsize;
1643 char *strings;
1644 file_ptr pos;
1645
1646 if (obj_coff_strings (abfd) != NULL)
1647 return obj_coff_strings (abfd);
1648
1649 if (obj_sym_filepos (abfd) == 0)
1650 {
1651 bfd_set_error (bfd_error_no_symbols);
1652 return NULL;
1653 }
1654
1655 pos = obj_sym_filepos (abfd);
1656 pos += obj_raw_syment_count (abfd) * bfd_coff_symesz (abfd);
1657 if (bfd_seek (abfd, pos, SEEK_SET) != 0)
1658 return NULL;
1659
1660 if (bfd_bread (extstrsize, (bfd_size_type) sizeof extstrsize, abfd)
1661 != sizeof extstrsize)
1662 {
1663 if (bfd_get_error () != bfd_error_file_truncated)
1664 return NULL;
1665
1666 /* There is no string table. */
1667 strsize = STRING_SIZE_SIZE;
1668 }
1669 else
1670 {
1671#if STRING_SIZE_SIZE == 4
1672 strsize = H_GET_32 (abfd, extstrsize);
1673#else
1674 #error Change H_GET_32
1675#endif
1676 }
1677
1678 if (strsize < STRING_SIZE_SIZE)
1679 {
1680 (*_bfd_error_handler)
1681 (_("%B: bad string table size %lu"), abfd, (unsigned long) strsize);
1682 bfd_set_error (bfd_error_bad_value);
1683 return NULL;
1684 }
1685
1686 strings = (char *) bfd_malloc (strsize);
1687 if (strings == NULL)
1688 return NULL;
1689
1690 if (bfd_bread (strings + STRING_SIZE_SIZE, strsize - STRING_SIZE_SIZE, abfd)
1691 != strsize - STRING_SIZE_SIZE)
1692 {
1693 free (strings);
1694 return NULL;
1695 }
1696
1697 obj_coff_strings (abfd) = strings;
1698
1699 return strings;
1700}
1701
1702/* Free up the external symbols and strings read from a COFF file. */
1703
1704bfd_boolean
1705_bfd_coff_free_symbols (bfd *abfd)
1706{
1707 if (obj_coff_external_syms (abfd) != NULL
1708 && ! obj_coff_keep_syms (abfd))
1709 {
1710 free (obj_coff_external_syms (abfd));
1711 obj_coff_external_syms (abfd) = NULL;
1712 }
1713 if (obj_coff_strings (abfd) != NULL
1714 && ! obj_coff_keep_strings (abfd))
1715 {
1716 free (obj_coff_strings (abfd));
1717 obj_coff_strings (abfd) = NULL;
1718 }
1719 return TRUE;
1720}
1721
1722/* Read a symbol table into freshly bfd_allocated memory, swap it, and
1723 knit the symbol names into a normalized form. By normalized here I
1724 mean that all symbols have an n_offset pointer that points to a null-
1725 terminated string. */
1726
1727combined_entry_type *
1728coff_get_normalized_symtab (bfd *abfd)
1729{
1730 combined_entry_type *internal;
1731 combined_entry_type *internal_ptr;
1732 combined_entry_type *symbol_ptr;
1733 combined_entry_type *internal_end;
1734 size_t symesz;
1735 char *raw_src;
1736 char *raw_end;
1737 const char *string_table = NULL;
1738 char *debug_section = NULL;
1739 bfd_size_type size;
1740
1741 if (obj_raw_syments (abfd) != NULL)
1742 return obj_raw_syments (abfd);
1743
1744 size = obj_raw_syment_count (abfd) * sizeof (combined_entry_type);
1745 internal = (combined_entry_type *) bfd_zalloc (abfd, size);
1746 if (internal == NULL && size != 0)
1747 return NULL;
1748 internal_end = internal + obj_raw_syment_count (abfd);
1749
1750 if (! _bfd_coff_get_external_symbols (abfd))
1751 return NULL;
1752
1753 raw_src = (char *) obj_coff_external_syms (abfd);
1754
1755 /* Mark the end of the symbols. */
1756 symesz = bfd_coff_symesz (abfd);
1757 raw_end = (char *) raw_src + obj_raw_syment_count (abfd) * symesz;
1758
1759 /* FIXME SOMEDAY. A string table size of zero is very weird, but
1760 probably possible. If one shows up, it will probably kill us. */
1761
1762 /* Swap all the raw entries. */
1763 for (internal_ptr = internal;
1764 raw_src < raw_end;
1765 raw_src += symesz, internal_ptr++)
1766 {
1767
1768 unsigned int i;
1769 bfd_coff_swap_sym_in (abfd, (void *) raw_src,
1770 (void *) & internal_ptr->u.syment);
1771 symbol_ptr = internal_ptr;
1772
1773 for (i = 0;
1774 i < symbol_ptr->u.syment.n_numaux;
1775 i++)
1776 {
1777 internal_ptr++;
1778 raw_src += symesz;
1779 bfd_coff_swap_aux_in (abfd, (void *) raw_src,
1780 symbol_ptr->u.syment.n_type,
1781 symbol_ptr->u.syment.n_sclass,
1782 (int) i, symbol_ptr->u.syment.n_numaux,
1783 &(internal_ptr->u.auxent));
1784 coff_pointerize_aux (abfd, internal, symbol_ptr, i,
1785 internal_ptr);
1786 }
1787 }
1788
1789 /* Free the raw symbols, but not the strings (if we have them). */
1790 obj_coff_keep_strings (abfd) = TRUE;
1791 if (! _bfd_coff_free_symbols (abfd))
1792 return NULL;
1793
1794 for (internal_ptr = internal; internal_ptr < internal_end;
1795 internal_ptr++)
1796 {
1797 if (internal_ptr->u.syment.n_sclass == C_FILE
1798 && internal_ptr->u.syment.n_numaux > 0)
1799 {
1800 /* Make a file symbol point to the name in the auxent, since
1801 the text ".file" is redundant. */
1802 if ((internal_ptr + 1)->u.auxent.x_file.x_n.x_zeroes == 0)
1803 {
1804 /* The filename is a long one, point into the string table. */
1805 if (string_table == NULL)
1806 {
1807 string_table = _bfd_coff_read_string_table (abfd);
1808 if (string_table == NULL)
1809 return NULL;
1810 }
1811
1812 internal_ptr->u.syment._n._n_n._n_offset =
1813 ((bfd_hostptr_t)
1814 (string_table
1815 + (internal_ptr + 1)->u.auxent.x_file.x_n.x_offset));
1816 }
1817 else
1818 {
1819 /* Ordinary short filename, put into memory anyway. The
1820 Microsoft PE tools sometimes store a filename in
1821 multiple AUX entries. */
1822 if (internal_ptr->u.syment.n_numaux > 1
1823 && coff_data (abfd)->pe)
1824 internal_ptr->u.syment._n._n_n._n_offset =
1825 ((bfd_hostptr_t)
1826 copy_name (abfd,
1827 (internal_ptr + 1)->u.auxent.x_file.x_fname,
1828 internal_ptr->u.syment.n_numaux * symesz));
1829 else
1830 internal_ptr->u.syment._n._n_n._n_offset =
1831 ((bfd_hostptr_t)
1832 copy_name (abfd,
1833 (internal_ptr + 1)->u.auxent.x_file.x_fname,
1834 (size_t) bfd_coff_filnmlen (abfd)));
1835 }
1836 }
1837 else
1838 {
1839 if (internal_ptr->u.syment._n._n_n._n_zeroes != 0)
1840 {
1841 /* This is a "short" name. Make it long. */
1842 size_t i;
1843 char *newstring;
1844
1845 /* Find the length of this string without walking into memory
1846 that isn't ours. */
1847 for (i = 0; i < 8; ++i)
1848 if (internal_ptr->u.syment._n._n_name[i] == '\0')
1849 break;
1850
1851 newstring = (char *) bfd_zalloc (abfd, (bfd_size_type) (i + 1));
1852 if (newstring == NULL)
1853 return NULL;
1854 strncpy (newstring, internal_ptr->u.syment._n._n_name, i);
1855 internal_ptr->u.syment._n._n_n._n_offset = (bfd_hostptr_t) newstring;
1856 internal_ptr->u.syment._n._n_n._n_zeroes = 0;
1857 }
1858 else if (internal_ptr->u.syment._n._n_n._n_offset == 0)
1859 internal_ptr->u.syment._n._n_n._n_offset = (bfd_hostptr_t) "";
1860 else if (!bfd_coff_symname_in_debug (abfd, &internal_ptr->u.syment))
1861 {
1862 /* Long name already. Point symbol at the string in the
1863 table. */
1864 if (string_table == NULL)
1865 {
1866 string_table = _bfd_coff_read_string_table (abfd);
1867 if (string_table == NULL)
1868 return NULL;
1869 }
1870 internal_ptr->u.syment._n._n_n._n_offset =
1871 ((bfd_hostptr_t)
1872 (string_table
1873 + internal_ptr->u.syment._n._n_n._n_offset));
1874 }
1875 else
1876 {
1877 /* Long name in debug section. Very similar. */
1878 if (debug_section == NULL)
1879 debug_section = build_debug_section (abfd);
1880 internal_ptr->u.syment._n._n_n._n_offset = (bfd_hostptr_t)
1881 (debug_section + internal_ptr->u.syment._n._n_n._n_offset);
1882 }
1883 }
1884 internal_ptr += internal_ptr->u.syment.n_numaux;
1885 }
1886
1887 obj_raw_syments (abfd) = internal;
1888 BFD_ASSERT (obj_raw_syment_count (abfd)
1889 == (unsigned int) (internal_ptr - internal));
1890
1891 return internal;
1892}
1893
1894long
1895coff_get_reloc_upper_bound (bfd *abfd, sec_ptr asect)
1896{
1897 if (bfd_get_format (abfd) != bfd_object)
1898 {
1899 bfd_set_error (bfd_error_invalid_operation);
1900 return -1;
1901 }
1902 return (asect->reloc_count + 1) * sizeof (arelent *);
1903}
1904
1905asymbol *
1906coff_make_empty_symbol (bfd *abfd)
1907{
1908 bfd_size_type amt = sizeof (coff_symbol_type);
1909 coff_symbol_type *new_symbol = (coff_symbol_type *) bfd_zalloc (abfd, amt);
1910
1911 if (new_symbol == NULL)
1912 return NULL;
1913 new_symbol->symbol.section = 0;
1914 new_symbol->native = 0;
1915 new_symbol->lineno = NULL;
1916 new_symbol->done_lineno = FALSE;
1917 new_symbol->symbol.the_bfd = abfd;
1918
1919 return & new_symbol->symbol;
1920}
1921
1922/* Make a debugging symbol. */
1923
1924asymbol *
1925coff_bfd_make_debug_symbol (bfd *abfd,
1926 void * ptr ATTRIBUTE_UNUSED,
1927 unsigned long sz ATTRIBUTE_UNUSED)
1928{
1929 bfd_size_type amt = sizeof (coff_symbol_type);
1930 coff_symbol_type *new_symbol = (coff_symbol_type *) bfd_alloc (abfd, amt);
1931
1932 if (new_symbol == NULL)
1933 return NULL;
1934 /* @@ The 10 is a guess at a plausible maximum number of aux entries
1935 (but shouldn't be a constant). */
1936 amt = sizeof (combined_entry_type) * 10;
1937 new_symbol->native = (combined_entry_type *) bfd_zalloc (abfd, amt);
1938 if (!new_symbol->native)
1939 return NULL;
1940 new_symbol->symbol.section = bfd_abs_section_ptr;
1941 new_symbol->symbol.flags = BSF_DEBUGGING;
1942 new_symbol->lineno = NULL;
1943 new_symbol->done_lineno = FALSE;
1944 new_symbol->symbol.the_bfd = abfd;
ef5ccd6c 1945
5796c8dc
SS
1946 return & new_symbol->symbol;
1947}
1948
1949void
1950coff_get_symbol_info (bfd *abfd, asymbol *symbol, symbol_info *ret)
1951{
1952 bfd_symbol_info (symbol, ret);
1953
1954 if (coffsymbol (symbol)->native != NULL
1955 && coffsymbol (symbol)->native->fix_value)
1956 ret->value = coffsymbol (symbol)->native->u.syment.n_value -
1957 (bfd_hostptr_t) obj_raw_syments (abfd);
1958}
1959
1960/* Return the COFF syment for a symbol. */
1961
1962bfd_boolean
1963bfd_coff_get_syment (bfd *abfd,
1964 asymbol *symbol,
1965 struct internal_syment *psyment)
1966{
1967 coff_symbol_type *csym;
1968
1969 csym = coff_symbol_from (abfd, symbol);
1970 if (csym == NULL || csym->native == NULL)
1971 {
1972 bfd_set_error (bfd_error_invalid_operation);
1973 return FALSE;
1974 }
1975
1976 *psyment = csym->native->u.syment;
1977
1978 if (csym->native->fix_value)
1979 psyment->n_value = psyment->n_value -
1980 (bfd_hostptr_t) obj_raw_syments (abfd);
1981
1982 /* FIXME: We should handle fix_line here. */
1983
1984 return TRUE;
1985}
1986
1987/* Return the COFF auxent for a symbol. */
1988
1989bfd_boolean
1990bfd_coff_get_auxent (bfd *abfd,
1991 asymbol *symbol,
1992 int indx,
1993 union internal_auxent *pauxent)
1994{
1995 coff_symbol_type *csym;
1996 combined_entry_type *ent;
1997
1998 csym = coff_symbol_from (abfd, symbol);
1999
2000 if (csym == NULL
2001 || csym->native == NULL
2002 || indx >= csym->native->u.syment.n_numaux)
2003 {
2004 bfd_set_error (bfd_error_invalid_operation);
2005 return FALSE;
2006 }
2007
2008 ent = csym->native + indx + 1;
2009
2010 *pauxent = ent->u.auxent;
2011
2012 if (ent->fix_tag)
2013 pauxent->x_sym.x_tagndx.l =
2014 ((combined_entry_type *) pauxent->x_sym.x_tagndx.p
2015 - obj_raw_syments (abfd));
2016
2017 if (ent->fix_end)
2018 pauxent->x_sym.x_fcnary.x_fcn.x_endndx.l =
2019 ((combined_entry_type *) pauxent->x_sym.x_fcnary.x_fcn.x_endndx.p
2020 - obj_raw_syments (abfd));
2021
2022 if (ent->fix_scnlen)
2023 pauxent->x_csect.x_scnlen.l =
2024 ((combined_entry_type *) pauxent->x_csect.x_scnlen.p
2025 - obj_raw_syments (abfd));
2026
2027 return TRUE;
2028}
2029
2030/* Print out information about COFF symbol. */
2031
2032void
2033coff_print_symbol (bfd *abfd,
2034 void * filep,
2035 asymbol *symbol,
2036 bfd_print_symbol_type how)
2037{
2038 FILE * file = (FILE *) filep;
2039
2040 switch (how)
2041 {
2042 case bfd_print_symbol_name:
2043 fprintf (file, "%s", symbol->name);
2044 break;
2045
2046 case bfd_print_symbol_more:
2047 fprintf (file, "coff %s %s",
2048 coffsymbol (symbol)->native ? "n" : "g",
2049 coffsymbol (symbol)->lineno ? "l" : " ");
2050 break;
2051
2052 case bfd_print_symbol_all:
2053 if (coffsymbol (symbol)->native)
2054 {
2055 bfd_vma val;
2056 unsigned int aux;
2057 combined_entry_type *combined = coffsymbol (symbol)->native;
2058 combined_entry_type *root = obj_raw_syments (abfd);
2059 struct lineno_cache_entry *l = coffsymbol (symbol)->lineno;
2060
2061 fprintf (file, "[%3ld]", (long) (combined - root));
2062
2063 if (! combined->fix_value)
2064 val = (bfd_vma) combined->u.syment.n_value;
2065 else
2066 val = combined->u.syment.n_value - (bfd_hostptr_t) root;
2067
2068 fprintf (file, "(sec %2d)(fl 0x%02x)(ty %3x)(scl %3d) (nx %d) 0x",
2069 combined->u.syment.n_scnum,
2070 combined->u.syment.n_flags,
2071 combined->u.syment.n_type,
2072 combined->u.syment.n_sclass,
2073 combined->u.syment.n_numaux);
2074 bfd_fprintf_vma (abfd, file, val);
2075 fprintf (file, " %s", symbol->name);
2076
2077 for (aux = 0; aux < combined->u.syment.n_numaux; aux++)
2078 {
2079 combined_entry_type *auxp = combined + aux + 1;
2080 long tagndx;
2081
2082 if (auxp->fix_tag)
2083 tagndx = auxp->u.auxent.x_sym.x_tagndx.p - root;
2084 else
2085 tagndx = auxp->u.auxent.x_sym.x_tagndx.l;
2086
2087 fprintf (file, "\n");
2088
2089 if (bfd_coff_print_aux (abfd, file, root, combined, auxp, aux))
2090 continue;
2091
2092 switch (combined->u.syment.n_sclass)
2093 {
2094 case C_FILE:
2095 fprintf (file, "File ");
2096 break;
2097
2098 case C_STAT:
2099 if (combined->u.syment.n_type == T_NULL)
2100 /* Probably a section symbol ? */
2101 {
2102 fprintf (file, "AUX scnlen 0x%lx nreloc %d nlnno %d",
2103 (unsigned long) auxp->u.auxent.x_scn.x_scnlen,
2104 auxp->u.auxent.x_scn.x_nreloc,
2105 auxp->u.auxent.x_scn.x_nlinno);
2106 if (auxp->u.auxent.x_scn.x_checksum != 0
2107 || auxp->u.auxent.x_scn.x_associated != 0
2108 || auxp->u.auxent.x_scn.x_comdat != 0)
2109 fprintf (file, " checksum 0x%lx assoc %d comdat %d",
2110 auxp->u.auxent.x_scn.x_checksum,
2111 auxp->u.auxent.x_scn.x_associated,
2112 auxp->u.auxent.x_scn.x_comdat);
2113 break;
2114 }
2115 /* Otherwise fall through. */
2116 case C_EXT:
2117 case C_AIX_WEAKEXT:
2118 if (ISFCN (combined->u.syment.n_type))
2119 {
2120 long next, llnos;
2121
2122 if (auxp->fix_end)
2123 next = (auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p
2124 - root);
2125 else
2126 next = auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l;
2127 llnos = auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_lnnoptr;
2128 fprintf (file,
2129 "AUX tagndx %ld ttlsiz 0x%lx lnnos %ld next %ld",
2130 tagndx,
2131 (unsigned long) auxp->u.auxent.x_sym.x_misc.x_fsize,
2132 llnos, next);
2133 break;
2134 }
2135 /* Otherwise fall through. */
2136 default:
2137 fprintf (file, "AUX lnno %d size 0x%x tagndx %ld",
2138 auxp->u.auxent.x_sym.x_misc.x_lnsz.x_lnno,
2139 auxp->u.auxent.x_sym.x_misc.x_lnsz.x_size,
2140 tagndx);
2141 if (auxp->fix_end)
2142 fprintf (file, " endndx %ld",
2143 ((long)
2144 (auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p
2145 - root)));
2146 break;
2147 }
2148 }
2149
2150 if (l)
2151 {
2152 fprintf (file, "\n%s :", l->u.sym->name);
2153 l++;
2154 while (l->line_number)
2155 {
2156 fprintf (file, "\n%4d : ", l->line_number);
2157 bfd_fprintf_vma (abfd, file, l->u.offset + symbol->section->vma);
2158 l++;
2159 }
2160 }
2161 }
2162 else
2163 {
2164 bfd_print_symbol_vandf (abfd, (void *) file, symbol);
2165 fprintf (file, " %-5s %s %s %s",
2166 symbol->section->name,
2167 coffsymbol (symbol)->native ? "n" : "g",
2168 coffsymbol (symbol)->lineno ? "l" : " ",
2169 symbol->name);
2170 }
2171 }
2172}
2173
2174/* Return whether a symbol name implies a local symbol. In COFF,
2175 local symbols generally start with ``.L''. Most targets use this
2176 function for the is_local_label_name entry point, but some may
2177 override it. */
2178
2179bfd_boolean
2180_bfd_coff_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
2181 const char *name)
2182{
2183 return name[0] == '.' && name[1] == 'L';
2184}
2185
2186/* Provided a BFD, a section and an offset (in bytes, not octets) into the
2187 section, calculate and return the name of the source file and the line
2188 nearest to the wanted location. */
2189
2190bfd_boolean
a45ae5f8
JM
2191coff_find_nearest_line_with_names (bfd *abfd,
2192 const struct dwarf_debug_section *debug_sections,
2193 asection *section,
2194 asymbol **symbols,
2195 bfd_vma offset,
2196 const char **filename_ptr,
2197 const char **functionname_ptr,
2198 unsigned int *line_ptr)
5796c8dc
SS
2199{
2200 bfd_boolean found;
2201 unsigned int i;
2202 unsigned int line_base;
2203 coff_data_type *cof = coff_data (abfd);
2204 /* Run through the raw syments if available. */
2205 combined_entry_type *p;
2206 combined_entry_type *pend;
2207 alent *l;
2208 struct coff_section_tdata *sec_data;
2209 bfd_size_type amt;
2210
2211 /* Before looking through the symbol table, try to use a .stab
2212 section to find the information. */
2213 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
2214 &found, filename_ptr,
2215 functionname_ptr, line_ptr,
2216 &coff_data(abfd)->line_info))
2217 return FALSE;
2218
2219 if (found)
2220 return TRUE;
2221
2222 /* Also try examining DWARF2 debugging information. */
a45ae5f8
JM
2223 if (_bfd_dwarf2_find_nearest_line (abfd, debug_sections,
2224 section, symbols, offset,
5796c8dc 2225 filename_ptr, functionname_ptr,
ef5ccd6c 2226 line_ptr, NULL, 0,
5796c8dc
SS
2227 &coff_data(abfd)->dwarf2_find_line_info))
2228 return TRUE;
2229
2230 *filename_ptr = 0;
2231 *functionname_ptr = 0;
2232 *line_ptr = 0;
2233
2234 /* Don't try and find line numbers in a non coff file. */
2235 if (!bfd_family_coff (abfd))
2236 return FALSE;
2237
2238 if (cof == NULL)
2239 return FALSE;
2240
2241 /* Find the first C_FILE symbol. */
2242 p = cof->raw_syments;
2243 if (!p)
2244 return FALSE;
2245
2246 pend = p + cof->raw_syment_count;
2247 while (p < pend)
2248 {
2249 if (p->u.syment.n_sclass == C_FILE)
2250 break;
2251 p += 1 + p->u.syment.n_numaux;
2252 }
2253
2254 if (p < pend)
2255 {
2256 bfd_vma sec_vma;
2257 bfd_vma maxdiff;
2258
2259 /* Look through the C_FILE symbols to find the best one. */
2260 sec_vma = bfd_get_section_vma (abfd, section);
2261 *filename_ptr = (char *) p->u.syment._n._n_n._n_offset;
2262 maxdiff = (bfd_vma) 0 - (bfd_vma) 1;
2263 while (1)
2264 {
cf7f2e2d 2265 bfd_vma file_addr;
5796c8dc
SS
2266 combined_entry_type *p2;
2267
2268 for (p2 = p + 1 + p->u.syment.n_numaux;
2269 p2 < pend;
2270 p2 += 1 + p2->u.syment.n_numaux)
2271 {
2272 if (p2->u.syment.n_scnum > 0
2273 && (section
2274 == coff_section_from_bfd_index (abfd,
2275 p2->u.syment.n_scnum)))
2276 break;
2277 if (p2->u.syment.n_sclass == C_FILE)
2278 {
2279 p2 = pend;
2280 break;
2281 }
2282 }
2283
cf7f2e2d
JM
2284 file_addr = (bfd_vma) p2->u.syment.n_value;
2285 /* PR 11512: Include the section address of the function name symbol. */
2286 if (p2->u.syment.n_scnum > 0)
2287 file_addr += coff_section_from_bfd_index (abfd,
2288 p2->u.syment.n_scnum)->vma;
5796c8dc
SS
2289 /* We use <= MAXDIFF here so that if we get a zero length
2290 file, we actually use the next file entry. */
2291 if (p2 < pend
cf7f2e2d
JM
2292 && offset + sec_vma >= file_addr
2293 && offset + sec_vma - file_addr <= maxdiff)
5796c8dc
SS
2294 {
2295 *filename_ptr = (char *) p->u.syment._n._n_n._n_offset;
2296 maxdiff = offset + sec_vma - p2->u.syment.n_value;
2297 }
2298
2299 /* Avoid endless loops on erroneous files by ensuring that
2300 we always move forward in the file. */
2301 if (p >= cof->raw_syments + p->u.syment.n_value)
2302 break;
2303
2304 p = cof->raw_syments + p->u.syment.n_value;
2305 if (p > pend || p->u.syment.n_sclass != C_FILE)
2306 break;
2307 }
2308 }
2309
2310 /* Now wander though the raw linenumbers of the section. */
2311 /* If we have been called on this section before, and the offset we
2312 want is further down then we can prime the lookup loop. */
2313 sec_data = coff_section_data (abfd, section);
2314 if (sec_data != NULL
2315 && sec_data->i > 0
2316 && offset >= sec_data->offset)
2317 {
2318 i = sec_data->i;
2319 *functionname_ptr = sec_data->function;
2320 line_base = sec_data->line_base;
2321 }
2322 else
2323 {
2324 i = 0;
2325 line_base = 0;
2326 }
2327
2328 if (section->lineno != NULL)
2329 {
2330 bfd_vma last_value = 0;
2331
2332 l = &section->lineno[i];
2333
2334 for (; i < section->lineno_count; i++)
2335 {
2336 if (l->line_number == 0)
2337 {
2338 /* Get the symbol this line number points at. */
2339 coff_symbol_type *coff = (coff_symbol_type *) (l->u.sym);
2340 if (coff->symbol.value > offset)
2341 break;
2342 *functionname_ptr = coff->symbol.name;
2343 last_value = coff->symbol.value;
2344 if (coff->native)
2345 {
2346 combined_entry_type *s = coff->native;
2347 s = s + 1 + s->u.syment.n_numaux;
2348
2349 /* In XCOFF a debugging symbol can follow the
2350 function symbol. */
2351 if (s->u.syment.n_scnum == N_DEBUG)
2352 s = s + 1 + s->u.syment.n_numaux;
2353
2354 /* S should now point to the .bf of the function. */
2355 if (s->u.syment.n_numaux)
2356 {
2357 /* The linenumber is stored in the auxent. */
2358 union internal_auxent *a = &((s + 1)->u.auxent);
2359 line_base = a->x_sym.x_misc.x_lnsz.x_lnno;
2360 *line_ptr = line_base;
2361 }
2362 }
2363 }
2364 else
2365 {
2366 if (l->u.offset > offset)
2367 break;
2368 *line_ptr = l->line_number + line_base - 1;
2369 }
2370 l++;
2371 }
2372
2373 /* If we fell off the end of the loop, then assume that this
2374 symbol has no line number info. Otherwise, symbols with no
2375 line number info get reported with the line number of the
2376 last line of the last symbol which does have line number
2377 info. We use 0x100 as a slop to account for cases where the
2378 last line has executable code. */
2379 if (i >= section->lineno_count
2380 && last_value != 0
2381 && offset - last_value > 0x100)
2382 {
2383 *functionname_ptr = NULL;
2384 *line_ptr = 0;
2385 }
2386 }
2387
2388 /* Cache the results for the next call. */
2389 if (sec_data == NULL && section->owner == abfd)
2390 {
2391 amt = sizeof (struct coff_section_tdata);
2392 section->used_by_bfd = bfd_zalloc (abfd, amt);
2393 sec_data = (struct coff_section_tdata *) section->used_by_bfd;
2394 }
2395 if (sec_data != NULL)
2396 {
2397 sec_data->offset = offset;
2398 sec_data->i = i - 1;
2399 sec_data->function = *functionname_ptr;
2400 sec_data->line_base = line_base;
2401 }
2402
2403 return TRUE;
2404}
2405
a45ae5f8
JM
2406bfd_boolean
2407coff_find_nearest_line (bfd *abfd,
2408 asection *section,
2409 asymbol **symbols,
2410 bfd_vma offset,
2411 const char **filename_ptr,
2412 const char **functionname_ptr,
2413 unsigned int *line_ptr)
2414{
2415 return coff_find_nearest_line_with_names (abfd, dwarf_debug_sections,
2416 section, symbols, offset,
2417 filename_ptr, functionname_ptr,
2418 line_ptr);
2419}
2420
ef5ccd6c
JM
2421bfd_boolean
2422coff_find_nearest_line_discriminator (bfd *abfd,
2423 asection *section,
2424 asymbol **symbols,
2425 bfd_vma offset,
2426 const char **filename_ptr,
2427 const char **functionname_ptr,
2428 unsigned int *line_ptr,
2429 unsigned int *discriminator)
2430{
2431 *discriminator = 0;
2432 return coff_find_nearest_line_with_names (abfd, dwarf_debug_sections,
2433 section, symbols, offset,
2434 filename_ptr, functionname_ptr,
2435 line_ptr);
2436}
2437
2438
5796c8dc
SS
2439bfd_boolean
2440coff_find_inliner_info (bfd *abfd,
2441 const char **filename_ptr,
2442 const char **functionname_ptr,
2443 unsigned int *line_ptr)
2444{
2445 bfd_boolean found;
2446
2447 found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
2448 functionname_ptr, line_ptr,
2449 &coff_data(abfd)->dwarf2_find_line_info);
2450 return (found);
2451}
2452
2453int
2454coff_sizeof_headers (bfd *abfd, struct bfd_link_info *info)
2455{
2456 size_t size;
2457
2458 if (!info->relocatable)
2459 size = bfd_coff_filhsz (abfd) + bfd_coff_aoutsz (abfd);
2460 else
2461 size = bfd_coff_filhsz (abfd);
2462
2463 size += abfd->section_count * bfd_coff_scnhsz (abfd);
2464 return size;
2465}
2466
2467/* Change the class of a coff symbol held by BFD. */
2468
2469bfd_boolean
2470bfd_coff_set_symbol_class (bfd * abfd,
2471 asymbol * symbol,
2472 unsigned int symbol_class)
2473{
2474 coff_symbol_type * csym;
2475
2476 csym = coff_symbol_from (abfd, symbol);
2477 if (csym == NULL)
2478 {
2479 bfd_set_error (bfd_error_invalid_operation);
2480 return FALSE;
2481 }
2482 else if (csym->native == NULL)
2483 {
2484 /* This is an alien symbol which no native coff backend data.
2485 We cheat here by creating a fake native entry for it and
2486 then filling in the class. This code is based on that in
2487 coff_write_alien_symbol(). */
2488
2489 combined_entry_type * native;
2490 bfd_size_type amt = sizeof (* native);
2491
2492 native = (combined_entry_type *) bfd_zalloc (abfd, amt);
2493 if (native == NULL)
2494 return FALSE;
2495
2496 native->u.syment.n_type = T_NULL;
2497 native->u.syment.n_sclass = symbol_class;
2498
2499 if (bfd_is_und_section (symbol->section))
2500 {
2501 native->u.syment.n_scnum = N_UNDEF;
2502 native->u.syment.n_value = symbol->value;
2503 }
2504 else if (bfd_is_com_section (symbol->section))
2505 {
2506 native->u.syment.n_scnum = N_UNDEF;
2507 native->u.syment.n_value = symbol->value;
2508 }
2509 else
2510 {
2511 native->u.syment.n_scnum =
2512 symbol->section->output_section->target_index;
2513 native->u.syment.n_value = (symbol->value
2514 + symbol->section->output_offset);
2515 if (! obj_pe (abfd))
2516 native->u.syment.n_value += symbol->section->output_section->vma;
2517
2518 /* Copy the any flags from the file header into the symbol.
2519 FIXME: Why? */
2520 native->u.syment.n_flags = bfd_asymbol_bfd (& csym->symbol)->flags;
2521 }
2522
2523 csym->native = native;
2524 }
2525 else
2526 csym->native->u.syment.n_sclass = symbol_class;
2527
2528 return TRUE;
2529}
2530
2531struct coff_comdat_info *
2532bfd_coff_get_comdat_section (bfd *abfd, struct bfd_section *sec)
2533{
2534 if (bfd_get_flavour (abfd) == bfd_target_coff_flavour
2535 && coff_section_data (abfd, sec) != NULL)
2536 return coff_section_data (abfd, sec)->comdat;
2537 else
2538 return NULL;
2539}
a45ae5f8
JM
2540
2541bfd_boolean
2542_bfd_coff_section_already_linked (bfd *abfd,
2543 asection *sec,
2544 struct bfd_link_info *info)
2545{
2546 flagword flags;
2547 const char *name, *key;
2548 struct bfd_section_already_linked *l;
2549 struct bfd_section_already_linked_hash_entry *already_linked_list;
2550 struct coff_comdat_info *s_comdat;
2551
2552 flags = sec->flags;
2553 if ((flags & SEC_LINK_ONCE) == 0)
2554 return FALSE;
2555
2556 /* The COFF backend linker doesn't support group sections. */
2557 if ((flags & SEC_GROUP) != 0)
2558 return FALSE;
2559
2560 name = bfd_get_section_name (abfd, sec);
2561 s_comdat = bfd_coff_get_comdat_section (abfd, sec);
2562
2563 if (s_comdat != NULL)
2564 key = s_comdat->name;
2565 else
2566 {
2567 if (CONST_STRNEQ (name, ".gnu.linkonce.")
2568 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
2569 key++;
2570 else
2571 /* FIXME: gcc as of 2011-09 emits sections like .text$<key>,
2572 .xdata$<key> and .pdata$<key> only the first of which has a
2573 comdat key. Should these all match the LTO IR key? */
2574 key = name;
2575 }
2576
2577 already_linked_list = bfd_section_already_linked_table_lookup (key);
2578
2579 for (l = already_linked_list->entry; l != NULL; l = l->next)
2580 {
2581 struct coff_comdat_info *l_comdat;
2582
2583 l_comdat = bfd_coff_get_comdat_section (l->sec->owner, l->sec);
2584
2585 /* The section names must match, and both sections must be
2586 comdat and have the same comdat name, or both sections must
2587 be non-comdat. LTO IR plugin sections are an exception. They
2588 are always named .gnu.linkonce.t.<key> (<key> is some string)
2589 and match any comdat section with comdat name of <key>, and
2590 any linkonce section with the same suffix, ie.
2591 .gnu.linkonce.*.<key>. */
2592 if (((s_comdat != NULL) == (l_comdat != NULL)
2593 && strcmp (name, l->sec->name) == 0)
2594 || (l->sec->owner->flags & BFD_PLUGIN) != 0)
2595 {
2596 /* The section has already been linked. See if we should
2597 issue a warning. */
2598 return _bfd_handle_already_linked (sec, l, info);
2599 }
2600 }
2601
2602 /* This is the first section with this name. Record it. */
2603 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
2604 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
2605 return FALSE;
2606}