blob: 075cc2c260c8c545a7b5dd6295eb1d34e0667aa2 [file] [log] [blame]
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07001/* ----------------------------------------------------------------------- *
H. Peter Anvin323fcff2009-07-12 12:04:56 -07002 *
H. Peter Anvin3366e312018-02-07 14:14:36 -08003 * Copyright 1996-2018 The NASM Authors - All Rights Reserved
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07004 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00006 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07007 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
H. Peter Anvin323fcff2009-07-12 12:04:56 -070017 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -070018 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 * ----------------------------------------------------------------------- */
33
H. Peter Anvin323fcff2009-07-12 12:04:56 -070034/*
H. Peter Anvin9e6747c2009-06-28 17:13:04 -070035 * The Netwide Assembler main program module
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000036 */
37
H. Peter Anvinfe501952007-10-02 21:53:51 -070038#include "compiler.h"
39
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000040
41#include "nasm.h"
42#include "nasmlib.h"
H. Peter Anvin13506202018-11-28 14:55:58 -080043#include "nctype.h"
H. Peter Anvinb20bc732017-03-07 19:23:03 -080044#include "error.h"
H. Peter Anvin1803ded2008-06-09 17:32:43 -070045#include "saa.h"
H. Peter Anvinfcb89092008-06-09 17:40:16 -070046#include "raa.h"
H. Peter Anvinf6c9e652007-10-16 14:40:27 -070047#include "float.h"
H. Peter Anvin74cc5e52007-08-30 22:35:34 +000048#include "stdscan.h"
H. Peter Anvinaf535c12002-04-30 20:59:21 +000049#include "insns.h"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000050#include "preproc.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000051#include "parser.h"
H. Peter Anvin76690a12002-04-30 20:52:49 +000052#include "eval.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000053#include "assemble.h"
54#include "labels.h"
H. Peter Anvine1f985c2016-05-25 12:06:29 -070055#include "outform.h"
H. Peter Anvin6768eb72002-04-30 20:52:26 +000056#include "listing.h"
Cyrill Gorcunov08359152013-11-09 22:16:11 +040057#include "iflag.h"
H. Peter Anvin2bc0ab32016-03-08 02:17:36 -080058#include "ver.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000059
H. Peter Anvin31387b22010-07-15 18:28:52 -070060/*
61 * This is the maximum number of optimization passes to do. If we ever
62 * find a case where the optimizer doesn't naturally converge, we might
63 * have to drop this value so the assembler doesn't appear to just hang.
64 */
65#define MAX_OPTIMIZE (INT_MAX >> 1)
66
H. Peter Anvine2c80182005-01-15 22:15:51 +000067struct forwrefinfo { /* info held on forward refs. */
H. Peter Anvineba20a72002-04-30 20:53:55 +000068 int lineno;
69 int operand;
70};
71
H. Peter Anvin55568c12016-10-03 19:46:49 -070072static void parse_cmdline(int, char **, int);
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +030073static void assemble_file(const char *, struct strlist *);
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -080074static bool skip_this_pass(errflags severity);
75static void nasm_verror_asm(errflags severity, const char *fmt, va_list args);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000076static void usage(void);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -070077static void help(char xopt);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000078
H. Peter Anvin77016c82018-12-10 22:29:49 -080079struct error_format {
80 const char *beforeline; /* Before line number, if present */
81 const char *afterline; /* After line number, if present */
82 const char *beforemsg; /* Before actual message */
83};
84
85static const struct error_format errfmt_gnu = { ":", "", ": " };
86static const struct error_format errfmt_msvc = { "(", ")", " : " };
87static const struct error_format *errfmt = &errfmt_gnu;
H. Peter Anvin (Intel)374312c2018-12-14 00:17:13 -080088static struct strlist *warn_list;
H. Peter Anvin77016c82018-12-10 22:29:49 -080089
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -070090unsigned int debug_nasm; /* Debugging messages? */
91
H. Peter Anvin283b3fb2016-03-07 23:18:30 -080092static bool using_debug_info, opt_verbose_info;
93static const char *debug_format;
94
H. Peter Anvin3366e312018-02-07 14:14:36 -080095#ifndef ABORT_ON_PANIC
96# define ABORT_ON_PANIC 0
97#endif
98static bool abort_on_panic = ABORT_ON_PANIC;
H. Peter Anvin29695c82018-06-14 17:04:32 -070099static bool keep_all;
H. Peter Anvin3366e312018-02-07 14:14:36 -0800100
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700101bool tasm_compatible_mode = false;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800102enum pass_type _pass_type;
103const char * const _pass_types[] =
104{
105 "init", "first", "optimize", "stabilize", "final"
106};
107int64_t _passn;
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +0000108int globalrel = 0;
Jin Kyu Songb287ff02013-12-04 20:05:55 -0800109int globalbnd = 0;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000110
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700111struct compile_time official_compile_time;
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800112
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800113const char *inname;
114const char *outname;
115static const char *listname;
116static const char *errname;
117
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -0700118static uint64_t list_options;
119uint64_t active_list_options; /* Set during the final pass only */
120
H. Peter Anvina3d96d02018-06-15 17:51:39 -0700121static int64_t globallineno; /* for forward-reference tracking */
Chang S. Baef0ceb1e2018-05-02 08:07:53 -0700122
H. Peter Anvin338656c2016-02-17 20:59:22 -0800123const struct ofmt *ofmt = &OF_DEFAULT;
124const struct ofmt_alias *ofmt_alias = NULL;
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400125const struct dfmt *dfmt;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000126
H. Peter Anvin (Intel)3b91f4c2018-12-13 13:55:25 -0800127FILE *error_file; /* Where to write error messages */
H. Peter Anvin620515a2002-04-30 20:57:38 +0000128
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400129FILE *ofile = NULL;
Chang S. Baea5786342018-08-15 23:22:21 +0300130struct optimization optimizing =
131 { MAX_OPTIMIZE, OPTIM_ALL_ENABLED }; /* number of optimization passes to take */
Martin Lindhe8cc93f52016-11-16 16:48:13 +0100132static int cmd_sb = 16; /* by default */
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400133
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800134iflag_t cpu;
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400135static iflag_t cmd_cpu;
136
H. Peter Anvincd7893d2016-02-18 01:25:46 -0800137struct location location;
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800138bool in_absolute; /* Flag we are in ABSOLUTE seg */
139struct location absolute; /* Segment/offset inside ABSOLUTE */
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000140
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000141static struct RAA *offsets;
H. Peter Anvinea838272002-04-30 20:51:53 +0000142
H. Peter Anvine2c80182005-01-15 22:15:51 +0000143static struct SAA *forwrefs; /* keep track of forward references */
H. Peter Anvin9d637df2007-10-04 13:42:56 -0700144static const struct forwrefinfo *forwref;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000145
H. Peter Anvine7469712016-02-18 02:20:59 -0800146static const struct preproc_ops *preproc;
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +0300147static struct strlist *include_path;
H. Peter Anvin (Intel)800c1682018-12-14 12:22:11 -0800148bool pp_noline; /* Ignore %line directives */
Cyrill Gorcunov86b2ad02011-07-01 10:38:25 +0400149
H. Peter Anvin34754622018-11-28 12:36:53 -0800150#define OP_NORMAL (1U << 0)
151#define OP_PREPROCESS (1U << 1)
152#define OP_DEPEND (1U << 2)
Cyrill Gorcunove9fc88c2014-06-23 02:22:02 +0400153
154static unsigned int operating_mode;
Cyrill Gorcunov3ed32cb2014-06-22 20:53:12 +0400155
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700156/* Dependency flags */
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700157static bool depend_emit_phony = false;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700158static bool depend_missing_ok = false;
159static const char *depend_target = NULL;
160static const char *depend_file = NULL;
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +0300161struct strlist *depend_list;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000162
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700163static bool want_usage;
164static bool terminate_after_phase;
H. Peter Anvin130736c2016-02-17 20:27:41 -0800165bool user_nolist = false;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000166
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700167static char *quote_for_pmake(const char *str);
168static char *quote_for_wmake(const char *str);
169static char *(*quote_for_make)(const char *) = quote_for_pmake;
H. Peter Anvin55340992012-09-09 17:09:00 -0700170
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700171/*
172 * Execution limits that can be set via a command-line option or %pragma
173 */
174
H. Peter Anvina3d96d02018-06-15 17:51:39 -0700175#define LIMIT_MAX_VAL (INT64_MAX >> 1) /* Effectively unlimited */
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700176
H. Peter Anvina3d96d02018-06-15 17:51:39 -0700177int64_t nasm_limit[LIMIT_MAX+1] =
178{ LIMIT_MAX_VAL, 1000, 1000000, 1000000, 1000000, 2000000000 };
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700179
180struct limit_info {
181 const char *name;
182 const char *help;
183};
184static const struct limit_info limit_info[LIMIT_MAX+1] = {
185 { "passes", "total number of passes" },
186 { "stalled-passes", "number of passes without forward progress" },
187 { "macro-levels", "levels of macro expansion"},
188 { "rep", "%rep count" },
H. Peter Anvina3d96d02018-06-15 17:51:39 -0700189 { "eval", "expression evaluation descent"},
190 { "lines", "total source lines processed"}
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700191};
192
H. Peter Anvinc805fd72018-06-12 14:23:05 -0700193enum directive_result
194nasm_set_limit(const char *limit, const char *valstr)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700195{
196 int i;
197 int64_t val;
198 bool rn_error;
H. Peter Anvinc805fd72018-06-12 14:23:05 -0700199 int errlevel;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700200
201 for (i = 0; i <= LIMIT_MAX; i++) {
202 if (!nasm_stricmp(limit, limit_info[i].name))
203 break;
204 }
205 if (i > LIMIT_MAX) {
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800206 if (not_started())
H. Peter Anvin (Intel)c3c6cea2018-12-14 13:44:35 -0800207 errlevel = ERR_WARNING|WARN_OTHER|ERR_USAGE;
H. Peter Anvinc805fd72018-06-12 14:23:05 -0700208 else
H. Peter Anvinfdeb3b02019-06-06 20:53:17 -0700209 errlevel = ERR_WARNING|WARN_PRAGMA_UNKNOWN;
H. Peter Anvinc805fd72018-06-12 14:23:05 -0700210 nasm_error(errlevel, "unknown limit: `%s'", limit);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700211 return DIRR_ERROR;
212 }
213
214 if (!nasm_stricmp(valstr, "unlimited")) {
215 val = LIMIT_MAX_VAL;
216 } else {
217 val = readnum(valstr, &rn_error);
218 if (rn_error || val < 0) {
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800219 if (not_started())
H. Peter Anvin (Intel)c3c6cea2018-12-14 13:44:35 -0800220 errlevel = ERR_WARNING|WARN_OTHER|ERR_USAGE;
H. Peter Anvinc805fd72018-06-12 14:23:05 -0700221 else
H. Peter Anvinfdeb3b02019-06-06 20:53:17 -0700222 errlevel = ERR_WARNING|WARN_PRAGMA_BAD;
H. Peter Anvinc805fd72018-06-12 14:23:05 -0700223 nasm_error(errlevel, "invalid limit value: `%s'", limit);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700224 return DIRR_ERROR;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700225 }
H. Peter Anvinc805fd72018-06-12 14:23:05 -0700226 if (val > LIMIT_MAX_VAL)
227 val = LIMIT_MAX_VAL;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700228 }
229
230 nasm_limit[i] = val;
231 return DIRR_OK;
232}
233
H. Peter Anvin892c4812018-05-30 14:43:46 -0700234int64_t switch_segment(int32_t segment)
Cyrill Gorcunov190232f2013-02-15 12:35:04 +0400235{
H. Peter Anvin892c4812018-05-30 14:43:46 -0700236 location.segment = segment;
237 if (segment == NO_SEG) {
238 location.offset = absolute.offset;
239 in_absolute = true;
240 } else {
241 location.offset = raa_read(offsets, segment);
242 in_absolute = false;
243 }
244 return location.offset;
Cyrill Gorcunov190232f2013-02-15 12:35:04 +0400245}
246
247static void set_curr_offs(int64_t l_off)
248{
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800249 if (in_absolute)
250 absolute.offset = l_off;
Cyrill Gorcunov190232f2013-02-15 12:35:04 +0400251 else
252 offsets = raa_write(offsets, location.segment, l_off);
253}
254
H. Peter Anvin892c4812018-05-30 14:43:46 -0700255static void increment_offset(int64_t delta)
256{
257 if (unlikely(delta == 0))
258 return;
259
260 location.offset += delta;
261 set_curr_offs(location.offset);
262}
263
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000264static void nasm_fputs(const char *line, FILE * outfile)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000265{
H. Peter Anvin310b3e12002-05-14 22:38:55 +0000266 if (outfile) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000267 fputs(line, outfile);
H. Peter Anvind1fb15c2007-11-13 09:37:59 -0800268 putc('\n', outfile);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000269 } else
H. Peter Anvine2c80182005-01-15 22:15:51 +0000270 puts(line);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000271}
272
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800273/*
274 * Define system-defined macros that are not part of
275 * macros/standard.mac.
276 */
277static void define_macros(void)
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800278{
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700279 const struct compile_time * const oct = &official_compile_time;
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800280 char temp[128];
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800281
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700282 if (oct->have_local) {
283 strftime(temp, sizeof temp, "__DATE__=\"%Y-%m-%d\"", &oct->local);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400284 preproc->pre_define(temp);
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700285 strftime(temp, sizeof temp, "__DATE_NUM__=%Y%m%d", &oct->local);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400286 preproc->pre_define(temp);
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700287 strftime(temp, sizeof temp, "__TIME__=\"%H:%M:%S\"", &oct->local);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400288 preproc->pre_define(temp);
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700289 strftime(temp, sizeof temp, "__TIME_NUM__=%H%M%S", &oct->local);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400290 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800291 }
292
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700293 if (oct->have_gm) {
294 strftime(temp, sizeof temp, "__UTC_DATE__=\"%Y-%m-%d\"", &oct->gm);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400295 preproc->pre_define(temp);
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700296 strftime(temp, sizeof temp, "__UTC_DATE_NUM__=%Y%m%d", &oct->gm);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400297 preproc->pre_define(temp);
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700298 strftime(temp, sizeof temp, "__UTC_TIME__=\"%H:%M:%S\"", &oct->gm);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400299 preproc->pre_define(temp);
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700300 strftime(temp, sizeof temp, "__UTC_TIME_NUM__=%H%M%S", &oct->gm);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400301 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800302 }
H. Peter Anvind85d2502008-05-04 17:53:31 -0700303
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700304 if (oct->have_posix) {
305 snprintf(temp, sizeof temp, "__POSIX_TIME__=%"PRId64, oct->posix);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400306 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800307 }
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800308
Cyrill Gorcunovc1936da2011-04-06 18:32:15 +0400309 /*
310 * In case if output format is defined by alias
311 * we have to put shortname of the alias itself here
312 * otherwise ABI backward compatibility gets broken.
313 */
Cyrill Gorcunov69ce7502010-07-12 15:19:17 +0400314 snprintf(temp, sizeof(temp), "__OUTPUT_FORMAT__=%s",
Cyrill Gorcunovc1936da2011-04-06 18:32:15 +0400315 ofmt_alias ? ofmt_alias->shortname : ofmt->shortname);
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +0400316 preproc->pre_define(temp);
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800317
318 /*
319 * Output-format specific macros.
320 */
321 if (ofmt->stdmac)
322 preproc->extra_stdmac(ofmt->stdmac);
323
324 /*
325 * Debug format, if any
326 */
327 if (dfmt != &null_debug_form) {
328 snprintf(temp, sizeof(temp), "__DEBUG_FORMAT__=%s", dfmt->shortname);
329 preproc->pre_define(temp);
330 }
331}
332
333/*
334 * Initialize the preprocessor, set up the include path, and define
335 * the system-included macros. This is called between passes 1 and 2
336 * of parsing the command options; ofmt and dfmt are defined at this
337 * point.
338 *
339 * Command-line specified preprocessor directives (-p, -d, -u,
340 * --pragma, --before) are processed after this function.
341 */
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +0300342static void preproc_init(struct strlist *ipath)
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800343{
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800344 preproc->init();
345 define_macros();
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +0300346 preproc->include_path(ipath);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800347}
348
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +0300349static void emit_dependencies(struct strlist *list)
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700350{
351 FILE *deps;
352 int linepos, len;
H. Peter Anvinf05034f2017-08-16 22:08:36 -0700353 bool wmake = (quote_for_make == quote_for_wmake);
354 const char *wrapstr, *nulltarget;
H. Peter Anvin (Intel)64471092018-12-11 13:06:14 -0800355 const struct strlist_entry *l;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -0700356
357 if (!list)
358 return;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700359
H. Peter Anvinf05034f2017-08-16 22:08:36 -0700360 wrapstr = wmake ? " &\n " : " \\\n ";
361 nulltarget = wmake ? "\t%null\n" : "";
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700362
363 if (depend_file && strcmp(depend_file, "-")) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -0700364 deps = nasm_open_write(depend_file, NF_TEXT);
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400365 if (!deps) {
H. Peter Anvinc55702e2018-12-10 22:06:15 -0800366 nasm_nonfatal("unable to write dependency file `%s'", depend_file);
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400367 return;
368 }
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700369 } else {
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400370 deps = stdout;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700371 }
H. Peter Anvin323fcff2009-07-12 12:04:56 -0700372
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700373 linepos = fprintf(deps, "%s :", depend_target);
H. Peter Anvin (Intel)64471092018-12-11 13:06:14 -0800374 strlist_for_each(l, list) {
H. Peter Anvin55340992012-09-09 17:09:00 -0700375 char *file = quote_for_make(l->str);
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400376 len = strlen(file);
377 if (linepos + len > 62 && linepos > 1) {
H. Peter Anvinf05034f2017-08-16 22:08:36 -0700378 fputs(wrapstr, deps);
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400379 linepos = 1;
380 }
381 fprintf(deps, " %s", file);
382 linepos += len+1;
H. Peter Anvin55340992012-09-09 17:09:00 -0700383 nasm_free(file);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700384 }
385 fprintf(deps, "\n\n");
H. Peter Anvin323fcff2009-07-12 12:04:56 -0700386
H. Peter Anvin (Intel)64471092018-12-11 13:06:14 -0800387 strlist_for_each(l, list) {
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700388 if (depend_emit_phony) {
389 char *file = quote_for_make(l->str);
H. Peter Anvinf05034f2017-08-16 22:08:36 -0700390 fprintf(deps, "%s :\n%s\n", file, nulltarget);
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700391 nasm_free(file);
392 }
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700393 }
394
H. Peter Anvin (Intel)374312c2018-12-14 00:17:13 -0800395 strlist_free(&list);
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -0700396
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700397 if (deps != stdout)
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400398 fclose(deps);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700399}
400
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700401/* Convert a struct tm to a POSIX-style time constant */
402static int64_t make_posix_time(const struct tm *tm)
403{
404 int64_t t;
405 int64_t y = tm->tm_year;
406
407 /* See IEEE 1003.1:2004, section 4.14 */
408
409 t = (y-70)*365 + (y-69)/4 - (y-1)/100 + (y+299)/400;
410 t += tm->tm_yday;
411 t *= 24;
412 t += tm->tm_hour;
413 t *= 60;
414 t += tm->tm_min;
415 t *= 60;
416 t += tm->tm_sec;
417
418 return t;
419}
420
421static void timestamp(void)
422{
423 struct compile_time * const oct = &official_compile_time;
424 const struct tm *tp, *best_gm;
425
426 time(&oct->t);
427
428 best_gm = NULL;
429
430 tp = localtime(&oct->t);
431 if (tp) {
432 oct->local = *tp;
433 best_gm = &oct->local;
434 oct->have_local = true;
435 }
436
437 tp = gmtime(&oct->t);
438 if (tp) {
439 oct->gm = *tp;
440 best_gm = &oct->gm;
441 oct->have_gm = true;
442 if (!oct->have_local)
443 oct->local = oct->gm;
444 } else {
445 oct->gm = oct->local;
446 }
447
448 if (best_gm) {
449 oct->posix = make_posix_time(best_gm);
450 oct->have_posix = true;
451 }
452}
453
H. Peter Anvin038d8612007-04-12 16:54:50 +0000454int main(int argc, char **argv)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000455{
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700456 timestamp();
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800457
H. Peter Anvin (Intel)3b91f4c2018-12-13 13:55:25 -0800458 error_file = stderr;
459
H. Peter Anvina7ecf262018-02-06 14:43:07 -0800460 iflag_set_default_cpu(&cpu);
461 iflag_set_default_cpu(&cmd_cpu);
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400462
H. Peter Anvin (Intel)7bb13ea2018-12-13 22:48:14 -0800463 include_path = strlist_alloc(true);
Cyrill Gorcunove3588512018-11-13 01:09:27 +0300464
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800465 _pass_type = PASS_INIT;
466 _passn = 0;
467
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700468 want_usage = terminate_after_phase = false;
H. Peter Anvin77016c82018-12-10 22:29:49 -0800469 nasm_set_verror(nasm_verror_asm);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000470
H. Peter Anvin13506202018-11-28 14:55:58 -0800471 nasm_ctype_init();
H. Peter Anvin274cda82016-05-10 02:56:29 -0700472 src_init();
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -0700473
H. Peter Anvin, Intel87d9e622018-06-25 12:58:49 -0700474 /*
475 * We must call init_labels() before the command line parsing,
476 * because we may be setting prefixes/suffixes from the command
477 * line.
478 */
479 init_labels();
480
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000481 offsets = raa_init();
Keith Kaniosb7a89542007-04-12 02:40:54 +0000482 forwrefs = saa_init((int32_t)sizeof(struct forwrefinfo));
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000483
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000484 preproc = &nasmpp;
Cyrill Gorcunov3ed32cb2014-06-22 20:53:12 +0400485 operating_mode = OP_NORMAL;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000486
H. Peter Anvin55568c12016-10-03 19:46:49 -0700487 parse_cmdline(argc, argv, 1);
488 if (terminate_after_phase) {
489 if (want_usage)
490 usage();
491 return 1;
492 }
493
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800494 /* At this point we have ofmt and the name of the desired debug format */
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800495 if (!using_debug_info) {
496 /* No debug info, redirect to the null backend (empty stubs) */
H. Peter Anvina7bc15d2016-02-17 20:55:08 -0800497 dfmt = &null_debug_form;
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800498 } else if (!debug_format) {
499 /* Default debug format for this backend */
Cyrill Gorcunov988cc122018-12-15 23:44:46 +0300500 dfmt = ofmt->default_dfmt;
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800501 } else {
502 dfmt = dfmt_find(ofmt, debug_format);
503 if (!dfmt) {
H. Peter Anvin77016c82018-12-10 22:29:49 -0800504 nasm_fatalf(ERR_USAGE, "unrecognized debug format `%s' for output format `%s'",
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800505 debug_format, ofmt->shortname);
506 }
507 }
H. Peter Anvinbb88d012003-09-10 23:34:23 +0000508
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +0300509 preproc_init(include_path);
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800510
511 parse_cmdline(argc, argv, 2);
512 if (terminate_after_phase) {
513 if (want_usage)
514 usage();
515 return 1;
516 }
517
518 /* Save away the default state of warnings */
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -0800519 init_warnings();
H. Peter Anvin76690a12002-04-30 20:52:49 +0000520
H. Peter Anvin34754622018-11-28 12:36:53 -0800521 /* Dependency filename if we are also doing other things */
522 if (!depend_file && (operating_mode & ~OP_DEPEND)) {
523 if (outname)
524 depend_file = nasm_strcat(outname, ".d");
525 else
526 depend_file = filename_set_extension(inname, ".d");
527 }
528
Cyrill Gorcunov69bb0522018-09-22 13:46:45 +0300529 /*
530 * If no output file name provided and this
H. Peter Anvin34754622018-11-28 12:36:53 -0800531 * is preprocess mode, we're perfectly
Cyrill Gorcunovda3780d2018-09-22 14:10:36 +0300532 * fine to output into stdout.
Cyrill Gorcunov69bb0522018-09-22 13:46:45 +0300533 */
H. Peter Anvin (Intel)7b6371b2018-11-20 10:56:57 -0800534 if (!outname && !(operating_mode & OP_PREPROCESS)) {
535 outname = filename_set_extension(inname, ofmt->extension);
536 if (!strcmp(outname, inname)) {
537 outname = "nasm.out";
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -0800538 nasm_warn(WARN_OTHER, "default output file same as input, using `%s' for output\n", outname);
H. Peter Anvin (Intel)7b6371b2018-11-20 10:56:57 -0800539 }
Cyrill Gorcunov69bb0522018-09-22 13:46:45 +0300540 }
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800541
H. Peter Anvin (Intel)7bb13ea2018-12-13 22:48:14 -0800542 depend_list = (operating_mode & OP_DEPEND) ? strlist_alloc(true) : NULL;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700543
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700544 if (!depend_target)
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400545 depend_target = quote_for_make(outname);
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700546
H. Peter Anvin34754622018-11-28 12:36:53 -0800547 if (!(operating_mode & (OP_PREPROCESS|OP_NORMAL))) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000548 char *line;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700549
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400550 if (depend_missing_ok)
551 preproc->include_path(NULL); /* "assume generated" */
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700552
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800553 preproc->reset(inname, PP_DEPS, depend_list);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000554 ofile = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000555 while ((line = preproc->getline()))
556 nasm_free(line);
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800557 preproc->cleanup_pass();
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -0800558 reset_warnings();
Cyrill Gorcunove9fc88c2014-06-23 02:22:02 +0400559 } else if (operating_mode & OP_PREPROCESS) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000560 char *line;
H. Peter Anvin274cda82016-05-10 02:56:29 -0700561 const char *file_name = NULL;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000562 int32_t prior_linnum = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000563 int lineinc = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000564
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800565 if (outname) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -0700566 ofile = nasm_open_write(outname, NF_TEXT);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000567 if (!ofile)
H. Peter Anvin77016c82018-12-10 22:29:49 -0800568 nasm_fatal("unable to open output file `%s'", outname);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000569 } else
570 ofile = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000571
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700572 location.known = false;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000573
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800574 _pass_type = PASS_FIRST; /* We emulate this assembly pass */
575 preproc->reset(inname, PP_PREPROC, depend_list);
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800576
H. Peter Anvine2c80182005-01-15 22:15:51 +0000577 while ((line = preproc->getline())) {
578 /*
579 * We generate %line directives if needed for later programs
580 */
Keith Kaniosb7a89542007-04-12 02:40:54 +0000581 int32_t linnum = prior_linnum += lineinc;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000582 int altline = src_get(&linnum, &file_name);
583 if (altline) {
584 if (altline == 1 && lineinc == 1)
585 nasm_fputs("", ofile);
586 else {
587 lineinc = (altline != -1 || lineinc != 1);
588 fprintf(ofile ? ofile : stdout,
Keith Kanios93f2e9a2007-04-14 00:10:59 +0000589 "%%line %"PRId32"+%d %s\n", linnum, lineinc,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000590 file_name);
591 }
592 prior_linnum = linnum;
593 }
594 nasm_fputs(line, ofile);
595 nasm_free(line);
596 }
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800597 preproc->cleanup_pass();
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -0800598 reset_warnings();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000599 if (ofile)
600 fclose(ofile);
H. Peter Anvin29695c82018-06-14 17:04:32 -0700601 if (ofile && terminate_after_phase && !keep_all)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000602 remove(outname);
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400603 ofile = NULL;
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400604 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000605
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400606 if (operating_mode & OP_NORMAL) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -0700607 ofile = nasm_open_write(outname, (ofmt->flags & OFMT_TEXT) ? NF_TEXT : NF_BINARY);
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400608 if (!ofile)
H. Peter Anvinc55702e2018-12-10 22:06:15 -0800609 nasm_fatal("unable to open output file `%s'", outname);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000610
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400611 ofmt->init();
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400612 dfmt->init();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000613
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -0700614 assemble_file(inname, depend_list);
Victor van den Elzenc82c3722008-06-04 15:24:20 +0200615
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400616 if (!terminate_after_phase) {
H. Peter Anvin477ae442016-03-07 22:53:06 -0800617 ofmt->cleanup();
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400618 cleanup_labels();
619 fflush(ofile);
H. Peter Anvinc55702e2018-12-10 22:06:15 -0800620 if (ferror(ofile))
621 nasm_nonfatal("write error on output file `%s'", outname);
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400622 }
623
624 if (ofile) {
625 fclose(ofile);
H. Peter Anvin29695c82018-06-14 17:04:32 -0700626 if (terminate_after_phase && !keep_all)
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400627 remove(outname);
628 ofile = NULL;
629 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000630 }
H. Peter Anvin734b1882002-04-30 21:01:08 +0000631
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800632 preproc->cleanup_session();
633
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700634 if (depend_list && !terminate_after_phase)
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400635 emit_dependencies(depend_list);
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700636
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000637 if (want_usage)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000638 usage();
H. Peter Anvin734b1882002-04-30 21:01:08 +0000639
H. Peter Anvine2c80182005-01-15 22:15:51 +0000640 raa_free(offsets);
641 saa_free(forwrefs);
642 eval_cleanup();
H. Peter Anvin74cc5e52007-08-30 22:35:34 +0000643 stdscan_cleanup();
H. Peter Anvin274cda82016-05-10 02:56:29 -0700644 src_free();
H. Peter Anvin (Intel)374312c2018-12-14 00:17:13 -0800645 strlist_free(&include_path);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000646
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700647 return terminate_after_phase;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000648}
649
H. Peter Anvineba20a72002-04-30 20:53:55 +0000650/*
651 * Get a parameter for a command line option.
652 * First arg must be in the form of e.g. -f...
653 */
H. Peter Anvin423e3812007-11-15 17:12:29 -0800654static char *get_param(char *p, char *q, bool *advance)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000655{
H. Peter Anvin423e3812007-11-15 17:12:29 -0800656 *advance = false;
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +0400657 if (p[2]) /* the parameter's in the option */
658 return nasm_skip_spaces(p + 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000659 if (q && q[0]) {
H. Peter Anvin423e3812007-11-15 17:12:29 -0800660 *advance = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000661 return q;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000662 }
H. Peter Anvinc55702e2018-12-10 22:06:15 -0800663 nasm_nonfatalf(ERR_USAGE, "option `-%c' requires an argument", p[1]);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000664 return NULL;
665}
666
H. Peter Anvindc242712007-11-18 11:55:10 -0800667/*
668 * Copy a filename
669 */
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800670static void copy_filename(const char **dst, const char *src, const char *what)
H. Peter Anvindc242712007-11-18 11:55:10 -0800671{
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800672 if (*dst)
H. Peter Anvinc5136902018-06-15 18:20:17 -0700673 nasm_fatal("more than one %s file specified: %s\n", what, src);
H. Peter Anvindc242712007-11-18 11:55:10 -0800674
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800675 *dst = nasm_strdup(src);
H. Peter Anvindc242712007-11-18 11:55:10 -0800676}
677
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700678/*
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700679 * Convert a string to a POSIX make-safe form
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700680 */
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700681static char *quote_for_pmake(const char *str)
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700682{
683 const char *p;
684 char *os, *q;
685
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400686 size_t n = 1; /* Terminating zero */
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700687 size_t nbs = 0;
688
689 if (!str)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400690 return NULL;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700691
692 for (p = str; *p; p++) {
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400693 switch (*p) {
694 case ' ':
695 case '\t':
696 /* Convert N backslashes + ws -> 2N+1 backslashes + ws */
697 n += nbs + 2;
698 nbs = 0;
699 break;
700 case '$':
701 case '#':
702 nbs = 0;
703 n += 2;
704 break;
705 case '\\':
706 nbs++;
707 n++;
708 break;
709 default:
710 nbs = 0;
711 n++;
712 break;
713 }
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700714 }
715
716 /* Convert N backslashes at the end of filename to 2N backslashes */
717 if (nbs)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400718 n += nbs;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700719
720 os = q = nasm_malloc(n);
721
722 nbs = 0;
723 for (p = str; *p; p++) {
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400724 switch (*p) {
725 case ' ':
726 case '\t':
727 while (nbs--)
728 *q++ = '\\';
729 *q++ = '\\';
730 *q++ = *p;
731 break;
732 case '$':
733 *q++ = *p;
734 *q++ = *p;
735 nbs = 0;
736 break;
737 case '#':
738 *q++ = '\\';
739 *q++ = *p;
740 nbs = 0;
741 break;
742 case '\\':
743 *q++ = *p;
744 nbs++;
745 break;
746 default:
747 *q++ = *p;
748 nbs = 0;
749 break;
750 }
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700751 }
752 while (nbs--)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400753 *q++ = '\\';
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700754
755 *q = '\0';
756
757 return os;
758}
759
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700760/*
761 * Convert a string to a Watcom make-safe form
762 */
763static char *quote_for_wmake(const char *str)
764{
765 const char *p;
766 char *os, *q;
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700767 bool quote = false;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700768
769 size_t n = 1; /* Terminating zero */
770
771 if (!str)
772 return NULL;
773
774 for (p = str; *p; p++) {
775 switch (*p) {
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700776 case ' ':
777 case '\t':
H. Peter Anvin3e30c322017-08-16 22:20:36 -0700778 case '&':
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700779 quote = true;
780 n++;
781 break;
782 case '\"':
783 quote = true;
784 n += 2;
785 break;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700786 case '$':
787 case '#':
788 n += 2;
789 break;
790 default:
791 n++;
792 break;
793 }
794 }
795
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700796 if (quote)
797 n += 2;
798
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700799 os = q = nasm_malloc(n);
800
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700801 if (quote)
802 *q++ = '\"';
803
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700804 for (p = str; *p; p++) {
805 switch (*p) {
806 case '$':
807 case '#':
808 *q++ = '$';
809 *q++ = *p;
810 break;
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700811 case '\"':
812 *q++ = *p;
813 *q++ = *p;
814 break;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700815 default:
816 *q++ = *p;
817 break;
818 }
819 }
820
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700821 if (quote)
822 *q++ = '\"';
823
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700824 *q = '\0';
825
826 return os;
827}
828
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100829enum text_options {
H. Peter Anvin3366e312018-02-07 14:14:36 -0800830 OPT_BOGUS,
831 OPT_VERSION,
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700832 OPT_HELP,
H. Peter Anvin3366e312018-02-07 14:14:36 -0800833 OPT_ABORT_ON_PANIC,
H. Peter Anvin05990342018-06-11 13:32:42 -0700834 OPT_MANGLE,
835 OPT_INCLUDE,
836 OPT_PRAGMA,
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700837 OPT_BEFORE,
H. Peter Anvin29695c82018-06-14 17:04:32 -0700838 OPT_LIMIT,
H. Peter Anvin (Intel)800c1682018-12-14 12:22:11 -0800839 OPT_KEEP_ALL,
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -0700840 OPT_NO_LINE,
841 OPT_DEBUG
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100842};
H. Peter Anvin3366e312018-02-07 14:14:36 -0800843struct textargs {
844 const char *label;
845 enum text_options opt;
846 bool need_arg;
H. Peter Anvin98578072018-06-01 18:02:54 -0700847 int pvt;
H. Peter Anvin3366e312018-02-07 14:14:36 -0800848};
H. Peter Anvin2530a102016-02-18 02:28:15 -0800849static const struct textargs textopts[] = {
H. Peter Anvin98578072018-06-01 18:02:54 -0700850 {"v", OPT_VERSION, false, 0},
851 {"version", OPT_VERSION, false, 0},
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700852 {"help", OPT_HELP, false, 0},
H. Peter Anvin98578072018-06-01 18:02:54 -0700853 {"abort-on-panic", OPT_ABORT_ON_PANIC, false, 0},
854 {"prefix", OPT_MANGLE, true, LM_GPREFIX},
855 {"postfix", OPT_MANGLE, true, LM_GSUFFIX},
856 {"gprefix", OPT_MANGLE, true, LM_GPREFIX},
857 {"gpostfix", OPT_MANGLE, true, LM_GSUFFIX},
858 {"lprefix", OPT_MANGLE, true, LM_LPREFIX},
859 {"lpostfix", OPT_MANGLE, true, LM_LSUFFIX},
H. Peter Anvin05990342018-06-11 13:32:42 -0700860 {"include", OPT_INCLUDE, true, 0},
861 {"pragma", OPT_PRAGMA, true, 0},
862 {"before", OPT_BEFORE, true, 0},
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700863 {"limit-", OPT_LIMIT, true, 0},
H. Peter Anvin29695c82018-06-14 17:04:32 -0700864 {"keep-all", OPT_KEEP_ALL, false, 0},
H. Peter Anvin (Intel)800c1682018-12-14 12:22:11 -0800865 {"no-line", OPT_NO_LINE, false, 0},
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -0700866 {"debug", OPT_DEBUG, false, 0},
H. Peter Anvin98578072018-06-01 18:02:54 -0700867 {NULL, OPT_BOGUS, false, 0}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000868};
869
Cyrill Gorcunove7438432014-05-09 22:34:34 +0400870static void show_version(void)
871{
872 printf("NASM version %s compiled on %s%s\n",
873 nasm_version, nasm_date, nasm_compile_options);
874 exit(0);
875}
876
H. Peter Anvin423e3812007-11-15 17:12:29 -0800877static bool stopoptions = false;
H. Peter Anvin55568c12016-10-03 19:46:49 -0700878static bool process_arg(char *p, char *q, int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000879{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000880 char *param;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800881 bool advance = false;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000882
883 if (!p || !p[0])
H. Peter Anvin423e3812007-11-15 17:12:29 -0800884 return false;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000885
H. Peter Anvine2c80182005-01-15 22:15:51 +0000886 if (p[0] == '-' && !stopoptions) {
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -0700887 if (strchr("oOfpPdDiIlLFXuUZwW", p[1])) {
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400888 /* These parameters take values */
889 if (!(param = get_param(p, q, &advance)))
890 return advance;
891 }
H. Peter Anvin423e3812007-11-15 17:12:29 -0800892
H. Peter Anvine2c80182005-01-15 22:15:51 +0000893 switch (p[1]) {
894 case 's':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700895 if (pass == 1)
896 error_file = stdout;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000897 break;
H. Peter Anvin70653092007-10-19 14:42:29 -0700898
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400899 case 'o': /* output file */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700900 if (pass == 2)
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800901 copy_filename(&outname, param, "output");
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400902 break;
H. Peter Anvinfd7dd112007-10-10 14:06:59 -0700903
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400904 case 'f': /* output format */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700905 if (pass == 1) {
906 ofmt = ofmt_find(param, &ofmt_alias);
907 if (!ofmt) {
H. Peter Anvin77016c82018-12-10 22:29:49 -0800908 nasm_fatalf(ERR_USAGE, "unrecognised output format `%s' - use -hf for a list", param);
H. Peter Anvin55568c12016-10-03 19:46:49 -0700909 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400910 }
911 break;
H. Peter Anvin70653092007-10-19 14:42:29 -0700912
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400913 case 'O': /* Optimization level */
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800914 if (pass == 1) {
H. Peter Anvin55568c12016-10-03 19:46:49 -0700915 int opt;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700916
H. Peter Anvin55568c12016-10-03 19:46:49 -0700917 if (!*param) {
918 /* Naked -O == -Ox */
Chang S. Baea5786342018-08-15 23:22:21 +0300919 optimizing.level = MAX_OPTIMIZE;
H. Peter Anvin55568c12016-10-03 19:46:49 -0700920 } else {
921 while (*param) {
922 switch (*param) {
923 case '0': case '1': case '2': case '3': case '4':
924 case '5': case '6': case '7': case '8': case '9':
925 opt = strtoul(param, &param, 10);
H. Peter Anvind85d2502008-05-04 17:53:31 -0700926
Chang S. Baea5786342018-08-15 23:22:21 +0300927 /* -O0 -> optimizing.level == -1, 0.98 behaviour */
928 /* -O1 -> optimizing.level == 0, 0.98.09 behaviour */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700929 if (opt < 2)
Chang S. Baea5786342018-08-15 23:22:21 +0300930 optimizing.level = opt - 1;
H. Peter Anvin55568c12016-10-03 19:46:49 -0700931 else
Chang S. Baea5786342018-08-15 23:22:21 +0300932 optimizing.level = opt;
H. Peter Anvin55568c12016-10-03 19:46:49 -0700933 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700934
H. Peter Anvin55568c12016-10-03 19:46:49 -0700935 case 'v':
936 case '+':
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400937 param++;
938 opt_verbose_info = true;
939 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700940
H. Peter Anvin55568c12016-10-03 19:46:49 -0700941 case 'x':
942 param++;
Chang S. Baea5786342018-08-15 23:22:21 +0300943 optimizing.level = MAX_OPTIMIZE;
H. Peter Anvin55568c12016-10-03 19:46:49 -0700944 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700945
H. Peter Anvin55568c12016-10-03 19:46:49 -0700946 default:
H. Peter Anvinc5136902018-06-15 18:20:17 -0700947 nasm_fatal("unknown optimization option -O%c\n",
H. Peter Anvin55568c12016-10-03 19:46:49 -0700948 *param);
949 break;
950 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400951 }
Chang S. Baea5786342018-08-15 23:22:21 +0300952 if (optimizing.level > MAX_OPTIMIZE)
953 optimizing.level = MAX_OPTIMIZE;
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400954 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400955 }
956 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800957
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400958 case 'p': /* pre-include */
959 case 'P':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700960 if (pass == 2)
961 preproc->pre_include(param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400962 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800963
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400964 case 'd': /* pre-define */
965 case 'D':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700966 if (pass == 2)
967 preproc->pre_define(param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400968 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800969
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400970 case 'u': /* un-define */
971 case 'U':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700972 if (pass == 2)
973 preproc->pre_undefine(param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400974 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800975
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400976 case 'i': /* include search path */
977 case 'I':
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800978 if (pass == 1)
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +0300979 strlist_add(include_path, param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400980 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800981
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400982 case 'l': /* listing file */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700983 if (pass == 2)
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800984 copy_filename(&listname, param, "listing");
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400985 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800986
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -0700987 case 'L': /* listing options */
988 if (pass == 2) {
989 while (*param) {
990 unsigned int p = *param - '@';
991 if (p <= 63)
992 list_options |= (UINT64_C(1) << p);
993 param++;
994 }
995 }
996 break;
997
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400998 case 'Z': /* error messages file */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700999 if (pass == 1)
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001000 copy_filename(&errname, param, "error");
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001001 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001002
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001003 case 'F': /* specify debug format */
H. Peter Anvinbf6230b2018-11-11 13:25:16 -08001004 if (pass == 1) {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001005 using_debug_info = true;
1006 debug_format = param;
1007 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001008 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001009
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001010 case 'X': /* specify error reporting format */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001011 if (pass == 1) {
H. Peter Anvin77016c82018-12-10 22:29:49 -08001012 if (!nasm_stricmp("vc", param) || !nasm_stricmp("msvc", param) || !nasm_stricmp("ms", param))
1013 errfmt = &errfmt_msvc;
1014 else if (!nasm_stricmp("gnu", param) || !nasm_stricmp("gcc", param))
1015 errfmt = &errfmt_gnu;
H. Peter Anvin55568c12016-10-03 19:46:49 -07001016 else
H. Peter Anvin77016c82018-12-10 22:29:49 -08001017 nasm_fatalf(ERR_USAGE, "unrecognized error reporting format `%s'", param);
H. Peter Anvin55568c12016-10-03 19:46:49 -07001018 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001019 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001020
H. Peter Anvine2c80182005-01-15 22:15:51 +00001021 case 'g':
H. Peter Anvinbf6230b2018-11-11 13:25:16 -08001022 if (pass == 1) {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001023 using_debug_info = true;
1024 if (p[2])
1025 debug_format = nasm_skip_spaces(p + 2);
1026 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001027 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001028
H. Peter Anvine2c80182005-01-15 22:15:51 +00001029 case 'h':
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001030 help(p[2]);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001031 exit(0); /* never need usage message here */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001032 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001033
H. Peter Anvine2c80182005-01-15 22:15:51 +00001034 case 'y':
1035 printf("\nvalid debug formats for '%s' output format are"
1036 " ('*' denotes default):\n", ofmt->shortname);
1037 dfmt_list(ofmt, stdout);
1038 exit(0);
1039 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001040
H. Peter Anvine2c80182005-01-15 22:15:51 +00001041 case 't':
H. Peter Anvin13506202018-11-28 14:55:58 -08001042 if (pass == 2) {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001043 tasm_compatible_mode = true;
H. Peter Anvin13506202018-11-28 14:55:58 -08001044 nasm_ctype_tasm_mode();
1045 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001046 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001047
H. Peter Anvine2c80182005-01-15 22:15:51 +00001048 case 'v':
Cyrill Gorcunove7438432014-05-09 22:34:34 +04001049 show_version();
H. Peter Anvine2c80182005-01-15 22:15:51 +00001050 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001051
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001052 case 'e': /* preprocess only */
1053 case 'E':
H. Peter Anvin55568c12016-10-03 19:46:49 -07001054 if (pass == 1)
1055 operating_mode = OP_PREPROCESS;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001056 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001057
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001058 case 'a': /* assemble only - don't preprocess */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001059 if (pass == 1)
1060 preproc = &preproc_nop;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001061 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001062
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001063 case 'w':
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001064 case 'W':
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08001065 if (pass == 2)
1066 set_warning_status(param);
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001067 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001068
H. Peter Anvine2c80182005-01-15 22:15:51 +00001069 case 'M':
H. Peter Anvin77c9bf62017-08-16 21:14:33 -07001070 if (pass == 1) {
1071 switch (p[2]) {
1072 case 'W':
1073 quote_for_make = quote_for_wmake;
1074 break;
1075 case 'D':
1076 case 'F':
1077 case 'T':
1078 case 'Q':
1079 advance = true;
1080 break;
1081 default:
1082 break;
1083 }
1084 } else {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001085 switch (p[2]) {
1086 case 0:
1087 operating_mode = OP_DEPEND;
1088 break;
1089 case 'G':
1090 operating_mode = OP_DEPEND;
1091 depend_missing_ok = true;
1092 break;
1093 case 'P':
1094 depend_emit_phony = true;
1095 break;
1096 case 'D':
H. Peter Anvin34754622018-11-28 12:36:53 -08001097 operating_mode |= OP_DEPEND;
1098 if (q && (q[0] != '-' || q[1] == '\0')) {
1099 depend_file = q;
1100 advance = true;
1101 }
H. Peter Anvin55568c12016-10-03 19:46:49 -07001102 break;
1103 case 'F':
1104 depend_file = q;
1105 advance = true;
1106 break;
1107 case 'T':
1108 depend_target = q;
1109 advance = true;
1110 break;
1111 case 'Q':
1112 depend_target = quote_for_make(q);
1113 advance = true;
1114 break;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -07001115 case 'W':
1116 /* handled in pass 1 */
1117 break;
H. Peter Anvin55568c12016-10-03 19:46:49 -07001118 default:
H. Peter Anvinc55702e2018-12-10 22:06:15 -08001119 nasm_nonfatalf(ERR_USAGE, "unknown dependency option `-M%c'", p[2]);
H. Peter Anvin55568c12016-10-03 19:46:49 -07001120 break;
1121 }
H. Peter Anvin77c9bf62017-08-16 21:14:33 -07001122 }
1123 if (advance && (!q || !q[0])) {
H. Peter Anvinc55702e2018-12-10 22:06:15 -08001124 nasm_nonfatalf(ERR_USAGE, "option `-M%c' requires a parameter", p[2]);
H. Peter Anvin77c9bf62017-08-16 21:14:33 -07001125 break;
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001126 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001127 break;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001128
H. Peter Anvine2c80182005-01-15 22:15:51 +00001129 case '-':
1130 {
H. Peter Anvin3366e312018-02-07 14:14:36 -08001131 const struct textargs *tx;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001132 size_t olen, plen;
H. Peter Anvinc805fd72018-06-12 14:23:05 -07001133 char *eqsave;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001134
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001135 p += 2;
1136
1137 if (!*p) { /* -- => stop processing options */
H. Peter Anvin3366e312018-02-07 14:14:36 -08001138 stopoptions = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001139 break;
1140 }
Cyrill Gorcunove7438432014-05-09 22:34:34 +04001141
H. Peter Anvin (Intel)1e2358b2018-12-14 13:02:39 -08001142 olen = 0; /* Placate gcc at lower optimization levels */
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001143 plen = strlen(p);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001144 for (tx = textopts; tx->label; tx++) {
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001145 olen = strlen(tx->label);
1146
1147 if (olen > plen)
1148 continue;
1149
1150 if (nasm_memicmp(p, tx->label, olen))
1151 continue;
1152
1153 if (tx->label[olen-1] == '-')
1154 break; /* Incomplete option */
1155
1156 if (!p[olen] || p[olen] == '=')
1157 break; /* Complete option */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001158 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001159
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001160 if (!tx->label) {
H. Peter Anvinc55702e2018-12-10 22:06:15 -08001161 nasm_nonfatalf(ERR_USAGE, "unrecognized option `--%s'", p);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001162 }
1163
H. Peter Anvinc805fd72018-06-12 14:23:05 -07001164 eqsave = param = strchr(p+olen, '=');
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001165 if (param)
1166 *param++ = '\0';
1167
H. Peter Anvin3366e312018-02-07 14:14:36 -08001168 if (tx->need_arg) {
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001169 if (!param) {
1170 param = q;
1171 advance = true;
1172 }
1173
1174 /* Note: a null string is a valid parameter */
1175 if (!param) {
H. Peter Anvinc55702e2018-12-10 22:06:15 -08001176 nasm_nonfatalf(ERR_USAGE, "option `--%s' requires an argument", p);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001177 break;
1178 }
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001179 } else {
1180 if (param) {
H. Peter Anvinc55702e2018-12-10 22:06:15 -08001181 nasm_nonfatalf(ERR_USAGE, "option `--%s' does not take an argument", p);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001182 }
H. Peter Anvin3366e312018-02-07 14:14:36 -08001183 }
1184
1185 switch (tx->opt) {
1186 case OPT_VERSION:
1187 show_version();
1188 break;
1189 case OPT_ABORT_ON_PANIC:
1190 abort_on_panic = true;
1191 break;
H. Peter Anvin98578072018-06-01 18:02:54 -07001192 case OPT_MANGLE:
H. Peter Anvin3366e312018-02-07 14:14:36 -08001193 if (pass == 2)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001194 set_label_mangle(tx->pvt, param);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001195 break;
H. Peter Anvin05990342018-06-11 13:32:42 -07001196 case OPT_INCLUDE:
1197 if (pass == 2)
1198 preproc->pre_include(q);
1199 break;
1200 case OPT_PRAGMA:
1201 if (pass == 2)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001202 preproc->pre_command("pragma", param);
H. Peter Anvin05990342018-06-11 13:32:42 -07001203 break;
1204 case OPT_BEFORE:
1205 if (pass == 2)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001206 preproc->pre_command(NULL, param);
H. Peter Anvin05990342018-06-11 13:32:42 -07001207 break;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001208 case OPT_LIMIT:
H. Peter Anvinbf6230b2018-11-11 13:25:16 -08001209 if (pass == 1)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001210 nasm_set_limit(p+olen, param);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001211 break;
H. Peter Anvin29695c82018-06-14 17:04:32 -07001212 case OPT_KEEP_ALL:
1213 keep_all = true;
1214 break;
H. Peter Anvin (Intel)800c1682018-12-14 12:22:11 -08001215 case OPT_NO_LINE:
1216 pp_noline = true;
1217 break;
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001218 case OPT_DEBUG:
1219 debug_nasm = param ? strtoul(param, NULL, 10) : 1;
1220 break;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001221 case OPT_HELP:
1222 help(0);
1223 exit(0);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001224 default:
1225 panic();
H. Peter Anvine2c80182005-01-15 22:15:51 +00001226 }
H. Peter Anvinc805fd72018-06-12 14:23:05 -07001227
1228 if (eqsave)
1229 *eqsave = '='; /* Restore = argument separator */
1230
H. Peter Anvine2c80182005-01-15 22:15:51 +00001231 break;
1232 }
1233
1234 default:
H. Peter Anvinc55702e2018-12-10 22:06:15 -08001235 nasm_nonfatalf(ERR_USAGE, "unrecognised option `-%c'", p[1]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001236 break;
1237 }
H. Peter Anvin55568c12016-10-03 19:46:49 -07001238 } else if (pass == 2) {
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001239 /* In theory we could allow multiple input files... */
1240 copy_filename(&inname, p, "input");
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001241 }
1242
1243 return advance;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001244}
1245
H. Peter Anvineba20a72002-04-30 20:53:55 +00001246#define ARG_BUF_DELTA 128
1247
H. Peter Anvin55568c12016-10-03 19:46:49 -07001248static void process_respfile(FILE * rfile, int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001249{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001250 char *buffer, *p, *q, *prevarg;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001251 int bufsize, prevargsize;
1252
1253 bufsize = prevargsize = ARG_BUF_DELTA;
1254 buffer = nasm_malloc(ARG_BUF_DELTA);
1255 prevarg = nasm_malloc(ARG_BUF_DELTA);
1256 prevarg[0] = '\0';
1257
H. Peter Anvine2c80182005-01-15 22:15:51 +00001258 while (1) { /* Loop to handle all lines in file */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001259 p = buffer;
1260 while (1) { /* Loop to handle long lines */
1261 q = fgets(p, bufsize - (p - buffer), rfile);
1262 if (!q)
1263 break;
1264 p += strlen(p);
1265 if (p > buffer && p[-1] == '\n')
1266 break;
1267 if (p - buffer > bufsize - 10) {
1268 int offset;
1269 offset = p - buffer;
1270 bufsize += ARG_BUF_DELTA;
1271 buffer = nasm_realloc(buffer, bufsize);
1272 p = buffer + offset;
1273 }
1274 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001275
H. Peter Anvine2c80182005-01-15 22:15:51 +00001276 if (!q && p == buffer) {
1277 if (prevarg[0])
H. Peter Anvin55568c12016-10-03 19:46:49 -07001278 process_arg(prevarg, NULL, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001279 nasm_free(buffer);
1280 nasm_free(prevarg);
1281 return;
1282 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001283
H. Peter Anvine2c80182005-01-15 22:15:51 +00001284 /*
1285 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
1286 * them are present at the end of the line.
1287 */
1288 *(p = &buffer[strcspn(buffer, "\r\n\032")]) = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001289
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001290 while (p > buffer && nasm_isspace(p[-1]))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001291 *--p = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001292
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +04001293 p = nasm_skip_spaces(buffer);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001294
H. Peter Anvin55568c12016-10-03 19:46:49 -07001295 if (process_arg(prevarg, p, pass))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001296 *p = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001297
Charles Crayne192d5b52007-10-18 19:02:42 -07001298 if ((int) strlen(p) > prevargsize - 10) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001299 prevargsize += ARG_BUF_DELTA;
1300 prevarg = nasm_realloc(prevarg, prevargsize);
1301 }
H. Peter Anvindc242712007-11-18 11:55:10 -08001302 strncpy(prevarg, p, prevargsize);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001303 }
1304}
1305
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001306/* Function to process args from a string of args, rather than the
1307 * argv array. Used by the environment variable and response file
1308 * processing.
1309 */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001310static void process_args(char *args, int pass)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001311{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001312 char *p, *q, *arg, *prevarg;
1313 char separator = ' ';
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001314
1315 p = args;
1316 if (*p && *p != '-')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001317 separator = *p++;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001318 arg = NULL;
1319 while (*p) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001320 q = p;
1321 while (*p && *p != separator)
1322 p++;
1323 while (*p == separator)
1324 *p++ = '\0';
1325 prevarg = arg;
1326 arg = q;
H. Peter Anvin55568c12016-10-03 19:46:49 -07001327 if (process_arg(prevarg, arg, pass))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001328 arg = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001329 }
1330 if (arg)
H. Peter Anvin55568c12016-10-03 19:46:49 -07001331 process_arg(arg, NULL, pass);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001332}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001333
H. Peter Anvin55568c12016-10-03 19:46:49 -07001334static void process_response_file(const char *file, int pass)
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001335{
1336 char str[2048];
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001337 FILE *f = nasm_open_read(file, NF_TEXT);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001338 if (!f) {
Cyrill Gorcunovf1964512013-02-15 12:14:06 +04001339 perror(file);
1340 exit(-1);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001341 }
1342 while (fgets(str, sizeof str, f)) {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001343 process_args(str, pass);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001344 }
1345 fclose(f);
1346}
1347
H. Peter Anvin55568c12016-10-03 19:46:49 -07001348static void parse_cmdline(int argc, char **argv, int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001349{
1350 FILE *rfile;
Cyrill Gorcunovf4941892011-07-17 13:55:25 +04001351 char *envreal, *envcopy = NULL, *p;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001352
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001353 /*
1354 * Initialize all the warnings to their default state, including
1355 * warning index 0 used for "always on".
1356 */
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -08001357 memcpy(warning_state, warning_default, sizeof warning_state);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001358
1359 /*
H. Peter Anvinff7ccc02002-05-06 19:41:57 +00001360 * First, process the NASMENV environment variable.
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001361 */
H. Peter Anvinff7ccc02002-05-06 19:41:57 +00001362 envreal = getenv("NASMENV");
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001363 if (envreal) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001364 envcopy = nasm_strdup(envreal);
H. Peter Anvin55568c12016-10-03 19:46:49 -07001365 process_args(envcopy, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001366 nasm_free(envcopy);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001367 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001368
1369 /*
1370 * Now process the actual command line.
1371 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001372 while (--argc) {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001373 bool advance;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001374 argv++;
1375 if (argv[0][0] == '@') {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001376 /*
1377 * We have a response file, so process this as a set of
H. Peter Anvine2c80182005-01-15 22:15:51 +00001378 * arguments like the environment variable. This allows us
1379 * to have multiple arguments on a single line, which is
1380 * different to the -@resp file processing below for regular
1381 * NASM.
1382 */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001383 process_response_file(argv[0]+1, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001384 argc--;
1385 argv++;
1386 }
1387 if (!stopoptions && argv[0][0] == '-' && argv[0][1] == '@') {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001388 p = get_param(argv[0], argc > 1 ? argv[1] : NULL, &advance);
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001389 if (p) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001390 rfile = nasm_open_read(p, NF_TEXT);
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001391 if (rfile) {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001392 process_respfile(rfile, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001393 fclose(rfile);
H. Peter Anvinc55702e2018-12-10 22:06:15 -08001394 } else {
1395 nasm_nonfatalf(ERR_USAGE, "unable to open response file `%s'", p);
1396 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001397 }
1398 } else
H. Peter Anvin55568c12016-10-03 19:46:49 -07001399 advance = process_arg(argv[0], argc > 1 ? argv[1] : NULL, pass);
H. Peter Anvin423e3812007-11-15 17:12:29 -08001400 argv += advance, argc -= advance;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001401 }
1402
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001403 /*
1404 * Look for basic command line typos. This definitely doesn't
1405 * catch all errors, but it might help cases of fumbled fingers.
1406 */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001407 if (pass != 2)
1408 return;
1409
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001410 if (!inname)
H. Peter Anvin77016c82018-12-10 22:29:49 -08001411 nasm_fatalf(ERR_USAGE, "no input file specified");
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001412 else if ((errname && !strcmp(inname, errname)) ||
1413 (outname && !strcmp(inname, outname)) ||
1414 (listname && !strcmp(inname, listname)) ||
1415 (depend_file && !strcmp(inname, depend_file)))
Cyrill Gorcunovc3527dd2018-11-24 22:17:47 +03001416 nasm_fatalf(ERR_USAGE, "will not overwrite input file");
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001417
1418 if (errname) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001419 error_file = nasm_open_write(errname, NF_TEXT);
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001420 if (!error_file) {
1421 error_file = stderr; /* Revert to default! */
H. Peter Anvin77016c82018-12-10 22:29:49 -08001422 nasm_fatalf(ERR_USAGE, "cannot open file `%s' for error messages", errname);
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001423 }
Charles Craynefcce07f2007-09-30 22:15:36 -07001424 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001425}
1426
H. Peter Anvin29651542018-12-18 19:14:40 -08001427static void forward_refs(insn *instruction)
1428{
1429 int i;
1430 struct forwrefinfo *fwinf;
1431
1432 instruction->forw_ref = false;
1433
1434 if (!optimizing.level)
1435 return; /* For -O0 don't bother */
1436
1437 if (!forwref)
1438 return;
1439
1440 if (forwref->lineno != globallineno)
1441 return;
1442
1443 instruction->forw_ref = true;
1444 do {
1445 instruction->oprs[forwref->operand].opflags |= OPFLAG_FORWARD;
1446 forwref = saa_rstruct(forwrefs);
1447 } while (forwref && forwref->lineno == globallineno);
1448
1449 if (!pass_first())
1450 return;
1451
1452 for (i = 0; i < instruction->operands; i++) {
1453 if (instruction->oprs[i].opflags & OPFLAG_FORWARD) {
1454 fwinf = saa_wstruct(forwrefs);
1455 fwinf->lineno = globallineno;
1456 fwinf->operand = i;
1457 }
1458 }
1459}
1460
1461static void process_insn(insn *instruction)
1462{
1463 int32_t n;
1464 int64_t l;
1465
1466 if (!instruction->times)
1467 return; /* Nothing to do... */
1468
1469 nasm_assert(instruction->times > 0);
1470
1471 /*
1472 * NOTE: insn_size() can change instruction->times
1473 * (usually to 1) when called.
1474 */
1475 if (!pass_final()) {
1476 for (n = 1; n <= instruction->times; n++) {
1477 l = insn_size(location.segment, location.offset,
1478 globalbits, instruction);
1479 if (l != -1) /* l == -1 -> invalid instruction */
1480 increment_offset(l);
1481 }
1482 } else {
1483 l = assemble(location.segment, location.offset,
1484 globalbits, instruction);
1485 /* We can't get an invalid instruction here */
1486 increment_offset(l);
1487
1488 if (instruction->times > 1) {
H. Peter Anvin0d4d4312019-08-07 00:46:27 -07001489 lfmt->uplevel(LIST_TIMES, instruction->times);
H. Peter Anvin29651542018-12-18 19:14:40 -08001490 for (n = 2; n <= instruction->times; n++) {
1491 l = assemble(location.segment, location.offset,
1492 globalbits, instruction);
1493 increment_offset(l);
1494 }
1495 lfmt->downlevel(LIST_TIMES);
1496 }
1497 }
1498}
1499
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03001500static void assemble_file(const char *fname, struct strlist *depend_list)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001501{
H. Peter Anvinc7131682017-03-07 17:45:01 -08001502 char *line;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001503 insn output_ins;
H. Peter Anvin9c595b62017-03-07 19:44:21 -08001504 uint64_t prev_offset_changed;
H. Peter Anvina3d96d02018-06-15 17:51:39 -07001505 int64_t stall_count = 0; /* Make sure we make forward progress... */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001506
H. Peter Anvina7ecf262018-02-06 14:43:07 -08001507 switch (cmd_sb) {
1508 case 16:
1509 break;
1510 case 32:
1511 if (!iflag_cpu_level_ok(&cmd_cpu, IF_386))
H. Peter Anvinc5136902018-06-15 18:20:17 -07001512 nasm_fatal("command line: 32-bit segment size requires a higher cpu");
H. Peter Anvina7ecf262018-02-06 14:43:07 -08001513 break;
1514 case 64:
1515 if (!iflag_cpu_level_ok(&cmd_cpu, IF_X86_64))
H. Peter Anvinc5136902018-06-15 18:20:17 -07001516 nasm_fatal("command line: 64-bit segment size requires a higher cpu");
H. Peter Anvina7ecf262018-02-06 14:43:07 -08001517 break;
1518 default:
1519 panic();
1520 break;
1521 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001522
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001523 prev_offset_changed = INT64_MAX;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001524
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001525 if (listname && !keep_all) {
1526 /* Remove the list file in case we die before the output pass */
1527 remove(listname);
1528 }
1529
1530 while (!terminate_after_phase && !pass_final()) {
1531 _passn++;
1532 if (pass_type() != PASS_OPT || !global_offset_changed)
1533 _pass_type++;
1534 global_offset_changed = 0;
1535
1536 /*
1537 * Create a warning buffer list unless we are in
1538 * pass 2 (everything will be emitted immediately in pass 2.)
1539 */
1540 if (warn_list) {
1541 if (warn_list->nstr || pass_final())
H. Peter Anvin (Intel)374312c2018-12-14 00:17:13 -08001542 strlist_free(&warn_list);
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03001543 }
H. Peter Anvin (Intel)374312c2018-12-14 00:17:13 -08001544
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001545 if (!pass_final() && !warn_list)
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03001546 warn_list = strlist_alloc(false);
H. Peter Anvin (Intel)374312c2018-12-14 00:17:13 -08001547
H. Peter Anvincac0b192017-03-28 16:12:30 -07001548 globalbits = cmd_sb; /* set 'bits' to command line default */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001549 cpu = cmd_cpu;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07001550 if (pass_final()) {
1551 active_list_options = list_options;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001552 lfmt->init(listname);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07001553 }
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001554
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001555 in_absolute = false;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001556 if (!pass_first()) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001557 saa_rewind(forwrefs);
1558 forwref = saa_rstruct(forwrefs);
1559 raa_free(offsets);
1560 offsets = raa_init();
1561 }
H. Peter Anvin892c4812018-05-30 14:43:46 -07001562 location.segment = NO_SEG;
1563 location.offset = 0;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001564 if (pass_first())
H. Peter Anvin892c4812018-05-30 14:43:46 -07001565 location.known = true;
1566 ofmt->reset();
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001567 switch_segment(ofmt->section(NULL, &globalbits));
1568 preproc->reset(fname, PP_NORMAL, pass_final() ? depend_list : NULL);
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001569
H. Peter Anvine2c80182005-01-15 22:15:51 +00001570 globallineno = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001571
H. Peter Anvine2c80182005-01-15 22:15:51 +00001572 while ((line = preproc->getline())) {
H. Peter Anvina3d96d02018-06-15 17:51:39 -07001573 if (++globallineno > nasm_limit[LIMIT_LINES])
H. Peter Anvinc5136902018-06-15 18:20:17 -07001574 nasm_fatal("overall line count exceeds the maximum %"PRId64"\n",
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03001575 nasm_limit[LIMIT_LINES]);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001576
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001577 /*
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001578 * Here we parse our directives; this is not handled by the
H. Peter Anvinc7131682017-03-07 17:45:01 -08001579 * main parser.
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001580 */
H. Peter Anvina6e26d92017-03-07 21:32:37 -08001581 if (process_directives(line))
H. Peter Anvinc7131682017-03-07 17:45:01 -08001582 goto end_of_line; /* Just do final cleanup */
H. Peter Anvin62b24d72007-08-29 16:38:05 +00001583
H. Peter Anvinc7131682017-03-07 17:45:01 -08001584 /* Not a directive, or even something that starts with [ */
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001585 parse_line(line, &output_ins);
H. Peter Anvin29651542018-12-18 19:14:40 -08001586 forward_refs(&output_ins);
1587 process_insn(&output_ins);
H. Peter Anvinc7131682017-03-07 17:45:01 -08001588 cleanup_insn(&output_ins);
1589
1590 end_of_line:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001591 nasm_free(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001592 } /* end while (line = preproc->getline... */
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001593
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001594 preproc->cleanup_pass();
1595
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001596 if (global_offset_changed) {
1597 switch (pass_type()) {
1598 case PASS_OPT:
1599 /*
1600 * This is the only pass type that can be executed more
1601 * than once, and therefore has the ability to stall.
1602 */
1603 if (global_offset_changed < prev_offset_changed) {
1604 prev_offset_changed = global_offset_changed;
1605 stall_count = 0;
1606 } else {
1607 stall_count++;
1608 }
1609
1610 if (stall_count > nasm_limit[LIMIT_STALLED] ||
1611 pass_count() >= nasm_limit[LIMIT_PASSES]) {
1612 /* No convergence, almost certainly dead */
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -08001613 nasm_nonfatalf(ERR_UNDEAD,
1614 "unable to find valid values for all labels "
1615 "after %"PRId64" passes; "
1616 "stalled for %"PRId64", giving up.",
1617 pass_count(), stall_count);
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001618 nasm_nonfatalf(ERR_UNDEAD,
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -08001619 "Possible causes: recursive EQUs, macro abuse.");
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001620 }
1621 break;
1622
1623 case PASS_STAB:
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08001624 /*!
1625 *!phase [off] phase error during stabilization
1626 *! warns about symbols having changed values during
1627 *! the second-to-last assembly pass. This is not
1628 *! inherently fatal, but may be a source of bugs.
1629 */
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -08001630 nasm_warn(WARN_PHASE|ERR_UNDEAD,
1631 "phase error during stabilization "
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001632 "pass, hoping for the best");
H. Peter Anvin (Intel)b45c03a2018-06-27 21:03:38 -07001633 break;
1634
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001635 case PASS_FINAL:
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -08001636 nasm_nonfatalf(ERR_UNDEAD,
1637 "phase error during code generation pass");
H. Peter Anvin (Intel)b45c03a2018-06-27 21:03:38 -07001638 break;
1639
1640 default:
1641 /* This is normal, we'll keep going... */
1642 break;
1643 }
1644 }
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -08001645
1646 reset_warnings();
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001647 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001648
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001649 if (opt_verbose_info && pass_final()) {
1650 /* -On and -Ov switches */
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001651 nasm_info("assembly required 1+%"PRId64"+2 passes\n", pass_count()-3);
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001652 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001653
H. Peter Anvin (Intel)374312c2018-12-14 00:17:13 -08001654 strlist_free(&warn_list);
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08001655 lfmt->cleanup();
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001656}
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001657
Ed Berosetfa771012002-06-09 20:56:40 +00001658/**
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001659 * get warning index; 0 if this is non-suppressible.
Ed Berosetfa771012002-06-09 20:56:40 +00001660 */
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08001661static size_t warn_index(errflags severity)
Ed Berosetfa771012002-06-09 20:56:40 +00001662{
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001663 size_t index;
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001664
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001665 if ((severity & ERR_MASK) >= ERR_FATAL)
1666 return 0; /* Fatal errors are never suppressible */
1667
H. Peter Anvin (Intel)c3c6cea2018-12-14 13:44:35 -08001668 /* Warnings MUST HAVE a warning category specifier! */
1669 nasm_assert((severity & (ERR_MASK|WARN_MASK)) != ERR_WARNING);
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001670
1671 index = WARN_IDX(severity);
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08001672 nasm_assert(index < WARN_IDX_ALL);
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001673
1674 return index;
Ed Berosetfa771012002-06-09 20:56:40 +00001675}
1676
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08001677static bool skip_this_pass(errflags severity)
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001678{
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001679 errflags type = severity & ERR_MASK;
1680
H. Peter Anvin8f622462017-04-02 19:02:29 -07001681 /*
1682 * See if it's a pass-specific error or warning which should be skipped.
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001683 * We can never skip fatal errors as by definition they cannot be
1684 * resumed from.
H. Peter Anvin8f622462017-04-02 19:02:29 -07001685 */
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001686 if (type >= ERR_FATAL)
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03001687 return false;
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001688
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001689 /*
1690 * ERR_LISTMSG messages are always skipped; the list file
1691 * receives them anyway as this function is not consulted
1692 * for sending to the list file.
1693 */
1694 if (type == ERR_LISTMSG)
1695 return true;
1696
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08001697 /* This message not applicable unless pass_final */
1698 return (severity & ERR_PASS2) && !pass_final();
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001699}
1700
Ed Berosetfa771012002-06-09 20:56:40 +00001701/**
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001702 * check for suppressed message (usually warnings or notes)
1703 *
1704 * @param severity the severity of the warning or error
1705 * @return true if we should abort error/warning printing
1706 */
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08001707static bool is_suppressed(errflags severity)
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001708{
H. Peter Anvin4cf86dd2018-12-27 11:24:17 -08001709 /* Fatal errors must never be suppressed */
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08001710 if ((severity & ERR_MASK) >= ERR_FATAL)
H. Peter Anvin4cf86dd2018-12-27 11:24:17 -08001711 return false;
1712
1713 /* This error/warning is pointless if we are dead anyway */
1714 if ((severity & ERR_UNDEAD) && terminate_after_phase)
1715 return true;
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08001716
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001717 return !(warning_state[warn_index(severity)] & WARN_ST_ENABLED);
1718}
1719
1720/**
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08001721 * Return the true error type (the ERR_MASK part) of the given
1722 * severity, accounting for warnings that may need to be promoted to
1723 * error.
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001724 *
1725 * @param severity the severity of the warning or error
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08001726 * @return true if we should error out
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001727 */
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08001728static errflags true_error_type(errflags severity)
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001729{
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08001730 const uint8_t warn_is_err = WARN_ST_ENABLED|WARN_ST_ERROR;
1731 int type;
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001732
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08001733 type = severity & ERR_MASK;
1734
1735 /* Promote warning to error? */
1736 if (type == ERR_WARNING) {
1737 uint8_t state = warning_state[warn_index(severity)];
1738 if ((state & warn_is_err) == warn_is_err)
1739 type = ERR_NONFATAL;
1740 }
1741
1742 return type;
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001743}
1744
1745/**
Ed Berosetfa771012002-06-09 20:56:40 +00001746 * common error reporting
1747 * This is the common back end of the error reporting schemes currently
H. Peter Anvin70653092007-10-19 14:42:29 -07001748 * implemented. It prints the nature of the warning and then the
Ed Berosetfa771012002-06-09 20:56:40 +00001749 * specific error message to error_file and may or may not return. It
1750 * doesn't return if the error severity is a "panic" or "debug" type.
H. Peter Anvin70653092007-10-19 14:42:29 -07001751 *
1752 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001753 * @param fmt the printf style format string
1754 */
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08001755static void nasm_verror_asm(errflags severity, const char *fmt, va_list args)
Ed Berosetfa771012002-06-09 20:56:40 +00001756{
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001757 char msg[1024];
H. Peter Anvinddb29062018-12-11 00:06:29 -08001758 char warnsuf[64];
1759 char linestr[64];
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001760 const char *pfx;
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08001761 errflags true_type = true_error_type(severity);
H. Peter Anvin77016c82018-12-10 22:29:49 -08001762 const char *currentfile = NULL;
1763 int32_t lineno = 0;
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08001764 static const char * const pfx_table[ERR_MASK+1] = {
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001765 ";;; ", "debug: ", "info: ", "warning: ",
1766 "error: ", "", "fatal: ", "panic: "
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08001767 };
H. Peter Anvin77016c82018-12-10 22:29:49 -08001768
H. Peter Anvinc0b32a32018-12-10 22:29:49 -08001769 if (is_suppressed(severity))
H. Peter Anvin77016c82018-12-10 22:29:49 -08001770 return;
1771
1772 if (!(severity & ERR_NOFILE)) {
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03001773 src_get(&lineno, &currentfile);
H. Peter Anvin77016c82018-12-10 22:29:49 -08001774 if (!currentfile) {
1775 currentfile = currentfile ? currentfile :
1776 inname && inname[0] ? inname :
1777 outname && outname[0] ? outname :
1778 NULL;
1779 lineno = 0;
1780 }
1781 }
H. Peter Anvin323fcff2009-07-12 12:04:56 -07001782
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08001783 if (severity & ERR_NO_SEVERITY)
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001784 pfx = "";
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08001785 else
1786 pfx = pfx_table[true_type];
H. Peter Anvinddb29062018-12-11 00:06:29 -08001787
1788 vsnprintf(msg, sizeof msg, fmt, args);
1789 *warnsuf = 0;
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001790 if ((severity & (ERR_MASK|ERR_HERE|ERR_PP_LISTMACRO)) == ERR_WARNING) {
1791 /*
1792 * It's a warning without ERR_HERE defined, and we are not already
1793 * unwinding the macros that led us here.
1794 */
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03001795 snprintf(warnsuf, sizeof warnsuf, " [-w+%s%s]",
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08001796 (true_type >= ERR_NONFATAL) ? "error=" : "",
1797 warning_name[warn_index(severity)]);
H. Peter Anvin934f0472016-05-09 12:00:19 -07001798 }
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001799
H. Peter Anvinddb29062018-12-11 00:06:29 -08001800 *linestr = 0;
1801 if (lineno) {
1802 snprintf(linestr, sizeof linestr, "%s%"PRId32"%s",
1803 errfmt->beforeline, lineno, errfmt->afterline);
1804 }
1805
H. Peter Anvin77016c82018-12-10 22:29:49 -08001806 if (!skip_this_pass(severity)) {
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03001807 const char *file = currentfile ? currentfile : "nasm";
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001808 const char *here = "";
1809
1810 if (severity & ERR_HERE)
1811 here = currentfile ? " here" : " in an unknown location";
H. Peter Anvin (Intel)374312c2018-12-14 00:17:13 -08001812
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08001813 if (warn_list && true_type < ERR_NONFATAL &&
1814 !(pass_first() && (severity & ERR_PASS1)))
1815 {
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03001816 /*
1817 * Buffer up warnings until we either get an error
1818 * or we are on the code-generation pass.
1819 */
1820 strlist_printf(warn_list, "%s%s%s%s%s%s%s",
1821 file, linestr, errfmt->beforemsg,
1822 pfx, msg, here, warnsuf);
1823 } else {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08001824 /*
1825 * If we have buffered warnings, and this is a non-warning,
1826 * output them now.
1827 */
1828 if (true_type >= ERR_NONFATAL && warn_list) {
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03001829 strlist_write(warn_list, "\n", error_file);
1830 strlist_free(&warn_list);
1831 }
H. Peter Anvin (Intel)374312c2018-12-14 00:17:13 -08001832
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03001833 fprintf(error_file, "%s%s%s%s%s%s%s\n",
1834 file, linestr, errfmt->beforemsg,
1835 pfx, msg, here, warnsuf);
H. Peter Anvin (Intel)374312c2018-12-14 00:17:13 -08001836
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03001837 }
H. Peter Anvin77016c82018-12-10 22:29:49 -08001838 }
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001839
H. Peter Anvin4def1a82016-05-09 13:59:44 -07001840 /* Are we recursing from error_list_macros? */
1841 if (severity & ERR_PP_LISTMACRO)
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03001842 return;
H. Peter Anvin4def1a82016-05-09 13:59:44 -07001843
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001844 /*
1845 * Don't suppress this with skip_this_pass(), or we don't get
H. Peter Anvin934f0472016-05-09 12:00:19 -07001846 * pass1 or preprocessor warnings in the list file
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001847 */
H. Peter Anvinddb29062018-12-11 00:06:29 -08001848 if (severity & ERR_HERE) {
1849 if (lineno)
1850 lfmt->error(severity, "%s%s at %s:%"PRId32"%s",
1851 pfx, msg, currentfile, lineno, warnsuf);
1852 else if (currentfile)
1853 lfmt->error(severity, "%s%s in file %s%s",
1854 pfx, msg, currentfile, warnsuf);
1855 else
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001856 lfmt->error(severity, "%s%s in an unknown location%s",
H. Peter Anvinddb29062018-12-11 00:06:29 -08001857 pfx, msg, warnsuf);
1858 } else {
1859 lfmt->error(severity, "%s%s%s", pfx, msg, warnsuf);
1860 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001861
H. Peter Anvin8f622462017-04-02 19:02:29 -07001862 if (skip_this_pass(severity))
1863 return;
1864
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001865 if (severity & ERR_USAGE)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001866 want_usage = true;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001867
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001868 /* error_list_macros can for obvious reasons not work with ERR_HERE */
1869 if (!(severity & ERR_HERE))
1870 preproc->error_list_macros(severity);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07001871
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08001872 switch (true_type) {
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001873 case ERR_LISTMSG:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001874 case ERR_DEBUG:
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001875 case ERR_INFO:
H. Peter Anvinb030c922007-11-13 11:31:15 -08001876 case ERR_WARNING:
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08001877 /* no further action, by definition */
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001878 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001879 case ERR_NONFATAL:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001880 terminate_after_phase = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001881 break;
1882 case ERR_FATAL:
1883 if (ofile) {
1884 fclose(ofile);
H. Peter Anvin29695c82018-06-14 17:04:32 -07001885 if (!keep_all)
1886 remove(outname);
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001887 ofile = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001888 }
1889 if (want_usage)
1890 usage();
1891 exit(1); /* instantly die */
1892 break; /* placate silly compilers */
1893 case ERR_PANIC:
1894 fflush(NULL);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001895
1896 if (abort_on_panic)
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03001897 abort(); /* halt, catch fire, dump core/stop debugger */
H. Peter Anvin3366e312018-02-07 14:14:36 -08001898
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001899 if (ofile) {
1900 fclose(ofile);
H. Peter Anvin29695c82018-06-14 17:04:32 -07001901 if (!keep_all)
1902 remove(outname);
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001903 ofile = NULL;
1904 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001905 exit(3);
1906 break;
H. Peter Anvin54aac9d2018-12-10 21:14:57 -08001907 default:
1908 break; /* ??? */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001909 }
1910}
1911
H. Peter Anvin734b1882002-04-30 21:01:08 +00001912static void usage(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001913{
H. Peter Anvin620515a2002-04-30 20:57:38 +00001914 fputs("type `nasm -h' for help\n", error_file);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001915}
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001916
1917static void help(const char xopt)
1918{
1919 int i;
1920
1921 printf
1922 ("usage: nasm [-@ response file] [-o outfile] [-f format] "
1923 "[-l listfile]\n"
1924 " [options...] [--] filename\n"
1925 " or nasm -v (or --v) for version info\n\n"
1926 "\n"
1927 "Response files should contain command line parameters,\n"
1928 "one per line.\n"
1929 "\n"
1930 " -t assemble in SciTech TASM compatible mode\n");
1931 printf
1932 (" -E (or -e) preprocess only (writes output to stdout by default)\n"
1933 " -a don't preprocess (assemble only)\n"
1934 " -M generate Makefile dependencies on stdout\n"
1935 " -MG d:o, missing files assumed generated\n"
1936 " -MF file set Makefile dependency file\n"
1937 " -MD file assemble and generate dependencies\n"
1938 " -MT file dependency target name\n"
1939 " -MQ file dependency target name (quoted)\n"
1940 " -MP emit phony target\n\n"
1941 " -Zfile redirect error messages to file\n"
1942 " -s redirect error messages to stdout\n\n"
1943 " -g generate debugging information\n\n"
1944 " -F format select a debugging format\n\n"
1945 " -gformat same as -g -F format\n\n"
1946 " -o outfile write output to an outfile\n\n"
1947 " -f format select an output format\n\n"
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07001948 " -l listfile write listing to a list file\n"
1949 " -Lflags... add optional information to the list file\n"
1950 " -Le show the preprocessed output\n\n"
1951 " -Lm show all single-line macro definitions\n\n"
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001952 " -Ipath add a pathname to the include file path\n");
1953 printf
H. Peter Anvin (Intel)1e2358b2018-12-14 13:02:39 -08001954 (" -Oflags... optimize opcodes, immediates and branch offsets\n"
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001955 " -O0 no optimization\n"
1956 " -O1 minimal optimization\n"
1957 " -Ox multipass optimization (default)\n"
H. Peter Anvin (Intel)1e2358b2018-12-14 13:02:39 -08001958 " -Ov display the number of passes executed at the end\n"
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001959 " -Pfile pre-include a file (also --include)\n"
1960 " -Dmacro[=str] pre-define a macro\n"
1961 " -Umacro undefine a macro\n"
1962 " -Xformat specifiy error reporting format (gnu or vc)\n"
1963 " -w+foo enable warning foo (equiv. -Wfoo)\n"
1964 " -w-foo disable warning foo (equiv. -Wno-foo)\n"
1965 " -w[+-]error[=foo]\n"
1966 " promote [specific] warnings to errors\n"
Chang S. Bae1af6ef42018-06-20 17:05:12 -07001967 " -h show invocation summary and exit (also --help)\n\n"
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001968 " --pragma str pre-executes a specific %%pragma\n"
1969 " --before str add line (usually a preprocessor statement) before the input\n"
1970 " --prefix str prepend the given string to all the given string\n"
Chang S. Bae1af6ef42018-06-20 17:05:12 -07001971 " to all extern, common and global symbols (also --gprefix)\n"
1972 " --postfix str append the given string to all the given string\n"
1973 " to all extern, common and global symbols (also --gpostfix)\n"
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001974 " --lprefix str prepend the given string to all other symbols\n"
Chang S. Bae1af6ef42018-06-20 17:05:12 -07001975 " --lpostfix str append the given string to all other symbols\n"
1976 " --keep-all output files will not be removed even if an error happens\n"
H. Peter Anvin (Intel)800c1682018-12-14 12:22:11 -08001977 " --no-line ignore %%line directives in input\n"
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001978 " --limit-X val set execution limit X\n");
1979
1980 for (i = 0; i <= LIMIT_MAX; i++) {
1981 printf(" %-15s %s (default ",
1982 limit_info[i].name, limit_info[i].help);
1983 if (nasm_limit[i] < LIMIT_MAX_VAL) {
H. Peter Anvina3d96d02018-06-15 17:51:39 -07001984 printf("%"PRId64")\n", nasm_limit[i]);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001985 } else {
1986 printf("unlimited)\n");
1987 }
1988 }
1989
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08001990 printf("\nWarnings for the -W/-w options: (defaults in brackets)\n");
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001991
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08001992 for (i = 1; i <= WARN_IDX_ALL; i++) {
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001993 printf(" %-23s %s%s\n",
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08001994 warning_name[i], warning_help[i],
1995 i == WARN_IDX_ALL ? "\n" :
1996 (warning_default[i] & WARN_ST_ERROR) ? " [error]" :
1997 (warning_default[i] & WARN_ST_ENABLED) ? " [on]" : " [off]");
1998 }
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001999
2000 if (xopt == 'f') {
2001 printf("valid output formats for -f are"
2002 " (`*' denotes default):\n");
2003 ofmt_list(ofmt, stdout);
2004 } else {
2005 printf("For a list of valid output formats, use -hf.\n");
2006 printf("For a list of debug formats, use -f <format> -y.\n");
2007 }
2008}