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