OpenCores
URL https://opencores.org/ocsvn/openrisc_2011-10-31/openrisc_2011-10-31/trunk

Subversion Repositories openrisc_2011-10-31

[/] [openrisc/] [trunk/] [gnu-src/] [binutils-2.18.50/] [bfd/] [coffgen.c] - Blame information for rev 309

Go to most recent revision | Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.