Commit | Line | Data |
---|---|---|
252b5132 | 1 | /* subsegs.c - subsegments - |
250d07de | 2 | Copyright (C) 1987-2021 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; |
fd361982 | 68 | bfd_set_section_userdata (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 | |
d1bcae83 L |
92 | /* Should the section symbol be kept? */ |
93 | if (bfd_keep_unused_section_symbols (stdoutput)) | |
94 | seg->symbol->flags |= BSF_SECTION_SYM_USED; | |
95 | ||
c9049d30 AM |
96 | /* Attempt to find or make a frchain for that subsection. |
97 | We keep the list sorted by subsection number. */ | |
98 | for (frcP = *(lastPP = &seginfo->frchainP); | |
99 | frcP != NULL; | |
252b5132 | 100 | frcP = *(lastPP = &frcP->frch_next)) |
c9049d30 AM |
101 | if (frcP->frch_subseg >= subseg) |
102 | break; | |
103 | ||
104 | if (frcP == NULL || frcP->frch_subseg != subseg) | |
252b5132 | 105 | { |
c9049d30 | 106 | /* This should be the only code that creates a frchainS. */ |
7be1c489 | 107 | |
1e9cc1c2 | 108 | newP = (frchainS *) obstack_alloc (&frchains, sizeof (frchainS)); |
252b5132 | 109 | newP->frch_subseg = subseg; |
252b5132 RH |
110 | newP->fix_root = NULL; |
111 | newP->fix_tail = NULL; | |
252b5132 RH |
112 | obstack_begin (&newP->frch_obstack, chunksize); |
113 | #if __GNUC__ >= 2 | |
114 | obstack_alignment_mask (&newP->frch_obstack) = __alignof__ (fragS) - 1; | |
115 | #endif | |
116 | newP->frch_frag_now = frag_alloc (&newP->frch_obstack); | |
117 | newP->frch_frag_now->fr_type = rs_fill; | |
5fd396ba | 118 | newP->frch_cfi_data = NULL; |
252b5132 RH |
119 | |
120 | newP->frch_root = newP->frch_last = newP->frch_frag_now; | |
121 | ||
122 | *lastPP = newP; | |
c9049d30 | 123 | newP->frch_next = frcP; |
252b5132 RH |
124 | frcP = newP; |
125 | } | |
c9049d30 | 126 | |
252b5132 RH |
127 | frchain_now = frcP; |
128 | frag_now = frcP->frch_frag_now; | |
129 | ||
9c2799c2 | 130 | gas_assert (frchain_now->frch_last == frag_now); |
252b5132 RH |
131 | } |
132 | ||
133 | /* | |
134 | * subseg_set(segT, subsegT) | |
135 | * | |
136 | * If you attempt to change to the current subsegment, nothing happens. | |
137 | * | |
138 | * In: segT, subsegT code for new subsegment. | |
139 | * frag_now -> incomplete frag for current subsegment. | |
140 | * If frag_now==NULL, then there is no old, incomplete frag, so | |
141 | * the old frag is not closed off. | |
142 | * | |
143 | * Out: now_subseg, now_seg updated. | |
144 | * Frchain_now points to the (possibly new) struct frchain for this | |
145 | * sub-segment. | |
252b5132 RH |
146 | */ |
147 | ||
252b5132 | 148 | segT |
24361518 | 149 | subseg_get (const char *segname, int force_new) |
252b5132 RH |
150 | { |
151 | segT secptr; | |
152 | segment_info_type *seginfo; | |
fd361982 | 153 | const char *now_seg_name = now_seg ? bfd_section_name (now_seg) : 0; |
252b5132 RH |
154 | |
155 | if (!force_new | |
156 | && now_seg_name | |
157 | && (now_seg_name == segname | |
158 | || !strcmp (now_seg_name, segname))) | |
159 | return now_seg; | |
160 | ||
161 | if (!force_new) | |
162 | secptr = bfd_make_section_old_way (stdoutput, segname); | |
163 | else | |
164 | secptr = bfd_make_section_anyway (stdoutput, segname); | |
165 | ||
166 | seginfo = seg_info (secptr); | |
167 | if (! seginfo) | |
168 | { | |
c9049d30 | 169 | secptr->output_section = secptr; |
add39d23 | 170 | seginfo = XCNEW (segment_info_type); |
252b5132 | 171 | seginfo->bfd_section = secptr; |
fd361982 | 172 | bfd_set_section_userdata (secptr, seginfo); |
252b5132 RH |
173 | } |
174 | return secptr; | |
175 | } | |
176 | ||
177 | segT | |
24361518 | 178 | subseg_new (const char *segname, subsegT subseg) |
252b5132 RH |
179 | { |
180 | segT secptr; | |
252b5132 RH |
181 | |
182 | secptr = subseg_get (segname, 0); | |
183 | subseg_set_rest (secptr, subseg); | |
252b5132 RH |
184 | return secptr; |
185 | } | |
186 | ||
187 | /* Like subseg_new, except a new section is always created, even if | |
188 | a section with that name already exists. */ | |
189 | segT | |
24361518 | 190 | subseg_force_new (const char *segname, subsegT subseg) |
252b5132 RH |
191 | { |
192 | segT secptr; | |
252b5132 RH |
193 | |
194 | secptr = subseg_get (segname, 1); | |
195 | subseg_set_rest (secptr, subseg); | |
252b5132 RH |
196 | return secptr; |
197 | } | |
198 | ||
199 | void | |
24361518 | 200 | subseg_set (segT secptr, subsegT subseg) |
252b5132 RH |
201 | { |
202 | if (! (secptr == now_seg && subseg == now_subseg)) | |
203 | subseg_set_rest (secptr, subseg); | |
204 | mri_common_symbol = NULL; | |
205 | } | |
206 | ||
207 | #ifndef obj_sec_sym_ok_for_reloc | |
208 | #define obj_sec_sym_ok_for_reloc(SEC) 0 | |
209 | #endif | |
210 | ||
252b5132 | 211 | symbolS * |
24361518 | 212 | section_symbol (segT sec) |
252b5132 RH |
213 | { |
214 | segment_info_type *seginfo = seg_info (sec); | |
215 | symbolS *s; | |
216 | ||
217 | if (seginfo == 0) | |
218 | abort (); | |
219 | if (seginfo->sym) | |
220 | return seginfo->sym; | |
221 | ||
222 | #ifndef EMIT_SECTION_SYMBOLS | |
223 | #define EMIT_SECTION_SYMBOLS 1 | |
224 | #endif | |
225 | ||
a161fe53 | 226 | if (! EMIT_SECTION_SYMBOLS || symbol_table_frozen) |
252b5132 RH |
227 | { |
228 | /* Here we know it won't be going into the symbol table. */ | |
e01e1cee | 229 | s = symbol_create (sec->symbol->name, sec, &zero_address_frag, 0); |
252b5132 RH |
230 | } |
231 | else | |
232 | { | |
90c1602c | 233 | segT seg; |
91c4c449 | 234 | s = symbol_find (sec->symbol->name); |
22fe14ad NC |
235 | /* We have to make sure it is the right symbol when we |
236 | have multiple sections with the same section name. */ | |
90c1602c L |
237 | if (s == NULL |
238 | || ((seg = S_GET_SEGMENT (s)) != sec | |
239 | && seg != undefined_section)) | |
e01e1cee | 240 | s = symbol_new (sec->symbol->name, sec, &zero_address_frag, 0); |
90c1602c | 241 | else if (seg == undefined_section) |
252b5132 | 242 | { |
90c1602c L |
243 | S_SET_SEGMENT (s, sec); |
244 | symbol_set_frag (s, &zero_address_frag); | |
252b5132 RH |
245 | } |
246 | } | |
247 | ||
248 | S_CLEAR_EXTERNAL (s); | |
249 | ||
250 | /* Use the BFD section symbol, if possible. */ | |
251 | if (obj_sec_sym_ok_for_reloc (sec)) | |
49309057 | 252 | symbol_set_bfdsym (s, sec->symbol); |
a161fe53 AM |
253 | else |
254 | symbol_get_bfdsym (s)->flags |= BSF_SECTION_SYM; | |
252b5132 RH |
255 | |
256 | seginfo->sym = s; | |
257 | return s; | |
258 | } | |
259 | ||
b9e57a38 ILT |
260 | /* Return whether the specified segment is thought to hold text. */ |
261 | ||
262 | int | |
24361518 | 263 | subseg_text_p (segT sec) |
b9e57a38 | 264 | { |
fd361982 | 265 | return (bfd_section_flags (sec) & SEC_CODE) != 0; |
b9e57a38 ILT |
266 | } |
267 | ||
c6cb92c5 NS |
268 | /* Return non zero if SEC has at least one byte of data. It is |
269 | possible that we'll return zero even on a non-empty section because | |
270 | we don't know all the fragment types, and it is possible that an | |
271 | fr_fix == 0 one still contributes data. Think of this as | |
272 | seg_definitely_not_empty_p. */ | |
273 | ||
4ee4d249 | 274 | int |
f17c130b | 275 | seg_not_empty_p (segT sec ATTRIBUTE_UNUSED) |
c6cb92c5 NS |
276 | { |
277 | segment_info_type *seginfo = seg_info (sec); | |
278 | frchainS *chain; | |
279 | fragS *frag; | |
280 | ||
281 | if (!seginfo) | |
282 | return 0; | |
34bca508 | 283 | |
c6cb92c5 NS |
284 | for (chain = seginfo->frchainP; chain; chain = chain->frch_next) |
285 | { | |
286 | for (frag = chain->frch_root; frag; frag = frag->fr_next) | |
287 | if (frag->fr_fix) | |
288 | return 1; | |
289 | if (obstack_next_free (&chain->frch_obstack) | |
290 | != chain->frch_last->fr_literal) | |
291 | return 1; | |
292 | } | |
293 | return 0; | |
294 | } | |
295 | ||
252b5132 | 296 | void |
24361518 | 297 | subsegs_print_statistics (FILE *file) |
252b5132 RH |
298 | { |
299 | frchainS *frchp; | |
c9049d30 AM |
300 | asection *s; |
301 | ||
4cbd8408 NC |
302 | /* PR 20897 - check to see if the output bfd was actually created. */ |
303 | if (stdoutput == NULL) | |
304 | return; | |
305 | ||
252b5132 | 306 | fprintf (file, "frag chains:\n"); |
c9049d30 | 307 | for (s = stdoutput->sections; s; s = s->next) |
252b5132 | 308 | { |
c9049d30 | 309 | segment_info_type *seginfo; |
252b5132 | 310 | |
c9049d30 AM |
311 | /* Skip gas-internal sections. */ |
312 | if (segment_name (s)[0] == '*') | |
252b5132 RH |
313 | continue; |
314 | ||
c9049d30 AM |
315 | seginfo = seg_info (s); |
316 | if (!seginfo) | |
252b5132 RH |
317 | continue; |
318 | ||
c9049d30 | 319 | for (frchp = seginfo->frchainP; frchp; frchp = frchp->frch_next) |
252b5132 | 320 | { |
c9049d30 AM |
321 | int count = 0; |
322 | fragS *fragp; | |
323 | ||
324 | for (fragp = frchp->frch_root; fragp; fragp = fragp->fr_next) | |
325 | count++; | |
326 | ||
327 | fprintf (file, "\n"); | |
328 | fprintf (file, "\t%p %-10s\t%10d frags\n", (void *) frchp, | |
329 | segment_name (s), count); | |
252b5132 | 330 | } |
252b5132 RH |
331 | } |
332 | } | |
333 | ||
334 | /* end of subsegs.c */ |