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;
34 segment_info_type segment_info[SEG_MAXIMUM_ORDINAL];
37 /* Commented in "subsegs.h". */
38 frchainS *data0_frchainP, *bss0_frchainP;
40 #endif /* MANY_SEGMENTS */
41 char const *const seg_name[] =
45 "e0", "e1", "e2", "e3", "e4", "e5", "e6", "e7", "e8", "e9",
50 #endif /* MANY_SEGMENTS */
52 "ASSEMBLER-INTERNAL-LOGIC-ERROR!",
55 "transfert vector preload",
56 "transfert vector postload",
59 }; /* Used by error reporters, dumpers etc. */
60 #else /* BFD_ASSEMBLER */
62 /* Gas segment information for bfd_abs_section_ptr and
63 bfd_und_section_ptr. */
64 static segment_info_type *abs_seg_info;
65 static segment_info_type *und_seg_info;
67 #endif /* BFD_ASSEMBLER */
69 static void subseg_set_rest PARAMS ((segT, subsegT));
74 /* Check table(s) seg_name[], seg_N_TYPE[] is in correct order */
75 #if !defined (MANY_SEGMENTS) && !defined (BFD_ASSEMBLER)
76 know (SEG_ABSOLUTE == 0);
80 know (SEG_UNKNOWN == 4);
83 know (SEG_DEBUG == 7);
86 know (SEG_REGISTER == 10);
87 know (SEG_MAXIMUM_ORDINAL == SEG_REGISTER);
91 frchain_now = NULL; /* Warn new_subseg() that we are booting. */
92 /* Fake up 1st frag. It won't be used=> is ok if obstack...
93 pads the end of it for alignment. */
94 frag_now = (fragS *) obstack_alloc (&frags, SIZEOF_STRUCT_FRAG);
95 memset (frag_now, 0, SIZEOF_STRUCT_FRAG);
98 /* This 1st frag will not be in any frchain.
99 We simply give subseg_new somewhere to scribble. */
100 now_subseg = 42; /* Lie for 1st call to subseg_new. */
104 for (i = SEG_E0; i < SEG_UNKNOWN; i++)
107 segment_info[i].frchainP = frchain_now;
111 subseg_set (SEG_DATA, 0); /* .data 0 */
112 data0_frchainP = frchain_now;
114 subseg_set (SEG_BSS, 0);
115 bss0_frchainP = frchain_now;
117 #endif /* ! MANY_SEGMENTS */
118 #endif /* ! BFD_ASSEMBLER */
125 * Change the subsegment we are in, BUT DO NOT MAKE A NEW FRAG for the
126 * subsegment. If we are already in the correct subsegment, change nothing.
127 * This is used eg as a worker for subseg_set [which does make a new frag_now]
128 * and for changing segments after we have read the source. We construct eg
129 * fixSs even after the source file is read, so we do have to keep the
130 * segment context correct.
133 subseg_change (seg, subseg)
142 segment_info_type *seginfo;
143 seginfo = (segment_info_type *) bfd_get_section_userdata (stdoutput, seg);
146 seginfo = (segment_info_type *) xmalloc (sizeof (*seginfo));
147 memset ((PTR) seginfo, 0, sizeof (*seginfo));
148 seginfo->fix_root = NULL;
149 seginfo->fix_tail = NULL;
150 seginfo->bfd_section = seg;
152 if (seg == bfd_abs_section_ptr)
153 abs_seg_info = seginfo;
154 else if (seg == bfd_und_section_ptr)
155 und_seg_info = seginfo;
157 bfd_set_section_userdata (stdoutput, seg, (PTR) seginfo);
162 seg_fix_rootP = &segment_info[seg].fix_root;
163 seg_fix_tailP = &segment_info[seg].fix_tail;
167 seg_fix_rootP = &data_fix_root;
168 seg_fix_tailP = &data_fix_tail;
170 else if (seg == SEG_TEXT)
172 seg_fix_rootP = &text_fix_root;
173 seg_fix_tailP = &text_fix_tail;
177 know (seg == SEG_BSS);
178 seg_fix_rootP = &bss_fix_root;
179 seg_fix_tailP = &bss_fix_tail;
187 subseg_set_rest (seg, subseg)
191 long tmp; /* JF for obstack alignment hacking */
192 register frchainS *frcP; /* crawl frchain chain */
193 register frchainS **lastPP; /* address of last pointer */
194 frchainS *newP; /* address of new frchain */
195 register fragS *former_last_fragP;
196 register fragS *new_fragP;
198 if (frag_now) /* If not bootstrapping. */
200 frag_now->fr_fix = frag_now_fix ();
201 frag_wane (frag_now); /* Close off any frag in old subseg. */
204 * It would be nice to keep an obstack for each subsegment, if we swap
205 * subsegments a lot. Hence we would have much fewer frag_wanes().
208 obstack_finish (&frags);
210 * If we don't do the above, the next object we put on obstack frags
211 * will appear to start at the fr_literal of the current frag.
212 * Also, above ensures that the next object will begin on a
213 * address that is aligned correctly for the engine that runs
217 subseg_change (seg, (int) subseg);
219 * Attempt to find or make a frchain for that sub seg.
220 * Crawl along chain of frchainSs, begins @ frchain_root.
221 * If we need to make a frchainS, link it into correct
222 * position of chain rooted in frchain_root.
224 for (frcP = *(lastPP = &frchain_root);
225 frcP && frcP->frch_seg <= seg;
226 frcP = *(lastPP = &frcP->frch_next))
228 if (frcP->frch_seg == seg
229 && frcP->frch_subseg >= subseg)
235 * frcP: Address of the 1st frchainS in correct segment with
236 * frch_subseg >= subseg.
237 * We want to either use this frchainS, or we want
238 * to insert a new frchainS just before it.
240 * If frcP==NULL, then we are at the end of the chain
241 * of frchainS-s. A NULL frcP means we fell off the end
242 * of the chain looking for a
243 * frch_subseg >= subseg, so we
244 * must make a new frchainS.
246 * If we ever maintain a pointer to
247 * the last frchainS in the chain, we change that pointer
248 * ONLY when frcP==NULL.
250 * lastPP: Address of the pointer with value frcP;
252 * May point to frchain_root.
256 || (frcP->frch_seg > seg
257 || frcP->frch_subseg > subseg)) /* Kinky logic only works with 2 segments. */
260 * This should be the only code that creates a frchainS.
262 newP = (frchainS *) obstack_alloc (&frags, sizeof (frchainS));
264 newP->frch_subseg = subseg;
265 newP->frch_seg = seg;
266 newP->frch_last = NULL;
268 newP->fix_root = NULL;
269 newP->fix_tail = NULL;
271 obstack_begin (&newP->frch_obstack, 5000);
274 newP->frch_next = frcP; /* perhaps NULL */
278 * Here with frcP pointing to the frchainS for subseg.
282 * Make a fresh frag for the subsegment.
284 /* We expect this to happen on a correct boundary since it was
285 proceeded by a obstack_done(). */
286 tmp = obstack_alignment_mask (&frags); /* JF disable alignment */
287 obstack_alignment_mask (&frags) = 0;
288 frag_now = (fragS *) obstack_alloc (&frags, SIZEOF_STRUCT_FRAG);
289 memset (frag_now, 0, SIZEOF_STRUCT_FRAG);
290 obstack_alignment_mask (&frags) = tmp;
291 /* But we want any more chars to come immediately after the
292 structure we just made. */
293 new_fragP = frag_now;
294 new_fragP->fr_next = NULL;
296 * Append new frag to current frchain.
298 former_last_fragP = frcP->frch_last;
299 if (former_last_fragP)
301 know (former_last_fragP->fr_next == NULL);
302 know (frchain_now->frch_root);
303 former_last_fragP->fr_next = new_fragP;
307 frcP->frch_root = new_fragP;
309 frcP->frch_last = new_fragP;
311 mri_common_symbol = NULL;
315 * subseg_set(segT, subsegT)
317 * If you attempt to change to the current subsegment, nothing happens.
319 * In: segT, subsegT code for new subsegment.
320 * frag_now -> incomplete frag for current subsegment.
321 * If frag_now==NULL, then there is no old, incomplete frag, so
322 * the old frag is not closed off.
324 * Out: now_subseg, now_seg updated.
325 * Frchain_now points to the (possibly new) struct frchain for this
327 * Frchain_root updated if needed.
330 #ifndef BFD_ASSEMBLER
333 subseg_new (segname, subseg)
339 for (i = 0; i < (int) SEG_MAXIMUM_ORDINAL; i++)
343 s = segment_name ((segT) i);
344 if (strcmp (segname, s) == 0
345 || (segname[0] == '.'
346 && strcmp (segname + 1, s) == 0))
348 subseg_set ((segT) i, subseg);
351 #ifdef obj_segment_name
352 s = obj_segment_name ((segT) i);
353 if (strcmp (segname, s) == 0
354 || (segname[0] == '.'
355 && strcmp (segname + 1, s) == 0))
357 subseg_set ((segT) i, subseg);
363 #ifdef obj_add_segment
366 new_seg = obj_add_segment (segname);
367 subseg_set (new_seg, subseg);
371 as_bad ("Attempt to switch to nonexistent segment \"%s\"", segname);
377 subseg_set (seg, subseg) /* begin assembly for a new sub-segment */
378 register segT seg; /* SEG_DATA or SEG_TEXT */
379 register subsegT subseg;
381 #ifndef MANY_SEGMENTS
382 know (seg == SEG_DATA || seg == SEG_TEXT || seg == SEG_BSS);
385 if (seg != now_seg || subseg != now_subseg)
386 { /* we just changed sub-segments */
387 subseg_set_rest (seg, subseg);
389 mri_common_symbol = NULL;
392 #else /* BFD_ASSEMBLER */
395 subseg_get (segname, force_new)
400 segment_info_type *seginfo;
401 const char *now_seg_name = (now_seg
402 ? bfd_get_section_name (stdoutput, now_seg)
407 && (now_seg_name == segname
408 || !strcmp (now_seg_name, segname)))
412 secptr = bfd_make_section_old_way (stdoutput, segname);
414 secptr = bfd_make_section_anyway (stdoutput, segname);
416 seginfo = seg_info (secptr);
419 /* Check whether output_section is set first because secptr may
420 be bfd_abs_section_ptr. */
421 if (secptr->output_section != secptr)
422 secptr->output_section = secptr;
423 seginfo = (segment_info_type *) xmalloc (sizeof (*seginfo));
424 memset ((PTR) seginfo, 0, sizeof (*seginfo));
425 seginfo->fix_root = NULL;
426 seginfo->fix_tail = NULL;
427 seginfo->bfd_section = secptr;
428 if (secptr == bfd_abs_section_ptr)
429 abs_seg_info = seginfo;
430 else if (secptr == bfd_und_section_ptr)
431 und_seg_info = seginfo;
433 bfd_set_section_userdata (stdoutput, secptr, (PTR) seginfo);
434 seginfo->frchainP = NULL;
435 seginfo->lineno_list_head = seginfo->lineno_list_tail = NULL;
443 subseg_new (segname, subseg)
448 segment_info_type *seginfo;
450 secptr = subseg_get (segname, 0);
451 subseg_set_rest (secptr, subseg);
452 seginfo = seg_info (secptr);
453 if (! seginfo->frchainP)
454 seginfo->frchainP = frchain_now;
458 /* Like subseg_new, except a new section is always created, even if
459 a section with that name already exists. */
461 subseg_force_new (segname, subseg)
466 segment_info_type *seginfo;
468 secptr = subseg_get (segname, 1);
469 subseg_set_rest (secptr, subseg);
470 seginfo = seg_info (secptr);
471 if (! seginfo->frchainP)
472 seginfo->frchainP = frchain_now;
477 subseg_set (secptr, subseg)
481 if (! (secptr == now_seg && subseg == now_subseg))
482 subseg_set_rest (secptr, subseg);
483 mri_common_symbol = NULL;
486 #ifndef obj_sec_sym_ok_for_reloc
487 #define obj_sec_sym_ok_for_reloc(SEC) 0
490 /* Get the gas information we are storing for a section. */
496 if (sec == bfd_abs_section_ptr)
498 else if (sec == bfd_und_section_ptr)
501 return (segment_info_type *) bfd_get_section_userdata (stdoutput, sec);
508 segment_info_type *seginfo = seg_info (sec);
515 s = symbol_find (sec->name);
518 #ifndef EMIT_SECTION_SYMBOLS
519 #define EMIT_SECTION_SYMBOLS 1
522 if (! EMIT_SECTION_SYMBOLS
524 && symbol_table_frozen
527 /* Here we know it won't be going into the symbol table. */
528 s = symbol_create (sec->name, sec, 0, &zero_address_frag);
530 s = symbol_new (sec->name, sec, 0, &zero_address_frag);
531 S_CLEAR_EXTERNAL (s);
533 /* Use the BFD section symbol, if possible. */
534 if (obj_sec_sym_ok_for_reloc (sec))
535 s->bsym = sec->symbol;
541 #endif /* BFD_ASSEMBLER */
543 /* end of subsegs.c */