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