frags.c revision 1.1.1.2 1 /* frags.c - manage frags -
2 Copyright 1987, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2003, 2004, 2005, 2006, 2007, 2008, 2009
4 Free Software Foundation, Inc.
5
6 This file is part of GAS, the GNU Assembler.
7
8 GAS is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
12
13 GAS is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GAS; see the file COPYING. If not, write to the Free
20 Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
21 02110-1301, USA. */
22
23 #include "as.h"
24 #include "subsegs.h"
25 #include "obstack.h"
26
27 extern fragS zero_address_frag;
28 extern fragS bss_address_frag;
29
30 /* Initialization for frag routines. */
32
33 void
34 frag_init (void)
35 {
36 zero_address_frag.fr_type = rs_fill;
37 bss_address_frag.fr_type = rs_fill;
38 }
39
40 /* Check that we're not trying to assemble into a section that can't
42 allocate frags (currently, this is only possible in the absolute
43 section), or into an mri common. */
44
45 static void
46 frag_alloc_check (const struct obstack *ob)
47 {
48 if (ob->chunk_size == 0)
49 {
50 as_bad (_("attempt to allocate data in absolute section"));
51 subseg_set (text_section, 0);
52 }
53
54 if (mri_common_symbol != NULL)
55 {
56 as_bad (_("attempt to allocate data in common section"));
57 mri_common_symbol = NULL;
58 }
59 }
60
61 /* Allocate a frag on the specified obstack.
62 Call this routine from everywhere else, so that all the weird alignment
63 hackery can be done in just one place. */
64
65 fragS *
66 frag_alloc (struct obstack *ob)
67 {
68 fragS *ptr;
69 int oalign;
70
71 (void) obstack_alloc (ob, 0);
72 oalign = obstack_alignment_mask (ob);
73 obstack_alignment_mask (ob) = 0;
74 ptr = (fragS *) obstack_alloc (ob, SIZEOF_STRUCT_FRAG);
75 obstack_alignment_mask (ob) = oalign;
76 memset (ptr, 0, SIZEOF_STRUCT_FRAG);
77 return ptr;
78 }
79
80 /* Try to augment current frag by nchars chars.
82 If there is no room, close of the current frag with a ".fill 0"
83 and begin a new frag. Unless the new frag has nchars chars available
84 do not return. Do not set up any fields of *now_frag. */
85
86 void
87 frag_grow (unsigned int nchars)
88 {
89 if (obstack_room (&frchain_now->frch_obstack) < nchars)
90 {
91 unsigned int n;
92 long oldc;
93
94 frag_wane (frag_now);
95 frag_new (0);
96 oldc = frchain_now->frch_obstack.chunk_size;
97 /* Try to allocate a bit more than needed right now. But don't do
98 this if we would waste too much memory. Especially necessary
99 for extremely big (like 2GB initialized) frags. */
100 if (nchars < 0x10000)
101 frchain_now->frch_obstack.chunk_size = 2 * nchars;
102 else
103 frchain_now->frch_obstack.chunk_size = nchars + 0x10000;
104 frchain_now->frch_obstack.chunk_size += SIZEOF_STRUCT_FRAG;
105 if (frchain_now->frch_obstack.chunk_size > 0)
106 while ((n = obstack_room (&frchain_now->frch_obstack)) < nchars
107 && (unsigned long) frchain_now->frch_obstack.chunk_size > nchars)
108 {
109 frag_wane (frag_now);
110 frag_new (0);
111 }
112 frchain_now->frch_obstack.chunk_size = oldc;
113 }
114 if (obstack_room (&frchain_now->frch_obstack) < nchars)
115 as_fatal (_("can't extend frag %u chars"), nchars);
116 }
117
118 /* Call this to close off a completed frag, and start up a new (empty)
120 frag, in the same subsegment as the old frag.
121 [frchain_now remains the same but frag_now is updated.]
122 Because this calculates the correct value of fr_fix by
123 looking at the obstack 'frags', it needs to know how many
124 characters at the end of the old frag belong to the maximal
125 variable part; The rest must belong to fr_fix.
126 It doesn't actually set up the old frag's fr_var. You may have
127 set fr_var == 1, but allocated 10 chars to the end of the frag;
128 In this case you pass old_frags_var_max_size == 10.
129 In fact, you may use fr_var for something totally unrelated to the
130 size of the variable part of the frag; None of the generic frag
131 handling code makes use of fr_var.
132
133 Make a new frag, initialising some components. Link new frag at end
134 of frchain_now. */
135
136 void
137 frag_new (int old_frags_var_max_size
138 /* Number of chars (already allocated on obstack frags) in
139 variable_length part of frag. */)
140 {
141 fragS *former_last_fragP;
142 frchainS *frchP;
143
144 gas_assert (frchain_now->frch_last == frag_now);
145
146 /* Fix up old frag's fr_fix. */
147 frag_now->fr_fix = frag_now_fix_octets () - old_frags_var_max_size;
148 /* Make sure its type is valid. */
149 gas_assert (frag_now->fr_type != 0);
150
151 /* This will align the obstack so the next struct we allocate on it
152 will begin at a correct boundary. */
153 obstack_finish (&frchain_now->frch_obstack);
154 frchP = frchain_now;
155 know (frchP);
156 former_last_fragP = frchP->frch_last;
157 gas_assert (former_last_fragP != 0);
158 gas_assert (former_last_fragP == frag_now);
159 frag_now = frag_alloc (&frchP->frch_obstack);
160
161 as_where (&frag_now->fr_file, &frag_now->fr_line);
162
163 /* Generally, frag_now->points to an address rounded up to next
164 alignment. However, characters will add to obstack frags
165 IMMEDIATELY after the struct frag, even if they are not starting
166 at an alignment address. */
167 former_last_fragP->fr_next = frag_now;
168 frchP->frch_last = frag_now;
169
170 #ifndef NO_LISTING
171 {
172 extern struct list_info_struct *listing_tail;
173 frag_now->line = listing_tail;
174 }
175 #endif
176
177 gas_assert (frchain_now->frch_last == frag_now);
178
179 frag_now->fr_next = NULL;
180 }
181
182 /* Start a new frag unless we have n more chars of room in the current frag.
184 Close off the old frag with a .fill 0.
185
186 Return the address of the 1st char to write into. Advance
187 frag_now_growth past the new chars. */
188
189 char *
190 frag_more (int nchars)
191 {
192 register char *retval;
193
194 frag_alloc_check (&frchain_now->frch_obstack);
195 frag_grow (nchars);
196 retval = obstack_next_free (&frchain_now->frch_obstack);
197 obstack_blank_fast (&frchain_now->frch_obstack, nchars);
198 return (retval);
199 }
200
201 /* Start a new frag unless we have max_chars more chars of room in the
203 current frag. Close off the old frag with a .fill 0.
204
205 Set up a machine_dependent relaxable frag, then start a new frag.
206 Return the address of the 1st char of the var part of the old frag
207 to write into. */
208
209 char *
210 frag_var (relax_stateT type, int max_chars, int var, relax_substateT subtype,
211 symbolS *symbol, offsetT offset, char *opcode)
212 {
213 register char *retval;
214
215 frag_grow (max_chars);
216 retval = obstack_next_free (&frchain_now->frch_obstack);
217 obstack_blank_fast (&frchain_now->frch_obstack, max_chars);
218 frag_now->fr_var = var;
219 frag_now->fr_type = type;
220 frag_now->fr_subtype = subtype;
221 frag_now->fr_symbol = symbol;
222 frag_now->fr_offset = offset;
223 frag_now->fr_opcode = opcode;
224 #ifdef USING_CGEN
225 frag_now->fr_cgen.insn = 0;
226 frag_now->fr_cgen.opindex = 0;
227 frag_now->fr_cgen.opinfo = 0;
228 #endif
229 #ifdef TC_FRAG_INIT
230 TC_FRAG_INIT (frag_now);
231 #endif
232 as_where (&frag_now->fr_file, &frag_now->fr_line);
233 frag_new (max_chars);
234 return (retval);
235 }
236
237 /* OVE: This variant of frag_var assumes that space for the tail has been
239 allocated by caller.
240 No call to frag_grow is done. */
241
242 char *
243 frag_variant (relax_stateT type, int max_chars, int var,
244 relax_substateT subtype, symbolS *symbol, offsetT offset,
245 char *opcode)
246 {
247 register char *retval;
248
249 retval = obstack_next_free (&frchain_now->frch_obstack);
250 frag_now->fr_var = var;
251 frag_now->fr_type = type;
252 frag_now->fr_subtype = subtype;
253 frag_now->fr_symbol = symbol;
254 frag_now->fr_offset = offset;
255 frag_now->fr_opcode = opcode;
256 #ifdef USING_CGEN
257 frag_now->fr_cgen.insn = 0;
258 frag_now->fr_cgen.opindex = 0;
259 frag_now->fr_cgen.opinfo = 0;
260 #endif
261 #ifdef TC_FRAG_INIT
262 TC_FRAG_INIT (frag_now);
263 #endif
264 as_where (&frag_now->fr_file, &frag_now->fr_line);
265 frag_new (max_chars);
266 return (retval);
267 }
268
269 /* Reduce the variable end of a frag to a harmless state. */
271
272 void
273 frag_wane (register fragS *fragP)
274 {
275 fragP->fr_type = rs_fill;
276 fragP->fr_offset = 0;
277 fragP->fr_var = 0;
278 }
279
280 /* Return the number of bytes by which the current frag can be grown. */
282
283 int
284 frag_room (void)
285 {
286 return obstack_room (&frchain_now->frch_obstack);
287 }
288
289 /* Make an alignment frag. The size of this frag will be adjusted to
291 force the next frag to have the appropriate alignment. ALIGNMENT
292 is the power of two to which to align. FILL_CHARACTER is the
293 character to use to fill in any bytes which are skipped. MAX is
294 the maximum number of characters to skip when doing the alignment,
295 or 0 if there is no maximum. */
296
297 void
298 frag_align (int alignment, int fill_character, int max)
299 {
300 if (now_seg == absolute_section)
301 {
302 addressT new_off;
303 addressT mask;
304
305 mask = (~(addressT) 0) << alignment;
306 new_off = (abs_section_offset + ~mask) & mask;
307 if (max == 0 || new_off - abs_section_offset <= (addressT) max)
308 abs_section_offset = new_off;
309 }
310 else
311 {
312 char *p;
313
314 p = frag_var (rs_align, 1, 1, (relax_substateT) max,
315 (symbolS *) 0, (offsetT) alignment, (char *) 0);
316 *p = fill_character;
317 }
318 }
319
320 /* Make an alignment frag like frag_align, but fill with a repeating
321 pattern rather than a single byte. ALIGNMENT is the power of two
322 to which to align. FILL_PATTERN is the fill pattern to repeat in
323 the bytes which are skipped. N_FILL is the number of bytes in
324 FILL_PATTERN. MAX is the maximum number of characters to skip when
325 doing the alignment, or 0 if there is no maximum. */
326
327 void
328 frag_align_pattern (int alignment, const char *fill_pattern,
329 int n_fill, int max)
330 {
331 char *p;
332
333 p = frag_var (rs_align, n_fill, n_fill, (relax_substateT) max,
334 (symbolS *) 0, (offsetT) alignment, (char *) 0);
335 memcpy (p, fill_pattern, n_fill);
336 }
337
338 /* The NOP_OPCODE is for the alignment fill value. Fill it with a nop
339 instruction so that the disassembler does not choke on it. */
340 #ifndef NOP_OPCODE
341 #define NOP_OPCODE 0x00
342 #endif
343
344 /* Use this to restrict the amount of memory allocated for representing
345 the alignment code. Needs to be large enough to hold any fixed sized
346 prologue plus the replicating portion. */
347 #ifndef MAX_MEM_FOR_RS_ALIGN_CODE
348 /* Assume that if HANDLE_ALIGN is not defined then no special action
349 is required to code fill, which means that we get just repeat the
350 one NOP_OPCODE byte. */
351 # ifndef HANDLE_ALIGN
352 # define MAX_MEM_FOR_RS_ALIGN_CODE 1
353 # else
354 # define MAX_MEM_FOR_RS_ALIGN_CODE ((1 << alignment) - 1)
355 # endif
356 #endif
357
358 void
359 frag_align_code (int alignment, int max)
360 {
361 char *p;
362
363 p = frag_var (rs_align_code, MAX_MEM_FOR_RS_ALIGN_CODE, 1,
364 (relax_substateT) max, (symbolS *) 0,
365 (offsetT) alignment, (char *) 0);
366 *p = NOP_OPCODE;
367 }
368
369 addressT
370 frag_now_fix_octets (void)
371 {
372 if (now_seg == absolute_section)
373 return abs_section_offset;
374
375 return ((char *) obstack_next_free (&frchain_now->frch_obstack)
376 - frag_now->fr_literal);
377 }
378
379 addressT
380 frag_now_fix (void)
381 {
382 return frag_now_fix_octets () / OCTETS_PER_BYTE;
383 }
384
385 void
386 frag_append_1_char (int datum)
387 {
388 frag_alloc_check (&frchain_now->frch_obstack);
389 if (obstack_room (&frchain_now->frch_obstack) <= 1)
390 {
391 frag_wane (frag_now);
392 frag_new (0);
393 }
394 obstack_1grow (&frchain_now->frch_obstack, datum);
395 }
396
397 /* Return TRUE if FRAG1 and FRAG2 have a fixed relationship between
398 their start addresses. Set OFFSET to the difference in address
399 not already accounted for in the frag FR_ADDRESS. */
400
401 bfd_boolean
402 frag_offset_fixed_p (const fragS *frag1, const fragS *frag2, bfd_vma *offset)
403 {
404 const fragS *frag;
405 bfd_vma off;
406
407 /* Start with offset initialised to difference between the two frags.
408 Prior to assigning frag addresses this will be zero. */
409 off = frag1->fr_address - frag2->fr_address;
410 if (frag1 == frag2)
411 {
412 *offset = off;
413 return TRUE;
414 }
415
416 /* Maybe frag2 is after frag1. */
417 frag = frag1;
418 while (frag->fr_type == rs_fill)
419 {
420 off += frag->fr_fix + frag->fr_offset * frag->fr_var;
421 frag = frag->fr_next;
422 if (frag == NULL)
423 break;
424 if (frag == frag2)
425 {
426 *offset = off;
427 return TRUE;
428 }
429 }
430
431 /* Maybe frag1 is after frag2. */
432 off = frag1->fr_address - frag2->fr_address;
433 frag = frag2;
434 while (frag->fr_type == rs_fill)
435 {
436 off -= frag->fr_fix + frag->fr_offset * frag->fr_var;
437 frag = frag->fr_next;
438 if (frag == NULL)
439 break;
440 if (frag == frag1)
441 {
442 *offset = off;
443 return TRUE;
444 }
445 }
446
447 return FALSE;
448 }
449