1 /* subsegs.c - subsegments -
2 Copyright (C) 1987, 1990, 1991, 1992 Free Software Foundation, Inc.
4 This file is part of GAS, the GNU Assembler.
6 GAS is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GAS is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GAS; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
21 * Segments & sub-segments.
29 frchainS *frchain_root, *frchain_now;
33 segment_info_type segment_info[SEG_MAXIMUM_ORDINAL];
36 /* Commented in "subsegs.h". */
37 frchainS *data0_frchainP, *bss0_frchainP;
39 #endif /* MANY_SEGMENTS */
40 char *const seg_name[] =
44 "e0", "e1", "e2", "e3", "e4", "e5", "e6", "e7", "e8", "e9",
49 #endif /* MANY_SEGMENTS */
51 "ASSEMBLER-INTERNAL-LOGIC-ERROR!",
54 "transfert vector preload",
55 "transfert vector postload",
58 }; /* Used by error reporters, dumpers etc. */
59 #endif /* BFD_ASSEMBLER */
61 static void subseg_set_rest PARAMS ((segT, subsegT));
66 /* Check table(s) seg_name[], seg_N_TYPE[] is in correct order */
67 #if !defined (MANY_SEGMENTS) && !defined (BFD_ASSEMBLER)
68 know (SEG_ABSOLUTE == 0);
72 know (SEG_UNKNOWN == 4);
75 know (SEG_DEBUG == 7);
78 know (SEG_REGISTER == 10);
79 know (SEG_MAXIMUM_ORDINAL == SEG_REGISTER);
82 obstack_begin (&frags, 5000);
84 frchain_now = NULL; /* Warn new_subseg() that we are booting. */
85 /* Fake up 1st frag. It won't be used=> is ok if obstack...
86 pads the end of it for alignment. */
87 frag_now = (fragS *) obstack_alloc (&frags, SIZEOF_STRUCT_FRAG);
88 memset (frag_now, SIZEOF_STRUCT_FRAG, 0);
91 /* This 1st frag will not be in any frchain.
92 We simply give subseg_new somewhere to scribble. */
93 now_subseg = 42; /* Lie for 1st call to subseg_new. */
97 for (i = SEG_E0; i < SEG_UNKNOWN; i++)
100 segment_info[i].frchainP = frchain_now;
104 subseg_set (SEG_DATA, 0); /* .data 0 */
105 data0_frchainP = frchain_now;
107 subseg_set (SEG_BSS, 0);
108 bss0_frchainP = frchain_now;
110 #endif /* ! MANY_SEGMENTS */
111 #endif /* ! BFD_ASSEMBLER */
118 * Change the subsegment we are in, BUT DO NOT MAKE A NEW FRAG for the
119 * subsegment. If we are already in the correct subsegment, change nothing.
120 * This is used eg as a worker for subseg_set [which does make a new frag_now]
121 * and for changing segments after we have read the source. We construct eg
122 * fixSs even after the source file is read, so we do have to keep the
123 * segment context correct.
126 subseg_change (seg, subseg)
135 segment_info_type *seginfo;
136 seginfo = (segment_info_type *) bfd_get_section_userdata (stdoutput, seg);
139 seginfo = (segment_info_type *) xmalloc (sizeof (*seginfo));
142 seginfo->fix_root = 0;
143 seginfo->fix_tail = 0;
144 seginfo->bfd_section = seg;
146 bfd_set_section_userdata (stdoutput, seg, (char *) seginfo);
151 seg_fix_rootP = &segment_info[seg].fix_root;
152 seg_fix_tailP = &segment_info[seg].fix_tail;
156 seg_fix_rootP = &data_fix_root;
157 seg_fix_tailP = &data_fix_tail;
159 else if (seg == SEG_TEXT)
161 seg_fix_rootP = &text_fix_root;
162 seg_fix_tailP = &text_fix_tail;
166 know (seg == SEG_BSS);
167 seg_fix_rootP = &bss_fix_root;
168 seg_fix_tailP = &bss_fix_tail;
176 subseg_set_rest (seg, subseg)
180 long tmp; /* JF for obstack alignment hacking */
181 register frchainS *frcP; /* crawl frchain chain */
182 register frchainS **lastPP; /* address of last pointer */
183 frchainS *newP; /* address of new frchain */
184 register fragS *former_last_fragP;
185 register fragS *new_fragP;
187 if (frag_now) /* If not bootstrapping. */
189 frag_now->fr_fix = (char*) obstack_next_free (&frags) - frag_now->fr_literal;
190 frag_wane (frag_now); /* Close off any frag in old subseg. */
193 * It would be nice to keep an obstack for each subsegment, if we swap
194 * subsegments a lot. Hence we would have much fewer frag_wanes().
197 obstack_finish (&frags);
199 * If we don't do the above, the next object we put on obstack frags
200 * will appear to start at the fr_literal of the current frag.
201 * Also, above ensures that the next object will begin on a
202 * address that is aligned correctly for the engine that runs
206 subseg_change (seg, (int) subseg);
208 * Attempt to find or make a frchain for that sub seg.
209 * Crawl along chain of frchainSs, begins @ frchain_root.
210 * If we need to make a frchainS, link it into correct
211 * position of chain rooted in frchain_root.
213 for (frcP = *(lastPP = &frchain_root);
214 frcP && (int) (frcP->frch_seg) <= (int) seg;
215 frcP = *(lastPP = &frcP->frch_next))
217 if ((int) (frcP->frch_seg) == (int) seg
218 && frcP->frch_subseg >= subseg)
224 * frcP: Address of the 1st frchainS in correct segment with
225 * frch_subseg >= subseg.
226 * We want to either use this frchainS, or we want
227 * to insert a new frchainS just before it.
229 * If frcP==NULL, then we are at the end of the chain
230 * of frchainS-s. A NULL frcP means we fell off the end
231 * of the chain looking for a
232 * frch_subseg >= subseg, so we
233 * must make a new frchainS.
235 * If we ever maintain a pointer to
236 * the last frchainS in the chain, we change that pointer
237 * ONLY when frcP==NULL.
239 * lastPP: Address of the pointer with value frcP;
241 * May point to frchain_root.
245 || ((int) (frcP->frch_seg) > (int) seg
246 || frcP->frch_subseg > subseg)) /* Kinky logic only works with 2 segments. */
249 * This should be the only code that creates a frchainS.
251 newP = (frchainS *) obstack_alloc (&frags, sizeof (frchainS));
252 memset (newP, sizeof (frchainS), 0);
253 /* This begines on a good boundary because a obstack_done()
254 preceeded it. It implies an obstack_done(), so we expect
255 the next object allocated to begin on a correct boundary. */
257 newP->frch_next = frcP; /* perhaps NULL */
258 (frcP = newP)->frch_subseg = subseg;
259 newP->frch_seg = seg;
260 newP->frch_last = NULL;
263 * Here with frcP ->ing to the frchainS for subseg.
267 * Make a fresh frag for the subsegment.
269 /* We expect this to happen on a correct boundary since it was
270 proceeded by a obstack_done(). */
271 tmp = obstack_alignment_mask (&frags); /* JF disable alignment */
272 obstack_alignment_mask (&frags) = 0;
273 frag_now = (fragS *) obstack_alloc (&frags, SIZEOF_STRUCT_FRAG);
274 memset (frag_now, 0, SIZEOF_STRUCT_FRAG);
275 obstack_alignment_mask (&frags) = tmp;
276 /* But we want any more chars to come immediately after the
277 structure we just made. */
278 new_fragP = frag_now;
279 new_fragP->fr_next = NULL;
281 * Append new frag to current frchain.
283 former_last_fragP = frcP->frch_last;
284 if (former_last_fragP)
286 know (former_last_fragP->fr_next == NULL);
287 know (frchain_now->frch_root);
288 former_last_fragP->fr_next = new_fragP;
292 frcP->frch_root = new_fragP;
294 frcP->frch_last = new_fragP;
298 * subseg_set(segT, subsegT)
300 * If you attempt to change to the current subsegment, nothing happens.
302 * In: segT, subsegT code for new subsegment.
303 * frag_now -> incomplete frag for current subsegment.
304 * If frag_now==NULL, then there is no old, incomplete frag, so
305 * the old frag is not closed off.
307 * Out: now_subseg, now_seg updated.
308 * Frchain_now points to the (possibly new) struct frchain for this
310 * Frchain_root updated if needed.
313 #ifndef BFD_ASSEMBLER
316 subseg_new (segname, subseg)
322 for (i = 0; i < (int) SEG_MAXIMUM_ORDINAL; i++)
326 s = segment_name ((segT) i);
327 if (strcmp (segname, s) == 0
328 || (segname[0] == '.'
329 && strcmp (segname + 1, s) == 0))
331 subseg_set ((segT) i, subseg);
334 #ifdef obj_segment_name
335 s = obj_segment_name ((segT) i);
336 if (strcmp (segname, s) == 0
337 || (segname[0] == '.'
338 && strcmp (segname + 1, s) == 0))
340 subseg_set ((segT) i, subseg);
346 #ifdef obj_add_segment
349 new_seg = obj_add_segment (segname);
350 subseg_set (new_seg, subseg);
354 as_bad ("Attempt to switch to nonexistent segment \"%s\"", segname);
360 subseg_set (seg, subseg) /* begin assembly for a new sub-segment */
361 register segT seg; /* SEG_DATA or SEG_TEXT */
362 register subsegT subseg;
364 #ifndef MANY_SEGMENTS
365 know (seg == SEG_DATA || seg == SEG_TEXT || seg == SEG_BSS);
368 if (seg != now_seg || subseg != now_subseg)
369 { /* we just changed sub-segments */
370 subseg_set_rest (seg, subseg);
374 #else /* BFD_ASSEMBLER */
377 subseg_get (segname, force_new)
382 segment_info_type *seginfo;
383 const char *now_seg_name = (now_seg
384 ? bfd_get_section_name (stdoutput, now_seg)
389 && (now_seg_name == segname
390 || !strcmp (now_seg_name, segname)))
394 secptr = bfd_make_section_old_way (stdoutput, segname);
396 secptr = bfd_make_section_anyway (stdoutput, segname);
398 seginfo = seg_info (secptr);
401 secptr->output_section = secptr;
402 seginfo = (segment_info_type *) xmalloc (sizeof (*seginfo));
403 seginfo->fix_root = 0;
404 seginfo->fix_tail = 0;
405 seginfo->bfd_section = secptr;
406 bfd_set_section_userdata (stdoutput, secptr, (char *) seginfo);
407 seginfo->frchainP = 0;
408 seginfo->lineno_list_head = seginfo->lineno_list_tail = 0;
412 seginfo->user_stuff = 0;
413 seginfo->stabu.stab_string_size = 0;
419 subseg_new (segname, subseg)
424 segment_info_type *seginfo;
426 secptr = subseg_get (segname, 0);
427 subseg_set_rest (secptr, subseg);
428 seginfo = seg_info (secptr);
429 if (! seginfo->frchainP)
430 seginfo->frchainP = frchain_now;
434 /* Like subseg_new, except a new section is always created, even if
435 a section with that name already exists. */
437 subseg_force_new (segname, subseg)
442 segment_info_type *seginfo;
444 secptr = subseg_get (segname, 1);
445 subseg_set_rest (secptr, subseg);
446 seginfo = seg_info (secptr);
447 if (! seginfo->frchainP)
448 seginfo->frchainP = frchain_now;
453 subseg_set (secptr, subseg)
457 if (! (secptr == now_seg && subseg == now_subseg))
458 subseg_set_rest (secptr, subseg);
465 segment_info_type *seginfo = seg_info (sec);
471 seginfo->sym = symbol_find (sec->name);
474 seginfo->sym = symbol_make (sec->name);
475 seginfo->sym->bsym = sec->symbol;
480 #endif /* BFD_ASSEMBLER */
482 /* end of subsegs.c */