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