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