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