1 /* read.c - read a source file -
2 Copyright (C) 1986-2024 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 /* If your chars aren't 8 bits, you will change this a bit (eg. to 0xFF).
22 But then, GNU isn't supposed to run on your machine anyway.
23 (RMS is so shortsighted sometimes.) */
24 #define MASK_CHAR ((int)(unsigned char) -1)
25
26 /* This is the largest known floating point format (for now). It will
27 grow when we do 4361 style flonums. */
28 #define MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT (16)
29
30 /* Routines that read assembler source text to build spaghetti in memory.
31 Another group of these functions is in the expr.c module. */
32
33 #include "as.h"
34 #include "safe-ctype.h"
35 #include "subsegs.h"
36 #include "sb.h"
37 #include "macro.h"
38 #include "obstack.h"
39 #include "ecoff.h"
40 #include "dw2gencfi.h"
41 #include "scfidw2gen.h"
42 #include "codeview.h"
43 #include "wchar.h"
44 #include "filenames.h"
45 #include "ginsn.h"
46
47 #include <limits.h>
48
49 #ifndef TC_START_LABEL
50 #define TC_START_LABEL(STR, NUL_CHAR, NEXT_CHAR) (NEXT_CHAR == ':')
51 #endif
52
53 /* Set by the object-format or the target. */
54 #ifndef TC_IMPLICIT_LCOMM_ALIGNMENT
55 #define TC_IMPLICIT_LCOMM_ALIGNMENT(SIZE, P2VAR) \
56 do \
57 { \
58 if ((SIZE) >= 8) \
59 (P2VAR) = 3; \
60 else if ((SIZE) >= 4) \
61 (P2VAR) = 2; \
62 else if ((SIZE) >= 2) \
63 (P2VAR) = 1; \
64 else \
65 (P2VAR) = 0; \
66 } \
67 while (0)
68 #endif
69
70 char *input_line_pointer; /*->next char of source file to parse. */
71 bool input_from_string = false;
72
73 #if BITS_PER_CHAR != 8
74 /* The following table is indexed by[(char)] and will break if
75 a char does not have exactly 256 states (hopefully 0:255!)! */
76 die horribly;
77 #endif
78
79 #ifndef LEX_AT
80 #define LEX_AT 0
81 #endif
82
83 #ifndef LEX_BR
84 /* The RS/6000 assembler uses {,},[,] as parts of symbol names. */
85 #define LEX_BR 0
86 #endif
87
88 #ifndef LEX_PCT
89 /* The Delta 68k assembler permits % inside label names. */
90 #define LEX_PCT 0
91 #endif
92
93 #ifndef LEX_QM
94 /* The PowerPC Windows NT assemblers permits ? inside label names. */
95 #define LEX_QM 0
96 #endif
97
98 #ifndef LEX_HASH
99 /* The IA-64 assembler uses # as a suffix designating a symbol. We include
100 it in the symbol and strip it out in tc_canonicalize_symbol_name. */
101 #define LEX_HASH 0
102 #endif
103
104 #ifndef LEX_DOLLAR
105 #define LEX_DOLLAR 3
106 #endif
107
108 #ifndef LEX_TILDE
109 /* The Delta 68k assembler permits ~ at start of label names. */
110 #define LEX_TILDE 0
111 #endif
112
113 /* Used by is_... macros. our ctype[]. */
114 char lex_type[256] = {
115 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* @ABCDEFGHIJKLMNO */
116 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* PQRSTUVWXYZ[\]^_ */
117 0, 0, 0, LEX_HASH, LEX_DOLLAR, LEX_PCT, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, /* _!"#$%&'()*+,-./ */
118 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, LEX_QM, /* 0123456789:;<=>? */
119 LEX_AT, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, /* @ABCDEFGHIJKLMNO */
120 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, LEX_BR, 0, LEX_BR, 0, 3, /* PQRSTUVWXYZ[\]^_ */
121 0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, /* `abcdefghijklmno */
122 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, LEX_BR, 0, LEX_BR, LEX_TILDE, 0, /* pqrstuvwxyz{|}~. */
123 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
124 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
125 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
126 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
127 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
128 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
129 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
130 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3
131 };
132
133 /* In: a character.
134 Out: 1 if this character ends a line.
135 2 if this character is a line separator. */
136 char is_end_of_line[256] = {
137 #ifdef CR_EOL
138 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, /* @abcdefghijklmno */
139 #else
140 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, /* @abcdefghijklmno */
141 #endif
142 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* */
143 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* _!"#$%&'()*+,-./ */
144 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0123456789:;<=>? */
145 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* */
146 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* */
147 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* */
148 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* */
149 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* */
150 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* */
151 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* */
152 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* */
153 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* */
154 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* */
155 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* */
156 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 /* */
157 };
158
159 #ifndef TC_CASE_SENSITIVE
160 char original_case_string[128];
161 #endif
162
163 /* Functions private to this file. */
164
165 static char *buffer; /* 1st char of each buffer of lines is here. */
166 static char *buffer_limit; /*->1 + last char in buffer. */
167
168 /* TARGET_BYTES_BIG_ENDIAN is required to be defined to either 0 or 1
169 in the tc-<CPU>.h file. See the "Porting GAS" section of the
170 internals manual. */
171 int target_big_endian = TARGET_BYTES_BIG_ENDIAN;
172
173 /* Variables for handling include file directory table. */
174
175 /* Table of pointers to directories to search for .include's. */
176 const char **include_dirs;
177
178 /* How many are in the table. */
179 size_t include_dir_count;
180
181 /* Length of longest in table. */
182 size_t include_dir_maxlen;
183
184 #ifndef WORKING_DOT_WORD
185 struct broken_word *broken_words;
186 int new_broken_words;
187 #endif
188
189 /* The current offset into the absolute section. We don't try to
190 build frags in the absolute section, since no data can be stored
191 there. We just keep track of the current offset. */
192 addressT abs_section_offset;
193
194 /* If this line had an MRI style label, it is stored in this variable.
195 This is used by some of the MRI pseudo-ops. */
196 symbolS *line_label;
197
198 /* This global variable is used to support MRI common sections. We
199 translate such sections into a common symbol. This variable is
200 non-NULL when we are in an MRI common section. */
201 symbolS *mri_common_symbol;
202
203 /* In MRI mode, after a dc.b pseudo-op with an odd number of bytes, we
204 need to align to an even byte boundary unless the next pseudo-op is
205 dc.b, ds.b, or dcb.b. This variable is set to 1 if an alignment
206 may be needed. */
207 static int mri_pending_align;
208
209 /* Record the current function so that we can issue an error message for
210 misplaced .func,.endfunc, and also so that .endfunc needs no
211 arguments. */
212 static char *current_name;
213 static char *current_label;
214
215 #ifndef NO_LISTING
216 #ifdef OBJ_ELF
217 static int dwarf_file;
218 static int dwarf_line;
219
220 /* This variable is set to be non-zero if the next string we see might
221 be the name of the source file in DWARF debugging information. See
222 the comment in emit_expr for the format we look for. */
223 static int dwarf_file_string;
224 #endif
225 #endif
226
227 /* If the target defines the md_frag_max_var hook then we know
228 enough to implement the .bundle_align_mode features. */
229 #ifdef md_frag_max_var
230 # define HANDLE_BUNDLE
231 #endif
232
233 #ifdef HANDLE_BUNDLE
234 /* .bundle_align_mode sets this. Normally it's zero. When nonzero,
235 it's the exponent of the bundle size, and aligned instruction bundle
236 mode is in effect. */
237 static unsigned int bundle_align_p2;
238
239 /* These are set by .bundle_lock and .bundle_unlock. .bundle_lock sets
240 bundle_lock_frag to frag_now and then starts a new frag with
241 frag_align_code. At the same time, bundle_lock_frain gets frchain_now,
242 so that .bundle_unlock can verify that we didn't change segments.
243 .bundle_unlock resets both to NULL. If we detect a bundling violation,
244 then we reset bundle_lock_frchain to NULL as an indicator that we've
245 already diagnosed the error with as_bad and don't need a cascade of
246 redundant errors, but bundle_lock_frag remains set to indicate that
247 we are expecting to see .bundle_unlock. */
248 static fragS *bundle_lock_frag;
249 static frchainS *bundle_lock_frchain;
250
251 /* This is incremented by .bundle_lock and decremented by .bundle_unlock,
252 to allow nesting. */
253 static unsigned int bundle_lock_depth;
254 #endif
255
256 static void do_s_func (int end_p, const char *default_prefix);
257 static void s_align (int, int);
258 static void s_altmacro (int);
259 static void s_bad_end (int);
260 static void s_reloc (int);
261 static int hex_float (int, char *);
262 static segT get_known_segmented_expression (expressionS * expP);
263 static void pobegin (void);
264 static void poend (void);
265 static size_t get_non_macro_line_sb (sb *);
266 static void generate_file_debug (void);
267 static char *_find_end_of_line (char *, int, int, int);
268
269 void
read_begin(void)270 read_begin (void)
271 {
272 const char *p;
273
274 pobegin ();
275 obj_read_begin_hook ();
276
277 obstack_begin (&cond_obstack, chunksize);
278
279 #ifndef tc_line_separator_chars
280 #define tc_line_separator_chars line_separator_chars
281 #endif
282 /* Use machine dependent syntax. */
283 for (p = tc_line_separator_chars; *p; p++)
284 is_end_of_line[(unsigned char) *p] = 2;
285 /* Use more. FIXME-SOMEDAY. */
286
287 if (flag_mri)
288 lex_type['?'] = 3;
289 stabs_begin ();
290
291 #ifndef WORKING_DOT_WORD
292 broken_words = NULL;
293 new_broken_words = 0;
294 #endif
295
296 abs_section_offset = 0;
297
298 line_label = NULL;
299 mri_common_symbol = NULL;
300 mri_pending_align = 0;
301
302 current_name = NULL;
303 current_label = NULL;
304
305 #ifndef NO_LISTING
306 #ifdef OBJ_ELF
307 dwarf_file = 0;
308 dwarf_line = -1;
309 dwarf_file_string = 0;
310 #endif
311 #endif
312
313 #ifdef HANDLE_BUNDLE
314 bundle_align_p2 = 0;
315 bundle_lock_frag = NULL;
316 bundle_lock_frchain = NULL;
317 bundle_lock_depth = 0;
318 #endif
319 }
320
321 void
read_end(void)322 read_end (void)
323 {
324 stabs_end ();
325 poend ();
326 _obstack_free (&cond_obstack, NULL);
327 free (current_name);
328 free (current_label);
329 }
330
331 #ifndef TC_ADDRESS_BYTES
332 #define TC_ADDRESS_BYTES address_bytes
333
334 static inline int
address_bytes(void)335 address_bytes (void)
336 {
337 /* Choose smallest of 1, 2, 4, 8 bytes that is large enough to
338 contain an address. */
339 int n = (stdoutput->arch_info->bits_per_address - 1) / 8;
340 n |= n >> 1;
341 n |= n >> 2;
342 n += 1;
343 return n;
344 }
345 #endif
346
347 /* Set up pseudo-op tables. */
348
349 static htab_t po_hash;
350
351 static const pseudo_typeS potable[] = {
352 {"abort", s_abort, 0},
353 {"align", s_align_ptwo, 0},
354 {"altmacro", s_altmacro, 1},
355 {"ascii", stringer, 8+0},
356 {"asciz", stringer, 8+1},
357 {"balign", s_align_bytes, 0},
358 {"balignw", s_align_bytes, -2},
359 {"balignl", s_align_bytes, -4},
360 /* block */
361 #ifdef HANDLE_BUNDLE
362 {"bundle_align_mode", s_bundle_align_mode, 0},
363 {"bundle_lock", s_bundle_lock, 0},
364 {"bundle_unlock", s_bundle_unlock, 0},
365 #endif
366 {"byte", cons, 1},
367 {"comm", s_comm, 0},
368 {"common", s_mri_common, 0},
369 {"common.s", s_mri_common, 1},
370 {"data", s_data, 0},
371 {"dc", cons, 2},
372 {"dc.a", cons, 0},
373 {"dc.b", cons, 1},
374 {"dc.d", float_cons, 'd'},
375 {"dc.l", cons, 4},
376 {"dc.s", float_cons, 'f'},
377 {"dc.w", cons, 2},
378 {"dc.x", float_cons, 'x'},
379 {"dcb", s_space, 2},
380 {"dcb.b", s_space, 1},
381 {"dcb.d", s_float_space, 'd'},
382 {"dcb.l", s_space, 4},
383 {"dcb.s", s_float_space, 'f'},
384 {"dcb.w", s_space, 2},
385 {"dcb.x", s_float_space, 'x'},
386 {"ds", s_space, 2},
387 {"ds.b", s_space, 1},
388 {"ds.d", s_space, 8},
389 {"ds.l", s_space, 4},
390 {"ds.p", s_space, 'p'},
391 {"ds.s", s_space, 4},
392 {"ds.w", s_space, 2},
393 {"ds.x", s_space, 'x'},
394 {"debug", s_ignore, 0},
395 #ifdef S_SET_DESC
396 {"desc", s_desc, 0},
397 #endif
398 /* dim */
399 {"double", float_cons, 'd'},
400 /* dsect */
401 {"eject", listing_eject, 0}, /* Formfeed listing. */
402 {"else", s_else, 0},
403 {"elsec", s_else, 0},
404 {"elseif", s_elseif, (int) O_ne},
405 {"end", s_end, 0},
406 {"endc", s_endif, 0},
407 {"endfunc", s_func, 1},
408 {"endif", s_endif, 0},
409 {"endm", s_bad_end, 0},
410 {"endr", s_bad_end, 1},
411 /* endef */
412 {"equ", s_set, 0},
413 {"equiv", s_set, 1},
414 {"eqv", s_set, -1},
415 {"err", s_err, 0},
416 {"error", s_errwarn, 1},
417 {"exitm", s_mexit, 0},
418 /* extend */
419 {"extern", s_ignore, 0}, /* We treat all undef as ext. */
420 {"fail", s_fail, 0},
421 {"file", s_file, 0},
422 {"fill", s_fill, 0},
423 {"float", float_cons, 'f'},
424 {"format", s_ignore, 0},
425 {"func", s_func, 0},
426 {"global", s_globl, 0},
427 {"globl", s_globl, 0},
428 {"hword", cons, 2},
429 {"if", s_if, (int) O_ne},
430 {"ifb", s_ifb, 1},
431 {"ifc", s_ifc, 0},
432 {"ifdef", s_ifdef, 0},
433 {"ifeq", s_if, (int) O_eq},
434 {"ifeqs", s_ifeqs, 0},
435 {"ifge", s_if, (int) O_ge},
436 {"ifgt", s_if, (int) O_gt},
437 {"ifle", s_if, (int) O_le},
438 {"iflt", s_if, (int) O_lt},
439 {"ifnb", s_ifb, 0},
440 {"ifnc", s_ifc, 1},
441 {"ifndef", s_ifdef, 1},
442 {"ifne", s_if, (int) O_ne},
443 {"ifnes", s_ifeqs, 1},
444 {"ifnotdef", s_ifdef, 1},
445 {"incbin", s_incbin, 0},
446 {"include", s_include, 0},
447 {"int", cons, 4},
448 {"irp", s_irp, 0},
449 {"irep", s_irp, 0},
450 {"irpc", s_irp, 1},
451 {"irepc", s_irp, 1},
452 {"lcomm", s_lcomm, 0},
453 {"lflags", s_ignore, 0}, /* Listing flags. */
454 {"linefile", s_linefile, 0},
455 {"linkonce", s_linkonce, 0},
456 {"list", listing_list, 1}, /* Turn listing on. */
457 {"llen", listing_psize, 1},
458 {"long", cons, 4},
459 {"lsym", s_lsym, 0},
460 {"macro", s_macro, 0},
461 {"mexit", s_mexit, 0},
462 {"mri", s_mri, 0},
463 {".mri", s_mri, 0}, /* Special case so .mri works in MRI mode. */
464 {"name", s_ignore, 0},
465 {"noaltmacro", s_altmacro, 0},
466 {"noformat", s_ignore, 0},
467 {"nolist", listing_list, 0}, /* Turn listing off. */
468 {"nopage", listing_nopage, 0},
469 {"nop", s_nop, 0},
470 {"nops", s_nops, 0},
471 {"octa", cons, 16},
472 {"offset", s_struct, 0},
473 {"org", s_org, 0},
474 {"p2align", s_align_ptwo, 0},
475 {"p2alignw", s_align_ptwo, -2},
476 {"p2alignl", s_align_ptwo, -4},
477 {"page", listing_eject, 0},
478 {"plen", listing_psize, 0},
479 {"print", s_print, 0},
480 {"psize", listing_psize, 0}, /* Set paper size. */
481 {"purgem", s_purgem, 0},
482 {"quad", cons, 8},
483 {"reloc", s_reloc, 0},
484 {"rep", s_rept, 0},
485 {"rept", s_rept, 0},
486 {"rva", s_rva, 4},
487 {"sbttl", listing_title, 1}, /* Subtitle of listing. */
488 /* scl */
489 /* sect */
490 {"set", s_set, 0},
491 {"short", cons, 2},
492 {"single", float_cons, 'f'},
493 /* size */
494 {"space", s_space, 0},
495 {"skip", s_space, 0},
496 {"sleb128", s_leb128, 1},
497 {"spc", s_ignore, 0},
498 {"stabd", s_stab, 'd'},
499 {"stabn", s_stab, 'n'},
500 {"stabs", s_stab, 's'},
501 {"string", stringer, 8+1},
502 {"string8", stringer, 8+1},
503 {"string16", stringer, 16+1},
504 {"string32", stringer, 32+1},
505 {"string64", stringer, 64+1},
506 {"struct", s_struct, 0},
507 /* tag */
508 {"text", s_text, 0},
509
510 /* This is for gcc to use. It's only just been added (2/94), so gcc
511 won't be able to use it for a while -- probably a year or more.
512 But once this has been released, check with gcc maintainers
513 before deleting it or even changing the spelling. */
514 {"this_GCC_requires_the_GNU_assembler", s_ignore, 0},
515 /* If we're folding case -- done for some targets, not necessarily
516 all -- the above string in an input file will be converted to
517 this one. Match it either way... */
518 {"this_gcc_requires_the_gnu_assembler", s_ignore, 0},
519
520 {"title", listing_title, 0}, /* Listing title. */
521 {"ttl", listing_title, 0},
522 /* type */
523 {"uleb128", s_leb128, 0},
524 /* use */
525 /* val */
526 {"xcom", s_comm, 0},
527 {"xdef", s_globl, 0},
528 {"xref", s_ignore, 0},
529 {"xstabs", s_xstab, 's'},
530 {"warning", s_errwarn, 0},
531 {"weakref", s_weakref, 0},
532 {"word", cons, 2},
533 {"zero", s_space, 0},
534 {"2byte", cons, 2},
535 {"4byte", cons, 4},
536 {"8byte", cons, 8},
537 {NULL, NULL, 0} /* End sentinel. */
538 };
539
540 static offsetT
get_absolute_expr(expressionS * exp)541 get_absolute_expr (expressionS *exp)
542 {
543 expression_and_evaluate (exp);
544
545 if (exp->X_op != O_constant)
546 {
547 if (exp->X_op != O_absent)
548 as_bad (_("bad or irreducible absolute expression"));
549 exp->X_add_number = 0;
550 }
551 return exp->X_add_number;
552 }
553
554 offsetT
get_absolute_expression(void)555 get_absolute_expression (void)
556 {
557 expressionS exp;
558
559 return get_absolute_expr (&exp);
560 }
561
562 static int pop_override_ok;
563 static const char *pop_table_name;
564
565 void
pop_insert(const pseudo_typeS * table)566 pop_insert (const pseudo_typeS *table)
567 {
568 const pseudo_typeS *pop;
569 for (pop = table; pop->poc_name; pop++)
570 {
571 if (str_hash_insert (po_hash, pop->poc_name, pop, 0) != NULL)
572 {
573 if (!pop_override_ok)
574 as_fatal (_("error constructing %s pseudo-op table"),
575 pop_table_name);
576 }
577 }
578 }
579
580 #ifndef md_pop_insert
581 #define md_pop_insert() pop_insert(md_pseudo_table)
582 #endif
583
584 #ifndef obj_pop_insert
585 #define obj_pop_insert() pop_insert(obj_pseudo_table)
586 #endif
587
588 #ifndef cfi_pop_insert
589 #define cfi_pop_insert() pop_insert(cfi_pseudo_table)
590 #endif
591
592 #ifndef scfi_pop_insert
593 #define scfi_pop_insert() pop_insert(scfi_pseudo_table)
594 #endif
595
596 static void
pobegin(void)597 pobegin (void)
598 {
599 po_hash = str_htab_create ();
600
601 /* Do the target-specific pseudo ops. */
602 pop_table_name = "md";
603 pop_override_ok = 0;
604 md_pop_insert ();
605
606 /* Now object specific. Skip any that were in the target table. */
607 pop_table_name = "obj";
608 pop_override_ok = 1;
609 obj_pop_insert ();
610
611 /* Now portable ones. Skip any that we've seen already. */
612 pop_table_name = "standard";
613 pop_insert (potable);
614
615 /* Now CFI ones. */
616 #if defined (TARGET_USE_SCFI) && defined (TARGET_USE_GINSN)
617 if (flag_synth_cfi)
618 {
619 pop_table_name = "scfi";
620 scfi_pop_insert ();
621 }
622 else
623 #endif
624 {
625 pop_table_name = "cfi";
626 cfi_pop_insert ();
627 }
628 }
629
630 static void
poend(void)631 poend (void)
632 {
633 htab_delete (po_hash);
634 }
635
636 #define HANDLE_CONDITIONAL_ASSEMBLY(num_read) \
637 if (ignore_input ()) \
638 { \
639 char *eol = find_end_of_line (input_line_pointer - (num_read), \
640 flag_m68k_mri); \
641 input_line_pointer = (input_line_pointer <= buffer_limit \
642 && eol >= buffer_limit) \
643 ? buffer_limit \
644 : eol + 1; \
645 continue; \
646 }
647
648 /* Helper function of read_a_source_file, which tries to expand a macro. */
649 static int
try_macro(char term,const char * line)650 try_macro (char term, const char *line)
651 {
652 sb out;
653 const char *err;
654 macro_entry *macro;
655
656 if (check_macro (line, &out, &err, ¯o))
657 {
658 if (err != NULL)
659 as_bad ("%s", err);
660 *input_line_pointer++ = term;
661 input_scrub_include_sb (&out,
662 input_line_pointer, expanding_macro);
663 sb_kill (&out);
664 buffer_limit =
665 input_scrub_next_buffer (&input_line_pointer);
666 #ifdef md_macro_info
667 md_macro_info (macro);
668 #endif
669 return 1;
670 }
671 return 0;
672 }
673
674 #ifdef HANDLE_BUNDLE
675 /* Start a new instruction bundle. Returns the rs_align_code frag that
676 will be used to align the new bundle. */
677 static fragS *
start_bundle(void)678 start_bundle (void)
679 {
680 fragS *frag = frag_now;
681
682 frag_align_code (0, 0);
683
684 while (frag->fr_type != rs_align_code)
685 frag = frag->fr_next;
686
687 gas_assert (frag != frag_now);
688
689 return frag;
690 }
691
692 /* Calculate the maximum size after relaxation of the region starting
693 at the given frag and extending through frag_now (which is unfinished). */
694 static unsigned int
pending_bundle_size(fragS * frag)695 pending_bundle_size (fragS *frag)
696 {
697 unsigned int offset = frag->fr_fix;
698 unsigned int size = 0;
699
700 gas_assert (frag != frag_now);
701 gas_assert (frag->fr_type == rs_align_code);
702
703 while (frag != frag_now)
704 {
705 /* This should only happen in what will later become an error case. */
706 if (frag == NULL)
707 return 0;
708
709 size += frag->fr_fix;
710 if (frag->fr_type == rs_machine_dependent)
711 size += md_frag_max_var (frag);
712
713 frag = frag->fr_next;
714 }
715
716 gas_assert (frag == frag_now);
717 size += frag_now_fix ();
718 if (frag->fr_type == rs_machine_dependent)
719 size += md_frag_max_var (frag);
720
721 gas_assert (size >= offset);
722
723 return size - offset;
724 }
725
726 /* Finish off the frag created to ensure bundle alignment. */
727 static void
finish_bundle(fragS * frag,unsigned int size)728 finish_bundle (fragS *frag, unsigned int size)
729 {
730 gas_assert (bundle_align_p2 > 0);
731 gas_assert (frag->fr_type == rs_align_code);
732
733 if (size > 1)
734 {
735 /* If there is more than a single byte, then we need to set up the
736 alignment frag. Otherwise we leave it at its initial state from
737 calling frag_align_code (0, 0), so that it does nothing. */
738 frag->fr_offset = bundle_align_p2;
739 frag->fr_subtype = size - 1;
740 }
741
742 /* We do this every time rather than just in s_bundle_align_mode
743 so that we catch any affected section without needing hooks all
744 over for all paths that do section changes. It's cheap enough. */
745 if (bundle_align_p2 > OCTETS_PER_BYTE_POWER)
746 record_alignment (now_seg, bundle_align_p2 - OCTETS_PER_BYTE_POWER);
747 }
748
749 /* Assemble one instruction. This takes care of the bundle features
750 around calling md_assemble. */
751 static void
assemble_one(char * line)752 assemble_one (char *line)
753 {
754 fragS *insn_start_frag = NULL;
755
756 if (bundle_lock_frchain != NULL && bundle_lock_frchain != frchain_now)
757 {
758 as_bad (_("cannot change section or subsection inside .bundle_lock"));
759 /* Clearing this serves as a marker that we have already complained. */
760 bundle_lock_frchain = NULL;
761 }
762
763 if (bundle_lock_frchain == NULL && bundle_align_p2 > 0)
764 insn_start_frag = start_bundle ();
765
766 md_assemble (line);
767
768 if (bundle_lock_frchain != NULL)
769 {
770 /* Make sure this hasn't pushed the locked sequence
771 past the bundle size. */
772 unsigned int bundle_size = pending_bundle_size (bundle_lock_frag);
773 if (bundle_size > 1U << bundle_align_p2)
774 as_bad (_ (".bundle_lock sequence at %u bytes, "
775 "but .bundle_align_mode limit is %u bytes"),
776 bundle_size, 1U << bundle_align_p2);
777 }
778 else if (bundle_align_p2 > 0)
779 {
780 unsigned int insn_size = pending_bundle_size (insn_start_frag);
781
782 if (insn_size > 1U << bundle_align_p2)
783 as_bad (_("single instruction is %u bytes long, "
784 "but .bundle_align_mode limit is %u bytes"),
785 insn_size, 1U << bundle_align_p2);
786
787 finish_bundle (insn_start_frag, insn_size);
788 }
789 }
790
791 #else /* !HANDLE_BUNDLE */
792
793 # define assemble_one(line) md_assemble(line)
794
795 #endif /* HANDLE_BUNDLE */
796
797 static bool
in_bss(void)798 in_bss (void)
799 {
800 flagword flags = bfd_section_flags (now_seg);
801
802 return (flags & SEC_ALLOC) && !(flags & (SEC_LOAD | SEC_HAS_CONTENTS));
803 }
804
805 /* Guts of .align directive:
806 N is the power of two to which to align. A value of zero is accepted but
807 ignored: the default alignment of the section will be at least this.
808 FILL may be NULL, or it may point to the bytes of the fill pattern.
809 LEN is the length of whatever FILL points to, if anything. If LEN is zero
810 but FILL is not NULL then LEN is treated as if it were one.
811 MAX is the maximum number of characters to skip when doing the alignment,
812 or 0 if there is no maximum. */
813
814 void
do_align(unsigned int n,char * fill,unsigned int len,unsigned int max)815 do_align (unsigned int n, char *fill, unsigned int len, unsigned int max)
816 {
817 if (now_seg == absolute_section || in_bss ())
818 {
819 if (fill != NULL)
820 while (len-- > 0)
821 if (*fill++ != '\0')
822 {
823 if (now_seg == absolute_section)
824 as_warn (_("ignoring fill value in absolute section"));
825 else
826 as_warn (_("ignoring fill value in section `%s'"),
827 segment_name (now_seg));
828 break;
829 }
830 fill = NULL;
831 len = 0;
832 }
833
834 #ifdef md_flush_pending_output
835 md_flush_pending_output ();
836 #endif
837
838 #ifdef md_do_align
839 md_do_align (n, fill, len, max, just_record_alignment);
840 #endif
841
842 /* Only make a frag if we HAVE to... */
843 if ((n > OCTETS_PER_BYTE_POWER) && !need_pass_2)
844 {
845 if (fill == NULL)
846 {
847 if (subseg_text_p (now_seg))
848 frag_align_code (n, max);
849 else
850 frag_align (n, 0, max);
851 }
852 else if (len <= 1)
853 frag_align (n, *fill, max);
854 else
855 frag_align_pattern (n, fill, len, max);
856 }
857
858 #ifdef md_do_align
859 just_record_alignment: ATTRIBUTE_UNUSED_LABEL
860 #endif
861
862 if (n > OCTETS_PER_BYTE_POWER)
863 record_alignment (now_seg, n - OCTETS_PER_BYTE_POWER);
864 }
865
866 /* We read the file, putting things into a web that represents what we
867 have been reading. */
868 void
read_a_source_file(const char * name)869 read_a_source_file (const char *name)
870 {
871 char nul_char;
872 char next_char;
873 char *s; /* String of symbol, '\0' appended. */
874 long temp;
875 const pseudo_typeS *pop;
876
877 #ifdef WARN_COMMENTS
878 found_comment = 0;
879 #endif
880
881 buffer = input_scrub_new_file (name);
882
883 listing_file (name);
884 listing_newline (NULL);
885 register_dependency (name);
886
887 /* Generate debugging information before we've read anything in to denote
888 this file as the "main" source file and not a subordinate one
889 (e.g. N_SO vs N_SOL in stabs). */
890 generate_file_debug ();
891
892 while ((buffer_limit = input_scrub_next_buffer (&input_line_pointer)) != 0)
893 { /* We have another line to parse. */
894 #ifndef NO_LISTING
895 /* In order to avoid listing macro expansion lines with labels
896 multiple times, keep track of which line was last issued. */
897 char *last_eol = NULL;
898
899 #endif
900 while (input_line_pointer < buffer_limit)
901 {
902 bool was_new_line;
903 /* We have more of this buffer to parse. */
904
905 /* We now have input_line_pointer->1st char of next line.
906 If input_line_pointer [-1] == '\n' then we just
907 scanned another line: so bump line counters. */
908 was_new_line = is_end_of_line[(unsigned char) input_line_pointer[-1]];
909 if (was_new_line)
910 {
911 symbol_set_value_now (&dot_symbol);
912 #ifdef md_start_line_hook
913 md_start_line_hook ();
914 #endif
915 if (input_line_pointer[-1] == '\n')
916 bump_line_counters ();
917 }
918
919 #ifndef NO_LISTING
920 /* If listing is on, and we are expanding a macro, then give
921 the listing code the contents of the expanded line. */
922 if (listing)
923 {
924 if ((listing & LISTING_MACEXP) && macro_nest > 0)
925 {
926 /* Find the end of the current expanded macro line. */
927 s = find_end_of_line (input_line_pointer, flag_m68k_mri);
928
929 if (s != last_eol
930 && !startswith (input_line_pointer,
931 !flag_m68k_mri ? " .linefile "
932 : " linefile "))
933 {
934 char *copy;
935 size_t len;
936
937 last_eol = s;
938 /* Copy it for safe keeping. Also give an indication of
939 how much macro nesting is involved at this point. */
940 len = s - input_line_pointer;
941 copy = XNEWVEC (char, len + macro_nest + 2);
942 memset (copy, '>', macro_nest);
943 copy[macro_nest] = ' ';
944 memcpy (copy + macro_nest + 1, input_line_pointer, len);
945 copy[macro_nest + 1 + len] = '\0';
946
947 /* Install the line with the listing facility. */
948 listing_newline (copy);
949 }
950 }
951 else
952 listing_newline (NULL);
953 }
954 #endif
955 if (was_new_line)
956 {
957 line_label = NULL;
958
959 if (LABELS_WITHOUT_COLONS || flag_m68k_mri)
960 {
961 next_char = * input_line_pointer;
962 /* Text at the start of a line must be a label, we
963 run down and stick a colon in. */
964 if (is_name_beginner (next_char) || next_char == '"')
965 {
966 char *line_start;
967 int mri_line_macro;
968
969 HANDLE_CONDITIONAL_ASSEMBLY (0);
970
971 nul_char = get_symbol_name (& line_start);
972 next_char = (nul_char == '"' ? input_line_pointer[1] : nul_char);
973
974 /* In MRI mode, the EQU and MACRO pseudoops must
975 be handled specially. */
976 mri_line_macro = 0;
977 if (flag_m68k_mri)
978 {
979 char *rest = input_line_pointer + 1;
980
981 if (*rest == ':')
982 ++rest;
983 if (*rest == ' ' || *rest == '\t')
984 ++rest;
985 if ((strncasecmp (rest, "EQU", 3) == 0
986 || strncasecmp (rest, "SET", 3) == 0)
987 && (rest[3] == ' ' || rest[3] == '\t'))
988 {
989 input_line_pointer = rest + 3;
990 equals (line_start,
991 strncasecmp (rest, "SET", 3) == 0);
992 continue;
993 }
994 if (strncasecmp (rest, "MACRO", 5) == 0
995 && (rest[5] == ' '
996 || rest[5] == '\t'
997 || is_end_of_line[(unsigned char) rest[5]]))
998 mri_line_macro = 1;
999 }
1000
1001 /* In MRI mode, we need to handle the MACRO
1002 pseudo-op specially: we don't want to put the
1003 symbol in the symbol table. */
1004 if (!mri_line_macro
1005 #ifdef TC_START_LABEL_WITHOUT_COLON
1006 && TC_START_LABEL_WITHOUT_COLON (nul_char, next_char)
1007 #endif
1008 )
1009 line_label = colon (line_start);
1010 else
1011 line_label = symbol_create (line_start,
1012 absolute_section,
1013 &zero_address_frag, 0);
1014
1015 next_char = restore_line_pointer (nul_char);
1016 if (next_char == ':')
1017 input_line_pointer++;
1018 }
1019 }
1020 }
1021
1022 /* We are at the beginning of a line, or similar place.
1023 We expect a well-formed assembler statement.
1024 A "symbol-name:" is a statement.
1025
1026 Depending on what compiler is used, the order of these tests
1027 may vary to catch most common case 1st.
1028 Each test is independent of all other tests at the (top)
1029 level. */
1030 do
1031 nul_char = next_char = *input_line_pointer++;
1032 while (next_char == '\t' || next_char == ' ' || next_char == '\f');
1033
1034 /* C is the 1st significant character.
1035 Input_line_pointer points after that character. */
1036 if (is_name_beginner (next_char) || next_char == '"')
1037 {
1038 char *rest;
1039
1040 /* Want user-defined label or pseudo/opcode. */
1041 HANDLE_CONDITIONAL_ASSEMBLY (1);
1042
1043 --input_line_pointer;
1044 nul_char = get_symbol_name (& s); /* name's delimiter. */
1045 next_char = (nul_char == '"' ? input_line_pointer[1] : nul_char);
1046 rest = input_line_pointer + (nul_char == '"' ? 2 : 1);
1047
1048 /* NEXT_CHAR is character after symbol.
1049 The end of symbol in the input line is now '\0'.
1050 S points to the beginning of the symbol.
1051 [In case of pseudo-op, s->'.'.]
1052 Input_line_pointer->'\0' where NUL_CHAR was. */
1053 if (TC_START_LABEL (s, nul_char, next_char))
1054 {
1055 if (flag_m68k_mri)
1056 {
1057 /* In MRI mode, \tsym: set 0 is permitted. */
1058 if (*rest == ':')
1059 ++rest;
1060
1061 if (*rest == ' ' || *rest == '\t')
1062 ++rest;
1063
1064 if ((strncasecmp (rest, "EQU", 3) == 0
1065 || strncasecmp (rest, "SET", 3) == 0)
1066 && (rest[3] == ' ' || rest[3] == '\t'))
1067 {
1068 input_line_pointer = rest + 3;
1069 equals (s, 1);
1070 continue;
1071 }
1072 }
1073
1074 line_label = colon (s); /* User-defined label. */
1075 restore_line_pointer (nul_char);
1076 ++ input_line_pointer;
1077 #ifdef tc_check_label
1078 tc_check_label (line_label);
1079 #endif
1080 /* Input_line_pointer->after ':'. */
1081 SKIP_WHITESPACE ();
1082 }
1083 else if ((next_char == '=' && *rest == '=')
1084 || ((next_char == ' ' || next_char == '\t')
1085 && rest[0] == '='
1086 && rest[1] == '='))
1087 {
1088 equals (s, -1);
1089 demand_empty_rest_of_line ();
1090 }
1091 else if ((next_char == '='
1092 || ((next_char == ' ' || next_char == '\t')
1093 && *rest == '='))
1094 #ifdef TC_EQUAL_IN_INSN
1095 && !TC_EQUAL_IN_INSN (next_char, s)
1096 #endif
1097 )
1098 {
1099 equals (s, 1);
1100 demand_empty_rest_of_line ();
1101 }
1102 else
1103 {
1104 /* Expect pseudo-op or machine instruction. */
1105 pop = NULL;
1106
1107 #ifndef TC_CASE_SENSITIVE
1108 {
1109 char *s2 = s;
1110
1111 strncpy (original_case_string, s2,
1112 sizeof (original_case_string) - 1);
1113 original_case_string[sizeof (original_case_string) - 1] = 0;
1114
1115 while (*s2)
1116 {
1117 *s2 = TOLOWER (*s2);
1118 s2++;
1119 }
1120 }
1121 #endif
1122 if (NO_PSEUDO_DOT || flag_m68k_mri)
1123 {
1124 /* The MRI assembler uses pseudo-ops without
1125 a period. */
1126 pop = str_hash_find (po_hash, s);
1127 if (pop != NULL && pop->poc_handler == NULL)
1128 pop = NULL;
1129 }
1130
1131 if (pop != NULL
1132 || (!flag_m68k_mri && *s == '.'))
1133 {
1134 /* PSEUDO - OP.
1135
1136 WARNING: next_char may be end-of-line.
1137 We lookup the pseudo-op table with s+1 because we
1138 already know that the pseudo-op begins with a '.'. */
1139
1140 if (pop == NULL)
1141 pop = str_hash_find (po_hash, s + 1);
1142 if (pop && !pop->poc_handler)
1143 pop = NULL;
1144
1145 /* In MRI mode, we may need to insert an
1146 automatic alignment directive. What a hack
1147 this is. */
1148 if (mri_pending_align
1149 && (pop == NULL
1150 || !((pop->poc_handler == cons
1151 && pop->poc_val == 1)
1152 || (pop->poc_handler == s_space
1153 && pop->poc_val == 1)
1154 #ifdef tc_conditional_pseudoop
1155 || tc_conditional_pseudoop (pop)
1156 #endif
1157 || pop->poc_handler == s_if
1158 || pop->poc_handler == s_ifdef
1159 || pop->poc_handler == s_ifc
1160 || pop->poc_handler == s_ifeqs
1161 || pop->poc_handler == s_else
1162 || pop->poc_handler == s_endif
1163 || pop->poc_handler == s_globl
1164 || pop->poc_handler == s_ignore)))
1165 {
1166 do_align (1, (char *) NULL, 0, 0);
1167 mri_pending_align = 0;
1168
1169 if (line_label != NULL)
1170 {
1171 symbol_set_frag (line_label, frag_now);
1172 S_SET_VALUE (line_label, frag_now_fix ());
1173 }
1174 }
1175
1176 /* Print the error msg now, while we still can. */
1177 if (pop == NULL)
1178 {
1179 char *end = input_line_pointer;
1180
1181 (void) restore_line_pointer (nul_char);
1182 s_ignore (0);
1183 nul_char = next_char = *--input_line_pointer;
1184 *input_line_pointer = '\0';
1185 if (! macro_defined || ! try_macro (next_char, s))
1186 {
1187 *end = '\0';
1188 as_bad (_("unknown pseudo-op: `%s'"), s);
1189 *input_line_pointer++ = nul_char;
1190 }
1191 continue;
1192 }
1193
1194 /* Put it back for error messages etc. */
1195 next_char = restore_line_pointer (nul_char);
1196 /* The following skip of whitespace is compulsory.
1197 A well shaped space is sometimes all that separates
1198 keyword from operands. */
1199 if (next_char == ' ' || next_char == '\t')
1200 input_line_pointer++;
1201
1202 /* Input_line is restored.
1203 Input_line_pointer->1st non-blank char
1204 after pseudo-operation. */
1205 (*pop->poc_handler) (pop->poc_val);
1206
1207 /* If that was .end, just get out now. */
1208 if (pop->poc_handler == s_end)
1209 goto quit;
1210 }
1211 else
1212 {
1213 /* WARNING: next_char may be end-of-line. */
1214 /* Also: input_line_pointer->`\0` where nul_char was. */
1215 (void) restore_line_pointer (nul_char);
1216 input_line_pointer = _find_end_of_line (input_line_pointer, flag_m68k_mri, 1, 0);
1217 next_char = nul_char = *input_line_pointer;
1218 *input_line_pointer = '\0';
1219
1220 generate_lineno_debug ();
1221
1222 if (macro_defined && try_macro (next_char, s))
1223 continue;
1224
1225 if (mri_pending_align)
1226 {
1227 do_align (1, (char *) NULL, 0, 0);
1228 mri_pending_align = 0;
1229 if (line_label != NULL)
1230 {
1231 symbol_set_frag (line_label, frag_now);
1232 S_SET_VALUE (line_label, frag_now_fix ());
1233 }
1234 }
1235
1236 assemble_one (s); /* Assemble 1 instruction. */
1237
1238 /* PR 19630: The backend may have set ilp to NULL
1239 if it encountered a catastrophic failure. */
1240 if (input_line_pointer == NULL)
1241 as_fatal (_("unable to continue with assembly."));
1242
1243 *input_line_pointer++ = nul_char;
1244
1245 /* We resume loop AFTER the end-of-line from
1246 this instruction. */
1247 }
1248 }
1249 continue;
1250 }
1251
1252 /* Empty statement? */
1253 if (is_end_of_line[(unsigned char) next_char])
1254 continue;
1255
1256 if ((LOCAL_LABELS_DOLLAR || LOCAL_LABELS_FB) && ISDIGIT (next_char))
1257 {
1258 /* local label ("4:") */
1259 char *backup = input_line_pointer;
1260
1261 HANDLE_CONDITIONAL_ASSEMBLY (1);
1262
1263 temp = next_char - '0';
1264
1265 if (nul_char == '"')
1266 ++ input_line_pointer;
1267
1268 /* Read the whole number. */
1269 while (ISDIGIT (*input_line_pointer))
1270 {
1271 const long digit = *input_line_pointer - '0';
1272 if (temp > (INT_MAX - digit) / 10)
1273 {
1274 as_bad (_("local label too large near %s"), backup);
1275 temp = -1;
1276 break;
1277 }
1278 temp = temp * 10 + digit;
1279 ++input_line_pointer;
1280 }
1281
1282 /* Overflow: stop processing the label. */
1283 if (temp == -1)
1284 {
1285 ignore_rest_of_line ();
1286 continue;
1287 }
1288
1289 if (LOCAL_LABELS_DOLLAR
1290 && *input_line_pointer == '$'
1291 && *(input_line_pointer + 1) == ':')
1292 {
1293 input_line_pointer += 2;
1294
1295 if (dollar_label_defined (temp))
1296 {
1297 as_fatal (_("label \"%ld$\" redefined"), temp);
1298 }
1299
1300 define_dollar_label (temp);
1301 colon (dollar_label_name (temp, 0));
1302 continue;
1303 }
1304
1305 if (LOCAL_LABELS_FB
1306 && *input_line_pointer++ == ':')
1307 {
1308 fb_label_instance_inc (temp);
1309 colon (fb_label_name (temp, 0));
1310 continue;
1311 }
1312
1313 input_line_pointer = backup;
1314 }
1315
1316 if (next_char && strchr (line_comment_chars, next_char))
1317 { /* Its a comment. Better say APP or NO_APP. */
1318 sb sbuf;
1319 char *ends;
1320 size_t len;
1321
1322 s = input_line_pointer;
1323 if (!startswith (s, "APP\n"))
1324 {
1325 /* We ignore it. */
1326 ignore_rest_of_line ();
1327 continue;
1328 }
1329 bump_line_counters ();
1330 s += 4;
1331
1332 ends = strstr (s, "#NO_APP\n");
1333 len = ends ? ends - s : buffer_limit - s;
1334
1335 sb_build (&sbuf, len + 100);
1336 sb_add_buffer (&sbuf, s, len);
1337 if (!ends)
1338 {
1339 /* The end of the #APP wasn't in this buffer. We
1340 keep reading in buffers until we find the #NO_APP
1341 that goes with this #APP There is one. The specs
1342 guarantee it... */
1343 do
1344 {
1345 buffer_limit = input_scrub_next_buffer (&buffer);
1346 if (!buffer_limit)
1347 break;
1348 ends = strstr (buffer, "#NO_APP\n");
1349 len = ends ? ends - buffer : buffer_limit - buffer;
1350 sb_add_buffer (&sbuf, buffer, len);
1351 }
1352 while (!ends);
1353 }
1354
1355 input_line_pointer = ends ? ends + 8 : NULL;
1356 input_scrub_include_sb (&sbuf, input_line_pointer, expanding_none);
1357 sb_kill (&sbuf);
1358 buffer_limit = input_scrub_next_buffer (&input_line_pointer);
1359 continue;
1360 }
1361
1362 HANDLE_CONDITIONAL_ASSEMBLY (1);
1363
1364 #ifdef tc_unrecognized_line
1365 if (tc_unrecognized_line (next_char))
1366 continue;
1367 #endif
1368 input_line_pointer--;
1369 /* Report unknown char as error. */
1370 demand_empty_rest_of_line ();
1371 }
1372 }
1373
1374 quit:
1375 symbol_set_value_now (&dot_symbol);
1376
1377 #ifdef HANDLE_BUNDLE
1378 if (bundle_lock_frag != NULL)
1379 {
1380 as_bad_where (bundle_lock_frag->fr_file, bundle_lock_frag->fr_line,
1381 _(".bundle_lock with no matching .bundle_unlock"));
1382 bundle_lock_frag = NULL;
1383 bundle_lock_frchain = NULL;
1384 bundle_lock_depth = 0;
1385 }
1386 #endif
1387
1388 if (flag_synth_cfi)
1389 ginsn_data_end (symbol_temp_new_now ());
1390
1391 #ifdef md_cleanup
1392 md_cleanup ();
1393 #endif
1394 /* Close the input file. */
1395 input_scrub_close ();
1396 #ifdef WARN_COMMENTS
1397 {
1398 if (warn_comment && found_comment)
1399 as_warn_where (found_comment_file, found_comment,
1400 "first comment found here");
1401 }
1402 #endif
1403 }
1404
1405 /* Convert O_constant expression EXP into the equivalent O_big representation.
1406 Take the sign of the number from SIGN rather than X_add_number. */
1407
1408 static void
convert_to_bignum(expressionS * exp,int sign)1409 convert_to_bignum (expressionS *exp, int sign)
1410 {
1411 valueT value;
1412 unsigned int i;
1413
1414 value = exp->X_add_number;
1415 for (i = 0; i < sizeof (exp->X_add_number) / CHARS_PER_LITTLENUM; i++)
1416 {
1417 generic_bignum[i] = value & LITTLENUM_MASK;
1418 value >>= LITTLENUM_NUMBER_OF_BITS;
1419 }
1420 /* Add a sequence of sign bits if the top bit of X_add_number is not
1421 the sign of the original value. */
1422 if ((exp->X_add_number < 0) == !sign)
1423 generic_bignum[i++] = sign ? LITTLENUM_MASK : 0;
1424 exp->X_op = O_big;
1425 exp->X_add_number = i;
1426 }
1427
1428 /* For most MRI pseudo-ops, the line actually ends at the first
1429 nonquoted space. This function looks for that point, stuffs a null
1430 in, and sets *STOPCP to the character that used to be there, and
1431 returns the location.
1432
1433 Until I hear otherwise, I am going to assume that this is only true
1434 for the m68k MRI assembler. */
1435
1436 char *
mri_comment_field(char * stopcp)1437 mri_comment_field (char *stopcp)
1438 {
1439 char *s;
1440 #ifdef TC_M68K
1441 int inquote = 0;
1442
1443 know (flag_m68k_mri);
1444
1445 for (s = input_line_pointer;
1446 ((!is_end_of_line[(unsigned char) *s] && *s != ' ' && *s != '\t')
1447 || inquote);
1448 s++)
1449 {
1450 if (*s == '\'')
1451 inquote = !inquote;
1452 }
1453 #else
1454 for (s = input_line_pointer;
1455 !is_end_of_line[(unsigned char) *s];
1456 s++)
1457 ;
1458 #endif
1459 *stopcp = *s;
1460 *s = '\0';
1461
1462 return s;
1463 }
1464
1465 /* Skip to the end of an MRI comment field. */
1466
1467 void
mri_comment_end(char * stop,int stopc)1468 mri_comment_end (char *stop, int stopc)
1469 {
1470 know (flag_mri);
1471
1472 input_line_pointer = stop;
1473 *stop = stopc;
1474 while (!is_end_of_line[(unsigned char) *input_line_pointer])
1475 ++input_line_pointer;
1476 }
1477
1478 void
s_abort(int ignore ATTRIBUTE_UNUSED)1479 s_abort (int ignore ATTRIBUTE_UNUSED)
1480 {
1481 as_fatal (_(".abort detected. Abandoning ship."));
1482 }
1483
1484 /* Handle the .align pseudo-op. A positive ARG is a default alignment
1485 (in bytes). A negative ARG is the negative of the length of the
1486 fill pattern. BYTES_P is non-zero if the alignment value should be
1487 interpreted as the byte boundary, rather than the power of 2. */
1488 #ifndef TC_ALIGN_LIMIT
1489 #define TC_ALIGN_LIMIT (stdoutput->arch_info->bits_per_address - 1)
1490 #endif
1491
1492 static void
s_align(signed int arg,int bytes_p)1493 s_align (signed int arg, int bytes_p)
1494 {
1495 unsigned int align_limit = TC_ALIGN_LIMIT;
1496 addressT align;
1497 char *stop = NULL;
1498 char stopc = 0;
1499 offsetT fill = 0;
1500 unsigned int max;
1501 int fill_p;
1502
1503 if (flag_mri)
1504 stop = mri_comment_field (&stopc);
1505
1506 if (is_end_of_line[(unsigned char) *input_line_pointer])
1507 {
1508 if (arg < 0)
1509 align = 0;
1510 else
1511 align = arg; /* Default value from pseudo-op table. */
1512 }
1513 else
1514 {
1515 align = get_absolute_expression ();
1516 SKIP_WHITESPACE ();
1517
1518 #ifdef TC_ALIGN_ZERO_IS_DEFAULT
1519 if (arg > 0 && align == 0)
1520 align = arg;
1521 #endif
1522 }
1523
1524 if (bytes_p)
1525 {
1526 /* Convert to a power of 2. */
1527 if (align != 0)
1528 {
1529 unsigned int i;
1530
1531 for (i = 0; (align & 1) == 0; align >>= 1, ++i)
1532 ;
1533 if (align != 1)
1534 as_bad (_("alignment not a power of 2"));
1535
1536 align = i;
1537 }
1538 }
1539
1540 if (align > align_limit)
1541 {
1542 align = align_limit;
1543 as_warn (_("alignment too large: %u assumed"), align_limit);
1544 }
1545
1546 if (*input_line_pointer != ',')
1547 {
1548 fill_p = 0;
1549 max = 0;
1550 }
1551 else
1552 {
1553 ++input_line_pointer;
1554 if (*input_line_pointer == ',')
1555 fill_p = 0;
1556 else
1557 {
1558 fill = get_absolute_expression ();
1559 SKIP_WHITESPACE ();
1560 fill_p = 1;
1561 }
1562
1563 if (*input_line_pointer != ',')
1564 max = 0;
1565 else
1566 {
1567 ++input_line_pointer;
1568 max = get_absolute_expression ();
1569 }
1570 }
1571
1572 if (!fill_p)
1573 {
1574 if (arg < 0)
1575 as_warn (_("expected fill pattern missing"));
1576 do_align (align, (char *) NULL, 0, max);
1577 }
1578 else
1579 {
1580 unsigned int fill_len;
1581
1582 if (arg >= 0)
1583 fill_len = 1;
1584 else
1585 fill_len = -arg;
1586
1587 if (fill_len <= 1)
1588 {
1589 char fill_char = 0;
1590
1591 fill_char = fill;
1592 do_align (align, &fill_char, fill_len, max);
1593 }
1594 else
1595 {
1596 char ab[16];
1597
1598 if ((size_t) fill_len > sizeof ab)
1599 {
1600 as_warn (_("fill pattern too long, truncating to %u"),
1601 (unsigned) sizeof ab);
1602 fill_len = sizeof ab;
1603 }
1604
1605 md_number_to_chars (ab, fill, fill_len);
1606 do_align (align, ab, fill_len, max);
1607 }
1608 }
1609
1610 demand_empty_rest_of_line ();
1611
1612 if (flag_mri)
1613 mri_comment_end (stop, stopc);
1614 }
1615
1616 /* Handle the .align pseudo-op on machines where ".align 4" means
1617 align to a 4 byte boundary. */
1618
1619 void
s_align_bytes(int arg)1620 s_align_bytes (int arg)
1621 {
1622 s_align (arg, 1);
1623 }
1624
1625 /* Handle the .align pseudo-op on machines where ".align 4" means align
1626 to a 2**4 boundary. */
1627
1628 void
s_align_ptwo(int arg)1629 s_align_ptwo (int arg)
1630 {
1631 s_align (arg, 0);
1632 }
1633
1634 /* Switch in and out of alternate macro mode. */
1635
1636 static void
s_altmacro(int on)1637 s_altmacro (int on)
1638 {
1639 demand_empty_rest_of_line ();
1640 flag_macro_alternate = on;
1641 }
1642
1643 /* Read a symbol name from input_line_pointer.
1644
1645 Stores the symbol name in a buffer and returns a pointer to this buffer.
1646 The buffer is xalloc'ed. It is the caller's responsibility to free
1647 this buffer.
1648
1649 The name is not left in the i_l_p buffer as it may need processing
1650 to handle escape characters.
1651
1652 Advances i_l_p to the next non-whitespace character.
1653
1654 If a symbol name could not be read, the routine issues an error
1655 messages, skips to the end of the line and returns NULL. */
1656
1657 char *
read_symbol_name(void)1658 read_symbol_name (void)
1659 {
1660 char * name;
1661 char * start;
1662 char c;
1663
1664 c = *input_line_pointer++;
1665
1666 if (c == '"')
1667 {
1668 #define SYM_NAME_CHUNK_LEN 128
1669 ptrdiff_t len = SYM_NAME_CHUNK_LEN;
1670 char * name_end;
1671 unsigned int C;
1672
1673 start = name = XNEWVEC (char, len + 1);
1674
1675 name_end = name + SYM_NAME_CHUNK_LEN;
1676
1677 while (is_a_char (C = next_char_of_string ()))
1678 {
1679 if (name >= name_end)
1680 {
1681 ptrdiff_t sofar;
1682
1683 sofar = name - start;
1684 len += SYM_NAME_CHUNK_LEN;
1685 start = XRESIZEVEC (char, start, len + 1);
1686 name_end = start + len;
1687 name = start + sofar;
1688 }
1689
1690 *name++ = (char) C;
1691 }
1692 *name = 0;
1693
1694 /* Since quoted symbol names can contain non-ASCII characters,
1695 check the string and warn if it cannot be recognised by the
1696 current character set. */
1697 /* PR 29447: mbstowcs ignores the third (length) parameter when
1698 the first (destination) parameter is NULL. For clarity sake
1699 therefore we pass 0 rather than 'len' as the third parameter. */
1700 if (mbstowcs (NULL, name, 0) == (size_t) -1)
1701 as_warn (_("symbol name not recognised in the current locale"));
1702 }
1703 else if (is_name_beginner (c) || (input_from_string && c == FAKE_LABEL_CHAR))
1704 {
1705 ptrdiff_t len;
1706
1707 name = input_line_pointer - 1;
1708
1709 /* We accept FAKE_LABEL_CHAR in a name in case this is
1710 being called with a constructed string. */
1711 while (is_part_of_name (c = *input_line_pointer++)
1712 || (input_from_string && c == FAKE_LABEL_CHAR))
1713 ;
1714
1715 len = (input_line_pointer - name) - 1;
1716 start = XNEWVEC (char, len + 1);
1717
1718 memcpy (start, name, len);
1719 start[len] = 0;
1720
1721 /* Skip a name ender char if one is present. */
1722 if (! is_name_ender (c))
1723 --input_line_pointer;
1724 }
1725 else
1726 name = start = NULL;
1727
1728 if (name == start)
1729 {
1730 as_bad (_("expected symbol name"));
1731 ignore_rest_of_line ();
1732 free (start);
1733 return NULL;
1734 }
1735
1736 SKIP_WHITESPACE ();
1737
1738 return start;
1739 }
1740
1741
1742 symbolS *
s_comm_internal(int param,symbolS * (* comm_parse_extra)(int,symbolS *,addressT))1743 s_comm_internal (int param,
1744 symbolS *(*comm_parse_extra) (int, symbolS *, addressT))
1745 {
1746 char *name;
1747 offsetT temp, size;
1748 symbolS *symbolP = NULL;
1749 char *stop = NULL;
1750 char stopc = 0;
1751 expressionS exp;
1752
1753 if (flag_mri)
1754 stop = mri_comment_field (&stopc);
1755
1756 if ((name = read_symbol_name ()) == NULL)
1757 goto out;
1758
1759 /* Accept an optional comma after the name. The comma used to be
1760 required, but Irix 5 cc does not generate it for .lcomm. */
1761 if (*input_line_pointer == ',')
1762 input_line_pointer++;
1763
1764 temp = get_absolute_expr (&exp);
1765 size = temp;
1766 size &= ((addressT) 2 << (stdoutput->arch_info->bits_per_address - 1)) - 1;
1767 if (exp.X_op == O_absent)
1768 {
1769 as_bad (_("missing size expression"));
1770 ignore_rest_of_line ();
1771 goto out;
1772 }
1773 else if (temp != size || (!exp.X_unsigned && exp.X_add_number < 0))
1774 {
1775 as_warn (_("size (%ld) out of range, ignored"), (long) temp);
1776 ignore_rest_of_line ();
1777 goto out;
1778 }
1779
1780 symbolP = symbol_find_or_make (name);
1781 if ((S_IS_DEFINED (symbolP) || symbol_equated_p (symbolP))
1782 && !S_IS_COMMON (symbolP))
1783 {
1784 if (!S_IS_VOLATILE (symbolP))
1785 {
1786 symbolP = NULL;
1787 as_bad (_("symbol `%s' is already defined"), name);
1788 ignore_rest_of_line ();
1789 goto out;
1790 }
1791 symbolP = symbol_clone (symbolP, 1);
1792 S_SET_SEGMENT (symbolP, undefined_section);
1793 S_SET_VALUE (symbolP, 0);
1794 symbol_set_frag (symbolP, &zero_address_frag);
1795 S_CLEAR_VOLATILE (symbolP);
1796 }
1797
1798 size = S_GET_VALUE (symbolP);
1799 if (size == 0)
1800 size = temp;
1801 else if (size != temp)
1802 as_warn (_("size of \"%s\" is already %ld; not changing to %ld"),
1803 name, (long) size, (long) temp);
1804
1805 if (comm_parse_extra != NULL)
1806 symbolP = (*comm_parse_extra) (param, symbolP, size);
1807 else
1808 {
1809 S_SET_VALUE (symbolP, (valueT) size);
1810 S_SET_EXTERNAL (symbolP);
1811 S_SET_SEGMENT (symbolP, bfd_com_section_ptr);
1812 }
1813
1814 demand_empty_rest_of_line ();
1815 out:
1816 if (flag_mri)
1817 mri_comment_end (stop, stopc);
1818 free (name);
1819 return symbolP;
1820 }
1821
1822 void
s_comm(int ignore)1823 s_comm (int ignore)
1824 {
1825 s_comm_internal (ignore, NULL);
1826 }
1827
1828 /* The MRI COMMON pseudo-op. We handle this by creating a common
1829 symbol with the appropriate name. We make s_space do the right
1830 thing by increasing the size. */
1831
1832 void
s_mri_common(int small ATTRIBUTE_UNUSED)1833 s_mri_common (int small ATTRIBUTE_UNUSED)
1834 {
1835 char *name;
1836 char c;
1837 char *alc = NULL;
1838 symbolS *sym;
1839 offsetT align;
1840 char *stop = NULL;
1841 char stopc = 0;
1842
1843 if (!flag_mri)
1844 {
1845 s_comm (0);
1846 return;
1847 }
1848
1849 stop = mri_comment_field (&stopc);
1850
1851 SKIP_WHITESPACE ();
1852
1853 name = input_line_pointer;
1854 if (!ISDIGIT (*name))
1855 c = get_symbol_name (& name);
1856 else
1857 {
1858 do
1859 {
1860 ++input_line_pointer;
1861 }
1862 while (ISDIGIT (*input_line_pointer));
1863
1864 c = *input_line_pointer;
1865 *input_line_pointer = '\0';
1866
1867 if (line_label != NULL)
1868 {
1869 alc = XNEWVEC (char, strlen (S_GET_NAME (line_label))
1870 + (input_line_pointer - name) + 1);
1871 sprintf (alc, "%s%s", name, S_GET_NAME (line_label));
1872 name = alc;
1873 }
1874 }
1875
1876 sym = symbol_find_or_make (name);
1877 c = restore_line_pointer (c);
1878 free (alc);
1879
1880 if (*input_line_pointer != ',')
1881 align = 0;
1882 else
1883 {
1884 ++input_line_pointer;
1885 align = get_absolute_expression ();
1886 }
1887
1888 if (S_IS_DEFINED (sym) && !S_IS_COMMON (sym))
1889 {
1890 as_bad (_("symbol `%s' is already defined"), S_GET_NAME (sym));
1891 mri_comment_end (stop, stopc);
1892 return;
1893 }
1894
1895 S_SET_EXTERNAL (sym);
1896 S_SET_SEGMENT (sym, bfd_com_section_ptr);
1897 mri_common_symbol = sym;
1898
1899 #ifdef S_SET_ALIGN
1900 if (align != 0)
1901 S_SET_ALIGN (sym, align);
1902 #else
1903 (void) align;
1904 #endif
1905
1906 if (line_label != NULL)
1907 {
1908 expressionS exp;
1909 exp.X_op = O_symbol;
1910 exp.X_add_symbol = sym;
1911 exp.X_add_number = 0;
1912 symbol_set_value_expression (line_label, &exp);
1913 symbol_set_frag (line_label, &zero_address_frag);
1914 S_SET_SEGMENT (line_label, expr_section);
1915 }
1916
1917 /* FIXME: We just ignore the small argument, which distinguishes
1918 COMMON and COMMON.S. I don't know what we can do about it. */
1919
1920 /* Ignore the type and hptype. */
1921 if (*input_line_pointer == ',')
1922 input_line_pointer += 2;
1923 if (*input_line_pointer == ',')
1924 input_line_pointer += 2;
1925
1926 demand_empty_rest_of_line ();
1927
1928 mri_comment_end (stop, stopc);
1929 }
1930
1931 void
s_data(int ignore ATTRIBUTE_UNUSED)1932 s_data (int ignore ATTRIBUTE_UNUSED)
1933 {
1934 segT section;
1935 int temp;
1936
1937 temp = get_absolute_expression ();
1938 if (flag_readonly_data_in_text)
1939 {
1940 section = text_section;
1941 temp += 1000;
1942 }
1943 else
1944 section = data_section;
1945
1946 subseg_set (section, (subsegT) temp);
1947
1948 demand_empty_rest_of_line ();
1949 }
1950
1951 /* Handle the .file pseudo-op. This default definition may be overridden by
1952 the object or CPU specific pseudo-ops. */
1953
1954 void
s_file_string(char * file)1955 s_file_string (char *file)
1956 {
1957 #ifdef LISTING
1958 if (listing)
1959 listing_source_file (file);
1960 #endif
1961 register_dependency (file);
1962 #ifdef obj_app_file
1963 obj_app_file (file);
1964 #endif
1965 }
1966
1967 void
s_file(int ignore ATTRIBUTE_UNUSED)1968 s_file (int ignore ATTRIBUTE_UNUSED)
1969 {
1970 char *s;
1971 int length;
1972
1973 /* Some assemblers tolerate immediately following '"'. */
1974 if ((s = demand_copy_string (&length)) != 0)
1975 {
1976 new_logical_line_flags (s, -1, 1);
1977
1978 /* In MRI mode, the preprocessor may have inserted an extraneous
1979 backquote. */
1980 if (flag_m68k_mri
1981 && *input_line_pointer == '\''
1982 && is_end_of_line[(unsigned char) input_line_pointer[1]])
1983 ++input_line_pointer;
1984
1985 demand_empty_rest_of_line ();
1986 s_file_string (s);
1987 }
1988 }
1989
1990 static bool
get_linefile_number(int * flag)1991 get_linefile_number (int *flag)
1992 {
1993 expressionS exp;
1994
1995 SKIP_WHITESPACE ();
1996
1997 if (*input_line_pointer < '0' || *input_line_pointer > '9')
1998 return false;
1999
2000 /* Don't mistakenly interpret octal numbers as line numbers. */
2001 if (*input_line_pointer == '0')
2002 {
2003 *flag = 0;
2004 ++input_line_pointer;
2005 return true;
2006 }
2007
2008 expression_and_evaluate (&exp);
2009 if (exp.X_op != O_constant)
2010 return false;
2011
2012 #if defined (BFD64) || LONG_MAX > INT_MAX
2013 if (exp.X_add_number < INT_MIN || exp.X_add_number > INT_MAX)
2014 return false;
2015 #endif
2016
2017 *flag = exp.X_add_number;
2018
2019 return true;
2020 }
2021
2022 /* Handle the .linefile pseudo-op. This is automatically generated by
2023 do_scrub_chars when a preprocessor # line comment is seen. This
2024 default definition may be overridden by the object or CPU specific
2025 pseudo-ops. */
2026
2027 void
s_linefile(int ignore ATTRIBUTE_UNUSED)2028 s_linefile (int ignore ATTRIBUTE_UNUSED)
2029 {
2030 char *file = NULL;
2031 int linenum, flags = 0;
2032
2033 /* The given number is that of the next line. */
2034 if (!get_linefile_number (&linenum))
2035 {
2036 ignore_rest_of_line ();
2037 return;
2038 }
2039
2040 if (linenum < 0)
2041 /* Some of the back ends can't deal with non-positive line numbers.
2042 Besides, it's silly. GCC however will generate a line number of
2043 zero when it is pre-processing builtins for assembler-with-cpp files:
2044
2045 # 0 "<built-in>"
2046
2047 We do not want to barf on this, especially since such files are used
2048 in the GCC and GDB testsuites. So we check for negative line numbers
2049 rather than non-positive line numbers. */
2050 as_warn (_("line numbers must be positive; line number %d rejected"),
2051 linenum);
2052 else
2053 {
2054 int length = 0;
2055
2056 SKIP_WHITESPACE ();
2057
2058 if (*input_line_pointer == '"')
2059 file = demand_copy_string (&length);
2060 else if (*input_line_pointer == '.')
2061 {
2062 /* buffer_and_nest() may insert this form. */
2063 ++input_line_pointer;
2064 flags = 1 << 3;
2065 }
2066
2067 if (file)
2068 {
2069 int this_flag;
2070
2071 while (get_linefile_number (&this_flag))
2072 switch (this_flag)
2073 {
2074 /* From GCC's cpp documentation:
2075 1: start of a new file.
2076 2: returning to a file after having included another file.
2077 3: following text comes from a system header file.
2078 4: following text should be treated as extern "C".
2079
2080 4 is nonsensical for the assembler; 3, we don't care about,
2081 so we ignore it just in case a system header file is
2082 included while preprocessing assembly. So 1 and 2 are all
2083 we care about, and they are mutually incompatible.
2084 new_logical_line_flags() demands this. */
2085 case 1:
2086 case 2:
2087 if (flags && flags != (1 << this_flag))
2088 as_warn (_("incompatible flag %i in line directive"),
2089 this_flag);
2090 else
2091 flags |= 1 << this_flag;
2092 break;
2093
2094 case 3:
2095 case 4:
2096 /* We ignore these. */
2097 break;
2098
2099 default:
2100 as_warn (_("unsupported flag %i in line directive"),
2101 this_flag);
2102 break;
2103 }
2104
2105 if (!is_end_of_line[(unsigned char)*input_line_pointer])
2106 file = NULL;
2107 }
2108
2109 if (file || flags)
2110 {
2111 demand_empty_rest_of_line ();
2112
2113 /* read_a_source_file() will bump the line number only if the line
2114 is terminated by '\n'. */
2115 if (input_line_pointer[-1] == '\n')
2116 linenum--;
2117
2118 new_logical_line_flags (file, linenum, flags);
2119 #ifdef LISTING
2120 if (listing)
2121 listing_source_line (linenum);
2122 #endif
2123 return;
2124 }
2125 }
2126 ignore_rest_of_line ();
2127 }
2128
2129 /* Handle the .end pseudo-op. Actually, the real work is done in
2130 read_a_source_file. */
2131
2132 void
s_end(int ignore ATTRIBUTE_UNUSED)2133 s_end (int ignore ATTRIBUTE_UNUSED)
2134 {
2135 if (flag_mri)
2136 {
2137 /* The MRI assembler permits the start symbol to follow .end,
2138 but we don't support that. */
2139 SKIP_WHITESPACE ();
2140 if (!is_end_of_line[(unsigned char) *input_line_pointer]
2141 && *input_line_pointer != '*'
2142 && *input_line_pointer != '!')
2143 as_warn (_("start address not supported"));
2144 }
2145 }
2146
2147 /* Handle the .err pseudo-op. */
2148
2149 void
s_err(int ignore ATTRIBUTE_UNUSED)2150 s_err (int ignore ATTRIBUTE_UNUSED)
2151 {
2152 as_bad (_(".err encountered"));
2153 demand_empty_rest_of_line ();
2154 }
2155
2156 /* Handle the .error and .warning pseudo-ops. */
2157
2158 void
s_errwarn(int err)2159 s_errwarn (int err)
2160 {
2161 int len;
2162 /* The purpose for the conditional assignment is not to
2163 internationalize the directive itself, but that we need a
2164 self-contained message, one that can be passed like the
2165 demand_copy_C_string return value, and with no assumption on the
2166 location of the name of the directive within the message. */
2167 const char *msg
2168 = (err ? _(".error directive invoked in source file")
2169 : _(".warning directive invoked in source file"));
2170
2171 if (!is_it_end_of_statement ())
2172 {
2173 if (*input_line_pointer != '\"')
2174 {
2175 as_bad (_("%s argument must be a string"),
2176 err ? ".error" : ".warning");
2177 ignore_rest_of_line ();
2178 return;
2179 }
2180
2181 msg = demand_copy_C_string (&len);
2182 if (msg == NULL)
2183 return;
2184 }
2185
2186 if (err)
2187 as_bad ("%s", msg);
2188 else
2189 as_warn ("%s", msg);
2190 demand_empty_rest_of_line ();
2191 }
2192
2193 /* Handle the MRI fail pseudo-op. */
2194
2195 void
s_fail(int ignore ATTRIBUTE_UNUSED)2196 s_fail (int ignore ATTRIBUTE_UNUSED)
2197 {
2198 offsetT temp;
2199 char *stop = NULL;
2200 char stopc = 0;
2201
2202 if (flag_mri)
2203 stop = mri_comment_field (&stopc);
2204
2205 temp = get_absolute_expression ();
2206 if (temp >= 500)
2207 as_warn (_(".fail %ld encountered"), (long) temp);
2208 else
2209 as_bad (_(".fail %ld encountered"), (long) temp);
2210
2211 demand_empty_rest_of_line ();
2212
2213 if (flag_mri)
2214 mri_comment_end (stop, stopc);
2215 }
2216
2217 void
s_fill(int ignore ATTRIBUTE_UNUSED)2218 s_fill (int ignore ATTRIBUTE_UNUSED)
2219 {
2220 expressionS rep_exp;
2221 long size = 1;
2222 long fill = 0;
2223 char *p;
2224
2225 #ifdef md_flush_pending_output
2226 md_flush_pending_output ();
2227 #endif
2228
2229 #ifdef md_cons_align
2230 md_cons_align (1);
2231 #endif
2232
2233 expression (&rep_exp);
2234 if (*input_line_pointer == ',')
2235 {
2236 input_line_pointer++;
2237 size = get_absolute_expression ();
2238 if (*input_line_pointer == ',')
2239 {
2240 input_line_pointer++;
2241 fill = get_absolute_expression ();
2242 }
2243 }
2244
2245 /* This is to be compatible with BSD 4.2 AS, not for any rational reason. */
2246 #define BSD_FILL_SIZE_CROCK_8 (8)
2247 if (size > BSD_FILL_SIZE_CROCK_8)
2248 {
2249 as_warn (_(".fill size clamped to %d"), BSD_FILL_SIZE_CROCK_8);
2250 size = BSD_FILL_SIZE_CROCK_8;
2251 }
2252 if (size < 0)
2253 {
2254 as_warn (_("size negative; .fill ignored"));
2255 size = 0;
2256 }
2257 else if (rep_exp.X_op == O_constant && rep_exp.X_add_number <= 0)
2258 {
2259 if (rep_exp.X_add_number < 0)
2260 as_warn (_("repeat < 0; .fill ignored"));
2261 size = 0;
2262 }
2263 else if (size && !need_pass_2)
2264 {
2265 if (now_seg == absolute_section && rep_exp.X_op != O_constant)
2266 {
2267 as_bad (_("non-constant fill count for absolute section"));
2268 size = 0;
2269 }
2270 else if (now_seg == absolute_section && fill && rep_exp.X_add_number != 0)
2271 {
2272 as_bad (_("attempt to fill absolute section with non-zero value"));
2273 size = 0;
2274 }
2275 else if (fill
2276 && (rep_exp.X_op != O_constant || rep_exp.X_add_number != 0)
2277 && in_bss ())
2278 {
2279 as_bad (_("attempt to fill section `%s' with non-zero value"),
2280 segment_name (now_seg));
2281 size = 0;
2282 }
2283 }
2284
2285 if (size && !need_pass_2)
2286 {
2287 if (now_seg == absolute_section)
2288 abs_section_offset += rep_exp.X_add_number * size;
2289
2290 if (rep_exp.X_op == O_constant)
2291 {
2292 p = frag_var (rs_fill, (int) size, (int) size,
2293 (relax_substateT) 0, (symbolS *) 0,
2294 (offsetT) rep_exp.X_add_number,
2295 (char *) 0);
2296 }
2297 else
2298 {
2299 /* We don't have a constant repeat count, so we can't use
2300 rs_fill. We can get the same results out of rs_space,
2301 but its argument is in bytes, so we must multiply the
2302 repeat count by size. */
2303
2304 symbolS *rep_sym;
2305 rep_sym = make_expr_symbol (&rep_exp);
2306 if (size != 1)
2307 {
2308 expressionS size_exp;
2309 size_exp.X_op = O_constant;
2310 size_exp.X_add_number = size;
2311
2312 rep_exp.X_op = O_multiply;
2313 rep_exp.X_add_symbol = rep_sym;
2314 rep_exp.X_op_symbol = make_expr_symbol (&size_exp);
2315 rep_exp.X_add_number = 0;
2316 rep_sym = make_expr_symbol (&rep_exp);
2317 }
2318
2319 p = frag_var (rs_space, (int) size, (int) size,
2320 (relax_substateT) 0, rep_sym, (offsetT) 0, (char *) 0);
2321 }
2322
2323 memset (p, 0, (unsigned int) size);
2324
2325 /* The magic number BSD_FILL_SIZE_CROCK_4 is from BSD 4.2 VAX
2326 flavoured AS. The following bizarre behaviour is to be
2327 compatible with above. I guess they tried to take up to 8
2328 bytes from a 4-byte expression and they forgot to sign
2329 extend. */
2330 #define BSD_FILL_SIZE_CROCK_4 (4)
2331 md_number_to_chars (p, (valueT) fill,
2332 (size > BSD_FILL_SIZE_CROCK_4
2333 ? BSD_FILL_SIZE_CROCK_4
2334 : (int) size));
2335 /* Note: .fill (),0 emits no frag (since we are asked to .fill 0 bytes)
2336 but emits no error message because it seems a legal thing to do.
2337 It is a degenerate case of .fill but could be emitted by a
2338 compiler. */
2339 }
2340 demand_empty_rest_of_line ();
2341 }
2342
2343 void
s_globl(int ignore ATTRIBUTE_UNUSED)2344 s_globl (int ignore ATTRIBUTE_UNUSED)
2345 {
2346 char *name;
2347 int c;
2348 symbolS *symbolP;
2349 char *stop = NULL;
2350 char stopc = 0;
2351
2352 if (flag_mri)
2353 stop = mri_comment_field (&stopc);
2354
2355 do
2356 {
2357 if ((name = read_symbol_name ()) == NULL)
2358 return;
2359
2360 symbolP = symbol_find_or_make (name);
2361 S_SET_EXTERNAL (symbolP);
2362
2363 SKIP_WHITESPACE ();
2364 c = *input_line_pointer;
2365 if (c == ',')
2366 {
2367 input_line_pointer++;
2368 SKIP_WHITESPACE ();
2369 if (is_end_of_line[(unsigned char) *input_line_pointer])
2370 c = '\n';
2371 }
2372
2373 free (name);
2374 }
2375 while (c == ',');
2376
2377 demand_empty_rest_of_line ();
2378
2379 if (flag_mri)
2380 mri_comment_end (stop, stopc);
2381 }
2382
2383 /* Handle the MRI IRP and IRPC pseudo-ops. */
2384
2385 void
s_irp(int irpc)2386 s_irp (int irpc)
2387 {
2388 char * eol;
2389 const char * file;
2390 unsigned int line;
2391 sb s;
2392 const char *err;
2393 sb out;
2394
2395 file = as_where (&line);
2396
2397 eol = find_end_of_line (input_line_pointer, 0);
2398 sb_build (&s, eol - input_line_pointer);
2399 sb_add_buffer (&s, input_line_pointer, eol - input_line_pointer);
2400 input_line_pointer = eol;
2401
2402 sb_new (&out);
2403
2404 err = expand_irp (irpc, 0, &s, &out, get_non_macro_line_sb);
2405 if (err != NULL)
2406 as_bad_where (file, line, "%s", err);
2407
2408 sb_kill (&s);
2409
2410 input_scrub_include_sb (&out, input_line_pointer, expanding_repeat);
2411 sb_kill (&out);
2412 buffer_limit = input_scrub_next_buffer (&input_line_pointer);
2413 }
2414
2415 /* Handle the .linkonce pseudo-op. This tells the assembler to mark
2416 the section to only be linked once. However, this is not supported
2417 by most object file formats. This takes an optional argument,
2418 which is what to do about duplicates. */
2419
2420 void
s_linkonce(int ignore ATTRIBUTE_UNUSED)2421 s_linkonce (int ignore ATTRIBUTE_UNUSED)
2422 {
2423 enum linkonce_type type;
2424
2425 SKIP_WHITESPACE ();
2426
2427 type = LINKONCE_DISCARD;
2428
2429 if (!is_end_of_line[(unsigned char) *input_line_pointer])
2430 {
2431 char *s;
2432 char c;
2433
2434 c = get_symbol_name (& s);
2435 if (strcasecmp (s, "discard") == 0)
2436 type = LINKONCE_DISCARD;
2437 else if (strcasecmp (s, "one_only") == 0)
2438 type = LINKONCE_ONE_ONLY;
2439 else if (strcasecmp (s, "same_size") == 0)
2440 type = LINKONCE_SAME_SIZE;
2441 else if (strcasecmp (s, "same_contents") == 0)
2442 type = LINKONCE_SAME_CONTENTS;
2443 else
2444 as_warn (_("unrecognized .linkonce type `%s'"), s);
2445
2446 (void) restore_line_pointer (c);
2447 }
2448
2449 #ifdef obj_handle_link_once
2450 obj_handle_link_once (type);
2451 #else /* ! defined (obj_handle_link_once) */
2452 {
2453 flagword flags;
2454
2455 if ((bfd_applicable_section_flags (stdoutput) & SEC_LINK_ONCE) == 0)
2456 as_warn (_(".linkonce is not supported for this object file format"));
2457
2458 flags = bfd_section_flags (now_seg);
2459 flags |= SEC_LINK_ONCE;
2460 switch (type)
2461 {
2462 default:
2463 abort ();
2464 case LINKONCE_DISCARD:
2465 flags |= SEC_LINK_DUPLICATES_DISCARD;
2466 break;
2467 case LINKONCE_ONE_ONLY:
2468 flags |= SEC_LINK_DUPLICATES_ONE_ONLY;
2469 break;
2470 case LINKONCE_SAME_SIZE:
2471 flags |= SEC_LINK_DUPLICATES_SAME_SIZE;
2472 break;
2473 case LINKONCE_SAME_CONTENTS:
2474 flags |= SEC_LINK_DUPLICATES_SAME_CONTENTS;
2475 break;
2476 }
2477 if (!bfd_set_section_flags (now_seg, flags))
2478 as_bad (_("bfd_set_section_flags: %s"),
2479 bfd_errmsg (bfd_get_error ()));
2480 }
2481 #endif /* ! defined (obj_handle_link_once) */
2482
2483 demand_empty_rest_of_line ();
2484 }
2485
2486 void
bss_alloc(symbolS * symbolP,addressT size,unsigned int align)2487 bss_alloc (symbolS *symbolP, addressT size, unsigned int align)
2488 {
2489 char *pfrag;
2490 segT current_seg = now_seg;
2491 subsegT current_subseg = now_subseg;
2492 segT bss_seg = bss_section;
2493
2494 #if defined (TC_MIPS) || defined (TC_ALPHA)
2495 if (OUTPUT_FLAVOR == bfd_target_ecoff_flavour
2496 || OUTPUT_FLAVOR == bfd_target_elf_flavour)
2497 {
2498 /* For MIPS and Alpha ECOFF or ELF, small objects are put in .sbss. */
2499 if (size <= bfd_get_gp_size (stdoutput))
2500 {
2501 bss_seg = subseg_new (".sbss", 1);
2502 seg_info (bss_seg)->bss = 1;
2503 if (!bfd_set_section_flags (bss_seg, SEC_ALLOC | SEC_SMALL_DATA))
2504 as_warn (_("error setting flags for \".sbss\": %s"),
2505 bfd_errmsg (bfd_get_error ()));
2506 }
2507 }
2508 #endif
2509 subseg_set (bss_seg, 1);
2510
2511 if (align > OCTETS_PER_BYTE_POWER)
2512 {
2513 record_alignment (bss_seg, align);
2514 frag_align (align, 0, 0);
2515 }
2516
2517 /* Detach from old frag. */
2518 if (S_GET_SEGMENT (symbolP) == bss_seg)
2519 symbol_get_frag (symbolP)->fr_symbol = NULL;
2520
2521 symbol_set_frag (symbolP, frag_now);
2522 pfrag = frag_var (rs_org, 1, 1, 0, symbolP, size * OCTETS_PER_BYTE, NULL);
2523 *pfrag = 0;
2524
2525 #ifdef S_SET_SIZE
2526 S_SET_SIZE (symbolP, size);
2527 #endif
2528 S_SET_SEGMENT (symbolP, bss_seg);
2529
2530 #ifdef OBJ_COFF
2531 /* The symbol may already have been created with a preceding
2532 ".globl" directive -- be careful not to step on storage class
2533 in that case. Otherwise, set it to static. */
2534 if (S_GET_STORAGE_CLASS (symbolP) != C_EXT)
2535 S_SET_STORAGE_CLASS (symbolP, C_STAT);
2536 #endif /* OBJ_COFF */
2537
2538 subseg_set (current_seg, current_subseg);
2539 }
2540
2541 offsetT
parse_align(int align_bytes)2542 parse_align (int align_bytes)
2543 {
2544 expressionS exp;
2545 addressT align;
2546
2547 SKIP_WHITESPACE ();
2548 if (*input_line_pointer != ',')
2549 {
2550 no_align:
2551 as_bad (_("expected alignment after size"));
2552 ignore_rest_of_line ();
2553 return -1;
2554 }
2555
2556 input_line_pointer++;
2557 SKIP_WHITESPACE ();
2558
2559 align = get_absolute_expr (&exp);
2560 if (exp.X_op == O_absent)
2561 goto no_align;
2562
2563 if (!exp.X_unsigned && exp.X_add_number < 0)
2564 {
2565 as_warn (_("alignment negative; 0 assumed"));
2566 align = 0;
2567 }
2568
2569 if (align_bytes && align != 0)
2570 {
2571 /* convert to a power of 2 alignment */
2572 unsigned int alignp2 = 0;
2573 while ((align & 1) == 0)
2574 align >>= 1, ++alignp2;
2575 if (align != 1)
2576 {
2577 as_bad (_("alignment not a power of 2"));
2578 ignore_rest_of_line ();
2579 return -1;
2580 }
2581 align = alignp2;
2582 }
2583 return align;
2584 }
2585
2586 /* Called from s_comm_internal after symbol name and size have been
2587 parsed. NEEDS_ALIGN is 0 if it was an ".lcomm" (2 args only),
2588 1 if this was a ".bss" directive which has a 3rd argument
2589 (alignment as a power of 2), or 2 if this was a ".bss" directive
2590 with alignment in bytes. */
2591
2592 symbolS *
s_lcomm_internal(int needs_align,symbolS * symbolP,addressT size)2593 s_lcomm_internal (int needs_align, symbolS *symbolP, addressT size)
2594 {
2595 addressT align = 0;
2596
2597 if (needs_align)
2598 {
2599 align = parse_align (needs_align - 1);
2600 if (align == (addressT) -1)
2601 return NULL;
2602 }
2603 else
2604 /* Assume some objects may require alignment on some systems. */
2605 TC_IMPLICIT_LCOMM_ALIGNMENT (size, align);
2606
2607 bss_alloc (symbolP, size, align);
2608 return symbolP;
2609 }
2610
2611 void
s_lcomm(int needs_align)2612 s_lcomm (int needs_align)
2613 {
2614 s_comm_internal (needs_align, s_lcomm_internal);
2615 }
2616
2617 void
s_lcomm_bytes(int needs_align)2618 s_lcomm_bytes (int needs_align)
2619 {
2620 s_comm_internal (needs_align * 2, s_lcomm_internal);
2621 }
2622
2623 void
s_lsym(int ignore ATTRIBUTE_UNUSED)2624 s_lsym (int ignore ATTRIBUTE_UNUSED)
2625 {
2626 char *name;
2627 expressionS exp;
2628 symbolS *symbolP;
2629
2630 /* We permit ANY defined expression: BSD4.2 demands constants. */
2631 if ((name = read_symbol_name ()) == NULL)
2632 return;
2633
2634 if (*input_line_pointer != ',')
2635 {
2636 as_bad (_("expected comma after \"%s\""), name);
2637 goto err_out;
2638 }
2639
2640 input_line_pointer++;
2641 expression_and_evaluate (&exp);
2642
2643 if (exp.X_op != O_constant
2644 && exp.X_op != O_register)
2645 {
2646 as_bad (_("bad expression"));
2647 goto err_out;
2648 }
2649
2650 symbolP = symbol_find_or_make (name);
2651
2652 if (S_GET_SEGMENT (symbolP) == undefined_section)
2653 {
2654 /* The name might be an undefined .global symbol; be sure to
2655 keep the "external" bit. */
2656 S_SET_SEGMENT (symbolP,
2657 (exp.X_op == O_constant
2658 ? absolute_section
2659 : reg_section));
2660 S_SET_VALUE (symbolP, (valueT) exp.X_add_number);
2661 }
2662 else
2663 {
2664 as_bad (_("symbol `%s' is already defined"), name);
2665 }
2666
2667 demand_empty_rest_of_line ();
2668 free (name);
2669 return;
2670
2671 err_out:
2672 ignore_rest_of_line ();
2673 free (name);
2674 return;
2675 }
2676
2677 /* Read a line into an sb. Returns the character that ended the line
2678 or zero if there are no more lines. */
2679
2680 static int
get_line_sb(sb * line,int in_macro)2681 get_line_sb (sb *line, int in_macro)
2682 {
2683 char *eol;
2684
2685 if (input_line_pointer[-1] == '\n')
2686 bump_line_counters ();
2687
2688 if (input_line_pointer >= buffer_limit)
2689 {
2690 buffer_limit = input_scrub_next_buffer (&input_line_pointer);
2691 if (buffer_limit == 0)
2692 return 0;
2693 }
2694
2695 eol = _find_end_of_line (input_line_pointer, flag_m68k_mri, 0, in_macro);
2696 sb_add_buffer (line, input_line_pointer, eol - input_line_pointer);
2697 input_line_pointer = eol;
2698
2699 /* Don't skip multiple end-of-line characters, because that breaks support
2700 for the IA-64 stop bit (;;) which looks like two consecutive end-of-line
2701 characters but isn't. Instead just skip one end of line character and
2702 return the character skipped so that the caller can re-insert it if
2703 necessary. */
2704 return *input_line_pointer++;
2705 }
2706
2707 static size_t
get_non_macro_line_sb(sb * line)2708 get_non_macro_line_sb (sb *line)
2709 {
2710 return get_line_sb (line, 0);
2711 }
2712
2713 static size_t
get_macro_line_sb(sb * line)2714 get_macro_line_sb (sb *line)
2715 {
2716 return get_line_sb (line, 1);
2717 }
2718
2719 /* Define a macro. This is an interface to macro.c. */
2720
2721 void
s_macro(int ignore ATTRIBUTE_UNUSED)2722 s_macro (int ignore ATTRIBUTE_UNUSED)
2723 {
2724 char *eol;
2725 sb s;
2726 macro_entry *macro;
2727
2728 eol = find_end_of_line (input_line_pointer, 0);
2729 sb_build (&s, eol - input_line_pointer);
2730 sb_add_buffer (&s, input_line_pointer, eol - input_line_pointer);
2731 input_line_pointer = eol;
2732
2733 if (line_label != NULL)
2734 {
2735 sb label;
2736 size_t len;
2737 const char *name;
2738
2739 name = S_GET_NAME (line_label);
2740 len = strlen (name);
2741 sb_build (&label, len);
2742 sb_add_buffer (&label, name, len);
2743 macro = define_macro (&s, &label, get_macro_line_sb);
2744 sb_kill (&label);
2745 }
2746 else
2747 macro = define_macro (&s, NULL, get_macro_line_sb);
2748 if (macro != NULL)
2749 {
2750 if (line_label != NULL)
2751 {
2752 S_SET_SEGMENT (line_label, absolute_section);
2753 S_SET_VALUE (line_label, 0);
2754 symbol_set_frag (line_label, &zero_address_frag);
2755 }
2756
2757 if (((NO_PSEUDO_DOT || flag_m68k_mri)
2758 && str_hash_find (po_hash, macro->name) != NULL)
2759 || (!flag_m68k_mri
2760 && macro->name[0] == '.'
2761 && str_hash_find (po_hash, macro->name + 1) != NULL))
2762 {
2763 as_warn_where (macro->file, macro->line,
2764 _("attempt to redefine pseudo-op `%s' ignored"),
2765 macro->name);
2766 str_hash_delete (macro_hash, macro->name);
2767 }
2768 }
2769
2770 sb_kill (&s);
2771 }
2772
2773 /* Handle the .mexit pseudo-op, which immediately exits a macro
2774 expansion. */
2775
2776 void
s_mexit(int ignore ATTRIBUTE_UNUSED)2777 s_mexit (int ignore ATTRIBUTE_UNUSED)
2778 {
2779 if (macro_nest)
2780 {
2781 cond_exit_macro (macro_nest);
2782 buffer_limit = input_scrub_next_buffer (&input_line_pointer);
2783 }
2784 else
2785 as_warn (_("ignoring macro exit outside a macro definition."));
2786 }
2787
2788 /* Switch in and out of MRI mode. */
2789
2790 void
s_mri(int ignore ATTRIBUTE_UNUSED)2791 s_mri (int ignore ATTRIBUTE_UNUSED)
2792 {
2793 int on;
2794 #ifdef MRI_MODE_CHANGE
2795 int old_flag;
2796 #endif
2797
2798 on = get_absolute_expression ();
2799 #ifdef MRI_MODE_CHANGE
2800 old_flag = flag_mri;
2801 #endif
2802 if (on != 0)
2803 {
2804 flag_mri = 1;
2805 #ifdef TC_M68K
2806 flag_m68k_mri = 1;
2807 #endif
2808 }
2809 else
2810 {
2811 flag_mri = 0;
2812 #ifdef TC_M68K
2813 flag_m68k_mri = 0;
2814 #endif
2815 }
2816
2817 /* Operator precedence changes in m68k MRI mode, so we need to
2818 update the operator rankings. */
2819 expr_set_precedence ();
2820
2821 #ifdef MRI_MODE_CHANGE
2822 if (on != old_flag)
2823 MRI_MODE_CHANGE (on);
2824 #endif
2825
2826 demand_empty_rest_of_line ();
2827 }
2828
2829 /* Handle changing the location counter. */
2830
2831 static void
do_org(segT segment,expressionS * exp,int fill)2832 do_org (segT segment, expressionS *exp, int fill)
2833 {
2834 if (segment != now_seg
2835 && segment != absolute_section
2836 && segment != expr_section)
2837 as_bad (_("invalid segment \"%s\""), segment_name (segment));
2838
2839 if (now_seg == absolute_section)
2840 {
2841 if (fill != 0)
2842 as_warn (_("ignoring fill value in absolute section"));
2843 if (exp->X_op != O_constant)
2844 {
2845 as_bad (_("only constant offsets supported in absolute section"));
2846 exp->X_add_number = 0;
2847 }
2848 abs_section_offset = exp->X_add_number;
2849 }
2850 else
2851 {
2852 char *p;
2853 symbolS *sym = exp->X_add_symbol;
2854 offsetT off = exp->X_add_number * OCTETS_PER_BYTE;
2855
2856 if (fill && in_bss ())
2857 as_warn (_("ignoring fill value in section `%s'"),
2858 segment_name (now_seg));
2859
2860 if (exp->X_op != O_constant && exp->X_op != O_symbol)
2861 {
2862 /* Handle complex expressions. */
2863 sym = make_expr_symbol (exp);
2864 off = 0;
2865 }
2866
2867 p = frag_var (rs_org, 1, 1, (relax_substateT) 0, sym, off, (char *) 0);
2868 *p = fill;
2869 }
2870 }
2871
2872 void
s_org(int ignore ATTRIBUTE_UNUSED)2873 s_org (int ignore ATTRIBUTE_UNUSED)
2874 {
2875 segT segment;
2876 expressionS exp;
2877 long temp_fill;
2878
2879 #ifdef md_flush_pending_output
2880 md_flush_pending_output ();
2881 #endif
2882
2883 /* The m68k MRI assembler has a different meaning for .org. It
2884 means to create an absolute section at a given address. We can't
2885 support that--use a linker script instead. */
2886 if (flag_m68k_mri)
2887 {
2888 as_bad (_("MRI style ORG pseudo-op not supported"));
2889 ignore_rest_of_line ();
2890 return;
2891 }
2892
2893 /* Don't believe the documentation of BSD 4.2 AS. There is no such
2894 thing as a sub-segment-relative origin. Any absolute origin is
2895 given a warning, then assumed to be segment-relative. Any
2896 segmented origin expression ("foo+42") had better be in the right
2897 segment or the .org is ignored.
2898
2899 BSD 4.2 AS warns if you try to .org backwards. We cannot because
2900 we never know sub-segment sizes when we are reading code. BSD
2901 will crash trying to emit negative numbers of filler bytes in
2902 certain .orgs. We don't crash, but see as-write for that code.
2903
2904 Don't make frag if need_pass_2==1. */
2905 segment = get_known_segmented_expression (&exp);
2906 if (*input_line_pointer == ',')
2907 {
2908 input_line_pointer++;
2909 temp_fill = get_absolute_expression ();
2910 }
2911 else
2912 temp_fill = 0;
2913
2914 if (!need_pass_2)
2915 do_org (segment, &exp, temp_fill);
2916
2917 demand_empty_rest_of_line ();
2918 }
2919
2920 /* Handle parsing for the MRI SECT/SECTION pseudo-op. This should be
2921 called by the obj-format routine which handles section changing
2922 when in MRI mode. It will create a new section, and return it. It
2923 will set *TYPE to the section type: one of 'C' (code), 'D' (data),
2924 'M' (mixed), or 'R' (romable). The flags will be set in the section. */
2925
2926 void
s_mri_sect(char * type ATTRIBUTE_UNUSED)2927 s_mri_sect (char *type ATTRIBUTE_UNUSED)
2928 {
2929 #ifdef TC_M68K
2930
2931 char *name;
2932 char c;
2933 segT seg;
2934
2935 SKIP_WHITESPACE ();
2936
2937 name = input_line_pointer;
2938 if (!ISDIGIT (*name))
2939 c = get_symbol_name (& name);
2940 else
2941 {
2942 do
2943 {
2944 ++input_line_pointer;
2945 }
2946 while (ISDIGIT (*input_line_pointer));
2947
2948 c = *input_line_pointer;
2949 *input_line_pointer = '\0';
2950 }
2951
2952 name = xstrdup (name);
2953
2954 c = restore_line_pointer (c);
2955
2956 seg = subseg_new (name, 0);
2957
2958 if (c == ',')
2959 {
2960 unsigned int align;
2961
2962 ++input_line_pointer;
2963 align = get_absolute_expression ();
2964 record_alignment (seg, align);
2965 }
2966
2967 *type = 'C';
2968 if (*input_line_pointer == ',')
2969 {
2970 c = *++input_line_pointer;
2971 c = TOUPPER (c);
2972 if (c == 'C' || c == 'D' || c == 'M' || c == 'R')
2973 *type = c;
2974 else
2975 as_bad (_("unrecognized section type"));
2976 ++input_line_pointer;
2977
2978 {
2979 flagword flags;
2980
2981 flags = SEC_NO_FLAGS;
2982 if (*type == 'C')
2983 flags = SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_CODE;
2984 else if (*type == 'D' || *type == 'M')
2985 flags = SEC_ALLOC | SEC_LOAD | SEC_DATA;
2986 else if (*type == 'R')
2987 flags = SEC_ALLOC | SEC_LOAD | SEC_DATA | SEC_READONLY | SEC_ROM;
2988 if (flags != SEC_NO_FLAGS)
2989 {
2990 if (!bfd_set_section_flags (seg, flags))
2991 as_warn (_("error setting flags for \"%s\": %s"),
2992 bfd_section_name (seg),
2993 bfd_errmsg (bfd_get_error ()));
2994 }
2995 }
2996 }
2997
2998 /* Ignore the HP type. */
2999 if (*input_line_pointer == ',')
3000 input_line_pointer += 2;
3001
3002 demand_empty_rest_of_line ();
3003
3004 #else /* ! TC_M68K */
3005 /* The MRI assembler seems to use different forms of .sect for
3006 different targets. */
3007 as_bad ("MRI mode not supported for this target");
3008 ignore_rest_of_line ();
3009 #endif /* ! TC_M68K */
3010 }
3011
3012 /* Handle the .print pseudo-op. */
3013
3014 void
s_print(int ignore ATTRIBUTE_UNUSED)3015 s_print (int ignore ATTRIBUTE_UNUSED)
3016 {
3017 char *s;
3018 int len;
3019
3020 s = demand_copy_C_string (&len);
3021 if (s != NULL)
3022 printf ("%s\n", s);
3023 demand_empty_rest_of_line ();
3024 }
3025
3026 /* Handle the .purgem pseudo-op. */
3027
3028 void
s_purgem(int ignore ATTRIBUTE_UNUSED)3029 s_purgem (int ignore ATTRIBUTE_UNUSED)
3030 {
3031 if (is_it_end_of_statement ())
3032 {
3033 demand_empty_rest_of_line ();
3034 return;
3035 }
3036
3037 do
3038 {
3039 char *name;
3040 char c;
3041
3042 SKIP_WHITESPACE ();
3043 c = get_symbol_name (& name);
3044 delete_macro (name);
3045 *input_line_pointer = c;
3046 SKIP_WHITESPACE_AFTER_NAME ();
3047 }
3048 while (*input_line_pointer++ == ',');
3049
3050 --input_line_pointer;
3051 demand_empty_rest_of_line ();
3052 }
3053
3054 /* Handle the .endm/.endr pseudo-ops. */
3055
3056 static void
s_bad_end(int endr)3057 s_bad_end (int endr)
3058 {
3059 as_warn (_(".end%c encountered without preceding %s"),
3060 endr ? 'r' : 'm',
3061 endr ? ".rept, .irp, or .irpc" : ".macro");
3062 demand_empty_rest_of_line ();
3063 }
3064
3065 /* Handle the .rept pseudo-op. */
3066
3067 void
s_rept(int ignore ATTRIBUTE_UNUSED)3068 s_rept (int ignore ATTRIBUTE_UNUSED)
3069 {
3070 size_t count;
3071
3072 count = (size_t) get_absolute_expression ();
3073
3074 do_repeat (count, "REPT", "ENDR", NULL);
3075 }
3076
3077 /* This function provides a generic repeat block implementation. It allows
3078 different directives to be used as the start/end keys. Any text matching
3079 the optional EXPANDER in the block is replaced by the remaining iteration
3080 count. */
3081
3082 void
do_repeat(size_t count,const char * start,const char * end,const char * expander)3083 do_repeat (size_t count, const char *start, const char *end,
3084 const char *expander)
3085 {
3086 sb one;
3087 sb many;
3088
3089 if (((ssize_t) count) < 0)
3090 {
3091 as_bad (_("negative count for %s - ignored"), start);
3092 count = 0;
3093 }
3094
3095 sb_new (&one);
3096 if (!buffer_and_nest (start, end, &one, get_non_macro_line_sb))
3097 {
3098 as_bad (_("%s without %s"), start, end);
3099 sb_kill (&one);
3100 return;
3101 }
3102
3103 if (expander == NULL || strstr (one.ptr, expander) == NULL)
3104 {
3105 sb_build (&many, count * one.len);
3106 while (count-- > 0)
3107 sb_add_sb (&many, &one);
3108 }
3109 else
3110 {
3111 sb_new (&many);
3112
3113 while (count -- > 0)
3114 {
3115 int len;
3116 char * sub;
3117 sb processed;
3118
3119 sb_build (& processed, one.len);
3120 sb_add_sb (& processed, & one);
3121 sub = strstr (processed.ptr, expander);
3122 len = sprintf (sub, "%lu", (unsigned long) count);
3123 gas_assert (len < 8);
3124 memmove (sub + len, sub + 8,
3125 processed.ptr + processed.len - (sub + 8));
3126 processed.len -= (8 - len);
3127 sb_add_sb (& many, & processed);
3128 sb_kill (& processed);
3129 }
3130 }
3131
3132 sb_kill (&one);
3133
3134 input_scrub_include_sb (&many, input_line_pointer, expanding_repeat);
3135 sb_kill (&many);
3136 buffer_limit = input_scrub_next_buffer (&input_line_pointer);
3137 }
3138
3139 /* Skip to end of current repeat loop; EXTRA indicates how many additional
3140 input buffers to skip. Assumes that conditionals preceding the loop end
3141 are properly nested.
3142
3143 This function makes it easier to implement a premature "break" out of the
3144 loop. The EXTRA arg accounts for other buffers we might have inserted,
3145 such as line substitutions. */
3146
3147 void
end_repeat(int extra)3148 end_repeat (int extra)
3149 {
3150 cond_exit_macro (macro_nest);
3151 while (extra-- >= 0)
3152 buffer_limit = input_scrub_next_buffer (&input_line_pointer);
3153 }
3154
3155 static void
assign_symbol(char * name,int mode)3156 assign_symbol (char *name, int mode)
3157 {
3158 symbolS *symbolP;
3159
3160 if (name[0] == '.' && name[1] == '\0')
3161 {
3162 /* Turn '. = mumble' into a .org mumble. */
3163 segT segment;
3164 expressionS exp;
3165
3166 segment = get_known_segmented_expression (&exp);
3167
3168 if (!need_pass_2)
3169 do_org (segment, &exp, 0);
3170
3171 return;
3172 }
3173
3174 if ((symbolP = symbol_find (name)) == NULL
3175 && (symbolP = md_undefined_symbol (name)) == NULL)
3176 {
3177 symbolP = symbol_find_or_make (name);
3178 #ifndef NO_LISTING
3179 /* When doing symbol listings, play games with dummy fragments living
3180 outside the normal fragment chain to record the file and line info
3181 for this symbol. */
3182 if (listing & LISTING_SYMBOLS)
3183 {
3184 extern struct list_info_struct *listing_tail;
3185 fragS *dummy_frag = notes_calloc (1, sizeof (*dummy_frag));
3186 dummy_frag->line = listing_tail;
3187 dummy_frag->fr_symbol = symbolP;
3188 symbol_set_frag (symbolP, dummy_frag);
3189 }
3190 #endif
3191 #if defined (OBJ_COFF) && !defined (TE_PE)
3192 /* "set" symbols are local unless otherwise specified. */
3193 SF_SET_LOCAL (symbolP);
3194 #endif
3195 }
3196
3197 if (S_IS_DEFINED (symbolP) || symbol_equated_p (symbolP))
3198 {
3199 if ((mode != 0 || !S_IS_VOLATILE (symbolP))
3200 && !S_CAN_BE_REDEFINED (symbolP))
3201 {
3202 as_bad (_("symbol `%s' is already defined"), name);
3203 ignore_rest_of_line ();
3204 input_line_pointer--;
3205 return;
3206 }
3207 /* If the symbol is volatile, copy the symbol and replace the
3208 original with the copy, so that previous uses of the symbol will
3209 retain the value of the symbol at the point of use. */
3210 else if (S_IS_VOLATILE (symbolP))
3211 symbolP = symbol_clone (symbolP, 1);
3212 }
3213
3214 if (mode == 0)
3215 S_SET_VOLATILE (symbolP);
3216 else if (mode < 0)
3217 S_SET_FORWARD_REF (symbolP);
3218
3219 pseudo_set (symbolP);
3220 }
3221
3222 /* Handle the .equ, .equiv, .eqv, and .set directives. If EQUIV is 1,
3223 then this is .equiv, and it is an error if the symbol is already
3224 defined. If EQUIV is -1, the symbol additionally is a forward
3225 reference. */
3226
3227 void
s_set(int equiv)3228 s_set (int equiv)
3229 {
3230 char *name;
3231
3232 /* Especial apologies for the random logic:
3233 this just grew, and could be parsed much more simply!
3234 Dean in haste. */
3235 if ((name = read_symbol_name ()) == NULL)
3236 return;
3237
3238 if (*input_line_pointer != ',')
3239 {
3240 as_bad (_("expected comma after \"%s\""), name);
3241 ignore_rest_of_line ();
3242 free (name);
3243 return;
3244 }
3245
3246 input_line_pointer++;
3247 assign_symbol (name, equiv);
3248 demand_empty_rest_of_line ();
3249 free (name);
3250 }
3251
3252 void
s_space(int mult)3253 s_space (int mult)
3254 {
3255 expressionS exp;
3256 expressionS val;
3257 char *p = 0;
3258 char *stop = NULL;
3259 char stopc = 0;
3260 int bytes;
3261
3262 #ifdef md_flush_pending_output
3263 md_flush_pending_output ();
3264 #endif
3265
3266 switch (mult)
3267 {
3268 case 'x':
3269 #ifdef X_PRECISION
3270 # ifndef P_PRECISION
3271 # define P_PRECISION X_PRECISION
3272 # define P_PRECISION_PAD X_PRECISION_PAD
3273 # endif
3274 mult = (X_PRECISION + X_PRECISION_PAD) * sizeof (LITTLENUM_TYPE);
3275 if (!mult)
3276 #endif
3277 mult = 12;
3278 break;
3279
3280 case 'p':
3281 #ifdef P_PRECISION
3282 mult = (P_PRECISION + P_PRECISION_PAD) * sizeof (LITTLENUM_TYPE);
3283 if (!mult)
3284 #endif
3285 mult = 12;
3286 break;
3287 }
3288
3289 #ifdef md_cons_align
3290 md_cons_align (1);
3291 #endif
3292
3293 if (flag_mri)
3294 stop = mri_comment_field (&stopc);
3295
3296 /* In m68k MRI mode, we need to align to a word boundary, unless
3297 this is ds.b. */
3298 if (flag_m68k_mri && mult > 1)
3299 {
3300 if (now_seg == absolute_section)
3301 {
3302 abs_section_offset += abs_section_offset & 1;
3303 if (line_label != NULL)
3304 S_SET_VALUE (line_label, abs_section_offset);
3305 }
3306 else if (mri_common_symbol != NULL)
3307 {
3308 valueT mri_val;
3309
3310 mri_val = S_GET_VALUE (mri_common_symbol);
3311 if ((mri_val & 1) != 0)
3312 {
3313 S_SET_VALUE (mri_common_symbol, mri_val + 1);
3314 if (line_label != NULL)
3315 {
3316 expressionS *symexp;
3317
3318 symexp = symbol_get_value_expression (line_label);
3319 know (symexp->X_op == O_symbol);
3320 know (symexp->X_add_symbol == mri_common_symbol);
3321 symexp->X_add_number += 1;
3322 }
3323 }
3324 }
3325 else
3326 {
3327 do_align (1, (char *) NULL, 0, 0);
3328 if (line_label != NULL)
3329 {
3330 symbol_set_frag (line_label, frag_now);
3331 S_SET_VALUE (line_label, frag_now_fix ());
3332 }
3333 }
3334 }
3335
3336 bytes = mult;
3337
3338 expression (&exp);
3339
3340 SKIP_WHITESPACE ();
3341 if (*input_line_pointer == ',')
3342 {
3343 ++input_line_pointer;
3344 expression (&val);
3345 }
3346 else
3347 {
3348 val.X_op = O_constant;
3349 val.X_add_number = 0;
3350 }
3351
3352 if ((val.X_op != O_constant
3353 || val.X_add_number < - 0x80
3354 || val.X_add_number > 0xff
3355 || (mult != 0 && mult != 1 && val.X_add_number != 0))
3356 && (now_seg != absolute_section && !in_bss ()))
3357 {
3358 resolve_expression (&exp);
3359 if (exp.X_op != O_constant)
3360 as_bad (_("unsupported variable size or fill value"));
3361 else
3362 {
3363 offsetT i;
3364
3365 /* PR 20901: Check for excessive values.
3366 FIXME: 1<<10 is an arbitrary limit. Maybe use maxpagesize instead ? */
3367 if (exp.X_add_number < 0 || exp.X_add_number > (1 << 10))
3368 as_bad (_("size value for space directive too large: %lx"),
3369 (long) exp.X_add_number);
3370 else
3371 {
3372 if (mult == 0)
3373 mult = 1;
3374 bytes = mult * exp.X_add_number;
3375
3376 for (i = 0; i < exp.X_add_number; i++)
3377 emit_expr (&val, mult);
3378 }
3379 }
3380 }
3381 else
3382 {
3383 if (now_seg == absolute_section || mri_common_symbol != NULL)
3384 resolve_expression (&exp);
3385
3386 if (exp.X_op == O_constant)
3387 {
3388 addressT repeat = exp.X_add_number;
3389 addressT total;
3390
3391 bytes = 0;
3392 if ((offsetT) repeat < 0)
3393 {
3394 as_warn (_(".space repeat count is negative, ignored"));
3395 goto getout;
3396 }
3397 if (repeat == 0)
3398 {
3399 if (!flag_mri)
3400 as_warn (_(".space repeat count is zero, ignored"));
3401 goto getout;
3402 }
3403 if ((unsigned int) mult <= 1)
3404 total = repeat;
3405 else if (gas_mul_overflow (repeat, mult, &total)
3406 || (offsetT) total < 0)
3407 {
3408 as_warn (_(".space repeat count overflow, ignored"));
3409 goto getout;
3410 }
3411 bytes = total;
3412
3413 /* If we are in the absolute section, just bump the offset. */
3414 if (now_seg == absolute_section)
3415 {
3416 if (val.X_op != O_constant || val.X_add_number != 0)
3417 as_warn (_("ignoring fill value in absolute section"));
3418 abs_section_offset += total;
3419 goto getout;
3420 }
3421
3422 /* If we are secretly in an MRI common section, then
3423 creating space just increases the size of the common
3424 symbol. */
3425 if (mri_common_symbol != NULL)
3426 {
3427 S_SET_VALUE (mri_common_symbol,
3428 S_GET_VALUE (mri_common_symbol) + total);
3429 goto getout;
3430 }
3431
3432 if (!need_pass_2)
3433 p = frag_var (rs_fill, 1, 1, (relax_substateT) 0, (symbolS *) 0,
3434 (offsetT) total, (char *) 0);
3435 }
3436 else
3437 {
3438 if (now_seg == absolute_section)
3439 {
3440 as_bad (_("space allocation too complex in absolute section"));
3441 subseg_set (text_section, 0);
3442 }
3443
3444 if (mri_common_symbol != NULL)
3445 {
3446 as_bad (_("space allocation too complex in common section"));
3447 mri_common_symbol = NULL;
3448 }
3449
3450 if (!need_pass_2)
3451 p = frag_var (rs_space, 1, 1, (relax_substateT) 0,
3452 make_expr_symbol (&exp), (offsetT) 0, (char *) 0);
3453 }
3454
3455 if ((val.X_op != O_constant || val.X_add_number != 0) && in_bss ())
3456 as_warn (_("ignoring fill value in section `%s'"),
3457 segment_name (now_seg));
3458 else if (p)
3459 *p = val.X_add_number;
3460 }
3461
3462 getout:
3463
3464 /* In MRI mode, after an odd number of bytes, we must align to an
3465 even word boundary, unless the next instruction is a dc.b, ds.b
3466 or dcb.b. */
3467 if (flag_mri && (bytes & 1) != 0)
3468 mri_pending_align = 1;
3469
3470 demand_empty_rest_of_line ();
3471
3472 if (flag_mri)
3473 mri_comment_end (stop, stopc);
3474 }
3475
3476 void
s_nop(int ignore ATTRIBUTE_UNUSED)3477 s_nop (int ignore ATTRIBUTE_UNUSED)
3478 {
3479 expressionS exp;
3480 fragS *start;
3481 addressT start_off;
3482 offsetT frag_off;
3483
3484 #ifdef md_flush_pending_output
3485 md_flush_pending_output ();
3486 #endif
3487
3488 SKIP_WHITESPACE ();
3489 expression (&exp);
3490 demand_empty_rest_of_line ();
3491
3492 start = frag_now;
3493 start_off = frag_now_fix ();
3494 do
3495 {
3496 #ifdef md_emit_single_noop
3497 md_emit_single_noop;
3498 #else
3499 char *nop;
3500
3501 #ifndef md_single_noop_insn
3502 #define md_single_noop_insn "nop"
3503 #endif
3504 /* md_assemble might modify its argument, so
3505 we must pass it a string that is writable. */
3506 if (asprintf (&nop, "%s", md_single_noop_insn) < 0)
3507 as_fatal ("%s", xstrerror (errno));
3508
3509 /* Some targets assume that they can update input_line_pointer
3510 inside md_assemble, and, worse, that they can leave it
3511 assigned to the string pointer that was provided as an
3512 argument. So preserve ilp here. */
3513 char *saved_ilp = input_line_pointer;
3514 md_assemble (nop);
3515 input_line_pointer = saved_ilp;
3516 free (nop);
3517 #endif
3518 #ifdef md_flush_pending_output
3519 md_flush_pending_output ();
3520 #endif
3521 } while (exp.X_op == O_constant
3522 && exp.X_add_number > 0
3523 && frag_offset_ignore_align_p (start, frag_now, &frag_off)
3524 && frag_off + frag_now_fix () < start_off + exp.X_add_number);
3525 }
3526
3527 void
s_nops(int ignore ATTRIBUTE_UNUSED)3528 s_nops (int ignore ATTRIBUTE_UNUSED)
3529 {
3530 expressionS exp;
3531 expressionS val;
3532
3533 #ifdef md_flush_pending_output
3534 md_flush_pending_output ();
3535 #endif
3536
3537 SKIP_WHITESPACE ();
3538 expression (&exp);
3539 /* Note - this expression is tested for an absolute value in
3540 write.c:relax_segment(). */
3541
3542 SKIP_WHITESPACE ();
3543 if (*input_line_pointer == ',')
3544 {
3545 ++input_line_pointer;
3546 expression (&val);
3547 }
3548 else
3549 {
3550 val.X_op = O_constant;
3551 val.X_add_number = 0;
3552 }
3553
3554 if (val.X_op != O_constant)
3555 {
3556 as_bad (_("unsupported variable nop control in .nops directive"));
3557 val.X_op = O_constant;
3558 val.X_add_number = 0;
3559 }
3560 else if (val.X_add_number < 0)
3561 {
3562 as_warn (_("negative nop control byte, ignored"));
3563 val.X_add_number = 0;
3564 }
3565
3566 demand_empty_rest_of_line ();
3567
3568 if (need_pass_2)
3569 /* Ignore this directive if we are going to perform a second pass. */
3570 return;
3571
3572 /* Store the no-op instruction control byte in the first byte of frag. */
3573 char *p;
3574 symbolS *sym = make_expr_symbol (&exp);
3575 p = frag_var (rs_space_nop, 1, 1, (relax_substateT) 0,
3576 sym, (offsetT) 0, (char *) 0);
3577 *p = val.X_add_number;
3578 }
3579
3580 /* Obtain the size of a floating point number, given a type. */
3581
3582 static int
float_length(int float_type,int * pad_p)3583 float_length (int float_type, int *pad_p)
3584 {
3585 int length, pad = 0;
3586
3587 switch (float_type)
3588 {
3589 case 'b':
3590 case 'B':
3591 case 'h':
3592 case 'H':
3593 length = 2;
3594 break;
3595
3596 case 'f':
3597 case 'F':
3598 case 's':
3599 case 'S':
3600 length = 4;
3601 break;
3602
3603 case 'd':
3604 case 'D':
3605 case 'r':
3606 case 'R':
3607 length = 8;
3608 break;
3609
3610 case 'x':
3611 case 'X':
3612 #ifdef X_PRECISION
3613 length = X_PRECISION * sizeof (LITTLENUM_TYPE);
3614 pad = X_PRECISION_PAD * sizeof (LITTLENUM_TYPE);
3615 if (!length)
3616 #endif
3617 length = 12;
3618 break;
3619
3620 case 'p':
3621 case 'P':
3622 #ifdef P_PRECISION
3623 length = P_PRECISION * sizeof (LITTLENUM_TYPE);
3624 pad = P_PRECISION_PAD * sizeof (LITTLENUM_TYPE);
3625 if (!length)
3626 #endif
3627 length = 12;
3628 break;
3629
3630 default:
3631 as_bad (_("unknown floating type '%c'"), float_type);
3632 length = -1;
3633 break;
3634 }
3635
3636 if (pad_p)
3637 *pad_p = pad;
3638
3639 return length;
3640 }
3641
3642 static int
parse_one_float(int float_type,char temp[MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT])3643 parse_one_float (int float_type, char temp[MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT])
3644 {
3645 int length;
3646
3647 SKIP_WHITESPACE ();
3648
3649 /* Skip any 0{letter} that may be present. Don't even check if the
3650 letter is legal. Someone may invent a "z" format and this routine
3651 has no use for such information. Lusers beware: you get
3652 diagnostics if your input is ill-conditioned. */
3653 if (input_line_pointer[0] == '0'
3654 && ISALPHA (input_line_pointer[1]))
3655 input_line_pointer += 2;
3656
3657 /* Accept :xxxx, where the x's are hex digits, for a floating point
3658 with the exact digits specified. */
3659 if (input_line_pointer[0] == ':')
3660 {
3661 ++input_line_pointer;
3662 length = hex_float (float_type, temp);
3663 if (length < 0)
3664 {
3665 ignore_rest_of_line ();
3666 return length;
3667 }
3668 }
3669 else
3670 {
3671 const char *err;
3672
3673 err = md_atof (float_type, temp, &length);
3674 know (length <= MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT);
3675 know (err != NULL || length > 0);
3676 if (err)
3677 {
3678 as_bad (_("bad floating literal: %s"), err);
3679 ignore_rest_of_line ();
3680 return -1;
3681 }
3682 }
3683
3684 return length;
3685 }
3686
3687 /* This is like s_space, but the value is a floating point number with
3688 the given precision. This is for the MRI dcb.s pseudo-op and
3689 friends. */
3690
3691 void
s_float_space(int float_type)3692 s_float_space (int float_type)
3693 {
3694 offsetT count;
3695 int flen;
3696 char temp[MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT];
3697 char *stop = NULL;
3698 char stopc = 0;
3699
3700 #ifdef md_cons_align
3701 md_cons_align (1);
3702 #endif
3703
3704 if (flag_mri)
3705 stop = mri_comment_field (&stopc);
3706
3707 count = get_absolute_expression ();
3708
3709 SKIP_WHITESPACE ();
3710 if (*input_line_pointer != ',')
3711 {
3712 int pad;
3713
3714 flen = float_length (float_type, &pad);
3715 if (flen >= 0)
3716 memset (temp, 0, flen += pad);
3717 }
3718 else
3719 {
3720 ++input_line_pointer;
3721
3722 flen = parse_one_float (float_type, temp);
3723 }
3724
3725 if (flen < 0)
3726 {
3727 if (flag_mri)
3728 mri_comment_end (stop, stopc);
3729 return;
3730 }
3731
3732 while (--count >= 0)
3733 {
3734 char *p;
3735
3736 p = frag_more (flen);
3737 memcpy (p, temp, (unsigned int) flen);
3738 }
3739
3740 demand_empty_rest_of_line ();
3741
3742 if (flag_mri)
3743 mri_comment_end (stop, stopc);
3744 }
3745
3746 /* Handle the .struct pseudo-op, as found in MIPS assemblers. */
3747
3748 void
s_struct(int ignore ATTRIBUTE_UNUSED)3749 s_struct (int ignore ATTRIBUTE_UNUSED)
3750 {
3751 char *stop = NULL;
3752 char stopc = 0;
3753
3754 if (flag_mri)
3755 stop = mri_comment_field (&stopc);
3756 abs_section_offset = get_absolute_expression ();
3757 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
3758 /* The ELF backend needs to know that we are changing sections, so
3759 that .previous works correctly. */
3760 if (IS_ELF)
3761 obj_elf_section_change_hook ();
3762 #endif
3763 subseg_set (absolute_section, 0);
3764 demand_empty_rest_of_line ();
3765 if (flag_mri)
3766 mri_comment_end (stop, stopc);
3767 }
3768
3769 void
s_text(int ignore ATTRIBUTE_UNUSED)3770 s_text (int ignore ATTRIBUTE_UNUSED)
3771 {
3772 int temp;
3773
3774 temp = get_absolute_expression ();
3775 subseg_set (text_section, (subsegT) temp);
3776 demand_empty_rest_of_line ();
3777 }
3778
3779 /* .weakref x, y sets x as an alias to y that, as long as y is not
3780 referenced directly, will cause y to become a weak symbol. */
3781 void
s_weakref(int ignore ATTRIBUTE_UNUSED)3782 s_weakref (int ignore ATTRIBUTE_UNUSED)
3783 {
3784 char *name;
3785 symbolS *symbolP;
3786 symbolS *symbolP2;
3787 expressionS exp;
3788
3789 if ((name = read_symbol_name ()) == NULL)
3790 return;
3791
3792 symbolP = symbol_find_or_make (name);
3793
3794 if (S_IS_DEFINED (symbolP) || symbol_equated_p (symbolP))
3795 {
3796 if (!S_IS_VOLATILE (symbolP))
3797 {
3798 as_bad (_("symbol `%s' is already defined"), name);
3799 goto err_out;
3800 }
3801 symbolP = symbol_clone (symbolP, 1);
3802 S_CLEAR_VOLATILE (symbolP);
3803 }
3804
3805 SKIP_WHITESPACE ();
3806
3807 if (*input_line_pointer != ',')
3808 {
3809 as_bad (_("expected comma after \"%s\""), name);
3810 goto err_out;
3811 }
3812
3813 input_line_pointer++;
3814
3815 SKIP_WHITESPACE ();
3816 free (name);
3817
3818 if ((name = read_symbol_name ()) == NULL)
3819 return;
3820
3821 if ((symbolP2 = symbol_find_noref (name, 1)) == NULL
3822 && (symbolP2 = md_undefined_symbol (name)) == NULL)
3823 {
3824 symbolP2 = symbol_find_or_make (name);
3825 S_SET_WEAKREFD (symbolP2);
3826 }
3827 else
3828 {
3829 symbolS *symp = symbolP2;
3830
3831 while (S_IS_WEAKREFR (symp) && symp != symbolP)
3832 {
3833 expressionS *expP = symbol_get_value_expression (symp);
3834
3835 gas_assert (expP->X_op == O_symbol
3836 && expP->X_add_number == 0);
3837 symp = expP->X_add_symbol;
3838 }
3839 if (symp == symbolP)
3840 {
3841 char *loop;
3842
3843 loop = concat (S_GET_NAME (symbolP),
3844 " => ", S_GET_NAME (symbolP2), (const char *) NULL);
3845
3846 symp = symbolP2;
3847 while (symp != symbolP)
3848 {
3849 char *old_loop = loop;
3850
3851 symp = symbol_get_value_expression (symp)->X_add_symbol;
3852 loop = concat (loop, " => ", S_GET_NAME (symp),
3853 (const char *) NULL);
3854 free (old_loop);
3855 }
3856
3857 as_bad (_("%s: would close weakref loop: %s"),
3858 S_GET_NAME (symbolP), loop);
3859
3860 free (loop);
3861 free (name);
3862 ignore_rest_of_line ();
3863 return;
3864 }
3865
3866 /* Short-circuiting instead of just checking here might speed
3867 things up a tiny little bit, but loop error messages would
3868 miss intermediate links. */
3869 /* symbolP2 = symp; */
3870 }
3871
3872 memset (&exp, 0, sizeof (exp));
3873 exp.X_op = O_symbol;
3874 exp.X_add_symbol = symbolP2;
3875
3876 S_SET_SEGMENT (symbolP, undefined_section);
3877 symbol_set_value_expression (symbolP, &exp);
3878 symbol_set_frag (symbolP, &zero_address_frag);
3879 S_SET_WEAKREFR (symbolP);
3880
3881 demand_empty_rest_of_line ();
3882 free (name);
3883 return;
3884
3885 err_out:
3886 ignore_rest_of_line ();
3887 free (name);
3888 return;
3889 }
3890
3891
3892 /* Verify that we are at the end of a line. If not, issue an error and
3893 skip to EOL. This function may leave input_line_pointer one past
3894 buffer_limit, so should not be called from places that may
3895 dereference input_line_pointer unconditionally. Note that when the
3896 gas parser is switched to handling a string (where buffer_limit
3897 should be the size of the string excluding the NUL terminator) this
3898 will be one past the NUL; is_end_of_line(0) returns true. */
3899
3900 void
demand_empty_rest_of_line(void)3901 demand_empty_rest_of_line (void)
3902 {
3903 SKIP_WHITESPACE ();
3904 if (input_line_pointer > buffer_limit)
3905 return;
3906 if (is_end_of_line[(unsigned char) *input_line_pointer])
3907 input_line_pointer++;
3908 else
3909 {
3910 if (ISPRINT (*input_line_pointer))
3911 as_bad (_("junk at end of line, first unrecognized character is `%c'"),
3912 *input_line_pointer);
3913 else
3914 as_bad (_("junk at end of line, first unrecognized character valued 0x%x"),
3915 *input_line_pointer);
3916 ignore_rest_of_line ();
3917 }
3918 /* Return pointing just after end-of-line. */
3919 }
3920
3921 /* Silently advance to the end of line. Use this after already having
3922 issued an error about something bad. Like demand_empty_rest_of_line,
3923 this function may leave input_line_pointer one after buffer_limit;
3924 Don't call it from within expression parsing code in an attempt to
3925 silence further errors. */
3926
3927 void
ignore_rest_of_line(void)3928 ignore_rest_of_line (void)
3929 {
3930 while (input_line_pointer <= buffer_limit)
3931 if (is_end_of_line[(unsigned char) *input_line_pointer++])
3932 break;
3933 /* Return pointing just after end-of-line. */
3934 }
3935
3936 /* Sets frag for given symbol to zero_address_frag, except when the
3937 symbol frag is already set to a dummy listing frag. */
3938
3939 static void
set_zero_frag(symbolS * symbolP)3940 set_zero_frag (symbolS *symbolP)
3941 {
3942 if (symbol_get_frag (symbolP)->fr_type != rs_dummy)
3943 symbol_set_frag (symbolP, &zero_address_frag);
3944 }
3945
3946 /* In: Pointer to a symbol.
3947 Input_line_pointer->expression.
3948
3949 Out: Input_line_pointer->just after any whitespace after expression.
3950 Tried to set symbol to value of expression.
3951 Will change symbols type, value, and frag; */
3952
3953 void
pseudo_set(symbolS * symbolP)3954 pseudo_set (symbolS *symbolP)
3955 {
3956 expressionS exp;
3957 segT seg;
3958
3959 know (symbolP); /* NULL pointer is logic error. */
3960
3961 if (!S_IS_FORWARD_REF (symbolP))
3962 (void) expression (&exp);
3963 else
3964 (void) deferred_expression (&exp);
3965
3966 if (exp.X_op == O_illegal)
3967 as_bad (_("illegal expression"));
3968 else if (exp.X_op == O_absent)
3969 as_bad (_("missing expression"));
3970 else if (exp.X_op == O_big)
3971 {
3972 if (exp.X_add_number > 0)
3973 as_bad (_("bignum invalid"));
3974 else
3975 as_bad (_("floating point number invalid"));
3976 }
3977 else if (exp.X_op == O_subtract
3978 && !S_IS_FORWARD_REF (symbolP)
3979 && SEG_NORMAL (S_GET_SEGMENT (exp.X_add_symbol))
3980 && (symbol_get_frag (exp.X_add_symbol)
3981 == symbol_get_frag (exp.X_op_symbol)))
3982 {
3983 exp.X_op = O_constant;
3984 exp.X_add_number = (S_GET_VALUE (exp.X_add_symbol)
3985 - S_GET_VALUE (exp.X_op_symbol));
3986 }
3987
3988 if (symbol_section_p (symbolP))
3989 {
3990 as_bad ("attempt to set value of section symbol");
3991 return;
3992 }
3993
3994 switch (exp.X_op)
3995 {
3996 case O_illegal:
3997 case O_absent:
3998 case O_big:
3999 exp.X_add_number = 0;
4000 /* Fall through. */
4001 case O_constant:
4002 S_SET_SEGMENT (symbolP, absolute_section);
4003 S_SET_VALUE (symbolP, (valueT) exp.X_add_number);
4004 set_zero_frag (symbolP);
4005 break;
4006
4007 case O_register:
4008 #ifndef TC_GLOBAL_REGISTER_SYMBOL_OK
4009 if (S_IS_EXTERNAL (symbolP))
4010 {
4011 as_bad ("can't equate global symbol `%s' with register name",
4012 S_GET_NAME (symbolP));
4013 return;
4014 }
4015 #endif
4016 /* Make sure symbol_equated_p() recognizes the symbol as an equate. */
4017 exp.X_add_symbol = make_expr_symbol (&exp);
4018 exp.X_add_number = 0;
4019 exp.X_op = O_symbol;
4020 symbol_set_value_expression (symbolP, &exp);
4021 S_SET_SEGMENT (symbolP, reg_section);
4022 set_zero_frag (symbolP);
4023 break;
4024
4025 case O_symbol:
4026 seg = S_GET_SEGMENT (exp.X_add_symbol);
4027 /* For x=undef+const, create an expression symbol.
4028 For x=x+const, just update x except when x is an undefined symbol
4029 For x=defined+const, evaluate x. */
4030 if (symbolP == exp.X_add_symbol
4031 && (seg != undefined_section
4032 || !symbol_constant_p (symbolP)))
4033 {
4034 *symbol_X_add_number (symbolP) += exp.X_add_number;
4035 break;
4036 }
4037 else if (!S_IS_FORWARD_REF (symbolP) && seg != undefined_section)
4038 {
4039 symbolS *s = exp.X_add_symbol;
4040
4041 if (S_IS_COMMON (s))
4042 as_bad (_("`%s' can't be equated to common symbol `%s'"),
4043 S_GET_NAME (symbolP), S_GET_NAME (s));
4044
4045 S_SET_SEGMENT (symbolP, seg);
4046 S_SET_VALUE (symbolP, exp.X_add_number + S_GET_VALUE (s));
4047 symbol_set_frag (symbolP, symbol_get_frag (s));
4048 copy_symbol_attributes (symbolP, s);
4049 break;
4050 }
4051 S_SET_SEGMENT (symbolP, undefined_section);
4052 symbol_set_value_expression (symbolP, &exp);
4053 copy_symbol_attributes (symbolP, exp.X_add_symbol);
4054 set_zero_frag (symbolP);
4055 break;
4056
4057 default:
4058 /* The value is some complex expression. */
4059 S_SET_SEGMENT (symbolP, expr_section);
4060 symbol_set_value_expression (symbolP, &exp);
4061 set_zero_frag (symbolP);
4062 break;
4063 }
4064 }
4065
4066 /* cons()
4067
4068 CONStruct more frag of .bytes, or .words etc.
4069 Should need_pass_2 be 1 then emit no frag(s).
4070 This understands EXPRESSIONS.
4071
4072 Bug (?)
4073
4074 This has a split personality. We use expression() to read the
4075 value. We can detect if the value won't fit in a byte or word.
4076 But we can't detect if expression() discarded significant digits
4077 in the case of a long. Not worth the crocks required to fix it. */
4078
4079 /* Select a parser for cons expressions. */
4080
4081 /* Some targets need to parse the expression in various fancy ways.
4082 You can define TC_PARSE_CONS_EXPRESSION to do whatever you like
4083 (for example, the HPPA does this). Otherwise, you can define
4084 REPEAT_CONS_EXPRESSIONS to permit repeat counts. If none of these
4085 are defined, which is the normal case, then only simple expressions
4086 are permitted. */
4087
4088 #ifdef TC_M68K
4089 static void
4090 parse_mri_cons (expressionS *exp, unsigned int nbytes);
4091 #endif
4092
4093 #ifndef TC_PARSE_CONS_EXPRESSION
4094 #ifdef REPEAT_CONS_EXPRESSIONS
4095 #define TC_PARSE_CONS_EXPRESSION(EXP, NBYTES) \
4096 (parse_repeat_cons (EXP, NBYTES), TC_PARSE_CONS_RETURN_NONE)
4097 static void
4098 parse_repeat_cons (expressionS *exp, unsigned int nbytes);
4099 #endif
4100
4101 /* If we haven't gotten one yet, just call expression. */
4102 #ifndef TC_PARSE_CONS_EXPRESSION
4103 #define TC_PARSE_CONS_EXPRESSION(EXP, NBYTES) \
4104 (expression (EXP), TC_PARSE_CONS_RETURN_NONE)
4105 #endif
4106 #endif
4107
4108 void
do_parse_cons_expression(expressionS * exp,int nbytes ATTRIBUTE_UNUSED)4109 do_parse_cons_expression (expressionS *exp,
4110 int nbytes ATTRIBUTE_UNUSED)
4111 {
4112 (void) TC_PARSE_CONS_EXPRESSION (exp, nbytes);
4113 }
4114
4115
4116 /* Worker to do .byte etc statements.
4117 Clobbers input_line_pointer and checks end-of-line. */
4118
4119 static void
cons_worker(int nbytes,int rva)4120 cons_worker (int nbytes, /* 1=.byte, 2=.word, 4=.long. */
4121 int rva)
4122 {
4123 int c;
4124 expressionS exp;
4125 char *stop = NULL;
4126 char stopc = 0;
4127
4128 #ifdef md_flush_pending_output
4129 md_flush_pending_output ();
4130 #endif
4131
4132 if (flag_mri)
4133 stop = mri_comment_field (&stopc);
4134
4135 if (is_it_end_of_statement ())
4136 {
4137 demand_empty_rest_of_line ();
4138 if (flag_mri)
4139 mri_comment_end (stop, stopc);
4140 return;
4141 }
4142
4143 if (nbytes == 0)
4144 nbytes = TC_ADDRESS_BYTES ();
4145
4146 #ifdef md_cons_align
4147 md_cons_align (nbytes);
4148 #endif
4149
4150 c = 0;
4151 do
4152 {
4153 TC_PARSE_CONS_RETURN_TYPE ret = TC_PARSE_CONS_RETURN_NONE;
4154 #ifdef TC_CONS_FIX_CHECK
4155 fixS **cur_fix = &frchain_now->fix_tail;
4156
4157 if (*cur_fix != NULL)
4158 cur_fix = &(*cur_fix)->fx_next;
4159 #endif
4160
4161 #ifdef TC_M68K
4162 if (flag_m68k_mri)
4163 parse_mri_cons (&exp, (unsigned int) nbytes);
4164 else
4165 #endif
4166 {
4167 #if 0
4168 if (*input_line_pointer == '"')
4169 {
4170 as_bad (_("unexpected `\"' in expression"));
4171 ignore_rest_of_line ();
4172 return;
4173 }
4174 #endif
4175 ret = TC_PARSE_CONS_EXPRESSION (&exp, (unsigned int) nbytes);
4176 }
4177
4178 if (rva)
4179 {
4180 if (exp.X_op == O_symbol)
4181 exp.X_op = O_symbol_rva;
4182 else
4183 as_fatal (_("rva without symbol"));
4184 }
4185 emit_expr_with_reloc (&exp, (unsigned int) nbytes, ret);
4186 #ifdef TC_CONS_FIX_CHECK
4187 TC_CONS_FIX_CHECK (&exp, nbytes, *cur_fix);
4188 #endif
4189 ++c;
4190 }
4191 while (*input_line_pointer++ == ',');
4192
4193 /* In MRI mode, after an odd number of bytes, we must align to an
4194 even word boundary, unless the next instruction is a dc.b, ds.b
4195 or dcb.b. */
4196 if (flag_mri && nbytes == 1 && (c & 1) != 0)
4197 mri_pending_align = 1;
4198
4199 input_line_pointer--; /* Put terminator back into stream. */
4200
4201 demand_empty_rest_of_line ();
4202
4203 if (flag_mri)
4204 mri_comment_end (stop, stopc);
4205
4206 /* Disallow hand-crafting instructions using .byte. FIXME - what about
4207 .word, .long etc ? */
4208 if (flag_synth_cfi && frchain_now && frchain_now->frch_ginsn_data
4209 && nbytes == 1)
4210 as_bad (_("SCFI: hand-crafting instructions not supported"));
4211 }
4212
4213 void
cons(int size)4214 cons (int size)
4215 {
4216 cons_worker (size, 0);
4217 }
4218
4219 void
s_rva(int size)4220 s_rva (int size)
4221 {
4222 cons_worker (size, 1);
4223 }
4224
4225 /* .reloc offset, reloc_name, symbol+addend. */
4226
4227 static void
s_reloc(int ignore ATTRIBUTE_UNUSED)4228 s_reloc (int ignore ATTRIBUTE_UNUSED)
4229 {
4230 char *stop = NULL;
4231 char stopc = 0;
4232 expressionS exp;
4233 char *r_name;
4234 int c;
4235 struct reloc_list *reloc;
4236 struct _bfd_rel { const char * name; bfd_reloc_code_real_type code; };
4237 static const struct _bfd_rel bfd_relocs[] =
4238 {
4239 { "NONE", BFD_RELOC_NONE },
4240 { "8", BFD_RELOC_8 },
4241 { "16", BFD_RELOC_16 },
4242 { "32", BFD_RELOC_32 },
4243 { "64", BFD_RELOC_64 }
4244 };
4245
4246 reloc = XNEW (struct reloc_list);
4247
4248 if (flag_mri)
4249 stop = mri_comment_field (&stopc);
4250
4251 expression (&exp);
4252 switch (exp.X_op)
4253 {
4254 case O_illegal:
4255 case O_absent:
4256 case O_big:
4257 case O_register:
4258 as_bad (_("missing or bad offset expression"));
4259 goto err_out;
4260 case O_constant:
4261 exp.X_add_symbol = section_symbol (now_seg);
4262 /* Mark the section symbol used in relocation so that it will be
4263 included in the symbol table. */
4264 symbol_mark_used_in_reloc (exp.X_add_symbol);
4265 exp.X_op = O_symbol;
4266 /* Fallthru */
4267 case O_symbol:
4268 if (exp.X_add_number == 0)
4269 {
4270 reloc->u.a.offset_sym = exp.X_add_symbol;
4271 break;
4272 }
4273 /* Fallthru */
4274 default:
4275 reloc->u.a.offset_sym = make_expr_symbol (&exp);
4276 break;
4277 }
4278
4279 SKIP_WHITESPACE ();
4280 if (*input_line_pointer != ',')
4281 {
4282 as_bad (_("missing reloc type"));
4283 goto err_out;
4284 }
4285
4286 ++input_line_pointer;
4287 SKIP_WHITESPACE ();
4288 c = get_symbol_name (& r_name);
4289 if (strncasecmp (r_name, "BFD_RELOC_", 10) == 0)
4290 {
4291 unsigned int i;
4292
4293 for (reloc->u.a.howto = NULL, i = 0; i < ARRAY_SIZE (bfd_relocs); i++)
4294 if (strcasecmp (r_name + 10, bfd_relocs[i].name) == 0)
4295 {
4296 reloc->u.a.howto = bfd_reloc_type_lookup (stdoutput,
4297 bfd_relocs[i].code);
4298 break;
4299 }
4300 }
4301 else
4302 reloc->u.a.howto = bfd_reloc_name_lookup (stdoutput, r_name);
4303 *input_line_pointer = c;
4304 if (reloc->u.a.howto == NULL)
4305 {
4306 as_bad (_("unrecognized reloc type"));
4307 goto err_out;
4308 }
4309
4310 exp.X_op = O_absent;
4311 SKIP_WHITESPACE_AFTER_NAME ();
4312 if (*input_line_pointer == ',')
4313 {
4314 ++input_line_pointer;
4315 expression (&exp);
4316 }
4317 switch (exp.X_op)
4318 {
4319 case O_illegal:
4320 case O_big:
4321 case O_register:
4322 as_bad (_("bad reloc expression"));
4323 err_out:
4324 ignore_rest_of_line ();
4325 free (reloc);
4326 if (flag_mri)
4327 mri_comment_end (stop, stopc);
4328 return;
4329 case O_absent:
4330 reloc->u.a.sym = NULL;
4331 reloc->u.a.addend = 0;
4332 break;
4333 case O_constant:
4334 reloc->u.a.sym = NULL;
4335 reloc->u.a.addend = exp.X_add_number;
4336 break;
4337 case O_symbol:
4338 reloc->u.a.sym = exp.X_add_symbol;
4339 reloc->u.a.addend = exp.X_add_number;
4340 break;
4341 default:
4342 reloc->u.a.sym = make_expr_symbol (&exp);
4343 reloc->u.a.addend = 0;
4344 break;
4345 }
4346
4347 reloc->file = as_where (&reloc->line);
4348 reloc->next = reloc_list;
4349 reloc_list = reloc;
4350
4351 demand_empty_rest_of_line ();
4352 if (flag_mri)
4353 mri_comment_end (stop, stopc);
4354 }
4355
4356 /* Put the contents of expression EXP into the object file using
4357 NBYTES bytes. If need_pass_2 is 1, this does nothing. */
4358
4359 void
emit_expr(expressionS * exp,unsigned int nbytes)4360 emit_expr (expressionS *exp, unsigned int nbytes)
4361 {
4362 emit_expr_with_reloc (exp, nbytes, TC_PARSE_CONS_RETURN_NONE);
4363 }
4364
4365 void
emit_expr_with_reloc(expressionS * exp,unsigned int nbytes,TC_PARSE_CONS_RETURN_TYPE reloc)4366 emit_expr_with_reloc (expressionS *exp,
4367 unsigned int nbytes,
4368 TC_PARSE_CONS_RETURN_TYPE reloc)
4369 {
4370 operatorT op;
4371 char *p;
4372 valueT extra_digit = 0;
4373
4374 /* Don't do anything if we are going to make another pass. */
4375 if (need_pass_2)
4376 return;
4377
4378 frag_grow (nbytes);
4379 dot_value = frag_now_fix ();
4380 dot_frag = frag_now;
4381
4382 #ifndef NO_LISTING
4383 #ifdef OBJ_ELF
4384 /* When gcc emits DWARF 1 debugging pseudo-ops, a line number will
4385 appear as a four byte positive constant in the .line section,
4386 followed by a 2 byte 0xffff. Look for that case here. */
4387 if (strcmp (segment_name (now_seg), ".line") != 0)
4388 dwarf_line = -1;
4389 else if (dwarf_line >= 0
4390 && nbytes == 2
4391 && exp->X_op == O_constant
4392 && (exp->X_add_number == -1 || exp->X_add_number == 0xffff))
4393 listing_source_line ((unsigned int) dwarf_line);
4394 else if (nbytes == 4
4395 && exp->X_op == O_constant
4396 && exp->X_add_number >= 0)
4397 dwarf_line = exp->X_add_number;
4398 else
4399 dwarf_line = -1;
4400
4401 /* When gcc emits DWARF 1 debugging pseudo-ops, a file name will
4402 appear as a 2 byte TAG_compile_unit (0x11) followed by a 2 byte
4403 AT_sibling (0x12) followed by a four byte address of the sibling
4404 followed by a 2 byte AT_name (0x38) followed by the name of the
4405 file. We look for that case here. */
4406 if (strcmp (segment_name (now_seg), ".debug") != 0)
4407 dwarf_file = 0;
4408 else if (dwarf_file == 0
4409 && nbytes == 2
4410 && exp->X_op == O_constant
4411 && exp->X_add_number == 0x11)
4412 dwarf_file = 1;
4413 else if (dwarf_file == 1
4414 && nbytes == 2
4415 && exp->X_op == O_constant
4416 && exp->X_add_number == 0x12)
4417 dwarf_file = 2;
4418 else if (dwarf_file == 2
4419 && nbytes == 4)
4420 dwarf_file = 3;
4421 else if (dwarf_file == 3
4422 && nbytes == 2
4423 && exp->X_op == O_constant
4424 && exp->X_add_number == 0x38)
4425 dwarf_file = 4;
4426 else
4427 dwarf_file = 0;
4428
4429 /* The variable dwarf_file_string tells stringer that the string
4430 may be the name of the source file. */
4431 if (dwarf_file == 4)
4432 dwarf_file_string = 1;
4433 else
4434 dwarf_file_string = 0;
4435 #endif
4436 #endif
4437
4438 if (check_eh_frame (exp, &nbytes))
4439 return;
4440
4441 op = exp->X_op;
4442
4443 /* Handle a negative bignum. */
4444 if (op == O_uminus
4445 && exp->X_add_number == 0
4446 && symbol_get_value_expression (exp->X_add_symbol)->X_op == O_big
4447 && symbol_get_value_expression (exp->X_add_symbol)->X_add_number > 0)
4448 {
4449 int i;
4450 unsigned long carry;
4451
4452 exp = symbol_get_value_expression (exp->X_add_symbol);
4453
4454 /* Negate the bignum: one's complement each digit and add 1. */
4455 carry = 1;
4456 for (i = 0; i < exp->X_add_number; i++)
4457 {
4458 unsigned long next;
4459
4460 next = (((~(generic_bignum[i] & LITTLENUM_MASK))
4461 & LITTLENUM_MASK)
4462 + carry);
4463 generic_bignum[i] = next & LITTLENUM_MASK;
4464 carry = next >> LITTLENUM_NUMBER_OF_BITS;
4465 }
4466
4467 /* We can ignore any carry out, because it will be handled by
4468 extra_digit if it is needed. */
4469
4470 extra_digit = (valueT) -1;
4471 op = O_big;
4472 }
4473
4474 if (op == O_absent || op == O_illegal)
4475 {
4476 as_warn (_("zero assumed for missing expression"));
4477 exp->X_add_number = 0;
4478 op = O_constant;
4479 }
4480 else if (op == O_big && exp->X_add_number <= 0)
4481 {
4482 as_bad (_("floating point number invalid"));
4483 exp->X_add_number = 0;
4484 op = O_constant;
4485 }
4486 else if (op == O_register)
4487 {
4488 as_warn (_("register value used as expression"));
4489 op = O_constant;
4490 }
4491
4492 /* Allow `.word 0' in the absolute section. */
4493 if (now_seg == absolute_section)
4494 {
4495 if (op != O_constant || exp->X_add_number != 0)
4496 as_bad (_("attempt to store value in absolute section"));
4497 abs_section_offset += nbytes;
4498 return;
4499 }
4500
4501 /* Allow `.word 0' in BSS style sections. */
4502 if ((op != O_constant || exp->X_add_number != 0) && in_bss ())
4503 as_bad (_("attempt to store non-zero value in section `%s'"),
4504 segment_name (now_seg));
4505
4506 p = frag_more ((int) nbytes);
4507
4508 if (reloc != TC_PARSE_CONS_RETURN_NONE)
4509 {
4510 emit_expr_fix (exp, nbytes, frag_now, p, reloc);
4511 return;
4512 }
4513
4514 #ifndef WORKING_DOT_WORD
4515 /* If we have the difference of two symbols in a word, save it on
4516 the broken_words list. See the code in write.c. */
4517 if (op == O_subtract && nbytes == 2)
4518 {
4519 struct broken_word *x;
4520
4521 x = XNEW (struct broken_word);
4522 x->next_broken_word = broken_words;
4523 broken_words = x;
4524 x->seg = now_seg;
4525 x->subseg = now_subseg;
4526 x->frag = frag_now;
4527 x->word_goes_here = p;
4528 x->dispfrag = 0;
4529 x->add = exp->X_add_symbol;
4530 x->sub = exp->X_op_symbol;
4531 x->addnum = exp->X_add_number;
4532 x->added = 0;
4533 x->use_jump = 0;
4534 new_broken_words++;
4535 return;
4536 }
4537 #endif
4538
4539 /* If we have an integer, but the number of bytes is too large to
4540 pass to md_number_to_chars, handle it as a bignum. */
4541 if (op == O_constant && nbytes > sizeof (valueT))
4542 {
4543 extra_digit = exp->X_unsigned ? 0 : -1;
4544 convert_to_bignum (exp, !exp->X_unsigned);
4545 op = O_big;
4546 }
4547
4548 if (op == O_constant)
4549 {
4550 valueT get;
4551 valueT use;
4552 valueT mask;
4553 valueT unmask;
4554
4555 /* JF << of >= number of bits in the object is undefined. In
4556 particular SPARC (Sun 4) has problems. */
4557 if (nbytes >= sizeof (valueT))
4558 {
4559 know (nbytes == sizeof (valueT));
4560 mask = 0;
4561 }
4562 else
4563 {
4564 /* Don't store these bits. */
4565 mask = ~(valueT) 0 << (BITS_PER_CHAR * nbytes);
4566 }
4567
4568 unmask = ~mask; /* Do store these bits. */
4569
4570 #ifdef NEVER
4571 "Do this mod if you want every overflow check to assume SIGNED 2's complement data.";
4572 mask = ~(unmask >> 1); /* Includes sign bit now. */
4573 #endif
4574
4575 get = exp->X_add_number;
4576 use = get & unmask;
4577 if ((get & mask) != 0 && (-get & mask) != 0)
4578 {
4579 /* Leading bits contain both 0s & 1s. */
4580 as_warn (_("value 0x%" PRIx64 " truncated to 0x%" PRIx64),
4581 (uint64_t) get, (uint64_t) use);
4582 }
4583 /* Put bytes in right order. */
4584 md_number_to_chars (p, use, (int) nbytes);
4585 }
4586 else if (op == O_big)
4587 {
4588 unsigned int size;
4589 LITTLENUM_TYPE *nums;
4590
4591 size = exp->X_add_number * CHARS_PER_LITTLENUM;
4592 if (nbytes < size)
4593 {
4594 int i = nbytes / CHARS_PER_LITTLENUM;
4595
4596 if (i != 0)
4597 {
4598 LITTLENUM_TYPE sign = 0;
4599 if ((generic_bignum[--i]
4600 & (1 << (LITTLENUM_NUMBER_OF_BITS - 1))) != 0)
4601 sign = ~(LITTLENUM_TYPE) 0;
4602
4603 while (++i < exp->X_add_number)
4604 if (generic_bignum[i] != sign)
4605 break;
4606 }
4607 else if (nbytes == 1)
4608 {
4609 /* We have nbytes == 1 and CHARS_PER_LITTLENUM == 2 (probably).
4610 Check that bits 8.. of generic_bignum[0] match bit 7
4611 and that they match all of generic_bignum[1..exp->X_add_number]. */
4612 LITTLENUM_TYPE sign = (generic_bignum[0] & (1 << 7)) ? -1 : 0;
4613 LITTLENUM_TYPE himask = LITTLENUM_MASK & ~ 0xFF;
4614
4615 if ((generic_bignum[0] & himask) == (sign & himask))
4616 {
4617 while (++i < exp->X_add_number)
4618 if (generic_bignum[i] != sign)
4619 break;
4620 }
4621 }
4622
4623 if (i < exp->X_add_number)
4624 as_warn (ngettext ("bignum truncated to %d byte",
4625 "bignum truncated to %d bytes",
4626 nbytes),
4627 nbytes);
4628 size = nbytes;
4629 }
4630
4631 if (nbytes == 1)
4632 {
4633 md_number_to_chars (p, (valueT) generic_bignum[0], 1);
4634 return;
4635 }
4636 know (nbytes % CHARS_PER_LITTLENUM == 0);
4637
4638 if (target_big_endian)
4639 {
4640 while (nbytes > size)
4641 {
4642 md_number_to_chars (p, extra_digit, CHARS_PER_LITTLENUM);
4643 nbytes -= CHARS_PER_LITTLENUM;
4644 p += CHARS_PER_LITTLENUM;
4645 }
4646
4647 nums = generic_bignum + size / CHARS_PER_LITTLENUM;
4648 while (size >= CHARS_PER_LITTLENUM)
4649 {
4650 --nums;
4651 md_number_to_chars (p, (valueT) *nums, CHARS_PER_LITTLENUM);
4652 size -= CHARS_PER_LITTLENUM;
4653 p += CHARS_PER_LITTLENUM;
4654 }
4655 }
4656 else
4657 {
4658 nums = generic_bignum;
4659 while (size >= CHARS_PER_LITTLENUM)
4660 {
4661 md_number_to_chars (p, (valueT) *nums, CHARS_PER_LITTLENUM);
4662 ++nums;
4663 size -= CHARS_PER_LITTLENUM;
4664 p += CHARS_PER_LITTLENUM;
4665 nbytes -= CHARS_PER_LITTLENUM;
4666 }
4667
4668 while (nbytes >= CHARS_PER_LITTLENUM)
4669 {
4670 md_number_to_chars (p, extra_digit, CHARS_PER_LITTLENUM);
4671 nbytes -= CHARS_PER_LITTLENUM;
4672 p += CHARS_PER_LITTLENUM;
4673 }
4674 }
4675 }
4676 else
4677 emit_expr_fix (exp, nbytes, frag_now, p, TC_PARSE_CONS_RETURN_NONE);
4678 }
4679
4680 void
emit_expr_fix(expressionS * exp,unsigned int nbytes,fragS * frag,char * p,TC_PARSE_CONS_RETURN_TYPE r ATTRIBUTE_UNUSED)4681 emit_expr_fix (expressionS *exp, unsigned int nbytes, fragS *frag, char *p,
4682 TC_PARSE_CONS_RETURN_TYPE r ATTRIBUTE_UNUSED)
4683 {
4684 int offset = 0;
4685 unsigned int size = nbytes;
4686
4687 memset (p, 0, size);
4688
4689 /* Generate a fixS to record the symbol value. */
4690
4691 #ifdef TC_CONS_FIX_NEW
4692 TC_CONS_FIX_NEW (frag, p - frag->fr_literal + offset, size, exp, r);
4693 #else
4694 if (r != TC_PARSE_CONS_RETURN_NONE)
4695 {
4696 reloc_howto_type *reloc_howto;
4697
4698 reloc_howto = bfd_reloc_type_lookup (stdoutput, r);
4699 size = bfd_get_reloc_size (reloc_howto);
4700
4701 if (size > nbytes)
4702 {
4703 as_bad (ngettext ("%s relocations do not fit in %u byte",
4704 "%s relocations do not fit in %u bytes",
4705 nbytes),
4706 reloc_howto->name, nbytes);
4707 return;
4708 }
4709 else if (target_big_endian)
4710 offset = nbytes - size;
4711 }
4712 else
4713 switch (size)
4714 {
4715 case 1:
4716 r = BFD_RELOC_8;
4717 break;
4718 case 2:
4719 r = BFD_RELOC_16;
4720 break;
4721 case 3:
4722 r = BFD_RELOC_24;
4723 break;
4724 case 4:
4725 r = BFD_RELOC_32;
4726 break;
4727 case 8:
4728 r = BFD_RELOC_64;
4729 break;
4730 default:
4731 as_bad (_("unsupported BFD relocation size %u"), size);
4732 return;
4733 }
4734 fix_new_exp (frag, p - frag->fr_literal + offset, size,
4735 exp, 0, r);
4736 #endif
4737 }
4738
4739 /* Handle an MRI style string expression. */
4740
4741 #ifdef TC_M68K
4742 static void
parse_mri_cons(expressionS * exp,unsigned int nbytes)4743 parse_mri_cons (expressionS *exp, unsigned int nbytes)
4744 {
4745 if (*input_line_pointer != '\''
4746 && (input_line_pointer[1] != '\''
4747 || (*input_line_pointer != 'A'
4748 && *input_line_pointer != 'E')))
4749 (void) TC_PARSE_CONS_EXPRESSION (exp, nbytes);
4750 else
4751 {
4752 unsigned int scan;
4753 unsigned int result = 0;
4754
4755 /* An MRI style string. Cut into as many bytes as will fit into
4756 a nbyte chunk, left justify if necessary, and separate with
4757 commas so we can try again later. */
4758 if (*input_line_pointer == 'A')
4759 ++input_line_pointer;
4760 else if (*input_line_pointer == 'E')
4761 {
4762 as_bad (_("EBCDIC constants are not supported"));
4763 ++input_line_pointer;
4764 }
4765
4766 input_line_pointer++;
4767 for (scan = 0; scan < nbytes; scan++)
4768 {
4769 if (*input_line_pointer == '\'')
4770 {
4771 if (input_line_pointer[1] == '\'')
4772 {
4773 input_line_pointer++;
4774 }
4775 else
4776 break;
4777 }
4778 result = (result << 8) | (*input_line_pointer++);
4779 }
4780
4781 /* Left justify. */
4782 while (scan < nbytes)
4783 {
4784 result <<= 8;
4785 scan++;
4786 }
4787
4788 /* Create correct expression. */
4789 exp->X_op = O_constant;
4790 exp->X_add_number = result;
4791
4792 /* Fake it so that we can read the next char too. */
4793 if (input_line_pointer[0] != '\'' ||
4794 (input_line_pointer[0] == '\'' && input_line_pointer[1] == '\''))
4795 {
4796 input_line_pointer -= 2;
4797 input_line_pointer[0] = ',';
4798 input_line_pointer[1] = '\'';
4799 }
4800 else
4801 input_line_pointer++;
4802 }
4803 }
4804 #endif /* TC_M68K */
4805
4806 #ifdef REPEAT_CONS_EXPRESSIONS
4807
4808 /* Parse a repeat expression for cons. This is used by the MIPS
4809 assembler. The format is NUMBER:COUNT; NUMBER appears in the
4810 object file COUNT times.
4811
4812 To use this for a target, define REPEAT_CONS_EXPRESSIONS. */
4813
4814 static void
parse_repeat_cons(expressionS * exp,unsigned int nbytes)4815 parse_repeat_cons (expressionS *exp, unsigned int nbytes)
4816 {
4817 expressionS count;
4818 int i;
4819
4820 expression (exp);
4821
4822 if (*input_line_pointer != ':')
4823 {
4824 /* No repeat count. */
4825 return;
4826 }
4827
4828 ++input_line_pointer;
4829 expression (&count);
4830 if (count.X_op != O_constant
4831 || count.X_add_number <= 0)
4832 {
4833 as_warn (_("unresolvable or nonpositive repeat count; using 1"));
4834 return;
4835 }
4836
4837 /* The cons function is going to output this expression once. So we
4838 output it count - 1 times. */
4839 for (i = count.X_add_number - 1; i > 0; i--)
4840 emit_expr (exp, nbytes);
4841 }
4842
4843 #endif /* REPEAT_CONS_EXPRESSIONS */
4844
4845 /* Parse a floating point number represented as a hex constant. This
4846 permits users to specify the exact bits they want in the floating
4847 point number. */
4848
4849 static int
hex_float(int float_type,char * bytes)4850 hex_float (int float_type, char *bytes)
4851 {
4852 int pad, length = float_length (float_type, &pad);
4853 int i;
4854
4855 if (length < 0)
4856 return length;
4857
4858 /* It would be nice if we could go through expression to parse the
4859 hex constant, but if we get a bignum it's a pain to sort it into
4860 the buffer correctly. */
4861 i = 0;
4862 while (hex_p (*input_line_pointer) || *input_line_pointer == '_')
4863 {
4864 int d;
4865
4866 /* The MRI assembler accepts arbitrary underscores strewn about
4867 through the hex constant, so we ignore them as well. */
4868 if (*input_line_pointer == '_')
4869 {
4870 ++input_line_pointer;
4871 continue;
4872 }
4873
4874 if (i >= length)
4875 {
4876 as_warn (_("floating point constant too large"));
4877 return -1;
4878 }
4879 d = hex_value (*input_line_pointer) << 4;
4880 ++input_line_pointer;
4881 while (*input_line_pointer == '_')
4882 ++input_line_pointer;
4883 if (hex_p (*input_line_pointer))
4884 {
4885 d += hex_value (*input_line_pointer);
4886 ++input_line_pointer;
4887 }
4888 if (target_big_endian)
4889 bytes[i] = d;
4890 else
4891 bytes[length - i - 1] = d;
4892 ++i;
4893 }
4894
4895 if (i < length)
4896 {
4897 if (target_big_endian)
4898 memset (bytes + i, 0, length - i);
4899 else
4900 memset (bytes, 0, length - i);
4901 }
4902
4903 memset (bytes + length, 0, pad);
4904
4905 return length + pad;
4906 }
4907
4908 /* float_cons()
4909
4910 CONStruct some more frag chars of .floats .ffloats etc.
4911 Makes 0 or more new frags.
4912 If need_pass_2 == 1, no frags are emitted.
4913 This understands only floating literals, not expressions. Sorry.
4914
4915 A floating constant is defined by atof_generic(), except it is preceded
4916 by 0d 0f 0g or 0h. After observing the STRANGE way my BSD AS does its
4917 reading, I decided to be incompatible. This always tries to give you
4918 rounded bits to the precision of the pseudo-op. Former AS did premature
4919 truncation, restored noisy bits instead of trailing 0s AND gave you
4920 a choice of 2 flavours of noise according to which of 2 floating-point
4921 scanners you directed AS to use.
4922
4923 In: input_line_pointer->whitespace before, or '0' of flonum. */
4924
4925 void
float_cons(int float_type)4926 float_cons (/* Clobbers input_line-pointer, checks end-of-line. */
4927 int float_type /* 'f':.ffloat ... 'F':.float ... */)
4928 {
4929 char *p;
4930 int length; /* Number of chars in an object. */
4931 char temp[MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT];
4932
4933 if (is_it_end_of_statement ())
4934 {
4935 demand_empty_rest_of_line ();
4936 return;
4937 }
4938
4939 if (now_seg == absolute_section)
4940 {
4941 as_bad (_("attempt to store float in absolute section"));
4942 ignore_rest_of_line ();
4943 return;
4944 }
4945
4946 if (in_bss ())
4947 {
4948 as_bad (_("attempt to store float in section `%s'"),
4949 segment_name (now_seg));
4950 ignore_rest_of_line ();
4951 return;
4952 }
4953
4954 #ifdef md_flush_pending_output
4955 md_flush_pending_output ();
4956 #endif
4957
4958 #ifdef md_cons_align
4959 md_cons_align (1);
4960 #endif
4961
4962 do
4963 {
4964 length = parse_one_float (float_type, temp);
4965 if (length < 0)
4966 return;
4967
4968 if (!need_pass_2)
4969 {
4970 int count;
4971
4972 count = 1;
4973
4974 #ifdef REPEAT_CONS_EXPRESSIONS
4975 if (*input_line_pointer == ':')
4976 {
4977 expressionS count_exp;
4978
4979 ++input_line_pointer;
4980 expression (&count_exp);
4981
4982 if (count_exp.X_op != O_constant
4983 || count_exp.X_add_number <= 0)
4984 as_warn (_("unresolvable or nonpositive repeat count; using 1"));
4985 else
4986 count = count_exp.X_add_number;
4987 }
4988 #endif
4989
4990 while (--count >= 0)
4991 {
4992 p = frag_more (length);
4993 memcpy (p, temp, (unsigned int) length);
4994 }
4995 }
4996 SKIP_WHITESPACE ();
4997 }
4998 while (*input_line_pointer++ == ',');
4999
5000 /* Put terminator back into stream. */
5001 --input_line_pointer;
5002 demand_empty_rest_of_line ();
5003 }
5004
5005 /* LEB128 Encoding.
5006
5007 Note - we are using the DWARF standard's definition of LEB128 encoding
5008 where each 7-bit value is a stored in a byte, *not* an octet. This
5009 means that on targets where a byte contains multiple octets there is
5010 a *huge waste of space*. (This also means that we do not have to
5011 have special versions of these functions for when OCTETS_PER_BYTE_POWER
5012 is non-zero).
5013
5014 If the 7-bit values were to be packed into N-bit bytes (where N > 8)
5015 we would then have to consider whether multiple, successive LEB128
5016 values should be packed into the bytes without padding (bad idea) or
5017 whether each LEB128 number is padded out to a whole number of bytes.
5018 Plus you have to decide on the endianness of packing octets into a
5019 byte. */
5020
5021 /* Return the size of a LEB128 value in bytes. */
5022
5023 static inline unsigned int
sizeof_sleb128(offsetT value)5024 sizeof_sleb128 (offsetT value)
5025 {
5026 int size = 0;
5027 unsigned byte;
5028
5029 do
5030 {
5031 byte = (value & 0x7f);
5032 /* Sadly, we cannot rely on typical arithmetic right shift behaviour.
5033 Fortunately, we can structure things so that the extra work reduces
5034 to a noop on systems that do things "properly". */
5035 value = (value >> 7) | ~(-(offsetT)1 >> 7);
5036 size += 1;
5037 }
5038 while (!(((value == 0) && ((byte & 0x40) == 0))
5039 || ((value == -1) && ((byte & 0x40) != 0))));
5040
5041 return size;
5042 }
5043
5044 static inline unsigned int
sizeof_uleb128(valueT value)5045 sizeof_uleb128 (valueT value)
5046 {
5047 int size = 0;
5048
5049 do
5050 {
5051 value >>= 7;
5052 size += 1;
5053 }
5054 while (value != 0);
5055
5056 return size;
5057 }
5058
5059 unsigned int
sizeof_leb128(valueT value,int sign)5060 sizeof_leb128 (valueT value, int sign)
5061 {
5062 if (sign)
5063 return sizeof_sleb128 ((offsetT) value);
5064 else
5065 return sizeof_uleb128 (value);
5066 }
5067
5068 /* Output a LEB128 value. Returns the number of bytes used. */
5069
5070 static inline unsigned int
output_sleb128(char * p,offsetT value)5071 output_sleb128 (char *p, offsetT value)
5072 {
5073 char *orig = p;
5074 int more;
5075
5076 do
5077 {
5078 unsigned byte = (value & 0x7f);
5079
5080 /* Sadly, we cannot rely on typical arithmetic right shift behaviour.
5081 Fortunately, we can structure things so that the extra work reduces
5082 to a noop on systems that do things "properly". */
5083 value = (value >> 7) | ~(-(offsetT)1 >> 7);
5084
5085 more = !((((value == 0) && ((byte & 0x40) == 0))
5086 || ((value == -1) && ((byte & 0x40) != 0))));
5087 if (more)
5088 byte |= 0x80;
5089
5090 *p++ = byte;
5091 }
5092 while (more);
5093
5094 return p - orig;
5095 }
5096
5097 static inline unsigned int
output_uleb128(char * p,valueT value)5098 output_uleb128 (char *p, valueT value)
5099 {
5100 char *orig = p;
5101
5102 do
5103 {
5104 unsigned byte = (value & 0x7f);
5105
5106 value >>= 7;
5107 if (value != 0)
5108 /* More bytes to follow. */
5109 byte |= 0x80;
5110
5111 *p++ = byte;
5112 }
5113 while (value != 0);
5114
5115 return p - orig;
5116 }
5117
5118 unsigned int
output_leb128(char * p,valueT value,int sign)5119 output_leb128 (char *p, valueT value, int sign)
5120 {
5121 if (sign)
5122 return output_sleb128 (p, (offsetT) value);
5123 else
5124 return output_uleb128 (p, value);
5125 }
5126
5127 /* Do the same for bignums. We combine sizeof with output here in that
5128 we don't output for NULL values of P. It isn't really as critical as
5129 for "normal" values that this be streamlined. Returns the number of
5130 bytes used. */
5131
5132 static inline unsigned int
output_big_sleb128(char * p,LITTLENUM_TYPE * bignum,unsigned int size)5133 output_big_sleb128 (char *p, LITTLENUM_TYPE *bignum, unsigned int size)
5134 {
5135 char *orig = p;
5136 valueT val = 0;
5137 int loaded = 0;
5138 unsigned byte;
5139
5140 /* Strip leading sign extensions off the bignum. */
5141 while (size > 1
5142 && bignum[size - 1] == LITTLENUM_MASK
5143 && bignum[size - 2] > LITTLENUM_MASK / 2)
5144 size--;
5145
5146 do
5147 {
5148 /* OR in the next part of the littlenum. */
5149 val |= (*bignum << loaded);
5150 loaded += LITTLENUM_NUMBER_OF_BITS;
5151 size--;
5152 bignum++;
5153
5154 /* Add bytes until there are less than 7 bits left in VAL
5155 or until every non-sign bit has been written. */
5156 do
5157 {
5158 byte = val & 0x7f;
5159 loaded -= 7;
5160 val >>= 7;
5161 if (size > 0
5162 || val != ((byte & 0x40) == 0 ? 0 : ((valueT) 1 << loaded) - 1))
5163 byte |= 0x80;
5164
5165 if (orig)
5166 *p = byte;
5167 p++;
5168 }
5169 while ((byte & 0x80) != 0 && loaded >= 7);
5170 }
5171 while (size > 0);
5172
5173 /* Mop up any left-over bits (of which there will be less than 7). */
5174 if ((byte & 0x80) != 0)
5175 {
5176 /* Sign-extend VAL. */
5177 if (val & (1 << (loaded - 1)))
5178 val |= ~0U << loaded;
5179 if (orig)
5180 *p = val & 0x7f;
5181 p++;
5182 }
5183
5184 return p - orig;
5185 }
5186
5187 static inline unsigned int
output_big_uleb128(char * p,LITTLENUM_TYPE * bignum,unsigned int size)5188 output_big_uleb128 (char *p, LITTLENUM_TYPE *bignum, unsigned int size)
5189 {
5190 char *orig = p;
5191 valueT val = 0;
5192 int loaded = 0;
5193 unsigned byte;
5194
5195 /* Strip leading zeros off the bignum. */
5196 /* XXX: Is this needed? */
5197 while (size > 0 && bignum[size - 1] == 0)
5198 size--;
5199
5200 do
5201 {
5202 if (loaded < 7 && size > 0)
5203 {
5204 val |= (*bignum << loaded);
5205 loaded += 8 * CHARS_PER_LITTLENUM;
5206 size--;
5207 bignum++;
5208 }
5209
5210 byte = val & 0x7f;
5211 loaded -= 7;
5212 val >>= 7;
5213
5214 if (size > 0 || val)
5215 byte |= 0x80;
5216
5217 if (orig)
5218 *p = byte;
5219 p++;
5220 }
5221 while (byte & 0x80);
5222
5223 return p - orig;
5224 }
5225
5226 static unsigned int
output_big_leb128(char * p,LITTLENUM_TYPE * bignum,unsigned int size,int sign)5227 output_big_leb128 (char *p, LITTLENUM_TYPE *bignum, unsigned int size, int sign)
5228 {
5229 if (sign)
5230 return output_big_sleb128 (p, bignum, size);
5231 else
5232 return output_big_uleb128 (p, bignum, size);
5233 }
5234
5235 /* Generate the appropriate fragments for a given expression to emit a
5236 leb128 value. SIGN is 1 for sleb, 0 for uleb. */
5237
5238 void
emit_leb128_expr(expressionS * exp,int sign)5239 emit_leb128_expr (expressionS *exp, int sign)
5240 {
5241 operatorT op = exp->X_op;
5242 unsigned int nbytes;
5243
5244 if (op == O_absent || op == O_illegal)
5245 {
5246 as_warn (_("zero assumed for missing expression"));
5247 exp->X_add_number = 0;
5248 op = O_constant;
5249 }
5250 else if (op == O_big && exp->X_add_number <= 0)
5251 {
5252 as_bad (_("floating point number invalid"));
5253 exp->X_add_number = 0;
5254 op = O_constant;
5255 }
5256 else if (op == O_register)
5257 {
5258 as_warn (_("register value used as expression"));
5259 op = O_constant;
5260 }
5261 else if (op == O_constant
5262 && sign
5263 && (exp->X_add_number < 0) == !exp->X_extrabit)
5264 {
5265 /* We're outputting a signed leb128 and the sign of X_add_number
5266 doesn't reflect the sign of the original value. Convert EXP
5267 to a correctly-extended bignum instead. */
5268 convert_to_bignum (exp, exp->X_extrabit);
5269 op = O_big;
5270 }
5271
5272 if (now_seg == absolute_section)
5273 {
5274 if (op != O_constant || exp->X_add_number != 0)
5275 as_bad (_("attempt to store value in absolute section"));
5276 abs_section_offset++;
5277 return;
5278 }
5279
5280 if ((op != O_constant || exp->X_add_number != 0) && in_bss ())
5281 as_bad (_("attempt to store non-zero value in section `%s'"),
5282 segment_name (now_seg));
5283
5284 /* Let check_eh_frame know that data is being emitted. nbytes == -1 is
5285 a signal that this is leb128 data. It shouldn't optimize this away. */
5286 nbytes = (unsigned int) -1;
5287 if (check_eh_frame (exp, &nbytes))
5288 abort ();
5289
5290 /* Let the backend know that subsequent data may be byte aligned. */
5291 #ifdef md_cons_align
5292 md_cons_align (1);
5293 #endif
5294
5295 if (op == O_constant)
5296 {
5297 /* If we've got a constant, emit the thing directly right now. */
5298
5299 valueT value = exp->X_add_number;
5300 unsigned int size;
5301 char *p;
5302
5303 size = sizeof_leb128 (value, sign);
5304 p = frag_more (size);
5305 if (output_leb128 (p, value, sign) > size)
5306 abort ();
5307 }
5308 else if (op == O_big)
5309 {
5310 /* O_big is a different sort of constant. */
5311 int nbr_digits = exp->X_add_number;
5312 unsigned int size;
5313 char *p;
5314
5315 /* If the leading littenum is 0xffff, prepend a 0 to avoid confusion with
5316 a signed number. Unary operators like - or ~ always extend the
5317 bignum to its largest size. */
5318 if (exp->X_unsigned
5319 && nbr_digits < SIZE_OF_LARGE_NUMBER
5320 && generic_bignum[nbr_digits - 1] == LITTLENUM_MASK)
5321 generic_bignum[nbr_digits++] = 0;
5322
5323 size = output_big_leb128 (NULL, generic_bignum, nbr_digits, sign);
5324 p = frag_more (size);
5325 if (output_big_leb128 (p, generic_bignum, nbr_digits, sign) > size)
5326 abort ();
5327 }
5328 else
5329 {
5330 /* Otherwise, we have to create a variable sized fragment and
5331 resolve things later. */
5332
5333 frag_var (rs_leb128, sizeof_uleb128 (~(valueT) 0), 0, sign,
5334 make_expr_symbol (exp), 0, (char *) NULL);
5335 }
5336 }
5337
5338 /* Parse the .sleb128 and .uleb128 pseudos. */
5339
5340 void
s_leb128(int sign)5341 s_leb128 (int sign)
5342 {
5343 expressionS exp;
5344
5345 #ifdef md_flush_pending_output
5346 md_flush_pending_output ();
5347 #endif
5348
5349 do
5350 {
5351 expression (&exp);
5352 emit_leb128_expr (&exp, sign);
5353 }
5354 while (*input_line_pointer++ == ',');
5355
5356 input_line_pointer--;
5357 demand_empty_rest_of_line ();
5358 }
5359
5360 static void
stringer_append_char(int c,int bitsize)5361 stringer_append_char (int c, int bitsize)
5362 {
5363 if (c && in_bss ())
5364 as_bad (_("attempt to store non-empty string in section `%s'"),
5365 segment_name (now_seg));
5366
5367 if (!target_big_endian)
5368 FRAG_APPEND_1_CHAR (c);
5369
5370 switch (bitsize)
5371 {
5372 case 64:
5373 FRAG_APPEND_1_CHAR (0);
5374 FRAG_APPEND_1_CHAR (0);
5375 FRAG_APPEND_1_CHAR (0);
5376 FRAG_APPEND_1_CHAR (0);
5377 /* Fall through. */
5378 case 32:
5379 FRAG_APPEND_1_CHAR (0);
5380 FRAG_APPEND_1_CHAR (0);
5381 /* Fall through. */
5382 case 16:
5383 FRAG_APPEND_1_CHAR (0);
5384 /* Fall through. */
5385 case 8:
5386 break;
5387 default:
5388 /* Called with invalid bitsize argument. */
5389 abort ();
5390 break;
5391 }
5392 if (target_big_endian)
5393 FRAG_APPEND_1_CHAR (c);
5394 }
5395
5396 /* Worker to do .ascii etc statements.
5397 Reads 0 or more ',' separated, double-quoted strings.
5398 Caller should have checked need_pass_2 is FALSE because we don't
5399 check it.
5400 Checks for end-of-line.
5401 BITS_APPENDZERO says how many bits are in a target char.
5402 The bottom bit is set if a NUL char should be appended to the strings. */
5403
5404 void
stringer(int bits_appendzero)5405 stringer (int bits_appendzero)
5406 {
5407 const int bitsize = bits_appendzero & ~7;
5408 const int append_zero = bits_appendzero & 1;
5409 unsigned int c;
5410 #if !defined(NO_LISTING) && defined (OBJ_ELF)
5411 char *start;
5412 #endif
5413
5414 #ifdef md_flush_pending_output
5415 md_flush_pending_output ();
5416 #endif
5417
5418 #ifdef md_cons_align
5419 md_cons_align (1);
5420 #endif
5421
5422 /* If we have been switched into the abs_section then we
5423 will not have an obstack onto which we can hang strings. */
5424 if (now_seg == absolute_section)
5425 {
5426 as_bad (_("strings must be placed into a section"));
5427 ignore_rest_of_line ();
5428 return;
5429 }
5430
5431 /* The following awkward logic is to parse ZERO or more strings,
5432 comma separated. Recall a string expression includes spaces
5433 before the opening '\"' and spaces after the closing '\"'.
5434 We fake a leading ',' if there is (supposed to be)
5435 a 1st, expression. We keep demanding expressions for each ','. */
5436 if (is_it_end_of_statement ())
5437 {
5438 c = 0; /* Skip loop. */
5439 ++input_line_pointer; /* Compensate for end of loop. */
5440 }
5441 else
5442 {
5443 c = ','; /* Do loop. */
5444 }
5445
5446 while (c == ',' || c == '<' || c == '"')
5447 {
5448 SKIP_WHITESPACE ();
5449 switch (*input_line_pointer)
5450 {
5451 case '\"':
5452 ++input_line_pointer; /*->1st char of string. */
5453 #if !defined(NO_LISTING) && defined (OBJ_ELF)
5454 start = input_line_pointer;
5455 #endif
5456
5457 while (is_a_char (c = next_char_of_string ()))
5458 stringer_append_char (c, bitsize);
5459
5460 /* Treat "a" "b" as "ab". Even if we are appending zeros. */
5461 SKIP_ALL_WHITESPACE ();
5462 if (*input_line_pointer == '"')
5463 break;
5464
5465 if (append_zero)
5466 stringer_append_char (0, bitsize);
5467
5468 #if !defined(NO_LISTING) && defined (OBJ_ELF)
5469 /* In ELF, when gcc is emitting DWARF 1 debugging output, it
5470 will emit .string with a filename in the .debug section
5471 after a sequence of constants. See the comment in
5472 emit_expr for the sequence. emit_expr will set
5473 dwarf_file_string to non-zero if this string might be a
5474 source file name. */
5475 if (strcmp (segment_name (now_seg), ".debug") != 0)
5476 dwarf_file_string = 0;
5477 else if (dwarf_file_string)
5478 {
5479 c = input_line_pointer[-1];
5480 input_line_pointer[-1] = '\0';
5481 listing_source_file (start);
5482 input_line_pointer[-1] = c;
5483 }
5484 #endif
5485
5486 break;
5487 case '<':
5488 input_line_pointer++;
5489 c = get_single_number ();
5490 stringer_append_char (c, bitsize);
5491 if (*input_line_pointer != '>')
5492 {
5493 as_bad (_("expected <nn>"));
5494 ignore_rest_of_line ();
5495 return;
5496 }
5497 input_line_pointer++;
5498 break;
5499 case ',':
5500 input_line_pointer++;
5501 break;
5502 }
5503 SKIP_WHITESPACE ();
5504 c = *input_line_pointer;
5505 }
5506
5507 demand_empty_rest_of_line ();
5508 }
5509
5510 /* FIXME-SOMEDAY: I had trouble here on characters with the
5511 high bits set. We'll probably also have trouble with
5512 multibyte chars, wide chars, etc. Also be careful about
5513 returning values bigger than 1 byte. xoxorich. */
5514
5515 unsigned int
next_char_of_string(void)5516 next_char_of_string (void)
5517 {
5518 unsigned int c;
5519
5520 c = *input_line_pointer++ & CHAR_MASK;
5521 switch (c)
5522 {
5523 case 0:
5524 /* PR 20902: Do not advance past the end of the buffer. */
5525 -- input_line_pointer;
5526 c = NOT_A_CHAR;
5527 break;
5528
5529 case '\"':
5530 c = NOT_A_CHAR;
5531 break;
5532
5533 case '\n':
5534 as_warn (_("unterminated string; newline inserted"));
5535 bump_line_counters ();
5536 break;
5537
5538 case '\\':
5539 if (!TC_STRING_ESCAPES)
5540 break;
5541 switch (c = *input_line_pointer++ & CHAR_MASK)
5542 {
5543 case 'b':
5544 c = '\b';
5545 break;
5546
5547 case 'f':
5548 c = '\f';
5549 break;
5550
5551 case 'n':
5552 c = '\n';
5553 break;
5554
5555 case 'r':
5556 c = '\r';
5557 break;
5558
5559 case 't':
5560 c = '\t';
5561 break;
5562
5563 case 'v':
5564 c = '\013';
5565 break;
5566
5567 case '\\':
5568 case '"':
5569 break; /* As itself. */
5570
5571 case '0':
5572 case '1':
5573 case '2':
5574 case '3':
5575 case '4':
5576 case '5':
5577 case '6':
5578 case '7':
5579 case '8':
5580 case '9':
5581 {
5582 unsigned number;
5583 int i;
5584
5585 for (i = 0, number = 0;
5586 ISDIGIT (c) && i < 3;
5587 c = *input_line_pointer++, i++)
5588 {
5589 number = number * 8 + c - '0';
5590 }
5591
5592 c = number & CHAR_MASK;
5593 }
5594 --input_line_pointer;
5595 break;
5596
5597 case 'x':
5598 case 'X':
5599 {
5600 unsigned number;
5601
5602 number = 0;
5603 c = *input_line_pointer++;
5604 while (ISXDIGIT (c))
5605 {
5606 if (ISDIGIT (c))
5607 number = number * 16 + c - '0';
5608 else if (ISUPPER (c))
5609 number = number * 16 + c - 'A' + 10;
5610 else
5611 number = number * 16 + c - 'a' + 10;
5612 c = *input_line_pointer++;
5613 }
5614 c = number & CHAR_MASK;
5615 --input_line_pointer;
5616 }
5617 break;
5618
5619 case '\n':
5620 /* To be compatible with BSD 4.2 as: give the luser a linefeed!! */
5621 as_warn (_("unterminated string; newline inserted"));
5622 c = '\n';
5623 bump_line_counters ();
5624 break;
5625
5626 case 0:
5627 /* Do not advance past the end of the buffer. */
5628 -- input_line_pointer;
5629 c = NOT_A_CHAR;
5630 break;
5631
5632 default:
5633
5634 #ifdef ONLY_STANDARD_ESCAPES
5635 as_bad (_("bad escaped character in string"));
5636 c = '?';
5637 #endif /* ONLY_STANDARD_ESCAPES */
5638
5639 break;
5640 }
5641 break;
5642
5643 default:
5644 break;
5645 }
5646 return (c);
5647 }
5648
5649 static segT
get_segmented_expression(expressionS * expP)5650 get_segmented_expression (expressionS *expP)
5651 {
5652 segT retval;
5653
5654 retval = expression (expP);
5655 if (expP->X_op == O_illegal
5656 || expP->X_op == O_absent
5657 || expP->X_op == O_big)
5658 {
5659 as_bad (_("expected address expression"));
5660 expP->X_op = O_constant;
5661 expP->X_add_number = 0;
5662 retval = absolute_section;
5663 }
5664 return retval;
5665 }
5666
5667 static segT
get_known_segmented_expression(expressionS * expP)5668 get_known_segmented_expression (expressionS *expP)
5669 {
5670 segT retval = get_segmented_expression (expP);
5671
5672 if (retval == undefined_section)
5673 {
5674 /* There is no easy way to extract the undefined symbol from the
5675 expression. */
5676 if (expP->X_add_symbol != NULL
5677 && S_GET_SEGMENT (expP->X_add_symbol) != expr_section)
5678 as_warn (_("symbol \"%s\" undefined; zero assumed"),
5679 S_GET_NAME (expP->X_add_symbol));
5680 else
5681 as_warn (_("some symbol undefined; zero assumed"));
5682 retval = absolute_section;
5683 expP->X_op = O_constant;
5684 expP->X_add_number = 0;
5685 }
5686 return retval;
5687 }
5688
5689 char /* Return terminator. */
get_absolute_expression_and_terminator(long * val_pointer)5690 get_absolute_expression_and_terminator (long *val_pointer /* Return value of expression. */)
5691 {
5692 /* FIXME: val_pointer should probably be offsetT *. */
5693 *val_pointer = (long) get_absolute_expression ();
5694 return (*input_line_pointer++);
5695 }
5696
5697 /* Like demand_copy_string, but return NULL if the string contains any '\0's.
5698 Give a warning if that happens. */
5699
5700 char *
demand_copy_C_string(int * len_pointer)5701 demand_copy_C_string (int *len_pointer)
5702 {
5703 char *s;
5704
5705 if ((s = demand_copy_string (len_pointer)) != 0)
5706 {
5707 int len;
5708
5709 for (len = *len_pointer; len > 0; len--)
5710 {
5711 if (s[len - 1] == 0)
5712 {
5713 s = 0;
5714 *len_pointer = 0;
5715 as_bad (_("this string may not contain \'\\0\'"));
5716 break;
5717 }
5718 }
5719 }
5720
5721 return s;
5722 }
5723
5724 /* Demand string, but return a safe (=private) copy of the string.
5725 Return NULL if we can't read a string here. */
5726
5727 char *
demand_copy_string(int * lenP)5728 demand_copy_string (int *lenP)
5729 {
5730 unsigned int c;
5731 int len;
5732 char *retval;
5733
5734 len = 0;
5735 SKIP_WHITESPACE ();
5736 if (*input_line_pointer == '\"')
5737 {
5738 input_line_pointer++; /* Skip opening quote. */
5739
5740 while (is_a_char (c = next_char_of_string ()))
5741 {
5742 obstack_1grow (¬es, c);
5743 len++;
5744 }
5745 /* JF this next line is so demand_copy_C_string will return a
5746 null terminated string. */
5747 obstack_1grow (¬es, '\0');
5748 retval = (char *) obstack_finish (¬es);
5749 }
5750 else
5751 {
5752 as_bad (_("missing string"));
5753 retval = NULL;
5754 ignore_rest_of_line ();
5755 }
5756 *lenP = len;
5757 return (retval);
5758 }
5759
5760 /* In: Input_line_pointer->next character.
5761
5762 Do: Skip input_line_pointer over all whitespace.
5763
5764 Out: 1 if input_line_pointer->end-of-line. */
5765
5766 int
is_it_end_of_statement(void)5767 is_it_end_of_statement (void)
5768 {
5769 SKIP_WHITESPACE ();
5770 return (is_end_of_line[(unsigned char) *input_line_pointer]);
5771 }
5772
5773 void
equals(char * sym_name,int reassign)5774 equals (char *sym_name, int reassign)
5775 {
5776 char *stop = NULL;
5777 char stopc = 0;
5778
5779 input_line_pointer++;
5780 if (*input_line_pointer == '=')
5781 input_line_pointer++;
5782 if (reassign < 0 && *input_line_pointer == '=')
5783 input_line_pointer++;
5784
5785 while (*input_line_pointer == ' ' || *input_line_pointer == '\t')
5786 input_line_pointer++;
5787
5788 if (flag_mri)
5789 stop = mri_comment_field (&stopc);
5790
5791 assign_symbol (sym_name, reassign >= 0 ? !reassign : reassign);
5792
5793 if (flag_mri)
5794 {
5795 demand_empty_rest_of_line ();
5796 mri_comment_end (stop, stopc);
5797 }
5798 }
5799
5800 /* Open FILENAME, first trying the unadorned file name, then if that
5801 fails and the file name is not an absolute path, attempt to open
5802 the file in current -I include paths. PATH is a preallocated
5803 buffer which will be set to the file opened, or FILENAME if no file
5804 is found. */
5805
5806 FILE *
search_and_open(const char * filename,char * path)5807 search_and_open (const char *filename, char *path)
5808 {
5809 FILE *f = fopen (filename, FOPEN_RB);
5810 if (f == NULL && !IS_ABSOLUTE_PATH (filename))
5811 {
5812 for (size_t i = 0; i < include_dir_count; i++)
5813 {
5814 sprintf (path, "%s/%s", include_dirs[i], filename);
5815 f = fopen (path, FOPEN_RB);
5816 if (f != NULL)
5817 return f;
5818 }
5819 }
5820 strcpy (path, filename);
5821 return f;
5822 }
5823
5824 /* .incbin -- include a file verbatim at the current location. */
5825
5826 void
s_incbin(int x ATTRIBUTE_UNUSED)5827 s_incbin (int x ATTRIBUTE_UNUSED)
5828 {
5829 FILE * binfile;
5830 char * path;
5831 char * filename;
5832 char * binfrag;
5833 long skip = 0;
5834 long count = 0;
5835 long bytes;
5836 int len;
5837
5838 #ifdef md_flush_pending_output
5839 md_flush_pending_output ();
5840 #endif
5841
5842 #ifdef md_cons_align
5843 md_cons_align (1);
5844 #endif
5845
5846 SKIP_WHITESPACE ();
5847 filename = demand_copy_string (& len);
5848 if (filename == NULL)
5849 return;
5850
5851 SKIP_WHITESPACE ();
5852
5853 /* Look for optional skip and count. */
5854 if (* input_line_pointer == ',')
5855 {
5856 ++ input_line_pointer;
5857 skip = get_absolute_expression ();
5858
5859 SKIP_WHITESPACE ();
5860
5861 if (* input_line_pointer == ',')
5862 {
5863 ++ input_line_pointer;
5864
5865 count = get_absolute_expression ();
5866 if (count == 0)
5867 as_warn (_(".incbin count zero, ignoring `%s'"), filename);
5868
5869 SKIP_WHITESPACE ();
5870 }
5871 }
5872
5873 demand_empty_rest_of_line ();
5874
5875 path = XNEWVEC (char, len + include_dir_maxlen + 2);
5876 binfile = search_and_open (filename, path);
5877
5878 if (binfile == NULL)
5879 as_bad (_("file not found: %s"), filename);
5880 else
5881 {
5882 long file_len;
5883 struct stat filestat;
5884
5885 if (fstat (fileno (binfile), &filestat) != 0
5886 || ! S_ISREG (filestat.st_mode)
5887 || S_ISDIR (filestat.st_mode))
5888 {
5889 as_bad (_("unable to include `%s'"), path);
5890 goto done;
5891 }
5892
5893 register_dependency (path);
5894
5895 /* Compute the length of the file. */
5896 if (fseek (binfile, 0, SEEK_END) != 0)
5897 {
5898 as_bad (_("seek to end of .incbin file failed `%s'"), path);
5899 goto done;
5900 }
5901 file_len = ftell (binfile);
5902
5903 /* If a count was not specified use the remainder of the file. */
5904 if (count == 0)
5905 count = file_len - skip;
5906
5907 if (skip < 0 || count < 0 || file_len < 0 || skip + count > file_len)
5908 {
5909 as_bad (_("skip (%ld) or count (%ld) invalid for file size (%ld)"),
5910 skip, count, file_len);
5911 goto done;
5912 }
5913
5914 if (fseek (binfile, skip, SEEK_SET) != 0)
5915 {
5916 as_bad (_("could not skip to %ld in file `%s'"), skip, path);
5917 goto done;
5918 }
5919
5920 /* Allocate frag space and store file contents in it. */
5921 binfrag = frag_more (count);
5922
5923 bytes = fread (binfrag, 1, count, binfile);
5924 if (bytes < count)
5925 as_warn (_("truncated file `%s', %ld of %ld bytes read"),
5926 path, bytes, count);
5927 }
5928 done:
5929 if (binfile != NULL)
5930 fclose (binfile);
5931 free (path);
5932 }
5933
5934 /* .include -- include a file at this point. */
5935
5936 void
s_include(int arg ATTRIBUTE_UNUSED)5937 s_include (int arg ATTRIBUTE_UNUSED)
5938 {
5939 char *filename;
5940 int i;
5941 FILE *try_file;
5942 char *path;
5943
5944 if (!flag_m68k_mri)
5945 {
5946 filename = demand_copy_string (&i);
5947 if (filename == NULL)
5948 {
5949 /* demand_copy_string has already printed an error and
5950 called ignore_rest_of_line. */
5951 return;
5952 }
5953 }
5954 else
5955 {
5956 SKIP_WHITESPACE ();
5957 i = 0;
5958 while (!is_end_of_line[(unsigned char) *input_line_pointer]
5959 && *input_line_pointer != ' '
5960 && *input_line_pointer != '\t')
5961 {
5962 obstack_1grow (¬es, *input_line_pointer);
5963 ++input_line_pointer;
5964 ++i;
5965 }
5966
5967 obstack_1grow (¬es, '\0');
5968 filename = (char *) obstack_finish (¬es);
5969 while (!is_end_of_line[(unsigned char) *input_line_pointer])
5970 ++input_line_pointer;
5971 }
5972
5973 demand_empty_rest_of_line ();
5974
5975 path = notes_alloc (i + include_dir_maxlen + 2);
5976 try_file = search_and_open (filename, path);
5977 if (try_file)
5978 fclose (try_file);
5979
5980 register_dependency (path);
5981 input_scrub_insert_file (path);
5982 }
5983
5984 void
init_include_dir(void)5985 init_include_dir (void)
5986 {
5987 include_dirs = XNEWVEC (const char *, 1);
5988 include_dirs[0] = "."; /* Current dir. */
5989 include_dir_count = 1;
5990 include_dir_maxlen = 1;
5991 }
5992
5993 void
add_include_dir(char * path)5994 add_include_dir (char *path)
5995 {
5996 include_dir_count++;
5997 include_dirs = XRESIZEVEC (const char *, include_dirs, include_dir_count);
5998 include_dirs[include_dir_count - 1] = path; /* New one. */
5999
6000 size_t i = strlen (path);
6001 if (i > include_dir_maxlen)
6002 include_dir_maxlen = i;
6003 }
6004
6005 /* Output debugging information to denote the source file. */
6006
6007 static void
generate_file_debug(void)6008 generate_file_debug (void)
6009 {
6010 if (debug_type == DEBUG_STABS)
6011 stabs_generate_asm_file ();
6012 }
6013
6014 /* Output line number debugging information for the current source line. */
6015
6016 void
generate_lineno_debug(void)6017 generate_lineno_debug (void)
6018 {
6019 switch (debug_type)
6020 {
6021 case DEBUG_UNSPECIFIED:
6022 case DEBUG_NONE:
6023 case DEBUG_DWARF:
6024 break;
6025 case DEBUG_STABS:
6026 stabs_generate_asm_lineno ();
6027 break;
6028 case DEBUG_ECOFF:
6029 ecoff_generate_asm_lineno ();
6030 break;
6031 case DEBUG_DWARF2:
6032 /* ??? We could here indicate to dwarf2dbg.c that something
6033 has changed. However, since there is additional backend
6034 support that is required (calling dwarf2_emit_insn), we
6035 let dwarf2dbg.c call as_where on its own. */
6036 break;
6037 case DEBUG_CODEVIEW:
6038 codeview_generate_asm_lineno ();
6039 break;
6040 }
6041 }
6042
6043 /* Output debugging information to mark a function entry point or end point.
6044 END_P is zero for .func, and non-zero for .endfunc. */
6045
6046 void
s_func(int end_p)6047 s_func (int end_p)
6048 {
6049 do_s_func (end_p, NULL);
6050 }
6051
6052 /* Subroutine of s_func so targets can choose a different default prefix.
6053 If DEFAULT_PREFIX is NULL, use the target's "leading char". */
6054
6055 static void
do_s_func(int end_p,const char * default_prefix)6056 do_s_func (int end_p, const char *default_prefix)
6057 {
6058 if (end_p)
6059 {
6060 if (current_name == NULL)
6061 {
6062 as_bad (_("missing .func"));
6063 ignore_rest_of_line ();
6064 return;
6065 }
6066
6067 if (debug_type == DEBUG_STABS)
6068 stabs_generate_asm_endfunc (current_name, current_label);
6069
6070 free (current_name);
6071 free (current_label);
6072 current_name = current_label = NULL;
6073 }
6074 else /* ! end_p */
6075 {
6076 char *name, *label;
6077 char delim1, delim2;
6078
6079 if (current_name != NULL)
6080 {
6081 as_bad (_(".endfunc missing for previous .func"));
6082 ignore_rest_of_line ();
6083 return;
6084 }
6085
6086 delim1 = get_symbol_name (& name);
6087 name = xstrdup (name);
6088 *input_line_pointer = delim1;
6089 SKIP_WHITESPACE_AFTER_NAME ();
6090 if (*input_line_pointer != ',')
6091 {
6092 if (default_prefix)
6093 {
6094 if (asprintf (&label, "%s%s", default_prefix, name) == -1)
6095 as_fatal ("%s", xstrerror (errno));
6096 }
6097 else
6098 {
6099 char leading_char = bfd_get_symbol_leading_char (stdoutput);
6100 /* Missing entry point, use function's name with the leading
6101 char prepended. */
6102 if (leading_char)
6103 {
6104 if (asprintf (&label, "%c%s", leading_char, name) == -1)
6105 as_fatal ("%s", xstrerror (errno));
6106 }
6107 else
6108 label = xstrdup (name);
6109 }
6110 }
6111 else
6112 {
6113 ++input_line_pointer;
6114 SKIP_WHITESPACE ();
6115 delim2 = get_symbol_name (& label);
6116 label = xstrdup (label);
6117 restore_line_pointer (delim2);
6118 }
6119
6120 if (debug_type == DEBUG_STABS)
6121 stabs_generate_asm_func (name, label);
6122
6123 current_name = name;
6124 current_label = label;
6125 }
6126
6127 demand_empty_rest_of_line ();
6128 }
6129
6130 #ifdef HANDLE_BUNDLE
6131
6132 void
s_bundle_align_mode(int arg ATTRIBUTE_UNUSED)6133 s_bundle_align_mode (int arg ATTRIBUTE_UNUSED)
6134 {
6135 unsigned int align = get_absolute_expression ();
6136 SKIP_WHITESPACE ();
6137 demand_empty_rest_of_line ();
6138
6139 if (align > (unsigned int) TC_ALIGN_LIMIT)
6140 as_fatal (_(".bundle_align_mode alignment too large (maximum %u)"),
6141 (unsigned int) TC_ALIGN_LIMIT);
6142
6143 if (bundle_lock_frag != NULL)
6144 {
6145 as_bad (_("cannot change .bundle_align_mode inside .bundle_lock"));
6146 return;
6147 }
6148
6149 bundle_align_p2 = align;
6150 }
6151
6152 void
s_bundle_lock(int arg ATTRIBUTE_UNUSED)6153 s_bundle_lock (int arg ATTRIBUTE_UNUSED)
6154 {
6155 demand_empty_rest_of_line ();
6156
6157 if (bundle_align_p2 == 0)
6158 {
6159 as_bad (_(".bundle_lock is meaningless without .bundle_align_mode"));
6160 return;
6161 }
6162
6163 if (bundle_lock_depth == 0)
6164 {
6165 bundle_lock_frchain = frchain_now;
6166 bundle_lock_frag = start_bundle ();
6167 }
6168 ++bundle_lock_depth;
6169 }
6170
6171 void
s_bundle_unlock(int arg ATTRIBUTE_UNUSED)6172 s_bundle_unlock (int arg ATTRIBUTE_UNUSED)
6173 {
6174 unsigned int size;
6175
6176 demand_empty_rest_of_line ();
6177
6178 if (bundle_lock_frag == NULL)
6179 {
6180 as_bad (_(".bundle_unlock without preceding .bundle_lock"));
6181 return;
6182 }
6183
6184 gas_assert (bundle_align_p2 > 0);
6185
6186 gas_assert (bundle_lock_depth > 0);
6187 if (--bundle_lock_depth > 0)
6188 return;
6189
6190 size = pending_bundle_size (bundle_lock_frag);
6191
6192 if (size > 1U << bundle_align_p2)
6193 as_bad (_(".bundle_lock sequence is %u bytes, "
6194 "but bundle size is only %u bytes"),
6195 size, 1u << bundle_align_p2);
6196 else
6197 finish_bundle (bundle_lock_frag, size);
6198
6199 bundle_lock_frag = NULL;
6200 bundle_lock_frchain = NULL;
6201 }
6202
6203 #endif /* HANDLE_BUNDLE */
6204
6205 void
s_ignore(int arg ATTRIBUTE_UNUSED)6206 s_ignore (int arg ATTRIBUTE_UNUSED)
6207 {
6208 ignore_rest_of_line ();
6209 }
6210
6211 void
read_print_statistics(FILE * file)6212 read_print_statistics (FILE *file)
6213 {
6214 htab_print_statistics (file, "pseudo-op table", po_hash);
6215 }
6216
6217 /* Inserts the given line into the input stream.
6218
6219 This call avoids macro/conditionals nesting checking, since the contents of
6220 the line are assumed to replace the contents of a line already scanned.
6221
6222 An appropriate use of this function would be substitution of input lines when
6223 called by md_start_line_hook(). The given line is assumed to already be
6224 properly scrubbed. */
6225
6226 void
input_scrub_insert_line(const char * line)6227 input_scrub_insert_line (const char *line)
6228 {
6229 sb newline;
6230 size_t len = strlen (line);
6231 sb_build (&newline, len);
6232 sb_add_buffer (&newline, line, len);
6233 input_scrub_include_sb (&newline, input_line_pointer, expanding_none);
6234 sb_kill (&newline);
6235 buffer_limit = input_scrub_next_buffer (&input_line_pointer);
6236 }
6237
6238 /* Insert a file into the input stream; the path must resolve to an actual
6239 file; no include path searching or dependency registering is performed. */
6240
6241 void
input_scrub_insert_file(char * path)6242 input_scrub_insert_file (char *path)
6243 {
6244 input_scrub_include_file (path, input_line_pointer);
6245 buffer_limit = input_scrub_next_buffer (&input_line_pointer);
6246 }
6247
6248 /* Find the end of a line, considering quotation and escaping of quotes. */
6249
6250 #if !defined(TC_SINGLE_QUOTE_STRINGS) && defined(SINGLE_QUOTE_STRINGS)
6251 # define TC_SINGLE_QUOTE_STRINGS 1
6252 #endif
6253
6254 static char *
_find_end_of_line(char * s,int mri_string,int insn ATTRIBUTE_UNUSED,int in_macro)6255 _find_end_of_line (char *s, int mri_string, int insn ATTRIBUTE_UNUSED,
6256 int in_macro)
6257 {
6258 char inquote = '\0';
6259 int inescape = 0;
6260
6261 while (!is_end_of_line[(unsigned char) *s]
6262 || (inquote && !ISCNTRL (*s))
6263 || (inquote == '\'' && flag_mri)
6264 #ifdef TC_EOL_IN_INSN
6265 || (insn && TC_EOL_IN_INSN (s))
6266 #endif
6267 /* PR 6926: When we are parsing the body of a macro the sequence
6268 \@ is special - it refers to the invocation count. If the @
6269 character happens to be registered as a line-separator character
6270 by the target, then the is_end_of_line[] test above will have
6271 returned true, but we need to ignore the line separating
6272 semantics in this particular case. */
6273 || (in_macro && inescape && *s == '@')
6274 )
6275 {
6276 if (mri_string && *s == '\'')
6277 inquote ^= *s;
6278 else if (inescape)
6279 inescape = 0;
6280 else if (*s == '\\')
6281 inescape = 1;
6282 else if (!inquote
6283 ? *s == '"'
6284 #ifdef TC_SINGLE_QUOTE_STRINGS
6285 || (TC_SINGLE_QUOTE_STRINGS && *s == '\'')
6286 #endif
6287 : *s == inquote)
6288 inquote ^= *s;
6289 ++s;
6290 }
6291 if (inquote)
6292 as_warn (_("missing closing `%c'"), inquote);
6293 if (inescape && !ignore_input ())
6294 as_warn (_("stray `\\'"));
6295 return s;
6296 }
6297
6298 char *
find_end_of_line(char * s,int mri_string)6299 find_end_of_line (char *s, int mri_string)
6300 {
6301 return _find_end_of_line (s, mri_string, 0, 0);
6302 }
6303
6304 static char *saved_ilp;
6305 static char *saved_limit;
6306
6307 /* Use BUF as a temporary input pointer for calling other functions in this
6308 file. BUF must be a C string, so that its end can be found by strlen.
6309 Also sets the buffer_limit variable (local to this file) so that buffer
6310 overruns should not occur. Saves the current input line pointer so that
6311 it can be restored by calling restore_ilp().
6312
6313 Does not support recursion. */
6314
6315 void
temp_ilp(char * buf)6316 temp_ilp (char *buf)
6317 {
6318 gas_assert (saved_ilp == NULL);
6319 gas_assert (buf != NULL);
6320
6321 saved_ilp = input_line_pointer;
6322 saved_limit = buffer_limit;
6323 /* Prevent the assert in restore_ilp from triggering if
6324 the input_line_pointer has not yet been initialised. */
6325 if (saved_ilp == NULL)
6326 saved_limit = saved_ilp = (char *) "";
6327
6328 input_line_pointer = buf;
6329 buffer_limit = buf + strlen (buf);
6330 input_from_string = true;
6331 }
6332
6333 /* Restore a saved input line pointer. */
6334
6335 void
restore_ilp(void)6336 restore_ilp (void)
6337 {
6338 gas_assert (saved_ilp != NULL);
6339
6340 input_line_pointer = saved_ilp;
6341 buffer_limit = saved_limit;
6342 input_from_string = false;
6343
6344 saved_ilp = NULL;
6345 }
6346