* coffcode.h (bfd_coff_backend_data): Add _bfd_filnmlen field.
[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 }
588 else if (coff_symbol_ptr->symbol.flags & BSF_DEBUGGING)
589 {
590 syment->n_value = coff_symbol_ptr->symbol.value;
591 }
592 else if (bfd_is_und_section (coff_symbol_ptr->symbol.section))
593 {
594 syment->n_scnum = N_UNDEF;
595 syment->n_value = 0;
596 }
7bb9db4d 597 /* FIXME: Do we need to handle the absolute section here? */
252b5132
RH
598 else
599 {
600 if (coff_symbol_ptr->symbol.section)
601 {
602 syment->n_scnum =
603 coff_symbol_ptr->symbol.section->output_section->target_index;
604
605 syment->n_value = (coff_symbol_ptr->symbol.value
606 + coff_symbol_ptr->symbol.section->output_offset);
607 if (! obj_pe (abfd))
608 syment->n_value +=
609 coff_symbol_ptr->symbol.section->output_section->vma;
610 }
611 else
612 {
613 BFD_ASSERT (0);
614 /* This can happen, but I don't know why yet (steve@cygnus.com) */
615 syment->n_scnum = N_ABS;
616 syment->n_value = coff_symbol_ptr->symbol.value;
617 }
618 }
619}
620
621/* Run through all the symbols in the symbol table and work out what
622 their indexes into the symbol table will be when output.
623
624 Coff requires that each C_FILE symbol points to the next one in the
625 chain, and that the last one points to the first external symbol. We
626 do that here too. */
627
628boolean
629coff_renumber_symbols (bfd_ptr, first_undef)
630 bfd *bfd_ptr;
631 int *first_undef;
632{
633 unsigned int symbol_count = bfd_get_symcount (bfd_ptr);
634 asymbol **symbol_ptr_ptr = bfd_ptr->outsymbols;
635 unsigned int native_index = 0;
636 struct internal_syment *last_file = (struct internal_syment *) NULL;
637 unsigned int symbol_index;
638
639 /* COFF demands that undefined symbols come after all other symbols.
640 Since we don't need to impose this extra knowledge on all our
641 client programs, deal with that here. Sort the symbol table;
642 just move the undefined symbols to the end, leaving the rest
643 alone. The O'Reilly book says that defined global symbols come
644 at the end before the undefined symbols, so we do that here as
645 well. */
646 /* @@ Do we have some condition we could test for, so we don't always
647 have to do this? I don't think relocatability is quite right, but
648 I'm not certain. [raeburn:19920508.1711EST] */
649 {
650 asymbol **newsyms;
651 unsigned int i;
652
653 newsyms = (asymbol **) bfd_alloc (bfd_ptr,
654 sizeof (asymbol *) * (symbol_count + 1));
655 if (!newsyms)
656 return false;
657 bfd_ptr->outsymbols = newsyms;
658 for (i = 0; i < symbol_count; i++)
659 if ((symbol_ptr_ptr[i]->flags & BSF_NOT_AT_END) != 0
660 || (!bfd_is_und_section (symbol_ptr_ptr[i]->section)
661 && !bfd_is_com_section (symbol_ptr_ptr[i]->section)
662 && ((symbol_ptr_ptr[i]->flags & BSF_FUNCTION) != 0
663 || ((symbol_ptr_ptr[i]->flags & (BSF_GLOBAL | BSF_WEAK))
664 == 0))))
665 *newsyms++ = symbol_ptr_ptr[i];
666
667 for (i = 0; i < symbol_count; i++)
668 if ((symbol_ptr_ptr[i]->flags & BSF_NOT_AT_END) == 0
669 && !bfd_is_und_section (symbol_ptr_ptr[i]->section)
670 && (bfd_is_com_section (symbol_ptr_ptr[i]->section)
671 || ((symbol_ptr_ptr[i]->flags & BSF_FUNCTION) == 0
672 && ((symbol_ptr_ptr[i]->flags & (BSF_GLOBAL | BSF_WEAK))
673 != 0))))
674 *newsyms++ = symbol_ptr_ptr[i];
675
676 *first_undef = newsyms - bfd_ptr->outsymbols;
677
678 for (i = 0; i < symbol_count; i++)
679 if ((symbol_ptr_ptr[i]->flags & BSF_NOT_AT_END) == 0
680 && bfd_is_und_section (symbol_ptr_ptr[i]->section))
681 *newsyms++ = symbol_ptr_ptr[i];
682 *newsyms = (asymbol *) NULL;
683 symbol_ptr_ptr = bfd_ptr->outsymbols;
684 }
685
686 for (symbol_index = 0; symbol_index < symbol_count; symbol_index++)
687 {
688 coff_symbol_type *coff_symbol_ptr = coff_symbol_from (bfd_ptr, symbol_ptr_ptr[symbol_index]);
689 symbol_ptr_ptr[symbol_index]->udata.i = symbol_index;
690 if (coff_symbol_ptr && coff_symbol_ptr->native)
691 {
692 combined_entry_type *s = coff_symbol_ptr->native;
693 int i;
694
695 if (s->u.syment.n_sclass == C_FILE)
696 {
697 if (last_file != (struct internal_syment *) NULL)
698 last_file->n_value = native_index;
699 last_file = &(s->u.syment);
700 }
701 else
702 {
703
704 /* Modify the symbol values according to their section and
705 type */
706
707 fixup_symbol_value (bfd_ptr, coff_symbol_ptr, &(s->u.syment));
708 }
709 for (i = 0; i < s->u.syment.n_numaux + 1; i++)
710 s[i].offset = native_index++;
711 }
712 else
713 {
714 native_index++;
715 }
716 }
717 obj_conv_table_size (bfd_ptr) = native_index;
718
719 return true;
720}
721
722/* Run thorough the symbol table again, and fix it so that all
723 pointers to entries are changed to the entries' index in the output
724 symbol table. */
725
726void
727coff_mangle_symbols (bfd_ptr)
728 bfd *bfd_ptr;
729{
730 unsigned int symbol_count = bfd_get_symcount (bfd_ptr);
731 asymbol **symbol_ptr_ptr = bfd_ptr->outsymbols;
732 unsigned int symbol_index;
733
734 for (symbol_index = 0; symbol_index < symbol_count; symbol_index++)
735 {
736 coff_symbol_type *coff_symbol_ptr =
737 coff_symbol_from (bfd_ptr, symbol_ptr_ptr[symbol_index]);
738
739 if (coff_symbol_ptr && coff_symbol_ptr->native)
740 {
741 int i;
742 combined_entry_type *s = coff_symbol_ptr->native;
743
744 if (s->fix_value)
745 {
746 /* FIXME: We should use a union here. */
747 s->u.syment.n_value =
748 ((combined_entry_type *) s->u.syment.n_value)->offset;
749 s->fix_value = 0;
750 }
751 if (s->fix_line)
752 {
753 /* The value is the offset into the line number entries
754 for the symbol's section. On output, the symbol's
755 section should be N_DEBUG. */
756 s->u.syment.n_value =
757 (coff_symbol_ptr->symbol.section->output_section->line_filepos
758 + s->u.syment.n_value * bfd_coff_linesz (bfd_ptr));
759 coff_symbol_ptr->symbol.section =
760 coff_section_from_bfd_index (bfd_ptr, N_DEBUG);
761 BFD_ASSERT (coff_symbol_ptr->symbol.flags & BSF_DEBUGGING);
762 }
763 for (i = 0; i < s->u.syment.n_numaux; i++)
764 {
765 combined_entry_type *a = s + i + 1;
766 if (a->fix_tag)
767 {
768 a->u.auxent.x_sym.x_tagndx.l =
769 a->u.auxent.x_sym.x_tagndx.p->offset;
770 a->fix_tag = 0;
771 }
772 if (a->fix_end)
773 {
774 a->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l =
775 a->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p->offset;
776 a->fix_end = 0;
777 }
778 if (a->fix_scnlen)
779 {
780 a->u.auxent.x_csect.x_scnlen.l =
781 a->u.auxent.x_csect.x_scnlen.p->offset;
782 a->fix_scnlen = 0;
783 }
784 }
785 }
786 }
787}
788
789static void
790coff_fix_symbol_name (abfd, symbol, native, string_size_p,
791 debug_string_section_p, debug_string_size_p)
792 bfd *abfd;
793 asymbol *symbol;
794 combined_entry_type *native;
795 bfd_size_type *string_size_p;
796 asection **debug_string_section_p;
797 bfd_size_type *debug_string_size_p;
798{
799 unsigned int name_length;
800 union internal_auxent *auxent;
801 char *name = (char *) (symbol->name);
802
803 if (name == (char *) NULL)
804 {
805 /* coff symbols always have names, so we'll make one up */
806 symbol->name = "strange";
807 name = (char *) symbol->name;
808 }
809 name_length = strlen (name);
810
811 if (native->u.syment.n_sclass == C_FILE
812 && native->u.syment.n_numaux > 0)
813 {
692b7d62
ILT
814 unsigned int filnmlen;
815
252b5132
RH
816 strncpy (native->u.syment._n._n_name, ".file", SYMNMLEN);
817 auxent = &(native + 1)->u.auxent;
818
692b7d62
ILT
819 filnmlen = bfd_coff_filnmlen (abfd);
820
252b5132
RH
821 if (bfd_coff_long_filenames (abfd))
822 {
692b7d62 823 if (name_length <= filnmlen)
252b5132 824 {
692b7d62 825 strncpy (auxent->x_file.x_fname, name, filnmlen);
252b5132
RH
826 }
827 else
828 {
829 auxent->x_file.x_n.x_offset = *string_size_p + STRING_SIZE_SIZE;
830 auxent->x_file.x_n.x_zeroes = 0;
831 *string_size_p += name_length + 1;
832 }
833 }
834 else
835 {
692b7d62
ILT
836 strncpy (auxent->x_file.x_fname, name, filnmlen);
837 if (name_length > filnmlen)
838 name[filnmlen] = '\0';
252b5132
RH
839 }
840 }
841 else
842 {
843 if (name_length <= SYMNMLEN)
844 {
845 /* This name will fit into the symbol neatly */
846 strncpy (native->u.syment._n._n_name, symbol->name, SYMNMLEN);
847 }
848 else if (!bfd_coff_symname_in_debug (abfd, &native->u.syment))
849 {
850 native->u.syment._n._n_n._n_offset = (*string_size_p
851 + STRING_SIZE_SIZE);
852 native->u.syment._n._n_n._n_zeroes = 0;
853 *string_size_p += name_length + 1;
854 }
855 else
856 {
857 long filepos;
858 bfd_byte buf[2];
859
860 /* This name should be written into the .debug section. For
861 some reason each name is preceded by a two byte length
862 and also followed by a null byte. FIXME: We assume that
863 the .debug section has already been created, and that it
864 is large enough. */
865 if (*debug_string_section_p == (asection *) NULL)
866 *debug_string_section_p = bfd_get_section_by_name (abfd, ".debug");
867 filepos = bfd_tell (abfd);
868 bfd_put_16 (abfd, name_length + 1, buf);
869 if (!bfd_set_section_contents (abfd,
870 *debug_string_section_p,
871 (PTR) buf,
872 (file_ptr) *debug_string_size_p,
873 (bfd_size_type) 2)
874 || !bfd_set_section_contents (abfd,
875 *debug_string_section_p,
876 (PTR) symbol->name,
877 ((file_ptr) *debug_string_size_p
878 + 2),
879 (bfd_size_type) name_length + 1))
880 abort ();
881 if (bfd_seek (abfd, filepos, SEEK_SET) != 0)
882 abort ();
883 native->u.syment._n._n_n._n_offset = *debug_string_size_p + 2;
884 native->u.syment._n._n_n._n_zeroes = 0;
885 *debug_string_size_p += name_length + 3;
886 }
887 }
888}
889
890/* We need to keep track of the symbol index so that when we write out
891 the relocs we can get the index for a symbol. This method is a
892 hack. FIXME. */
893
894#define set_index(symbol, idx) ((symbol)->udata.i = (idx))
895
896/* Write a symbol out to a COFF file. */
897
898static boolean
899coff_write_symbol (abfd, symbol, native, written, string_size_p,
900 debug_string_section_p, debug_string_size_p)
901 bfd *abfd;
902 asymbol *symbol;
903 combined_entry_type *native;
904 unsigned int *written;
905 bfd_size_type *string_size_p;
906 asection **debug_string_section_p;
907 bfd_size_type *debug_string_size_p;
908{
909 unsigned int numaux = native->u.syment.n_numaux;
910 int type = native->u.syment.n_type;
911 int class = native->u.syment.n_sclass;
912 PTR buf;
913 bfd_size_type symesz;
914
915 if (native->u.syment.n_sclass == C_FILE)
916 symbol->flags |= BSF_DEBUGGING;
917
918 if (symbol->flags & BSF_DEBUGGING
919 && bfd_is_abs_section (symbol->section))
920 {
921 native->u.syment.n_scnum = N_DEBUG;
922 }
923 else if (bfd_is_abs_section (symbol->section))
924 {
925 native->u.syment.n_scnum = N_ABS;
926 }
927 else if (bfd_is_und_section (symbol->section))
928 {
929 native->u.syment.n_scnum = N_UNDEF;
930 }
931 else
932 {
933 native->u.syment.n_scnum =
934 symbol->section->output_section->target_index;
935 }
936
937 coff_fix_symbol_name (abfd, symbol, native, string_size_p,
938 debug_string_section_p, debug_string_size_p);
939
940 symesz = bfd_coff_symesz (abfd);
941 buf = bfd_alloc (abfd, symesz);
942 if (!buf)
943 return false;
944 bfd_coff_swap_sym_out (abfd, &native->u.syment, buf);
945 if (bfd_write (buf, 1, symesz, abfd) != symesz)
946 return false;
947 bfd_release (abfd, buf);
948
949 if (native->u.syment.n_numaux > 0)
950 {
951 bfd_size_type auxesz;
952 unsigned int j;
953
954 auxesz = bfd_coff_auxesz (abfd);
955 buf = bfd_alloc (abfd, auxesz);
956 if (!buf)
957 return false;
958 for (j = 0; j < native->u.syment.n_numaux; j++)
959 {
960 bfd_coff_swap_aux_out (abfd,
961 &((native + j + 1)->u.auxent),
962 type,
963 class,
964 j,
965 native->u.syment.n_numaux,
966 buf);
967 if (bfd_write (buf, 1, auxesz, abfd) != auxesz)
968 return false;
969 }
970 bfd_release (abfd, buf);
971 }
972
973 /* Store the index for use when we write out the relocs. */
974 set_index (symbol, *written);
975
976 *written += numaux + 1;
977 return true;
978}
979
980/* Write out a symbol to a COFF file that does not come from a COFF
981 file originally. This symbol may have been created by the linker,
982 or we may be linking a non COFF file to a COFF file. */
983
984static boolean
985coff_write_alien_symbol (abfd, symbol, written, string_size_p,
986 debug_string_section_p, debug_string_size_p)
987 bfd *abfd;
988 asymbol *symbol;
989 unsigned int *written;
990 bfd_size_type *string_size_p;
991 asection **debug_string_section_p;
992 bfd_size_type *debug_string_size_p;
993{
994 combined_entry_type *native;
995 combined_entry_type dummy;
996
997 native = &dummy;
998 native->u.syment.n_type = T_NULL;
999 native->u.syment.n_flags = 0;
1000 if (bfd_is_und_section (symbol->section))
1001 {
1002 native->u.syment.n_scnum = N_UNDEF;
1003 native->u.syment.n_value = symbol->value;
1004 }
1005 else if (bfd_is_com_section (symbol->section))
1006 {
1007 native->u.syment.n_scnum = N_UNDEF;
1008 native->u.syment.n_value = symbol->value;
1009 }
1010 else if (symbol->flags & BSF_DEBUGGING)
1011 {
1012 /* There isn't much point to writing out a debugging symbol
1013 unless we are prepared to convert it into COFF debugging
1014 format. So, we just ignore them. We must clobber the symbol
1015 name to keep it from being put in the string table. */
1016 symbol->name = "";
1017 return true;
1018 }
1019 else
1020 {
1021 native->u.syment.n_scnum =
1022 symbol->section->output_section->target_index;
1023 native->u.syment.n_value = (symbol->value
1024 + symbol->section->output_offset);
1025 if (! obj_pe (abfd))
1026 native->u.syment.n_value += symbol->section->output_section->vma;
1027
1028 /* Copy the any flags from the the file header into the symbol.
1029 FIXME: Why? */
1030 {
1031 coff_symbol_type *c = coff_symbol_from (abfd, symbol);
1032 if (c != (coff_symbol_type *) NULL)
1033 native->u.syment.n_flags = bfd_asymbol_bfd (&c->symbol)->flags;
1034 }
1035 }
1036
1037 native->u.syment.n_type = 0;
1038 if (symbol->flags & BSF_LOCAL)
1039 native->u.syment.n_sclass = C_STAT;
1040 else if (symbol->flags & BSF_WEAK)
1041 native->u.syment.n_sclass = obj_pe (abfd) ? C_NT_WEAK : C_WEAKEXT;
1042 else
1043 native->u.syment.n_sclass = C_EXT;
1044 native->u.syment.n_numaux = 0;
1045
1046 return coff_write_symbol (abfd, symbol, native, written, string_size_p,
1047 debug_string_section_p, debug_string_size_p);
1048}
1049
1050/* Write a native symbol to a COFF file. */
1051
1052static boolean
1053coff_write_native_symbol (abfd, symbol, written, string_size_p,
1054 debug_string_section_p, debug_string_size_p)
1055 bfd *abfd;
1056 coff_symbol_type *symbol;
1057 unsigned int *written;
1058 bfd_size_type *string_size_p;
1059 asection **debug_string_section_p;
1060 bfd_size_type *debug_string_size_p;
1061{
1062 combined_entry_type *native = symbol->native;
1063 alent *lineno = symbol->lineno;
1064
1065 /* If this symbol has an associated line number, we must store the
1066 symbol index in the line number field. We also tag the auxent to
1067 point to the right place in the lineno table. */
1068 if (lineno && !symbol->done_lineno && symbol->symbol.section->owner != NULL)
1069 {
1070 unsigned int count = 0;
1071 lineno[count].u.offset = *written;
1072 if (native->u.syment.n_numaux)
1073 {
1074 union internal_auxent *a = &((native + 1)->u.auxent);
1075
1076 a->x_sym.x_fcnary.x_fcn.x_lnnoptr =
1077 symbol->symbol.section->output_section->moving_line_filepos;
1078 }
1079
1080 /* Count and relocate all other linenumbers. */
1081 count++;
1082 while (lineno[count].line_number != 0)
1083 {
1084#if 0
1085 /* 13 april 92. sac
1086 I've been told this, but still need proof:
1087 > The second bug is also in `bfd/coffcode.h'. This bug
1088 > causes the linker to screw up the pc-relocations for
1089 > all the line numbers in COFF code. This bug isn't only
1090 > specific to A29K implementations, but affects all
1091 > systems using COFF format binaries. Note that in COFF
1092 > object files, the line number core offsets output by
1093 > the assembler are relative to the start of each
1094 > procedure, not to the start of the .text section. This
1095 > patch relocates the line numbers relative to the
1096 > `native->u.syment.n_value' instead of the section
1097 > virtual address.
1098 > modular!olson@cs.arizona.edu (Jon Olson)
1099 */
1100 lineno[count].u.offset += native->u.syment.n_value;
1101#else
1102 lineno[count].u.offset +=
1103 (symbol->symbol.section->output_section->vma
1104 + symbol->symbol.section->output_offset);
1105#endif
1106 count++;
1107 }
1108 symbol->done_lineno = true;
1109
1110 symbol->symbol.section->output_section->moving_line_filepos +=
1111 count * bfd_coff_linesz (abfd);
1112 }
1113
1114 return coff_write_symbol (abfd, &(symbol->symbol), native, written,
1115 string_size_p, debug_string_section_p,
1116 debug_string_size_p);
1117}
1118
1119/* Write out the COFF symbols. */
1120
1121boolean
1122coff_write_symbols (abfd)
1123 bfd *abfd;
1124{
1125 bfd_size_type string_size;
1126 asection *debug_string_section;
1127 bfd_size_type debug_string_size;
1128 unsigned int i;
1129 unsigned int limit = bfd_get_symcount (abfd);
1130 unsigned int written = 0;
1131 asymbol **p;
1132
1133 string_size = 0;
1134 debug_string_section = NULL;
1135 debug_string_size = 0;
1136
1137 /* If this target supports long section names, they must be put into
1138 the string table. This is supported by PE. This code must
1139 handle section names just as they are handled in
1140 coff_write_object_contents. */
1141 if (bfd_coff_long_section_names (abfd))
1142 {
1143 asection *o;
1144
1145 for (o = abfd->sections; o != NULL; o = o->next)
1146 {
1147 size_t len;
1148
1149 len = strlen (o->name);
1150 if (len > SCNNMLEN)
1151 string_size += len + 1;
1152 }
1153 }
1154
1155 /* Seek to the right place */
1156 if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0)
1157 return false;
1158
1159 /* Output all the symbols we have */
1160
1161 written = 0;
1162 for (p = abfd->outsymbols, i = 0; i < limit; i++, p++)
1163 {
1164 asymbol *symbol = *p;
1165 coff_symbol_type *c_symbol = coff_symbol_from (abfd, symbol);
1166
1167 if (c_symbol == (coff_symbol_type *) NULL
1168 || c_symbol->native == (combined_entry_type *) NULL)
1169 {
1170 if (!coff_write_alien_symbol (abfd, symbol, &written, &string_size,
1171 &debug_string_section,
1172 &debug_string_size))
1173 return false;
1174 }
1175 else
1176 {
1177 if (!coff_write_native_symbol (abfd, c_symbol, &written,
1178 &string_size, &debug_string_section,
1179 &debug_string_size))
1180 return false;
1181 }
1182 }
1183
1184 obj_raw_syment_count (abfd) = written;
1185
1186 /* Now write out strings */
1187
1188 if (string_size != 0)
1189 {
1190 unsigned int size = string_size + STRING_SIZE_SIZE;
1191 bfd_byte buffer[STRING_SIZE_SIZE];
1192
1193#if STRING_SIZE_SIZE == 4
1194 bfd_h_put_32 (abfd, size, buffer);
1195#else
1196 #error Change bfd_h_put_32
1197#endif
1198 if (bfd_write ((PTR) buffer, 1, sizeof (buffer), abfd) != sizeof (buffer))
1199 return false;
1200
1201 /* Handle long section names. This code must handle section
1202 names just as they are handled in coff_write_object_contents. */
1203 if (bfd_coff_long_section_names (abfd))
1204 {
1205 asection *o;
1206
1207 for (o = abfd->sections; o != NULL; o = o->next)
1208 {
1209 size_t len;
1210
1211 len = strlen (o->name);
1212 if (len > SCNNMLEN)
1213 {
1214 if (bfd_write (o->name, 1, len + 1, abfd) != len + 1)
1215 return false;
1216 }
1217 }
1218 }
1219
1220 for (p = abfd->outsymbols, i = 0;
1221 i < limit;
1222 i++, p++)
1223 {
1224 asymbol *q = *p;
1225 size_t name_length = strlen (q->name);
1226 coff_symbol_type *c_symbol = coff_symbol_from (abfd, q);
1227 size_t maxlen;
1228
1229 /* Figure out whether the symbol name should go in the string
1230 table. Symbol names that are short enough are stored
1231 directly in the syment structure. File names permit a
1232 different, longer, length in the syment structure. On
1233 XCOFF, some symbol names are stored in the .debug section
1234 rather than in the string table. */
1235
1236 if (c_symbol == NULL
1237 || c_symbol->native == NULL)
1238 {
1239 /* This is not a COFF symbol, so it certainly is not a
1240 file name, nor does it go in the .debug section. */
1241 maxlen = SYMNMLEN;
1242 }
1243 else if (bfd_coff_symname_in_debug (abfd,
1244 &c_symbol->native->u.syment))
1245 {
1246 /* This symbol name is in the XCOFF .debug section.
1247 Don't write it into the string table. */
1248 maxlen = name_length;
1249 }
1250 else if (c_symbol->native->u.syment.n_sclass == C_FILE
1251 && c_symbol->native->u.syment.n_numaux > 0)
692b7d62 1252 maxlen = bfd_coff_filnmlen (abfd);
252b5132
RH
1253 else
1254 maxlen = SYMNMLEN;
1255
1256 if (name_length > maxlen)
1257 {
1258 if (bfd_write ((PTR) (q->name), 1, name_length + 1, abfd)
1259 != name_length + 1)
1260 return false;
1261 }
1262 }
1263 }
1264 else
1265 {
1266 /* We would normally not write anything here, but we'll write
1267 out 4 so that any stupid coff reader which tries to read the
1268 string table even when there isn't one won't croak. */
1269 unsigned int size = STRING_SIZE_SIZE;
1270 bfd_byte buffer[STRING_SIZE_SIZE];
1271
1272#if STRING_SIZE_SIZE == 4
1273 bfd_h_put_32 (abfd, size, buffer);
1274#else
1275 #error Change bfd_h_put_32
1276#endif
1277 if (bfd_write ((PTR) buffer, 1, STRING_SIZE_SIZE, abfd)
1278 != STRING_SIZE_SIZE)
1279 return false;
1280 }
1281
1282 /* Make sure the .debug section was created to be the correct size.
1283 We should create it ourselves on the fly, but we don't because
1284 BFD won't let us write to any section until we know how large all
1285 the sections are. We could still do it by making another pass
1286 over the symbols. FIXME. */
1287 BFD_ASSERT (debug_string_size == 0
1288 || (debug_string_section != (asection *) NULL
1289 && (BFD_ALIGN (debug_string_size,
1290 1 << debug_string_section->alignment_power)
1291 == bfd_section_size (abfd, debug_string_section))));
1292
1293 return true;
1294}
1295
1296boolean
1297coff_write_linenumbers (abfd)
1298 bfd *abfd;
1299{
1300 asection *s;
1301 bfd_size_type linesz;
1302 PTR buff;
1303
1304 linesz = bfd_coff_linesz (abfd);
1305 buff = bfd_alloc (abfd, linesz);
1306 if (!buff)
1307 return false;
1308 for (s = abfd->sections; s != (asection *) NULL; s = s->next)
1309 {
1310 if (s->lineno_count)
1311 {
1312 asymbol **q = abfd->outsymbols;
1313 if (bfd_seek (abfd, s->line_filepos, SEEK_SET) != 0)
1314 return false;
1315 /* Find all the linenumbers in this section */
1316 while (*q)
1317 {
1318 asymbol *p = *q;
1319 if (p->section->output_section == s)
1320 {
1321 alent *l =
1322 BFD_SEND (bfd_asymbol_bfd (p), _get_lineno,
1323 (bfd_asymbol_bfd (p), p));
1324 if (l)
1325 {
1326 /* Found a linenumber entry, output */
1327 struct internal_lineno out;
1328 memset ((PTR) & out, 0, sizeof (out));
1329 out.l_lnno = 0;
1330 out.l_addr.l_symndx = l->u.offset;
1331 bfd_coff_swap_lineno_out (abfd, &out, buff);
1332 if (bfd_write (buff, 1, linesz, abfd) != linesz)
1333 return false;
1334 l++;
1335 while (l->line_number)
1336 {
1337 out.l_lnno = l->line_number;
1338 out.l_addr.l_symndx = l->u.offset;
1339 bfd_coff_swap_lineno_out (abfd, &out, buff);
1340 if (bfd_write (buff, 1, linesz, abfd) != linesz)
1341 return false;
1342 l++;
1343 }
1344 }
1345 }
1346 q++;
1347 }
1348 }
1349 }
1350 bfd_release (abfd, buff);
1351 return true;
1352}
1353
1354/*ARGSUSED */
1355alent *
1356coff_get_lineno (ignore_abfd, symbol)
7442e600 1357 bfd *ignore_abfd ATTRIBUTE_UNUSED;
252b5132
RH
1358 asymbol *symbol;
1359{
1360 return coffsymbol (symbol)->lineno;
1361}
1362
1363#if 0
1364
1365/* This is only called from coff_add_missing_symbols, which has been
1366 disabled. */
1367
1368asymbol *
1369coff_section_symbol (abfd, name)
1370 bfd *abfd;
1371 char *name;
1372{
1373 asection *sec = bfd_make_section_old_way (abfd, name);
1374 asymbol *sym;
1375 combined_entry_type *csym;
1376
1377 sym = sec->symbol;
1378 csym = coff_symbol_from (abfd, sym)->native;
1379 /* Make sure back-end COFF stuff is there. */
1380 if (csym == 0)
1381 {
1382 struct foo
1383 {
1384 coff_symbol_type sym;
1385 /* @@FIXME This shouldn't use a fixed size!! */
1386 combined_entry_type e[10];
1387 };
1388 struct foo *f;
1389 f = (struct foo *) bfd_alloc (abfd, sizeof (*f));
1390 if (!f)
1391 {
1392 bfd_set_error (bfd_error_no_error);
1393 return NULL;
1394 }
1395 memset ((char *) f, 0, sizeof (*f));
1396 coff_symbol_from (abfd, sym)->native = csym = f->e;
1397 }
1398 csym[0].u.syment.n_sclass = C_STAT;
1399 csym[0].u.syment.n_numaux = 1;
1400/* SF_SET_STATICS (sym); @@ ??? */
1401 csym[1].u.auxent.x_scn.x_scnlen = sec->_raw_size;
1402 csym[1].u.auxent.x_scn.x_nreloc = sec->reloc_count;
1403 csym[1].u.auxent.x_scn.x_nlinno = sec->lineno_count;
1404
1405 if (sec->output_section == NULL)
1406 {
1407 sec->output_section = sec;
1408 sec->output_offset = 0;
1409 }
1410
1411 return sym;
1412}
1413
1414#endif /* 0 */
1415
1416/* This function transforms the offsets into the symbol table into
1417 pointers to syments. */
1418
1419static void
1420coff_pointerize_aux (abfd, table_base, symbol, indaux, auxent)
1421 bfd *abfd;
1422 combined_entry_type *table_base;
1423 combined_entry_type *symbol;
1424 unsigned int indaux;
1425 combined_entry_type *auxent;
1426{
1427 unsigned int type = symbol->u.syment.n_type;
1428 unsigned int class = symbol->u.syment.n_sclass;
1429
1430 if (coff_backend_info (abfd)->_bfd_coff_pointerize_aux_hook)
1431 {
1432 if ((*coff_backend_info (abfd)->_bfd_coff_pointerize_aux_hook)
1433 (abfd, table_base, symbol, indaux, auxent))
1434 return;
1435 }
1436
1437 /* Don't bother if this is a file or a section */
1438 if (class == C_STAT && type == T_NULL)
1439 return;
1440 if (class == C_FILE)
1441 return;
1442
1443 /* Otherwise patch up */
1444#define N_TMASK coff_data (abfd)->local_n_tmask
1445#define N_BTSHFT coff_data (abfd)->local_n_btshft
1446 if ((ISFCN (type) || ISTAG (class) || class == C_BLOCK || class == C_FCN)
1447 && auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l > 0)
1448 {
1449 auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p =
1450 table_base + auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l;
1451 auxent->fix_end = 1;
1452 }
1453 /* A negative tagndx is meaningless, but the SCO 3.2v4 cc can
1454 generate one, so we must be careful to ignore it. */
1455 if (auxent->u.auxent.x_sym.x_tagndx.l > 0)
1456 {
1457 auxent->u.auxent.x_sym.x_tagndx.p =
1458 table_base + auxent->u.auxent.x_sym.x_tagndx.l;
1459 auxent->fix_tag = 1;
1460 }
1461}
1462
1463/* Allocate space for the ".debug" section, and read it.
1464 We did not read the debug section until now, because
1465 we didn't want to go to the trouble until someone needed it. */
1466
1467static char *
1468build_debug_section (abfd)
1469 bfd *abfd;
1470{
1471 char *debug_section;
1472 long position;
1473
1474 asection *sect = bfd_get_section_by_name (abfd, ".debug");
1475
1476 if (!sect)
1477 {
1478 bfd_set_error (bfd_error_no_debug_section);
1479 return NULL;
1480 }
1481
1482 debug_section = (PTR) bfd_alloc (abfd,
1483 bfd_get_section_size_before_reloc (sect));
1484 if (debug_section == NULL)
1485 return NULL;
1486
1487 /* Seek to the beginning of the `.debug' section and read it.
1488 Save the current position first; it is needed by our caller.
1489 Then read debug section and reset the file pointer. */
1490
1491 position = bfd_tell (abfd);
1492 if (bfd_seek (abfd, sect->filepos, SEEK_SET) != 0
1493 || (bfd_read (debug_section,
1494 bfd_get_section_size_before_reloc (sect), 1, abfd)
1495 != bfd_get_section_size_before_reloc (sect))
1496 || bfd_seek (abfd, position, SEEK_SET) != 0)
1497 return NULL;
1498 return debug_section;
1499}
1500
1501
1502/* Return a pointer to a malloc'd copy of 'name'. 'name' may not be
1503 \0-terminated, but will not exceed 'maxlen' characters. The copy *will*
1504 be \0-terminated. */
1505static char *
1506copy_name (abfd, name, maxlen)
1507 bfd *abfd;
1508 char *name;
1509 int maxlen;
1510{
1511 int len;
1512 char *newname;
1513
1514 for (len = 0; len < maxlen; ++len)
1515 {
1516 if (name[len] == '\0')
1517 {
1518 break;
1519 }
1520 }
1521
1522 if ((newname = (PTR) bfd_alloc (abfd, len + 1)) == NULL)
1523 return (NULL);
1524 strncpy (newname, name, len);
1525 newname[len] = '\0';
1526 return newname;
1527}
1528
1529/* Read in the external symbols. */
1530
1531boolean
1532_bfd_coff_get_external_symbols (abfd)
1533 bfd *abfd;
1534{
1535 bfd_size_type symesz;
1536 size_t size;
1537 PTR syms;
1538
1539 if (obj_coff_external_syms (abfd) != NULL)
1540 return true;
1541
1542 symesz = bfd_coff_symesz (abfd);
1543
1544 size = obj_raw_syment_count (abfd) * symesz;
1545
1546 syms = (PTR) bfd_malloc (size);
1547 if (syms == NULL && size != 0)
1548 return false;
1549
1550 if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0
1551 || bfd_read (syms, size, 1, abfd) != size)
1552 {
1553 if (syms != NULL)
1554 free (syms);
1555 return false;
1556 }
1557
1558 obj_coff_external_syms (abfd) = syms;
1559
1560 return true;
1561}
1562
1563/* Read in the external strings. The strings are not loaded until
1564 they are needed. This is because we have no simple way of
1565 detecting a missing string table in an archive. */
1566
1567const char *
1568_bfd_coff_read_string_table (abfd)
1569 bfd *abfd;
1570{
1571 char extstrsize[STRING_SIZE_SIZE];
1572 size_t strsize;
1573 char *strings;
1574
1575 if (obj_coff_strings (abfd) != NULL)
1576 return obj_coff_strings (abfd);
1577
1578 if (obj_sym_filepos (abfd) == 0)
1579 {
1580 bfd_set_error (bfd_error_no_symbols);
1581 return NULL;
1582 }
1583
1584 if (bfd_seek (abfd,
1585 (obj_sym_filepos (abfd)
1586 + obj_raw_syment_count (abfd) * bfd_coff_symesz (abfd)),
1587 SEEK_SET) != 0)
1588 return NULL;
1589
1590 if (bfd_read (extstrsize, sizeof extstrsize, 1, abfd) != sizeof extstrsize)
1591 {
1592 if (bfd_get_error () != bfd_error_file_truncated)
1593 return NULL;
1594
1595 /* There is no string table. */
1596 strsize = STRING_SIZE_SIZE;
1597 }
1598 else
1599 {
1600#if STRING_SIZE_SIZE == 4
1601 strsize = bfd_h_get_32 (abfd, (bfd_byte *) extstrsize);
1602#else
1603 #error Change bfd_h_get_32
1604#endif
1605 }
1606
1607 if (strsize < STRING_SIZE_SIZE)
1608 {
1609 (*_bfd_error_handler)
1610 (_("%s: bad string table size %lu"), bfd_get_filename (abfd),
1611 (unsigned long) strsize);
1612 bfd_set_error (bfd_error_bad_value);
1613 return NULL;
1614 }
1615
1616 strings = (char *) bfd_malloc (strsize);
1617 if (strings == NULL)
1618 return NULL;
1619
1620 if (bfd_read (strings + STRING_SIZE_SIZE,
1621 strsize - STRING_SIZE_SIZE, 1, abfd)
1622 != strsize - STRING_SIZE_SIZE)
1623 {
1624 free (strings);
1625 return NULL;
1626 }
1627
1628 obj_coff_strings (abfd) = strings;
1629
1630 return strings;
1631}
1632
1633/* Free up the external symbols and strings read from a COFF file. */
1634
1635boolean
1636_bfd_coff_free_symbols (abfd)
1637 bfd *abfd;
1638{
1639 if (obj_coff_external_syms (abfd) != NULL
1640 && ! obj_coff_keep_syms (abfd))
1641 {
1642 free (obj_coff_external_syms (abfd));
1643 obj_coff_external_syms (abfd) = NULL;
1644 }
1645 if (obj_coff_strings (abfd) != NULL
1646 && ! obj_coff_keep_strings (abfd))
1647 {
1648 free (obj_coff_strings (abfd));
1649 obj_coff_strings (abfd) = NULL;
1650 }
1651 return true;
1652}
1653
1654/* Read a symbol table into freshly bfd_allocated memory, swap it, and
1655 knit the symbol names into a normalized form. By normalized here I
1656 mean that all symbols have an n_offset pointer that points to a null-
1657 terminated string. */
1658
1659combined_entry_type *
1660coff_get_normalized_symtab (abfd)
1661 bfd *abfd;
1662{
1663 combined_entry_type *internal;
1664 combined_entry_type *internal_ptr;
1665 combined_entry_type *symbol_ptr;
1666 combined_entry_type *internal_end;
1667 bfd_size_type symesz;
1668 char *raw_src;
1669 char *raw_end;
1670 const char *string_table = NULL;
1671 char *debug_section = NULL;
1672 unsigned long size;
1673
1674 if (obj_raw_syments (abfd) != NULL)
1675 return obj_raw_syments (abfd);
1676
1677 size = obj_raw_syment_count (abfd) * sizeof (combined_entry_type);
1678 internal = (combined_entry_type *) bfd_zalloc (abfd, size);
1679 if (internal == NULL && size != 0)
1680 return NULL;
1681 internal_end = internal + obj_raw_syment_count (abfd);
1682
1683 if (! _bfd_coff_get_external_symbols (abfd))
1684 return NULL;
1685
1686 raw_src = (char *) obj_coff_external_syms (abfd);
1687
1688 /* mark the end of the symbols */
1689 symesz = bfd_coff_symesz (abfd);
1690 raw_end = (char *) raw_src + obj_raw_syment_count (abfd) * symesz;
1691
1692 /* FIXME SOMEDAY. A string table size of zero is very weird, but
1693 probably possible. If one shows up, it will probably kill us. */
1694
1695 /* Swap all the raw entries */
1696 for (internal_ptr = internal;
1697 raw_src < raw_end;
1698 raw_src += symesz, internal_ptr++)
1699 {
1700
1701 unsigned int i;
1702 bfd_coff_swap_sym_in (abfd, (PTR) raw_src,
1703 (PTR) & internal_ptr->u.syment);
1704 symbol_ptr = internal_ptr;
1705
1706 for (i = 0;
1707 i < symbol_ptr->u.syment.n_numaux;
1708 i++)
1709 {
1710 internal_ptr++;
1711 raw_src += symesz;
1712 bfd_coff_swap_aux_in (abfd, (PTR) raw_src,
1713 symbol_ptr->u.syment.n_type,
1714 symbol_ptr->u.syment.n_sclass,
1715 i, symbol_ptr->u.syment.n_numaux,
1716 &(internal_ptr->u.auxent));
1717 coff_pointerize_aux (abfd, internal, symbol_ptr, i,
1718 internal_ptr);
1719 }
1720 }
1721
1722 /* Free the raw symbols, but not the strings (if we have them). */
1723 obj_coff_keep_strings (abfd) = true;
1724 if (! _bfd_coff_free_symbols (abfd))
1725 return NULL;
1726
1727 for (internal_ptr = internal; internal_ptr < internal_end;
1728 internal_ptr++)
1729 {
1730 if (internal_ptr->u.syment.n_sclass == C_FILE
1731 && internal_ptr->u.syment.n_numaux > 0)
1732 {
1733 /* make a file symbol point to the name in the auxent, since
1734 the text ".file" is redundant */
1735 if ((internal_ptr + 1)->u.auxent.x_file.x_n.x_zeroes == 0)
1736 {
1737 /* the filename is a long one, point into the string table */
1738 if (string_table == NULL)
1739 {
1740 string_table = _bfd_coff_read_string_table (abfd);
1741 if (string_table == NULL)
1742 return NULL;
1743 }
1744
1745 internal_ptr->u.syment._n._n_n._n_offset =
1746 ((long)
1747 (string_table
1748 + (internal_ptr + 1)->u.auxent.x_file.x_n.x_offset));
1749 }
1750 else
1751 {
7bb9db4d
ILT
1752 /* Ordinary short filename, put into memory anyway. The
1753 Microsoft PE tools sometimes store a filename in
1754 multiple AUX entries. */
ec0ef80e
DD
1755 if (internal_ptr->u.syment.n_numaux > 1
1756 && coff_data (abfd)->pe)
1757 {
7bb9db4d
ILT
1758 internal_ptr->u.syment._n._n_n._n_offset =
1759 ((long)
1760 copy_name (abfd,
1761 (internal_ptr + 1)->u.auxent.x_file.x_fname,
1762 internal_ptr->u.syment.n_numaux * symesz));
ec0ef80e
DD
1763 }
1764 else
1765 {
7bb9db4d
ILT
1766 internal_ptr->u.syment._n._n_n._n_offset =
1767 ((long)
1768 copy_name (abfd,
1769 (internal_ptr + 1)->u.auxent.x_file.x_fname,
692b7d62 1770 bfd_coff_filnmlen (abfd)));
ec0ef80e 1771 }
252b5132
RH
1772 }
1773 }
1774 else
1775 {
1776 if (internal_ptr->u.syment._n._n_n._n_zeroes != 0)
1777 {
1778 /* This is a "short" name. Make it long. */
1779 unsigned long i = 0;
1780 char *newstring = NULL;
1781
1782 /* find the length of this string without walking into memory
1783 that isn't ours. */
1784 for (i = 0; i < 8; ++i)
1785 {
1786 if (internal_ptr->u.syment._n._n_name[i] == '\0')
1787 {
1788 break;
1789 } /* if end of string */
1790 } /* possible lengths of this string. */
1791
1792 if ((newstring = (PTR) bfd_alloc (abfd, ++i)) == NULL)
1793 return (NULL);
1794 memset (newstring, 0, i);
1795 strncpy (newstring, internal_ptr->u.syment._n._n_name, i - 1);
1796 internal_ptr->u.syment._n._n_n._n_offset = (long int) newstring;
1797 internal_ptr->u.syment._n._n_n._n_zeroes = 0;
1798 }
1799 else if (internal_ptr->u.syment._n._n_n._n_offset == 0)
1800 internal_ptr->u.syment._n._n_n._n_offset = (long int) "";
1801 else if (!bfd_coff_symname_in_debug (abfd, &internal_ptr->u.syment))
1802 {
1803 /* Long name already. Point symbol at the string in the
1804 table. */
1805 if (string_table == NULL)
1806 {
1807 string_table = _bfd_coff_read_string_table (abfd);
1808 if (string_table == NULL)
1809 return NULL;
1810 }
1811 internal_ptr->u.syment._n._n_n._n_offset =
1812 ((long int)
1813 (string_table
1814 + internal_ptr->u.syment._n._n_n._n_offset));
1815 }
1816 else
1817 {
1818 /* Long name in debug section. Very similar. */
1819 if (debug_section == NULL)
1820 debug_section = build_debug_section (abfd);
1821 internal_ptr->u.syment._n._n_n._n_offset = (long int)
1822 (debug_section + internal_ptr->u.syment._n._n_n._n_offset);
1823 }
1824 }
1825 internal_ptr += internal_ptr->u.syment.n_numaux;
1826 }
1827
1828 obj_raw_syments (abfd) = internal;
1829 BFD_ASSERT (obj_raw_syment_count (abfd)
1830 == (unsigned int) (internal_ptr - internal));
1831
1832 return (internal);
1833} /* coff_get_normalized_symtab() */
1834
1835long
1836coff_get_reloc_upper_bound (abfd, asect)
1837 bfd *abfd;
1838 sec_ptr asect;
1839{
1840 if (bfd_get_format (abfd) != bfd_object)
1841 {
1842 bfd_set_error (bfd_error_invalid_operation);
1843 return -1;
1844 }
1845 return (asect->reloc_count + 1) * sizeof (arelent *);
1846}
1847
1848asymbol *
1849coff_make_empty_symbol (abfd)
1850 bfd *abfd;
1851{
1852 coff_symbol_type *new = (coff_symbol_type *) bfd_alloc (abfd, sizeof (coff_symbol_type));
1853 if (new == NULL)
1854 return (NULL);
1855 memset (new, 0, sizeof *new);
1856 new->symbol.section = 0;
1857 new->native = 0;
1858 new->lineno = (alent *) NULL;
1859 new->done_lineno = false;
1860 new->symbol.the_bfd = abfd;
1861 return &new->symbol;
1862}
1863
1864/* Make a debugging symbol. */
1865
1866asymbol *
1867coff_bfd_make_debug_symbol (abfd, ptr, sz)
1868 bfd *abfd;
7442e600
ILT
1869 PTR ptr ATTRIBUTE_UNUSED;
1870 unsigned long sz ATTRIBUTE_UNUSED;
252b5132
RH
1871{
1872 coff_symbol_type *new = (coff_symbol_type *) bfd_alloc (abfd, sizeof (coff_symbol_type));
1873 if (new == NULL)
1874 return (NULL);
1875 /* @@ The 10 is a guess at a plausible maximum number of aux entries
1876 (but shouldn't be a constant). */
1877 new->native = (combined_entry_type *) bfd_zalloc (abfd, sizeof (combined_entry_type) * 10);
1878 if (!new->native)
1879 return (NULL);
1880 new->symbol.section = bfd_abs_section_ptr;
1881 new->symbol.flags = BSF_DEBUGGING;
1882 new->lineno = (alent *) NULL;
1883 new->done_lineno = false;
1884 new->symbol.the_bfd = abfd;
1885 return &new->symbol;
1886}
1887
1888/*ARGSUSED */
1889void
1890coff_get_symbol_info (abfd, symbol, ret)
1891 bfd *abfd;
1892 asymbol *symbol;
1893 symbol_info *ret;
1894{
1895 bfd_symbol_info (symbol, ret);
1896 if (coffsymbol (symbol)->native != NULL
1897 && coffsymbol (symbol)->native->fix_value)
1898 {
1899 combined_entry_type *psym;
1900
1901 psym = ((combined_entry_type *)
1902 coffsymbol (symbol)->native->u.syment.n_value);
1903 ret->value = (bfd_vma) (psym - obj_raw_syments (abfd));
1904 }
1905}
1906
1907/* Return the COFF syment for a symbol. */
1908
1909boolean
1910bfd_coff_get_syment (abfd, symbol, psyment)
1911 bfd *abfd;
1912 asymbol *symbol;
1913 struct internal_syment *psyment;
1914{
1915 coff_symbol_type *csym;
1916
1917 csym = coff_symbol_from (abfd, symbol);
1918 if (csym == NULL || csym->native == NULL)
1919 {
1920 bfd_set_error (bfd_error_invalid_operation);
1921 return false;
1922 }
1923
1924 *psyment = csym->native->u.syment;
1925
1926 if (csym->native->fix_value)
1927 psyment->n_value = ((combined_entry_type *) psyment->n_value
1928 - obj_raw_syments (abfd));
1929
1930 /* FIXME: We should handle fix_line here. */
1931
1932 return true;
1933}
1934
1935/* Return the COFF auxent for a symbol. */
1936
1937boolean
1938bfd_coff_get_auxent (abfd, symbol, indx, pauxent)
1939 bfd *abfd;
1940 asymbol *symbol;
1941 int indx;
1942 union internal_auxent *pauxent;
1943{
1944 coff_symbol_type *csym;
1945 combined_entry_type *ent;
1946
1947 csym = coff_symbol_from (abfd, symbol);
1948
1949 if (csym == NULL
1950 || csym->native == NULL
1951 || indx >= csym->native->u.syment.n_numaux)
1952 {
1953 bfd_set_error (bfd_error_invalid_operation);
1954 return false;
1955 }
1956
1957 ent = csym->native + indx + 1;
1958
1959 *pauxent = ent->u.auxent;
1960
1961 if (ent->fix_tag)
1962 pauxent->x_sym.x_tagndx.l =
1963 ((combined_entry_type *) pauxent->x_sym.x_tagndx.p
1964 - obj_raw_syments (abfd));
1965
1966 if (ent->fix_end)
1967 pauxent->x_sym.x_fcnary.x_fcn.x_endndx.l =
1968 ((combined_entry_type *) pauxent->x_sym.x_fcnary.x_fcn.x_endndx.p
1969 - obj_raw_syments (abfd));
1970
1971 if (ent->fix_scnlen)
1972 pauxent->x_csect.x_scnlen.l =
1973 ((combined_entry_type *) pauxent->x_csect.x_scnlen.p
1974 - obj_raw_syments (abfd));
1975
1976 return true;
1977}
1978
1979/* Print out information about COFF symbol. */
1980
1981void
1982coff_print_symbol (abfd, filep, symbol, how)
1983 bfd *abfd;
1984 PTR filep;
1985 asymbol *symbol;
1986 bfd_print_symbol_type how;
1987{
1988 FILE *file = (FILE *) filep;
1989
1990 switch (how)
1991 {
1992 case bfd_print_symbol_name:
1993 fprintf (file, "%s", symbol->name);
1994 break;
1995
1996 case bfd_print_symbol_more:
1997 fprintf (file, "coff %s %s",
1998 coffsymbol (symbol)->native ? "n" : "g",
1999 coffsymbol (symbol)->lineno ? "l" : " ");
2000 break;
2001
2002 case bfd_print_symbol_all:
2003 if (coffsymbol (symbol)->native)
2004 {
2005 unsigned long val;
2006 unsigned int aux;
2007 combined_entry_type *combined = coffsymbol (symbol)->native;
2008 combined_entry_type *root = obj_raw_syments (abfd);
2009 struct lineno_cache_entry *l = coffsymbol (symbol)->lineno;
2010
2011 fprintf (file, "[%3ld]", (long) (combined - root));
2012
2013 if (! combined->fix_value)
2014 val = (unsigned long) combined->u.syment.n_value;
2015 else
2016 val = ((unsigned long)
2017 ((combined_entry_type *) combined->u.syment.n_value
2018 - root));
2019
2020 fprintf (file,
2021 "(sec %2d)(fl 0x%02x)(ty %3x)(scl %3d) (nx %d) 0x%08lx %s",
2022 combined->u.syment.n_scnum,
2023 combined->u.syment.n_flags,
2024 combined->u.syment.n_type,
2025 combined->u.syment.n_sclass,
2026 combined->u.syment.n_numaux,
2027 val,
2028 symbol->name);
2029
2030 for (aux = 0; aux < combined->u.syment.n_numaux; aux++)
2031 {
2032 combined_entry_type *auxp = combined + aux + 1;
2033 long tagndx;
2034
2035 if (auxp->fix_tag)
2036 tagndx = auxp->u.auxent.x_sym.x_tagndx.p - root;
2037 else
2038 tagndx = auxp->u.auxent.x_sym.x_tagndx.l;
2039
2040 fprintf (file, "\n");
2041
2042 if (bfd_coff_print_aux (abfd, file, root, combined, auxp, aux))
2043 continue;
2044
2045 switch (combined->u.syment.n_sclass)
2046 {
2047 case C_FILE:
2048 fprintf (file, "File ");
2049 break;
2050
2051 case C_STAT:
2052 if (combined->u.syment.n_type == T_NULL)
2053 /* probably a section symbol? */
2054 {
2055 fprintf (file, "AUX scnlen 0x%lx nreloc %d nlnno %d",
2056 (long) auxp->u.auxent.x_scn.x_scnlen,
2057 auxp->u.auxent.x_scn.x_nreloc,
2058 auxp->u.auxent.x_scn.x_nlinno);
2059 if (auxp->u.auxent.x_scn.x_checksum != 0
2060 || auxp->u.auxent.x_scn.x_associated != 0
2061 || auxp->u.auxent.x_scn.x_comdat != 0)
2062 fprintf (file, " checksum 0x%lx assoc %d comdat %d",
2063 auxp->u.auxent.x_scn.x_checksum,
2064 auxp->u.auxent.x_scn.x_associated,
2065 auxp->u.auxent.x_scn.x_comdat);
2066 break;
2067 }
2068 /* else fall through */
2069
2070 default:
2071 fprintf (file, "AUX lnno %d size 0x%x tagndx %ld",
2072 auxp->u.auxent.x_sym.x_misc.x_lnsz.x_lnno,
2073 auxp->u.auxent.x_sym.x_misc.x_lnsz.x_size,
2074 tagndx);
2075 if (auxp->fix_end)
2076 fprintf (file, " endndx %ld",
2077 ((long)
2078 (auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p
2079 - root)));
2080 break;
2081 }
2082 }
2083
2084 if (l)
2085 {
2086 fprintf (file, "\n%s :", l->u.sym->name);
2087 l++;
2088 while (l->line_number)
2089 {
2090 fprintf (file, "\n%4d : 0x%lx",
2091 l->line_number,
2092 ((unsigned long)
2093 (l->u.offset + symbol->section->vma)));
2094 l++;
2095 }
2096 }
2097 }
2098 else
2099 {
2100 bfd_print_symbol_vandf ((PTR) file, symbol);
2101 fprintf (file, " %-5s %s %s %s",
2102 symbol->section->name,
2103 coffsymbol (symbol)->native ? "n" : "g",
2104 coffsymbol (symbol)->lineno ? "l" : " ",
2105 symbol->name);
2106 }
2107 }
2108}
2109
2110/* Return whether a symbol name implies a local symbol. In COFF,
2111 local symbols generally start with ``.L''. Most targets use this
2112 function for the is_local_label_name entry point, but some may
2113 override it. */
2114
2115boolean
2116_bfd_coff_is_local_label_name (abfd, name)
7442e600 2117 bfd *abfd ATTRIBUTE_UNUSED;
252b5132
RH
2118 const char *name;
2119{
2120 return name[0] == '.' && name[1] == 'L';
2121}
2122
2123/* Provided a BFD, a section and an offset into the section, calculate
2124 and return the name of the source file and the line nearest to the
2125 wanted location. */
2126
2127/*ARGSUSED*/
2128boolean
2129coff_find_nearest_line (abfd, section, symbols, offset, filename_ptr,
2130 functionname_ptr, line_ptr)
2131 bfd *abfd;
2132 asection *section;
2133 asymbol **symbols;
2134 bfd_vma offset;
2135 CONST char **filename_ptr;
2136 CONST char **functionname_ptr;
2137 unsigned int *line_ptr;
2138{
2139 boolean found;
2140 unsigned int i;
2141 unsigned int line_base;
2142 coff_data_type *cof = coff_data (abfd);
2143 /* Run through the raw syments if available */
2144 combined_entry_type *p;
2145 combined_entry_type *pend;
2146 alent *l;
2147 struct coff_section_tdata *sec_data;
2148
2149 /* Before looking through the symbol table, try to use a .stab
2150 section to find the information. */
2151 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
2152 &found, filename_ptr,
2153 functionname_ptr, line_ptr,
2154 &coff_data (abfd)->line_info))
2155 return false;
2156 if (found)
2157 return true;
2158
2159 *filename_ptr = 0;
2160 *functionname_ptr = 0;
2161 *line_ptr = 0;
2162
2163 /* Don't try and find line numbers in a non coff file */
2164 if (abfd->xvec->flavour != bfd_target_coff_flavour)
2165 return false;
2166
2167 if (cof == NULL)
2168 return false;
2169
2170 /* Find the first C_FILE symbol. */
2171 p = cof->raw_syments;
2172 if (!p)
2173 return false;
2174
2175 pend = p + cof->raw_syment_count;
2176 while (p < pend)
2177 {
2178 if (p->u.syment.n_sclass == C_FILE)
2179 break;
2180 p += 1 + p->u.syment.n_numaux;
2181 }
2182
2183 if (p < pend)
2184 {
2185 bfd_vma sec_vma;
2186 bfd_vma maxdiff;
2187
2188 /* Look through the C_FILE symbols to find the best one. */
2189 sec_vma = bfd_get_section_vma (abfd, section);
2190 *filename_ptr = (char *) p->u.syment._n._n_n._n_offset;
2191 maxdiff = (bfd_vma) 0 - (bfd_vma) 1;
2192 while (1)
2193 {
2194 combined_entry_type *p2;
2195
2196 for (p2 = p + 1 + p->u.syment.n_numaux;
2197 p2 < pend;
2198 p2 += 1 + p2->u.syment.n_numaux)
2199 {
2200 if (p2->u.syment.n_scnum > 0
2201 && (section
2202 == coff_section_from_bfd_index (abfd,
2203 p2->u.syment.n_scnum)))
2204 break;
2205 if (p2->u.syment.n_sclass == C_FILE)
2206 {
2207 p2 = pend;
2208 break;
2209 }
2210 }
2211
798c1fb8
ILT
2212 /* We use <= MAXDIFF here so that if we get a zero length
2213 file, we actually use the next file entry. */
252b5132
RH
2214 if (p2 < pend
2215 && offset + sec_vma >= (bfd_vma) p2->u.syment.n_value
798c1fb8 2216 && offset + sec_vma - (bfd_vma) p2->u.syment.n_value <= maxdiff)
252b5132
RH
2217 {
2218 *filename_ptr = (char *) p->u.syment._n._n_n._n_offset;
2219 maxdiff = offset + sec_vma - p2->u.syment.n_value;
2220 }
2221
2222 /* Avoid endless loops on erroneous files by ensuring that
2223 we always move forward in the file. */
2224 if (p - cof->raw_syments >= p->u.syment.n_value)
2225 break;
2226
2227 p = cof->raw_syments + p->u.syment.n_value;
2228 if (p > pend || p->u.syment.n_sclass != C_FILE)
2229 break;
2230 }
2231 }
2232
2233 /* Now wander though the raw linenumbers of the section */
2234 /* If we have been called on this section before, and the offset we
2235 want is further down then we can prime the lookup loop. */
2236 sec_data = coff_section_data (abfd, section);
2237 if (sec_data != NULL
2238 && sec_data->i > 0
2239 && offset >= sec_data->offset)
2240 {
2241 i = sec_data->i;
2242 *functionname_ptr = sec_data->function;
2243 line_base = sec_data->line_base;
2244 }
2245 else
2246 {
2247 i = 0;
2248 line_base = 0;
2249 }
2250
2251 if (section->lineno != NULL)
2252 {
798c1fb8
ILT
2253 bfd_vma last_value = 0;
2254
252b5132
RH
2255 l = &section->lineno[i];
2256
2257 for (; i < section->lineno_count; i++)
2258 {
2259 if (l->line_number == 0)
2260 {
2261 /* Get the symbol this line number points at */
2262 coff_symbol_type *coff = (coff_symbol_type *) (l->u.sym);
2263 if (coff->symbol.value > offset)
2264 break;
2265 *functionname_ptr = coff->symbol.name;
798c1fb8 2266 last_value = coff->symbol.value;
252b5132
RH
2267 if (coff->native)
2268 {
2269 combined_entry_type *s = coff->native;
2270 s = s + 1 + s->u.syment.n_numaux;
2271
2272 /* In XCOFF a debugging symbol can follow the
2273 function symbol. */
2274 if (s->u.syment.n_scnum == N_DEBUG)
2275 s = s + 1 + s->u.syment.n_numaux;
2276
2277 /* S should now point to the .bf of the function. */
2278 if (s->u.syment.n_numaux)
2279 {
2280 /* The linenumber is stored in the auxent. */
2281 union internal_auxent *a = &((s + 1)->u.auxent);
2282 line_base = a->x_sym.x_misc.x_lnsz.x_lnno;
2283 *line_ptr = line_base;
2284 }
2285 }
2286 }
2287 else
2288 {
2289 if (l->u.offset > offset)
2290 break;
2291 *line_ptr = l->line_number + line_base - 1;
2292 }
2293 l++;
2294 }
798c1fb8
ILT
2295
2296 /* If we fell off the end of the loop, then assume that this
2297 symbol has no line number info. Otherwise, symbols with no
2298 line number info get reported with the line number of the
2299 last line of the last symbol which does have line number
2300 info. We use 0x100 as a slop to account for cases where the
2301 last line has executable code. */
2302 if (i >= section->lineno_count
2303 && last_value != 0
2304 && offset - last_value > 0x100)
2305 {
2306 *functionname_ptr = NULL;
2307 *line_ptr = 0;
2308 }
252b5132
RH
2309 }
2310
2311 /* Cache the results for the next call. */
2312 if (sec_data == NULL && section->owner == abfd)
2313 {
2314 section->used_by_bfd =
2315 ((PTR) bfd_zalloc (abfd,
2316 sizeof (struct coff_section_tdata)));
2317 sec_data = (struct coff_section_tdata *) section->used_by_bfd;
2318 }
2319 if (sec_data != NULL)
2320 {
2321 sec_data->offset = offset;
2322 sec_data->i = i;
2323 sec_data->function = *functionname_ptr;
2324 sec_data->line_base = line_base;
2325 }
2326
2327 return true;
2328}
2329
2330int
2331coff_sizeof_headers (abfd, reloc)
2332 bfd *abfd;
2333 boolean reloc;
2334{
2335 size_t size;
2336
2337 if (reloc == false)
2338 {
2339 size = bfd_coff_filhsz (abfd) + bfd_coff_aoutsz (abfd);
2340 }
2341 else
2342 {
2343 size = bfd_coff_filhsz (abfd);
2344 }
2345
2346 size += abfd->section_count * bfd_coff_scnhsz (abfd);
2347 return size;
2348}
2349
2350/* Change the class of a coff symbol held by BFD. */
2351boolean
2352bfd_coff_set_symbol_class (abfd, symbol, class)
2353 bfd * abfd;
2354 asymbol * symbol;
2355 unsigned int class;
2356{
2357 coff_symbol_type * csym;
2358
2359 csym = coff_symbol_from (abfd, symbol);
2360 if (csym == NULL)
2361 {
2362 bfd_set_error (bfd_error_invalid_operation);
2363 return false;
2364 }
2365 else if (csym->native == NULL)
2366 {
2367 /* This is an alien symbol which no native coff backend data.
2368 We cheat here by creating a fake native entry for it and
2369 then filling in the class. This code is based on that in
2370 coff_write_alien_symbol(). */
2371
2372 combined_entry_type * native;
2373
2374 native = (combined_entry_type *) bfd_alloc (abfd, sizeof (* native));
2375 if (native == NULL)
2376 return false;
2377
2378 memset (native, 0, sizeof (* native));
2379
2380 native->u.syment.n_type = T_NULL;
2381 native->u.syment.n_sclass = class;
2382
2383 if (bfd_is_und_section (symbol->section))
2384 {
2385 native->u.syment.n_scnum = N_UNDEF;
2386 native->u.syment.n_value = symbol->value;
2387 }
2388 else if (bfd_is_com_section (symbol->section))
2389 {
2390 native->u.syment.n_scnum = N_UNDEF;
2391 native->u.syment.n_value = symbol->value;
2392 }
2393 else
2394 {
2395 native->u.syment.n_scnum =
2396 symbol->section->output_section->target_index;
2397 native->u.syment.n_value = (symbol->value
2398 + symbol->section->output_offset);
2399 if (! obj_pe (abfd))
2400 native->u.syment.n_value += symbol->section->output_section->vma;
2401
2402 /* Copy the any flags from the the file header into the symbol.
2403 FIXME: Why? */
2404 native->u.syment.n_flags = bfd_asymbol_bfd (& csym->symbol)->flags;
2405 }
2406
2407 csym->native = native;
2408 }
2409 else
2410 {
2411 csym->native->u.syment.n_sclass = class;
2412 }
2413
2414 return true;
2415}
2416
This page took 0.225948 seconds and 4 git commands to generate.