* coffgen.c: #include seclet.h.
[binutils-gdb.git] / bfd / coffgen.c
1 /* Support for the generic parts of COFF, for BFD.
2 Copyright 1990, 1991, 1992 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., 675 Mass Ave, Cambridge, MA 02139, 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 "seclet.h"
43 #include "libcoff.h"
44
45 static asection bfd_debug_section = { "*DEBUG*" };
46
47 /* Take a section header read from a coff file (in HOST byte order),
48 and make a BFD "section" out of it. This is used by ECOFF. */
49 static boolean
50 DEFUN(make_a_section_from_file,(abfd, hdr, target_index),
51 bfd *abfd AND
52 struct internal_scnhdr *hdr AND
53 unsigned int target_index)
54 {
55 asection *return_section;
56 char *name;
57
58 /* Assorted wastage to null-terminate the name, thanks AT&T! */
59 name = bfd_alloc(abfd, sizeof (hdr->s_name)+1);
60 if (name == NULL) {
61 bfd_error = no_memory;
62 return false;
63 }
64 strncpy(name, (char *) &hdr->s_name[0], sizeof (hdr->s_name));
65 name[sizeof (hdr->s_name)] = 0;
66
67 return_section = bfd_make_section(abfd, name);
68 if (return_section == NULL)
69 return_section = bfd_coff_make_section_hook (abfd, name);
70 if (return_section == NULL)
71 return false;
72
73 /* s_paddr is presumed to be = to s_vaddr */
74
75 return_section->vma = hdr->s_vaddr;
76 return_section->_raw_size = hdr->s_size;
77 return_section->filepos = hdr->s_scnptr;
78 return_section->rel_filepos = hdr->s_relptr;
79 return_section->reloc_count = hdr->s_nreloc;
80
81 bfd_coff_set_alignment_hook (abfd, return_section, hdr);
82
83 return_section->line_filepos = hdr->s_lnnoptr;
84
85 return_section->lineno_count = hdr->s_nlnno;
86 return_section->userdata = NULL;
87 return_section->next = (asection *) NULL;
88 return_section->flags = bfd_coff_styp_to_sec_flags_hook (abfd, hdr);
89
90 return_section->target_index = target_index;
91
92 if (hdr->s_nreloc != 0)
93 return_section->flags |= SEC_RELOC;
94 /* FIXME: should this check 'hdr->s_size > 0' */
95 if (hdr->s_scnptr != 0)
96 return_section->flags |= SEC_HAS_CONTENTS;
97 return true;
98 }
99
100 /* Read in a COFF object and make it into a BFD. This is used by
101 ECOFF as well. */
102
103 static
104 bfd_target *
105 DEFUN(coff_real_object_p,(abfd, nscns, internal_f, internal_a),
106 bfd *abfd AND
107 unsigned nscns AND
108 struct internal_filehdr *internal_f AND
109 struct internal_aouthdr *internal_a)
110 {
111 PTR tdata;
112 size_t readsize; /* length of file_info */
113 unsigned int scnhsz;
114 char *external_sections;
115
116 /* Build a play area */
117 tdata = bfd_coff_mkobject_hook (abfd, (PTR) internal_f);
118 if (tdata == NULL)
119 return 0;
120
121 scnhsz = bfd_coff_scnhsz (abfd);
122 readsize = nscns * scnhsz;
123 external_sections = (char *)bfd_alloc(abfd, readsize);
124
125 if (bfd_read((PTR)external_sections, 1, readsize, abfd) != readsize) {
126 goto fail;
127 }
128
129 /* Now copy data as required; construct all asections etc */
130 if (nscns != 0) {
131 unsigned int i;
132 for (i = 0; i < nscns; i++) {
133 struct internal_scnhdr tmp;
134 bfd_coff_swap_scnhdr_in(abfd, (PTR) (external_sections + i * scnhsz),
135 (PTR) &tmp);
136 make_a_section_from_file(abfd,&tmp, i+1);
137 }
138 }
139
140 /* make_abs_section(abfd);*/
141
142 if (bfd_coff_set_arch_mach_hook (abfd, (PTR) internal_f) == false)
143 goto fail;
144
145 if (!(internal_f->f_flags & F_RELFLG))
146 abfd->flags |= HAS_RELOC;
147 if ((internal_f->f_flags & F_EXEC))
148 abfd->flags |= EXEC_P;
149 if (!(internal_f->f_flags & F_LNNO))
150 abfd->flags |= HAS_LINENO;
151 if (!(internal_f->f_flags & F_LSYMS))
152 abfd->flags |= HAS_LOCALS;
153
154
155 bfd_get_symcount(abfd) = internal_f->f_nsyms;
156 if (internal_f->f_nsyms)
157 abfd->flags |= HAS_SYMS;
158
159 bfd_get_start_address(abfd) = internal_f->f_opthdr ? internal_a->entry : 0;
160
161 return abfd->xvec;
162 fail:
163 bfd_release(abfd, tdata);
164 return (bfd_target *)NULL;
165 }
166
167 /* Turn a COFF file into a BFD, but fail with wrong_format if it is
168 not a COFF file. This is also used by ECOFF. */
169
170 bfd_target *
171 DEFUN(coff_object_p,(abfd),
172 bfd *abfd)
173 {
174 unsigned int filhsz;
175 unsigned int aoutsz;
176 int nscns;
177 PTR filehdr;
178 struct internal_filehdr internal_f;
179 struct internal_aouthdr internal_a;
180
181 bfd_error = system_call_error;
182
183 /* figure out how much to read */
184 filhsz = bfd_coff_filhsz (abfd);
185 aoutsz = bfd_coff_aoutsz (abfd);
186
187 filehdr = bfd_alloc (abfd, filhsz);
188 if (filehdr == NULL)
189 return 0;
190 if (bfd_read(filehdr, 1, filhsz, abfd) != filhsz)
191 return 0;
192 bfd_coff_swap_filehdr_in(abfd, filehdr, &internal_f);
193 bfd_release (abfd, filehdr);
194
195 if (bfd_coff_bad_format_hook (abfd, &internal_f) == false) {
196 bfd_error = wrong_format;
197 return 0;
198 }
199 nscns =internal_f.f_nscns;
200
201 if (internal_f.f_opthdr) {
202 PTR opthdr;
203
204 opthdr = bfd_alloc (abfd, aoutsz);
205 if (opthdr == NULL)
206 return 0;;
207 if (bfd_read(opthdr, 1,aoutsz, abfd) != aoutsz) {
208 return 0;
209 }
210 bfd_coff_swap_aouthdr_in(abfd, opthdr, (PTR)&internal_a);
211 }
212
213 /* Seek past the opt hdr stuff */
214 bfd_seek(abfd, (file_ptr) (internal_f.f_opthdr + filhsz), SEEK_SET);
215
216 return coff_real_object_p(abfd, nscns, &internal_f, &internal_a);
217 }
218
219 /* Get the BFD section from a COFF symbol section number. */
220
221 struct sec *
222 DEFUN(coff_section_from_bfd_index,(abfd, index),
223 bfd *abfd AND
224 int index)
225 {
226 struct sec *answer = abfd->sections;
227
228 if (index == N_ABS)
229 {
230 return &bfd_abs_section;
231 }
232 if (index == N_UNDEF)
233 {
234 return &bfd_und_section;
235 }
236 if(index == N_DEBUG)
237 {
238 return &bfd_debug_section;
239
240 }
241
242 while (answer) {
243 if (answer->target_index == index)
244 return answer;
245 answer = answer->next;
246 }
247 BFD_ASSERT(0);
248 return &bfd_und_section; /* For gcc -W and lint. Never executed. */
249 }
250
251 /* Get the upper bound of a COFF symbol table. */
252
253 unsigned int
254 coff_get_symtab_upper_bound(abfd)
255 bfd *abfd;
256 {
257 if (!bfd_coff_slurp_symbol_table(abfd))
258 return 0;
259
260 return (bfd_get_symcount(abfd) + 1) * (sizeof(coff_symbol_type *));
261 }
262
263
264 /* Canonicalize a COFF symbol table. */
265
266 unsigned int
267 DEFUN(coff_get_symtab, (abfd, alocation),
268 bfd *abfd AND
269 asymbol **alocation)
270 {
271 unsigned int counter = 0;
272 coff_symbol_type *symbase;
273 coff_symbol_type **location = (coff_symbol_type **) (alocation);
274 if (!bfd_coff_slurp_symbol_table(abfd))
275 return 0;
276
277 symbase = obj_symbols(abfd);
278 while (counter < bfd_get_symcount(abfd))
279 {
280 /* This nasty code looks at the symbol to decide whether or
281 not it is descibes a constructor/destructor entry point. It
282 is structured this way to (hopefully) speed non matches */
283 #if 0
284 if (0 && symbase->symbol.name[9] == '$')
285 {
286 bfd_constructor_entry(abfd,
287 (asymbol **)location,
288 symbase->symbol.name[10] == 'I' ?
289 "CTOR" : "DTOR");
290 }
291 #endif
292 *(location++) = symbase++;
293 counter++;
294 }
295 *location++ = 0;
296 return bfd_get_symcount(abfd);
297 }
298
299 /* Set lineno_count for the output sections of a COFF file. */
300
301 void
302 DEFUN(coff_count_linenumbers,(abfd),
303 bfd *abfd)
304 {
305 unsigned int limit = bfd_get_symcount(abfd);
306 unsigned int i;
307 asymbol **p;
308 {
309 asection *s = abfd->sections->output_section;
310 while (s) {
311 BFD_ASSERT(s->lineno_count == 0);
312 s = s->next;
313 }
314 }
315
316
317 for (p = abfd->outsymbols, i = 0; i < limit; i++, p++) {
318 asymbol *q_maybe = *p;
319 if (bfd_asymbol_flavour(q_maybe) == bfd_target_coff_flavour) {
320 coff_symbol_type *q = coffsymbol(q_maybe);
321 if (q->lineno) {
322 /*
323 This symbol has a linenumber, increment the owning
324 section's linenumber count
325 */
326 alent *l = q->lineno;
327 q->symbol.section->output_section->lineno_count++;
328 l++;
329 while (l->line_number) {
330 q->symbol.section->output_section->lineno_count++;
331 l++;
332 }
333 }
334 }
335 }
336 }
337
338 /* Takes a bfd and a symbol, returns a pointer to the coff specific
339 area of the symbol if there is one. */
340
341 coff_symbol_type *
342 DEFUN(coff_symbol_from,(ignore_abfd, symbol),
343 bfd *ignore_abfd AND
344 asymbol *symbol)
345 {
346 if (bfd_asymbol_flavour(symbol) != bfd_target_coff_flavour)
347 return (coff_symbol_type *)NULL;
348
349 if (bfd_asymbol_bfd(symbol)->tdata.coff_obj_data == (coff_data_type*)NULL)
350 return (coff_symbol_type *)NULL;
351
352 return (coff_symbol_type *) symbol;
353 }
354
355 static void
356 DEFUN(fixup_symbol_value,(coff_symbol_ptr, syment),
357 coff_symbol_type *coff_symbol_ptr AND
358 struct internal_syment *syment)
359 {
360
361 /* Normalize the symbol flags */
362 if (coff_symbol_ptr->symbol.section == &bfd_com_section) {
363 /* a common symbol is undefined with a value */
364 syment->n_scnum = N_UNDEF;
365 syment->n_value = coff_symbol_ptr->symbol.value;
366 }
367 else if (coff_symbol_ptr->symbol.flags & BSF_DEBUGGING) {
368 syment->n_value = coff_symbol_ptr->symbol.value;
369 }
370 else if (coff_symbol_ptr->symbol.section == & bfd_und_section) {
371 syment->n_scnum = N_UNDEF;
372 syment->n_value = 0;
373 }
374 else {
375 if (coff_symbol_ptr->symbol.section) {
376 syment->n_scnum =
377 coff_symbol_ptr->symbol.section->output_section->target_index;
378
379 syment->n_value =
380 coff_symbol_ptr->symbol.value +
381 coff_symbol_ptr->symbol.section->output_offset +
382 coff_symbol_ptr->symbol.section->output_section->vma;
383 }
384 else {
385 BFD_ASSERT(0);
386 /* This can happen, but I don't know why yet (steve@cygnus.com) */
387 syment->n_scnum = N_ABS;
388 syment->n_value = coff_symbol_ptr->symbol.value;
389 }
390 }
391 }
392
393 /* run through all the symbols in the symbol table and work out what
394 their indexes into the symbol table will be when output
395
396 Coff requires that each C_FILE symbol points to the next one in the
397 chain, and that the last one points to the first external symbol. We
398 do that here too.
399
400 */
401 void
402 DEFUN(coff_renumber_symbols,(bfd_ptr),
403 bfd *bfd_ptr)
404 {
405 unsigned int symbol_count = bfd_get_symcount(bfd_ptr);
406 asymbol **symbol_ptr_ptr = bfd_ptr->outsymbols;
407 unsigned int native_index = 0;
408 struct internal_syment *last_file = (struct internal_syment *)NULL;
409 unsigned int symbol_index;
410
411 /* COFF demands that undefined symbols come after all other symbols.
412 Since we don't need to impose this extra knowledge on all our client
413 programs, deal with that here. Sort the symbol table; just move the
414 undefined symbols to the end, leaving the rest alone. */
415 /* @@ Do we have some condition we could test for, so we don't always
416 have to do this? I don't think relocatability is quite right, but
417 I'm not certain. [raeburn:19920508.1711EST] */
418 {
419 asymbol **newsyms;
420 int i;
421
422 newsyms = (asymbol **) bfd_alloc_by_size_t (bfd_ptr,
423 sizeof (asymbol *)
424 * (symbol_count + 1));
425 bfd_ptr->outsymbols = newsyms;
426 for (i = 0; i < symbol_count; i++)
427 if (symbol_ptr_ptr[i]->section != &bfd_und_section)
428 *newsyms++ = symbol_ptr_ptr[i];
429 for (i = 0; i < symbol_count; i++)
430 if (symbol_ptr_ptr[i]->section == &bfd_und_section)
431 *newsyms++ = symbol_ptr_ptr[i];
432 *newsyms = (asymbol *) NULL;
433 symbol_ptr_ptr = bfd_ptr->outsymbols;
434 }
435
436 for (symbol_index = 0; symbol_index < symbol_count; symbol_index++)
437 {
438 coff_symbol_type *coff_symbol_ptr = coff_symbol_from(bfd_ptr, symbol_ptr_ptr[symbol_index]);
439 if (coff_symbol_ptr && coff_symbol_ptr->native) {
440 combined_entry_type *s = coff_symbol_ptr->native;
441 int i;
442
443 if (s->u.syment.n_sclass == C_FILE)
444 {
445 if (last_file != (struct internal_syment *)NULL) {
446 last_file->n_value = native_index;
447 }
448 last_file = &(s->u.syment);
449 }
450 else {
451
452 /* Modify the symbol values according to their section and
453 type */
454
455 fixup_symbol_value(coff_symbol_ptr, &(s->u.syment));
456 }
457 for (i = 0; i < s->u.syment.n_numaux + 1; i++) {
458 s[i].offset = native_index ++;
459 }
460 }
461 else {
462 native_index++;
463 }
464 }
465 obj_conv_table_size (bfd_ptr) = native_index;
466 }
467
468 /*
469 Run thorough the symbol table again, and fix it so that all pointers to
470 entries are changed to the entries' index in the output symbol table.
471
472 */
473 void
474 DEFUN(coff_mangle_symbols,(bfd_ptr),
475 bfd *bfd_ptr)
476 {
477 unsigned int symbol_count = bfd_get_symcount(bfd_ptr);
478 asymbol **symbol_ptr_ptr = bfd_ptr->outsymbols;
479 unsigned int symbol_index;
480
481 for (symbol_index = 0; symbol_index < symbol_count; symbol_index++)
482 {
483 coff_symbol_type *coff_symbol_ptr =
484 coff_symbol_from(bfd_ptr, symbol_ptr_ptr[symbol_index]);
485
486 if (coff_symbol_ptr && coff_symbol_ptr->native) {
487 int i;
488 combined_entry_type *s = coff_symbol_ptr->native;
489
490 for (i = 0; i < s->u.syment.n_numaux ; i++) {
491 combined_entry_type *a = s + i + 1;
492 if (a->fix_tag) {
493 a->u.auxent.x_sym.x_tagndx.l =
494 a->u.auxent.x_sym.x_tagndx.p->offset;
495 a->fix_tag = 0;
496 }
497 if (a->fix_end) {
498 a->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l =
499 a->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p->offset;
500 a->fix_end = 0;
501
502 }
503
504 }
505 }
506 }
507 }
508
509 static int string_size;
510
511 static void
512 DEFUN(coff_fix_symbol_name,(abfd, symbol, native),
513 bfd *abfd AND
514 asymbol *symbol AND
515 combined_entry_type *native)
516 {
517 unsigned int name_length;
518 union internal_auxent *auxent;
519 char * name = ( char *)(symbol->name);
520
521 if (name == (char *) NULL) {
522 /* coff symbols always have names, so we'll make one up */
523 symbol->name = "strange";
524 name = (char *)symbol->name;
525 }
526 name_length = strlen(name);
527
528 if (native->u.syment.n_sclass == C_FILE) {
529 strncpy(native->u.syment._n._n_name, ".file", SYMNMLEN);
530 auxent = &(native+1)->u.auxent;
531
532 if (bfd_coff_long_filenames (abfd)) {
533 if (name_length <= FILNMLEN) {
534 strncpy(auxent->x_file.x_fname, name, FILNMLEN);
535 }
536 else {
537 auxent->x_file.x_n.x_offset = string_size + 4;
538 auxent->x_file.x_n.x_zeroes = 0;
539 string_size += name_length + 1;
540 }
541 }
542 else {
543 strncpy(auxent->x_file.x_fname, name, FILNMLEN);
544 if (name_length > FILNMLEN) {
545 name[FILNMLEN] = '\0';
546 }
547 }
548 }
549 else
550 { /* NOT A C_FILE SYMBOL */
551 if (name_length <= SYMNMLEN) {
552 /* This name will fit into the symbol neatly */
553 strncpy(native->u.syment._n._n_name, symbol->name, SYMNMLEN);
554 }
555 else {
556 native->u.syment._n._n_n._n_offset = string_size + 4;
557 native->u.syment._n._n_n._n_zeroes = 0;
558 string_size += name_length + 1;
559 }
560 }
561 }
562
563 #define set_index(symbol, idx) ((symbol)->udata =(PTR) (idx))
564
565 static unsigned int
566 DEFUN(coff_write_symbol,(abfd, symbol, native, written),
567 bfd *abfd AND
568 asymbol *symbol AND
569 combined_entry_type *native AND
570 unsigned int written)
571 {
572 unsigned int numaux = native->u.syment.n_numaux;
573 int type = native->u.syment.n_type;
574 int class = native->u.syment.n_sclass;
575 PTR buf;
576 bfd_size_type symesz;
577
578 /* @@ bfd_debug_section isn't accessible outside this file, but we know
579 that C_FILE symbols belong there. So move them. */
580 if (native->u.syment.n_sclass == C_FILE)
581 symbol->section = &bfd_debug_section;
582
583 if (symbol->section == &bfd_abs_section)
584 {
585 native->u.syment.n_scnum = N_ABS;
586 }
587 else if (symbol->section == &bfd_debug_section)
588 {
589 native->u.syment.n_scnum = N_DEBUG;
590 }
591 else if (symbol->section == &bfd_und_section)
592 {
593 native->u.syment.n_scnum = N_UNDEF;
594 }
595 else
596 {
597 native->u.syment.n_scnum =
598 symbol->section->output_section->target_index;
599 }
600
601
602 coff_fix_symbol_name(abfd, symbol, native);
603
604 symesz = bfd_coff_symesz (abfd);
605 buf = bfd_alloc (abfd, symesz);
606 bfd_coff_swap_sym_out(abfd, &native->u.syment, buf);
607 bfd_write(buf, 1, symesz, abfd);
608 bfd_release (abfd, buf);
609
610 if (native->u.syment.n_numaux > 0)
611 {
612 bfd_size_type auxesz;
613 unsigned int j;
614
615 auxesz = bfd_coff_auxesz (abfd);
616 buf = bfd_alloc (abfd, auxesz);
617 for (j = 0; j < native->u.syment.n_numaux; j++)
618 {
619 bfd_coff_swap_aux_out(abfd,
620 &((native + j + 1)->u.auxent),
621 type,
622 class,
623 buf);
624 bfd_write(buf, 1, auxesz, abfd);
625 }
626 bfd_release (abfd, buf);
627 }
628 /*
629 Reuse somewhere in the symbol to keep the index
630 */
631 set_index(symbol, written);
632 return written + 1 + numaux;
633 }
634
635
636 static unsigned int
637 DEFUN(coff_write_alien_symbol,(abfd, symbol, written),
638 bfd *abfd AND
639 asymbol *symbol AND
640 unsigned int written)
641 {
642 /*
643 This symbol has been created by the loader, or come from a non
644 coff format. It has no native element to inherit, make our
645 own
646 */
647 combined_entry_type *native;
648 combined_entry_type dummy;
649 native = &dummy;
650 native->u.syment.n_type = T_NULL;
651 native->u.syment.n_flags = 0;
652 if (symbol->section == &bfd_und_section)
653 {
654 native->u.syment.n_scnum = N_UNDEF;
655 native->u.syment.n_value = symbol->value;
656 }
657 else if (symbol->section == &bfd_com_section)
658 {
659 native->u.syment.n_scnum = N_UNDEF;
660 native->u.syment.n_value = symbol->value;
661
662 }
663
664 else if (symbol->flags & BSF_DEBUGGING) {
665 /*
666 remove name so it doesn't take up any space
667 */
668 symbol->name = "";
669 }
670 else {
671 native->u.syment.n_scnum = symbol->section->output_section->target_index;
672 native->u.syment.n_value = symbol->value +
673 symbol->section->output_section->vma +
674 symbol->section->output_offset;
675 /* Copy the any flags from the the file hdr into the symbol */
676 {
677 coff_symbol_type *c = coff_symbol_from(abfd, symbol);
678 if (c != (coff_symbol_type *)NULL) {
679 native->u.syment.n_flags = bfd_asymbol_bfd(&c->symbol)->flags;
680 }
681 }
682 }
683
684 native->u.syment.n_type = 0;
685 if (symbol->flags & BSF_LOCAL)
686 native->u.syment.n_sclass = C_STAT;
687 else
688 native->u.syment.n_sclass = C_EXT;
689 native->u.syment.n_numaux = 0;
690
691 return coff_write_symbol(abfd, symbol, native, written);
692 }
693
694 static unsigned int
695 DEFUN(coff_write_native_symbol,(abfd, symbol, written),
696 bfd *abfd AND
697 coff_symbol_type *symbol AND
698 unsigned int written)
699 {
700 /*
701 Does this symbol have an ascociated line number - if so then
702 make it remember this symbol index. Also tag the auxent of
703 this symbol to point to the right place in the lineno table
704 */
705 combined_entry_type *native = symbol->native;
706
707 alent *lineno = symbol->lineno;
708
709 if (lineno && !symbol->done_lineno) {
710 unsigned int count = 0;
711 lineno[count].u.offset = written;
712 if (native->u.syment.n_numaux) {
713 union internal_auxent *a = &((native+1)->u.auxent);
714
715 a->x_sym.x_fcnary.x_fcn.x_lnnoptr =
716 symbol->symbol.section->output_section->moving_line_filepos;
717 }
718 /*
719 And count and relocate all other linenumbers
720 */
721
722 count++;
723 while (lineno[count].line_number) {
724 #if 0
725 /* 13 april 92. sac
726 I've been told this, but still need proof:
727 > The second bug is also in `bfd/coffcode.h'. This bug causes the linker to screw
728 > up the pc-relocations for all the line numbers in COFF code. This bug isn't
729 > only specific to A29K implementations, but affects all systems using COFF
730 > format binaries. Note that in COFF object files, the line number core offsets
731 > output by the assembler are relative to the start of each procedure, not
732 > to the start of the .text section. This patch relocates the line numbers
733 > relative to the `native->u.syment.n_value' instead of the section virtual
734 > address. modular!olson@cs.arizona.edu (Jon Olson)
735 */
736 lineno[count].u.offset += native->u.syment.n_value;
737
738 #else
739 lineno[count].u.offset +=
740 symbol->symbol.section->output_section->vma +
741 symbol->symbol.section->output_offset;
742 #endif
743 count++;
744 }
745 symbol->done_lineno = true;
746
747 symbol->symbol.section->output_section->moving_line_filepos +=
748 count * bfd_coff_linesz (abfd);
749 }
750 return coff_write_symbol(abfd, &( symbol->symbol), native,written);
751 }
752
753 void
754 DEFUN(coff_write_symbols,(abfd),
755 bfd *abfd)
756 {
757 unsigned int i;
758 unsigned int limit = bfd_get_symcount(abfd);
759 unsigned int written = 0;
760
761 asymbol **p;
762
763 string_size = 0;
764
765
766 /* Seek to the right place */
767 bfd_seek(abfd, obj_sym_filepos(abfd), SEEK_SET);
768
769 /* Output all the symbols we have */
770
771 written = 0;
772 for (p = abfd->outsymbols, i = 0; i < limit; i++, p++)
773 {
774 asymbol *symbol = *p;
775 coff_symbol_type *c_symbol = coff_symbol_from(abfd, symbol);
776
777 if (c_symbol == (coff_symbol_type *) NULL ||
778 c_symbol->native == (combined_entry_type *)NULL)
779 {
780 written = coff_write_alien_symbol(abfd, symbol, written);
781 }
782 else
783 {
784 written = coff_write_native_symbol(abfd, c_symbol, written);
785 }
786
787 }
788
789 bfd_get_symcount(abfd) = written;
790
791 /* Now write out strings */
792
793 if (string_size != 0)
794 {
795 unsigned int size = string_size + 4;
796 bfd_byte buffer[4];
797
798 bfd_h_put_32(abfd, size, buffer);
799 bfd_write((PTR) buffer, 1, sizeof(buffer), abfd);
800 for (p = abfd->outsymbols, i = 0;
801 i < limit;
802 i++, p++)
803 {
804 asymbol *q = *p;
805 size_t name_length = strlen(q->name);
806 int maxlen;
807 coff_symbol_type* c_symbol = coff_symbol_from(abfd, q);
808 maxlen = ((c_symbol != NULL && c_symbol->native != NULL) &&
809 (c_symbol->native->u.syment.n_sclass == C_FILE)) ?
810 FILNMLEN : SYMNMLEN;
811
812 if (name_length > maxlen) {
813 bfd_write((PTR) (q->name), 1, name_length + 1, abfd);
814 }
815 }
816 }
817 else {
818 /* We would normally not write anything here, but we'll write
819 out 4 so that any stupid coff reader which tries to read
820 the string table even when there isn't one won't croak.
821 */
822
823 uint32e_type size = 4;
824 size = size;
825 bfd_write((PTR)&size, 1, sizeof(size), abfd);
826
827 }
828 }
829
830 void
831 DEFUN(coff_write_linenumbers,(abfd),
832 bfd *abfd)
833 {
834 asection *s;
835 bfd_size_type linesz;
836 PTR buff;
837
838 linesz = bfd_coff_linesz (abfd);
839 buff = bfd_alloc (abfd, linesz);
840 for (s = abfd->sections; s != (asection *) NULL; s = s->next) {
841 if (s->lineno_count) {
842 asymbol **q = abfd->outsymbols;
843 bfd_seek(abfd, s->line_filepos, SEEK_SET);
844 /* Find all the linenumbers in this section */
845 while (*q) {
846 asymbol *p = *q;
847 alent *l =
848 BFD_SEND(bfd_asymbol_bfd(p), _get_lineno, (bfd_asymbol_bfd(p), p));
849 if (l) {
850 /* Found a linenumber entry, output */
851 struct internal_lineno out;
852 memset( (PTR)&out, 0, sizeof(out));
853 out.l_lnno = 0;
854 out.l_addr.l_symndx = l->u.offset;
855 bfd_coff_swap_lineno_out(abfd, &out, buff);
856 bfd_write(buff, 1, linesz, abfd);
857 l++;
858 while (l->line_number) {
859 out.l_lnno = l->line_number;
860 out.l_addr.l_symndx = l->u.offset;
861 bfd_coff_swap_lineno_out(abfd, &out, buff);
862 bfd_write(buff, 1, linesz, abfd);
863 l++;
864 }
865 }
866 q++;
867 }
868 }
869 }
870 bfd_release (abfd, buff);
871 }
872
873 alent *
874 DEFUN(coff_get_lineno,(ignore_abfd, symbol),
875 bfd *ignore_abfd AND
876 asymbol *symbol)
877 {
878 return coffsymbol(symbol)->lineno;
879 }
880
881 asymbol *
882 coff_section_symbol (abfd, name)
883 bfd *abfd;
884 char *name;
885 {
886 asection *sec = bfd_make_section_old_way (abfd, name);
887 asymbol *sym;
888 combined_entry_type *csym;
889
890 sym = sec->symbol;
891 if (coff_symbol_from (abfd, sym))
892 csym = coff_symbol_from (abfd, sym)->native;
893 else
894 csym = 0;
895 /* Make sure back-end COFF stuff is there. */
896 if (csym == 0)
897 {
898 struct foo {
899 coff_symbol_type sym;
900 /* @@FIXME This shouldn't use a fixed size!! */
901 combined_entry_type e[10];
902 };
903 struct foo *f;
904 f = (struct foo *) bfd_alloc_by_size_t (abfd, sizeof (*f));
905 memset ((char *) f, 0, sizeof (*f));
906 coff_symbol_from (abfd, sym)->native = csym = f->e;
907 }
908 csym[0].u.syment.n_sclass = C_STAT;
909 csym[0].u.syment.n_numaux = 1;
910 /* SF_SET_STATICS (sym); @@ ??? */
911 if (sec)
912 {
913 csym[1].u.auxent.x_scn.x_scnlen = sec->_raw_size;
914 csym[1].u.auxent.x_scn.x_nreloc = sec->reloc_count;
915 csym[1].u.auxent.x_scn.x_nlinno = sec->lineno_count;
916 }
917 else
918 {
919 csym[1].u.auxent.x_scn.x_scnlen = 0;
920 csym[1].u.auxent.x_scn.x_nreloc = 0;
921 csym[1].u.auxent.x_scn.x_nlinno = 0;
922 }
923 return sym;
924 }
925
926 /* This function transforms the offsets into the symbol table into
927 pointers to syments. */
928
929 static void
930 DEFUN(coff_pointerize_aux,(abfd, table_base, type, class, auxent),
931 bfd *abfd AND
932 combined_entry_type *table_base AND
933 int type AND
934 int class AND
935 combined_entry_type *auxent)
936 {
937 /* Don't bother if this is a file or a section */
938 if (class == C_STAT && type == T_NULL) return;
939 if (class == C_FILE) return;
940
941 /* Otherwise patch up */
942 #define N_TMASK coff_data (abfd)->local_n_tmask
943 #define N_BTSHFT coff_data (abfd)->local_n_btshft
944 if (ISFCN(type) || ISTAG(class) || class == C_BLOCK) {
945 auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p = table_base +
946 auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l;
947 auxent->fix_end = 1;
948 }
949 /* A negative tagndx is meaningless, but the SCO 3.2v4 cc can
950 generate one, so we must be careful to ignore it. */
951 if (auxent->u.auxent.x_sym.x_tagndx.l > 0) {
952 auxent->u.auxent.x_sym.x_tagndx.p =
953 table_base + auxent->u.auxent.x_sym.x_tagndx.l;
954 auxent->fix_tag = 1;
955 }
956 }
957
958 static char *
959 DEFUN(build_string_table,(abfd),
960 bfd *abfd)
961 {
962 char string_table_size_buffer[4];
963 unsigned int string_table_size;
964 char *string_table;
965
966 /* At this point we should be "seek"'d to the end of the
967 symbols === the symbol table size. */
968 if (bfd_read((char *) string_table_size_buffer,
969 sizeof(string_table_size_buffer),
970 1, abfd) != sizeof(string_table_size)) {
971 bfd_error = system_call_error;
972 return (NULL);
973 } /* on error */
974
975 string_table_size = bfd_h_get_32(abfd, (bfd_byte *) string_table_size_buffer);
976
977 if ((string_table = (PTR) bfd_alloc(abfd, string_table_size -= 4)) == NULL) {
978 bfd_error = no_memory;
979 return (NULL);
980 } /* on mallocation error */
981 if (bfd_read(string_table, string_table_size, 1, abfd) != string_table_size) {
982 bfd_error = system_call_error;
983 return (NULL);
984 }
985 return string_table;
986 }
987
988 /* Allocate space for the ".debug" section, and read it.
989 We did not read the debug section until now, because
990 we didn't want to go to the trouble until someone needed it. */
991
992 static char *
993 DEFUN(build_debug_section,(abfd),
994 bfd *abfd)
995 {
996 char *debug_section;
997 long position;
998
999 asection *sect = bfd_get_section_by_name (abfd, ".debug");
1000
1001 if (!sect) {
1002 bfd_error = no_debug_section;
1003 return NULL;
1004 }
1005
1006 debug_section = (PTR) bfd_alloc (abfd,
1007 bfd_get_section_size_before_reloc (sect));
1008 if (debug_section == NULL) {
1009 bfd_error = no_memory;
1010 return NULL;
1011 }
1012
1013 /* Seek to the beginning of the `.debug' section and read it.
1014 Save the current position first; it is needed by our caller.
1015 Then read debug section and reset the file pointer. */
1016
1017 position = bfd_tell (abfd);
1018 bfd_seek (abfd, sect->filepos, SEEK_SET);
1019 if (bfd_read (debug_section,
1020 bfd_get_section_size_before_reloc (sect), 1, abfd)
1021 != bfd_get_section_size_before_reloc(sect)) {
1022 bfd_error = system_call_error;
1023 return NULL;
1024 }
1025 bfd_seek (abfd, position, SEEK_SET);
1026 return debug_section;
1027 }
1028
1029
1030 /* Return a pointer to a malloc'd copy of 'name'. 'name' may not be
1031 \0-terminated, but will not exceed 'maxlen' characters. The copy *will*
1032 be \0-terminated. */
1033 static char *
1034 DEFUN(copy_name,(abfd, name, maxlen),
1035 bfd *abfd AND
1036 char *name AND
1037 int maxlen)
1038 {
1039 int len;
1040 char *newname;
1041
1042 for (len = 0; len < maxlen; ++len) {
1043 if (name[len] == '\0') {
1044 break;
1045 }
1046 }
1047
1048 if ((newname = (PTR) bfd_alloc(abfd, len+1)) == NULL) {
1049 bfd_error = no_memory;
1050 return (NULL);
1051 }
1052 strncpy(newname, name, len);
1053 newname[len] = '\0';
1054 return newname;
1055 }
1056
1057 /* Read a symbol table into freshly bfd_allocated memory, swap it, and
1058 knit the symbol names into a normalized form. By normalized here I
1059 mean that all symbols have an n_offset pointer that points to a null-
1060 terminated string. */
1061
1062 combined_entry_type *
1063 DEFUN(coff_get_normalized_symtab,(abfd),
1064 bfd *abfd)
1065 {
1066 combined_entry_type *internal;
1067 combined_entry_type *internal_ptr;
1068 combined_entry_type *symbol_ptr;
1069 combined_entry_type *internal_end;
1070 bfd_size_type symesz;
1071 PTR raw;
1072 char *raw_src;
1073 char *raw_end;
1074 char *string_table = NULL;
1075 char *debug_section = NULL;
1076 unsigned long size;
1077
1078 unsigned int raw_size;
1079 if (obj_raw_syments(abfd) != (combined_entry_type *)NULL) {
1080 return obj_raw_syments(abfd);
1081 }
1082 if ((size = bfd_get_symcount(abfd) * sizeof(combined_entry_type)) == 0) {
1083 bfd_error = no_symbols;
1084 return (NULL);
1085 }
1086
1087 internal = (combined_entry_type *)bfd_alloc(abfd, size);
1088 internal_end = internal + bfd_get_symcount(abfd);
1089
1090 symesz = bfd_coff_symesz (abfd);
1091 raw_size = bfd_get_symcount(abfd) * symesz;
1092 raw = bfd_alloc(abfd,raw_size);
1093
1094 if (bfd_seek(abfd, obj_sym_filepos(abfd), SEEK_SET) == -1
1095 || bfd_read(raw, raw_size, 1, abfd) != raw_size) {
1096 bfd_error = system_call_error;
1097 return (NULL);
1098 }
1099 /* mark the end of the symbols */
1100 raw_end = (char *) raw + bfd_get_symcount(abfd) * symesz;
1101 /*
1102 FIXME SOMEDAY. A string table size of zero is very weird, but
1103 probably possible. If one shows up, it will probably kill us.
1104 */
1105
1106 /* Swap all the raw entries */
1107 for (raw_src = (char *) raw, internal_ptr = internal;
1108 raw_src < raw_end;
1109 raw_src += symesz, internal_ptr++) {
1110
1111 unsigned int i;
1112 bfd_coff_swap_sym_in(abfd, (PTR)raw_src, (PTR)&internal_ptr->u.syment);
1113 internal_ptr->fix_tag = 0;
1114 internal_ptr->fix_end = 0;
1115 symbol_ptr = internal_ptr;
1116
1117 for (i = 0;
1118 i < symbol_ptr->u.syment.n_numaux;
1119 i++)
1120 {
1121 internal_ptr++;
1122 raw_src += symesz;
1123
1124 internal_ptr->fix_tag = 0;
1125 internal_ptr->fix_end = 0;
1126 bfd_coff_swap_aux_in(abfd, (PTR) raw_src,
1127 symbol_ptr->u.syment.n_type,
1128 symbol_ptr->u.syment.n_sclass,
1129 &(internal_ptr->u.auxent));
1130 /* Remember that bal entries arn't pointerized */
1131 if (i != 1 || symbol_ptr->u.syment.n_sclass != C_LEAFPROC)
1132 {
1133
1134 coff_pointerize_aux(abfd,
1135 internal,
1136 symbol_ptr->u.syment.n_type,
1137 symbol_ptr->u.syment.n_sclass,
1138 internal_ptr);
1139 }
1140
1141 }
1142 }
1143
1144 /* Free all the raw stuff */
1145 bfd_release(abfd, raw);
1146
1147 for (internal_ptr = internal; internal_ptr < internal_end;
1148 internal_ptr ++)
1149 {
1150 if (internal_ptr->u.syment.n_sclass == C_FILE) {
1151 /* make a file symbol point to the name in the auxent, since
1152 the text ".file" is redundant */
1153 if ((internal_ptr+1)->u.auxent.x_file.x_n.x_zeroes == 0) {
1154 /* the filename is a long one, point into the string table */
1155 if (string_table == NULL) {
1156 string_table = build_string_table(abfd);
1157 }
1158
1159 internal_ptr->u.syment._n._n_n._n_offset =
1160 (int) (string_table - 4 +
1161 (internal_ptr+1)->u.auxent.x_file.x_n.x_offset);
1162 }
1163 else {
1164 /* ordinary short filename, put into memory anyway */
1165 internal_ptr->u.syment._n._n_n._n_offset = (int)
1166 copy_name(abfd, (internal_ptr+1)->u.auxent.x_file.x_fname,
1167 FILNMLEN);
1168 }
1169 }
1170 else {
1171 if (internal_ptr->u.syment._n._n_n._n_zeroes != 0) {
1172 /* This is a "short" name. Make it long. */
1173 unsigned long i = 0;
1174 char *newstring = NULL;
1175
1176 /* find the length of this string without walking into memory
1177 that isn't ours. */
1178 for (i = 0; i < 8; ++i) {
1179 if (internal_ptr->u.syment._n._n_name[i] == '\0') {
1180 break;
1181 } /* if end of string */
1182 } /* possible lengths of this string. */
1183
1184 if ((newstring = (PTR) bfd_alloc(abfd, ++i)) == NULL) {
1185 bfd_error = no_memory;
1186 return (NULL);
1187 } /* on error */
1188 memset(newstring, 0, i);
1189 strncpy(newstring, internal_ptr->u.syment._n._n_name, i-1);
1190 internal_ptr->u.syment._n._n_n._n_offset = (int) newstring;
1191 internal_ptr->u.syment._n._n_n._n_zeroes = 0;
1192 }
1193 else if (!bfd_coff_symname_in_debug(abfd, &internal_ptr->u.syment)) {
1194 /* Long name already. Point symbol at the string in the table. */
1195 if (string_table == NULL) {
1196 string_table = build_string_table(abfd);
1197 }
1198 internal_ptr->u.syment._n._n_n._n_offset = (int)
1199 (string_table - 4 + internal_ptr->u.syment._n._n_n._n_offset);
1200 }
1201 else {
1202 /* Long name in debug section. Very similar. */
1203 if (debug_section == NULL) {
1204 debug_section = build_debug_section(abfd);
1205 }
1206 internal_ptr->u.syment._n._n_n._n_offset = (int)
1207 (debug_section + internal_ptr->u.syment._n._n_n._n_offset);
1208 }
1209 }
1210 internal_ptr += internal_ptr->u.syment.n_numaux;
1211 }
1212
1213 obj_raw_syments(abfd) = internal;
1214
1215 return (internal);
1216 } /* coff_get_normalized_symtab() */
1217
1218 unsigned int
1219 DEFUN (coff_get_reloc_upper_bound, (abfd, asect),
1220 bfd *abfd AND
1221 sec_ptr asect)
1222 {
1223 if (bfd_get_format(abfd) != bfd_object) {
1224 bfd_error = invalid_operation;
1225 return 0;
1226 }
1227 return (asect->reloc_count + 1) * sizeof(arelent *);
1228 }
1229
1230 asymbol *
1231 DEFUN (coff_make_empty_symbol, (abfd),
1232 bfd *abfd)
1233 {
1234 coff_symbol_type *new = (coff_symbol_type *) bfd_alloc(abfd, sizeof(coff_symbol_type));
1235 if (new == NULL) {
1236 bfd_error = no_memory;
1237 return (NULL);
1238 } /* on error */
1239 new->symbol.section = 0;
1240 new->native = 0;
1241 new->lineno = (alent *) NULL;
1242 new->done_lineno = false;
1243 new->symbol.the_bfd = abfd;
1244 return &new->symbol;
1245 }
1246
1247 asymbol *
1248 DEFUN (coff_make_debug_symbol, (abfd, ptr, sz),
1249 bfd *abfd AND
1250 PTR ptr AND
1251 unsigned long sz)
1252 {
1253 coff_symbol_type *new = (coff_symbol_type *) bfd_alloc(abfd, sizeof(coff_symbol_type));
1254 if (new == NULL) {
1255 bfd_error = no_memory;
1256 return (NULL);
1257 } /* on error */
1258 /* @@ This shouldn't be using a constant multiplier. */
1259 new->native = (combined_entry_type *) bfd_zalloc (abfd, sizeof (combined_entry_type) * 10);
1260 new->symbol.section = &bfd_debug_section;
1261 new->lineno = (alent *) NULL;
1262 new->done_lineno = false;
1263 new->symbol.the_bfd = abfd;
1264 return &new->symbol;
1265 }
1266
1267 void
1268 DEFUN(coff_print_symbol,(abfd, filep, symbol, how),
1269 bfd *abfd AND
1270 PTR filep AND
1271 asymbol *symbol AND
1272 bfd_print_symbol_type how)
1273 {
1274 FILE *file = (FILE *)filep;
1275 switch (how) {
1276 case bfd_print_symbol_name:
1277 fprintf(file, "%s", symbol->name);
1278 break;
1279 case bfd_print_symbol_more:
1280 fprintf(file, "coff %lx %lx", (unsigned long) coffsymbol(symbol)->native,
1281 (unsigned long) coffsymbol(symbol)->lineno);
1282 break;
1283 case bfd_print_symbol_nm:
1284
1285 {
1286 CONST char *section_name = symbol->section->name;
1287 bfd_print_symbol_vandf((PTR) file, symbol);
1288
1289
1290 fprintf(file, " %-5s %s %s %s",
1291 section_name,
1292 coffsymbol(symbol)->native ? "n" : "g",
1293 coffsymbol(symbol)->lineno ? "l" : " ",
1294 symbol->name);
1295 }
1296
1297
1298 break;
1299 case bfd_print_symbol_all:
1300 /* Print out the symbols in a reasonable way */
1301 {
1302 CONST char *section_name = symbol->section->name;
1303
1304
1305 if (coffsymbol(symbol)->native)
1306 {
1307 unsigned int aux;
1308 combined_entry_type *combined = coffsymbol(symbol)->native;
1309 combined_entry_type *root = obj_raw_syments(abfd);
1310
1311 fprintf(file,"[%3d]",
1312 combined - root);
1313
1314
1315 fprintf(file, "(sc %2d)(fl%4x)(ty%3x)(sc%3d) nx(%d) %08x %s",
1316 combined->u.syment.n_scnum,
1317 combined->u.syment.n_flags,
1318 combined->u.syment.n_type,
1319 combined->u.syment.n_sclass,
1320 combined->u.syment.n_numaux,
1321 combined->u.syment.n_value,
1322 symbol->name
1323 );
1324 for (aux = 0; aux < combined->u.syment.n_numaux; aux++)
1325 {
1326 fprintf(file,"\n");
1327 switch (combined->u.syment.n_sclass) {
1328 case C_FILE:
1329 fprintf(file, "File ");
1330 break;
1331 default:
1332 fprintf(file, "AUX lnno %x size %x tagndx %x",
1333 combined[aux+1].u.auxent.x_sym.x_misc.x_lnsz.x_lnno,
1334 combined[aux+1].u.auxent.x_sym.x_misc.x_lnsz.x_size,
1335 combined[aux+1].u.auxent.x_sym.x_tagndx.l);
1336 break;
1337
1338 }
1339
1340 }
1341
1342 {
1343 struct lineno_cache_entry *l = coffsymbol(symbol)->lineno;
1344 if (l)
1345 {
1346 printf("\n%s :", l->u.sym->name);
1347 l++;
1348 while (l->line_number)
1349 {
1350 printf("\n%4d : %x",
1351 l->line_number,
1352 l->u.offset);
1353 l++;
1354
1355 }
1356 }
1357 }
1358
1359
1360
1361 }
1362
1363 else {
1364 bfd_print_symbol_vandf((PTR) file, symbol);
1365 fprintf(file, " %-5s %s %s %s",
1366 section_name,
1367 coffsymbol(symbol)->native ? "n" : "g",
1368 coffsymbol(symbol)->lineno ? "l" : " ",
1369 symbol->name);
1370 }
1371
1372 }
1373
1374 }
1375 }
1376
1377 /* Provided a BFD, a section and an offset into the section, calculate
1378 and return the name of the source file and the line nearest to the
1379 wanted location. */
1380
1381 boolean
1382 DEFUN(coff_find_nearest_line,(abfd,
1383 section,
1384 ignore_symbols,
1385 offset,
1386 filename_ptr,
1387 functionname_ptr,
1388 line_ptr),
1389 bfd *abfd AND
1390 asection *section AND
1391 asymbol **ignore_symbols AND
1392 bfd_vma offset AND
1393 CONST char **filename_ptr AND
1394 CONST char **functionname_ptr AND
1395 unsigned int *line_ptr)
1396 {
1397 static bfd *cache_abfd;
1398 static asection *cache_section;
1399 static bfd_vma cache_offset;
1400 static unsigned int cache_i;
1401 static alent *cache_l;
1402
1403 unsigned int i = 0;
1404 coff_data_type *cof = coff_data(abfd);
1405 /* Run through the raw syments if available */
1406 combined_entry_type *p;
1407 alent *l;
1408 unsigned int line_base = 0;
1409
1410
1411 *filename_ptr = 0;
1412 *functionname_ptr = 0;
1413 *line_ptr = 0;
1414
1415 /* Don't try and find line numbers in a non coff file */
1416 if (abfd->xvec->flavour != bfd_target_coff_flavour)
1417 return false;
1418
1419 if (cof == NULL)
1420 return false;
1421
1422 p = cof->raw_syments;
1423
1424 for (i = 0; i < cof->raw_syment_count; i++) {
1425 if (p->u.syment.n_sclass == C_FILE) {
1426 /* File name has been moved into symbol */
1427 *filename_ptr = (char *) p->u.syment._n._n_n._n_offset;
1428 break;
1429 }
1430 p += 1 + p->u.syment.n_numaux;
1431 }
1432 /* Now wander though the raw linenumbers of the section */
1433 /*
1434 If this is the same BFD as we were previously called with and this is
1435 the same section, and the offset we want is further down then we can
1436 prime the lookup loop
1437 */
1438 if (abfd == cache_abfd &&
1439 section == cache_section &&
1440 offset >= cache_offset) {
1441 i = cache_i;
1442 l = cache_l;
1443 }
1444 else {
1445 i = 0;
1446 l = section->lineno;
1447 }
1448
1449 for (; i < section->lineno_count; i++) {
1450 if (l->line_number == 0) {
1451 /* Get the symbol this line number points at */
1452 coff_symbol_type *coff = (coff_symbol_type *) (l->u.sym);
1453 *functionname_ptr = coff->symbol.name;
1454 if (coff->native) {
1455 combined_entry_type *s = coff->native;
1456 s = s + 1 + s->u.syment.n_numaux;
1457 /*
1458 S should now point to the .bf of the function
1459 */
1460 if (s->u.syment.n_numaux) {
1461 /*
1462 The linenumber is stored in the auxent
1463 */
1464 union internal_auxent *a = &((s + 1)->u.auxent);
1465 line_base = a->x_sym.x_misc.x_lnsz.x_lnno;
1466 }
1467 }
1468 }
1469 else {
1470 if (l->u.offset > offset)
1471 break;
1472 *line_ptr = l->line_number + line_base + 1;
1473 }
1474 l++;
1475 }
1476
1477 cache_abfd = abfd;
1478 cache_section = section;
1479 cache_offset = offset;
1480 cache_i = i;
1481 cache_l = l;
1482
1483 return true;
1484 }
1485
1486 int
1487 DEFUN(coff_sizeof_headers,(abfd, reloc),
1488 bfd *abfd AND
1489 boolean reloc)
1490 {
1491 size_t size;
1492
1493 if (reloc == false) {
1494 size = bfd_coff_filhsz (abfd) + bfd_coff_aoutsz (abfd);
1495 }
1496 else {
1497 size = bfd_coff_filhsz (abfd);
1498 }
1499
1500 size += abfd->section_count * bfd_coff_scnhsz (abfd);
1501 return size;
1502 }