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