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