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