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