frags.c revision 1.1.1.2.2.1 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, 2011, 2012
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 predefined_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 predefined_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 long oldc;
92 long newc;
93
94 /* Try to allocate a bit more than needed right now. But don't do
95 this if we would waste too much memory. Especially necessary
96 for extremely big (like 2GB initialized) frags. */
97 if (nchars < 0x10000)
98 newc = 2 * nchars;
99 else
100 newc = nchars + 0x10000;
101 newc += SIZEOF_STRUCT_FRAG;
102
103 /* Check for possible overflow. */
104 if (newc < 0)
105 as_fatal (_("can't extend frag %u chars"), nchars);
106
107 /* Force to allocate at least NEWC bytes, but not less than the
108 default. */
109 oldc = obstack_chunk_size (&frchain_now->frch_obstack);
110 if (newc > oldc)
111 obstack_chunk_size (&frchain_now->frch_obstack) = newc;
112
113 while (obstack_room (&frchain_now->frch_obstack) < nchars)
114 {
115 /* Not enough room in this frag. Close it and start a new one.
116 This must be done in a loop because the created frag may not
117 be big enough if the current obstack chunk is used. */
118 frag_wane (frag_now);
119 frag_new (0);
120 }
121
122 /* Restore the old chunk size. */
123 obstack_chunk_size (&frchain_now->frch_obstack) = oldc;
124 }
125 }
126
127 /* Call this to close off a completed frag, and start up a new (empty)
129 frag, in the same subsegment as the old frag.
130 [frchain_now remains the same but frag_now is updated.]
131 Because this calculates the correct value of fr_fix by
132 looking at the obstack 'frags', it needs to know how many
133 characters at the end of the old frag belong to the maximal
134 variable part; The rest must belong to fr_fix.
135 It doesn't actually set up the old frag's fr_var. You may have
136 set fr_var == 1, but allocated 10 chars to the end of the frag;
137 In this case you pass old_frags_var_max_size == 10.
138 In fact, you may use fr_var for something totally unrelated to the
139 size of the variable part of the frag; None of the generic frag
140 handling code makes use of fr_var.
141
142 Make a new frag, initialising some components. Link new frag at end
143 of frchain_now. */
144
145 void
146 frag_new (int old_frags_var_max_size
147 /* Number of chars (already allocated on obstack frags) in
148 variable_length part of frag. */)
149 {
150 fragS *former_last_fragP;
151 frchainS *frchP;
152
153 gas_assert (frchain_now->frch_last == frag_now);
154
155 /* Fix up old frag's fr_fix. */
156 frag_now->fr_fix = frag_now_fix_octets () - old_frags_var_max_size;
157 /* Make sure its type is valid. */
158 gas_assert (frag_now->fr_type != 0);
159
160 /* This will align the obstack so the next struct we allocate on it
161 will begin at a correct boundary. */
162 obstack_finish (&frchain_now->frch_obstack);
163 frchP = frchain_now;
164 know (frchP);
165 former_last_fragP = frchP->frch_last;
166 gas_assert (former_last_fragP != 0);
167 gas_assert (former_last_fragP == frag_now);
168 frag_now = frag_alloc (&frchP->frch_obstack);
169
170 as_where (&frag_now->fr_file, &frag_now->fr_line);
171
172 /* Generally, frag_now->points to an address rounded up to next
173 alignment. However, characters will add to obstack frags
174 IMMEDIATELY after the struct frag, even if they are not starting
175 at an alignment address. */
176 former_last_fragP->fr_next = frag_now;
177 frchP->frch_last = frag_now;
178
179 #ifndef NO_LISTING
180 {
181 extern struct list_info_struct *listing_tail;
182 frag_now->line = listing_tail;
183 }
184 #endif
185
186 gas_assert (frchain_now->frch_last == frag_now);
187
188 frag_now->fr_next = NULL;
189 }
190
191 /* Start a new frag unless we have n more chars of room in the current frag.
193 Close off the old frag with a .fill 0.
194
195 Return the address of the 1st char to write into. Advance
196 frag_now_growth past the new chars. */
197
198 char *
199 frag_more (int nchars)
200 {
201 register char *retval;
202
203 frag_alloc_check (&frchain_now->frch_obstack);
204 frag_grow (nchars);
205 retval = obstack_next_free (&frchain_now->frch_obstack);
206 obstack_blank_fast (&frchain_now->frch_obstack, nchars);
207 return (retval);
208 }
209
210 /* Close the current frag, setting its fields for a relaxable frag. Start a
212 new frag. */
213
214 static void
215 frag_var_init (relax_stateT type, int max_chars, int var,
216 relax_substateT subtype, symbolS *symbol, offsetT offset,
217 char *opcode)
218 {
219 frag_now->fr_var = var;
220 frag_now->fr_type = type;
221 frag_now->fr_subtype = subtype;
222 frag_now->fr_symbol = symbol;
223 frag_now->fr_offset = offset;
224 frag_now->fr_opcode = opcode;
225 #ifdef USING_CGEN
226 frag_now->fr_cgen.insn = 0;
227 frag_now->fr_cgen.opindex = 0;
228 frag_now->fr_cgen.opinfo = 0;
229 #endif
230 #ifdef TC_FRAG_INIT
231 TC_FRAG_INIT (frag_now);
232 #endif
233 as_where (&frag_now->fr_file, &frag_now->fr_line);
234
235 frag_new (max_chars);
236 }
237
238 /* Start a new frag unless we have max_chars more chars of room in the
239 current frag. Close off the old frag with a .fill 0.
240
241 Set up a machine_dependent relaxable frag, then start a new frag.
242 Return the address of the 1st char of the var part of the old frag
243 to write into. */
244
245 char *
246 frag_var (relax_stateT type, int max_chars, int var, relax_substateT subtype,
247 symbolS *symbol, offsetT offset, char *opcode)
248 {
249 register char *retval;
250
251 frag_grow (max_chars);
252 retval = obstack_next_free (&frchain_now->frch_obstack);
253 obstack_blank_fast (&frchain_now->frch_obstack, max_chars);
254 frag_var_init (type, max_chars, var, subtype, symbol, offset, opcode);
255 return retval;
256 }
257
258 /* OVE: This variant of frag_var assumes that space for the tail has been
260 allocated by caller.
261 No call to frag_grow is done. */
262
263 char *
264 frag_variant (relax_stateT type, int max_chars, int var,
265 relax_substateT subtype, symbolS *symbol, offsetT offset,
266 char *opcode)
267 {
268 register char *retval;
269
270 retval = obstack_next_free (&frchain_now->frch_obstack);
271 frag_var_init (type, max_chars, var, subtype, symbol, offset, opcode);
272
273 return retval;
274 }
275
276 /* Reduce the variable end of a frag to a harmless state. */
278
279 void
280 frag_wane (register fragS *fragP)
281 {
282 fragP->fr_type = rs_fill;
283 fragP->fr_offset = 0;
284 fragP->fr_var = 0;
285 }
286
287 /* Return the number of bytes by which the current frag can be grown. */
289
290 int
291 frag_room (void)
292 {
293 return obstack_room (&frchain_now->frch_obstack);
294 }
295
296 /* Make an alignment frag. The size of this frag will be adjusted to
298 force the next frag to have the appropriate alignment. ALIGNMENT
299 is the power of two to which to align. FILL_CHARACTER is the
300 character to use to fill in any bytes which are skipped. MAX is
301 the maximum number of characters to skip when doing the alignment,
302 or 0 if there is no maximum. */
303
304 void
305 frag_align (int alignment, int fill_character, int max)
306 {
307 if (now_seg == absolute_section)
308 {
309 addressT new_off;
310 addressT mask;
311
312 mask = (~(addressT) 0) << alignment;
313 new_off = (abs_section_offset + ~mask) & mask;
314 if (max == 0 || new_off - abs_section_offset <= (addressT) max)
315 abs_section_offset = new_off;
316 }
317 else
318 {
319 char *p;
320
321 p = frag_var (rs_align, 1, 1, (relax_substateT) max,
322 (symbolS *) 0, (offsetT) alignment, (char *) 0);
323 *p = fill_character;
324 }
325 }
326
327 /* Make an alignment frag like frag_align, but fill with a repeating
328 pattern rather than a single byte. ALIGNMENT is the power of two
329 to which to align. FILL_PATTERN is the fill pattern to repeat in
330 the bytes which are skipped. N_FILL is the number of bytes in
331 FILL_PATTERN. MAX is the maximum number of characters to skip when
332 doing the alignment, or 0 if there is no maximum. */
333
334 void
335 frag_align_pattern (int alignment, const char *fill_pattern,
336 int n_fill, int max)
337 {
338 char *p;
339
340 p = frag_var (rs_align, n_fill, n_fill, (relax_substateT) max,
341 (symbolS *) 0, (offsetT) alignment, (char *) 0);
342 memcpy (p, fill_pattern, n_fill);
343 }
344
345 /* The NOP_OPCODE is for the alignment fill value. Fill it with a nop
346 instruction so that the disassembler does not choke on it. */
347 #ifndef NOP_OPCODE
348 #define NOP_OPCODE 0x00
349 #endif
350
351 /* Use this to restrict the amount of memory allocated for representing
352 the alignment code. Needs to be large enough to hold any fixed sized
353 prologue plus the replicating portion. */
354 #ifndef MAX_MEM_FOR_RS_ALIGN_CODE
355 /* Assume that if HANDLE_ALIGN is not defined then no special action
356 is required to code fill, which means that we get just repeat the
357 one NOP_OPCODE byte. */
358 # ifndef HANDLE_ALIGN
359 # define MAX_MEM_FOR_RS_ALIGN_CODE 1
360 # else
361 # define MAX_MEM_FOR_RS_ALIGN_CODE ((1 << alignment) - 1)
362 # endif
363 #endif
364
365 void
366 frag_align_code (int alignment, int max)
367 {
368 char *p;
369
370 p = frag_var (rs_align_code, MAX_MEM_FOR_RS_ALIGN_CODE, 1,
371 (relax_substateT) max, (symbolS *) 0,
372 (offsetT) alignment, (char *) 0);
373 *p = NOP_OPCODE;
374 }
375
376 addressT
377 frag_now_fix_octets (void)
378 {
379 if (now_seg == absolute_section)
380 return abs_section_offset;
381
382 return ((char *) obstack_next_free (&frchain_now->frch_obstack)
383 - frag_now->fr_literal);
384 }
385
386 addressT
387 frag_now_fix (void)
388 {
389 return frag_now_fix_octets () / OCTETS_PER_BYTE;
390 }
391
392 void
393 frag_append_1_char (int datum)
394 {
395 frag_alloc_check (&frchain_now->frch_obstack);
396 if (obstack_room (&frchain_now->frch_obstack) <= 1)
397 {
398 frag_wane (frag_now);
399 frag_new (0);
400 }
401 obstack_1grow (&frchain_now->frch_obstack, datum);
402 }
403
404 /* Return TRUE if FRAG1 and FRAG2 have a fixed relationship between
405 their start addresses. Set OFFSET to the difference in address
406 not already accounted for in the frag FR_ADDRESS. */
407
408 bfd_boolean
409 frag_offset_fixed_p (const fragS *frag1, const fragS *frag2, offsetT *offset)
410 {
411 const fragS *frag;
412 offsetT off;
413
414 /* Start with offset initialised to difference between the two frags.
415 Prior to assigning frag addresses this will be zero. */
416 off = frag1->fr_address - frag2->fr_address;
417 if (frag1 == frag2)
418 {
419 *offset = off;
420 return TRUE;
421 }
422
423 /* Maybe frag2 is after frag1. */
424 frag = frag1;
425 while (frag->fr_type == rs_fill)
426 {
427 off += frag->fr_fix + frag->fr_offset * frag->fr_var;
428 frag = frag->fr_next;
429 if (frag == NULL)
430 break;
431 if (frag == frag2)
432 {
433 *offset = off;
434 return TRUE;
435 }
436 }
437
438 /* Maybe frag1 is after frag2. */
439 off = frag1->fr_address - frag2->fr_address;
440 frag = frag2;
441 while (frag->fr_type == rs_fill)
442 {
443 off -= frag->fr_fix + frag->fr_offset * frag->fr_var;
444 frag = frag->fr_next;
445 if (frag == NULL)
446 break;
447 if (frag == frag1)
448 {
449 *offset = off;
450 return TRUE;
451 }
452 }
453
454 return FALSE;
455 }
456