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