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