1 /* as.c - GAS main program.
2 Copyright (C) 1987-2016 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, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
14 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 /* Main program for AS; a 32-bit assembler of GNU.
22 Understands command arguments.
23 Has a few routines that don't fit in other modules because they
24 are shared.
25
26 bugs
27
28 : initialisers
29 Since no-one else says they will support them in future: I
30 don't support them now. */
31
32 #define COMMON
33
34 #include "as.h"
35 #include "subsegs.h"
36 #include "output-file.h"
37 #include "sb.h"
38 #include "macro.h"
39 #include "dwarf2dbg.h"
40 #include "dw2gencfi.h"
41 #include "bfdver.h"
42
43 #ifdef HAVE_ITBL_CPU
44 #include "itbl-ops.h"
45 #else
46 #define itbl_init()
47 #endif
48
49 #ifdef __MACH__
50 #undef HAVE_SBRK
51 #endif
52
53 #ifdef HAVE_SBRK
54 #ifdef NEED_DECLARATION_SBRK
55 extern void *sbrk ();
56 #endif
57 #endif
58
59 #ifdef USING_CGEN
60 /* Perform any cgen specific initialisation for gas. */
61 extern void gas_cgen_begin (void);
62 #endif
63
64 /* We build a list of defsyms as we read the options, and then define
65 them after we have initialized everything. */
66 struct defsym_list
67 {
68 struct defsym_list *next;
69 char *name;
70 valueT value;
71 };
72
73
74 /* True if a listing is wanted. */
75 int listing;
76
77 /* Type of debugging to generate. */
78 enum debug_info_type debug_type = DEBUG_UNSPECIFIED;
79 int use_gnu_debug_info_extensions = 0;
80
81 #ifndef MD_DEBUG_FORMAT_SELECTOR
82 #define MD_DEBUG_FORMAT_SELECTOR NULL
83 #endif
84 static enum debug_info_type (*md_debug_format_selector) (int *) = MD_DEBUG_FORMAT_SELECTOR;
85
86 /* Maximum level of macro nesting. */
87 int max_macro_nest = 100;
88
89 /* argv[0] */
90 static char * myname;
91
92 /* The default obstack chunk size. If we set this to zero, the
93 obstack code will use whatever will fit in a 4096 byte block. */
94 int chunksize = 0;
95
96 /* To monitor memory allocation more effectively, make this non-zero.
97 Then the chunk sizes for gas and bfd will be reduced. */
98 int debug_memory = 0;
99
100 /* Enable verbose mode. */
101 int verbose = 0;
102
103 #if defined OBJ_ELF || defined OBJ_MAYBE_ELF
104 int flag_use_elf_stt_common = DEFAULT_GENERATE_ELF_STT_COMMON;
105 #endif
106
107 /* Enable incbin directive. */
108 int allow_incbin_directive = 1;
109
110 /* Keep the output file. */
111 static int keep_it = 0;
112
113 segT reg_section;
114 segT expr_section;
115 segT text_section;
116 segT data_section;
117 segT bss_section;
118
119 /* Name of listing file. */
120 static char *listing_filename = NULL;
121
122 static struct defsym_list *defsyms;
123
124 #ifdef HAVE_ITBL_CPU
125 /* Keep a record of the itbl files we read in. */
126 struct itbl_file_list
127 {
128 struct itbl_file_list *next;
129 char *name;
130 };
131 static struct itbl_file_list *itbl_files;
132 #endif
133
134 static long start_time;
135 #ifdef HAVE_SBRK
136 char *start_sbrk;
137 #endif
138
139 static int flag_macro_alternate;
140
141
142 #ifdef USE_EMULATIONS
143 #define EMULATION_ENVIRON "AS_EMULATION"
144
145 extern struct emulation mipsbelf, mipslelf, mipself;
146 extern struct emulation i386coff, i386elf, i386aout;
147 extern struct emulation crisaout, criself;
148
149 static struct emulation *const emulations[] = { EMULATIONS };
150 static const int n_emulations = sizeof (emulations) / sizeof (emulations[0]);
151
152 static void
select_emulation_mode(int argc,char ** argv)153 select_emulation_mode (int argc, char **argv)
154 {
155 int i;
156 char *p;
157 const char *em = NULL;
158
159 for (i = 1; i < argc; i++)
160 if (!strncmp ("--em", argv[i], 4))
161 break;
162
163 if (i == argc)
164 goto do_default;
165
166 p = strchr (argv[i], '=');
167 if (p)
168 p++;
169 else
170 p = argv[i + 1];
171
172 if (!p || !*p)
173 as_fatal (_("missing emulation mode name"));
174 em = p;
175
176 do_default:
177 if (em == 0)
178 em = getenv (EMULATION_ENVIRON);
179 if (em == 0)
180 em = DEFAULT_EMULATION;
181
182 if (em)
183 {
184 for (i = 0; i < n_emulations; i++)
185 if (!strcmp (emulations[i]->name, em))
186 break;
187 if (i == n_emulations)
188 as_fatal (_("unrecognized emulation name `%s'"), em);
189 this_emulation = emulations[i];
190 }
191 else
192 this_emulation = emulations[0];
193
194 this_emulation->init ();
195 }
196
197 const char *
default_emul_bfd_name(void)198 default_emul_bfd_name (void)
199 {
200 abort ();
201 return NULL;
202 }
203
204 void
common_emul_init(void)205 common_emul_init (void)
206 {
207 this_format = this_emulation->format;
208
209 if (this_emulation->leading_underscore == 2)
210 this_emulation->leading_underscore = this_format->dfl_leading_underscore;
211
212 if (this_emulation->default_endian != 2)
213 target_big_endian = this_emulation->default_endian;
214
215 if (this_emulation->fake_label_name == 0)
216 {
217 if (this_emulation->leading_underscore)
218 this_emulation->fake_label_name = "L0\001";
219 else
220 /* What other parameters should we test? */
221 this_emulation->fake_label_name = ".L0\001";
222 }
223 }
224 #endif
225
226 void
print_version_id(void)227 print_version_id (void)
228 {
229 static int printed;
230
231 if (printed)
232 return;
233 printed = 1;
234
235 fprintf (stderr, _("GNU assembler version %s (%s) using BFD version %s\n"),
236 VERSION, TARGET_ALIAS, BFD_VERSION_STRING);
237 }
238
239 #ifdef DEFAULT_FLAG_COMPRESS_DEBUG
240 enum compressed_debug_section_type flag_compress_debug
241 = COMPRESS_DEBUG_GABI_ZLIB;
242 #endif
243
244 static void
show_usage(FILE * stream)245 show_usage (FILE * stream)
246 {
247 fprintf (stream, _("Usage: %s [option...] [asmfile...]\n"), myname);
248
249 fprintf (stream, _("\
250 Options:\n\
251 -a[sub-option...] turn on listings\n\
252 Sub-options [default hls]:\n\
253 c omit false conditionals\n\
254 d omit debugging directives\n\
255 g include general info\n\
256 h include high-level source\n\
257 l include assembly\n\
258 m include macro expansions\n\
259 n omit forms processing\n\
260 s include symbols\n\
261 =FILE list to FILE (must be last sub-option)\n"));
262
263 fprintf (stream, _("\
264 --alternate initially turn on alternate macro syntax\n"));
265 #ifdef DEFAULT_FLAG_COMPRESS_DEBUG
266 fprintf (stream, _("\
267 --compress-debug-sections[={none|zlib|zlib-gnu|zlib-gabi}]\n\
268 compress DWARF debug sections using zlib [default]\n"));
269 fprintf (stream, _("\
270 --nocompress-debug-sections\n\
271 don't compress DWARF debug sections\n"));
272 #else
273 fprintf (stream, _("\
274 --compress-debug-sections[={none|zlib|zlib-gnu|zlib-gabi}]\n\
275 compress DWARF debug sections using zlib\n"));
276 fprintf (stream, _("\
277 --nocompress-debug-sections\n\
278 don't compress DWARF debug sections [default]\n"));
279 #endif
280 fprintf (stream, _("\
281 -D produce assembler debugging messages\n"));
282 fprintf (stream, _("\
283 --debug-prefix-map OLD=NEW\n\
284 map OLD to NEW in debug information\n"));
285 fprintf (stream, _("\
286 --defsym SYM=VAL define symbol SYM to given value\n"));
287 #ifdef USE_EMULATIONS
288 {
289 int i;
290 const char *def_em;
291
292 fprintf (stream, "\
293 --em=[");
294 for (i = 0; i < n_emulations - 1; i++)
295 fprintf (stream, "%s | ", emulations[i]->name);
296 fprintf (stream, "%s]\n", emulations[i]->name);
297
298 def_em = getenv (EMULATION_ENVIRON);
299 if (!def_em)
300 def_em = DEFAULT_EMULATION;
301 fprintf (stream, _("\
302 emulate output (default %s)\n"), def_em);
303 }
304 #endif
305 #if defined OBJ_ELF || defined OBJ_MAYBE_ELF
306 fprintf (stream, _("\
307 --execstack require executable stack for this object\n"));
308 fprintf (stream, _("\
309 --noexecstack don't require executable stack for this object\n"));
310 fprintf (stream, _("\
311 --size-check=[error|warning]\n\
312 ELF .size directive check (default --size-check=error)\n"));
313 fprintf (stream, _("\
314 --elf-stt-common=[no|yes]\n\
315 generate ELF common symbols with STT_COMMON type\n"));
316 fprintf (stream, _("\
317 --sectname-subst enable section name substitution sequences\n"));
318 #endif
319 fprintf (stream, _("\
320 -f skip whitespace and comment preprocessing\n"));
321 fprintf (stream, _("\
322 -g --gen-debug generate debugging information\n"));
323 fprintf (stream, _("\
324 --gstabs generate STABS debugging information\n"));
325 fprintf (stream, _("\
326 --gstabs+ generate STABS debug info with GNU extensions\n"));
327 fprintf (stream, _("\
328 --gdwarf-2 generate DWARF2 debugging information\n"));
329 fprintf (stream, _("\
330 --gdwarf-sections generate per-function section names for DWARF line information\n"));
331 fprintf (stream, _("\
332 --hash-size=<value> set the hash table size close to <value>\n"));
333 fprintf (stream, _("\
334 --help show this message and exit\n"));
335 fprintf (stream, _("\
336 --target-help show target specific options\n"));
337 fprintf (stream, _("\
338 -I DIR add DIR to search list for .include directives\n"));
339 fprintf (stream, _("\
340 -J don't warn about signed overflow\n"));
341 fprintf (stream, _("\
342 -K warn when differences altered for long displacements\n"));
343 fprintf (stream, _("\
344 -L,--keep-locals keep local symbols (e.g. starting with `L')\n"));
345 fprintf (stream, _("\
346 -M,--mri assemble in MRI compatibility mode\n"));
347 fprintf (stream, _("\
348 --MD FILE write dependency information in FILE (default none)\n"));
349 fprintf (stream, _("\
350 -nocpp ignored\n"));
351 fprintf (stream, _("\
352 -no-pad-sections do not pad the end of sections to alignment boundaries\n"));
353 fprintf (stream, _("\
354 -o OBJFILE name the object-file output OBJFILE (default a.out)\n"));
355 fprintf (stream, _("\
356 -R fold data section into text section\n"));
357 fprintf (stream, _("\
358 --reduce-memory-overheads \n\
359 prefer smaller memory use at the cost of longer\n\
360 assembly times\n"));
361 fprintf (stream, _("\
362 --statistics print various measured statistics from execution\n"));
363 fprintf (stream, _("\
364 --strip-local-absolute strip local absolute symbols\n"));
365 fprintf (stream, _("\
366 --traditional-format Use same format as native assembler when possible\n"));
367 fprintf (stream, _("\
368 --version print assembler version number and exit\n"));
369 fprintf (stream, _("\
370 -W --no-warn suppress warnings\n"));
371 fprintf (stream, _("\
372 --warn don't suppress warnings\n"));
373 fprintf (stream, _("\
374 --fatal-warnings treat warnings as errors\n"));
375 #ifdef HAVE_ITBL_CPU
376 fprintf (stream, _("\
377 --itbl INSTTBL extend instruction set to include instructions\n\
378 matching the specifications defined in file INSTTBL\n"));
379 #endif
380 fprintf (stream, _("\
381 -w ignored\n"));
382 fprintf (stream, _("\
383 -X ignored\n"));
384 fprintf (stream, _("\
385 -Z generate object file even after errors\n"));
386 fprintf (stream, _("\
387 --listing-lhs-width set the width in words of the output data column of\n\
388 the listing\n"));
389 fprintf (stream, _("\
390 --listing-lhs-width2 set the width in words of the continuation lines\n\
391 of the output data column; ignored if smaller than\n\
392 the width of the first line\n"));
393 fprintf (stream, _("\
394 --listing-rhs-width set the max width in characters of the lines from\n\
395 the source file\n"));
396 fprintf (stream, _("\
397 --listing-cont-lines set the maximum number of continuation lines used\n\
398 for the output data column of the listing\n"));
399 fprintf (stream, _("\
400 @FILE read options from FILE\n"));
401
402 md_show_usage (stream);
403
404 fputc ('\n', stream);
405
406 if (REPORT_BUGS_TO[0] && stream == stdout)
407 fprintf (stream, _("Report bugs to %s\n"), REPORT_BUGS_TO);
408 }
409
410 /* Since it is easy to do here we interpret the special arg "-"
411 to mean "use stdin" and we set that argv[] pointing to "".
412 After we have munged argv[], the only things left are source file
413 name(s) and ""(s) denoting stdin. These file names are used
414 (perhaps more than once) later.
415
416 check for new machine-dep cmdline options in
417 md_parse_option definitions in config/tc-*.c. */
418
419 static void
parse_args(int * pargc,char *** pargv)420 parse_args (int * pargc, char *** pargv)
421 {
422 int old_argc;
423 int new_argc;
424 char ** old_argv;
425 char ** new_argv;
426 /* Starting the short option string with '-' is for programs that
427 expect options and other ARGV-elements in any order and that care about
428 the ordering of the two. We describe each non-option ARGV-element
429 as if it were the argument of an option with character code 1. */
430 char *shortopts;
431 extern const char *md_shortopts;
432 static const char std_shortopts[] =
433 {
434 '-', 'J',
435 #ifndef WORKING_DOT_WORD
436 /* -K is not meaningful if .word is not being hacked. */
437 'K',
438 #endif
439 'L', 'M', 'R', 'W', 'Z', 'a', ':', ':', 'D', 'f', 'g', ':',':', 'I', ':', 'o', ':',
440 #ifndef VMS
441 /* -v takes an argument on VMS, so we don't make it a generic
442 option. */
443 'v',
444 #endif
445 'w', 'X',
446 #ifdef HAVE_ITBL_CPU
447 /* New option for extending instruction set (see also --itbl below). */
448 't', ':',
449 #endif
450 '\0'
451 };
452 struct option *longopts;
453 extern struct option md_longopts[];
454 extern size_t md_longopts_size;
455 /* Codes used for the long options with no short synonyms. */
456 enum option_values
457 {
458 OPTION_HELP = OPTION_STD_BASE,
459 OPTION_NOCPP,
460 OPTION_STATISTICS,
461 OPTION_VERSION,
462 OPTION_DUMPCONFIG,
463 OPTION_VERBOSE,
464 OPTION_EMULATION,
465 OPTION_DEBUG_PREFIX_MAP,
466 OPTION_DEFSYM,
467 OPTION_LISTING_LHS_WIDTH,
468 OPTION_LISTING_LHS_WIDTH2,
469 OPTION_LISTING_RHS_WIDTH,
470 OPTION_LISTING_CONT_LINES,
471 OPTION_DEPFILE,
472 OPTION_GSTABS,
473 OPTION_GSTABS_PLUS,
474 OPTION_GDWARF2,
475 OPTION_GDWARF_SECTIONS,
476 OPTION_STRIP_LOCAL_ABSOLUTE,
477 OPTION_TRADITIONAL_FORMAT,
478 OPTION_WARN,
479 OPTION_TARGET_HELP,
480 OPTION_EXECSTACK,
481 OPTION_NOEXECSTACK,
482 OPTION_SIZE_CHECK,
483 OPTION_ELF_STT_COMMON,
484 OPTION_SECTNAME_SUBST,
485 OPTION_ALTERNATE,
486 OPTION_AL,
487 OPTION_HASH_TABLE_SIZE,
488 OPTION_REDUCE_MEMORY_OVERHEADS,
489 OPTION_WARN_FATAL,
490 OPTION_COMPRESS_DEBUG,
491 OPTION_NOCOMPRESS_DEBUG,
492 OPTION_NO_PAD_SECTIONS, /* = STD_BASE + 40 */
493 OPTION_ALLOW_INCBIN,
494 OPTION_NOALLOW_INCBIN
495 /* When you add options here, check that they do
496 not collide with OPTION_MD_BASE. See as.h. */
497 };
498
499 static const struct option std_longopts[] =
500 {
501 /* Note: commas are placed at the start of the line rather than
502 the end of the preceding line so that it is simpler to
503 selectively add and remove lines from this list. */
504 {"alternate", no_argument, NULL, OPTION_ALTERNATE}
505 /* The entry for "a" is here to prevent getopt_long_only() from
506 considering that -a is an abbreviation for --alternate. This is
507 necessary because -a=<FILE> is a valid switch but getopt would
508 normally reject it since --alternate does not take an argument. */
509 ,{"a", optional_argument, NULL, 'a'}
510 /* Handle -al=<FILE>. */
511 ,{"al", optional_argument, NULL, OPTION_AL}
512 ,{"compress-debug-sections", optional_argument, NULL, OPTION_COMPRESS_DEBUG}
513 ,{"nocompress-debug-sections", no_argument, NULL, OPTION_NOCOMPRESS_DEBUG}
514 ,{"allow-incbin", optional_argument, NULL, OPTION_ALLOW_INCBIN}
515 ,{"noallow-incbin", optional_argument, NULL, OPTION_NOALLOW_INCBIN}
516 ,{"debug-prefix-map", required_argument, NULL, OPTION_DEBUG_PREFIX_MAP}
517 ,{"defsym", required_argument, NULL, OPTION_DEFSYM}
518 ,{"dump-config", no_argument, NULL, OPTION_DUMPCONFIG}
519 ,{"emulation", required_argument, NULL, OPTION_EMULATION}
520 #if defined OBJ_ELF || defined OBJ_MAYBE_ELF
521 ,{"execstack", no_argument, NULL, OPTION_EXECSTACK}
522 ,{"noexecstack", no_argument, NULL, OPTION_NOEXECSTACK}
523 ,{"size-check", required_argument, NULL, OPTION_SIZE_CHECK}
524 ,{"elf-stt-common", required_argument, NULL, OPTION_ELF_STT_COMMON}
525 ,{"sectname-subst", no_argument, NULL, OPTION_SECTNAME_SUBST}
526 #endif
527 ,{"fatal-warnings", no_argument, NULL, OPTION_WARN_FATAL}
528 ,{"gdwarf-2", no_argument, NULL, OPTION_GDWARF2}
529 /* GCC uses --gdwarf-2 but GAS uses to use --gdwarf2,
530 so we keep it here for backwards compatibility. */
531 ,{"gdwarf2", no_argument, NULL, OPTION_GDWARF2}
532 ,{"gdwarf-sections", no_argument, NULL, OPTION_GDWARF_SECTIONS}
533 ,{"gen-debug", no_argument, NULL, 'g'}
534 ,{"gstabs", no_argument, NULL, OPTION_GSTABS}
535 ,{"gstabs+", no_argument, NULL, OPTION_GSTABS_PLUS}
536 ,{"hash-size", required_argument, NULL, OPTION_HASH_TABLE_SIZE}
537 ,{"help", no_argument, NULL, OPTION_HELP}
538 #ifdef HAVE_ITBL_CPU
539 /* New option for extending instruction set (see also -t above).
540 The "-t file" or "--itbl file" option extends the basic set of
541 valid instructions by reading "file", a text file containing a
542 list of instruction formats. The additional opcodes and their
543 formats are added to the built-in set of instructions, and
544 mnemonics for new registers may also be defined. */
545 ,{"itbl", required_argument, NULL, 't'}
546 #endif
547 /* getopt allows abbreviations, so we do this to stop it from
548 treating -k as an abbreviation for --keep-locals. Some
549 ports use -k to enable PIC assembly. */
550 ,{"keep-locals", no_argument, NULL, 'L'}
551 ,{"keep-locals", no_argument, NULL, 'L'}
552 ,{"listing-lhs-width", required_argument, NULL, OPTION_LISTING_LHS_WIDTH}
553 ,{"listing-lhs-width2", required_argument, NULL, OPTION_LISTING_LHS_WIDTH2}
554 ,{"listing-rhs-width", required_argument, NULL, OPTION_LISTING_RHS_WIDTH}
555 ,{"listing-cont-lines", required_argument, NULL, OPTION_LISTING_CONT_LINES}
556 ,{"MD", required_argument, NULL, OPTION_DEPFILE}
557 ,{"mri", no_argument, NULL, 'M'}
558 ,{"nocpp", no_argument, NULL, OPTION_NOCPP}
559 ,{"no-pad-sections", no_argument, NULL, OPTION_NO_PAD_SECTIONS}
560 ,{"no-warn", no_argument, NULL, 'W'}
561 ,{"reduce-memory-overheads", no_argument, NULL, OPTION_REDUCE_MEMORY_OVERHEADS}
562 ,{"statistics", no_argument, NULL, OPTION_STATISTICS}
563 ,{"strip-local-absolute", no_argument, NULL, OPTION_STRIP_LOCAL_ABSOLUTE}
564 ,{"version", no_argument, NULL, OPTION_VERSION}
565 ,{"verbose", no_argument, NULL, OPTION_VERBOSE}
566 ,{"target-help", no_argument, NULL, OPTION_TARGET_HELP}
567 ,{"traditional-format", no_argument, NULL, OPTION_TRADITIONAL_FORMAT}
568 ,{"warn", no_argument, NULL, OPTION_WARN}
569 };
570
571 /* Construct the option lists from the standard list and the target
572 dependent list. Include space for an extra NULL option and
573 always NULL terminate. */
574 shortopts = concat (std_shortopts, md_shortopts, (char *) NULL);
575 longopts = (struct option *) xmalloc (sizeof (std_longopts)
576 + md_longopts_size + sizeof (struct option));
577 memcpy (longopts, std_longopts, sizeof (std_longopts));
578 memcpy (((char *) longopts) + sizeof (std_longopts), md_longopts, md_longopts_size);
579 memset (((char *) longopts) + sizeof (std_longopts) + md_longopts_size,
580 0, sizeof (struct option));
581
582 /* Make a local copy of the old argv. */
583 old_argc = *pargc;
584 old_argv = *pargv;
585
586 /* Initialize a new argv that contains no options. */
587 new_argv = XNEWVEC (char *, old_argc + 1);
588 new_argv[0] = old_argv[0];
589 new_argc = 1;
590 new_argv[new_argc] = NULL;
591
592 while (1)
593 {
594 /* getopt_long_only is like getopt_long, but '-' as well as '--' can
595 indicate a long option. */
596 int longind;
597 int optc = getopt_long_only (old_argc, old_argv, shortopts, longopts,
598 &longind);
599
600 if (optc == -1)
601 break;
602
603 switch (optc)
604 {
605 default:
606 /* md_parse_option should return 1 if it recognizes optc,
607 0 if not. */
608 if (md_parse_option (optc, optarg) != 0)
609 break;
610 /* `-v' isn't included in the general short_opts list, so check for
611 it explicitly here before deciding we've gotten a bad argument. */
612 if (optc == 'v')
613 {
614 #ifdef VMS
615 /* Telling getopt to treat -v's value as optional can result
616 in it picking up a following filename argument here. The
617 VMS code in md_parse_option can return 0 in that case,
618 but it has no way of pushing the filename argument back. */
619 if (optarg && *optarg)
620 new_argv[new_argc++] = optarg, new_argv[new_argc] = NULL;
621 else
622 #else
623 case 'v':
624 #endif
625 case OPTION_VERBOSE:
626 print_version_id ();
627 verbose = 1;
628 break;
629 }
630 else
631 as_bad (_("unrecognized option -%c%s"), optc, optarg ? optarg : "");
632 /* Fall through. */
633
634 case '?':
635 exit (EXIT_FAILURE);
636
637 case 1: /* File name. */
638 if (!strcmp (optarg, "-"))
639 optarg = (char *) "";
640 new_argv[new_argc++] = optarg;
641 new_argv[new_argc] = NULL;
642 break;
643
644 case OPTION_TARGET_HELP:
645 md_show_usage (stdout);
646 exit (EXIT_SUCCESS);
647
648 case OPTION_HELP:
649 show_usage (stdout);
650 exit (EXIT_SUCCESS);
651
652 case OPTION_NOCPP:
653 break;
654
655 case OPTION_NO_PAD_SECTIONS:
656 do_not_pad_sections_to_alignment = 1;
657 break;
658
659 case OPTION_STATISTICS:
660 flag_print_statistics = 1;
661 break;
662
663 case OPTION_STRIP_LOCAL_ABSOLUTE:
664 flag_strip_local_absolute = 1;
665 break;
666
667 case OPTION_TRADITIONAL_FORMAT:
668 flag_traditional_format = 1;
669 break;
670
671 case OPTION_VERSION:
672 /* This output is intended to follow the GNU standards document. */
673 printf (_("GNU assembler %s\n"), BFD_VERSION_STRING);
674 printf (_("Copyright (C) 2016 Free Software Foundation, Inc.\n"));
675 printf (_("\
676 This program is free software; you may redistribute it under the terms of\n\
677 the GNU General Public License version 3 or later.\n\
678 This program has absolutely no warranty.\n"));
679 #ifdef TARGET_WITH_CPU
680 printf (_("This assembler was configured for a target of `%s' "
681 "and default,\ncpu type `%s'.\n"),
682 TARGET_ALIAS, TARGET_WITH_CPU);
683 #else
684 printf (_("This assembler was configured for a target of `%s'.\n"),
685 TARGET_ALIAS);
686 #endif
687 exit (EXIT_SUCCESS);
688
689 case OPTION_EMULATION:
690 #ifdef USE_EMULATIONS
691 if (strcmp (optarg, this_emulation->name))
692 as_fatal (_("multiple emulation names specified"));
693 #else
694 as_fatal (_("emulations not handled in this configuration"));
695 #endif
696 break;
697
698 case OPTION_DUMPCONFIG:
699 fprintf (stderr, _("alias = %s\n"), TARGET_ALIAS);
700 fprintf (stderr, _("canonical = %s\n"), TARGET_CANONICAL);
701 fprintf (stderr, _("cpu-type = %s\n"), TARGET_CPU);
702 #ifdef TARGET_OBJ_FORMAT
703 fprintf (stderr, _("format = %s\n"), TARGET_OBJ_FORMAT);
704 #endif
705 #ifdef TARGET_FORMAT
706 fprintf (stderr, _("bfd-target = %s\n"), TARGET_FORMAT);
707 #endif
708 exit (EXIT_SUCCESS);
709
710 case OPTION_COMPRESS_DEBUG:
711 if (optarg)
712 {
713 #if defined OBJ_ELF || defined OBJ_MAYBE_ELF
714 if (strcasecmp (optarg, "none") == 0)
715 flag_compress_debug = COMPRESS_DEBUG_NONE;
716 else if (strcasecmp (optarg, "zlib") == 0)
717 flag_compress_debug = COMPRESS_DEBUG_GABI_ZLIB;
718 else if (strcasecmp (optarg, "zlib-gnu") == 0)
719 flag_compress_debug = COMPRESS_DEBUG_GNU_ZLIB;
720 else if (strcasecmp (optarg, "zlib-gabi") == 0)
721 flag_compress_debug = COMPRESS_DEBUG_GABI_ZLIB;
722 else
723 as_fatal (_("Invalid --compress-debug-sections option: `%s'"),
724 optarg);
725 #else
726 as_fatal (_("--compress-debug-sections=%s is unsupported"),
727 optarg);
728 #endif
729 }
730 else
731 flag_compress_debug = COMPRESS_DEBUG_GABI_ZLIB;
732 break;
733
734 case OPTION_NOCOMPRESS_DEBUG:
735 flag_compress_debug = COMPRESS_DEBUG_NONE;
736 break;
737
738 case OPTION_ALLOW_INCBIN:
739 allow_incbin_directive = 1;
740 break;
741
742 case OPTION_NOALLOW_INCBIN:
743 allow_incbin_directive = 0;
744 break;
745
746 case OPTION_DEBUG_PREFIX_MAP:
747 add_debug_prefix_map (optarg);
748 break;
749
750 case OPTION_DEFSYM:
751 {
752 char *s;
753 valueT i;
754 struct defsym_list *n;
755
756 for (s = optarg; *s != '\0' && *s != '='; s++)
757 ;
758 if (*s == '\0')
759 as_fatal (_("bad defsym; format is --defsym name=value"));
760 *s++ = '\0';
761 i = bfd_scan_vma (s, (const char **) NULL, 0);
762 n = XNEW (struct defsym_list);
763 n->next = defsyms;
764 n->name = optarg;
765 n->value = i;
766 defsyms = n;
767 }
768 break;
769
770 #ifdef HAVE_ITBL_CPU
771 case 't':
772 {
773 /* optarg is the name of the file containing the instruction
774 formats, opcodes, register names, etc. */
775 struct itbl_file_list *n;
776
777 if (optarg == NULL)
778 {
779 as_warn (_("no file name following -t option"));
780 break;
781 }
782
783 n = XNEW (struct itbl_file_list);
784 n->next = itbl_files;
785 n->name = optarg;
786 itbl_files = n;
787
788 /* Parse the file and add the new instructions to our internal
789 table. If multiple instruction tables are specified, the
790 information from this table gets appended onto the existing
791 internal table. */
792 itbl_files->name = xstrdup (optarg);
793 if (itbl_parse (itbl_files->name) != 0)
794 as_fatal (_("failed to read instruction table %s\n"),
795 itbl_files->name);
796 }
797 break;
798 #endif
799
800 case OPTION_DEPFILE:
801 start_dependencies (optarg);
802 break;
803
804 case 'g':
805 /* Some backends, eg Alpha and Mips, use the -g switch for their
806 own purposes. So we check here for an explicit -g and allow
807 the backend to decide if it wants to process it. */
808 if ( old_argv[optind - 1][1] == 'g'
809 && md_parse_option (optc, optarg))
810 continue;
811
812 if (md_debug_format_selector)
813 debug_type = md_debug_format_selector (& use_gnu_debug_info_extensions);
814 else if (IS_ELF)
815 debug_type = DEBUG_DWARF2;
816 else
817 debug_type = DEBUG_STABS;
818 break;
819
820 case OPTION_GSTABS_PLUS:
821 use_gnu_debug_info_extensions = 1;
822 /* Fall through. */
823 case OPTION_GSTABS:
824 debug_type = DEBUG_STABS;
825 break;
826
827 case OPTION_GDWARF2:
828 debug_type = DEBUG_DWARF2;
829 break;
830
831 case OPTION_GDWARF_SECTIONS:
832 flag_dwarf_sections = TRUE;
833 break;
834
835 case 'J':
836 flag_signed_overflow_ok = 1;
837 break;
838
839 #ifndef WORKING_DOT_WORD
840 case 'K':
841 flag_warn_displacement = 1;
842 break;
843 #endif
844 case 'L':
845 flag_keep_locals = 1;
846 break;
847
848 case OPTION_LISTING_LHS_WIDTH:
849 listing_lhs_width = atoi (optarg);
850 if (listing_lhs_width_second < listing_lhs_width)
851 listing_lhs_width_second = listing_lhs_width;
852 break;
853 case OPTION_LISTING_LHS_WIDTH2:
854 {
855 int tmp = atoi (optarg);
856
857 if (tmp > listing_lhs_width)
858 listing_lhs_width_second = tmp;
859 }
860 break;
861 case OPTION_LISTING_RHS_WIDTH:
862 listing_rhs_width = atoi (optarg);
863 break;
864 case OPTION_LISTING_CONT_LINES:
865 listing_lhs_cont_lines = atoi (optarg);
866 break;
867
868 case 'M':
869 flag_mri = 1;
870 #ifdef TC_M68K
871 flag_m68k_mri = 1;
872 #endif
873 break;
874
875 case 'R':
876 flag_readonly_data_in_text = 1;
877 break;
878
879 case 'W':
880 flag_no_warnings = 1;
881 break;
882
883 case OPTION_WARN:
884 flag_no_warnings = 0;
885 flag_fatal_warnings = 0;
886 break;
887
888 case OPTION_WARN_FATAL:
889 flag_no_warnings = 0;
890 flag_fatal_warnings = 1;
891 break;
892
893 #if defined OBJ_ELF || defined OBJ_MAYBE_ELF
894 case OPTION_EXECSTACK:
895 flag_execstack = 1;
896 flag_noexecstack = 0;
897 break;
898
899 case OPTION_NOEXECSTACK:
900 flag_noexecstack = 1;
901 flag_execstack = 0;
902 break;
903
904 case OPTION_SIZE_CHECK:
905 if (strcasecmp (optarg, "error") == 0)
906 flag_allow_nonconst_size = FALSE;
907 else if (strcasecmp (optarg, "warning") == 0)
908 flag_allow_nonconst_size = TRUE;
909 else
910 as_fatal (_("Invalid --size-check= option: `%s'"), optarg);
911 break;
912
913 case OPTION_ELF_STT_COMMON:
914 if (strcasecmp (optarg, "no") == 0)
915 flag_use_elf_stt_common = 0;
916 else if (strcasecmp (optarg, "yes") == 0)
917 flag_use_elf_stt_common = 1;
918 else
919 as_fatal (_("Invalid --elf-stt-common= option: `%s'"),
920 optarg);
921 break;
922
923 case OPTION_SECTNAME_SUBST:
924 flag_sectname_subst = 1;
925 break;
926 #endif
927 case 'Z':
928 flag_always_generate_output = 1;
929 break;
930
931 case OPTION_AL:
932 listing |= LISTING_LISTING;
933 if (optarg)
934 listing_filename = xstrdup (optarg);
935 break;
936
937 case OPTION_ALTERNATE:
938 optarg = old_argv [optind - 1];
939 while (* optarg == '-')
940 optarg ++;
941
942 if (strcmp (optarg, "alternate") == 0)
943 {
944 flag_macro_alternate = 1;
945 break;
946 }
947 optarg ++;
948 /* Fall through. */
949
950 case 'a':
951 if (optarg)
952 {
953 if (optarg != old_argv[optind] && optarg[-1] == '=')
954 --optarg;
955
956 if (md_parse_option (optc, optarg) != 0)
957 break;
958
959 while (*optarg)
960 {
961 switch (*optarg)
962 {
963 case 'c':
964 listing |= LISTING_NOCOND;
965 break;
966 case 'd':
967 listing |= LISTING_NODEBUG;
968 break;
969 case 'g':
970 listing |= LISTING_GENERAL;
971 break;
972 case 'h':
973 listing |= LISTING_HLL;
974 break;
975 case 'l':
976 listing |= LISTING_LISTING;
977 break;
978 case 'm':
979 listing |= LISTING_MACEXP;
980 break;
981 case 'n':
982 listing |= LISTING_NOFORM;
983 break;
984 case 's':
985 listing |= LISTING_SYMBOLS;
986 break;
987 case '=':
988 listing_filename = xstrdup (optarg + 1);
989 optarg += strlen (listing_filename);
990 break;
991 default:
992 as_fatal (_("invalid listing option `%c'"), *optarg);
993 break;
994 }
995 optarg++;
996 }
997 }
998 if (!listing)
999 listing = LISTING_DEFAULT;
1000 break;
1001
1002 case 'D':
1003 /* DEBUG is implemented: it debugs different
1004 things from other people's assemblers. */
1005 flag_debug = 1;
1006 break;
1007
1008 case 'f':
1009 flag_no_comments = 1;
1010 break;
1011
1012 case 'I':
1013 { /* Include file directory. */
1014 char *temp = xstrdup (optarg);
1015
1016 add_include_dir (temp);
1017 break;
1018 }
1019
1020 case 'o':
1021 out_file_name = xstrdup (optarg);
1022 break;
1023
1024 case 'w':
1025 break;
1026
1027 case 'X':
1028 /* -X means treat warnings as errors. */
1029 break;
1030
1031 case OPTION_REDUCE_MEMORY_OVERHEADS:
1032 /* The only change we make at the moment is to reduce
1033 the size of the hash tables that we use. */
1034 set_gas_hash_table_size (4051);
1035 break;
1036
1037 case OPTION_HASH_TABLE_SIZE:
1038 {
1039 unsigned long new_size;
1040
1041 new_size = strtoul (optarg, NULL, 0);
1042 if (new_size)
1043 set_gas_hash_table_size (new_size);
1044 else
1045 as_fatal (_("--hash-size needs a numeric argument"));
1046 break;
1047 }
1048 }
1049 }
1050
1051 free (shortopts);
1052 free (longopts);
1053
1054 *pargc = new_argc;
1055 *pargv = new_argv;
1056
1057 #ifdef md_after_parse_args
1058 md_after_parse_args ();
1059 #endif
1060 }
1061
1062 static void
dump_statistics(void)1063 dump_statistics (void)
1064 {
1065 #ifdef HAVE_SBRK
1066 char *lim = (char *) sbrk (0);
1067 #endif
1068 long run_time = get_run_time () - start_time;
1069
1070 fprintf (stderr, _("%s: total time in assembly: %ld.%06ld\n"),
1071 myname, run_time / 1000000, run_time % 1000000);
1072 #ifdef HAVE_SBRK
1073 fprintf (stderr, _("%s: data size %ld\n"),
1074 myname, (long) (lim - start_sbrk));
1075 #endif
1076
1077 subsegs_print_statistics (stderr);
1078 write_print_statistics (stderr);
1079 symbol_print_statistics (stderr);
1080 read_print_statistics (stderr);
1081
1082 #ifdef tc_print_statistics
1083 tc_print_statistics (stderr);
1084 #endif
1085
1086 #ifdef obj_print_statistics
1087 obj_print_statistics (stderr);
1088 #endif
1089 }
1090
1091 static void
close_output_file(void)1092 close_output_file (void)
1093 {
1094 output_file_close (out_file_name);
1095 if (!keep_it)
1096 unlink_if_ordinary (out_file_name);
1097 }
1098
1099 /* The interface between the macro code and gas expression handling. */
1100
1101 static size_t
macro_expr(const char * emsg,size_t idx,sb * in,offsetT * val)1102 macro_expr (const char *emsg, size_t idx, sb *in, offsetT *val)
1103 {
1104 char *hold;
1105 expressionS ex;
1106
1107 sb_terminate (in);
1108
1109 hold = input_line_pointer;
1110 input_line_pointer = in->ptr + idx;
1111 expression_and_evaluate (&ex);
1112 idx = input_line_pointer - in->ptr;
1113 input_line_pointer = hold;
1114
1115 if (ex.X_op != O_constant)
1116 as_bad ("%s", emsg);
1117
1118 *val = ex.X_add_number;
1119
1120 return idx;
1121 }
1122
1123 /* Here to attempt 1 pass over each input file.
1124 We scan argv[*] looking for filenames or exactly "" which is
1125 shorthand for stdin. Any argv that is NULL is not a file-name.
1126 We set need_pass_2 TRUE if, after this, we still have unresolved
1127 expressions of the form (unknown value)+-(unknown value).
1128
1129 Note the un*x semantics: there is only 1 logical input file, but it
1130 may be a catenation of many 'physical' input files. */
1131
1132 static void
perform_an_assembly_pass(int argc,char ** argv)1133 perform_an_assembly_pass (int argc, char ** argv)
1134 {
1135 int saw_a_file = 0;
1136 #ifndef OBJ_MACH_O
1137 flagword applicable;
1138 #endif
1139
1140 need_pass_2 = 0;
1141
1142 #ifndef OBJ_MACH_O
1143 /* Create the standard sections, and those the assembler uses
1144 internally. */
1145 text_section = subseg_new (TEXT_SECTION_NAME, 0);
1146 data_section = subseg_new (DATA_SECTION_NAME, 0);
1147 bss_section = subseg_new (BSS_SECTION_NAME, 0);
1148 /* @@ FIXME -- we're setting the RELOC flag so that sections are assumed
1149 to have relocs, otherwise we don't find out in time. */
1150 applicable = bfd_applicable_section_flags (stdoutput);
1151 bfd_set_section_flags (stdoutput, text_section,
1152 applicable & (SEC_ALLOC | SEC_LOAD | SEC_RELOC
1153 | SEC_CODE | SEC_READONLY));
1154 bfd_set_section_flags (stdoutput, data_section,
1155 applicable & (SEC_ALLOC | SEC_LOAD | SEC_RELOC
1156 | SEC_DATA));
1157 bfd_set_section_flags (stdoutput, bss_section, applicable & SEC_ALLOC);
1158 seg_info (bss_section)->bss = 1;
1159 #endif
1160 subseg_new (BFD_ABS_SECTION_NAME, 0);
1161 subseg_new (BFD_UND_SECTION_NAME, 0);
1162 reg_section = subseg_new ("*GAS `reg' section*", 0);
1163 expr_section = subseg_new ("*GAS `expr' section*", 0);
1164
1165 #ifndef OBJ_MACH_O
1166 subseg_set (text_section, 0);
1167 #endif
1168
1169 /* This may add symbol table entries, which requires having an open BFD,
1170 and sections already created. */
1171 md_begin ();
1172
1173 #ifdef USING_CGEN
1174 gas_cgen_begin ();
1175 #endif
1176 #ifdef obj_begin
1177 obj_begin ();
1178 #endif
1179
1180 /* Skip argv[0]. */
1181 argv++;
1182 argc--;
1183
1184 while (argc--)
1185 {
1186 if (*argv)
1187 { /* Is it a file-name argument? */
1188 PROGRESS (1);
1189 saw_a_file++;
1190 /* argv->"" if stdin desired, else->filename. */
1191 read_a_source_file (*argv);
1192 }
1193 argv++; /* Completed that argv. */
1194 }
1195 if (!saw_a_file)
1196 read_a_source_file ("");
1197 }
1198
1199
1200 int
main(int argc,char ** argv)1201 main (int argc, char ** argv)
1202 {
1203 char ** argv_orig = argv;
1204
1205 int macro_strip_at;
1206
1207 start_time = get_run_time ();
1208 #ifdef HAVE_SBRK
1209 start_sbrk = (char *) sbrk (0);
1210 #endif
1211
1212 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
1213 setlocale (LC_MESSAGES, "");
1214 #endif
1215 #if defined (HAVE_SETLOCALE)
1216 setlocale (LC_CTYPE, "");
1217 #endif
1218 bindtextdomain (PACKAGE, LOCALEDIR);
1219 textdomain (PACKAGE);
1220
1221 if (debug_memory)
1222 chunksize = 64;
1223
1224 #ifdef HOST_SPECIAL_INIT
1225 HOST_SPECIAL_INIT (argc, argv);
1226 #endif
1227
1228 myname = argv[0];
1229 xmalloc_set_program_name (myname);
1230
1231 expandargv (&argc, &argv);
1232
1233 START_PROGRESS (myname, 0);
1234
1235 #ifndef OBJ_DEFAULT_OUTPUT_FILE_NAME
1236 #define OBJ_DEFAULT_OUTPUT_FILE_NAME "a.out"
1237 #endif
1238
1239 out_file_name = OBJ_DEFAULT_OUTPUT_FILE_NAME;
1240
1241 hex_init ();
1242 bfd_init ();
1243 bfd_set_error_program_name (myname);
1244
1245 #ifdef USE_EMULATIONS
1246 select_emulation_mode (argc, argv);
1247 #endif
1248
1249 PROGRESS (1);
1250 /* Call parse_args before any of the init/begin functions
1251 so that switches like --hash-size can be honored. */
1252 parse_args (&argc, &argv);
1253 symbol_begin ();
1254 frag_init ();
1255 subsegs_begin ();
1256 read_begin ();
1257 input_scrub_begin ();
1258 expr_begin ();
1259
1260 /* It has to be called after dump_statistics (). */
1261 xatexit (close_output_file);
1262
1263 if (flag_print_statistics)
1264 xatexit (dump_statistics);
1265
1266 macro_strip_at = 0;
1267 #ifdef TC_I960
1268 macro_strip_at = flag_mri;
1269 #endif
1270
1271 macro_init (flag_macro_alternate, flag_mri, macro_strip_at, macro_expr);
1272
1273 PROGRESS (1);
1274
1275 output_file_create (out_file_name);
1276 gas_assert (stdoutput != 0);
1277
1278 dot_symbol_init ();
1279
1280 #ifdef tc_init_after_args
1281 tc_init_after_args ();
1282 #endif
1283
1284 itbl_init ();
1285
1286 dwarf2_init ();
1287
1288 local_symbol_make (".gasversion.", absolute_section,
1289 BFD_VERSION / 10000UL, &predefined_address_frag);
1290
1291 /* Now that we have fully initialized, and have created the output
1292 file, define any symbols requested by --defsym command line
1293 arguments. */
1294 while (defsyms != NULL)
1295 {
1296 symbolS *sym;
1297 struct defsym_list *next;
1298
1299 sym = symbol_new (defsyms->name, absolute_section, defsyms->value,
1300 &zero_address_frag);
1301 /* Make symbols defined on the command line volatile, so that they
1302 can be redefined inside a source file. This makes this assembler's
1303 behaviour compatible with earlier versions, but it may not be
1304 completely intuitive. */
1305 S_SET_VOLATILE (sym);
1306 symbol_table_insert (sym);
1307 next = defsyms->next;
1308 free (defsyms);
1309 defsyms = next;
1310 }
1311
1312 PROGRESS (1);
1313
1314 /* Assemble it. */
1315 perform_an_assembly_pass (argc, argv);
1316
1317 cond_finish_check (-1);
1318
1319 #ifdef md_end
1320 md_end ();
1321 #endif
1322
1323 #if defined OBJ_ELF || defined OBJ_MAYBE_ELF
1324 if ((flag_execstack || flag_noexecstack)
1325 && OUTPUT_FLAVOR == bfd_target_elf_flavour)
1326 {
1327 segT gnustack;
1328
1329 gnustack = subseg_new (".note.GNU-stack", 0);
1330 bfd_set_section_flags (stdoutput, gnustack,
1331 SEC_READONLY | (flag_execstack ? SEC_CODE : 0));
1332
1333 }
1334 #endif
1335
1336 /* If we've been collecting dwarf2 .debug_line info, either for
1337 assembly debugging or on behalf of the compiler, emit it now. */
1338 dwarf2_finish ();
1339
1340 /* If we constructed dwarf2 .eh_frame info, either via .cfi
1341 directives from the user or by the backend, emit it now. */
1342 cfi_finish ();
1343
1344 keep_it = 0;
1345 if (seen_at_least_1_file ())
1346 {
1347 int n_warns, n_errs;
1348 char warn_msg[50];
1349 char err_msg[50];
1350
1351 write_object_file ();
1352
1353 n_warns = had_warnings ();
1354 n_errs = had_errors ();
1355
1356 if (n_warns == 1)
1357 sprintf (warn_msg, _("%d warning"), n_warns);
1358 else
1359 sprintf (warn_msg, _("%d warnings"), n_warns);
1360 if (n_errs == 1)
1361 sprintf (err_msg, _("%d error"), n_errs);
1362 else
1363 sprintf (err_msg, _("%d errors"), n_errs);
1364
1365 if (flag_fatal_warnings && n_warns != 0)
1366 {
1367 if (n_errs == 0)
1368 as_bad (_("%s, treating warnings as errors"), warn_msg);
1369 n_errs += n_warns;
1370 }
1371
1372 if (n_errs == 0)
1373 keep_it = 1;
1374 else if (flag_always_generate_output)
1375 {
1376 /* The -Z flag indicates that an object file should be generated,
1377 regardless of warnings and errors. */
1378 keep_it = 1;
1379 fprintf (stderr, _("%s, %s, generating bad object file\n"),
1380 err_msg, warn_msg);
1381 }
1382 }
1383
1384 fflush (stderr);
1385
1386 #ifndef NO_LISTING
1387 listing_print (listing_filename, argv_orig);
1388 #endif
1389
1390 input_scrub_end ();
1391
1392 END_PROGRESS (myname);
1393
1394 /* Use xexit instead of return, because under VMS environments they
1395 may not place the same interpretation on the value given. */
1396 if (had_errors () != 0)
1397 xexit (EXIT_FAILURE);
1398
1399 /* Only generate dependency file if assembler was successful. */
1400 print_dependencies ();
1401
1402 xexit (EXIT_SUCCESS);
1403 }
1404