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