* target-descriptions.c (tdesc_named_type): Move code_ptr and data_ptr
[deliverable/binutils-gdb.git] / gas / subsegs.c
1 /* subsegs.c - subsegments -
2 Copyright 1987, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006
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 2, 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 /* Segments & sub-segments. */
24
25 #include "as.h"
26
27 #include "subsegs.h"
28 #include "obstack.h"
29
30 frchainS *frchain_now;
31
32 static struct obstack frchains;
33
34 static fragS dummy_frag;
35
36 \f
37 void
38 subsegs_begin (void)
39 {
40 obstack_begin (&frchains, chunksize);
41 #if __GNUC__ >= 2
42 obstack_alignment_mask (&frchains) = __alignof__ (frchainS) - 1;
43 #endif
44
45 frchain_now = NULL; /* Warn new_subseg() that we are booting. */
46 frag_now = &dummy_frag;
47 }
48 \f
49 /*
50 * subseg_change()
51 *
52 * Change the subsegment we are in, BUT DO NOT MAKE A NEW FRAG for the
53 * subsegment. If we are already in the correct subsegment, change nothing.
54 * This is used eg as a worker for subseg_set [which does make a new frag_now]
55 * and for changing segments after we have read the source. We construct eg
56 * fixSs even after the source file is read, so we do have to keep the
57 * segment context correct.
58 */
59 void
60 subseg_change (register segT seg, register int subseg)
61 {
62 segment_info_type *seginfo = seg_info (seg);
63 now_seg = seg;
64 now_subseg = subseg;
65
66 if (! seginfo)
67 {
68 seginfo = (segment_info_type *) xmalloc (sizeof (*seginfo));
69 memset ((PTR) seginfo, 0, sizeof (*seginfo));
70 seginfo->fix_root = NULL;
71 seginfo->fix_tail = NULL;
72 seginfo->bfd_section = seg;
73 seginfo->sym = 0;
74 bfd_set_section_userdata (stdoutput, seg, (PTR) seginfo);
75 }
76 }
77 \f
78 static void
79 subseg_set_rest (segT seg, subsegT subseg)
80 {
81 frchainS *frcP; /* crawl frchain chain */
82 frchainS **lastPP; /* address of last pointer */
83 frchainS *newP; /* address of new frchain */
84 segment_info_type *seginfo;
85
86 mri_common_symbol = NULL;
87
88 if (frag_now && frchain_now)
89 frchain_now->frch_frag_now = frag_now;
90
91 assert (frchain_now == 0
92 || frchain_now->frch_last == frag_now);
93
94 subseg_change (seg, (int) subseg);
95
96 seginfo = seg_info (seg);
97
98 /* Attempt to find or make a frchain for that subsection.
99 We keep the list sorted by subsection number. */
100 for (frcP = *(lastPP = &seginfo->frchainP);
101 frcP != NULL;
102 frcP = *(lastPP = &frcP->frch_next))
103 if (frcP->frch_subseg >= subseg)
104 break;
105
106 if (frcP == NULL || frcP->frch_subseg != subseg)
107 {
108 /* This should be the only code that creates a frchainS. */
109
110 newP = (frchainS *) obstack_alloc (&frchains, sizeof (frchainS));
111 newP->frch_subseg = subseg;
112 newP->fix_root = NULL;
113 newP->fix_tail = NULL;
114 obstack_begin (&newP->frch_obstack, chunksize);
115 #if __GNUC__ >= 2
116 obstack_alignment_mask (&newP->frch_obstack) = __alignof__ (fragS) - 1;
117 #endif
118 newP->frch_frag_now = frag_alloc (&newP->frch_obstack);
119 newP->frch_frag_now->fr_type = rs_fill;
120 newP->frch_cfi_data = NULL;
121
122 newP->frch_root = newP->frch_last = newP->frch_frag_now;
123
124 *lastPP = newP;
125 newP->frch_next = frcP;
126 frcP = newP;
127 }
128
129 frchain_now = frcP;
130 frag_now = frcP->frch_frag_now;
131
132 assert (frchain_now->frch_last == frag_now);
133 }
134
135 /*
136 * subseg_set(segT, subsegT)
137 *
138 * If you attempt to change to the current subsegment, nothing happens.
139 *
140 * In: segT, subsegT code for new subsegment.
141 * frag_now -> incomplete frag for current subsegment.
142 * If frag_now==NULL, then there is no old, incomplete frag, so
143 * the old frag is not closed off.
144 *
145 * Out: now_subseg, now_seg updated.
146 * Frchain_now points to the (possibly new) struct frchain for this
147 * sub-segment.
148 */
149
150 segT
151 subseg_get (const char *segname, int force_new)
152 {
153 segT secptr;
154 segment_info_type *seginfo;
155 const char *now_seg_name = (now_seg
156 ? bfd_get_section_name (stdoutput, now_seg)
157 : 0);
158
159 if (!force_new
160 && now_seg_name
161 && (now_seg_name == segname
162 || !strcmp (now_seg_name, segname)))
163 return now_seg;
164
165 if (!force_new)
166 secptr = bfd_make_section_old_way (stdoutput, segname);
167 else
168 secptr = bfd_make_section_anyway (stdoutput, segname);
169
170 seginfo = seg_info (secptr);
171 if (! seginfo)
172 {
173 secptr->output_section = secptr;
174 seginfo = (segment_info_type *) xmalloc (sizeof (*seginfo));
175 memset ((PTR) seginfo, 0, sizeof (*seginfo));
176 seginfo->fix_root = NULL;
177 seginfo->fix_tail = NULL;
178 seginfo->bfd_section = secptr;
179 bfd_set_section_userdata (stdoutput, secptr, (PTR) seginfo);
180 seginfo->frchainP = NULL;
181 seginfo->lineno_list_head = seginfo->lineno_list_tail = NULL;
182 seginfo->sym = NULL;
183 seginfo->dot = NULL;
184 }
185 return secptr;
186 }
187
188 segT
189 subseg_new (const char *segname, subsegT subseg)
190 {
191 segT secptr;
192
193 secptr = subseg_get (segname, 0);
194 subseg_set_rest (secptr, subseg);
195 return secptr;
196 }
197
198 /* Like subseg_new, except a new section is always created, even if
199 a section with that name already exists. */
200 segT
201 subseg_force_new (const char *segname, subsegT subseg)
202 {
203 segT secptr;
204
205 secptr = subseg_get (segname, 1);
206 subseg_set_rest (secptr, subseg);
207 return secptr;
208 }
209
210 void
211 subseg_set (segT secptr, subsegT subseg)
212 {
213 if (! (secptr == now_seg && subseg == now_subseg))
214 subseg_set_rest (secptr, subseg);
215 mri_common_symbol = NULL;
216 }
217
218 #ifndef obj_sec_sym_ok_for_reloc
219 #define obj_sec_sym_ok_for_reloc(SEC) 0
220 #endif
221
222 symbolS *
223 section_symbol (segT sec)
224 {
225 segment_info_type *seginfo = seg_info (sec);
226 symbolS *s;
227
228 if (seginfo == 0)
229 abort ();
230 if (seginfo->sym)
231 return seginfo->sym;
232
233 #ifndef EMIT_SECTION_SYMBOLS
234 #define EMIT_SECTION_SYMBOLS 1
235 #endif
236
237 if (! EMIT_SECTION_SYMBOLS || symbol_table_frozen)
238 {
239 /* Here we know it won't be going into the symbol table. */
240 s = symbol_create (sec->symbol->name, sec, 0, &zero_address_frag);
241 }
242 else
243 {
244 segT seg;
245 s = symbol_find (sec->symbol->name);
246 /* We have to make sure it is the right symbol when we
247 have multiple sections with the same section name. */
248 if (s == NULL
249 || ((seg = S_GET_SEGMENT (s)) != sec
250 && seg != undefined_section))
251 s = symbol_new (sec->symbol->name, sec, 0, &zero_address_frag);
252 else if (seg == undefined_section)
253 {
254 S_SET_SEGMENT (s, sec);
255 symbol_set_frag (s, &zero_address_frag);
256 }
257 }
258
259 S_CLEAR_EXTERNAL (s);
260
261 /* Use the BFD section symbol, if possible. */
262 if (obj_sec_sym_ok_for_reloc (sec))
263 symbol_set_bfdsym (s, sec->symbol);
264 else
265 symbol_get_bfdsym (s)->flags |= BSF_SECTION_SYM;
266
267 seginfo->sym = s;
268 return s;
269 }
270
271 /* Return whether the specified segment is thought to hold text. */
272
273 int
274 subseg_text_p (segT sec)
275 {
276 return (bfd_get_section_flags (stdoutput, sec) & SEC_CODE) != 0;
277 }
278
279 /* Return non zero if SEC has at least one byte of data. It is
280 possible that we'll return zero even on a non-empty section because
281 we don't know all the fragment types, and it is possible that an
282 fr_fix == 0 one still contributes data. Think of this as
283 seg_definitely_not_empty_p. */
284
285 int
286 seg_not_empty_p (segT sec ATTRIBUTE_UNUSED)
287 {
288 segment_info_type *seginfo = seg_info (sec);
289 frchainS *chain;
290 fragS *frag;
291
292 if (!seginfo)
293 return 0;
294
295 for (chain = seginfo->frchainP; chain; chain = chain->frch_next)
296 {
297 for (frag = chain->frch_root; frag; frag = frag->fr_next)
298 if (frag->fr_fix)
299 return 1;
300 if (obstack_next_free (&chain->frch_obstack)
301 != chain->frch_last->fr_literal)
302 return 1;
303 }
304 return 0;
305 }
306
307 void
308 subsegs_print_statistics (FILE *file)
309 {
310 frchainS *frchp;
311 asection *s;
312
313 fprintf (file, "frag chains:\n");
314 for (s = stdoutput->sections; s; s = s->next)
315 {
316 segment_info_type *seginfo;
317
318 /* Skip gas-internal sections. */
319 if (segment_name (s)[0] == '*')
320 continue;
321
322 seginfo = seg_info (s);
323 if (!seginfo)
324 continue;
325
326 for (frchp = seginfo->frchainP; frchp; frchp = frchp->frch_next)
327 {
328 int count = 0;
329 fragS *fragp;
330
331 for (fragp = frchp->frch_root; fragp; fragp = fragp->fr_next)
332 count++;
333
334 fprintf (file, "\n");
335 fprintf (file, "\t%p %-10s\t%10d frags\n", (void *) frchp,
336 segment_name (s), count);
337 }
338 }
339 }
340
341 /* end of subsegs.c */
This page took 0.036146 seconds and 4 git commands to generate.