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