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