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