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