1 /* Support for Intel 960 COFF and Motorola 88k BCS COFF (and maybe others) */
3 /* Copyright (C) 1990, 1991 Free Software Foundation, Inc.
5 This file is part of BFD, the Binary File Diddler.
7 BFD is free software; you can redistribute it and/or modify it under the
8 terms of the GNU General Public License as published by the Free Software
9 Foundation; either version 1, or (at your option) any later version.
11 BFD is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
16 You should have received a copy of the GNU General Public License along with
17 BFD; see the file COPYING. If not, write to the Free Software Foundation,
18 675 Mass Ave, Cambridge, MA 02139, USA.
22 /* Most of this hacked by Steve Chamberlain, steve@cygnus.com */
24 #include "archures.h" /* Machine architectures and types */
31 /* Align an address upward to a boundary, expressed as a number of bytes.
32 E.g. align to an 8-byte boundary with argument of 8. */
33 #define ALIGN(this, boundary) \
34 ((( (this) + ((boundary) -1)) & (~((boundary)-1))))
36 /* Align an address upward to a power of two. Argument is the power
37 of two, e.g. 8-byte alignment uses argument of 3 (8 == 2^3). */
38 #define i960_align(addr, align) \
39 ( ((addr) + ((1<<(align))-1)) & (-1 << (align)))
41 #define sp(x) bfd_h_put_x(abfd, x, &x)
43 PROTO(static void,force_indices_file_symbol_relative
,(bfd
*abfd
, SYMENT
*symtab
));
45 /* All the swapping routines:
47 FIXME, these routines assume that the sizes, alignments, and offsets of
48 these fields are the same in the host and target. This is an invalid
49 assumption, which in particular breaks on the 386 and SPARC. Fix this
50 the same way that a.out.h and sunos.c were fixed: define char arrays
51 that represent the headers in the target systems' file, and have these
52 functions translate in from that format, and out to that format. */
55 DEFUN(swap_reloc
,(abfd
, reloc
),
66 DEFUN(swap_filehdr
,(abfd
, filehdr
),
72 sp(filehdr
->f_timdat
);
73 sp(filehdr
->f_symptr
);
75 sp(filehdr
->f_opthdr
);
82 DEFUN(swap_aouthdr
,(abfd
, aouthdr
),
93 sp(aouthdr
->text_start
);
94 sp(aouthdr
->data_start
);
96 sp(aouthdr
->tagentries
);
101 DEFUN(swap_scnhdr
,(abfd
, scnhdr
),
108 sp(scnhdr
->s_scnptr
);
109 sp(scnhdr
->s_relptr
);
110 sp(scnhdr
->s_lnnoptr
);
111 sp(scnhdr
->s_nreloc
);
119 /* void warning(); */
120 extern asection abs_section
;
123 DEFUN(get_index
,(symbol
),
126 return (int) symbol
->value
;
130 DEFUN(set_index
,(symbol
, idx
),
138 initialize a section structure with information peculiar to this
139 particular implementation of coff
143 DEFUN(coff_new_section_hook
,(abfd_ignore
, section_ignore
),
145 asection
*section_ignore
)
148 /* FIXME, shouldn't this ifdef be on something that says we are
149 actually COMPILING FOR an 88K coff file, rather than simply
150 knowing its magic number? */
151 /* Align to at least 16 bytes */
152 section_ignore
->alignment_power
= 4;
157 /* Take a section header read from a coff file (in HOST byte order),
158 and make a BFD "section" out of it. */
160 DEFUN(make_a_section_from_file
,(abfd
, hdr
),
165 asection
*return_section
;
168 /* Assorted wastage to null-terminate the name, thanks AT&T! */
169 char *name
= bfd_alloc(abfd
, sizeof (hdr
->s_name
)+1);
171 bfd_error
= no_memory
;
174 strncpy(name
, (char *) &hdr
->s_name
[0], sizeof (hdr
->s_name
));
175 name
[sizeof (hdr
->s_name
)] = 0;
177 return_section
= bfd_make_section(abfd
, name
);
180 /* s_paddr is presumed to be = to s_vaddr */
181 #define assign(to, from) return_section->to = hdr->from
182 assign(vma
, s_vaddr
);
183 /* assign (vma, s_vaddr); */
184 assign(size
, s_size
);
185 assign(filepos
, s_scnptr
);
186 assign(rel_filepos
, s_relptr
);
187 assign(reloc_count
, s_nreloc
);
190 /* FIXME, use a temp var rather than alignment_power */
191 assign(alignment_power
, s_align
);
194 for (i
= 0; i
< 32; i
++) {
195 if ((1 << i
) >= (int) (return_section
->alignment_power
)) {
196 return_section
->alignment_power
= i
;
203 assign(line_filepos
, s_lnnoptr
);
205 return_section->linesize = hdr->s_nlnno * sizeof (struct lineno);
209 return_section
->lineno_count
= hdr
->s_nlnno
;
210 return_section
->userdata
= NULL
;
211 return_section
->next
= (asection
*) NULL
;
212 if ((hdr
->s_flags
& STYP_TEXT
) || (hdr
->s_flags
& STYP_DATA
))
213 return_section
->flags
= (SEC_LOAD
| SEC_ALLOC
);
214 else if (hdr
->s_flags
& STYP_BSS
)
215 return_section
->flags
= SEC_ALLOC
;
217 if (hdr
->s_nreloc
!= 0)
218 return_section
->flags
|= SEC_RELOC
;
219 if (hdr
->s_scnptr
!= 0)
220 return_section
->flags
|= SEC_HAS_CONTENTS
;
225 DEFUN(coff_mkobject
,(abfd
),
228 set_tdata (abfd
, bfd_alloc (abfd
,sizeof(coff_data_type
)));
229 if (coff_data(abfd
) == 0) {
230 bfd_error
= no_memory
;
233 coff_data(abfd
)->relocbase
= 0;
239 DEFUN(coff_real_object_p
,(abfd
, nscns
, opthdr
),
244 coff_data_type
*coff
;
245 char *file_info
; /* buffer for all the headers */
246 size_t readsize
; /* length of file_info */
247 struct filehdr
*filehdr
; /* points into file_info */
248 struct scnhdr
*sections
; /* points into file_info */
250 /* Build a play area */
251 if (coff_mkobject(abfd
) != true)
253 coff
= coff_data(abfd
);
255 /* OK, now we know the format, read in the filehdr, soi-disant "optional
256 header", and all the sections. */
258 readsize
= sizeof(struct filehdr
)
260 + (nscns
* sizeof(struct scnhdr
));
262 file_info
= (PTR
) bfd_alloc(abfd
, readsize
);
263 if (file_info
== NULL
) {
264 bfd_error
= no_memory
;
268 if (bfd_seek(abfd
, 0L, SEEK_SET
) < 0) {
269 bfd_release (abfd
, (PTR
)file_info
);
272 if (bfd_read((PTR
) file_info
, 1, readsize
, abfd
) != readsize
) {
273 bfd_release (abfd
, (PTR
)file_info
);
276 filehdr
= (struct filehdr
*) file_info
;
277 sections
= (struct scnhdr
*) (file_info
+ sizeof(struct filehdr
) + opthdr
);
279 swap_filehdr(abfd
, filehdr
);
281 /* Now copy data as required; construct all asections etc */
282 coff
->symbol_index_slew
= 0;
284 coff
->raw_syment_count
= 0;
285 coff
->raw_linenos
= 0;
286 coff
->raw_syments
= 0;
287 coff
->sym_filepos
=0;
288 coff
->flags
= filehdr
->f_flags
;
291 for (i
= 0; i
< nscns
; i
++) {
292 swap_scnhdr(abfd
, sections
+ i
);
293 make_a_section_from_file(abfd
, sections
+ i
);
296 /* Determine the machine architecture and type. */
297 abfd
->obj_machine
= 0;
298 switch (filehdr
->f_magic
) {
302 abfd
->obj_arch
= bfd_arch_m68k
;
303 abfd
->obj_machine
= 68020;
310 abfd
->obj_arch
= bfd_arch_m88k
;
311 abfd
->obj_machine
= 88100;
317 abfd
->obj_arch
= bfd_arch_i960
;
318 switch (F_I960TYPE
& filehdr
->f_flags
)
322 abfd
->obj_machine
= bfd_mach_i960_core
;
325 abfd
->obj_machine
= bfd_mach_i960_kb_sb
;
328 abfd
->obj_machine
= bfd_mach_i960_mc
;
331 abfd
->obj_machine
= bfd_mach_i960_xa
;
334 abfd
->obj_machine
= bfd_mach_i960_ca
;
337 abfd
->obj_machine
= bfd_mach_i960_ka_sa
;
344 default: /* Unreadable input file type */
345 abfd
->obj_arch
= bfd_arch_obscure
;
349 if (!(filehdr
->f_flags
& F_RELFLG
))
350 abfd
->flags
|= HAS_RELOC
;
351 if ((filehdr
->f_flags
& F_EXEC
))
352 abfd
->flags
|= EXEC_P
;
353 if (!(filehdr
->f_flags
& F_LNNO
))
354 abfd
->flags
|= HAS_LINENO
;
355 if (!(filehdr
->f_flags
& F_LSYMS
))
356 abfd
->flags
|= HAS_LOCALS
;
359 bfd_get_symcount(abfd
) = filehdr
->f_nsyms
;
360 if (filehdr
->f_nsyms
)
361 abfd
->flags
|= HAS_SYMS
;
363 coff
->sym_filepos
= filehdr
->f_symptr
;
364 swap_aouthdr(abfd
, &coff
->hdr
);
366 coff
->symbols
= (coff_symbol_type
*) NULL
;
367 bfd_get_start_address(abfd
) = opthdr
? exec_hdr(abfd
).entry
: 0;
371 bfd_release(abfd
, coff
);
372 return (bfd_target
*)NULL
;
377 Takes a bfd and a symbol, returns a pointer to the coff specific area
378 of the symbol if there is one.
380 static coff_symbol_type
*
381 DEFUN(coff_symbol_from
,(abfd
, symbol
),
385 if (symbol
->the_bfd
->xvec
->flavour
!= bfd_target_coff_flavour_enum
)
386 return (coff_symbol_type
*)NULL
;
388 if (symbol
->the_bfd
->tdata
== (PTR
)NULL
)
389 return (coff_symbol_type
*)NULL
;
391 return (coff_symbol_type
*) symbol
;
397 DEFUN(coff_object_p
,(abfd
),
400 unsigned short magic
,
403 bfd_error
= system_call_error
;
405 /* figure out how much to read */
406 if (bfd_read((PTR
) &magic
, 1, sizeof(magic
), abfd
) != sizeof(magic
))
409 magic
= bfd_h_getshort(abfd
, (bfd_byte
*) (&magic
));
411 if (BADMAG(*((struct filehdr
*) & magic
))) {
412 bfd_error
= wrong_format
;
415 if (bfd_read((PTR
) &nscns
, 1, sizeof(nscns
), abfd
) != sizeof(nscns
))
417 nscns
= bfd_h_getshort(abfd
, (unsigned char *) &nscns
);
418 if (bfd_seek(abfd
, (file_ptr
) & (((struct filehdr
*) NULL
)->f_opthdr
), SEEK_SET
)
421 if (bfd_read((PTR
) &opthdr
, 1, sizeof(opthdr
), abfd
) != sizeof(opthdr
))
423 opthdr
= bfd_h_getshort(abfd
, (unsigned char *) &opthdr
);
425 /* if the optional header is NULL or not the correct size then
426 quit; the only difference I can see between m88k dgux headers (MC88DMAGIC)
427 and Intel 960 readwrite headers (I960WRMAGIC) is that the
428 optional header is of a different size
433 return (bfd_target
*)NULL
;
435 return coff_real_object_p(abfd
, nscns
, opthdr
);
442 DEFUN(coff_count_linenumbers
,(abfd
),
445 unsigned int limit
= bfd_get_symcount(abfd
);
449 asection
*s
= abfd
->sections
->output_section
;
451 BFD_ASSERT(s
->lineno_count
== 0);
457 for (p
= abfd
->outsymbols
, i
= 0; i
< limit
; i
++, p
++) {
458 asymbol
*q_maybe
= *p
;
459 if (q_maybe
->the_bfd
->xvec
->flavour
== bfd_target_coff_flavour_enum
) {
460 coff_symbol_type
*q
= coffsymbol(q_maybe
);
463 This symbol has a linenumber, increment the owning
464 section's linenumber count
466 alent
*l
= q
->lineno
;
467 q
->symbol
.section
->output_section
->lineno_count
++;
469 while (l
->line_number
) {
470 q
->symbol
.section
->output_section
->lineno_count
++;
479 This function returns true if the supplied SYMENT has an AUXENT with
480 a tagndx field which should be relocated.
482 The coff book says that all auxents have this and should be moved,
483 but all the actual implementations I've looked at do this ..
488 DEFUN(uses_x_sym_x_tagndx_p
,(native
),
491 if (BTYPE(native
->n_type
) == T_STRUCT
) return true;
492 if (BTYPE(native
->n_type
) == T_UNION
) return true;
493 if (BTYPE(native
->n_type
) == T_ENUM
) return true;
499 This procedure runs through the native entries in a coff symbol table
500 and links up all the elements which should point to one another, in
501 particular these are:
503 strtag, entag and untags have an auxent endindex which points to the
504 first syment after the .eos. This is simple to do, we just keep a
505 pointer to the symbol with the most recent pending strtag and patch it
506 when we see the eos. This works since coff structs are never nested.
508 ISFCN type entries have an endindex which points to the next static or
509 extern in the table, thereby skipping the function contents.
510 The coff book says that an ISFCN's tagindex
511 points to the first .bf for the function, so far I havn't seen it
512 used. We do this using the same mechanism as strtags.
514 Each file entry has a value which points to the next file entry,
515 the last file entry points to the first extern symbol in the table
516 which is not an ISFCN.
518 Each .bb entry points to the matching .eb entry, but these are nested
519 so we keep a stack of them.
521 The tagndx of .eos items points to the strtag attached to them, this
522 is simply the last_tagndx again.
524 The tagndx of items with type strtag point to the defining struct.
525 This bit is complicated; We know that a struct ref and def must be
526 within the same file, so all the natives will be in the same vector.
527 This means that we can subtracts two pointers and get the index
528 differences between to items, used to work out the true index of the
531 We store in the name field of each syment the actual native index
532 applied so we can dig it out through a pointer. */
535 coff_mangle_symbols(bfd_ptr
)
538 unsigned int symbol_count
= bfd_get_symcount(bfd_ptr
);
539 asymbol
**symbol_ptr_ptr
= bfd_ptr
->outsymbols
;
540 SYMENT
*last_tagndx
= (SYMENT
*)NULL
;
541 SYMENT
*last_file
= (SYMENT
*)NULL
;
542 SYMENT
*last_fcn
= (SYMENT
*)NULL
;
543 SYMENT
*block_stack
[50];
544 SYMENT
**last_block
= &block_stack
[0];
545 boolean first_time
= true;
546 unsigned int symbol_index
;
547 unsigned int native_index
= 0;
549 for (symbol_index
= 0; symbol_index
< symbol_count
; symbol_index
++) {
550 coff_symbol_type
*coff_symbol_ptr
=
551 coff_symbol_from(bfd_ptr
, symbol_ptr_ptr
[symbol_index
]);
552 if (coff_symbol_ptr
== (coff_symbol_type
*)NULL
) {
554 This symbol has no coff information in it, it will take up
555 only one slot in the output symbol table
560 SYMENT
*syment
= coff_symbol_ptr
->native
;
561 if (syment
== (SYMENT
*)NULL
) {
565 /* Normalize the symbol flags */
566 if (coff_symbol_ptr
->symbol
.flags
& BSF_FORT_COMM
) {
567 /* a common symbol is undefined with a value */
568 syment
->n_scnum
= N_UNDEF
;
569 syment
->n_value
= coff_symbol_ptr
->symbol
.value
;
571 else if (coff_symbol_ptr
->symbol
.flags
& BSF_DEBUGGING
) {
572 syment
->n_value
= coff_symbol_ptr
->symbol
.value
;
574 else if (coff_symbol_ptr
->symbol
.flags
& BSF_UNDEFINED
) {
575 syment
->n_scnum
= N_UNDEF
;
578 else if (coff_symbol_ptr
->symbol
.flags
& BSF_ABSOLUTE
) {
579 syment
->n_scnum
= N_ABS
;
580 syment
->n_value
= coff_symbol_ptr
->symbol
.value
;
584 coff_symbol_ptr
->symbol
.section
->output_section
->index
+1;
586 coff_symbol_ptr
->symbol
.value
+
587 coff_symbol_ptr
->symbol
.section
->output_offset
+
588 coff_symbol_ptr
->symbol
.section
->output_section
->vma
;
592 /* If this symbol ties up something then do it */
594 if (syment
->n_sclass
== C_FILE
&& last_file
!= (SYMENT
*)NULL
)
596 last_file
->n_value
= native_index
;
598 else if ((syment
->n_sclass
== C_EXT
599 || syment
->n_sclass
== C_STAT
601 || syment
->n_sclass
== C_LEAFEXT
602 || syment
->n_sclass
== C_LEAFSTAT
605 && last_fcn
!= (SYMENT
*)NULL
)
607 AUXENT
*auxent
= (AUXENT
*)(last_fcn
+1);
608 auxent
->x_sym
.x_fcnary
.x_fcn
.x_endndx
= native_index
;
609 last_fcn
= (SYMENT
*)NULL
;
612 else if (syment
->n_sclass
== C_EOS
&& last_tagndx
!= (SYMENT
*)NULL
)
614 AUXENT
*auxent
= (AUXENT
*)(last_tagndx
+1);
615 /* Remember that we keep the native index in the offset
616 so patch the beginning of the struct to point to this
618 auxent
->x_sym
.x_tagndx
= last_tagndx
->n_offset
;
619 auxent
->x_sym
.x_fcnary
.x_fcn
.x_endndx
=
620 native_index
+ syment
->n_numaux
+ 1 ;
621 /* Now point the eos to the structure */
622 auxent
= (AUXENT
*)(syment
+1);
623 auxent
->x_sym
.x_tagndx
= last_tagndx
->n_offset
;
627 else if (syment
->n_sclass
== C_BLOCK
628 && coff_symbol_ptr
->symbol
.name
[1] == 'e')
630 AUXENT
*auxent
= (AUXENT
*)((*(--last_block
))+1);
631 auxent
->x_sym
.x_fcnary
.x_fcn
.x_endndx
=
632 native_index
+ syment
->n_numaux
+ 1;
634 if (syment
->n_sclass
== C_EXT
635 && !ISFCN(syment
->n_type
)
636 && first_time
== true
637 && last_file
!= (SYMENT
*)NULL
) {
638 /* This is the first external symbol seen which isn't a
639 function place it in the last .file entry */
640 last_file
->n_value
= native_index
;
644 if (syment
->n_sclass
== C_LEAFPROC
&& syment
->n_numaux
== 2) {
645 AUXENT
*auxent
= (AUXENT
*)(syment
+2);
646 /* This is the definition of a leaf proc, we'll relocate the
649 auxent
->x_bal
.x_balntry
+=
650 coff_symbol_ptr
->symbol
.section
->output_offset
+
651 coff_symbol_ptr
->symbol
.section
->output_section
->vma
;
654 /* If this symbol needs to be tied up then remember some facts */
655 if (syment
->n_sclass
== C_FILE
)
659 if (syment
->n_numaux
!= 0) {
661 If this symbol would like to point to something in the
662 future then remember where it is
664 if (uses_x_sym_x_tagndx_p(syment
)) {
666 If this is a ref to a structure then we'll tie it up
667 now - there are never any forward refs for one
669 if (syment
->n_sclass
== C_STRTAG
||
670 syment
->n_sclass
== C_ENTAG
||
671 syment
->n_sclass
== C_UNTAG
) {
672 last_tagndx
= syment
;
676 This is a ref to a structure - the structure must
677 have been defined within the same file, and previous
678 to this point, so we can deduce the new tagndx
681 AUXENT
*auxent
= (AUXENT
*)(syment
+1);
682 bfd
*bfd_ptr
= coff_symbol_ptr
->symbol
.the_bfd
;
683 SYMENT
*base
= obj_raw_syments(bfd_ptr
);
684 auxent
->x_sym
.x_tagndx
= base
[auxent
->x_sym
.x_tagndx
].n_offset
;
688 if (ISFCN(syment
->n_type
)) {
691 if (syment
->n_sclass
== C_BLOCK
692 && coff_symbol_ptr
->symbol
.name
[1] == 'b')
694 *last_block
++ = syment
;
697 syment
->n_offset
= native_index
;
698 native_index
= native_index
+ 1 + syment
->n_numaux
;
706 coff_write_symbols(abfd
)
710 unsigned int limit
= bfd_get_symcount(abfd
);
711 unsigned int written
= 0;
714 unsigned int string_size
= 0;
717 /* Seek to the right place */
718 bfd_seek(abfd
, obj_sym_filepos(abfd
), SEEK_SET
);
720 /* Output all the symbols we have */
723 for (p
= abfd
->outsymbols
, i
= 0; i
< limit
; i
++, p
++) {
724 asymbol
*symbol
= *p
;
725 coff_symbol_type
*c_symbol
= coff_symbol_from(abfd
, symbol
);
729 if (c_symbol
== (coff_symbol_type
*) NULL
||
730 c_symbol
->native
== (SYMENT
*) NULL
) {
732 This symbol has been created by the loader, or come from a non
733 coff format. It has no native element to inherit, make our
738 native
->n_type
= T_NULL
;
742 if (symbol
->flags
& BSF_ABSOLUTE
) {
743 native
->n_scnum
= N_ABS
;
744 native
->n_value
= symbol
->value
;
746 else if (symbol
->flags
& (BSF_UNDEFINED
| BSF_FORT_COMM
)) {
747 native
->n_scnum
= N_UNDEF
;
748 native
->n_value
= symbol
->value
;
750 else if (symbol
->flags
& BSF_DEBUGGING
) {
752 remove name so it doesn't take up any space
755 #if 0 /* FIXME -- Steve hasn't decided what to do
758 Don't do anything with debugs from the loader
760 native
->n_scnum
= N_DEBUG
;
765 native
->n_scnum
= symbol
->section
->output_section
->index
+ 1;
766 native
->n_value
= symbol
->value
+
767 symbol
->section
->output_section
->vma
+
768 symbol
->section
->output_offset
;
770 /* Copy the any flags from the the file hdr into the symbol */
772 coff_symbol_type
*c
= coff_symbol_from(abfd
, symbol
);
773 if (c
!= (coff_symbol_type
*)NULL
) {
774 native
->n_flags
= c
->symbol
.the_bfd
->flags
;
791 native
->n_sclass
= C_EXT
;
792 native
->n_numaux
= 0;
796 Does this symbol have an ascociated line number - if so then
797 make it remember this symbol index. Also tag the auxent of
798 this symbol to point to the right place in the lineno table
801 alent
*lineno
= c_symbol
->lineno
;
802 native
= c_symbol
->native
;
804 unsigned int count
= 0;
805 lineno
[count
].u
.offset
= written
;
806 if (native
->n_numaux
) {
807 union auxent
*a
= (union auxent
*) (native
+ 1);
808 a
->x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
=
809 c_symbol
->symbol
.section
->output_section
->moving_line_filepos
;
812 And count and relocate all other linenumbers
815 while (lineno
[count
].line_number
) {
816 lineno
[count
].u
.offset
+=
817 c_symbol
->symbol
.section
->output_section
->vma
+
818 c_symbol
->symbol
.section
->output_offset
;
821 c_symbol
->symbol
.section
->output_section
->moving_line_filepos
+=
822 count
* sizeof(struct lineno
);
825 } /* if symbol new to coff */
827 /* Fix the symbol names */
829 unsigned int name_length
;
830 if (symbol
->name
== (char *) NULL
) {
832 coff symbols always have names, so we'll make one up
834 symbol
->name
= "strange";
836 name_length
= strlen(symbol
->name
);
837 if (name_length
<= SYMNMLEN
) {
838 /* This name will fit into the symbol neatly */
839 strncpy(native
->n_name
, symbol
->name
, SYMNMLEN
);
842 native
->n_offset
= string_size
+ 4;
843 native
->n_zeroes
= 0;
844 string_size
+= name_length
+ 1;
847 unsigned int numaux
= native
->n_numaux
;
848 int type
= native
->n_type
;
849 int class = native
->n_sclass
;
850 bfd_coff_swap_sym(abfd
, native
);
851 bfd_write((PTR
) native
, 1, SYMESZ
, abfd
);
852 for (j
= 0; j
!= native
->n_numaux
; j
++) {
853 bfd_coff_swap_aux(abfd
, (AUXENT
*)(native
+ j
+ 1), type
, class);
854 bfd_write((PTR
) (native
+ j
+ 1), 1, AUXESZ
, abfd
);
858 Reuse somewhere in the symbol to keep the index
860 set_index(symbol
, written
);
861 written
+= 1 + numaux
;
864 } /* for each out symbol */
866 bfd_get_symcount(abfd
) = written
;
867 /* Now write out strings */
870 unsigned int size
= string_size
+ 4;
871 bfd_h_put_x(abfd
, size
, &size
);
872 bfd_write((PTR
) &size
, 1, sizeof(size
), abfd
);
873 for (p
= abfd
->outsymbols
, i
= 0; i
< limit
; i
++, p
++) {
875 size_t name_length
= strlen(q
->name
);
876 if (name_length
> SYMNMLEN
) {
877 bfd_write((PTR
) (q
->name
), 1, name_length
+ 1, abfd
);
882 /* We would normally not write anything here, but we'll write
883 out 4 so that any stupid coff reader which tries to read
884 the string table even when there isn't one won't croak.
887 uint32e_type size
= 4;
888 bfd_h_put_x(abfd
, size
, &size
);
889 bfd_write((PTR
)&size
, 1, sizeof(size
), abfd
);
896 coff_write_relocs(abfd
)
900 for (s
= abfd
->sections
; s
!= (asection
*) NULL
; s
= s
->next
) {
902 arelent
**p
= s
->orelocation
;
903 bfd_seek(abfd
, s
->rel_filepos
, SEEK_SET
);
904 for (i
= 0; i
< s
->reloc_count
; i
++) {
907 memset((PTR
)&n
, 0, sizeof(n
));
908 n
.r_vaddr
= q
->address
+ s
->vma
;
909 if (q
->sym_ptr_ptr
) {
910 n
.r_symndx
= get_index((*(q
->sym_ptr_ptr
)));
912 n
.r_type
= q
->howto
->type
;
913 swap_reloc(abfd
, &n
);
914 bfd_write((PTR
) &n
, 1, RELSZ
, abfd
);
920 coff_write_linenumbers(abfd
)
924 for (s
= abfd
->sections
; s
!= (asection
*) NULL
; s
= s
->next
) {
925 if (s
->lineno_count
) {
926 asymbol
**q
= abfd
->outsymbols
;
927 bfd_seek(abfd
, s
->line_filepos
, SEEK_SET
);
928 /* Find all the linenumbers in this section */
931 alent
*l
= BFD_SEND(p
->the_bfd
, _get_lineno
, (p
->the_bfd
, p
));
933 /* Found a linenumber entry, output */
935 bzero( (PTR
)&out
, sizeof(out
));
937 out
.l_addr
.l_symndx
= l
->u
.offset
;
938 bfd_coff_swap_lineno(abfd
, &out
);
939 bfd_write((PTR
) &out
, 1, LINESZ
, abfd
);
941 while (l
->line_number
) {
942 out
.l_lnno
= l
->line_number
;
943 out
.l_addr
.l_symndx
= l
->u
.offset
;
944 bfd_coff_swap_lineno(abfd
, &out
);
945 bfd_write((PTR
) &out
, 1, LINESZ
, abfd
);
957 coff_make_empty_symbol(abfd
)
960 coff_symbol_type
*new = (coff_symbol_type
*) bfd_alloc(abfd
, sizeof(coff_symbol_type
));
962 bfd_error
= no_memory
;
966 new->lineno
= (alent
*) NULL
;
967 new->symbol
.the_bfd
= abfd
;
972 coff_print_symbol(ignore_abfd
, file
, symbol
, how
)
976 bfd_print_symbol_enum_type how
;
979 case bfd_print_symbol_name_enum
:
980 fprintf(file
, "%s", symbol
->name
);
982 case bfd_print_symbol_type_enum
:
983 fprintf(file
, "coff %lx %lx", (unsigned long) coffsymbol(symbol
)->native
,
984 (unsigned long) coffsymbol(symbol
)->lineno
);
986 case bfd_print_symbol_all_enum
:
988 CONST
char *section_name
= symbol
->section
== (asection
*) NULL
?
989 "*abs" : symbol
->section
->name
;
990 bfd_print_symbol_vandf((PTR
) file
, symbol
);
992 fprintf(file
, " %-5s %s %s %s",
994 coffsymbol(symbol
)->native
? "n" : "g",
995 coffsymbol(symbol
)->lineno
? "l" : " ",
1005 coff_get_lineno(ignore_abfd
, symbol
)
1009 return coffsymbol(symbol
)->lineno
;
1013 Set flags and magic number of a coff file from architecture and machine
1014 type. Result is true if we can represent the arch&type, false if not.
1017 coff_set_flags(abfd
, magicp
, flagsp
)
1023 switch (abfd
->obj_arch
) {
1031 *magicp
= I960ROMAGIC
;
1033 ((bfd_get_file_flags(abfd) & WP_TEXT) ? I960ROMAGIC :
1034 I960RWMAGIC); FIXME???
1036 switch (abfd
->obj_machine
) {
1037 case bfd_mach_i960_core
:
1040 case bfd_mach_i960_kb_sb
:
1043 case bfd_mach_i960_mc
:
1046 case bfd_mach_i960_xa
:
1049 case bfd_mach_i960_ca
:
1052 case bfd_mach_i960_ka_sa
:
1066 *magicp
= MC68MAGIC
;
1072 *magicp
= MC88OMAGIC
;
1077 default: /* Unknown architecture */
1086 coff_set_arch_mach(abfd
, arch
, machine
)
1088 enum bfd_architecture arch
;
1089 unsigned long machine
;
1093 abfd
->obj_arch
= arch
;
1094 abfd
->obj_machine
= machine
;
1095 if (arch
!= bfd_arch_unknown
&&
1096 coff_set_flags(abfd
, &dummy1
, &dummy2
) != true)
1097 return false; /* We can't represent this type */
1098 return true; /* We're easy ... */
1102 /* Calculate the file position for each section. */
1105 coff_compute_section_file_positions(abfd
)
1109 file_ptr sofar
= FILHSZ
;
1110 if (bfd_get_start_address(abfd
)) {
1112 A start address may have been added to the original file. In this
1113 case it will need an optional header to record it.
1115 abfd
->flags
|= EXEC_P
;
1117 if (abfd
->flags
& EXEC_P
)
1121 sofar
+= abfd
->section_count
* SCNHSZ
;
1123 for (current
= abfd
->sections
; current
!= NULL
; current
=
1125 /* Only deal with sections which have contents */
1126 if (!(current
->flags
& SEC_HAS_CONTENTS
))
1129 /* Align the sections in the file to the same boundary on
1130 which they are aligned in virtual memory. I960 doesn't
1131 do this (FIXME) so we can stay in sync with Intel. 960
1132 doesn't yet page from files... */
1134 sofar
= ALIGN(sofar
, 1 << current
->alignment_power
);
1136 /* FIXME, in demand paged files, the low order bits of the file
1137 offset must match the low order bits of the virtual address.
1138 "Low order" is apparently implementation defined. Add code
1139 here to round sofar up to match the virtual address. */
1141 current
->filepos
= sofar
;
1142 sofar
+= current
->size
;
1144 obj_relocbase(abfd
) = sofar
;
1153 coff_write_object_contents(abfd
)
1156 struct filehdr file_header
;
1158 boolean hasrelocs
= false;
1159 boolean haslinno
= false;
1160 file_ptr reloc_base
;
1161 file_ptr lineno_base
;
1165 unsigned long reloc_size
= 0;
1166 unsigned long lnno_size
= 0;
1167 asection
*text_sec
= NULL
;
1168 asection
*data_sec
= NULL
;
1169 asection
*bss_sec
= NULL
;
1174 struct icofdata
*coff
= obj_icof(abfd
);
1177 bfd_error
= system_call_error
;
1180 if(abfd
->output_has_begun
== false) {
1181 coff_compute_section_file_positions(abfd
);
1184 if (abfd
->sections
!= (asection
*)NULL
) {
1185 scn_base
= abfd
->sections
->filepos
;
1190 if (bfd_seek(abfd
, scn_base
, SEEK_SET
) != 0)
1192 reloc_base
= obj_relocbase(abfd
);
1197 Make a pass through the symbol table to count line number entries and
1198 put them into the correct asections
1200 coff_count_linenumbers(abfd
);
1201 data_base
= scn_base
;
1202 /* Work out the size of the reloc and linno areas */
1204 for (current
= abfd
->sections
; current
!= NULL
; current
= current
->next
) {
1205 reloc_size
+= current
->reloc_count
* sizeof(struct reloc
);
1206 lnno_size
+= current
->lineno_count
* sizeof(struct lineno
);
1207 data_base
+= sizeof(struct scnhdr
);
1211 lineno_base
= reloc_base
+ reloc_size
;
1212 sym_base
= lineno_base
+ lnno_size
;
1214 /* Indicate in each section->line_filepos its actual file address */
1215 for (current
= abfd
->sections
; current
!= NULL
; current
= current
->next
) {
1216 if (current
->lineno_count
) {
1217 current
->line_filepos
= lineno_base
;
1218 current
->moving_line_filepos
= lineno_base
;
1219 lineno_base
+= current
->lineno_count
* sizeof(struct lineno
);
1222 current
->line_filepos
= 0;
1224 if (current
->reloc_count
) {
1225 current
->rel_filepos
= reloc_base
;
1226 reloc_base
+= current
->reloc_count
* sizeof(struct reloc
);
1229 current
->rel_filepos
= 0;
1233 /* Write section headers to the file. */
1236 (file_ptr
) ((abfd
->flags
& EXEC_P
) ?
1237 (FILHSZ
+ AOUTSZ
) : FILHSZ
),
1242 unsigned int pad
= abfd
->flags
& D_PAGED
? data_base
: 0;
1244 unsigned int pad
= 0;
1246 for (current
= abfd
->sections
; current
!= NULL
; current
= current
->next
) {
1248 strncpy(&(section
.s_name
[0]), current
->name
, 8);
1249 section
.s_vaddr
= current
->vma
+ pad
;
1250 section
.s_paddr
= current
->vma
+ pad
;
1251 section
.s_size
= current
->size
- pad
;
1253 If this section has no size or is unloadable then the scnptr
1256 if (current
->size
- pad
== 0 ||
1257 (current
->flags
& SEC_LOAD
) == 0) {
1258 section
.s_scnptr
= 0;
1262 section
.s_scnptr
= current
->filepos
;
1264 section
.s_relptr
= current
->rel_filepos
;
1265 section
.s_lnnoptr
= current
->line_filepos
;
1266 section
.s_nreloc
= current
->reloc_count
;
1267 section
.s_nlnno
= current
->lineno_count
;
1268 if (current
->reloc_count
!= 0)
1270 if (current
->lineno_count
!= 0)
1273 if (!strcmp(current
->name
, _TEXT
)) {
1275 section
.s_flags
= STYP_TEXT
; /* kinda stupid */
1277 else if (!strcmp(current
->name
, _DATA
)) {
1279 section
.s_flags
= STYP_DATA
; /* kinda stupid */
1281 else if (!strcmp(current
->name
, _BSS
)) {
1283 section
.s_flags
= STYP_BSS
; /* kinda stupid */
1288 section
.s_align
= (current
->alignment_power
1289 ? 1 << current
->alignment_power
1293 swap_scnhdr(abfd
, §ion
);
1294 bfd_write((PTR
) (§ion
), 1, SCNHSZ
, abfd
);
1299 /* OK, now set up the filehdr... */
1301 bfd_h_put_x(abfd
, abfd
->section_count
, &file_header
.f_nscns
);
1303 We will NOT put a fucking timestamp in the header here. Every time you
1304 put it back, I will come in and take it out again. I'm sorry. This
1305 field does not belong here. We fill it with a 0 so it compares the
1306 same but is not a reasonable time. -- gnu@cygnus.com
1309 Well, I like it, so I'm conditionally compiling it in.
1312 #ifdef COFF_TIMESTAMP
1313 bfd_h_put_x(abfd
, time(0), &file_header
.f_timdat
);
1315 bfd_h_put_x(abfd
, 0, &file_header
.f_timdat
);
1318 if (bfd_get_symcount(abfd
) != 0)
1319 bfd_h_put_x(abfd
, sym_base
, &file_header
.f_symptr
);
1321 bfd_h_put_x(abfd
, 0, &file_header
.f_symptr
);
1323 file_header
.f_flags
= 0;
1325 if (abfd
->flags
& EXEC_P
)
1326 bfd_h_put_x(abfd
, sizeof(AOUTHDR
), &file_header
.f_opthdr
);
1328 bfd_h_put_x(abfd
, 0, &file_header
.f_opthdr
);
1331 file_header
.f_flags
|= F_RELFLG
;
1333 file_header
.f_flags
|= F_LNNO
;
1334 if (0 == bfd_get_symcount(abfd
))
1335 file_header
.f_flags
|= F_LSYMS
;
1336 if (abfd
->flags
& EXEC_P
)
1337 file_header
.f_flags
|= F_EXEC
;
1339 file_header
.f_flags
|= F_AR32W
;
1341 if (!abfd
->xvec
->byteorder_big_p
)
1342 file_header
.f_flags
|= F_AR32WR
;
1345 FIXME, should do something about the other byte orders and
1349 /* Set up architecture-dependent stuff */
1353 coff_set_flags(abfd
, &magic
, &flags
);
1354 file_header
.f_flags
|= flags
;
1356 bfd_h_put_x(abfd
, magic
, &file_header
.f_magic
);
1357 bfd_h_put_x(abfd
, file_header
.f_flags
, &file_header
.f_flags
);
1359 /* ...and the "opt"hdr... */
1361 bfd_h_put_x(abfd
, (magic
== I960ROMAGIC
? NMAGIC
: OMAGIC
),
1362 &(exec_hdr(abfd
).magic
));
1365 exec_hdr(abfd
).magic
= PAGEMAGICBCS
;
1368 /* Now should write relocs, strings, syms */
1369 obj_sym_filepos(abfd
) = sym_base
;
1371 if (bfd_get_symcount(abfd
) != 0) {
1372 coff_mangle_symbols(abfd
);
1373 coff_write_symbols(abfd
);
1374 coff_write_linenumbers(abfd
);
1375 coff_write_relocs(abfd
);
1378 bfd_h_put_x(abfd
, text_sec
->size
, &coff
->hdr
.tsize
);
1380 text_sec
->size
? text_sec
->vma
: 0,
1381 &exec_hdr(abfd
).text_start
);
1384 bfd_h_put_x(abfd
, data_sec
->size
, &coff
->hdr
.dsize
);
1386 data_sec
->size
? data_sec
->vma
: 0,
1387 &exec_hdr(abfd
).data_start
);
1390 bfd_h_put_x(abfd
, bss_sec
->size
, &coff
->hdr
.bsize
);
1393 bfd_h_put_x(abfd
, bfd_get_start_address(abfd
), &coff
->hdr
.entry
);
1394 bfd_h_put_x(abfd
, bfd_get_symcount(abfd
), &file_header
.f_nsyms
);
1396 /* now write them */
1397 if (bfd_seek(abfd
, 0L, SEEK_SET
) != 0)
1400 bfd_write((PTR
) &file_header
, 1, FILHSZ
, abfd
);
1402 if (abfd
->flags
& EXEC_P
) {
1403 bfd_write((PTR
) &coff
->hdr
, 1, AOUTSZ
, abfd
);
1409 coff_set_section_contents(abfd
, section
, location
, offset
, count
)
1416 if (abfd
->output_has_begun
== false) /* set by bfd.c handler */
1417 coff_compute_section_file_positions(abfd
);
1419 bfd_seek(abfd
, (file_ptr
) (section
->filepos
+ offset
), SEEK_SET
);
1422 return (bfd_write(location
, 1, count
, abfd
) == count
) ? true : false;
1428 coff_get_section_contents(abfd
, section
, location
, offset
, count
)
1436 || offset
>= section
->size
1437 || bfd_seek(abfd
, section
->filepos
+ offset
, SEEK_SET
) == -1
1438 || bfd_read(location
, 1, count
, abfd
) != count
) {
1442 } /* coff_get_section_contents() */
1446 coff_close_and_cleanup(abfd
)
1449 if (!bfd_read_p(abfd
))
1450 switch (abfd
->format
) {
1452 if (!_bfd_write_archive_contents(abfd
))
1456 if (!coff_write_object_contents(abfd
))
1460 bfd_error
= invalid_operation
;
1464 /* We depend on bfd_close to free all the memory on the obstack. */
1465 /* FIXME if bfd_release is not using obstacks! */
1471 buy_and_read(abfd
, where
, seek_direction
, size
)
1477 PTR area
= (PTR
) bfd_alloc(abfd
, size
);
1479 bfd_error
= no_memory
;
1482 bfd_seek(abfd
, where
, seek_direction
);
1483 if (bfd_read(area
, 1, size
, abfd
) != size
) {
1484 bfd_error
= system_call_error
;
1488 } /* buy_and_read() */
1491 offset_symbol_indices(symtab
, count
, offset
)
1493 unsigned long count
;
1496 SYMENT
*end
= symtab
+ count
;
1497 for (; symtab
< end
; ++symtab
) {
1498 if (symtab
->n_sclass
== C_FILE
) {
1499 symtab
->n_value
= 0;
1501 else if (symtab
->n_sclass
== C_ALIAS
) {
1503 These guys have indices in their values.
1505 symtab
->n_value
+= offset
;
1508 else if (symtab
->n_numaux
) {
1510 anybody else without an aux, has no indices.
1513 if (symtab
->n_sclass
== C_EOS
1514 || (BTYPE(symtab
->n_type
) == T_STRUCT
1515 && symtab
->n_sclass
!= C_STRTAG
)
1516 || BTYPE(symtab
->n_type
) == T_UNION
1517 || BTYPE(symtab
->n_type
) == T_ENUM
) {
1518 /* If the tagndx is 0 then the struct hasn't really been
1519 defined, so leave it alone */
1521 if(((AUXENT
*) (symtab
+ 1))->x_sym
.x_tagndx
!= 0) {
1522 ((AUXENT
*) (symtab
+ 1))->x_sym
.x_tagndx
+= offset
;
1525 } /* These guys have a tagndx */
1526 if (symtab
->n_sclass
== C_STRTAG
1527 || symtab
->n_sclass
== C_UNTAG
1528 || symtab
->n_sclass
== C_ENTAG
1529 || symtab
->n_sclass
== C_BLOCK
1530 || symtab
->n_sclass
== C_FCN
1531 || ISFCN(symtab
->n_type
)) {
1533 ((AUXENT
*) (symtab
+ 1))->x_sym
.x_fcnary
.x_fcn
.x_endndx
+= offset
;
1535 } /* These guys have an endndx */
1537 if (ISFCN(symtab
->n_type
)) {
1538 ((AUXENT
*) (symtab
+ 1))->x_sym
.x_tvndx
+= offset
;
1539 } /* These guys have a tvndx. I think...
1541 #endif /* Not I960 */
1543 } /* if value, else if aux */
1544 symtab
+= symtab
->n_numaux
;
1545 } /* walk the symtab */
1548 } /* offset_symbol_indices() */
1550 /* swap the entire symbol table */
1552 swap_raw_symtab(abfd
, raw_symtab
)
1557 SYMENT
*end
= raw_symtab
+ bfd_get_symcount(abfd
);
1558 for (; raw_symtab
< end
; ++raw_symtab
) {
1559 bfd_coff_swap_sym(abfd
, raw_symtab
);
1561 for (i
= raw_symtab
->n_numaux
; i
; --i
, ++raw_symtab
) {
1562 bfd_coff_swap_aux(abfd
,
1563 (AUXENT
*)(raw_symtab
+ 1),
1565 raw_symtab
->n_sclass
);
1566 } /* swap all the aux entries */
1567 } /* walk the symbol table */
1570 } /* swap_raw_symtab() */
1573 read a symbol table into freshly mallocated memory, swap it, and knit the
1574 symbol names into a normalized form. By normalized here I mean that all
1575 symbols have an n_offset pointer that points to a NULL terminated string.
1576 Oh, and the first symbol MUST be a C_FILE. If there wasn't one there
1577 before, put one there.
1581 get_normalized_symtab(abfd
)
1587 char *string_table
= NULL
;
1589 unsigned long string_table_size
= 0;
1591 if ((size
= bfd_get_symcount(abfd
) * sizeof(SYMENT
)) == 0) {
1592 bfd_error
= no_symbols
;
1596 /* This is a hack. Some tool chains fail to put a C_FILE symbol at the
1597 beginning of the symbol table. To make life simpler for our users, we
1598 inject one if it wasn't there originally.
1600 We'd like to keep all of this bfd's native symbols in one block to keep
1601 table traversals simple. To do that, we need to know whether we will
1602 be prepending the C_FILE symbol before we read the rest of the table.
1604 if ((s
= (SYMENT
*) bfd_alloc(abfd
, sizeof(SYMENT
) * 2)) == NULL
) {
1605 bfd_error
= no_memory
;
1608 if (bfd_seek(abfd
, obj_sym_filepos(abfd
), SEEK_SET
) == -1
1609 || bfd_read(s
, sizeof(SYMENT
), 1, abfd
) != sizeof(SYMENT
)) {
1610 bfd_error
= system_call_error
;
1613 bfd_coff_swap_sym(abfd
, s
);
1615 if (s
->n_sclass
== C_FILE
) {
1616 obj_symbol_slew(abfd
) = 0;
1618 if ((retval
= (SYMENT
*) bfd_alloc(abfd
, size
)) == NULL
) {
1619 bfd_error
= no_memory
;
1621 } /* on malloc error */
1624 unsigned long namelength
= 0;
1625 CONST
char *filename
;
1626 obj_symbol_slew(abfd
) = 2;
1628 if ((retval
= (SYMENT
*) bfd_alloc(abfd
, size
1629 + (obj_symbol_slew(abfd
)
1630 * sizeof(SYMENT
)))) == NULL
) {
1631 bfd_error
= no_memory
;
1633 } /* on malloc error */
1634 bzero((char *) retval
, size
+ (obj_symbol_slew(abfd
) * sizeof(SYMENT
)));
1636 #define FILE_ENTRY_NAME ".file"
1638 if ((retval
->n_offset
= (int) bfd_alloc(abfd
, strlen(FILE_ENTRY_NAME
)
1640 bfd_error
= no_memory
;
1642 } /* on malloc error */
1643 strcpy((char *) retval
->n_offset
, FILE_ENTRY_NAME
);
1644 retval
->n_sclass
= C_FILE
;
1645 retval
->n_scnum
= N_DEBUG
;
1646 retval
->n_numaux
= 1;
1647 retval
->n_value
= 2; /* Say that externals follow */
1649 #undef FILE_ENTRY_NAME
1651 if ((filename
= bfd_get_filename(abfd
)) == NULL
) {
1653 } /* if we know it's name */
1654 if ((namelength
= strlen(filename
)) <= FILNMLEN
) {
1655 strncpy(((AUXENT
*) (retval
+ 1))->x_file
.x_fname
, filename
, FILNMLEN
);
1658 if ((((AUXENT
*) (retval
+ 1))->x_file
.x_n
.x_offset
1659 = (int) bfd_alloc(abfd
, namelength
+1)) == NULL
) {
1660 bfd_error
= no_memory
;
1663 strcpy((char *) (((AUXENT
*) (retval
+ 1))->x_file
.x_n
.x_offset
),
1666 } /* if "short" name */
1667 } /* missing file entry. */
1671 if (bfd_seek(abfd
, obj_sym_filepos(abfd
), SEEK_SET
) == -1
1672 || bfd_read(retval
+ obj_symbol_slew(abfd
), size
, 1, abfd
) != size
) {
1673 bfd_error
= system_call_error
;
1675 } /* on read error */
1676 /* mark the end of the symbols */
1677 end
= retval
+ obj_symbol_slew(abfd
) + bfd_get_symcount(abfd
);
1679 FIXME SOMEDAY. A string table size of zero is very weird, but
1680 probably possible. If one shows up, it will probably kill us.
1683 swap_raw_symtab(abfd
, retval
+ obj_symbol_slew(abfd
));
1685 /* ...and normalize symbol names. */
1687 for (s
= retval
+ obj_symbol_slew(abfd
); s
< end
; ++s
) {
1688 if (s
->n_zeroes
!= 0) {
1690 This is a "short" name. Make it long.
1692 unsigned long i
= 0;
1693 char *newstring
= NULL
;
1695 find the length of this string without walking into memory
1699 for (i
= 0; i
< 8; ++i
) {
1700 if (s
->n_name
[i
] == '\0') {
1702 } /* if end of string */
1703 } /* possible lengths of this string. */
1705 if ((newstring
= (PTR
) bfd_alloc(abfd
, ++i
)) == NULL
) {
1706 bfd_error
= no_memory
;
1709 bzero(newstring
, i
);
1710 strncpy(newstring
, s
->n_name
, i
-1);
1711 s
->n_offset
= (int) newstring
;
1716 if (string_table
== NULL
) {
1718 NOTE: we don't read the string table until now because we
1719 don't necessarily know that we have one until now.
1722 At this point we should be "seek"'d to the end of the
1723 symbols === the symbol table size.
1726 if (bfd_read((char *) &string_table_size
, sizeof(string_table_size
), 1, abfd
) != sizeof(string_table_size
)) {
1727 bfd_error
= system_call_error
;
1730 sp(string_table_size
);
1732 if ((string_table
= (PTR
) bfd_alloc(abfd
, string_table_size
-= 4)) == NULL
) {
1733 bfd_error
= no_memory
;
1735 } /* on mallocation error */
1736 if (bfd_read(string_table
, string_table_size
, 1, abfd
) != string_table_size
) {
1737 bfd_error
= system_call_error
;
1740 } /* have not yet read the string table. */
1742 This is a long name already. Just point it at the string in
1745 s
->n_offset
= (int) (string_table
- 4 + s
->n_offset
);
1746 } /* switch on type of symbol name */
1749 } /* for each symbol */
1751 If we had to insert a C_FILE symbol, then everybody's indices are off
1752 by 2, so correct them.
1755 if (obj_symbol_slew(abfd
) > 0) {
1756 offset_symbol_indices(retval
+ 2, bfd_get_symcount(abfd
), 2);
1758 /* and let the world know there are two more of them. */
1759 bfd_get_symcount(abfd
) += 2;
1762 } /* if we added a C_FILE */
1765 /* I'm not sure of the repercussions of this, so the Intel
1766 folks will always do the force
1768 if (obj_symbol_slew(abfd
) > 0)
1769 force_indices_file_symbol_relative(abfd
, retval
);
1771 force_indices_file_symbol_relative(abfd
, retval
);
1774 obj_raw_syments(abfd
) = retval
;
1775 obj_string_table(abfd
) = string_table
;
1778 } /* get_normalized_symtab() */
1782 section_from_bfd_index(abfd
, index
)
1787 struct sec
*answer
= abfd
->sections
;
1789 answer
= answer
->next
;
1800 coff_slurp_line_table(abfd
, asect
)
1804 struct lineno
*native_lineno
;
1805 alent
*lineno_cache
;
1807 BFD_ASSERT(asect
->lineno
== (alent
*) NULL
);
1809 native_lineno
= (struct lineno
*) buy_and_read(abfd
,
1810 asect
->line_filepos
,
1812 (size_t) (sizeof(struct lineno
) *
1813 asect
->lineno_count
));
1815 (alent
*) bfd_alloc(abfd
, (size_t) ((asect
->lineno_count
+ 1) * sizeof(alent
)));
1816 if (lineno_cache
== NULL
) {
1817 bfd_error
= no_memory
;
1820 unsigned int counter
= 0;
1821 alent
*cache_ptr
= lineno_cache
;
1822 struct lineno
*src
= native_lineno
;
1824 while (counter
< asect
->lineno_count
) {
1825 bfd_coff_swap_lineno(abfd
, src
);
1826 cache_ptr
->line_number
= src
->l_lnno
;
1828 if (cache_ptr
->line_number
== 0) {
1829 coff_symbol_type
*sym
=
1830 (coff_symbol_type
*) (src
->l_addr
.l_symndx
1831 + obj_symbol_slew(abfd
)
1832 + obj_raw_syments(abfd
))->n_zeroes
;
1833 cache_ptr
->u
.sym
= (asymbol
*) sym
;
1834 sym
->lineno
= cache_ptr
;
1837 cache_ptr
->u
.offset
= src
->l_addr
.l_paddr
1838 - bfd_section_vma(abfd
, asect
);
1839 } /* If no linenumber expect a symbol index */
1845 cache_ptr
->line_number
= 0;
1848 asect
->lineno
= lineno_cache
;
1849 /* FIXME, free native_lineno here, or use alloca or something. */
1851 } /* coff_slurp_line_table() */
1854 find_next_file_symbol(current
, end
)
1858 current
+= current
->n_numaux
+ 1;
1860 while (current
< end
) {
1861 if (current
->n_sclass
== C_FILE
) {
1864 current
+= current
->n_numaux
+ 1;
1872 Note that C_FILE symbols can, and some do, have more than 1 aux entry.
1876 DEFUN(force_indices_file_symbol_relative
,(abfd
, symtab
),
1880 SYMENT
*end
= symtab
+ bfd_get_symcount(abfd
);
1883 /* the first symbol had damn well better be a C_FILE. */
1884 BFD_ASSERT(symtab
->n_sclass
== C_FILE
);
1886 for (current
= find_next_file_symbol(symtab
, end
);
1889 offset_symbol_indices(current
,
1891 find_next_file_symbol(current
,
1894 } /* walk the table */
1897 } /* force_indices_file_symbol_relative() */
1900 coff_slurp_symbol_table(abfd
)
1903 SYMENT
*native_symbols
;
1904 coff_symbol_type
*cached_area
;
1905 unsigned int *table_ptr
;
1907 unsigned int number_of_symbols
= 0;
1908 if (obj_symbols(abfd
))
1910 bfd_seek(abfd
, obj_sym_filepos(abfd
), SEEK_SET
);
1912 /* Read in the symbol table */
1913 if ((native_symbols
= get_normalized_symtab(abfd
)) == NULL
) {
1918 /* Allocate enough room for all the symbols in cached form */
1920 (coff_symbol_type
*)
1921 bfd_alloc(abfd
, (size_t) (bfd_get_symcount(abfd
) * sizeof(coff_symbol_type
)));
1923 if (cached_area
== NULL
) {
1924 bfd_error
= no_memory
;
1929 bfd_alloc(abfd
, (size_t) (bfd_get_symcount(abfd
) * sizeof(unsigned int)));
1931 if (table_ptr
== NULL
) {
1932 bfd_error
= no_memory
;
1935 coff_symbol_type
*dst
= cached_area
;
1936 unsigned int last_native_index
= bfd_get_symcount(abfd
);
1937 unsigned int this_index
= 0;
1938 while (this_index
< last_native_index
) {
1939 SYMENT
*src
= native_symbols
+ this_index
;
1940 table_ptr
[this_index
] = number_of_symbols
;
1941 dst
->symbol
.the_bfd
= abfd
;
1943 dst
->symbol
.name
= (char *) src
->n_offset
; /* which was normalized
1945 terminated string. */
1947 We use the native name field to point to the cached field
1949 src
->n_zeroes
= (int) dst
;
1950 dst
->symbol
.section
= section_from_bfd_index(abfd
, src
->n_scnum
);
1952 switch (src
->n_sclass
) {
1956 dst
->symbol
.value
= src
->n_value
- dst
->symbol
.section
->vma
;
1957 dst
->symbol
.flags
= BSF_EXPORT
| BSF_GLOBAL
;
1958 dst
->symbol
.flags
|= BSF_NOT_AT_END
;
1960 /* Fall through to next case */
1965 if (src
->n_scnum
== 0) {
1966 if (src
->n_value
== 0) {
1967 dst
->symbol
.flags
= BSF_UNDEFINED
;
1970 dst
->symbol
.flags
= BSF_FORT_COMM
;
1971 dst
->symbol
.value
= src
->n_value
;
1976 Base the value as an index from the base of the
1979 if (dst
->symbol
.section
== (asection
*) NULL
) {
1980 dst
->symbol
.flags
= BSF_EXPORT
| BSF_GLOBAL
| BSF_ABSOLUTE
;
1981 dst
->symbol
.value
= src
->n_value
;
1984 dst
->symbol
.flags
= BSF_EXPORT
| BSF_GLOBAL
;
1985 dst
->symbol
.value
= src
->n_value
- dst
->symbol
.section
->vma
;
1987 if (ISFCN(src
->n_type
)) {
1989 A function ext does not go at the end of a file
1991 dst
->symbol
.flags
|= BSF_NOT_AT_END
;
1996 case C_STAT
: /* static */
1998 case C_LEAFSTAT
: /* static leaf procedure */
2000 case C_LABEL
: /* label */
2001 dst
->symbol
.flags
= BSF_LOCAL
;
2003 Base the value as an index from the base of the section
2005 dst
->symbol
.value
= src
->n_value
- dst
->symbol
.section
->vma
;
2008 case C_MOS
: /* member of structure */
2009 case C_EOS
: /* end of structure */
2010 case C_REGPARM
: /* register parameter */
2011 case C_REG
: /* register variable */
2013 case C_AUTOARG
: /* 960-specific storage class */
2015 case C_TPDEF
: /* type definition */
2018 case C_AUTO
: /* automatic variable */
2019 case C_FIELD
: /* bit field */
2020 case C_ENTAG
: /* enumeration tag */
2021 case C_MOE
: /* member of enumeration */
2022 case C_MOU
: /* member of union */
2023 case C_UNTAG
: /* union tag */
2025 dst
->symbol
.flags
= BSF_DEBUGGING
;
2026 dst
->symbol
.value
= src
->n_value
;
2029 case C_FILE
: /* file name */
2030 case C_STRTAG
: /* structure tag */
2031 dst
->symbol
.flags
= BSF_DEBUGGING
;
2032 dst
->symbol
.value
= src
->n_value
;
2035 case C_BLOCK
: /* ".bb" or ".eb" */
2036 case C_FCN
: /* ".bf" or ".ef" */
2037 dst
->symbol
.flags
= BSF_LOCAL
;
2039 Base the value as an index from the base of the section
2041 dst
->symbol
.value
= src
->n_value
- dst
->symbol
.section
->vma
;
2044 case C_EFCN
: /* physical end of function */
2046 case C_EXTDEF
: /* external definition */
2047 case C_ULABEL
: /* undefined label */
2048 case C_USTATIC
: /* undefined static */
2049 case C_LINE
: /* line # reformatted as symbol table entry */
2050 case C_ALIAS
: /* duplicate tag */
2051 case C_HIDDEN
: /* ext symbol in dmert public lib */
2055 printf("SICK%d\n", src
->n_sclass
);
2057 dst
->symbol
.flags
= BSF_DEBUGGING
;
2058 dst
->symbol
.value
= src
->n_value
;
2063 BFD_ASSERT(dst
->symbol
.flags
!= 0);
2067 dst
->symbol
.udata
= 0;
2068 dst
->lineno
= (alent
*) NULL
;
2069 this_index
+= src
->n_numaux
+ 1;
2071 number_of_symbols
++;
2072 } /* walk the native symtab */
2073 } /* bfdize the native symtab */
2075 obj_symbols(abfd
) = cached_area
;
2076 obj_raw_syments(abfd
) = native_symbols
;
2078 bfd_get_symcount(abfd
) = number_of_symbols
;
2079 obj_convert(abfd
) = table_ptr
;
2080 /* Slurp the line tables for each section too */
2085 coff_slurp_line_table(abfd
, p
);
2090 } /* coff_slurp_symbol_table() */
2093 coff_get_symtab_upper_bound(abfd
)
2096 if (!coff_slurp_symbol_table(abfd
))
2099 return (bfd_get_symcount(abfd
) + 1) * (sizeof(coff_symbol_type
*));
2104 coff_get_symtab(abfd
, alocation
)
2106 asymbol
**alocation
;
2108 unsigned int counter
= 0;
2109 coff_symbol_type
*symbase
;
2110 coff_symbol_type
**location
= (coff_symbol_type
**) (alocation
);
2111 if (!coff_slurp_symbol_table(abfd
))
2114 for (symbase
= obj_symbols(abfd
); counter
++ < bfd_get_symcount(abfd
);)
2115 *(location
++) = symbase
++;
2117 return bfd_get_symcount(abfd
);
2121 coff_get_reloc_upper_bound(abfd
, asect
)
2125 if (bfd_get_format(abfd
) != bfd_object
) {
2126 bfd_error
= invalid_operation
;
2129 return (asect
->reloc_count
+ 1) * sizeof(arelent
*);
2134 coff_slurp_reloc_table(abfd
, asect
, symbols
)
2139 struct reloc
*native_relocs
;
2140 arelent
*reloc_cache
;
2141 if (asect
->relocation
)
2143 if (asect
->reloc_count
== 0)
2145 if (!coff_slurp_symbol_table(abfd
))
2148 (struct reloc
*) buy_and_read(abfd
,
2151 (size_t) (sizeof(struct reloc
) *
2152 asect
->reloc_count
));
2153 reloc_cache
= (arelent
*)
2154 bfd_alloc(abfd
, (size_t) (asect
->reloc_count
* sizeof(arelent
)));
2156 if (reloc_cache
== NULL
) {
2157 bfd_error
= no_memory
;
2162 for (cache_ptr
= reloc_cache
,
2163 src
= native_relocs
;
2164 cache_ptr
< reloc_cache
+ asect
->reloc_count
;
2168 swap_reloc(abfd
, src
);
2169 src
->r_symndx
+= obj_symbol_slew(abfd
);
2170 cache_ptr
->sym_ptr_ptr
= symbols
+ obj_convert(abfd
)[src
->r_symndx
];
2172 ptr
= *(cache_ptr
->sym_ptr_ptr
);
2173 cache_ptr
->address
= src
->r_vaddr
;
2175 The symbols definitions that we have read in have been
2176 relocated as if their sections started at 0. But the offsets
2177 refering to the symbols in the raw data have not been
2178 modified, so we have to have a negative addend to compensate.
2180 Note that symbols which used to be common must be left alone
2183 if (ptr
->the_bfd
== abfd
2184 && ptr
->section
!= (asection
*) NULL
2185 && ((ptr
->flags
& BSF_OLD_COMMON
)== 0))
2187 cache_ptr
->addend
= -(ptr
->section
->vma
+ ptr
->value
);
2190 cache_ptr
->addend
= 0;
2193 cache_ptr
->address
-= asect
->vma
;
2195 cache_ptr
->section
= (asection
*) NULL
;
2198 cache_ptr
->howto
= howto_table
+ src
->r_type
;
2201 if (src
->r_type
>= R_PCR16L
&& src
->r_type
<= R_VRT32
) {
2202 cache_ptr
->howto
= howto_table
+ src
->r_type
- R_PCR16L
;
2203 cache_ptr
->addend
+= src
->r_offset
<< 16;
2210 cache_ptr
->howto
= howto_table
+ src
->r_type
;
2217 asect
->relocation
= reloc_cache
;
2222 /* This is stupid. This function should be a boolean predicate */
2224 coff_canonicalize_reloc(abfd
, section
, relptr
, symbols
)
2230 arelent
*tblptr
= section
->relocation
;
2231 unsigned int count
= 0;
2232 if (!(tblptr
|| coff_slurp_reloc_table(abfd
, section
, symbols
)))
2234 tblptr
= section
->relocation
;
2238 for (; count
++ < section
->reloc_count
;)
2239 *relptr
++ = tblptr
++;
2243 return section
->reloc_count
;
2248 provided a bfd, a section and an offset into the section, calculate and
2249 return the name of the source file and the line nearest to the wanted
2254 DEFUN(coff_find_nearest_line
,(abfd
,
2262 asection
*section AND
2263 asymbol
**symbols AND
2265 CONST
char **filename_ptr AND
2266 CONST
char **functionname_ptr AND
2267 unsigned int *line_ptr
)
2269 static bfd
*cache_abfd
;
2270 static asection
*cache_section
;
2271 static bfd_vma cache_offset
;
2272 static unsigned int cache_i
;
2273 static alent
*cache_l
;
2276 struct icofdata
*cof
= obj_icof(abfd
);
2277 /* Run through the raw syments if available */
2280 unsigned int line_base
= 0;
2284 *functionname_ptr
= 0;
2287 /* Don't try and find line numbers in a non coff file */
2288 if (abfd
->xvec
->flavour
!= bfd_target_coff_flavour_enum
)
2291 if (cof
== (struct icofdata
*)NULL
)
2294 p
= cof
->raw_syments
;
2296 I don't know for sure what's right, but this isn't it. First off, an
2297 object file may not have any C_FILE's in it. After
2298 get_normalized_symtab(), it should have at least 1, the one I put
2299 there, but otherwise, all bets are off. Point #2, the first C_FILE
2300 isn't necessarily the right C_FILE because any given object may have
2301 many. I think you'll have to track sections as they coelesce in order
2302 to find the C_STAT symbol for this section. Then you'll have to work
2303 backwards to find the previous C_FILE, or choke if you get to a C_STAT
2304 for the same kind of section. That will mean that the original object
2305 file didn't have a C_FILE. xoxorich.
2308 #ifdef WEREBEINGPEDANTIC
2312 for (i
= 0; i
< cof
->raw_syment_count
; i
++) {
2313 if (p
->n_sclass
== C_FILE
) {
2314 /* File name is embeded in auxent */
2316 This isn't right. The fname should probably be normalized
2317 during get_normalized_symtab(). In any case, what was here
2318 wasn't right because a SYMENT.n_name isn't an
2319 AUXENT.x_file.x_fname. xoxorich.
2322 *filename_ptr
= ((AUXENT
*) (p
+ 1))->x_file
.x_fname
;
2325 p
+= 1 + p
->n_numaux
;
2327 /* Now wander though the raw linenumbers of the section */
2329 If this is the same bfd as we were previously called with and this is
2330 the same section, and the offset we want is further down then we can
2331 prime the lookup loop
2333 if (abfd
== cache_abfd
&&
2334 section
== cache_section
&&
2335 offset
>= cache_offset
) {
2341 l
= section
->lineno
;
2344 for (; i
< section
->lineno_count
; i
++) {
2345 if (l
->line_number
== 0) {
2346 /* Get the symbol this line number points at */
2347 coff_symbol_type
*coff
= (coff_symbol_type
*) (l
->u
.sym
);
2348 *functionname_ptr
= coff
->symbol
.name
;
2350 struct syment
*s
= coff
->native
;
2351 s
= s
+ 1 + s
->n_numaux
;
2353 S should now point to the .bf of the function
2357 The linenumber is stored in the auxent
2359 union auxent
*a
= (union auxent
*) (s
+ 1);
2360 line_base
= a
->x_sym
.x_misc
.x_lnsz
.x_lnno
;
2365 if (l
->u
.offset
> offset
)
2367 *line_ptr
= l
->line_number
+ line_base
+ 1;
2373 cache_section
= section
;
2374 cache_offset
= offset
;
2382 coff_sym_filepos(abfd
)
2385 return obj_sym_filepos(abfd
);
2391 DEFUN(coff_sizeof_headers
,(abfd
, reloc
),
2397 if (reloc
== false) {
2398 size
= sizeof(struct filehdr
) + sizeof(AOUTHDR
);
2401 size
= sizeof(struct filehdr
);
2404 size
+= abfd
->section_count
* SCNHSZ
;
2409 #define coff_core_file_failing_command _bfd_dummy_core_file_failing_command
2410 #define coff_core_file_failing_signal _bfd_dummy_core_file_failing_signal
2411 #define coff_core_file_matches_executable_p _bfd_dummy_core_file_matches_executable_p
2412 #define coff_slurp_armap bfd_slurp_coff_armap
2413 #define coff_slurp_extended_name_table _bfd_slurp_extended_name_table
2414 #define coff_truncate_arname bfd_dont_truncate_arname
2415 #define coff_openr_next_archived_file bfd_generic_openr_next_archived_file
2416 #define coff_generic_stat_arch_elt bfd_generic_stat_arch_elt