1 /* subsegs.c - subsegments -
2 Copyright 1987, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
4 Free Software Foundation, Inc.
6 This file is part of GAS, the GNU Assembler.
8 GAS is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
13 GAS is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GAS; see the file COPYING. If not, write to the Free
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
23 /* Segments & sub-segments. */
30 frchainS
*frchain_root
, *frchain_now
;
32 static struct obstack frchains
;
36 segment_info_type segment_info
[SEG_MAXIMUM_ORDINAL
];
39 /* Commented in "subsegs.h". */
40 frchainS
*data0_frchainP
, *bss0_frchainP
;
42 #endif /* MANY_SEGMENTS */
43 char const *const seg_name
[] = {
46 "e0", "e1", "e2", "e3", "e4", "e5", "e6", "e7", "e8", "e9",
47 "e10", "e11", "e12", "e13", "e14", "e15", "e16", "e17", "e18", "e19",
48 "e20", "e21", "e22", "e23", "e24", "e25", "e26", "e27", "e28", "e29",
49 "e30", "e31", "e32", "e33", "e34", "e35", "e36", "e37", "e38", "e39",
54 #endif /* MANY_SEGMENTS */
56 "ASSEMBLER-INTERNAL-LOGIC-ERROR!",
59 "transfert vector preload",
60 "transfert vector postload",
63 }; /* Used by error reporters, dumpers etc. */
64 #else /* BFD_ASSEMBLER */
66 /* Gas segment information for bfd_abs_section_ptr and
67 bfd_und_section_ptr. */
68 static segment_info_type
*abs_seg_info
;
69 static segment_info_type
*und_seg_info
;
71 #endif /* BFD_ASSEMBLER */
73 static void subseg_set_rest
PARAMS ((segT
, subsegT
));
75 static fragS dummy_frag
;
77 static frchainS absolute_frchain
;
82 /* Check table(s) seg_name[], seg_N_TYPE[] is in correct order */
83 #if !defined (MANY_SEGMENTS) && !defined (BFD_ASSEMBLER)
84 know (SEG_ABSOLUTE
== 0);
88 know (SEG_UNKNOWN
== 4);
91 know (SEG_DEBUG
== 7);
94 know (SEG_REGISTER
== 10);
95 know (SEG_MAXIMUM_ORDINAL
== SEG_REGISTER
);
98 obstack_begin (&frchains
, chunksize
);
100 obstack_alignment_mask (&frchains
) = __alignof__ (frchainS
) - 1;
104 frchain_now
= NULL
; /* Warn new_subseg() that we are booting. */
106 frag_now
= &dummy_frag
;
108 #ifndef BFD_ASSEMBLER
109 now_subseg
= 42; /* Lie for 1st call to subseg_new. */
113 for (i
= SEG_E0
; i
< SEG_UNKNOWN
; i
++)
116 segment_info
[i
].frchainP
= frchain_now
;
120 subseg_set (SEG_DATA
, 0); /* .data 0 */
121 data0_frchainP
= frchain_now
;
123 subseg_set (SEG_BSS
, 0);
124 bss0_frchainP
= frchain_now
;
126 #endif /* ! MANY_SEGMENTS */
127 #endif /* ! BFD_ASSEMBLER */
129 absolute_frchain
.frch_seg
= absolute_section
;
130 absolute_frchain
.frch_subseg
= 0;
132 absolute_frchain
.fix_root
= absolute_frchain
.fix_tail
= 0;
134 absolute_frchain
.frch_frag_now
= &zero_address_frag
;
135 absolute_frchain
.frch_root
= absolute_frchain
.frch_last
= &zero_address_frag
;
141 * Change the subsegment we are in, BUT DO NOT MAKE A NEW FRAG for the
142 * subsegment. If we are already in the correct subsegment, change nothing.
143 * This is used eg as a worker for subseg_set [which does make a new frag_now]
144 * and for changing segments after we have read the source. We construct eg
145 * fixSs even after the source file is read, so we do have to keep the
146 * segment context correct.
149 subseg_change (seg
, subseg
)
156 if (now_seg
== absolute_section
)
161 segment_info_type
*seginfo
;
162 seginfo
= (segment_info_type
*) bfd_get_section_userdata (stdoutput
, seg
);
165 seginfo
= (segment_info_type
*) xmalloc (sizeof (*seginfo
));
166 memset ((PTR
) seginfo
, 0, sizeof (*seginfo
));
167 seginfo
->fix_root
= NULL
;
168 seginfo
->fix_tail
= NULL
;
169 seginfo
->bfd_section
= seg
;
171 if (seg
== bfd_abs_section_ptr
)
172 abs_seg_info
= seginfo
;
173 else if (seg
== bfd_und_section_ptr
)
174 und_seg_info
= seginfo
;
176 bfd_set_section_userdata (stdoutput
, seg
, (PTR
) seginfo
);
181 seg_fix_rootP
= &segment_info
[seg
].fix_root
;
182 seg_fix_tailP
= &segment_info
[seg
].fix_tail
;
186 seg_fix_rootP
= &data_fix_root
;
187 seg_fix_tailP
= &data_fix_tail
;
189 else if (seg
== SEG_TEXT
)
191 seg_fix_rootP
= &text_fix_root
;
192 seg_fix_tailP
= &text_fix_tail
;
196 know (seg
== SEG_BSS
);
197 seg_fix_rootP
= &bss_fix_root
;
198 seg_fix_tailP
= &bss_fix_tail
;
206 subseg_set_rest (seg
, subseg
)
210 register frchainS
*frcP
; /* crawl frchain chain */
211 register frchainS
**lastPP
; /* address of last pointer */
212 frchainS
*newP
; /* address of new frchain */
214 mri_common_symbol
= NULL
;
216 if (frag_now
&& frchain_now
)
217 frchain_now
->frch_frag_now
= frag_now
;
219 assert (frchain_now
== 0
220 || now_seg
== undefined_section
221 || now_seg
== absolute_section
222 || frchain_now
->frch_last
== frag_now
);
224 subseg_change (seg
, (int) subseg
);
226 if (seg
== absolute_section
)
228 frchain_now
= &absolute_frchain
;
229 frag_now
= &zero_address_frag
;
233 assert (frchain_now
== 0
234 || now_seg
== undefined_section
235 || frchain_now
->frch_last
== frag_now
);
238 * Attempt to find or make a frchain for that sub seg.
239 * Crawl along chain of frchainSs, begins @ frchain_root.
240 * If we need to make a frchainS, link it into correct
241 * position of chain rooted in frchain_root.
243 for (frcP
= *(lastPP
= &frchain_root
);
244 frcP
&& frcP
->frch_seg
<= seg
;
245 frcP
= *(lastPP
= &frcP
->frch_next
))
247 if (frcP
->frch_seg
== seg
248 && frcP
->frch_subseg
>= subseg
)
254 * frcP: Address of the 1st frchainS in correct segment with
255 * frch_subseg >= subseg.
256 * We want to either use this frchainS, or we want
257 * to insert a new frchainS just before it.
259 * If frcP==NULL, then we are at the end of the chain
260 * of frchainS-s. A NULL frcP means we fell off the end
261 * of the chain looking for a
262 * frch_subseg >= subseg, so we
263 * must make a new frchainS.
265 * If we ever maintain a pointer to
266 * the last frchainS in the chain, we change that pointer
267 * ONLY when frcP==NULL.
269 * lastPP: Address of the pointer with value frcP;
271 * May point to frchain_root.
275 || (frcP
->frch_seg
> seg
276 || frcP
->frch_subseg
> subseg
)) /* Kinky logic only works with 2 segments. */
279 * This should be the only code that creates a frchainS.
281 newP
= (frchainS
*) obstack_alloc (&frchains
, sizeof (frchainS
));
282 newP
->frch_subseg
= subseg
;
283 newP
->frch_seg
= seg
;
285 newP
->fix_root
= NULL
;
286 newP
->fix_tail
= NULL
;
288 obstack_begin (&newP
->frch_obstack
, chunksize
);
290 obstack_alignment_mask (&newP
->frch_obstack
) = __alignof__ (fragS
) - 1;
292 newP
->frch_frag_now
= frag_alloc (&newP
->frch_obstack
);
293 newP
->frch_frag_now
->fr_type
= rs_fill
;
295 newP
->frch_root
= newP
->frch_last
= newP
->frch_frag_now
;
298 newP
->frch_next
= frcP
; /* perhaps NULL */
302 segment_info_type
*seginfo
;
303 seginfo
= seg_info (seg
);
304 if (seginfo
&& seginfo
->frchainP
== frcP
)
305 seginfo
->frchainP
= newP
;
312 * Here with frcP pointing to the frchainS for subseg.
315 frag_now
= frcP
->frch_frag_now
;
317 assert (frchain_now
->frch_last
== frag_now
);
321 * subseg_set(segT, subsegT)
323 * If you attempt to change to the current subsegment, nothing happens.
325 * In: segT, subsegT code for new subsegment.
326 * frag_now -> incomplete frag for current subsegment.
327 * If frag_now==NULL, then there is no old, incomplete frag, so
328 * the old frag is not closed off.
330 * Out: now_subseg, now_seg updated.
331 * Frchain_now points to the (possibly new) struct frchain for this
333 * Frchain_root updated if needed.
336 #ifndef BFD_ASSEMBLER
339 subseg_new (segname
, subseg
)
345 for (i
= 0; i
< (int) SEG_MAXIMUM_ORDINAL
; i
++)
349 s
= segment_name ((segT
) i
);
350 if (strcmp (segname
, s
) == 0
351 || (segname
[0] == '.'
352 && strcmp (segname
+ 1, s
) == 0))
354 subseg_set ((segT
) i
, subseg
);
357 #ifdef obj_segment_name
358 s
= obj_segment_name ((segT
) i
);
359 if (strcmp (segname
, s
) == 0
360 || (segname
[0] == '.'
361 && strcmp (segname
+ 1, s
) == 0))
363 subseg_set ((segT
) i
, subseg
);
369 #ifdef obj_add_segment
372 new_seg
= obj_add_segment (segname
);
373 subseg_set (new_seg
, subseg
);
377 as_bad (_("attempt to switch to nonexistent segment \"%s\""), segname
);
383 subseg_set (seg
, subseg
) /* begin assembly for a new sub-segment */
384 register segT seg
; /* SEG_DATA or SEG_TEXT */
385 register subsegT subseg
;
387 #ifndef MANY_SEGMENTS
388 know (seg
== SEG_DATA
391 || seg
== SEG_ABSOLUTE
);
394 if (seg
!= now_seg
|| subseg
!= now_subseg
)
395 { /* we just changed sub-segments */
396 subseg_set_rest (seg
, subseg
);
398 mri_common_symbol
= NULL
;
401 #else /* BFD_ASSEMBLER */
404 subseg_get (segname
, force_new
)
409 segment_info_type
*seginfo
;
410 const char *now_seg_name
= (now_seg
411 ? bfd_get_section_name (stdoutput
, now_seg
)
416 && (now_seg_name
== segname
417 || !strcmp (now_seg_name
, segname
)))
421 secptr
= bfd_make_section_old_way (stdoutput
, segname
);
423 secptr
= bfd_make_section_anyway (stdoutput
, segname
);
425 seginfo
= seg_info (secptr
);
428 /* Check whether output_section is set first because secptr may
429 be bfd_abs_section_ptr. */
430 if (secptr
->output_section
!= secptr
)
431 secptr
->output_section
= secptr
;
432 seginfo
= (segment_info_type
*) xmalloc (sizeof (*seginfo
));
433 memset ((PTR
) seginfo
, 0, sizeof (*seginfo
));
434 seginfo
->fix_root
= NULL
;
435 seginfo
->fix_tail
= NULL
;
436 seginfo
->bfd_section
= secptr
;
437 if (secptr
== bfd_abs_section_ptr
)
438 abs_seg_info
= seginfo
;
439 else if (secptr
== bfd_und_section_ptr
)
440 und_seg_info
= seginfo
;
442 bfd_set_section_userdata (stdoutput
, secptr
, (PTR
) seginfo
);
443 seginfo
->frchainP
= NULL
;
444 seginfo
->lineno_list_head
= seginfo
->lineno_list_tail
= NULL
;
452 subseg_new (segname
, subseg
)
457 segment_info_type
*seginfo
;
459 secptr
= subseg_get (segname
, 0);
460 subseg_set_rest (secptr
, subseg
);
461 seginfo
= seg_info (secptr
);
462 if (! seginfo
->frchainP
)
463 seginfo
->frchainP
= frchain_now
;
467 /* Like subseg_new, except a new section is always created, even if
468 a section with that name already exists. */
470 subseg_force_new (segname
, subseg
)
475 segment_info_type
*seginfo
;
477 secptr
= subseg_get (segname
, 1);
478 subseg_set_rest (secptr
, subseg
);
479 seginfo
= seg_info (secptr
);
480 if (! seginfo
->frchainP
)
481 seginfo
->frchainP
= frchain_now
;
486 subseg_set (secptr
, subseg
)
490 if (! (secptr
== now_seg
&& subseg
== now_subseg
))
491 subseg_set_rest (secptr
, subseg
);
492 mri_common_symbol
= NULL
;
495 #ifndef obj_sec_sym_ok_for_reloc
496 #define obj_sec_sym_ok_for_reloc(SEC) 0
499 /* Get the gas information we are storing for a section. */
505 if (sec
== bfd_abs_section_ptr
)
507 else if (sec
== bfd_und_section_ptr
)
510 return (segment_info_type
*) bfd_get_section_userdata (stdoutput
, sec
);
517 segment_info_type
*seginfo
= seg_info (sec
);
525 #ifndef EMIT_SECTION_SYMBOLS
526 #define EMIT_SECTION_SYMBOLS 1
529 if (! EMIT_SECTION_SYMBOLS
|| symbol_table_frozen
)
531 /* Here we know it won't be going into the symbol table. */
532 s
= symbol_create (sec
->name
, sec
, 0, &zero_address_frag
);
536 s
= symbol_find_base (sec
->name
, 0);
538 s
= symbol_new (sec
->name
, sec
, 0, &zero_address_frag
);
541 if (S_GET_SEGMENT (s
) == undefined_section
)
543 S_SET_SEGMENT (s
, sec
);
544 symbol_set_frag (s
, &zero_address_frag
);
549 S_CLEAR_EXTERNAL (s
);
551 /* Use the BFD section symbol, if possible. */
552 if (obj_sec_sym_ok_for_reloc (sec
))
553 symbol_set_bfdsym (s
, sec
->symbol
);
555 symbol_get_bfdsym (s
)->flags
|= BSF_SECTION_SYM
;
561 #endif /* BFD_ASSEMBLER */
563 /* Return whether the specified segment is thought to hold text. */
565 #ifndef BFD_ASSEMBLER
566 const char * const nontext_section_names
[] = {
570 #ifndef COFF_LONG_SECTION_NAMES
577 #endif /* ! BFD_ASSEMBLER */
584 return (bfd_get_section_flags (stdoutput
, sec
) & SEC_CODE
) != 0;
585 #else /* ! BFD_ASSEMBLER */
586 const char * const *p
;
588 if (sec
== data_section
|| sec
== bss_section
|| sec
== absolute_section
)
591 for (p
= nontext_section_names
; *p
!= NULL
; ++p
)
593 if (strcmp (segment_name (sec
), *p
) == 0)
596 #ifdef obj_segment_name
597 if (strcmp (obj_segment_name (sec
), *p
) == 0)
604 #endif /* ! BFD_ASSEMBLER */
608 subsegs_print_statistics (file
)
612 fprintf (file
, "frag chains:\n");
613 for (frchp
= frchain_root
; frchp
; frchp
= frchp
->frch_next
)
618 /* If frch_subseg is non-zero, it's probably been chained onto
619 the end of a previous subsection. Don't count it again. */
620 if (frchp
->frch_subseg
!= 0)
623 /* Skip gas-internal sections. */
624 if (segment_name (frchp
->frch_seg
)[0] == '*')
627 for (fragp
= frchp
->frch_root
; fragp
; fragp
= fragp
->fr_next
)
630 switch (fragp
->fr_type
)
633 fprintf (file
, "f"); break;
635 fprintf (file
, "a"); break;
637 fprintf (file
, "c"); break;
639 fprintf (file
, "o"); break;
640 case rs_machine_dependent
:
641 fprintf (file
, "m"); break;
643 fprintf (file
, "s"); break;
645 fprintf (file
, "0"); break;
647 fprintf (file
, "?"); break;
652 fprintf (file
, "\n");
653 fprintf (file
, "\t%p %-10s\t%10d frags\n", (void *) frchp
,
654 segment_name (frchp
->frch_seg
), count
);
658 /* end of subsegs.c */