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));
150 seginfo->fix_root = NULL;
151 seginfo->fix_tail = NULL;
152 seginfo->bfd_section = seg;
154 if (seg == bfd_abs_section_ptr)
155 abs_seg_info = seginfo;
156 else if (seg == bfd_und_section_ptr)
157 und_seg_info = seginfo;
159 bfd_set_section_userdata (stdoutput, seg, (PTR) seginfo);
164 seg_fix_rootP = &segment_info[seg].fix_root;
165 seg_fix_tailP = &segment_info[seg].fix_tail;
169 seg_fix_rootP = &data_fix_root;
170 seg_fix_tailP = &data_fix_tail;
172 else if (seg == SEG_TEXT)
174 seg_fix_rootP = &text_fix_root;
175 seg_fix_tailP = &text_fix_tail;
179 know (seg == SEG_BSS);
180 seg_fix_rootP = &bss_fix_root;
181 seg_fix_tailP = &bss_fix_tail;
189 subseg_set_rest (seg, subseg)
193 long tmp; /* JF for obstack alignment hacking */
194 register frchainS *frcP; /* crawl frchain chain */
195 register frchainS **lastPP; /* address of last pointer */
196 frchainS *newP; /* address of new frchain */
197 register fragS *former_last_fragP;
198 register fragS *new_fragP;
200 if (frag_now) /* If not bootstrapping. */
202 frag_now->fr_fix = (char*) obstack_next_free (&frags) - frag_now->fr_literal;
203 frag_wane (frag_now); /* Close off any frag in old subseg. */
206 * It would be nice to keep an obstack for each subsegment, if we swap
207 * subsegments a lot. Hence we would have much fewer frag_wanes().
210 obstack_finish (&frags);
212 * If we don't do the above, the next object we put on obstack frags
213 * will appear to start at the fr_literal of the current frag.
214 * Also, above ensures that the next object will begin on a
215 * address that is aligned correctly for the engine that runs
219 subseg_change (seg, (int) subseg);
221 * Attempt to find or make a frchain for that sub seg.
222 * Crawl along chain of frchainSs, begins @ frchain_root.
223 * If we need to make a frchainS, link it into correct
224 * position of chain rooted in frchain_root.
226 for (frcP = *(lastPP = &frchain_root);
227 frcP && (int) (frcP->frch_seg) <= (int) seg;
228 frcP = *(lastPP = &frcP->frch_next))
230 if ((int) (frcP->frch_seg) == (int) seg
231 && frcP->frch_subseg >= subseg)
237 * frcP: Address of the 1st frchainS in correct segment with
238 * frch_subseg >= subseg.
239 * We want to either use this frchainS, or we want
240 * to insert a new frchainS just before it.
242 * If frcP==NULL, then we are at the end of the chain
243 * of frchainS-s. A NULL frcP means we fell off the end
244 * of the chain looking for a
245 * frch_subseg >= subseg, so we
246 * must make a new frchainS.
248 * If we ever maintain a pointer to
249 * the last frchainS in the chain, we change that pointer
250 * ONLY when frcP==NULL.
252 * lastPP: Address of the pointer with value frcP;
254 * May point to frchain_root.
258 || ((int) (frcP->frch_seg) > (int) seg
259 || frcP->frch_subseg > subseg)) /* Kinky logic only works with 2 segments. */
262 * This should be the only code that creates a frchainS.
264 newP = (frchainS *) obstack_alloc (&frags, sizeof (frchainS));
265 memset (newP, 0, sizeof (frchainS));
266 /* This begines on a good boundary because a obstack_done()
267 preceeded it. It implies an obstack_done(), so we expect
268 the next object allocated to begin on a correct boundary. */
270 newP->frch_next = frcP; /* perhaps NULL */
271 (frcP = newP)->frch_subseg = subseg;
272 newP->frch_seg = seg;
273 newP->frch_last = NULL;
275 newP->fix_root = NULL;
276 newP->fix_tail = NULL;
280 * Here with frcP ->ing to the frchainS for subseg.
284 * Make a fresh frag for the subsegment.
286 /* We expect this to happen on a correct boundary since it was
287 proceeded by a obstack_done(). */
288 tmp = obstack_alignment_mask (&frags); /* JF disable alignment */
289 obstack_alignment_mask (&frags) = 0;
290 frag_now = (fragS *) obstack_alloc (&frags, SIZEOF_STRUCT_FRAG);
291 memset (frag_now, 0, SIZEOF_STRUCT_FRAG);
292 obstack_alignment_mask (&frags) = tmp;
293 /* But we want any more chars to come immediately after the
294 structure we just made. */
295 new_fragP = frag_now;
296 new_fragP->fr_next = NULL;
298 * Append new frag to current frchain.
300 former_last_fragP = frcP->frch_last;
301 if (former_last_fragP)
303 know (former_last_fragP->fr_next == NULL);
304 know (frchain_now->frch_root);
305 former_last_fragP->fr_next = new_fragP;
309 frcP->frch_root = new_fragP;
311 frcP->frch_last = new_fragP;
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);
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 ((char *) 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);
484 #ifndef obj_sec_sym_ok_for_reloc
485 #define obj_sec_sym_ok_for_reloc(SEC) 0
488 /* Get the gas information we are storing for a section. */
494 if (sec == bfd_abs_section_ptr)
496 else if (sec == bfd_und_section_ptr)
499 return (segment_info_type *) bfd_get_section_userdata (stdoutput, sec);
506 segment_info_type *seginfo = seg_info (sec);
513 s = symbol_find (sec->name);
516 s = symbol_new (sec->name, sec, 0, &zero_address_frag);
517 S_CLEAR_EXTERNAL (s);
519 /* Use the BFD section symbol, if possible. */
520 if (obj_sec_sym_ok_for_reloc (sec))
521 s->bsym = sec->symbol;
527 #endif /* BFD_ASSEMBLER */
529 /* end of subsegs.c */