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