2 Copyright (C) 1990, 1991 Free Software Foundation, Inc.
4 This file is part of BFD, the Binary File Diddler.
6 BFD is free software; you can redistribute it and/or modify it under the
7 terms of the GNU General Public License as published by the Free Software
8 Foundation; either version 1, or (at your option) any later version.
10 BFD is distributed in the hope that it will be useful, but WITHOUT ANY
11 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
15 You should have received a copy of the GNU General Public License along with
16 BFD; see the file COPYING. If not, write to the Free Software Foundation,
17 675 Mass Ave, Cambridge, MA 02139, USA.
22 Most of this hacked by Steve Chamberlain, steve@cygnus.com
23 (All the ugly bits by rich :)
26 #include "archures.h" /* Machine architectures and types */
34 #define ALIGN(this, boundary) \
35 ((( (this) + ((boundary) -1)) & (~((boundary)-1))))
37 #define sp(x) bfd_h_put_x(abfd, x, &x)
42 Align an address by rounding it up to a power of two. It leaves the
43 address unchanged if align == 0 (2^0 = alignment of 1 byte)
45 #define i960_align(addr, align) \
46 ( ((addr) + ((1<<(align))-1)) & (-1 << (align)))
49 PROTO(static void,force_indices_file_symbol_relative
,(bfd
*abfd
, SYMENT
*symtab
));
51 /* All the swapping routines */
54 DEFUN(swap_reloc
,(abfd
, reloc
),
65 DEFUN(swap_filehdr
,(abfd
, filehdr
),
71 sp(filehdr
->f_timdat
);
72 sp(filehdr
->f_symptr
);
74 sp(filehdr
->f_opthdr
);
81 DEFUN(swap_aouthdr
,(abfd
, aouthdr
),
92 sp(aouthdr
->text_start
);
93 sp(aouthdr
->data_start
);
95 sp(aouthdr
->tagentries
);
100 DEFUN(swap_scnhdr
,(abfd
, scnhdr
),
107 sp(scnhdr
->s_scnptr
);
108 sp(scnhdr
->s_relptr
);
109 sp(scnhdr
->s_lnnoptr
);
110 sp(scnhdr
->s_nreloc
);
119 DEFUN(swap_name
,(abfd
, ptr
),
124 /* There is an index which needs to be swapped */
125 bfd_h_put_x(abfd
, ptr
[1], (ptr
+ 1));
128 /* This is a string .. leave it alone */
133 DEFUN(bfd_coff_swap_sym
,(abfd
, se
),
137 swap_name(abfd
, (long*)(se
->n_name
));
138 bfd_h_put_x(abfd
, se
->n_value
, &se
->n_value
);
139 bfd_h_put_x(abfd
, se
->n_scnum
, &se
->n_scnum
);
140 bfd_h_put_x(abfd
, se
->n_type
, &se
->n_type
);
141 bfd_h_put_x(abfd
, se
->n_sclass
, &se
->n_sclass
);
142 bfd_h_put_x(abfd
, se
->n_numaux
, &se
->n_numaux
);
149 DEFUN(bfd_coff_swap_aux
,(abfd
, au
, type
, class),
157 swap_name(abfd
, (long *)(&au
->x_file
.x_n
));
164 if (type
== T_NULL
) {
165 sp(au
->x_scn
.x_scnlen
);
166 sp(au
->x_scn
.x_nreloc
);
167 sp(au
->x_scn
.x_nlinno
);
171 sp(au
->x_sym
.x_tagndx
);
172 sp(au
->x_sym
.x_tvndx
);
175 sp(au
->x_sym
.x_fcnary
.x_ary
.x_dimen
[0]);
176 sp(au
->x_sym
.x_fcnary
.x_ary
.x_dimen
[1]);
177 sp(au
->x_sym
.x_fcnary
.x_ary
.x_dimen
[2]);
178 sp(au
->x_sym
.x_fcnary
.x_ary
.x_dimen
[3]);
181 sp(au
->x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
);
182 sp(au
->x_sym
.x_fcnary
.x_fcn
.x_endndx
);
185 sp(au
->x_sym
.x_misc
.x_fsize
);
188 sp(au
->x_sym
.x_misc
.x_lnsz
.x_lnno
);
189 sp(au
->x_sym
.x_misc
.x_lnsz
.x_size
);
198 DEFUN(bfd_coff_swap_lineno
,(abfd
, lineno
),
202 sp(lineno
->l_addr
.l_symndx
);
207 /* void warning(); */
208 extern asection abs_section
;
213 DEFUN(get_index
,(symbol
),
216 return (int) symbol
->value
;
220 DEFUN(set_index
,(symbol
, idx
),
228 initialize a section structure with information peculiar to this
229 particular implementation of coff
233 DEFUN(coff_new_section_hook
,(abfd_ignore
, section_ignore
),
235 asection
*section_ignore
)
239 /* actually it makes itself and its children from the file headers */
241 DEFUN(make_a_section_from_file
,(abfd
, hdr
),
246 asection
*return_section
;
248 char *name
= bfd_alloc(abfd
, 9);
250 bfd_error
= no_memory
;
251 return (BFD_FAILURE
);
253 strncpy(name
, (char *) &hdr
->s_name
[0], 8);
255 return_section
= bfd_make_section(abfd
, name
);
256 (return_section
->name
)[8] = 0;
259 /* s_paddr is presumed to be = to s_vaddr */
260 #define assign(to, from) return_section->to = hdr->from
261 assign(vma
, s_vaddr
);
262 /* assign (vma, s_vaddr); */
263 assign(size
, s_size
);
264 assign(filepos
, s_scnptr
);
265 assign(rel_filepos
, s_relptr
);
266 assign(reloc_count
, s_nreloc
);
270 assign(alignment_power
, s_align
);
273 for (i
= 0; i
< 32; i
++) {
274 if ((1 << i
) >= (int) (return_section
->alignment_power
)) {
275 return_section
->alignment_power
= i
;
282 assign(line_filepos
, s_lnnoptr
);
284 return_section->linesize = hdr->s_nlnno * sizeof (struct lineno);
288 return_section
->lineno_count
= hdr
->s_nlnno
;
289 return_section
->userdata
= NULL
;
290 return_section
->next
= (asection
*) NULL
;
291 if ((hdr
->s_flags
& STYP_TEXT
) || (hdr
->s_flags
& STYP_DATA
))
292 return_section
->flags
= (SEC_LOAD
| SEC_ALLOC
);
293 else if (hdr
->s_flags
& STYP_BSS
)
294 return_section
->flags
= SEC_ALLOC
;
296 if (hdr
->s_nreloc
!= 0)
297 return_section
->flags
|= SEC_RELOC
;
298 if (hdr
->s_scnptr
!= 0)
299 return_section
->flags
|= SEC_HAS_CONTENTS
;
304 DEFUN(coff_mkobject
,(abfd
),
307 coff_data_type
*coff
;
309 (coff_data_type
*)bfd_alloc(abfd
,sizeof(coff_data_type
));
310 coff
= coff_data(abfd
);
312 coff
->hdr
= (AOUTHDR
*) bfd_alloc(abfd
, sizeof(AOUTHDR
));
319 DEFUN(coff_real_object_p
,(abfd
, nscns
, opthdr
),
324 coff_data_type
*coff
;
325 char *file_info
; /* buffer for all the headers */
326 size_t readsize
; /* length of file_info */
327 struct filehdr
*filehdr
; /* points into file_info */
328 struct scnhdr
*sections
; /* points into file_info */
330 /* Build a play area */
331 (void) coff_mkobject(abfd
);
332 coff
= coff_data(abfd
);
334 OK, now we know the format, read in the filehdr, soi-disant "optional
335 header", and all the sections.
337 readsize
= sizeof(struct filehdr
)
339 + (nscns
* sizeof(struct scnhdr
));
341 file_info
= (PTR
) bfd_alloc(abfd
, readsize
);
342 if (bfd_seek(abfd
, 0L, SEEK_SET
) < 0)
345 if (bfd_read((PTR
) file_info
, 1, readsize
, abfd
) != readsize
)
347 filehdr
= (struct filehdr
*) file_info
;
348 sections
= (struct scnhdr
*) (file_info
+ sizeof(struct filehdr
) + opthdr
);
351 swap_filehdr(abfd
, filehdr
);
353 /* Now copy data as required; construct all asections etc */
354 coff
->symbol_index_slew
= 0;
356 coff
->raw_syment_count
= 0;
357 coff
->raw_linenos
= 0;
358 coff
->raw_syments
= 0;
359 coff
->sym_filepos
=0;
360 coff
->flags
= filehdr
->f_flags
;
363 for (i
= 0; i
< nscns
; i
++) {
364 swap_scnhdr(abfd
, sections
+ i
);
365 make_a_section_from_file(abfd
, sections
+ i
);
368 /* Determine the machine architecture and type. */
369 abfd
->obj_machine
= 0;
370 switch (filehdr
->f_magic
) {
374 abfd
->obj_arch
= bfd_arch_m68k
;
375 abfd
->obj_machine
= 68020;
382 abfd
->obj_arch
= bfd_arch_m88k
;
383 abfd
->obj_machine
= 88100;
389 abfd
->obj_arch
= bfd_arch_i960
;
390 switch (F_I960TYPE
& filehdr
->f_flags
)
394 abfd
->obj_machine
= bfd_mach_i960_core
;
397 abfd
->obj_machine
= bfd_mach_i960_kb_sb
;
400 abfd
->obj_machine
= bfd_mach_i960_mc
;
403 abfd
->obj_machine
= bfd_mach_i960_xa
;
406 abfd
->obj_machine
= bfd_mach_i960_ca
;
409 abfd
->obj_machine
= bfd_mach_i960_ka_sa
;
416 default: /* Unreadable input file type */
417 abfd
->obj_arch
= bfd_arch_obscure
;
421 if (!(filehdr
->f_flags
& F_RELFLG
))
422 abfd
->flags
|= HAS_RELOC
;
423 if ((filehdr
->f_flags
& F_EXEC
))
424 abfd
->flags
|= EXEC_P
;
425 if (!(filehdr
->f_flags
& F_LNNO
))
426 abfd
->flags
|= HAS_LINENO
;
427 if (!(filehdr
->f_flags
& F_LSYMS
))
428 abfd
->flags
|= HAS_LOCALS
;
431 bfd_get_symcount(abfd
) = filehdr
->f_nsyms
;
432 if (filehdr
->f_nsyms
)
433 abfd
->flags
|= HAS_SYMS
;
435 coff
->sym_filepos
= filehdr
->f_symptr
;
436 swap_aouthdr(abfd
, coff
->hdr
);
438 coff
->symbols
= (coff_symbol_type
*) NULL
;
439 bfd_get_start_address(abfd
) = opthdr
? exec_hdr(abfd
)->entry
: 0;
443 bfd_release(abfd
, coff
);
444 return (bfd_target
*)NULL
;
449 Takes a bfd and a symbol, returns a pointer to the coff specific area
450 of the symbol if there is one.
452 static coff_symbol_type
*
453 DEFUN(coff_symbol_from
,(abfd
, symbol
),
457 if (symbol
->the_bfd
->xvec
->flavour
!= bfd_target_coff_flavour_enum
)
458 return (coff_symbol_type
*)NULL
;
460 if (symbol
->the_bfd
->tdata
== (PTR
)NULL
)
461 return (coff_symbol_type
*)NULL
;
463 return (coff_symbol_type
*) symbol
;
469 DEFUN(coff_object_p
,(abfd
),
472 unsigned short magic
,
475 bfd_error
= system_call_error
;
477 /* figure out how much to read */
478 if (bfd_read((PTR
) &magic
, 1, sizeof(magic
), abfd
) != sizeof(magic
))
481 magic
= bfd_h_getshort(abfd
, (bfd_byte
*) (&magic
));
483 if (BADMAG(*((struct filehdr
*) & magic
))) {
484 bfd_error
= wrong_format
;
487 if (bfd_read((PTR
) &nscns
, 1, sizeof(nscns
), abfd
) != sizeof(nscns
))
489 nscns
= bfd_h_getshort(abfd
, (unsigned char *) &nscns
);
490 if (bfd_seek(abfd
, (file_ptr
) & (((struct filehdr
*) NULL
)->f_opthdr
), SEEK_SET
)
492 return (bfd_target
*) NULL
;
493 if (bfd_read((PTR
) &opthdr
, 1, sizeof(opthdr
), abfd
) != sizeof(opthdr
))
494 return (bfd_target
*) NULL
;
495 opthdr
= bfd_h_getshort(abfd
, (unsigned char *) &opthdr
);
497 /* if the optional header is NULL or not the correct size then
498 quit; the only difference I can see between m88k dgux headers (MC88DMAGIC)
499 and Intel 960 readwrite headers (I960WRMAGIC) is that the
500 optional header is of a different size
505 return (bfd_target
*)NULL
;
507 return coff_real_object_p(abfd
, nscns
, opthdr
);
514 DEFUN(coff_count_linenumbers
,(abfd
),
517 unsigned int limit
= bfd_get_symcount(abfd
);
521 asection
*s
= abfd
->sections
->output_section
;
523 BFD_ASSERT(s
->lineno_count
== 0);
529 for (p
= abfd
->outsymbols
, i
= 0; i
< limit
; i
++, p
++) {
530 asymbol
*q_maybe
= *p
;
531 if (q_maybe
->the_bfd
->xvec
->flavour
== bfd_target_coff_flavour_enum
) {
532 coff_symbol_type
*q
= coffsymbol(q_maybe
);
535 This symbol has a linenumber, increment the owning
536 section's linenumber count
538 alent
*l
= q
->lineno
;
539 q
->symbol
.section
->output_section
->lineno_count
++;
541 while (l
->line_number
) {
542 q
->symbol
.section
->output_section
->lineno_count
++;
551 This function returns true if the supplied SYMENT has an AUXENT with
552 a tagndx field which should be relocated.
554 The coff book says that all auxents have this and should be moved,
555 but all the actual implementations I've looked at do this ..
560 DEFUN(uses_x_sym_x_tagndx_p
,(native
),
563 if (BTYPE(native
->n_type
) == T_STRUCT
) return true;
564 if (BTYPE(native
->n_type
) == T_UNION
) return true;
565 if (BTYPE(native
->n_type
) == T_ENUM
)return true;
575 This procedure runs through the native entries in a coff symbol table
576 and links up all the elements which should point to one another, in
577 particular these are:
579 strtag, entag and untags have an auxent endindex which points to the
580 first syment after the .eos. This is simple to do, we just keep a
581 pointer to the symbol with the most recent pending strtag and patch it
582 when we see the eos. This works since coff structs are never nested.
584 ISFCN type entries have an endindex which points to the next static or
585 extern in the table, thereby skipping the function contents.
586 The coff book says that an ISFCN's tagindex
587 points to the first .bf for the function, so far I havn't seen it
588 used. We do this using the same mechanism as strtags.
590 Each file entry has a value which points to the next file entry,
591 the last file entry points to the first extern symbol in the table
592 which is not an ISFCN.
594 Each .bb entry points to the matching .eb entry, but these are nested
595 so we keep a stack of them.
597 The tagndx of .eos items points to the strtag attached to them, this
598 is simply the last_tagndx again.
600 The tagndx of items with type strtag point to the defining struct.
601 This bit is complicated; We know that a struct ref and def must be
602 within the same file, so all the natives will be in the same vector.
603 This means that we can subtracts two pointers and get the index
604 differences between to items, used to work out the true index of the
607 We store in the name field of each syment the actual native index
608 applied so we can dig it out through a pointer.
613 coff_mangle_symbols(bfd_ptr
)
616 unsigned int symbol_count
= bfd_get_symcount(bfd_ptr
);
617 asymbol
**symbol_ptr_ptr
= bfd_ptr
->outsymbols
;
618 SYMENT
*last_tagndx
= (SYMENT
*)NULL
;
619 SYMENT
*last_file
= (SYMENT
*)NULL
;
620 SYMENT
*last_fcn
= (SYMENT
*)NULL
;
621 SYMENT
*block_stack
[50];
622 SYMENT
**last_block
= &block_stack
[0];
623 boolean first_time
= true;
624 unsigned int symbol_index
;
625 unsigned int native_index
= 0;
627 for (symbol_index
= 0; symbol_index
< symbol_count
; symbol_index
++) {
628 coff_symbol_type
*coff_symbol_ptr
=
629 coff_symbol_from(bfd_ptr
, symbol_ptr_ptr
[symbol_index
]);
630 if (coff_symbol_ptr
== (coff_symbol_type
*)NULL
) {
632 This symbol has no coff information in it, it will take up
633 only one slot in the output symbol table
638 SYMENT
*syment
= coff_symbol_ptr
->native
;
639 if (syment
== (SYMENT
*)NULL
) {
643 /* Normalize the symbol flags */
644 if (coff_symbol_ptr
->symbol
.flags
& BSF_FORT_COMM
) {
645 /* a common symbol is undefined with a value */
646 syment
->n_scnum
= N_UNDEF
;
647 syment
->n_value
= coff_symbol_ptr
->symbol
.value
;
649 else if (coff_symbol_ptr
->symbol
.flags
& BSF_DEBUGGING
) {
650 syment
->n_value
= coff_symbol_ptr
->symbol
.value
;
652 else if (coff_symbol_ptr
->symbol
.flags
& BSF_UNDEFINED
) {
653 syment
->n_scnum
= N_UNDEF
;
656 else if (coff_symbol_ptr
->symbol
.flags
& BSF_ABSOLUTE
) {
657 syment
->n_scnum
= N_ABS
;
658 syment
->n_value
= coff_symbol_ptr
->symbol
.value
;
662 coff_symbol_ptr
->symbol
.section
->output_section
->index
+1;
664 coff_symbol_ptr
->symbol
.value
+
665 coff_symbol_ptr
->symbol
.section
->output_offset
+
666 coff_symbol_ptr
->symbol
.section
->output_section
->vma
;
670 /* If this symbol ties up something then do it */
672 if (syment
->n_sclass
== C_FILE
&& last_file
!= (SYMENT
*)NULL
)
674 last_file
->n_value
= native_index
;
676 else if ((syment
->n_sclass
== C_EXT
677 || syment
->n_sclass
== C_STAT
679 || syment
->n_sclass
== C_LEAFEXT
680 || syment
->n_sclass
== C_LEAFSTAT
683 && last_fcn
!= (SYMENT
*)NULL
)
685 AUXENT
*auxent
= (AUXENT
*)(last_fcn
+1);
686 auxent
->x_sym
.x_fcnary
.x_fcn
.x_endndx
= native_index
;
687 last_fcn
= (SYMENT
*)NULL
;
690 else if (syment
->n_sclass
== C_EOS
&& last_tagndx
!= (SYMENT
*)NULL
)
692 AUXENT
*auxent
= (AUXENT
*)(last_tagndx
+1);
693 /* Remember that we keep the native index in the offset
694 so patch the beginning of the struct to point to this
696 auxent
->x_sym
.x_tagndx
= last_tagndx
->n_offset
;
697 auxent
->x_sym
.x_fcnary
.x_fcn
.x_endndx
=
698 native_index
+ syment
->n_numaux
+ 1 ;
699 /* Now point the eos to the structure */
700 auxent
= (AUXENT
*)(syment
+1);
701 auxent
->x_sym
.x_tagndx
= last_tagndx
->n_offset
;
705 else if (syment
->n_sclass
== C_BLOCK
706 && coff_symbol_ptr
->symbol
.name
[1] == 'e')
708 AUXENT
*auxent
= (AUXENT
*)((*(--last_block
))+1);
709 auxent
->x_sym
.x_fcnary
.x_fcn
.x_endndx
=
710 native_index
+ syment
->n_numaux
+ 1;
712 if (syment
->n_sclass
== C_EXT
713 && !ISFCN(syment
->n_type
)
714 && first_time
== true
715 && last_file
!= (SYMENT
*)NULL
) {
716 /* This is the first external symbol seen which isn't a
717 function place it in the last .file entry */
718 last_file
->n_value
= native_index
;
722 if (syment
->n_sclass
== C_LEAFPROC
&& syment
->n_numaux
== 2) {
723 AUXENT
*auxent
= (AUXENT
*)(syment
+2);
724 /* This is the definition of a leaf proc, we'll relocate the
727 auxent
->x_bal
.x_balntry
+=
728 coff_symbol_ptr
->symbol
.section
->output_offset
+
729 coff_symbol_ptr
->symbol
.section
->output_section
->vma
;
732 /* If this symbol needs to be tied up then remember some facts */
733 if (syment
->n_sclass
== C_FILE
)
737 if (syment
->n_numaux
!= 0) {
739 If this symbol would like to point to something in the
740 future then remember where it is
742 if (uses_x_sym_x_tagndx_p(syment
)) {
744 If this is a ref to a structure then we'll tie it up
745 now - there are never any forward refs for one
747 if (syment
->n_sclass
== C_STRTAG
||
748 syment
->n_sclass
== C_ENTAG
||
749 syment
->n_sclass
== C_UNTAG
) {
750 last_tagndx
= syment
;
754 This is a ref to a structure - the structure must
755 have been defined within the same file, and previous
756 to this point, so we can deduce the new tagndx
759 AUXENT
*auxent
= (AUXENT
*)(syment
+1);
760 bfd
*bfd_ptr
= coff_symbol_ptr
->symbol
.the_bfd
;
761 SYMENT
*base
= obj_raw_syments(bfd_ptr
);
762 auxent
->x_sym
.x_tagndx
= base
[auxent
->x_sym
.x_tagndx
].n_offset
;
766 if (ISFCN(syment
->n_type
)) {
769 if (syment
->n_sclass
== C_BLOCK
770 && coff_symbol_ptr
->symbol
.name
[1] == 'b')
772 *last_block
++ = syment
;
775 syment
->n_offset
= native_index
;
776 native_index
= native_index
+ 1 + syment
->n_numaux
;
789 coff_write_symbols(abfd
)
793 unsigned int limit
= bfd_get_symcount(abfd
);
794 unsigned int written
= 0;
797 unsigned int string_size
= 0;
800 /* Seek to the right place */
801 bfd_seek(abfd
, obj_sym_filepos(abfd
), SEEK_SET
);
803 /* Output all the symbols we have */
806 for (p
= abfd
->outsymbols
, i
= 0; i
< limit
; i
++, p
++) {
807 asymbol
*symbol
= *p
;
808 coff_symbol_type
*c_symbol
= coff_symbol_from(abfd
, symbol
);
812 if (c_symbol
== (coff_symbol_type
*) NULL
||
813 c_symbol
->native
== (SYMENT
*) NULL
) {
815 This symbol has been created by the loader, or come from a non
816 coff format. It has no native element to inherit, make our
821 native
->n_type
= T_NULL
;
825 if (symbol
->flags
& BSF_ABSOLUTE
) {
826 native
->n_scnum
= N_ABS
;
827 native
->n_value
= symbol
->value
;
829 else if (symbol
->flags
& (BSF_UNDEFINED
| BSF_FORT_COMM
)) {
830 native
->n_scnum
= N_UNDEF
;
831 native
->n_value
= symbol
->value
;
833 else if (symbol
->flags
& BSF_DEBUGGING
) {
835 remove name so it doesn't take up any space
838 #if 0 /* FIXME -- Steve hasn't decided what to do
841 Don't do anything with debugs from the loader
843 native
->n_scnum
= N_DEBUG
;
848 native
->n_scnum
= symbol
->section
->output_section
->index
+ 1;
849 native
->n_value
= symbol
->value
+
850 symbol
->section
->output_section
->vma
+
851 symbol
->section
->output_offset
;
853 /* Copy the any flags from the the file hdr into the symbol */
855 coff_symbol_type
*c
= coff_symbol_from(abfd
, symbol
);
856 if (c
!= (coff_symbol_type
*)NULL
) {
857 native
->n_flags
= c
->symbol
.the_bfd
->flags
;
874 native
->n_sclass
= C_EXT
;
875 native
->n_numaux
= 0;
879 Does this symbol have an ascociated line number - if so then
880 make it remember this symbol index. Also tag the auxent of
881 this symbol to point to the right place in the lineno table
884 alent
*lineno
= c_symbol
->lineno
;
885 native
= c_symbol
->native
;
887 unsigned int count
= 0;
888 lineno
[count
].u
.offset
= written
;
889 if (native
->n_numaux
) {
890 union auxent
*a
= (union auxent
*) (native
+ 1);
891 a
->x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
=
892 c_symbol
->symbol
.section
->output_section
->moving_line_filepos
;
895 And count and relocate all other linenumbers
898 while (lineno
[count
].line_number
) {
899 lineno
[count
].u
.offset
+=
900 c_symbol
->symbol
.section
->output_section
->vma
+
901 c_symbol
->symbol
.section
->output_offset
;
904 c_symbol
->symbol
.section
->output_section
->moving_line_filepos
+=
905 count
* sizeof(struct lineno
);
908 } /* if symbol new to coff */
910 /* Fix the symbol names */
912 unsigned int name_length
;
913 if (symbol
->name
== (char *) NULL
) {
915 coff symbols always have names, so we'll make one up
917 symbol
->name
= "strange";
919 name_length
= strlen(symbol
->name
);
920 if (name_length
<= SYMNMLEN
) {
921 /* This name will fit into the symbol neatly */
922 strncpy(native
->n_name
, symbol
->name
, SYMNMLEN
);
925 native
->n_offset
= string_size
+ 4;
926 native
->n_zeroes
= 0;
927 string_size
+= name_length
+ 1;
930 unsigned int numaux
= native
->n_numaux
;
931 int type
= native
->n_type
;
932 int class = native
->n_sclass
;
933 bfd_coff_swap_sym(abfd
, native
);
934 bfd_write((PTR
) native
, 1, SYMESZ
, abfd
);
935 for (j
= 0; j
!= native
->n_numaux
; j
++) {
936 bfd_coff_swap_aux(abfd
, (AUXENT
*)(native
+ j
+ 1), type
, class);
937 bfd_write((PTR
) (native
+ j
+ 1), 1, AUXESZ
, abfd
);
941 Reuse somewhere in the symbol to keep the index
943 set_index(symbol
, written
);
944 written
+= 1 + numaux
;
947 } /* for each out symbol */
949 bfd_get_symcount(abfd
) = written
;
950 /* Now write out strings */
953 unsigned int size
= string_size
+ 4;
954 bfd_h_put_x(abfd
, size
, &size
);
955 bfd_write((PTR
) &size
, 1, sizeof(size
), abfd
);
956 for (p
= abfd
->outsymbols
, i
= 0; i
< limit
; i
++, p
++) {
958 size_t name_length
= strlen(q
->name
);
959 if (name_length
> SYMNMLEN
) {
960 bfd_write((PTR
) (q
->name
), 1, name_length
+ 1, abfd
);
965 /* We would normally not write anything here, but we'll write
966 out 4 so that any stupid coff reader which tries to read
967 the string table even when there isn't one won't croak.
970 uint32e_type size
= 4;
971 bfd_h_put_x(abfd
, size
, &size
);
972 bfd_write((PTR
)&size
, 1, sizeof(size
), abfd
);
979 coff_write_relocs(abfd
)
983 for (s
= abfd
->sections
; s
!= (asection
*) NULL
; s
= s
->next
) {
985 arelent
**p
= s
->orelocation
;
986 bfd_seek(abfd
, s
->rel_filepos
, SEEK_SET
);
987 for (i
= 0; i
< s
->reloc_count
; i
++) {
990 memset((PTR
)&n
, 0, sizeof(n
));
991 n
.r_vaddr
= q
->address
+ s
->vma
;
992 if (q
->sym_ptr_ptr
) {
993 n
.r_symndx
= get_index((*(q
->sym_ptr_ptr
)));
995 n
.r_type
= q
->howto
->type
;
996 swap_reloc(abfd
, &n
);
997 bfd_write((PTR
) &n
, 1, RELSZ
, abfd
);
1002 coff_write_linenumbers(abfd
)
1006 for (s
= abfd
->sections
; s
!= (asection
*) NULL
; s
= s
->next
) {
1007 if (s
->lineno_count
) {
1008 asymbol
**q
= abfd
->outsymbols
;
1009 bfd_seek(abfd
, s
->line_filepos
, SEEK_SET
);
1010 /* Find all the linenumbers in this section */
1013 alent
*l
= BFD_SEND(p
->the_bfd
, _get_lineno
, (p
->the_bfd
, p
));
1015 /* Found a linenumber entry, output */
1017 bzero( (PTR
)&out
, sizeof(out
));
1019 out
.l_addr
.l_symndx
= l
->u
.offset
;
1020 bfd_coff_swap_lineno(abfd
, &out
);
1021 bfd_write((PTR
) &out
, 1, LINESZ
, abfd
);
1023 while (l
->line_number
) {
1024 out
.l_lnno
= l
->line_number
;
1025 out
.l_addr
.l_symndx
= l
->u
.offset
;
1026 bfd_coff_swap_lineno(abfd
, &out
);
1027 bfd_write((PTR
) &out
, 1, LINESZ
, abfd
);
1039 coff_make_empty_symbol(abfd
)
1042 coff_symbol_type
*new = (coff_symbol_type
*) bfd_alloc(abfd
, sizeof(coff_symbol_type
));
1044 bfd_error
= no_memory
;
1048 new->lineno
= (alent
*) NULL
;
1049 new->symbol
.the_bfd
= abfd
;
1050 return &new->symbol
;
1054 coff_print_symbol(ignore_abfd
, file
, symbol
, how
)
1058 bfd_print_symbol_enum_type how
;
1061 case bfd_print_symbol_name_enum
:
1062 fprintf(file
, "%s", symbol
->name
);
1064 case bfd_print_symbol_type_enum
:
1065 fprintf(file
, "coff %lx %lx", (unsigned long) coffsymbol(symbol
)->native
,
1066 (unsigned long) coffsymbol(symbol
)->lineno
);
1068 case bfd_print_symbol_all_enum
:
1070 CONST
char *section_name
= symbol
->section
== (asection
*) NULL
?
1071 "*abs" : symbol
->section
->name
;
1072 bfd_print_symbol_vandf((PTR
) file
, symbol
);
1074 fprintf(file
, " %-5s %s %s %s",
1076 coffsymbol(symbol
)->native
? "n" : "g",
1077 coffsymbol(symbol
)->lineno
? "l" : " ",
1086 coff_get_lineno(ignore_abfd
, symbol
)
1090 return coffsymbol(symbol
)->lineno
;
1093 Set flags and magic number of a coff file from architecture and machine
1094 type. Result is true if we can represent the arch&type, false if not.
1097 coff_set_flags(abfd
, magicp
, flagsp
)
1104 switch (abfd
->obj_arch
) {
1112 *magicp
= I960ROMAGIC
;
1114 ((bfd_get_file_flags(abfd) & WP_TEXT) ? I960ROMAGIC :
1115 I960RWMAGIC); FIXME???
1117 switch (abfd
->obj_machine
) {
1118 case bfd_mach_i960_core
:
1121 case bfd_mach_i960_kb_sb
:
1124 case bfd_mach_i960_mc
:
1127 case bfd_mach_i960_xa
:
1130 case bfd_mach_i960_ca
:
1133 case bfd_mach_i960_ka_sa
:
1146 *magicp
= MC68MAGIC
;
1151 *magicp
= MC88OMAGIC
;
1156 default: /* Unknown architecture */
1165 coff_set_arch_mach(abfd
, arch
, machine
)
1167 enum bfd_architecture arch
;
1168 unsigned long machine
;
1172 abfd
->obj_arch
= arch
;
1173 abfd
->obj_machine
= machine
;
1174 if (arch
!= bfd_arch_unknown
&&
1175 coff_set_flags(abfd
, &dummy1
, &dummy2
) != true)
1176 return false; /* We can't represent this type */
1177 return true; /* We're easy ... */
1181 /* Calculate the file position for each section. */
1184 coff_compute_section_file_positions(abfd
)
1188 file_ptr sofar
= FILHSZ
;
1189 if (bfd_get_start_address(abfd
)) {
1191 A start address may have been added to the original file. In this
1192 case it will need an optional header to record it.
1194 abfd
->flags
|= EXEC_P
;
1196 if (abfd
->flags
& EXEC_P
)
1200 sofar
+= abfd
->section_count
* SCNHSZ
;
1202 for (current
= abfd
->sections
; current
!= NULL
; current
= current
->next
) {
1203 current
->filepos
= sofar
;
1204 /* Only add sections which have contents */
1205 if (current
->flags
& SEC_HAS_CONTENTS
)
1206 sofar
+= current
->size
;
1208 obj_relocbase(abfd
) = sofar
;
1217 coff_write_object_contents(abfd
)
1220 struct filehdr file_header
;
1222 boolean hasrelocs
= false;
1223 boolean haslinno
= false;
1224 file_ptr reloc_base
;
1225 file_ptr lineno_base
;
1229 unsigned long reloc_size
= 0;
1230 unsigned long lnno_size
= 0;
1231 asection
*text_sec
= NULL
;
1232 asection
*data_sec
= NULL
;
1233 asection
*bss_sec
= NULL
;
1236 bfd_error
= system_call_error
;
1239 if(abfd
->output_has_begun
== false) {
1240 coff_compute_section_file_positions(abfd
);
1242 scn_base
= (file_ptr
) (sizeof(struct filehdr
)
1243 + ((abfd
->flags
& EXEC_P
) ? sizeof(AOUTHDR
) : 0));
1245 if (bfd_seek(abfd
, scn_base
, SEEK_SET
) != 0)
1247 reloc_base
= obj_relocbase(abfd
);
1254 Make a pass through the symbol table to count line number entries and
1255 put them into the correct asections
1257 coff_count_linenumbers(abfd
);
1258 data_base
= scn_base
;
1259 /* Work out the size of the reloc and linno areas */
1261 for (current
= abfd
->sections
; current
!= NULL
; current
= current
->next
) {
1262 reloc_size
+= current
->reloc_count
* sizeof(struct reloc
);
1263 lnno_size
+= current
->lineno_count
* sizeof(struct lineno
);
1264 data_base
+= sizeof(struct scnhdr
);
1268 lineno_base
= reloc_base
+ reloc_size
;
1269 sym_base
= lineno_base
+ lnno_size
;
1271 /* Indicate in each section->line_filepos its actual file address */
1272 for (current
= abfd
->sections
; current
!= NULL
; current
= current
->next
) {
1273 if (current
->lineno_count
) {
1274 current
->line_filepos
= lineno_base
;
1275 current
->moving_line_filepos
= lineno_base
;
1276 lineno_base
+= current
->lineno_count
* sizeof(struct lineno
);
1280 current
->line_filepos
= 0;
1282 if (current
->reloc_count
) {
1283 current
->rel_filepos
= reloc_base
;
1284 reloc_base
+= current
->reloc_count
* sizeof(struct reloc
);
1287 current
->rel_filepos
= 0;
1294 (file_ptr
) ((abfd
->flags
& EXEC_P
) ?
1295 (FILHSZ
+ AOUTSZ
) : FILHSZ
),
1299 unsigned int pad
= abfd
->flags
& D_PAGED
? data_base
: 0;
1301 unsigned int pad
= 0;
1302 for (current
= abfd
->sections
; current
!= NULL
; current
= current
->next
) {
1304 strncpy(&(section
.s_name
[0]), current
->name
, 8);
1305 section
.s_vaddr
= current
->vma
+ pad
;
1306 section
.s_paddr
= current
->vma
+ pad
;
1307 section
.s_size
= current
->size
- pad
;
1309 If this section has no size or is unloadable then the scnptr
1312 if (current
->size
- pad
== 0 ||
1313 (current
->flags
& SEC_LOAD
) == 0) {
1314 section
.s_scnptr
= 0;
1318 section
.s_scnptr
= current
->filepos
;
1320 section
.s_relptr
= current
->rel_filepos
;
1321 section
.s_lnnoptr
= current
->line_filepos
;
1322 section
.s_nreloc
= current
->reloc_count
;
1323 section
.s_nlnno
= current
->lineno_count
;
1324 if (current
->reloc_count
!= 0)
1326 if (current
->lineno_count
!= 0)
1329 if (!strcmp(current
->name
, _TEXT
)) {
1331 section
.s_flags
= STYP_TEXT
; /* kind stupid optimisation */
1335 if (!strcmp(current
->name
, _DATA
)) {
1337 section
.s_flags
= STYP_DATA
; /* kind stupid
1340 else if (!strcmp(current
->name
, _BSS
)) {
1342 section
.s_flags
= STYP_BSS
; /* kind stupid optimisation */
1348 section
.s_align
= (current
->alignment_power
1349 ? 1 << current
->alignment_power
1353 swap_scnhdr(abfd
, §ion
);
1354 bfd_write((PTR
) (§ion
), 1, SCNHSZ
, abfd
);
1359 /* OK, now set up the filehdr... */
1361 bfd_h_put_x(abfd
, abfd
->section_count
, &file_header
.f_nscns
);
1363 We will NOT put a fucking timestamp in the header here. Every time you
1364 put it back, I will come in and take it out again. I'm sorry. This
1365 field does not belong here. We fill it with a 0 so it compares the
1366 same but is not a reasonable time. -- gnu@cygnus.com
1369 Well, I like it, so I'm conditionally compiling it in.
1372 #ifdef COFF_TIMESTAMP
1373 bfd_h_put_x(abfd
, time(0), &file_header
.f_timdat
);
1375 bfd_h_put_x(abfd
, 0, &file_header
.f_timdat
);
1378 if (bfd_get_symcount(abfd
) != 0)
1379 bfd_h_put_x(abfd
, sym_base
, &file_header
.f_symptr
);
1381 bfd_h_put_x(abfd
, 0, &file_header
.f_symptr
);
1383 file_header
.f_flags
= 0;
1385 if (abfd
->flags
& EXEC_P
)
1386 bfd_h_put_x(abfd
, sizeof(AOUTHDR
), &file_header
.f_opthdr
);
1388 bfd_h_put_x(abfd
, 0, &file_header
.f_opthdr
);
1391 file_header
.f_flags
|= F_RELFLG
;
1393 file_header
.f_flags
|= F_LNNO
;
1394 if (0 == bfd_get_symcount(abfd
))
1395 file_header
.f_flags
|= F_LSYMS
;
1396 if (abfd
->flags
& EXEC_P
)
1397 file_header
.f_flags
|= F_EXEC
;
1398 if (!abfd
->xvec
->byteorder_big_p
)
1399 file_header
.f_flags
|= F_AR32WR
;
1401 FIXME, should do something about the other byte orders and
1405 /* Set up architecture-dependent stuff */
1409 coff_set_flags(abfd
, &magic
, &flags
);
1410 file_header
.f_flags
|= flags
;
1412 bfd_h_put_x(abfd
, magic
, &file_header
.f_magic
);
1413 bfd_h_put_x(abfd
, file_header
.f_flags
, &file_header
.f_flags
);
1415 /* ...and the "opt"hdr... */
1417 bfd_h_put_x(abfd
, (magic
== I960ROMAGIC
? NMAGIC
: OMAGIC
),
1418 &(exec_hdr(abfd
)->magic
));
1421 exec_hdr(abfd
)->magic
= PAGEMAGIC3
;
1424 /* Now should write relocs, strings, syms */
1425 obj_sym_filepos(abfd
) = sym_base
;
1427 if (bfd_get_symcount(abfd
) != 0) {
1428 coff_mangle_symbols(abfd
);
1429 coff_write_symbols(abfd
);
1430 coff_write_linenumbers(abfd
);
1431 coff_write_relocs(abfd
);
1434 bfd_h_put_x(abfd
, text_sec
->size
, &exec_hdr(abfd
)->tsize
);
1436 text_sec
->size
? text_sec
->vma
: 0,
1437 &exec_hdr(abfd
)->text_start
);
1440 bfd_h_put_x(abfd
, data_sec
->size
, &exec_hdr(abfd
)->dsize
);
1442 data_sec
->size
? data_sec
->vma
: 0,
1443 &exec_hdr(abfd
)->data_start
);
1446 bfd_h_put_x(abfd
, bss_sec
->size
, &exec_hdr(abfd
)->bsize
);
1448 bfd_h_put_x(abfd
, bfd_get_start_address(abfd
), &exec_hdr(abfd
)->entry
);
1451 /* now write them */
1452 bfd_h_put_x(abfd
, bfd_get_symcount(abfd
), &file_header
.f_nsyms
);
1453 if (bfd_seek(abfd
, 0L, SEEK_SET
) != 0)
1456 bfd_write((PTR
) &file_header
, 1, FILHSZ
, abfd
);
1458 if (abfd
->flags
& EXEC_P
) {
1460 bfd_write((PTR
) exec_hdr(abfd
), 1, AOUTSZ
, abfd
);
1468 coff_set_section_contents(abfd
, section
, location
, offset
, count
)
1475 if (abfd
->output_has_begun
== false) /* set by bfd.c handler */
1476 coff_compute_section_file_positions(abfd
);
1478 bfd_seek(abfd
, (file_ptr
) (section
->filepos
+ offset
), SEEK_SET
);
1481 return (bfd_write(location
, 1, count
, abfd
) == count
) ? true : false;
1486 coff_get_section_contents(abfd
, section
, location
, offset
, count
)
1494 || offset
>= section
->size
1495 || bfd_seek(abfd
, section
->filepos
+ offset
, SEEK_SET
) == -1
1496 || bfd_read(location
, 1, count
, abfd
) != count
) {
1500 } /* coff_get_section_contents() */
1504 coff_close_and_cleanup(abfd
)
1507 if (!bfd_read_p(abfd
))
1508 switch (abfd
->format
) {
1510 if (!_bfd_write_archive_contents(abfd
))
1514 if (!coff_write_object_contents(abfd
))
1518 bfd_error
= invalid_operation
;
1522 #define cleaner(ptr) if (abfd->ptr) free (abfd->ptr)
1525 if (abfd
->my_archive
)
1538 buy_and_read(abfd
, where
, seek_direction
, size
)
1544 PTR area
= (PTR
) bfd_alloc(abfd
, size
);
1546 bfd_error
= no_memory
;
1549 bfd_seek(abfd
, where
, seek_direction
);
1550 if (bfd_read(area
, 1, size
, abfd
) != size
) {
1551 bfd_error
= system_call_error
;
1555 } /* buy_and_read() */
1558 offset_symbol_indices(symtab
, count
, offset
)
1560 unsigned long count
;
1563 SYMENT
*end
= symtab
+ count
;
1564 for (; symtab
< end
; ++symtab
) {
1565 if (symtab
->n_sclass
== C_FILE
) {
1566 symtab
->n_value
= 0;
1568 else if (symtab
->n_sclass
== C_ALIAS
) {
1570 These guys have indices in their values.
1572 symtab
->n_value
+= offset
;
1575 else if (symtab
->n_numaux
) {
1577 anybody else without an aux, has no indices.
1580 if (symtab
->n_sclass
== C_EOS
1581 || (BTYPE(symtab
->n_type
) == T_STRUCT
1582 && symtab
->n_sclass
!= C_STRTAG
)
1583 || BTYPE(symtab
->n_type
) == T_UNION
1584 || BTYPE(symtab
->n_type
) == T_ENUM
) {
1585 /* If the tagndx is 0 then the struct hasn't really been
1586 defined, so leave it alone */
1588 if(((AUXENT
*) (symtab
+ 1))->x_sym
.x_tagndx
!= 0) {
1589 ((AUXENT
*) (symtab
+ 1))->x_sym
.x_tagndx
+= offset
;
1592 } /* These guys have a tagndx */
1593 if (symtab
->n_sclass
== C_STRTAG
1594 || symtab
->n_sclass
== C_UNTAG
1595 || symtab
->n_sclass
== C_ENTAG
1596 || symtab
->n_sclass
== C_BLOCK
1597 || symtab
->n_sclass
== C_FCN
1598 || ISFCN(symtab
->n_type
)) {
1600 ((AUXENT
*) (symtab
+ 1))->x_sym
.x_fcnary
.x_fcn
.x_endndx
+= offset
;
1602 } /* These guys have an endndx */
1604 if (ISFCN(symtab
->n_type
)) {
1605 ((AUXENT
*) (symtab
+ 1))->x_sym
.x_tvndx
+= offset
;
1606 } /* These guys have a tvndx. I think...
1608 #endif /* Not I960 */
1610 } /* if value, else if aux */
1611 symtab
+= symtab
->n_numaux
;
1612 } /* walk the symtab */
1615 } /* offset_symbol_indices() */
1616 /* swap the entire symbol table */
1618 swap_raw_symtab(abfd
, raw_symtab
)
1623 SYMENT
*end
= raw_symtab
+ bfd_get_symcount(abfd
);
1624 for (; raw_symtab
< end
; ++raw_symtab
) {
1625 bfd_coff_swap_sym(abfd
, raw_symtab
);
1627 for (i
= raw_symtab
->n_numaux
; i
; --i
, ++raw_symtab
) {
1628 bfd_coff_swap_aux(abfd
,
1629 (AUXENT
*)(raw_symtab
+ 1),
1631 raw_symtab
->n_sclass
);
1632 } /* swap all the aux entries */
1633 } /* walk the symbol table */
1636 } /* swap_raw_symtab() */
1638 read a symbol table into freshly mallocated memory, swap it, and knit the
1639 symbol names into a normalized form. By normalized here I mean that all
1640 symbols have an n_offset pointer that points to a NULL terminated string.
1641 Oh, and the first symbol MUST be a C_FILE. If there wasn't one there
1642 before, put one there.
1646 get_normalized_symtab(abfd
)
1652 char *string_table
= NULL
;
1654 unsigned long string_table_size
= 0;
1657 if ((size
= bfd_get_symcount(abfd
) * sizeof(SYMENT
)) == 0) {
1658 bfd_error
= no_symbols
;
1662 This is a hack. Some tool chains fail to put a C_FILE symbol at the
1663 beginning of the symbol table. To make life simpler for our users, we
1664 inject one if it wasn't there originally.
1666 We'd like to keep all of this bfd's native symbols in one block to keep
1667 table traversals simple. To do that, we need to know whether we will
1668 be prepending the C_FILE symbol before we read the rest of the table.
1670 if ((s
= (SYMENT
*) bfd_alloc(abfd
, sizeof(SYMENT
) * 2)) == NULL
) {
1671 bfd_error
= no_memory
;
1674 if (bfd_seek(abfd
, obj_sym_filepos(abfd
), SEEK_SET
) == -1
1675 || bfd_read(s
, sizeof(SYMENT
), 1, abfd
) != sizeof(SYMENT
)) {
1676 bfd_error
= system_call_error
;
1679 bfd_coff_swap_sym(abfd
, s
);
1681 if (s
->n_sclass
== C_FILE
) {
1682 obj_symbol_slew(abfd
) = 0;
1684 if ((retval
= (SYMENT
*) bfd_alloc(abfd
, size
)) == NULL
) {
1685 bfd_error
= no_memory
;
1687 } /* on malloc error */
1690 unsigned long namelength
= 0;
1691 CONST
char *filename
;
1692 obj_symbol_slew(abfd
) = 2;
1694 if ((retval
= (SYMENT
*) bfd_alloc(abfd
, size
1695 + (obj_symbol_slew(abfd
)
1696 * sizeof(SYMENT
)))) == NULL
) {
1697 bfd_error
= no_memory
;
1699 } /* on malloc error */
1700 bzero((char *) retval
, size
+ (obj_symbol_slew(abfd
) * sizeof(SYMENT
)));
1702 #define FILE_ENTRY_NAME ".file"
1704 if ((retval
->n_offset
= (int) bfd_alloc(abfd
, strlen(FILE_ENTRY_NAME
)
1706 bfd_error
= no_memory
;
1708 } /* on malloc error */
1709 strcpy((char *) retval
->n_offset
, FILE_ENTRY_NAME
);
1710 retval
->n_sclass
= C_FILE
;
1711 retval
->n_scnum
= N_DEBUG
;
1712 retval
->n_numaux
= 1;
1713 retval
->n_value
= 2; /* Say that externals follow */
1715 #undef FILE_ENTRY_NAME
1717 if ((filename
= bfd_get_filename(abfd
)) == NULL
) {
1719 } /* if we know it's name */
1720 if ((namelength
= strlen(filename
)) <= FILNMLEN
) {
1721 strncpy(((AUXENT
*) (retval
+ 1))->x_file
.x_fname
, filename
, FILNMLEN
);
1724 if ((((AUXENT
*) (retval
+ 1))->x_file
.x_n
.x_offset
1725 = (int) bfd_alloc(abfd
, namelength
+1)) == NULL
) {
1726 bfd_error
= no_memory
;
1729 strcpy((char *) (((AUXENT
*) (retval
+ 1))->x_file
.x_n
.x_offset
),
1732 } /* if "short" name */
1733 } /* missing file entry. */
1737 if (bfd_seek(abfd
, obj_sym_filepos(abfd
), SEEK_SET
) == -1
1738 || bfd_read(retval
+ obj_symbol_slew(abfd
), size
, 1, abfd
) != size
) {
1739 bfd_error
= system_call_error
;
1741 } /* on read error */
1742 /* mark the end of the symbols */
1743 end
= retval
+ obj_symbol_slew(abfd
) + bfd_get_symcount(abfd
);
1745 FIXME SOMEDAY. A string table size of zero is very weird, but
1746 probably possible. If one shows up, it will probably kill us.
1749 swap_raw_symtab(abfd
, retval
+ obj_symbol_slew(abfd
));
1751 /* ...and normalize symbol names. */
1753 for (s
= retval
+ obj_symbol_slew(abfd
); s
< end
; ++s
) {
1754 if (s
->n_zeroes
!= 0) {
1756 This is a "short" name. Make it long.
1758 unsigned long i
= 0;
1759 char *newstring
= NULL
;
1761 find the length of this string without walking into memory
1765 for (i
= 0; i
< 8; ++i
) {
1766 if (s
->n_name
[i
] == '\0') {
1768 } /* if end of string */
1769 } /* possible lengths of this string. */
1771 if ((newstring
= (PTR
) bfd_alloc(abfd
, ++i
)) == NULL
) {
1772 bfd_error
= no_memory
;
1775 bzero(newstring
, i
);
1776 strncpy(newstring
, s
->n_name
, i
-1);
1777 s
->n_offset
= (int) newstring
;
1782 if (string_table
== NULL
) {
1784 NOTE: we don't read the string table until now because we
1785 don't necessarily know that we have one until now.
1788 At this point we should be "seek"'d to the end of the
1789 symbols === the symbol table size.
1792 if (bfd_read((char *) &string_table_size
, sizeof(string_table_size
), 1, abfd
) != sizeof(string_table_size
)) {
1793 bfd_error
= system_call_error
;
1796 sp(string_table_size
);
1798 if ((string_table
= (PTR
) bfd_alloc(abfd
, string_table_size
-= 4)) == NULL
) {
1799 bfd_error
= no_memory
;
1801 } /* on mallocation error */
1802 if (bfd_read(string_table
, string_table_size
, 1, abfd
) != string_table_size
) {
1803 bfd_error
= system_call_error
;
1806 } /* have not yet read the string table. */
1808 This is a long name already. Just point it at the string in
1811 s
->n_offset
= (int) (string_table
- 4 + s
->n_offset
);
1812 } /* switch on type of symbol name */
1815 } /* for each symbol */
1817 If we had to insert a C_FILE symbol, then everybody's indices are off
1818 by 2, so correct them.
1821 if (obj_symbol_slew(abfd
) > 0) {
1822 offset_symbol_indices(retval
+ 2, bfd_get_symcount(abfd
), 2);
1824 /* and let the world know there are two more of them. */
1825 bfd_get_symcount(abfd
) += 2;
1828 } /* if we added a C_FILE */
1831 /* I'm not sure of the repercussions of this, so the Intel
1832 folks will always to the force
1834 if (obj_symbol_slew(abfd
) > 0)
1835 force_indices_file_symbol_relative(abfd
, retval
);
1837 force_indices_file_symbol_relative(abfd
, retval
);
1840 obj_raw_syments(abfd
) = retval
;
1841 obj_string_table(abfd
) = string_table
;
1846 } /* get_normalized_symtab() */
1850 section_from_bfd_index(abfd
, index
)
1855 struct sec_struct
*answer
= abfd
->sections
;
1857 answer
= answer
->next
;
1869 coff_slurp_line_table(abfd
, asect
)
1873 struct lineno
*native_lineno
;
1874 alent
*lineno_cache
;
1875 BFD_ASSERT(asect
->lineno
== (alent
*) NULL
);
1878 native_lineno
= (struct lineno
*) buy_and_read(abfd
,
1879 asect
->line_filepos
,
1881 (size_t) (sizeof(struct lineno
) *
1882 asect
->lineno_count
));
1884 (alent
*) bfd_alloc(abfd
, (size_t) ((asect
->lineno_count
+ 1) * sizeof(alent
)));
1885 if (lineno_cache
== NULL
) {
1886 bfd_error
= no_memory
;
1887 return (BFD_FAILURE
);
1889 unsigned int counter
= 0;
1890 alent
*cache_ptr
= lineno_cache
;
1891 struct lineno
*src
= native_lineno
;
1892 while (counter
< asect
->lineno_count
) {
1893 bfd_coff_swap_lineno(abfd
, src
);
1894 cache_ptr
->line_number
= src
->l_lnno
;
1896 if (cache_ptr
->line_number
== 0) {
1897 coff_symbol_type
*sym
=
1898 (coff_symbol_type
*) (src
->l_addr
.l_symndx
1899 + obj_symbol_slew(abfd
)
1900 + obj_raw_syments(abfd
))->n_zeroes
;
1901 cache_ptr
->u
.sym
= (asymbol
*) sym
;
1902 sym
->lineno
= cache_ptr
;
1905 cache_ptr
->u
.offset
= src
->l_addr
.l_paddr
1906 - bfd_section_vma(abfd
, asect
);
1907 } /* If no linenumber expect a symbol index */
1913 cache_ptr
->line_number
= 0;
1916 asect
->lineno
= lineno_cache
;
1918 } /* coff_slurp_line_table() */
1921 find_next_file_symbol(current
, end
)
1925 unsigned int next
= current
->n_value
;
1926 current
+= current
->n_numaux
+ 1;
1928 while (current
< end
) {
1929 if (current
->n_sclass
== C_FILE
) {
1932 current
+= current
->n_numaux
+ 1;
1940 Note that C_FILE symbols can, and some do, have more than 1 aux entry.
1944 DEFUN(force_indices_file_symbol_relative
,(abfd
, symtab
),
1948 SYMENT
*end
= symtab
+ bfd_get_symcount(abfd
);
1951 /* the first symbol had damn well better be a C_FILE. */
1952 BFD_ASSERT(symtab
->n_sclass
== C_FILE
);
1954 for (current
= find_next_file_symbol(symtab
, end
);
1957 offset_symbol_indices(current
,
1959 find_next_file_symbol(current
,
1962 } /* walk the table */
1965 } /* force_indices_file_symbol_relative() */
1968 coff_slurp_symbol_table(abfd
)
1971 SYMENT
*native_symbols
;
1972 coff_symbol_type
*cached_area
;
1973 unsigned int *table_ptr
;
1975 unsigned int number_of_symbols
= 0;
1976 if (obj_symbols(abfd
))
1978 bfd_seek(abfd
, obj_sym_filepos(abfd
), SEEK_SET
);
1980 /* Read in the symbol table */
1981 if ((native_symbols
= get_normalized_symtab(abfd
)) == NULL
) {
1986 /* Allocate enough room for all the symbols in cached form */
1988 (coff_symbol_type
*)
1989 bfd_alloc(abfd
, (size_t) (bfd_get_symcount(abfd
) * sizeof(coff_symbol_type
)));
1991 if (cached_area
== NULL
) {
1992 bfd_error
= no_memory
;
1993 return (BFD_FAILURE
);
1997 bfd_alloc(abfd
, (size_t) (bfd_get_symcount(abfd
) * sizeof(unsigned int)));
1999 if (table_ptr
== NULL
) {
2000 bfd_error
= no_memory
;
2001 return (BFD_FAILURE
);
2003 coff_symbol_type
*dst
= cached_area
;
2004 unsigned int last_native_index
= bfd_get_symcount(abfd
);
2005 unsigned int this_index
= 0;
2006 while (this_index
< last_native_index
) {
2007 SYMENT
*src
= native_symbols
+ this_index
;
2008 table_ptr
[this_index
] = number_of_symbols
;
2009 dst
->symbol
.the_bfd
= abfd
;
2011 dst
->symbol
.name
= (char *) src
->n_offset
; /* which was normalized
2013 terminated string. */
2015 We use the native name field to point to the cached field
2017 src
->n_zeroes
= (int) dst
;
2018 dst
->symbol
.section
= section_from_bfd_index(abfd
, src
->n_scnum
);
2020 switch (src
->n_sclass
) {
2024 dst
->symbol
.value
= src
->n_value
- dst
->symbol
.section
->vma
;
2025 dst
->symbol
.flags
= BSF_EXPORT
| BSF_GLOBAL
;
2026 dst
->symbol
.flags
|= BSF_NOT_AT_END
;
2028 /* Fall through to next case */
2033 if (src
->n_scnum
== 0) {
2034 if (src
->n_value
== 0) {
2035 dst
->symbol
.flags
= BSF_UNDEFINED
;
2038 dst
->symbol
.flags
= BSF_FORT_COMM
;
2039 dst
->symbol
.value
= src
->n_value
;
2044 Base the value as an index from the base of the
2047 if (dst
->symbol
.section
== (asection
*) NULL
) {
2048 dst
->symbol
.flags
= BSF_EXPORT
| BSF_GLOBAL
| BSF_ABSOLUTE
;
2049 dst
->symbol
.value
= src
->n_value
;
2052 dst
->symbol
.flags
= BSF_EXPORT
| BSF_GLOBAL
;
2053 dst
->symbol
.value
= src
->n_value
- dst
->symbol
.section
->vma
;
2055 if (ISFCN(src
->n_type
)) {
2057 A function ext does not go at the end of a file
2059 dst
->symbol
.flags
|= BSF_NOT_AT_END
;
2064 case C_STAT
: /* static */
2066 case C_LEAFSTAT
: /* static leaf procedure */
2068 case C_LABEL
: /* label */
2069 dst
->symbol
.flags
= BSF_LOCAL
;
2071 Base the value as an index from the base of the section
2073 dst
->symbol
.value
= src
->n_value
- dst
->symbol
.section
->vma
;
2076 case C_MOS
: /* member of structure */
2077 case C_EOS
: /* end of structure */
2078 case C_REGPARM
: /* register parameter */
2079 case C_REG
: /* register variable */
2081 case C_AUTOARG
: /* 960-specific storage class */
2083 case C_TPDEF
: /* type definition */
2086 case C_AUTO
: /* automatic variable */
2087 case C_FIELD
: /* bit field */
2088 case C_ENTAG
: /* enumeration tag */
2089 case C_MOE
: /* member of enumeration */
2090 case C_MOU
: /* member of union */
2091 case C_UNTAG
: /* union tag */
2093 dst
->symbol
.flags
= BSF_DEBUGGING
;
2094 dst
->symbol
.value
= src
->n_value
;
2097 case C_FILE
: /* file name */
2098 case C_STRTAG
: /* structure tag */
2099 dst
->symbol
.flags
= BSF_DEBUGGING
;
2100 dst
->symbol
.value
= src
->n_value
;
2103 case C_BLOCK
: /* ".bb" or ".eb" */
2104 case C_FCN
: /* ".bf" or ".ef" */
2105 dst
->symbol
.flags
= BSF_LOCAL
;
2107 Base the value as an index from the base of the section
2109 dst
->symbol
.value
= src
->n_value
- dst
->symbol
.section
->vma
;
2112 case C_EFCN
: /* physical end of function */
2114 case C_EXTDEF
: /* external definition */
2115 case C_ULABEL
: /* undefined label */
2116 case C_USTATIC
: /* undefined static */
2117 case C_LINE
: /* line # reformatted as symbol table entry */
2118 case C_ALIAS
: /* duplicate tag */
2119 case C_HIDDEN
: /* ext symbol in dmert public lib */
2123 printf("SICK%d\n", src
->n_sclass
);
2125 dst
->symbol
.flags
= BSF_DEBUGGING
;
2126 dst
->symbol
.value
= src
->n_value
;
2134 BFD_ASSERT(dst
->symbol
.flags
!= 0);
2138 dst
->symbol
.udata
= 0;
2139 dst
->lineno
= (alent
*) NULL
;
2140 this_index
+= src
->n_numaux
+ 1;
2142 number_of_symbols
++;
2143 } /* walk the native symtab */
2145 } /* bfdize the native symtab */
2147 obj_symbols(abfd
) = cached_area
;
2148 obj_raw_syments(abfd
) = native_symbols
;
2150 bfd_get_symcount(abfd
) = number_of_symbols
;
2151 obj_convert(abfd
) = table_ptr
;
2152 /* Slurp the line tables for each section too */
2157 coff_slurp_line_table(abfd
, p
);
2162 } /* coff_slurp_symbol_table() */
2165 coff_get_symtab_upper_bound(abfd
)
2168 if (!coff_slurp_symbol_table(abfd
))
2171 return (bfd_get_symcount(abfd
) + 1) * (sizeof(coff_symbol_type
*));
2176 coff_get_symtab(abfd
, alocation
)
2178 asymbol
**alocation
;
2180 unsigned int counter
= 0;
2181 coff_symbol_type
*symbase
;
2182 coff_symbol_type
**location
= (coff_symbol_type
**) (alocation
);
2183 if (!coff_slurp_symbol_table(abfd
))
2186 for (symbase
= obj_symbols(abfd
); counter
++ < bfd_get_symcount(abfd
);)
2187 *(location
++) = symbase
++;
2189 return bfd_get_symcount(abfd
);
2193 coff_get_reloc_upper_bound(abfd
, asect
)
2197 if (bfd_get_format(abfd
) != bfd_object
) {
2198 bfd_error
= invalid_operation
;
2201 return (asect
->reloc_count
+ 1) * sizeof(arelent
*);
2206 coff_slurp_reloc_table(abfd
, asect
, symbols
)
2211 struct reloc
*native_relocs
;
2212 arelent
*reloc_cache
;
2213 if (asect
->relocation
)
2215 if (asect
->reloc_count
== 0)
2217 if (!coff_slurp_symbol_table(abfd
))
2220 (struct reloc
*) buy_and_read(abfd
,
2223 (size_t) (sizeof(struct reloc
) *
2224 asect
->reloc_count
));
2225 reloc_cache
= (arelent
*)
2226 bfd_alloc(abfd
, (size_t) (asect
->reloc_count
* sizeof(arelent
)));
2228 if (reloc_cache
== NULL
) {
2229 bfd_error
= no_memory
;
2230 return (BFD_FAILURE
);
2234 for (cache_ptr
= reloc_cache
,
2235 src
= native_relocs
;
2236 cache_ptr
< reloc_cache
+ asect
->reloc_count
;
2240 swap_reloc(abfd
, src
);
2241 src
->r_symndx
+= obj_symbol_slew(abfd
);
2242 cache_ptr
->sym_ptr_ptr
= symbols
+ obj_convert(abfd
)[src
->r_symndx
];
2244 ptr
= *(cache_ptr
->sym_ptr_ptr
);
2245 cache_ptr
->address
= src
->r_vaddr
;
2247 The symbols definitions that we have read in have been
2248 relocated as if their sections started at 0. But the offsets
2249 refering to the symbols in the raw data have not been
2250 modified, so we have to have a negative addend to compensate.
2252 Note that symbols which used to be common must be left alone
2255 if (ptr
->the_bfd
== abfd
2256 && ptr
->section
!= (asection
*) NULL
2257 && ((ptr
->flags
& BSF_OLD_COMMON
)== 0))
2259 cache_ptr
->addend
= -(ptr
->section
->vma
+ ptr
->value
);
2262 cache_ptr
->addend
= 0;
2265 cache_ptr
->address
-= asect
->vma
;
2267 cache_ptr
->section
= (asection
*) NULL
;
2270 cache_ptr
->howto
= howto_table
+ src
->r_type
;
2273 if (src
->r_type
>= R_PCR16L
&& src
->r_type
<= R_VRT32
) {
2274 cache_ptr
->howto
= howto_table
+ src
->r_type
- R_PCR16L
;
2281 cache_ptr
->howto
= howto_table
+ src
->r_type
;
2289 asect
->relocation
= reloc_cache
;
2294 /* This is stupid. This function should be a boolean predicate */
2296 coff_canonicalize_reloc(abfd
, section
, relptr
, symbols
)
2302 arelent
*tblptr
= section
->relocation
;
2303 unsigned int count
= 0;
2304 if (!(tblptr
|| coff_slurp_reloc_table(abfd
, section
, symbols
)))
2306 tblptr
= section
->relocation
;
2310 for (; count
++ < section
->reloc_count
;)
2311 *relptr
++ = tblptr
++;
2315 return section
->reloc_count
;
2323 provided a bfd, a section and an offset into the section, calculate and
2324 return the name of the source file and the line nearest to the wanted
2329 DEFUN(coff_find_nearest_line
,(abfd
,
2337 asection
*section AND
2338 asymbol
**symbols AND
2340 CONST
char **filename_ptr AND
2341 CONST
char **functionname_ptr AND
2342 unsigned int *line_ptr
)
2344 static bfd
*cache_abfd
;
2345 static asection
*cache_section
;
2346 static bfd_vma cache_offset
;
2347 static unsigned int cache_i
;
2348 static alent
*cache_l
;
2351 struct icofdata
*cof
= obj_icof(abfd
);
2352 /* Run through the raw syments if available */
2355 unsigned int line_base
= 0;
2359 *functionname_ptr
= 0;
2362 /* Don't try and find line numbers in a non coff file */
2363 if (abfd
->xvec
->flavour
!= bfd_target_coff_flavour_enum
)
2366 if (cof
== (struct icofdata
*)NULL
)
2370 p
= cof
->raw_syments
;
2372 I don't know for sure what's right, but this isn't it. First off, an
2373 object file may not have any C_FILE's in it. After
2374 get_normalized_symtab(), it should have at least 1, the one I put
2375 there, but otherwise, all bets are off. Point #2, the first C_FILE
2376 isn't necessarily the right C_FILE because any given object may have
2377 many. I think you'll have to track sections as they coelesce in order
2378 to find the C_STAT symbol for this section. Then you'll have to work
2379 backwards to find the previous C_FILE, or choke if you get to a C_STAT
2380 for the same kind of section. That will mean that the original object
2381 file didn't have a C_FILE. xoxorich.
2384 #ifdef WEREBEINGPEDANTIC
2390 for (i
= 0; i
< cof
->raw_syment_count
; i
++) {
2391 if (p
->n_sclass
== C_FILE
) {
2392 /* File name is embeded in auxent */
2394 This isn't right. The fname should probably be normalized
2395 during get_normalized_symtab(). In any case, what was here
2396 wasn't right because a SYMENT.n_name isn't an
2397 AUXENT.x_file.x_fname. xoxorich.
2400 *filename_ptr
= ((AUXENT
*) (p
+ 1))->x_file
.x_fname
;
2403 p
+= 1 + p
->n_numaux
;
2405 /* Now wander though the raw linenumbers of the section */
2411 If this is the same bfd as we were previously called with and this is
2412 the same section, and the offset we want is further down then we can
2413 prime the lookup loop
2415 if (abfd
== cache_abfd
&&
2416 section
== cache_section
&&
2417 offset
>= cache_offset
) {
2423 l
= section
->lineno
;
2426 for (; i
< section
->lineno_count
; i
++) {
2427 if (l
->line_number
== 0) {
2428 /* Get the symbol this line number points at */
2429 coff_symbol_type
*coff
= (coff_symbol_type
*) (l
->u
.sym
);
2430 *functionname_ptr
= coff
->symbol
.name
;
2432 struct syment
*s
= coff
->native
;
2433 s
= s
+ 1 + s
->n_numaux
;
2435 S should now point to the .bf of the function
2439 The linenumber is stored in the auxent
2441 union auxent
*a
= (union auxent
*) (s
+ 1);
2442 line_base
= a
->x_sym
.x_misc
.x_lnsz
.x_lnno
;
2447 if (l
->u
.offset
> offset
)
2449 *line_ptr
= l
->line_number
+ line_base
+ 1;
2455 cache_section
= section
;
2456 cache_offset
= offset
;
2464 coff_sym_filepos(abfd
)
2467 return obj_sym_filepos(abfd
);
2473 #define coff_core_file_failing_command _bfd_dummy_core_file_failing_command
2474 #define coff_core_file_failing_signal _bfd_dummy_core_file_failing_signal
2475 #define coff_core_file_matches_executable_p _bfd_dummy_core_file_matches_executable_p
2476 #define coff_slurp_armap bfd_slurp_coff_armap
2477 #define coff_slurp_extended_name_table _bfd_slurp_extended_name_table
2478 #define coff_truncate_arname bfd_dont_truncate_arname
2479 #define coff_openr_next_archived_file bfd_generic_openr_next_archived_file
2480 #define coff_generic_stat_arch_elt bfd_generic_stat_arch_elt