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