as.c 42 KB

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