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