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 #endif /* BFD_ASSEMBLER */
62 static void subseg_set_rest PARAMS ((segT, subsegT));
67 /* Check table(s) seg_name[], seg_N_TYPE[] is in correct order */
68 #if !defined (MANY_SEGMENTS) && !defined (BFD_ASSEMBLER)
69 know (SEG_ABSOLUTE == 0);
73 know (SEG_UNKNOWN == 4);
76 know (SEG_DEBUG == 7);
79 know (SEG_REGISTER == 10);
80 know (SEG_MAXIMUM_ORDINAL == SEG_REGISTER);
83 obstack_begin (&frags, 5000);
85 frchain_now = NULL; /* Warn new_subseg() that we are booting. */
86 /* Fake up 1st frag. It won't be used=> is ok if obstack...
87 pads the end of it for alignment. */
88 frag_now = (fragS *) obstack_alloc (&frags, SIZEOF_STRUCT_FRAG);
89 memset (frag_now, 0, SIZEOF_STRUCT_FRAG);
92 /* This 1st frag will not be in any frchain.
93 We simply give subseg_new somewhere to scribble. */
94 now_subseg = 42; /* Lie for 1st call to subseg_new. */
98 for (i = SEG_E0; i < SEG_UNKNOWN; i++)
101 segment_info[i].frchainP = frchain_now;
105 subseg_set (SEG_DATA, 0); /* .data 0 */
106 data0_frchainP = frchain_now;
108 subseg_set (SEG_BSS, 0);
109 bss0_frchainP = frchain_now;
111 #endif /* ! MANY_SEGMENTS */
112 #endif /* ! BFD_ASSEMBLER */
119 * Change the subsegment we are in, BUT DO NOT MAKE A NEW FRAG for the
120 * subsegment. If we are already in the correct subsegment, change nothing.
121 * This is used eg as a worker for subseg_set [which does make a new frag_now]
122 * and for changing segments after we have read the source. We construct eg
123 * fixSs even after the source file is read, so we do have to keep the
124 * segment context correct.
127 subseg_change (seg, subseg)
136 segment_info_type *seginfo;
137 seginfo = (segment_info_type *) bfd_get_section_userdata (stdoutput, seg);
140 seginfo = (segment_info_type *) xmalloc (sizeof (*seginfo));
143 seginfo->fix_root = 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, 0, sizeof (frchainS));
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;
262 newP->fix_root = NULL;
263 newP->fix_tail = NULL;
267 * Here with frcP ->ing to the frchainS for subseg.
271 * Make a fresh frag for the subsegment.
273 /* We expect this to happen on a correct boundary since it was
274 proceeded by a obstack_done(). */
275 tmp = obstack_alignment_mask (&frags); /* JF disable alignment */
276 obstack_alignment_mask (&frags) = 0;
277 frag_now = (fragS *) obstack_alloc (&frags, SIZEOF_STRUCT_FRAG);
278 memset (frag_now, 0, SIZEOF_STRUCT_FRAG);
279 obstack_alignment_mask (&frags) = tmp;
280 /* But we want any more chars to come immediately after the
281 structure we just made. */
282 new_fragP = frag_now;
283 new_fragP->fr_next = NULL;
285 * Append new frag to current frchain.
287 former_last_fragP = frcP->frch_last;
288 if (former_last_fragP)
290 know (former_last_fragP->fr_next == NULL);
291 know (frchain_now->frch_root);
292 former_last_fragP->fr_next = new_fragP;
296 frcP->frch_root = new_fragP;
298 frcP->frch_last = new_fragP;
302 * subseg_set(segT, subsegT)
304 * If you attempt to change to the current subsegment, nothing happens.
306 * In: segT, subsegT code for new subsegment.
307 * frag_now -> incomplete frag for current subsegment.
308 * If frag_now==NULL, then there is no old, incomplete frag, so
309 * the old frag is not closed off.
311 * Out: now_subseg, now_seg updated.
312 * Frchain_now points to the (possibly new) struct frchain for this
314 * Frchain_root updated if needed.
317 #ifndef BFD_ASSEMBLER
320 subseg_new (segname, subseg)
326 for (i = 0; i < (int) SEG_MAXIMUM_ORDINAL; i++)
330 s = segment_name ((segT) i);
331 if (strcmp (segname, s) == 0
332 || (segname[0] == '.'
333 && strcmp (segname + 1, s) == 0))
335 subseg_set ((segT) i, subseg);
338 #ifdef obj_segment_name
339 s = obj_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);
350 #ifdef obj_add_segment
353 new_seg = obj_add_segment (segname);
354 subseg_set (new_seg, subseg);
358 as_bad ("Attempt to switch to nonexistent segment \"%s\"", segname);
364 subseg_set (seg, subseg) /* begin assembly for a new sub-segment */
365 register segT seg; /* SEG_DATA or SEG_TEXT */
366 register subsegT subseg;
368 #ifndef MANY_SEGMENTS
369 know (seg == SEG_DATA || seg == SEG_TEXT || seg == SEG_BSS);
372 if (seg != now_seg || subseg != now_subseg)
373 { /* we just changed sub-segments */
374 subseg_set_rest (seg, subseg);
378 #else /* BFD_ASSEMBLER */
381 subseg_get (segname, force_new)
386 segment_info_type *seginfo;
387 const char *now_seg_name = (now_seg
388 ? bfd_get_section_name (stdoutput, now_seg)
393 && (now_seg_name == segname
394 || !strcmp (now_seg_name, segname)))
398 secptr = bfd_make_section_old_way (stdoutput, segname);
400 secptr = bfd_make_section_anyway (stdoutput, segname);
402 seginfo = seg_info (secptr);
405 secptr->output_section = secptr;
406 seginfo = (segment_info_type *) xmalloc (sizeof (*seginfo));
407 memset ((char *) seginfo, 0, sizeof(seginfo));
408 seginfo->fix_root = NULL;
409 seginfo->bfd_section = secptr;
410 bfd_set_section_userdata (stdoutput, secptr, (char *) seginfo);
411 seginfo->frchainP = NULL;
412 seginfo->lineno_list_head = seginfo->lineno_list_tail = NULL;
420 subseg_new (segname, subseg)
425 segment_info_type *seginfo;
427 secptr = subseg_get (segname, 0);
428 subseg_set_rest (secptr, subseg);
429 seginfo = seg_info (secptr);
430 if (! seginfo->frchainP)
431 seginfo->frchainP = frchain_now;
435 /* Like subseg_new, except a new section is always created, even if
436 a section with that name already exists. */
438 subseg_force_new (segname, subseg)
443 segment_info_type *seginfo;
445 secptr = subseg_get (segname, 1);
446 subseg_set_rest (secptr, subseg);
447 seginfo = seg_info (secptr);
448 if (! seginfo->frchainP)
449 seginfo->frchainP = frchain_now;
454 subseg_set (secptr, subseg)
458 if (! (secptr == now_seg && subseg == now_subseg))
459 subseg_set_rest (secptr, subseg);
462 #ifndef obj_sec_sym_ok_for_reloc
463 #define obj_sec_sym_ok_for_reloc(SEC) 0
470 segment_info_type *seginfo = seg_info (sec);
477 s = symbol_find (sec->name);
480 s = symbol_new (sec->name, sec, 0, &zero_address_frag);
481 S_CLEAR_EXTERNAL (s);
483 /* Use the BFD section symbol, if possible. */
484 if (obj_sec_sym_ok_for_reloc (sec))
485 s->bsym = sec->symbol;
491 #endif /* BFD_ASSEMBLER */
493 /* end of subsegs.c */