Update year range in copyright notice of binutils files
[binutils-gdb.git] / gas / subsegs.c
blob42f42c5ffacbe4e660e3de74b8c6f18ed9fffb87
1 /* subsegs.c - subsegments -
2 Copyright (C) 1987-2024 Free Software Foundation, Inc.
4 This file is part of GAS, the GNU Assembler.
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.
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.
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. */
21 /* Segments & sub-segments. */
23 #include "as.h"
25 #include "subsegs.h"
26 #include "obstack.h"
28 frchainS *frchain_now;
30 static struct obstack frchains;
32 static fragS dummy_frag;
35 void
36 subsegs_begin (void)
38 obstack_begin (&frchains, chunksize);
39 #if __GNUC__ >= 2
40 obstack_alignment_mask (&frchains) = __alignof__ (frchainS) - 1;
41 #endif
43 frchain_now = NULL; /* Warn new_subseg() that we are booting. */
44 frag_now = &dummy_frag;
47 void
48 subsegs_end (struct obstack **obs)
50 for (; *obs; obs++)
51 _obstack_free (*obs, NULL);
52 _obstack_free (&frchains, NULL);
53 bfd_set_section_userdata (bfd_abs_section_ptr, NULL);
54 bfd_set_section_userdata (bfd_und_section_ptr, NULL);
57 static void
58 alloc_seginfo (segT seg)
60 segment_info_type *seginfo;
62 seginfo = obstack_alloc (&notes, sizeof (*seginfo));
63 memset (seginfo, 0, sizeof (*seginfo));
64 bfd_set_section_userdata (seg, seginfo);
67 * subseg_change()
69 * Change the subsegment we are in, BUT DO NOT MAKE A NEW FRAG for the
70 * subsegment. If we are already in the correct subsegment, change nothing.
71 * This is used eg as a worker for subseg_set [which does make a new frag_now]
72 * and for changing segments after we have read the source. We construct eg
73 * fixSs even after the source file is read, so we do have to keep the
74 * segment context correct.
76 void
77 subseg_change (segT seg, int subseg)
79 now_seg = seg;
80 now_subseg = subseg;
82 if (!seg_info (seg))
83 alloc_seginfo (seg);
86 static void
87 subseg_set_rest (segT seg, subsegT subseg)
89 frchainS *frcP; /* crawl frchain chain */
90 frchainS **lastPP; /* address of last pointer */
91 frchainS *newP; /* address of new frchain */
92 segment_info_type *seginfo;
94 mri_common_symbol = NULL;
96 if (frag_now && frchain_now)
97 frchain_now->frch_frag_now = frag_now;
99 gas_assert (frchain_now == 0
100 || frchain_now->frch_last == frag_now);
102 subseg_change (seg, (int) subseg);
104 seginfo = seg_info (seg);
106 /* Should the section symbol be kept? */
107 if (bfd_keep_unused_section_symbols (stdoutput))
108 seg->symbol->flags |= BSF_SECTION_SYM_USED;
110 /* Attempt to find or make a frchain for that subsection.
111 We keep the list sorted by subsection number. */
112 for (frcP = *(lastPP = &seginfo->frchainP);
113 frcP != NULL;
114 frcP = *(lastPP = &frcP->frch_next))
115 if (frcP->frch_subseg >= subseg)
116 break;
118 if (frcP == NULL || frcP->frch_subseg != subseg)
120 /* This should be the only code that creates a frchainS. */
122 newP = (frchainS *) obstack_alloc (&frchains, sizeof (frchainS));
123 newP->frch_subseg = subseg;
124 newP->fix_root = NULL;
125 newP->fix_tail = NULL;
126 obstack_begin (&newP->frch_obstack, chunksize);
127 #if __GNUC__ >= 2
128 obstack_alignment_mask (&newP->frch_obstack) = __alignof__ (fragS) - 1;
129 #endif
130 newP->frch_frag_now = frag_alloc (&newP->frch_obstack);
131 newP->frch_frag_now->fr_type = rs_fill;
132 newP->frch_cfi_data = NULL;
134 newP->frch_root = newP->frch_last = newP->frch_frag_now;
136 *lastPP = newP;
137 newP->frch_next = frcP;
138 frcP = newP;
141 frchain_now = frcP;
142 frag_now = frcP->frch_frag_now;
144 gas_assert (frchain_now->frch_last == frag_now);
148 * subseg_set(segT, subsegT)
150 * If you attempt to change to the current subsegment, nothing happens.
152 * In: segT, subsegT code for new subsegment.
153 * frag_now -> incomplete frag for current subsegment.
154 * If frag_now==NULL, then there is no old, incomplete frag, so
155 * the old frag is not closed off.
157 * Out: now_subseg, now_seg updated.
158 * Frchain_now points to the (possibly new) struct frchain for this
159 * sub-segment.
162 segT
163 subseg_get (const char *segname, int force_new)
165 segT secptr;
166 const char *now_seg_name = now_seg ? bfd_section_name (now_seg) : 0;
168 if (!force_new
169 && now_seg_name
170 && (now_seg_name == segname
171 || !strcmp (now_seg_name, segname)))
172 return now_seg;
174 if (!force_new)
175 secptr = bfd_make_section_old_way (stdoutput, segname);
176 else
177 secptr = bfd_make_section_anyway (stdoutput, segname);
179 if (!seg_info (secptr))
181 secptr->output_section = secptr;
182 alloc_seginfo (secptr);
184 return secptr;
187 segT
188 subseg_new (const char *segname, subsegT subseg)
190 segT secptr;
192 secptr = subseg_get (segname, 0);
193 subseg_set_rest (secptr, subseg);
194 return secptr;
197 /* Like subseg_new, except a new section is always created, even if
198 a section with that name already exists. */
199 segT
200 subseg_force_new (const char *segname, subsegT subseg)
202 segT secptr;
204 secptr = subseg_get (segname, 1);
205 subseg_set_rest (secptr, subseg);
206 return secptr;
209 void
210 subseg_set (segT secptr, subsegT subseg)
212 if (! (secptr == now_seg && subseg == now_subseg))
213 subseg_set_rest (secptr, subseg);
214 mri_common_symbol = NULL;
217 #ifndef obj_sec_sym_ok_for_reloc
218 #define obj_sec_sym_ok_for_reloc(SEC) 0
219 #endif
221 symbolS *
222 section_symbol (segT sec)
224 segment_info_type *seginfo = seg_info (sec);
225 symbolS *s;
227 if (seginfo == 0)
228 abort ();
229 if (seginfo->sym)
230 return seginfo->sym;
232 #ifndef EMIT_SECTION_SYMBOLS
233 #define EMIT_SECTION_SYMBOLS 1
234 #endif
236 if (! EMIT_SECTION_SYMBOLS || symbol_table_frozen)
238 /* Here we know it won't be going into the symbol table. */
239 s = symbol_create (sec->symbol->name, sec, &zero_address_frag, 0);
241 else
243 segT seg;
244 s = symbol_find (sec->symbol->name);
245 /* We have to make sure it is the right symbol when we
246 have multiple sections with the same section name. */
247 if (s == NULL
248 || ((seg = S_GET_SEGMENT (s)) != sec
249 && seg != undefined_section))
250 s = symbol_new (sec->symbol->name, sec, &zero_address_frag, 0);
251 else if (seg == undefined_section)
253 S_SET_SEGMENT (s, sec);
254 symbol_set_frag (s, &zero_address_frag);
258 S_CLEAR_EXTERNAL (s);
260 /* Use the BFD section symbol, if possible. */
261 if (obj_sec_sym_ok_for_reloc (sec))
262 symbol_set_bfdsym (s, sec->symbol);
263 else
264 symbol_get_bfdsym (s)->flags |= BSF_SECTION_SYM;
266 seginfo->sym = s;
267 return s;
270 /* Return whether the specified segment is thought to hold text. */
273 subseg_text_p (segT sec)
275 return (bfd_section_flags (sec) & SEC_CODE) != 0;
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. */
285 seg_not_empty_p (segT sec ATTRIBUTE_UNUSED)
287 segment_info_type *seginfo = seg_info (sec);
288 frchainS *chain;
289 fragS *frag;
291 if (!seginfo)
292 return 0;
294 for (chain = seginfo->frchainP; chain; chain = chain->frch_next)
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;
303 return 0;
306 void
307 subsegs_print_statistics (FILE *file)
309 frchainS *frchp;
310 asection *s;
312 /* PR 20897 - check to see if the output bfd was actually created. */
313 if (stdoutput == NULL)
314 return;
316 fprintf (file, "frag chains:\n");
317 for (s = stdoutput->sections; s; s = s->next)
319 segment_info_type *seginfo;
321 /* Skip gas-internal sections. */
322 if (segment_name (s)[0] == '*')
323 continue;
325 seginfo = seg_info (s);
326 if (!seginfo)
327 continue;
329 for (frchp = seginfo->frchainP; frchp; frchp = frchp->frch_next)
331 int count = 0;
332 fragS *fragp;
334 for (fragp = frchp->frch_root; fragp; fragp = fragp->fr_next)
335 count++;
337 fprintf (file, "\n");
338 fprintf (file, "\t%p %-10s\t%10d frags\n", (void *) frchp,
339 segment_name (s), count);
344 /* end of subsegs.c */