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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-old/] [gdb-6.8/] [bfd/] [coffgen.c] - Blame information for rev 840

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

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