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