1 /* subsegs.c - subsegments -
2 Copyright (C) 1987, 1990, 1991, 1992, 1993, 1994
3 Free Software Foundation, Inc.
5 This file is part of GAS, the GNU Assembler.
7 GAS is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GAS is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GAS; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
22 * 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
[] =
47 "e0", "e1", "e2", "e3", "e4", "e5", "e6", "e7", "e8", "e9",
52 #endif /* MANY_SEGMENTS */
54 "ASSEMBLER-INTERNAL-LOGIC-ERROR!",
57 "transfert vector preload",
58 "transfert vector postload",
61 }; /* Used by error reporters, dumpers etc. */
62 #else /* BFD_ASSEMBLER */
64 /* Gas segment information for bfd_abs_section_ptr and
65 bfd_und_section_ptr. */
66 static segment_info_type
*abs_seg_info
;
67 static segment_info_type
*und_seg_info
;
69 #endif /* BFD_ASSEMBLER */
71 static void subseg_set_rest
PARAMS ((segT
, subsegT
));
73 static fragS dummy_frag
;
75 static frchainS absolute_frchain
;
80 /* Check table(s) seg_name[], seg_N_TYPE[] is in correct order */
81 #if !defined (MANY_SEGMENTS) && !defined (BFD_ASSEMBLER)
82 know (SEG_ABSOLUTE
== 0);
86 know (SEG_UNKNOWN
== 4);
89 know (SEG_DEBUG
== 7);
92 know (SEG_REGISTER
== 10);
93 know (SEG_MAXIMUM_ORDINAL
== SEG_REGISTER
);
96 obstack_begin (&frchains
, chunksize
);
98 obstack_alignment_mask (&frchains
) = __alignof__ (frchainS
) - 1;
102 frchain_now
= NULL
; /* Warn new_subseg() that we are booting. */
104 frag_now
= &dummy_frag
;
106 #ifndef BFD_ASSEMBLER
107 now_subseg
= 42; /* Lie for 1st call to subseg_new. */
111 for (i
= SEG_E0
; i
< SEG_UNKNOWN
; i
++)
114 segment_info
[i
].frchainP
= frchain_now
;
118 subseg_set (SEG_DATA
, 0); /* .data 0 */
119 data0_frchainP
= frchain_now
;
121 subseg_set (SEG_BSS
, 0);
122 bss0_frchainP
= frchain_now
;
124 #endif /* ! MANY_SEGMENTS */
125 #endif /* ! BFD_ASSEMBLER */
127 absolute_frchain
.frch_seg
= absolute_section
;
128 absolute_frchain
.frch_subseg
= 0;
129 absolute_frchain
.fix_root
= absolute_frchain
.fix_tail
= 0;
130 absolute_frchain
.frch_frag_now
= &zero_address_frag
;
131 absolute_frchain
.frch_root
= absolute_frchain
.frch_last
= &zero_address_frag
;
137 * Change the subsegment we are in, BUT DO NOT MAKE A NEW FRAG for the
138 * subsegment. If we are already in the correct subsegment, change nothing.
139 * This is used eg as a worker for subseg_set [which does make a new frag_now]
140 * and for changing segments after we have read the source. We construct eg
141 * fixSs even after the source file is read, so we do have to keep the
142 * segment context correct.
145 subseg_change (seg
, subseg
)
152 if (now_seg
== absolute_section
)
157 segment_info_type
*seginfo
;
158 seginfo
= (segment_info_type
*) bfd_get_section_userdata (stdoutput
, seg
);
161 seginfo
= (segment_info_type
*) xmalloc (sizeof (*seginfo
));
162 memset ((PTR
) seginfo
, 0, sizeof (*seginfo
));
163 seginfo
->fix_root
= NULL
;
164 seginfo
->fix_tail
= NULL
;
165 seginfo
->bfd_section
= seg
;
167 if (seg
== bfd_abs_section_ptr
)
168 abs_seg_info
= seginfo
;
169 else if (seg
== bfd_und_section_ptr
)
170 und_seg_info
= seginfo
;
172 bfd_set_section_userdata (stdoutput
, seg
, (PTR
) seginfo
);
177 seg_fix_rootP
= &segment_info
[seg
].fix_root
;
178 seg_fix_tailP
= &segment_info
[seg
].fix_tail
;
182 seg_fix_rootP
= &data_fix_root
;
183 seg_fix_tailP
= &data_fix_tail
;
185 else if (seg
== SEG_TEXT
)
187 seg_fix_rootP
= &text_fix_root
;
188 seg_fix_tailP
= &text_fix_tail
;
192 know (seg
== SEG_BSS
);
193 seg_fix_rootP
= &bss_fix_root
;
194 seg_fix_tailP
= &bss_fix_tail
;
202 subseg_set_rest (seg
, subseg
)
206 long tmp
; /* JF for obstack alignment hacking */
207 register frchainS
*frcP
; /* crawl frchain chain */
208 register frchainS
**lastPP
; /* address of last pointer */
209 frchainS
*newP
; /* address of new frchain */
210 register fragS
*former_last_fragP
;
211 register fragS
*new_fragP
;
213 mri_common_symbol
= NULL
;
215 if (frag_now
&& frchain_now
)
216 frchain_now
->frch_frag_now
= frag_now
;
218 assert (frchain_now
== 0
219 || now_seg
== undefined_section
220 || now_seg
== absolute_section
221 || frchain_now
->frch_last
== frag_now
);
223 subseg_change (seg
, (int) subseg
);
225 if (seg
== absolute_section
)
227 frchain_now
= &absolute_frchain
;
228 frag_now
= &zero_address_frag
;
232 assert (frchain_now
== 0
233 || now_seg
== undefined_section
234 || frchain_now
->frch_last
== frag_now
);
237 * Attempt to find or make a frchain for that sub seg.
238 * Crawl along chain of frchainSs, begins @ frchain_root.
239 * If we need to make a frchainS, link it into correct
240 * position of chain rooted in frchain_root.
242 for (frcP
= *(lastPP
= &frchain_root
);
243 frcP
&& frcP
->frch_seg
<= seg
;
244 frcP
= *(lastPP
= &frcP
->frch_next
))
246 if (frcP
->frch_seg
== seg
247 && frcP
->frch_subseg
>= subseg
)
253 * frcP: Address of the 1st frchainS in correct segment with
254 * frch_subseg >= subseg.
255 * We want to either use this frchainS, or we want
256 * to insert a new frchainS just before it.
258 * If frcP==NULL, then we are at the end of the chain
259 * of frchainS-s. A NULL frcP means we fell off the end
260 * of the chain looking for a
261 * frch_subseg >= subseg, so we
262 * must make a new frchainS.
264 * If we ever maintain a pointer to
265 * the last frchainS in the chain, we change that pointer
266 * ONLY when frcP==NULL.
268 * lastPP: Address of the pointer with value frcP;
270 * May point to frchain_root.
274 || (frcP
->frch_seg
> seg
275 || frcP
->frch_subseg
> subseg
)) /* Kinky logic only works with 2 segments. */
278 * This should be the only code that creates a frchainS.
280 extern fragS
*frag_alloc ();
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
, 5000);
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 * Here with frcP pointing to the frchainS for subseg.
305 frag_now
= frcP
->frch_frag_now
;
307 assert (frchain_now
->frch_last
== frag_now
);
311 * subseg_set(segT, subsegT)
313 * If you attempt to change to the current subsegment, nothing happens.
315 * In: segT, subsegT code for new subsegment.
316 * frag_now -> incomplete frag for current subsegment.
317 * If frag_now==NULL, then there is no old, incomplete frag, so
318 * the old frag is not closed off.
320 * Out: now_subseg, now_seg updated.
321 * Frchain_now points to the (possibly new) struct frchain for this
323 * Frchain_root updated if needed.
326 #ifndef BFD_ASSEMBLER
329 subseg_new (segname
, subseg
)
335 for (i
= 0; i
< (int) SEG_MAXIMUM_ORDINAL
; i
++)
339 s
= segment_name ((segT
) i
);
340 if (strcmp (segname
, s
) == 0
341 || (segname
[0] == '.'
342 && strcmp (segname
+ 1, s
) == 0))
344 subseg_set ((segT
) i
, subseg
);
347 #ifdef obj_segment_name
348 s
= obj_segment_name ((segT
) i
);
349 if (strcmp (segname
, s
) == 0
350 || (segname
[0] == '.'
351 && strcmp (segname
+ 1, s
) == 0))
353 subseg_set ((segT
) i
, subseg
);
359 #ifdef obj_add_segment
362 new_seg
= obj_add_segment (segname
);
363 subseg_set (new_seg
, subseg
);
367 as_bad ("Attempt to switch to nonexistent segment \"%s\"", segname
);
373 subseg_set (seg
, subseg
) /* begin assembly for a new sub-segment */
374 register segT seg
; /* SEG_DATA or SEG_TEXT */
375 register subsegT subseg
;
377 #ifndef MANY_SEGMENTS
378 know (seg
== SEG_DATA
381 || seg
== SEG_ABSOLUTE
);
384 if (seg
!= now_seg
|| subseg
!= now_subseg
)
385 { /* we just changed sub-segments */
386 subseg_set_rest (seg
, subseg
);
388 mri_common_symbol
= NULL
;
391 #else /* BFD_ASSEMBLER */
394 subseg_get (segname
, force_new
)
399 segment_info_type
*seginfo
;
400 const char *now_seg_name
= (now_seg
401 ? bfd_get_section_name (stdoutput
, now_seg
)
406 && (now_seg_name
== segname
407 || !strcmp (now_seg_name
, segname
)))
411 secptr
= bfd_make_section_old_way (stdoutput
, segname
);
413 secptr
= bfd_make_section_anyway (stdoutput
, segname
);
415 seginfo
= seg_info (secptr
);
418 /* Check whether output_section is set first because secptr may
419 be bfd_abs_section_ptr. */
420 if (secptr
->output_section
!= secptr
)
421 secptr
->output_section
= secptr
;
422 seginfo
= (segment_info_type
*) xmalloc (sizeof (*seginfo
));
423 memset ((PTR
) seginfo
, 0, sizeof (*seginfo
));
424 seginfo
->fix_root
= NULL
;
425 seginfo
->fix_tail
= NULL
;
426 seginfo
->bfd_section
= secptr
;
427 if (secptr
== bfd_abs_section_ptr
)
428 abs_seg_info
= seginfo
;
429 else if (secptr
== bfd_und_section_ptr
)
430 und_seg_info
= seginfo
;
432 bfd_set_section_userdata (stdoutput
, secptr
, (PTR
) seginfo
);
433 seginfo
->frchainP
= NULL
;
434 seginfo
->lineno_list_head
= seginfo
->lineno_list_tail
= NULL
;
442 subseg_new (segname
, subseg
)
447 segment_info_type
*seginfo
;
449 secptr
= subseg_get (segname
, 0);
450 subseg_set_rest (secptr
, subseg
);
451 seginfo
= seg_info (secptr
);
452 if (! seginfo
->frchainP
)
453 seginfo
->frchainP
= frchain_now
;
457 /* Like subseg_new, except a new section is always created, even if
458 a section with that name already exists. */
460 subseg_force_new (segname
, subseg
)
465 segment_info_type
*seginfo
;
467 secptr
= subseg_get (segname
, 1);
468 subseg_set_rest (secptr
, subseg
);
469 seginfo
= seg_info (secptr
);
470 if (! seginfo
->frchainP
)
471 seginfo
->frchainP
= frchain_now
;
476 subseg_set (secptr
, subseg
)
480 if (! (secptr
== now_seg
&& subseg
== now_subseg
))
481 subseg_set_rest (secptr
, subseg
);
482 mri_common_symbol
= NULL
;
485 #ifndef obj_sec_sym_ok_for_reloc
486 #define obj_sec_sym_ok_for_reloc(SEC) 0
489 /* Get the gas information we are storing for a section. */
495 if (sec
== bfd_abs_section_ptr
)
497 else if (sec
== bfd_und_section_ptr
)
500 return (segment_info_type
*) bfd_get_section_userdata (stdoutput
, sec
);
507 segment_info_type
*seginfo
= seg_info (sec
);
514 s
= symbol_find (sec
->name
);
517 #ifndef EMIT_SECTION_SYMBOLS
518 #define EMIT_SECTION_SYMBOLS 1
521 if (! EMIT_SECTION_SYMBOLS
523 && symbol_table_frozen
526 /* Here we know it won't be going into the symbol table. */
527 s
= symbol_create (sec
->name
, sec
, 0, &zero_address_frag
);
529 s
= symbol_new (sec
->name
, sec
, 0, &zero_address_frag
);
530 S_CLEAR_EXTERNAL (s
);
532 /* Use the BFD section symbol, if possible. */
533 if (obj_sec_sym_ok_for_reloc (sec
))
534 s
->bsym
= sec
->symbol
;
540 #endif /* BFD_ASSEMBLER */
542 /* end of subsegs.c */