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