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