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