1 /* obj-format for ieee-695 records.
2 Copyright (C) 1991, 1992 Free Software Foundation, Inc.
4 This file is part of GAS, the GNU Assembler.
6 GAS is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GAS is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GAS; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
24 steve chamberlain steve@cygnus.com
28 this will hopefully become the port through which bfd and gas talk,
29 for the moment, only ieee is known to work well.
35 #include "output-file.h"
40 /* How many addresses does the .align take? */
42 relax_align (address
, alignment
)
43 register relax_addressT address
; /* Address now. */
44 register long alignment
; /* Alignment (binary). */
47 relax_addressT new_address
;
49 mask
= ~((~0) << alignment
);
50 new_address
= (address
+ mask
) & (~mask
);
51 return (new_address
- address
);
54 /* calculate the size of the frag chain and create a bfd section
55 to contain all of it */
57 DEFUN (size_section
, (abfd
, idx
),
62 unsigned int size
= 0;
63 fragS
*frag
= segment_info
[idx
].frag_root
;
66 if (frag
->fr_address
!= size
)
68 printf ("Out of step\n");
69 size
= frag
->fr_address
;
72 switch (frag
->fr_type
)
76 size
+= frag
->fr_offset
* frag
->fr_var
;
79 size
+= relax_align (size
, frag
->fr_offset
);
85 char *name
= segment_info
[idx
].name
;
86 if (name
== (char *) NULL
)
90 segment_info
[idx
].user_stuff
= (char *) (sec
= bfd_make_section (abfd
, name
));
91 /* Make it output through itself */
92 sec
->output_section
= sec
;
93 sec
->flags
|= SEC_HAS_CONTENTS
;
94 bfd_set_section_size (abfd
, sec
, size
);
98 /* run through a frag chain and write out the data to go with it */
100 DEFUN (fill_section
, (abfd
, idx
),
104 asection
*sec
= segment_info
[idx
].user_stuff
;
107 fragS
*frag
= segment_info
[idx
].frag_root
;
108 unsigned int offset
= 0;
111 unsigned int fill_size
;
113 switch (frag
->fr_type
)
120 bfd_set_section_contents (abfd
,
126 offset
+= frag
->fr_fix
;
127 fill_size
= frag
->fr_var
;
130 unsigned int off
= frag
->fr_fix
;
131 for (count
= frag
->fr_offset
; count
; count
--)
133 bfd_set_section_contents (abfd
, sec
,
136 frag
->fr_address
+ off
,
145 frag
= frag
->fr_next
;
150 /* Count the relocations in a chain */
153 DEFUN (count_entries_in_chain
, (idx
),
156 unsigned int nrelocs
;
159 /* Count the relocations */
160 fixup_ptr
= segment_info
[idx
].fix_root
;
162 while (fixup_ptr
!= (fixS
*) NULL
)
164 fixup_ptr
= fixup_ptr
->fx_next
;
170 /* output all the relocations for a section */
172 DEFUN (do_relocs_for
, (idx
),
175 unsigned int nrelocs
;
176 arelent
**reloc_ptr_vector
;
177 arelent
*reloc_vector
;
179 asection
*section
= (asection
*) (segment_info
[idx
].user_stuff
);
184 nrelocs
= count_entries_in_chain (idx
);
186 reloc_ptr_vector
= (arelent
**) malloc ((nrelocs
+ 1) * sizeof (arelent
*));
187 reloc_vector
= (arelent
*) malloc (nrelocs
* sizeof (arelent
));
188 ptrs
= (asymbol
**) malloc (nrelocs
* sizeof (asymbol
*));
189 from
= segment_info
[idx
].fix_root
;
190 for (i
= 0; i
< nrelocs
; i
++)
192 arelent
*to
= reloc_vector
+ i
;
194 reloc_ptr_vector
[i
] = to
;
195 to
->howto
= (reloc_howto_type
*) (from
->fx_r_type
);
197 #if 0 /* We can't represent complicated things in a reloc yet */
198 if (from
->fx_addsy
== 0 || from
->fx_subsy
!= 0) abort();
201 s
= &(from
->fx_addsy
->sy_symbol
.sy
);
202 to
->address
= ((char *) (from
->fx_frag
->fr_address
+
204 - ((char *) (&(from
->fx_frag
->fr_literal
)));
205 to
->addend
= from
->fx_offset
;
206 /* If we know the symbol which we want to relocate to, turn
207 this reloaction into a section relative.
209 If this relocation is pcrelative, and we know the
210 destination, we still want to keep the relocation - since
211 the linker might relax some of the bytes, but it stops
212 being pc relative and turns into an absolute relocation. */
215 if ((s
->flags
& BSF_UNDEFINED
) == 0)
217 to
->section
= s
->section
;
219 /* We can refer directly to the value field here,
220 rather than using S_GET_VALUE, because this is
221 only called after do_symbols, which sets up the
223 to
->addend
+= s
->value
;
226 if (to
->howto
->pcrel_offset
)
228 /* This is a pcrel relocation, the addend should be adjusted */
229 to
->addend
-= to
->address
+ 1;
235 *ptrs
= &(from
->fx_addsy
->sy_symbol
.sy
);
236 to
->sym_ptr_ptr
= ptrs
;
238 if (to
->howto
->pcrel_offset
)
240 /* This is a pcrel relocation, the addend should be adjusted */
241 to
->addend
-= to
->address
- 1;
252 from
= from
->fx_next
;
255 /* attatch to the section */
256 section
->orelocation
= reloc_ptr_vector
;
257 section
->reloc_count
= nrelocs
;
258 section
->flags
|= SEC_LOAD
;
262 /* do the symbols.. */
264 DEFUN (do_symbols
, (abfd
),
267 extern symbolS
*symbol_rootP
;
269 asymbol
**symbol_ptr_vec
;
271 unsigned int count
= 0;
275 for (ptr
= symbol_rootP
;
276 ptr
!= (symbolS
*) NULL
;
279 if (SEG_NORMAL (ptr
->sy_symbol
.seg
))
281 ptr
->sy_symbol
.sy
.section
=
282 (asection
*) (segment_info
[ptr
->sy_symbol
.seg
].user_stuff
);
283 S_SET_VALUE (ptr
, S_GET_VALUE (ptr
) + ptr
->sy_frag
->fr_address
);
284 if (ptr
->sy_symbol
.sy
.flags
== 0)
286 ptr
->sy_symbol
.sy
.flags
= BSF_LOCAL
;
291 switch (ptr
->sy_symbol
.seg
)
294 ptr
->sy_symbol
.sy
.flags
|= BSF_ABSOLUTE
;
295 ptr
->sy_symbol
.sy
.section
= 0;
298 ptr
->sy_symbol
.sy
.flags
= BSF_UNDEFINED
;
299 ptr
->sy_symbol
.sy
.section
= 0;
305 ptr
->sy_symbol
.sy
.value
= S_GET_VALUE (ptr
);
308 symbol_ptr_vec
= (asymbol
**) malloc ((count
+ 1) * sizeof (asymbol
*));
311 for (ptr
= symbol_rootP
;
312 ptr
!= (symbolS
*) NULL
;
315 symbol_ptr_vec
[index
] = &(ptr
->sy_symbol
.sy
);
318 symbol_ptr_vec
[index
] = 0;
319 abfd
->outsymbols
= symbol_ptr_vec
;
320 abfd
->symcount
= count
;
323 /* The generic as->bfd converter. Other backends may have special case
327 DEFUN_VOID (bfd_as_write_hook
)
331 for (i
= SEG_E0
; i
< SEG_UNKNOWN
; i
++)
333 size_section (abfd
, i
);
337 for (i
= SEG_E0
; i
< SEG_UNKNOWN
; i
++)
338 fill_section (abfd
, i
);
342 for (i
= SEG_E0
; i
< SEG_UNKNOWN
; i
++)
351 x
->sy_symbol
.seg
= y
;
357 if (SEG_NORMAL (x
->sy_symbol
.seg
))
361 switch (x
->sy_symbol
.seg
)
383 return x
->sy_symbol
.seg
;
389 x
->sy_symbol
.sy
.flags
|= BSF_GLOBAL
| BSF_EXPORT
;
396 x
->sy_symbol
.sy
.name
= y
;
416 obj_read_begin_hook ()
421 obj_ieee_section (ignore
)
424 extern char *input_line_pointer
;
425 extern char is_end_of_line
[];
426 char *p
= input_line_pointer
;
429 /* Look up the name, if it doesn't exist, make it */
430 while (*p
&& *p
!= ' ' && *p
!= ',' && !is_end_of_line
[*p
])
434 for (i
= SEG_E0
; i
< SEG_UNKNOWN
; i
++)
436 if (segment_info
[i
].hadone
)
438 if (strncmp (segment_info
[i
].name
, s
, p
- s
) == 0)
447 if (i
== SEG_UNKNOWN
)
449 as_bad ("too many sections");
453 segment_info
[i
].hadone
= 1;
454 segment_info
[i
].name
= malloc (p
- s
+ 1);
455 memcpy (segment_info
[i
].name
, s
, p
- s
);
456 segment_info
[i
].name
[p
- s
] = 0;
459 while (!is_end_of_line
[*p
])
461 input_line_pointer
= p
;
471 const pseudo_typeS obj_pseudo_table
[] =
473 {"section", obj_ieee_section
, 0},
477 {"export", s_globl
, 0},
478 {"option", s_ignore
, 0},
479 {"end", s_ignore
, 0},
480 {"import", s_ignore
, 0},
481 {"sdata", stringer
, 0},
489 obj_symbol_new_hook (symbolP
)
492 symbolP
->sy_symbol
.sy
.the_bfd
= abfd
;
501 DEFUN_VOID (write_object_file
)
504 struct frchain
*frchain_ptr
;
505 struct frag
*frag_ptr
;
507 abfd
= bfd_openw (out_file_name
, "ieee");
511 as_perror ("FATAL: Can't create %s", out_file_name
);
514 bfd_set_format (abfd
, bfd_object
);
515 bfd_set_arch_mach (abfd
, bfd_arch_h8300
, 0);
519 for (frchain_ptr
= frchain_root
;
520 frchain_ptr
!= (struct frchain
*) NULL
;
521 frchain_ptr
= frchain_ptr
->frch_next
)
523 /* Run through all the sub-segments and align them up. Also close any
524 open frags. We tack a .fill onto the end of the frag chain so
525 that any .align's size can be worked by looking at the next
528 subseg_set (frchain_ptr
->frch_seg
, frchain_ptr
->frch_subseg
);
529 #ifndef SUB_SEGMENT_ALIGN
530 #define SUB_SEGMENT_ALIGN(SEG) 2
532 frag_align (SUB_SEGMENT_ALIGN (now_seg
), 0);
533 frag_wane (frag_now
);
534 frag_now
->fr_fix
= 0;
535 know (frag_now
->fr_next
== NULL
);
538 /* Now build one big frag chain for each segment, linked through
540 for (i
= SEG_E0
; i
< SEG_UNKNOWN
; i
++)
543 fragS
**prev_frag_ptr_ptr
;
544 struct frchain
*next_frchain_ptr
;
546 /* struct frag **head_ptr = segment_info[i].frag_root;*/
548 segment_info
[i
].frag_root
= segment_info
[i
].frchainP
->frch_root
;
550 /* Im not sure what this is for */
551 for (frchain_ptr
= segment_info
[i
].frchainP
->frch_root
;
552 frchain_ptr
!= (struct frchain
*) NULL
;
553 frchain_ptr
= frchain_ptr
->frch_next
)
555 *head_ptr
= frchain_ptr
;
556 head_ptr
= &frchain_ptr
->next
;
563 for (i
= SEG_E0
; i
< SEG_UNKNOWN
; i
++)
565 relax_segment (segment_info
[i
].frag_root
, i
);
568 /* Now the addresses of the frags are correct within the segment */
570 bfd_as_write_hook ();
576 H_SET_TEXT_SIZE (a
, b
)
596 H_SET_RELOCATION_SIZE ()
601 H_SET_MAGIC_NUMBER ()
611 H_GET_TEXT_RELOCATION_SIZE ()
616 /* end of obj-ieee.c */