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