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, 675 Mass Ave, Cambridge, MA 02139, 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 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);
90 obstack_begin (&frags, 5000);
92 frchain_now = NULL; /* Warn new_subseg() that we are booting. */
93 /* Fake up 1st frag. It won't be used=> is ok if obstack...
94 pads the end of it for alignment. */
95 frag_now = (fragS *) obstack_alloc (&frags, SIZEOF_STRUCT_FRAG);
96 memset (frag_now, 0, SIZEOF_STRUCT_FRAG);
99 /* This 1st frag will not be in any frchain.
100 We simply give subseg_new somewhere to scribble. */
101 now_subseg = 42; /* Lie for 1st call to subseg_new. */
105 for (i = SEG_E0; i < SEG_UNKNOWN; i++)
108 segment_info[i].frchainP = frchain_now;
112 subseg_set (SEG_DATA, 0); /* .data 0 */
113 data0_frchainP = frchain_now;
115 subseg_set (SEG_BSS, 0);
116 bss0_frchainP = frchain_now;
118 #endif /* ! MANY_SEGMENTS */
119 #endif /* ! BFD_ASSEMBLER */
126 * Change the subsegment we are in, BUT DO NOT MAKE A NEW FRAG for the
127 * subsegment. If we are already in the correct subsegment, change nothing.
128 * This is used eg as a worker for subseg_set [which does make a new frag_now]
129 * and for changing segments after we have read the source. We construct eg
130 * fixSs even after the source file is read, so we do have to keep the
131 * segment context correct.
134 subseg_change (seg, subseg)
143 segment_info_type *seginfo;
144 seginfo = (segment_info_type *) bfd_get_section_userdata (stdoutput, seg);
147 seginfo = (segment_info_type *) xmalloc (sizeof (*seginfo));
148 memset ((PTR) seginfo, 0, sizeof (*seginfo));
149 seginfo->fix_root = NULL;
150 seginfo->fix_tail = NULL;
151 seginfo->bfd_section = seg;
153 if (seg == bfd_abs_section_ptr)
154 abs_seg_info = seginfo;
155 else if (seg == bfd_und_section_ptr)
156 und_seg_info = seginfo;
158 bfd_set_section_userdata (stdoutput, seg, (PTR) seginfo);
163 seg_fix_rootP = &segment_info[seg].fix_root;
164 seg_fix_tailP = &segment_info[seg].fix_tail;
168 seg_fix_rootP = &data_fix_root;
169 seg_fix_tailP = &data_fix_tail;
171 else if (seg == SEG_TEXT)
173 seg_fix_rootP = &text_fix_root;
174 seg_fix_tailP = &text_fix_tail;
178 know (seg == SEG_BSS);
179 seg_fix_rootP = &bss_fix_root;
180 seg_fix_tailP = &bss_fix_tail;
188 subseg_set_rest (seg, subseg)
192 long tmp; /* JF for obstack alignment hacking */
193 register frchainS *frcP; /* crawl frchain chain */
194 register frchainS **lastPP; /* address of last pointer */
195 frchainS *newP; /* address of new frchain */
196 register fragS *former_last_fragP;
197 register fragS *new_fragP;
199 if (frag_now) /* If not bootstrapping. */
201 frag_now->fr_fix = (char*) obstack_next_free (&frags) - frag_now->fr_literal;
202 frag_wane (frag_now); /* Close off any frag in old subseg. */
205 * It would be nice to keep an obstack for each subsegment, if we swap
206 * subsegments a lot. Hence we would have much fewer frag_wanes().
209 obstack_finish (&frags);
211 * If we don't do the above, the next object we put on obstack frags
212 * will appear to start at the fr_literal of the current frag.
213 * Also, above ensures that the next object will begin on a
214 * address that is aligned correctly for the engine that runs
218 subseg_change (seg, (int) subseg);
220 * Attempt to find or make a frchain for that sub seg.
221 * Crawl along chain of frchainSs, begins @ frchain_root.
222 * If we need to make a frchainS, link it into correct
223 * position of chain rooted in frchain_root.
225 for (frcP = *(lastPP = &frchain_root);
226 frcP && frcP->frch_seg <= seg;
227 frcP = *(lastPP = &frcP->frch_next))
229 if (frcP->frch_seg == seg
230 && frcP->frch_subseg >= subseg)
236 * frcP: Address of the 1st frchainS in correct segment with
237 * frch_subseg >= subseg.
238 * We want to either use this frchainS, or we want
239 * to insert a new frchainS just before it.
241 * If frcP==NULL, then we are at the end of the chain
242 * of frchainS-s. A NULL frcP means we fell off the end
243 * of the chain looking for a
244 * frch_subseg >= subseg, so we
245 * must make a new frchainS.
247 * If we ever maintain a pointer to
248 * the last frchainS in the chain, we change that pointer
249 * ONLY when frcP==NULL.
251 * lastPP: Address of the pointer with value frcP;
253 * May point to frchain_root.
257 || (frcP->frch_seg > seg
258 || frcP->frch_subseg > subseg)) /* Kinky logic only works with 2 segments. */
261 * This should be the only code that creates a frchainS.
263 newP = (frchainS *) obstack_alloc (&frags, sizeof (frchainS));
264 memset (newP, 0, sizeof (frchainS));
265 /* This begines on a good boundary because a obstack_done()
266 preceeded it. It implies an obstack_done(), so we expect
267 the next object allocated to begin on a correct boundary. */
269 newP->frch_next = frcP; /* perhaps NULL */
270 (frcP = newP)->frch_subseg = subseg;
271 newP->frch_seg = seg;
272 newP->frch_last = NULL;
274 newP->fix_root = NULL;
275 newP->fix_tail = NULL;
279 * Here with frcP ->ing to the frchainS for subseg.
283 * Make a fresh frag for the subsegment.
285 /* We expect this to happen on a correct boundary since it was
286 proceeded by a obstack_done(). */
287 tmp = obstack_alignment_mask (&frags); /* JF disable alignment */
288 obstack_alignment_mask (&frags) = 0;
289 frag_now = (fragS *) obstack_alloc (&frags, SIZEOF_STRUCT_FRAG);
290 memset (frag_now, 0, SIZEOF_STRUCT_FRAG);
291 obstack_alignment_mask (&frags) = tmp;
292 /* But we want any more chars to come immediately after the
293 structure we just made. */
294 new_fragP = frag_now;
295 new_fragP->fr_next = NULL;
297 * Append new frag to current frchain.
299 former_last_fragP = frcP->frch_last;
300 if (former_last_fragP)
302 know (former_last_fragP->fr_next == NULL);
303 know (frchain_now->frch_root);
304 former_last_fragP->fr_next = new_fragP;
308 frcP->frch_root = new_fragP;
310 frcP->frch_last = new_fragP;
314 * subseg_set(segT, subsegT)
316 * If you attempt to change to the current subsegment, nothing happens.
318 * In: segT, subsegT code for new subsegment.
319 * frag_now -> incomplete frag for current subsegment.
320 * If frag_now==NULL, then there is no old, incomplete frag, so
321 * the old frag is not closed off.
323 * Out: now_subseg, now_seg updated.
324 * Frchain_now points to the (possibly new) struct frchain for this
326 * Frchain_root updated if needed.
329 #ifndef BFD_ASSEMBLER
332 subseg_new (segname, subseg)
338 for (i = 0; i < (int) SEG_MAXIMUM_ORDINAL; i++)
342 s = segment_name ((segT) i);
343 if (strcmp (segname, s) == 0
344 || (segname[0] == '.'
345 && strcmp (segname + 1, s) == 0))
347 subseg_set ((segT) i, subseg);
350 #ifdef obj_segment_name
351 s = obj_segment_name ((segT) i);
352 if (strcmp (segname, s) == 0
353 || (segname[0] == '.'
354 && strcmp (segname + 1, s) == 0))
356 subseg_set ((segT) i, subseg);
362 #ifdef obj_add_segment
365 new_seg = obj_add_segment (segname);
366 subseg_set (new_seg, subseg);
370 as_bad ("Attempt to switch to nonexistent segment \"%s\"", segname);
376 subseg_set (seg, subseg) /* begin assembly for a new sub-segment */
377 register segT seg; /* SEG_DATA or SEG_TEXT */
378 register subsegT subseg;
380 #ifndef MANY_SEGMENTS
381 know (seg == SEG_DATA || seg == SEG_TEXT || seg == SEG_BSS);
384 if (seg != now_seg || subseg != now_subseg)
385 { /* we just changed sub-segments */
386 subseg_set_rest (seg, subseg);
390 #else /* BFD_ASSEMBLER */
393 subseg_get (segname, force_new)
398 segment_info_type *seginfo;
399 const char *now_seg_name = (now_seg
400 ? bfd_get_section_name (stdoutput, now_seg)
405 && (now_seg_name == segname
406 || !strcmp (now_seg_name, segname)))
410 secptr = bfd_make_section_old_way (stdoutput, segname);
412 secptr = bfd_make_section_anyway (stdoutput, segname);
414 seginfo = seg_info (secptr);
417 /* Check whether output_section is set first because secptr may
418 be bfd_abs_section_ptr. */
419 if (secptr->output_section != secptr)
420 secptr->output_section = secptr;
421 seginfo = (segment_info_type *) xmalloc (sizeof (*seginfo));
422 memset ((PTR) seginfo, 0, sizeof (*seginfo));
423 seginfo->fix_root = NULL;
424 seginfo->fix_tail = NULL;
425 seginfo->bfd_section = secptr;
426 if (secptr == bfd_abs_section_ptr)
427 abs_seg_info = seginfo;
428 else if (secptr == bfd_und_section_ptr)
429 und_seg_info = seginfo;
431 bfd_set_section_userdata (stdoutput, secptr, (PTR) seginfo);
432 seginfo->frchainP = NULL;
433 seginfo->lineno_list_head = seginfo->lineno_list_tail = NULL;
441 subseg_new (segname, subseg)
446 segment_info_type *seginfo;
448 secptr = subseg_get (segname, 0);
449 subseg_set_rest (secptr, subseg);
450 seginfo = seg_info (secptr);
451 if (! seginfo->frchainP)
452 seginfo->frchainP = frchain_now;
456 /* Like subseg_new, except a new section is always created, even if
457 a section with that name already exists. */
459 subseg_force_new (segname, subseg)
464 segment_info_type *seginfo;
466 secptr = subseg_get (segname, 1);
467 subseg_set_rest (secptr, subseg);
468 seginfo = seg_info (secptr);
469 if (! seginfo->frchainP)
470 seginfo->frchainP = frchain_now;
475 subseg_set (secptr, subseg)
479 if (! (secptr == now_seg && subseg == now_subseg))
480 subseg_set_rest (secptr, subseg);
483 #ifndef obj_sec_sym_ok_for_reloc
484 #define obj_sec_sym_ok_for_reloc(SEC) 0
487 /* Get the gas information we are storing for a section. */
493 if (sec == bfd_abs_section_ptr)
495 else if (sec == bfd_und_section_ptr)
498 return (segment_info_type *) bfd_get_section_userdata (stdoutput, sec);
505 segment_info_type *seginfo = seg_info (sec);
512 s = symbol_find (sec->name);
515 #ifndef EMIT_SECTION_SYMBOLS
516 #define EMIT_SECTION_SYMBOLS 1
519 if (EMIT_SECTION_SYMBOLS
521 && symbol_table_frozen
524 s = symbol_new (sec->name, sec, 0, &zero_address_frag);
526 s = symbol_create (sec->name, sec, 0, &zero_address_frag);
527 S_CLEAR_EXTERNAL (s);
529 /* Use the BFD section symbol, if possible. */
530 if (obj_sec_sym_ok_for_reloc (sec))
531 s->bsym = sec->symbol;
537 #endif /* BFD_ASSEMBLER */
539 /* end of subsegs.c */