Commit | Line | Data |
---|---|---|
252b5132 | 1 | /* subsegs.c - subsegments - |
6f2750fe | 2 | Copyright (C) 1987-2016 Free Software Foundation, Inc. |
252b5132 RH |
3 | |
4 | This file is part of GAS, the GNU Assembler. | |
5 | ||
6 | GAS is free software; you can redistribute it and/or modify | |
7 | it under the terms of the GNU General Public License as published by | |
ec2655a6 | 8 | the Free Software Foundation; either version 3, or (at your option) |
252b5132 RH |
9 | any later version. |
10 | ||
11 | GAS is distributed in the hope that it will be useful, | |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | GNU General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU General Public License | |
17 | along with GAS; see the file COPYING. If not, write to the Free | |
4b4da160 NC |
18 | Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA |
19 | 02110-1301, USA. */ | |
252b5132 | 20 | |
ef99799a | 21 | /* Segments & sub-segments. */ |
252b5132 RH |
22 | |
23 | #include "as.h" | |
24 | ||
25 | #include "subsegs.h" | |
26 | #include "obstack.h" | |
27 | ||
c9049d30 | 28 | frchainS *frchain_now; |
252b5132 RH |
29 | |
30 | static struct obstack frchains; | |
31 | ||
252b5132 RH |
32 | static fragS dummy_frag; |
33 | ||
252b5132 RH |
34 | \f |
35 | void | |
24361518 | 36 | subsegs_begin (void) |
252b5132 | 37 | { |
252b5132 RH |
38 | obstack_begin (&frchains, chunksize); |
39 | #if __GNUC__ >= 2 | |
40 | obstack_alignment_mask (&frchains) = __alignof__ (frchainS) - 1; | |
41 | #endif | |
42 | ||
f0e652b4 | 43 | frchain_now = NULL; /* Warn new_subseg() that we are booting. */ |
252b5132 | 44 | frag_now = &dummy_frag; |
252b5132 RH |
45 | } |
46 | \f | |
47 | /* | |
48 | * subseg_change() | |
49 | * | |
50 | * Change the subsegment we are in, BUT DO NOT MAKE A NEW FRAG for the | |
51 | * subsegment. If we are already in the correct subsegment, change nothing. | |
52 | * This is used eg as a worker for subseg_set [which does make a new frag_now] | |
53 | * and for changing segments after we have read the source. We construct eg | |
54 | * fixSs even after the source file is read, so we do have to keep the | |
55 | * segment context correct. | |
56 | */ | |
57 | void | |
ed9e98c2 | 58 | subseg_change (segT seg, int subseg) |
252b5132 | 59 | { |
c9049d30 | 60 | segment_info_type *seginfo = seg_info (seg); |
252b5132 RH |
61 | now_seg = seg; |
62 | now_subseg = subseg; | |
63 | ||
7be1c489 | 64 | if (! seginfo) |
252b5132 | 65 | { |
add39d23 | 66 | seginfo = XCNEW (segment_info_type); |
7be1c489 | 67 | seginfo->bfd_section = seg; |
e7ff5c73 | 68 | bfd_set_section_userdata (stdoutput, seg, seginfo); |
252b5132 | 69 | } |
252b5132 RH |
70 | } |
71 | \f | |
72 | static void | |
24361518 | 73 | subseg_set_rest (segT seg, subsegT subseg) |
252b5132 | 74 | { |
c9049d30 AM |
75 | frchainS *frcP; /* crawl frchain chain */ |
76 | frchainS **lastPP; /* address of last pointer */ | |
252b5132 | 77 | frchainS *newP; /* address of new frchain */ |
c9049d30 | 78 | segment_info_type *seginfo; |
252b5132 RH |
79 | |
80 | mri_common_symbol = NULL; | |
81 | ||
82 | if (frag_now && frchain_now) | |
83 | frchain_now->frch_frag_now = frag_now; | |
84 | ||
9c2799c2 | 85 | gas_assert (frchain_now == 0 |
252b5132 RH |
86 | || frchain_now->frch_last == frag_now); |
87 | ||
88 | subseg_change (seg, (int) subseg); | |
89 | ||
c9049d30 | 90 | seginfo = seg_info (seg); |
252b5132 | 91 | |
c9049d30 AM |
92 | /* Attempt to find or make a frchain for that subsection. |
93 | We keep the list sorted by subsection number. */ | |
94 | for (frcP = *(lastPP = &seginfo->frchainP); | |
95 | frcP != NULL; | |
252b5132 | 96 | frcP = *(lastPP = &frcP->frch_next)) |
c9049d30 AM |
97 | if (frcP->frch_subseg >= subseg) |
98 | break; | |
99 | ||
100 | if (frcP == NULL || frcP->frch_subseg != subseg) | |
252b5132 | 101 | { |
c9049d30 | 102 | /* This should be the only code that creates a frchainS. */ |
7be1c489 | 103 | |
1e9cc1c2 | 104 | newP = (frchainS *) obstack_alloc (&frchains, sizeof (frchainS)); |
252b5132 | 105 | newP->frch_subseg = subseg; |
252b5132 RH |
106 | newP->fix_root = NULL; |
107 | newP->fix_tail = NULL; | |
252b5132 RH |
108 | obstack_begin (&newP->frch_obstack, chunksize); |
109 | #if __GNUC__ >= 2 | |
110 | obstack_alignment_mask (&newP->frch_obstack) = __alignof__ (fragS) - 1; | |
111 | #endif | |
112 | newP->frch_frag_now = frag_alloc (&newP->frch_obstack); | |
113 | newP->frch_frag_now->fr_type = rs_fill; | |
5fd396ba | 114 | newP->frch_cfi_data = NULL; |
252b5132 RH |
115 | |
116 | newP->frch_root = newP->frch_last = newP->frch_frag_now; | |
117 | ||
118 | *lastPP = newP; | |
c9049d30 | 119 | newP->frch_next = frcP; |
252b5132 RH |
120 | frcP = newP; |
121 | } | |
c9049d30 | 122 | |
252b5132 RH |
123 | frchain_now = frcP; |
124 | frag_now = frcP->frch_frag_now; | |
125 | ||
9c2799c2 | 126 | gas_assert (frchain_now->frch_last == frag_now); |
252b5132 RH |
127 | } |
128 | ||
129 | /* | |
130 | * subseg_set(segT, subsegT) | |
131 | * | |
132 | * If you attempt to change to the current subsegment, nothing happens. | |
133 | * | |
134 | * In: segT, subsegT code for new subsegment. | |
135 | * frag_now -> incomplete frag for current subsegment. | |
136 | * If frag_now==NULL, then there is no old, incomplete frag, so | |
137 | * the old frag is not closed off. | |
138 | * | |
139 | * Out: now_subseg, now_seg updated. | |
140 | * Frchain_now points to the (possibly new) struct frchain for this | |
141 | * sub-segment. | |
252b5132 RH |
142 | */ |
143 | ||
252b5132 | 144 | segT |
24361518 | 145 | subseg_get (const char *segname, int force_new) |
252b5132 RH |
146 | { |
147 | segT secptr; | |
148 | segment_info_type *seginfo; | |
149 | const char *now_seg_name = (now_seg | |
150 | ? bfd_get_section_name (stdoutput, now_seg) | |
151 | : 0); | |
152 | ||
153 | if (!force_new | |
154 | && now_seg_name | |
155 | && (now_seg_name == segname | |
156 | || !strcmp (now_seg_name, segname))) | |
157 | return now_seg; | |
158 | ||
159 | if (!force_new) | |
160 | secptr = bfd_make_section_old_way (stdoutput, segname); | |
161 | else | |
162 | secptr = bfd_make_section_anyway (stdoutput, segname); | |
163 | ||
164 | seginfo = seg_info (secptr); | |
165 | if (! seginfo) | |
166 | { | |
c9049d30 | 167 | secptr->output_section = secptr; |
add39d23 | 168 | seginfo = XCNEW (segment_info_type); |
252b5132 | 169 | seginfo->bfd_section = secptr; |
e7ff5c73 | 170 | bfd_set_section_userdata (stdoutput, secptr, seginfo); |
252b5132 RH |
171 | } |
172 | return secptr; | |
173 | } | |
174 | ||
175 | segT | |
24361518 | 176 | subseg_new (const char *segname, subsegT subseg) |
252b5132 RH |
177 | { |
178 | segT secptr; | |
252b5132 RH |
179 | |
180 | secptr = subseg_get (segname, 0); | |
181 | subseg_set_rest (secptr, subseg); | |
252b5132 RH |
182 | return secptr; |
183 | } | |
184 | ||
185 | /* Like subseg_new, except a new section is always created, even if | |
186 | a section with that name already exists. */ | |
187 | segT | |
24361518 | 188 | subseg_force_new (const char *segname, subsegT subseg) |
252b5132 RH |
189 | { |
190 | segT secptr; | |
252b5132 RH |
191 | |
192 | secptr = subseg_get (segname, 1); | |
193 | subseg_set_rest (secptr, subseg); | |
252b5132 RH |
194 | return secptr; |
195 | } | |
196 | ||
197 | void | |
24361518 | 198 | subseg_set (segT secptr, subsegT subseg) |
252b5132 RH |
199 | { |
200 | if (! (secptr == now_seg && subseg == now_subseg)) | |
201 | subseg_set_rest (secptr, subseg); | |
202 | mri_common_symbol = NULL; | |
203 | } | |
204 | ||
205 | #ifndef obj_sec_sym_ok_for_reloc | |
206 | #define obj_sec_sym_ok_for_reloc(SEC) 0 | |
207 | #endif | |
208 | ||
252b5132 | 209 | symbolS * |
24361518 | 210 | section_symbol (segT sec) |
252b5132 RH |
211 | { |
212 | segment_info_type *seginfo = seg_info (sec); | |
213 | symbolS *s; | |
214 | ||
215 | if (seginfo == 0) | |
216 | abort (); | |
217 | if (seginfo->sym) | |
218 | return seginfo->sym; | |
219 | ||
220 | #ifndef EMIT_SECTION_SYMBOLS | |
221 | #define EMIT_SECTION_SYMBOLS 1 | |
222 | #endif | |
223 | ||
a161fe53 | 224 | if (! EMIT_SECTION_SYMBOLS || symbol_table_frozen) |
252b5132 RH |
225 | { |
226 | /* Here we know it won't be going into the symbol table. */ | |
546c73f9 | 227 | s = symbol_create (sec->symbol->name, sec, 0, &zero_address_frag); |
252b5132 RH |
228 | } |
229 | else | |
230 | { | |
90c1602c | 231 | segT seg; |
91c4c449 | 232 | s = symbol_find (sec->symbol->name); |
22fe14ad NC |
233 | /* We have to make sure it is the right symbol when we |
234 | have multiple sections with the same section name. */ | |
90c1602c L |
235 | if (s == NULL |
236 | || ((seg = S_GET_SEGMENT (s)) != sec | |
237 | && seg != undefined_section)) | |
546c73f9 | 238 | s = symbol_new (sec->symbol->name, sec, 0, &zero_address_frag); |
90c1602c | 239 | else if (seg == undefined_section) |
252b5132 | 240 | { |
90c1602c L |
241 | S_SET_SEGMENT (s, sec); |
242 | symbol_set_frag (s, &zero_address_frag); | |
252b5132 RH |
243 | } |
244 | } | |
245 | ||
246 | S_CLEAR_EXTERNAL (s); | |
247 | ||
248 | /* Use the BFD section symbol, if possible. */ | |
249 | if (obj_sec_sym_ok_for_reloc (sec)) | |
49309057 | 250 | symbol_set_bfdsym (s, sec->symbol); |
a161fe53 AM |
251 | else |
252 | symbol_get_bfdsym (s)->flags |= BSF_SECTION_SYM; | |
252b5132 RH |
253 | |
254 | seginfo->sym = s; | |
255 | return s; | |
256 | } | |
257 | ||
b9e57a38 ILT |
258 | /* Return whether the specified segment is thought to hold text. */ |
259 | ||
260 | int | |
24361518 | 261 | subseg_text_p (segT sec) |
b9e57a38 | 262 | { |
b9e57a38 | 263 | return (bfd_get_section_flags (stdoutput, sec) & SEC_CODE) != 0; |
b9e57a38 ILT |
264 | } |
265 | ||
c6cb92c5 NS |
266 | /* Return non zero if SEC has at least one byte of data. It is |
267 | possible that we'll return zero even on a non-empty section because | |
268 | we don't know all the fragment types, and it is possible that an | |
269 | fr_fix == 0 one still contributes data. Think of this as | |
270 | seg_definitely_not_empty_p. */ | |
271 | ||
4ee4d249 | 272 | int |
f17c130b | 273 | seg_not_empty_p (segT sec ATTRIBUTE_UNUSED) |
c6cb92c5 NS |
274 | { |
275 | segment_info_type *seginfo = seg_info (sec); | |
276 | frchainS *chain; | |
277 | fragS *frag; | |
278 | ||
279 | if (!seginfo) | |
280 | return 0; | |
34bca508 | 281 | |
c6cb92c5 NS |
282 | for (chain = seginfo->frchainP; chain; chain = chain->frch_next) |
283 | { | |
284 | for (frag = chain->frch_root; frag; frag = frag->fr_next) | |
285 | if (frag->fr_fix) | |
286 | return 1; | |
287 | if (obstack_next_free (&chain->frch_obstack) | |
288 | != chain->frch_last->fr_literal) | |
289 | return 1; | |
290 | } | |
291 | return 0; | |
292 | } | |
293 | ||
252b5132 | 294 | void |
24361518 | 295 | subsegs_print_statistics (FILE *file) |
252b5132 RH |
296 | { |
297 | frchainS *frchp; | |
c9049d30 AM |
298 | asection *s; |
299 | ||
252b5132 | 300 | fprintf (file, "frag chains:\n"); |
c9049d30 | 301 | for (s = stdoutput->sections; s; s = s->next) |
252b5132 | 302 | { |
c9049d30 | 303 | segment_info_type *seginfo; |
252b5132 | 304 | |
c9049d30 AM |
305 | /* Skip gas-internal sections. */ |
306 | if (segment_name (s)[0] == '*') | |
252b5132 RH |
307 | continue; |
308 | ||
c9049d30 AM |
309 | seginfo = seg_info (s); |
310 | if (!seginfo) | |
252b5132 RH |
311 | continue; |
312 | ||
c9049d30 | 313 | for (frchp = seginfo->frchainP; frchp; frchp = frchp->frch_next) |
252b5132 | 314 | { |
c9049d30 AM |
315 | int count = 0; |
316 | fragS *fragp; | |
317 | ||
318 | for (fragp = frchp->frch_root; fragp; fragp = fragp->fr_next) | |
319 | count++; | |
320 | ||
321 | fprintf (file, "\n"); | |
322 | fprintf (file, "\t%p %-10s\t%10d frags\n", (void *) frchp, | |
323 | segment_name (s), count); | |
252b5132 | 324 | } |
252b5132 RH |
325 | } |
326 | } | |
327 | ||
328 | /* end of subsegs.c */ |