OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [insight/] [bfd/] [coffgen.c] - Blame information for rev 1767

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

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

powered by: WebSVN 2.1.0

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