blob: 0deec783ae38ded606d50ad74ad8397757f3a673 [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#include <stdio.h>
41#include <stdarg.h>
42#include <stdlib.h>
43#include <string.h>
44#include <ctype.h>
H. Peter Anvinfd7dd112007-10-10 14:06:59 -070045#include <limits.h>
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000046
47#include "nasm.h"
48#include "nasmlib.h"
H. Peter Anvinb20bc732017-03-07 19:23:03 -080049#include "error.h"
H. Peter Anvin1803ded2008-06-09 17:32:43 -070050#include "saa.h"
H. Peter Anvinfcb89092008-06-09 17:40:16 -070051#include "raa.h"
H. Peter Anvinf6c9e652007-10-16 14:40:27 -070052#include "float.h"
H. Peter Anvin74cc5e52007-08-30 22:35:34 +000053#include "stdscan.h"
H. Peter Anvinaf535c12002-04-30 20:59:21 +000054#include "insns.h"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000055#include "preproc.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000056#include "parser.h"
H. Peter Anvin76690a12002-04-30 20:52:49 +000057#include "eval.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000058#include "assemble.h"
59#include "labels.h"
H. Peter Anvine1f985c2016-05-25 12:06:29 -070060#include "outform.h"
H. Peter Anvin6768eb72002-04-30 20:52:26 +000061#include "listing.h"
Cyrill Gorcunov08359152013-11-09 22:16:11 +040062#include "iflag.h"
H. Peter Anvin2bc0ab32016-03-08 02:17:36 -080063#include "ver.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000064
H. Peter Anvin31387b22010-07-15 18:28:52 -070065/*
66 * This is the maximum number of optimization passes to do. If we ever
67 * find a case where the optimizer doesn't naturally converge, we might
68 * have to drop this value so the assembler doesn't appear to just hang.
69 */
70#define MAX_OPTIMIZE (INT_MAX >> 1)
71
H. Peter Anvine2c80182005-01-15 22:15:51 +000072struct forwrefinfo { /* info held on forward refs. */
H. Peter Anvineba20a72002-04-30 20:53:55 +000073 int lineno;
74 int operand;
75};
76
H. Peter Anvin55568c12016-10-03 19:46:49 -070077static void parse_cmdline(int, char **, int);
H. Peter Anvin81b62b92017-12-20 13:33:49 -080078static void assemble_file(const char *, StrList **);
H. Peter Anvin130736c2016-02-17 20:27:41 -080079static bool is_suppressed_warning(int severity);
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -080080static bool skip_this_pass(int severity);
H. Peter Anvin9bd15062009-07-18 21:07:17 -040081static void nasm_verror_gnu(int severity, const char *fmt, va_list args);
82static void nasm_verror_vc(int severity, const char *fmt, va_list args);
83static void nasm_verror_common(int severity, const char *fmt, va_list args);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000084static void usage(void);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -070085static void help(char xopt);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000086
H. Peter Anvin283b3fb2016-03-07 23:18:30 -080087static bool using_debug_info, opt_verbose_info;
88static const char *debug_format;
89
H. Peter Anvin3366e312018-02-07 14:14:36 -080090#ifndef ABORT_ON_PANIC
91# define ABORT_ON_PANIC 0
92#endif
93static bool abort_on_panic = ABORT_ON_PANIC;
H. Peter Anvin29695c82018-06-14 17:04:32 -070094static bool keep_all;
H. Peter Anvin3366e312018-02-07 14:14:36 -080095
H. Peter Anvin6867acc2007-10-10 14:58:45 -070096bool tasm_compatible_mode = false;
H. Peter Anvin79561022018-06-15 17:51:39 -070097int pass0;
98int64_t passn;
H. Peter Anvinc7131682017-03-07 17:45:01 -080099static int pass1, pass2; /* XXX: Get rid of these, they are redundant */
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +0000100int globalrel = 0;
Jin Kyu Songb287ff02013-12-04 20:05:55 -0800101int globalbnd = 0;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000102
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700103struct compile_time official_compile_time;
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800104
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800105const char *inname;
106const char *outname;
107static const char *listname;
108static const char *errname;
109
H. Peter Anvin79561022018-06-15 17:51:39 -0700110static int64_t globallineno; /* for forward-reference tracking */
Chang S. Baef0ceb1e2018-05-02 08:07:53 -0700111
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000112/* static int pass = 0; */
H. Peter Anvin338656c2016-02-17 20:59:22 -0800113const struct ofmt *ofmt = &OF_DEFAULT;
114const struct ofmt_alias *ofmt_alias = NULL;
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400115const struct dfmt *dfmt;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000116
H. Peter Anvine2c80182005-01-15 22:15:51 +0000117static FILE *error_file; /* Where to write error messages */
H. Peter Anvin620515a2002-04-30 20:57:38 +0000118
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400119FILE *ofile = NULL;
Chang S. Baea5786342018-08-15 23:22:21 +0300120struct optimization optimizing =
121 { MAX_OPTIMIZE, OPTIM_ALL_ENABLED }; /* number of optimization passes to take */
Martin Lindhe8cc93f52016-11-16 16:48:13 +0100122static int cmd_sb = 16; /* by default */
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400123
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800124iflag_t cpu;
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400125static iflag_t cmd_cpu;
126
H. Peter Anvincd7893d2016-02-18 01:25:46 -0800127struct location location;
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800128bool in_absolute; /* Flag we are in ABSOLUTE seg */
129struct location absolute; /* Segment/offset inside ABSOLUTE */
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000130
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000131static struct RAA *offsets;
H. Peter Anvinea838272002-04-30 20:51:53 +0000132
H. Peter Anvine2c80182005-01-15 22:15:51 +0000133static struct SAA *forwrefs; /* keep track of forward references */
H. Peter Anvin9d637df2007-10-04 13:42:56 -0700134static const struct forwrefinfo *forwref;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000135
H. Peter Anvine7469712016-02-18 02:20:59 -0800136static const struct preproc_ops *preproc;
Cyrill Gorcunov86b2ad02011-07-01 10:38:25 +0400137
Cyrill Gorcunove9fc88c2014-06-23 02:22:02 +0400138#define OP_NORMAL (1u << 0)
139#define OP_PREPROCESS (1u << 1)
140#define OP_DEPEND (1u << 2)
141
142static unsigned int operating_mode;
Cyrill Gorcunov3ed32cb2014-06-22 20:53:12 +0400143
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700144/* Dependency flags */
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700145static bool depend_emit_phony = false;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700146static bool depend_missing_ok = false;
147static const char *depend_target = NULL;
148static const char *depend_file = NULL;
H. Peter Anvina771be82017-08-16 14:53:18 -0700149StrList *depend_list;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000150
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700151static bool want_usage;
152static bool terminate_after_phase;
H. Peter Anvin130736c2016-02-17 20:27:41 -0800153bool user_nolist = false;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000154
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700155static char *quote_for_pmake(const char *str);
156static char *quote_for_wmake(const char *str);
157static char *(*quote_for_make)(const char *) = quote_for_pmake;
H. Peter Anvin55340992012-09-09 17:09:00 -0700158
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700159/*
160 * Execution limits that can be set via a command-line option or %pragma
161 */
162
H. Peter Anvin79561022018-06-15 17:51:39 -0700163#define LIMIT_MAX_VAL (INT64_MAX >> 1) /* Effectively unlimited */
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700164
H. Peter Anvin79561022018-06-15 17:51:39 -0700165int64_t nasm_limit[LIMIT_MAX+1] =
166{ LIMIT_MAX_VAL, 1000, 1000000, 1000000, 1000000, 2000000000 };
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700167
168struct limit_info {
169 const char *name;
170 const char *help;
171};
172static const struct limit_info limit_info[LIMIT_MAX+1] = {
173 { "passes", "total number of passes" },
174 { "stalled-passes", "number of passes without forward progress" },
175 { "macro-levels", "levels of macro expansion"},
176 { "rep", "%rep count" },
H. Peter Anvin79561022018-06-15 17:51:39 -0700177 { "eval", "expression evaluation descent"},
178 { "lines", "total source lines processed"}
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700179};
180
H. Peter Anvinc805fd72018-06-12 14:23:05 -0700181enum directive_result
182nasm_set_limit(const char *limit, const char *valstr)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700183{
184 int i;
185 int64_t val;
186 bool rn_error;
H. Peter Anvinc805fd72018-06-12 14:23:05 -0700187 int errlevel;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700188
189 for (i = 0; i <= LIMIT_MAX; i++) {
190 if (!nasm_stricmp(limit, limit_info[i].name))
191 break;
192 }
193 if (i > LIMIT_MAX) {
H. Peter Anvinc805fd72018-06-12 14:23:05 -0700194 if (passn == 0)
195 errlevel = ERR_WARNING|ERR_NOFILE|ERR_USAGE;
196 else
197 errlevel = ERR_WARNING|ERR_PASS1|ERR_WARN_UNKNOWN_PRAGMA;
198 nasm_error(errlevel, "unknown limit: `%s'", limit);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700199 return DIRR_ERROR;
200 }
201
202 if (!nasm_stricmp(valstr, "unlimited")) {
203 val = LIMIT_MAX_VAL;
204 } else {
205 val = readnum(valstr, &rn_error);
206 if (rn_error || val < 0) {
H. Peter Anvinc805fd72018-06-12 14:23:05 -0700207 if (passn == 0)
208 errlevel = ERR_WARNING|ERR_NOFILE|ERR_USAGE;
209 else
210 errlevel = ERR_WARNING|ERR_PASS1|ERR_WARN_BAD_PRAGMA;
211 nasm_error(errlevel, "invalid limit value: `%s'", limit);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700212 return DIRR_ERROR;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700213 }
H. Peter Anvinc805fd72018-06-12 14:23:05 -0700214 if (val > LIMIT_MAX_VAL)
215 val = LIMIT_MAX_VAL;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700216 }
217
218 nasm_limit[i] = val;
219 return DIRR_OK;
220}
221
H. Peter Anvin892c4812018-05-30 14:43:46 -0700222int64_t switch_segment(int32_t segment)
Cyrill Gorcunov190232f2013-02-15 12:35:04 +0400223{
H. Peter Anvin892c4812018-05-30 14:43:46 -0700224 location.segment = segment;
225 if (segment == NO_SEG) {
226 location.offset = absolute.offset;
227 in_absolute = true;
228 } else {
229 location.offset = raa_read(offsets, segment);
230 in_absolute = false;
231 }
232 return location.offset;
Cyrill Gorcunov190232f2013-02-15 12:35:04 +0400233}
234
235static void set_curr_offs(int64_t l_off)
236{
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800237 if (in_absolute)
238 absolute.offset = l_off;
Cyrill Gorcunov190232f2013-02-15 12:35:04 +0400239 else
240 offsets = raa_write(offsets, location.segment, l_off);
241}
242
H. Peter Anvin892c4812018-05-30 14:43:46 -0700243static void increment_offset(int64_t delta)
244{
245 if (unlikely(delta == 0))
246 return;
247
248 location.offset += delta;
249 set_curr_offs(location.offset);
250}
251
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000252static void nasm_fputs(const char *line, FILE * outfile)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000253{
H. Peter Anvin310b3e12002-05-14 22:38:55 +0000254 if (outfile) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000255 fputs(line, outfile);
H. Peter Anvind1fb15c2007-11-13 09:37:59 -0800256 putc('\n', outfile);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000257 } else
H. Peter Anvine2c80182005-01-15 22:15:51 +0000258 puts(line);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000259}
260
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800261static void define_macros_early(void)
262{
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700263 const struct compile_time * const oct = &official_compile_time;
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800264 char temp[128];
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800265
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700266 if (oct->have_local) {
267 strftime(temp, sizeof temp, "__DATE__=\"%Y-%m-%d\"", &oct->local);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400268 preproc->pre_define(temp);
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700269 strftime(temp, sizeof temp, "__DATE_NUM__=%Y%m%d", &oct->local);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400270 preproc->pre_define(temp);
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700271 strftime(temp, sizeof temp, "__TIME__=\"%H:%M:%S\"", &oct->local);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400272 preproc->pre_define(temp);
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700273 strftime(temp, sizeof temp, "__TIME_NUM__=%H%M%S", &oct->local);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400274 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800275 }
276
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700277 if (oct->have_gm) {
278 strftime(temp, sizeof temp, "__UTC_DATE__=\"%Y-%m-%d\"", &oct->gm);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400279 preproc->pre_define(temp);
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700280 strftime(temp, sizeof temp, "__UTC_DATE_NUM__=%Y%m%d", &oct->gm);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400281 preproc->pre_define(temp);
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700282 strftime(temp, sizeof temp, "__UTC_TIME__=\"%H:%M:%S\"", &oct->gm);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400283 preproc->pre_define(temp);
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700284 strftime(temp, sizeof temp, "__UTC_TIME_NUM__=%H%M%S", &oct->gm);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400285 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800286 }
H. Peter Anvind85d2502008-05-04 17:53:31 -0700287
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700288 if (oct->have_posix) {
289 snprintf(temp, sizeof temp, "__POSIX_TIME__=%"PRId64, oct->posix);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400290 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800291 }
292}
293
294static void define_macros_late(void)
295{
296 char temp[128];
297
Cyrill Gorcunovc1936da2011-04-06 18:32:15 +0400298 /*
299 * In case if output format is defined by alias
300 * we have to put shortname of the alias itself here
301 * otherwise ABI backward compatibility gets broken.
302 */
Cyrill Gorcunov69ce7502010-07-12 15:19:17 +0400303 snprintf(temp, sizeof(temp), "__OUTPUT_FORMAT__=%s",
Cyrill Gorcunovc1936da2011-04-06 18:32:15 +0400304 ofmt_alias ? ofmt_alias->shortname : ofmt->shortname);
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +0400305 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800306}
307
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700308static void emit_dependencies(StrList *list)
309{
310 FILE *deps;
311 int linepos, len;
312 StrList *l, *nl;
H. Peter Anvinf05034f2017-08-16 22:08:36 -0700313 bool wmake = (quote_for_make == quote_for_wmake);
314 const char *wrapstr, *nulltarget;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700315
H. Peter Anvinf05034f2017-08-16 22:08:36 -0700316 wrapstr = wmake ? " &\n " : " \\\n ";
317 nulltarget = wmake ? "\t%null\n" : "";
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700318
319 if (depend_file && strcmp(depend_file, "-")) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -0700320 deps = nasm_open_write(depend_file, NF_TEXT);
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400321 if (!deps) {
322 nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
323 "unable to write dependency file `%s'", depend_file);
324 return;
325 }
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700326 } else {
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400327 deps = stdout;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700328 }
H. Peter Anvin323fcff2009-07-12 12:04:56 -0700329
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700330 linepos = fprintf(deps, "%s :", depend_target);
Cyrill Gorcunovfcd0a742009-07-27 16:26:26 +0400331 list_for_each(l, list) {
H. Peter Anvin55340992012-09-09 17:09:00 -0700332 char *file = quote_for_make(l->str);
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400333 len = strlen(file);
334 if (linepos + len > 62 && linepos > 1) {
H. Peter Anvinf05034f2017-08-16 22:08:36 -0700335 fputs(wrapstr, deps);
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400336 linepos = 1;
337 }
338 fprintf(deps, " %s", file);
339 linepos += len+1;
H. Peter Anvin55340992012-09-09 17:09:00 -0700340 nasm_free(file);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700341 }
342 fprintf(deps, "\n\n");
H. Peter Anvin323fcff2009-07-12 12:04:56 -0700343
Cyrill Gorcunovfcd0a742009-07-27 16:26:26 +0400344 list_for_each_safe(l, nl, list) {
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700345 if (depend_emit_phony) {
346 char *file = quote_for_make(l->str);
H. Peter Anvinf05034f2017-08-16 22:08:36 -0700347 fprintf(deps, "%s :\n%s\n", file, nulltarget);
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700348 nasm_free(file);
349 }
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400350 nasm_free(l);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700351 }
352
353 if (deps != stdout)
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400354 fclose(deps);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700355}
356
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700357/* Convert a struct tm to a POSIX-style time constant */
358static int64_t make_posix_time(const struct tm *tm)
359{
360 int64_t t;
361 int64_t y = tm->tm_year;
362
363 /* See IEEE 1003.1:2004, section 4.14 */
364
365 t = (y-70)*365 + (y-69)/4 - (y-1)/100 + (y+299)/400;
366 t += tm->tm_yday;
367 t *= 24;
368 t += tm->tm_hour;
369 t *= 60;
370 t += tm->tm_min;
371 t *= 60;
372 t += tm->tm_sec;
373
374 return t;
375}
376
377static void timestamp(void)
378{
379 struct compile_time * const oct = &official_compile_time;
380 const struct tm *tp, *best_gm;
381
382 time(&oct->t);
383
384 best_gm = NULL;
385
386 tp = localtime(&oct->t);
387 if (tp) {
388 oct->local = *tp;
389 best_gm = &oct->local;
390 oct->have_local = true;
391 }
392
393 tp = gmtime(&oct->t);
394 if (tp) {
395 oct->gm = *tp;
396 best_gm = &oct->gm;
397 oct->have_gm = true;
398 if (!oct->have_local)
399 oct->local = oct->gm;
400 } else {
401 oct->gm = oct->local;
402 }
403
404 if (best_gm) {
405 oct->posix = make_posix_time(best_gm);
406 oct->have_posix = true;
407 }
408}
409
H. Peter Anvin038d8612007-04-12 16:54:50 +0000410int main(int argc, char **argv)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000411{
H. Peter Anvina771be82017-08-16 14:53:18 -0700412 StrList **depend_ptr;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700413
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700414 timestamp();
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800415
H. Peter Anvina7ecf262018-02-06 14:43:07 -0800416 iflag_set_default_cpu(&cpu);
417 iflag_set_default_cpu(&cmd_cpu);
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400418
Charles Crayne2581c862008-09-10 19:21:52 -0700419 pass0 = 0;
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700420 want_usage = terminate_after_phase = false;
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400421 nasm_set_verror(nasm_verror_gnu);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000422
H. Peter Anvin97e15752007-09-25 08:47:47 -0700423 error_file = stderr;
424
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -0700425 tolower_init();
H. Peter Anvin274cda82016-05-10 02:56:29 -0700426 src_init();
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -0700427
H. Peter Anvin, Intel87d9e622018-06-25 12:58:49 -0700428 /*
429 * We must call init_labels() before the command line parsing,
430 * because we may be setting prefixes/suffixes from the command
431 * line.
432 */
433 init_labels();
434
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000435 offsets = raa_init();
Keith Kaniosb7a89542007-04-12 02:40:54 +0000436 forwrefs = saa_init((int32_t)sizeof(struct forwrefinfo));
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000437
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000438 preproc = &nasmpp;
Cyrill Gorcunov3ed32cb2014-06-22 20:53:12 +0400439 operating_mode = OP_NORMAL;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000440
H. Peter Anvin55568c12016-10-03 19:46:49 -0700441 parse_cmdline(argc, argv, 1);
442 if (terminate_after_phase) {
443 if (want_usage)
444 usage();
445 return 1;
446 }
447
H. Peter Anvin169ac7c2016-09-25 17:08:05 -0700448 /*
449 * Define some macros dependent on the runtime, but not
H. Peter Anvin55568c12016-10-03 19:46:49 -0700450 * on the command line (as those are scanned in cmdline pass 2.)
H. Peter Anvin169ac7c2016-09-25 17:08:05 -0700451 */
452 preproc->init();
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800453 define_macros_early();
454
H. Peter Anvin55568c12016-10-03 19:46:49 -0700455 parse_cmdline(argc, argv, 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000456 if (terminate_after_phase) {
457 if (want_usage)
458 usage();
459 return 1;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000460 }
461
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800462 /* Save away the default state of warnings */
463 memcpy(warning_state_init, warning_state, sizeof warning_state);
464
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800465 if (!using_debug_info) {
466 /* No debug info, redirect to the null backend (empty stubs) */
H. Peter Anvina7bc15d2016-02-17 20:55:08 -0800467 dfmt = &null_debug_form;
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800468 } else if (!debug_format) {
469 /* Default debug format for this backend */
H. Peter Anvina7bc15d2016-02-17 20:55:08 -0800470 dfmt = ofmt->default_dfmt;
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800471 } else {
472 dfmt = dfmt_find(ofmt, debug_format);
473 if (!dfmt) {
474 nasm_fatal(ERR_NOFILE | ERR_USAGE,
475 "unrecognized debug format `%s' for"
476 " output format `%s'",
477 debug_format, ofmt->shortname);
478 }
479 }
H. Peter Anvinbb88d012003-09-10 23:34:23 +0000480
H. Peter Anvin76690a12002-04-30 20:52:49 +0000481 if (ofmt->stdmac)
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +0400482 preproc->extra_stdmac(ofmt->stdmac);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000483
Cyrill Gorcunov69bb0522018-09-22 13:46:45 +0300484 /*
485 * If no output file name provided and this
Cyrill Gorcunovda3780d2018-09-22 14:10:36 +0300486 * is a preprocess mode, we're perfectly
487 * fine to output into stdout.
Cyrill Gorcunov69bb0522018-09-22 13:46:45 +0300488 */
489 if (!outname) {
490 if (!(operating_mode & OP_PREPROCESS))
491 outname = filename_set_extension(inname, ofmt->extension);
492 }
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800493
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000494 /* define some macros dependent of command-line */
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800495 define_macros_late();
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000496
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400497 depend_ptr = (depend_file || (operating_mode & OP_DEPEND)) ? &depend_list : NULL;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700498
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700499 if (!depend_target)
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400500 depend_target = quote_for_make(outname);
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700501
Cyrill Gorcunove9fc88c2014-06-23 02:22:02 +0400502 if (operating_mode & OP_DEPEND) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000503 char *line;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700504
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400505 if (depend_missing_ok)
506 preproc->include_path(NULL); /* "assume generated" */
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700507
H. Peter Anvin130736c2016-02-17 20:27:41 -0800508 preproc->reset(inname, 0, depend_ptr);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000509 ofile = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000510 while ((line = preproc->getline()))
511 nasm_free(line);
512 preproc->cleanup(0);
Cyrill Gorcunove9fc88c2014-06-23 02:22:02 +0400513 } else if (operating_mode & OP_PREPROCESS) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000514 char *line;
H. Peter Anvin274cda82016-05-10 02:56:29 -0700515 const char *file_name = NULL;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000516 int32_t prior_linnum = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000517 int lineinc = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000518
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800519 if (outname) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -0700520 ofile = nasm_open_write(outname, NF_TEXT);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000521 if (!ofile)
H. Peter Anvin41087062016-03-03 14:27:34 -0800522 nasm_fatal(ERR_NOFILE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000523 "unable to open output file `%s'",
524 outname);
525 } else
526 ofile = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000527
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700528 location.known = false;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000529
Cyrill Gorcunovb574b072011-12-05 01:56:40 +0400530 /* pass = 1; */
H. Peter Anvin130736c2016-02-17 20:27:41 -0800531 preproc->reset(inname, 3, depend_ptr);
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800532
533 /* Revert all warnings to the default state */
534 memcpy(warning_state, warning_state_init, sizeof warning_state);
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700535
H. Peter Anvine2c80182005-01-15 22:15:51 +0000536 while ((line = preproc->getline())) {
537 /*
538 * We generate %line directives if needed for later programs
539 */
Keith Kaniosb7a89542007-04-12 02:40:54 +0000540 int32_t linnum = prior_linnum += lineinc;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000541 int altline = src_get(&linnum, &file_name);
542 if (altline) {
543 if (altline == 1 && lineinc == 1)
544 nasm_fputs("", ofile);
545 else {
546 lineinc = (altline != -1 || lineinc != 1);
547 fprintf(ofile ? ofile : stdout,
Keith Kanios93f2e9a2007-04-14 00:10:59 +0000548 "%%line %"PRId32"+%d %s\n", linnum, lineinc,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000549 file_name);
550 }
551 prior_linnum = linnum;
552 }
553 nasm_fputs(line, ofile);
554 nasm_free(line);
555 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000556 preproc->cleanup(0);
557 if (ofile)
558 fclose(ofile);
H. Peter Anvin29695c82018-06-14 17:04:32 -0700559 if (ofile && terminate_after_phase && !keep_all)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000560 remove(outname);
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400561 ofile = NULL;
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400562 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000563
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400564 if (operating_mode & OP_NORMAL) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -0700565 ofile = nasm_open_write(outname, (ofmt->flags & OFMT_TEXT) ? NF_TEXT : NF_BINARY);
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400566 if (!ofile)
H. Peter Anvin41087062016-03-03 14:27:34 -0800567 nasm_fatal(ERR_NOFILE,
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400568 "unable to open output file `%s'", outname);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000569
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400570 ofmt->init();
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400571 dfmt->init();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000572
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400573 assemble_file(inname, depend_ptr);
Victor van den Elzenc82c3722008-06-04 15:24:20 +0200574
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400575 if (!terminate_after_phase) {
H. Peter Anvin477ae442016-03-07 22:53:06 -0800576 ofmt->cleanup();
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400577 cleanup_labels();
578 fflush(ofile);
H. Peter Anvin274cda82016-05-10 02:56:29 -0700579 if (ferror(ofile)) {
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400580 nasm_error(ERR_NONFATAL|ERR_NOFILE,
581 "write error on output file `%s'", outname);
H. Peter Anvin274cda82016-05-10 02:56:29 -0700582 terminate_after_phase = true;
583 }
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400584 }
585
586 if (ofile) {
587 fclose(ofile);
H. Peter Anvin29695c82018-06-14 17:04:32 -0700588 if (terminate_after_phase && !keep_all)
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400589 remove(outname);
590 ofile = NULL;
591 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000592 }
H. Peter Anvin734b1882002-04-30 21:01:08 +0000593
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700594 if (depend_list && !terminate_after_phase)
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400595 emit_dependencies(depend_list);
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700596
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000597 if (want_usage)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000598 usage();
H. Peter Anvin734b1882002-04-30 21:01:08 +0000599
H. Peter Anvine2c80182005-01-15 22:15:51 +0000600 raa_free(offsets);
601 saa_free(forwrefs);
602 eval_cleanup();
H. Peter Anvin74cc5e52007-08-30 22:35:34 +0000603 stdscan_cleanup();
H. Peter Anvin274cda82016-05-10 02:56:29 -0700604 src_free();
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000605
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700606 return terminate_after_phase;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000607}
608
H. Peter Anvineba20a72002-04-30 20:53:55 +0000609/*
610 * Get a parameter for a command line option.
611 * First arg must be in the form of e.g. -f...
612 */
H. Peter Anvin423e3812007-11-15 17:12:29 -0800613static char *get_param(char *p, char *q, bool *advance)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000614{
H. Peter Anvin423e3812007-11-15 17:12:29 -0800615 *advance = false;
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +0400616 if (p[2]) /* the parameter's in the option */
617 return nasm_skip_spaces(p + 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000618 if (q && q[0]) {
H. Peter Anvin423e3812007-11-15 17:12:29 -0800619 *advance = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000620 return q;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000621 }
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400622 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000623 "option `-%c' requires an argument", p[1]);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000624 return NULL;
625}
626
H. Peter Anvindc242712007-11-18 11:55:10 -0800627/*
628 * Copy a filename
629 */
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800630static void copy_filename(const char **dst, const char *src, const char *what)
H. Peter Anvindc242712007-11-18 11:55:10 -0800631{
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800632 if (*dst)
633 nasm_fatal(0, "more than one %s file specified: %s\n", what, src);
H. Peter Anvindc242712007-11-18 11:55:10 -0800634
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800635 *dst = nasm_strdup(src);
H. Peter Anvindc242712007-11-18 11:55:10 -0800636}
637
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700638/*
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700639 * Convert a string to a POSIX make-safe form
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700640 */
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700641static char *quote_for_pmake(const char *str)
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700642{
643 const char *p;
644 char *os, *q;
645
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400646 size_t n = 1; /* Terminating zero */
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700647 size_t nbs = 0;
648
649 if (!str)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400650 return NULL;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700651
652 for (p = str; *p; p++) {
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400653 switch (*p) {
654 case ' ':
655 case '\t':
656 /* Convert N backslashes + ws -> 2N+1 backslashes + ws */
657 n += nbs + 2;
658 nbs = 0;
659 break;
660 case '$':
661 case '#':
662 nbs = 0;
663 n += 2;
664 break;
665 case '\\':
666 nbs++;
667 n++;
668 break;
669 default:
670 nbs = 0;
671 n++;
672 break;
673 }
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700674 }
675
676 /* Convert N backslashes at the end of filename to 2N backslashes */
677 if (nbs)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400678 n += nbs;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700679
680 os = q = nasm_malloc(n);
681
682 nbs = 0;
683 for (p = str; *p; p++) {
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400684 switch (*p) {
685 case ' ':
686 case '\t':
687 while (nbs--)
688 *q++ = '\\';
689 *q++ = '\\';
690 *q++ = *p;
691 break;
692 case '$':
693 *q++ = *p;
694 *q++ = *p;
695 nbs = 0;
696 break;
697 case '#':
698 *q++ = '\\';
699 *q++ = *p;
700 nbs = 0;
701 break;
702 case '\\':
703 *q++ = *p;
704 nbs++;
705 break;
706 default:
707 *q++ = *p;
708 nbs = 0;
709 break;
710 }
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700711 }
712 while (nbs--)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400713 *q++ = '\\';
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700714
715 *q = '\0';
716
717 return os;
718}
719
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700720/*
721 * Convert a string to a Watcom make-safe form
722 */
723static char *quote_for_wmake(const char *str)
724{
725 const char *p;
726 char *os, *q;
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700727 bool quote = false;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700728
729 size_t n = 1; /* Terminating zero */
730
731 if (!str)
732 return NULL;
733
734 for (p = str; *p; p++) {
735 switch (*p) {
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700736 case ' ':
737 case '\t':
H. Peter Anvin3e30c322017-08-16 22:20:36 -0700738 case '&':
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700739 quote = true;
740 n++;
741 break;
742 case '\"':
743 quote = true;
744 n += 2;
745 break;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700746 case '$':
747 case '#':
748 n += 2;
749 break;
750 default:
751 n++;
752 break;
753 }
754 }
755
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700756 if (quote)
757 n += 2;
758
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700759 os = q = nasm_malloc(n);
760
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700761 if (quote)
762 *q++ = '\"';
763
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700764 for (p = str; *p; p++) {
765 switch (*p) {
766 case '$':
767 case '#':
768 *q++ = '$';
769 *q++ = *p;
770 break;
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700771 case '\"':
772 *q++ = *p;
773 *q++ = *p;
774 break;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700775 default:
776 *q++ = *p;
777 break;
778 }
779 }
780
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700781 if (quote)
782 *q++ = '\"';
783
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700784 *q = '\0';
785
786 return os;
787}
788
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100789enum text_options {
H. Peter Anvin3366e312018-02-07 14:14:36 -0800790 OPT_BOGUS,
791 OPT_VERSION,
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700792 OPT_HELP,
H. Peter Anvin3366e312018-02-07 14:14:36 -0800793 OPT_ABORT_ON_PANIC,
H. Peter Anvin05990342018-06-11 13:32:42 -0700794 OPT_MANGLE,
795 OPT_INCLUDE,
796 OPT_PRAGMA,
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700797 OPT_BEFORE,
H. Peter Anvin29695c82018-06-14 17:04:32 -0700798 OPT_LIMIT,
799 OPT_KEEP_ALL
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100800};
H. Peter Anvin3366e312018-02-07 14:14:36 -0800801struct textargs {
802 const char *label;
803 enum text_options opt;
804 bool need_arg;
H. Peter Anvin98578072018-06-01 18:02:54 -0700805 int pvt;
H. Peter Anvin3366e312018-02-07 14:14:36 -0800806};
H. Peter Anvin2530a102016-02-18 02:28:15 -0800807static const struct textargs textopts[] = {
H. Peter Anvin98578072018-06-01 18:02:54 -0700808 {"v", OPT_VERSION, false, 0},
809 {"version", OPT_VERSION, false, 0},
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700810 {"help", OPT_HELP, false, 0},
H. Peter Anvin98578072018-06-01 18:02:54 -0700811 {"abort-on-panic", OPT_ABORT_ON_PANIC, false, 0},
812 {"prefix", OPT_MANGLE, true, LM_GPREFIX},
813 {"postfix", OPT_MANGLE, true, LM_GSUFFIX},
814 {"gprefix", OPT_MANGLE, true, LM_GPREFIX},
815 {"gpostfix", OPT_MANGLE, true, LM_GSUFFIX},
816 {"lprefix", OPT_MANGLE, true, LM_LPREFIX},
817 {"lpostfix", OPT_MANGLE, true, LM_LSUFFIX},
H. Peter Anvin05990342018-06-11 13:32:42 -0700818 {"include", OPT_INCLUDE, true, 0},
819 {"pragma", OPT_PRAGMA, true, 0},
820 {"before", OPT_BEFORE, true, 0},
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700821 {"limit-", OPT_LIMIT, true, 0},
H. Peter Anvin29695c82018-06-14 17:04:32 -0700822 {"keep-all", OPT_KEEP_ALL, false, 0},
H. Peter Anvin98578072018-06-01 18:02:54 -0700823 {NULL, OPT_BOGUS, false, 0}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000824};
825
Cyrill Gorcunove7438432014-05-09 22:34:34 +0400826static void show_version(void)
827{
828 printf("NASM version %s compiled on %s%s\n",
829 nasm_version, nasm_date, nasm_compile_options);
830 exit(0);
831}
832
H. Peter Anvin423e3812007-11-15 17:12:29 -0800833static bool stopoptions = false;
H. Peter Anvin55568c12016-10-03 19:46:49 -0700834static bool process_arg(char *p, char *q, int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000835{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000836 char *param;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800837 bool advance = false;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000838
839 if (!p || !p[0])
H. Peter Anvin423e3812007-11-15 17:12:29 -0800840 return false;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000841
H. Peter Anvine2c80182005-01-15 22:15:51 +0000842 if (p[0] == '-' && !stopoptions) {
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400843 if (strchr("oOfpPdDiIlFXuUZwW", p[1])) {
844 /* These parameters take values */
845 if (!(param = get_param(p, q, &advance)))
846 return advance;
847 }
H. Peter Anvin423e3812007-11-15 17:12:29 -0800848
H. Peter Anvine2c80182005-01-15 22:15:51 +0000849 switch (p[1]) {
850 case 's':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700851 if (pass == 1)
852 error_file = stdout;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000853 break;
H. Peter Anvin70653092007-10-19 14:42:29 -0700854
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400855 case 'o': /* output file */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700856 if (pass == 2)
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800857 copy_filename(&outname, param, "output");
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400858 break;
H. Peter Anvinfd7dd112007-10-10 14:06:59 -0700859
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400860 case 'f': /* output format */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700861 if (pass == 1) {
862 ofmt = ofmt_find(param, &ofmt_alias);
863 if (!ofmt) {
864 nasm_fatal(ERR_NOFILE | ERR_USAGE,
865 "unrecognised output format `%s' - "
866 "use -hf for a list", param);
867 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400868 }
869 break;
H. Peter Anvin70653092007-10-19 14:42:29 -0700870
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400871 case 'O': /* Optimization level */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700872 if (pass == 2) {
873 int opt;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700874
H. Peter Anvin55568c12016-10-03 19:46:49 -0700875 if (!*param) {
876 /* Naked -O == -Ox */
Chang S. Baea5786342018-08-15 23:22:21 +0300877 optimizing.level = MAX_OPTIMIZE;
H. Peter Anvin55568c12016-10-03 19:46:49 -0700878 } else {
879 while (*param) {
880 switch (*param) {
881 case '0': case '1': case '2': case '3': case '4':
882 case '5': case '6': case '7': case '8': case '9':
883 opt = strtoul(param, &param, 10);
H. Peter Anvind85d2502008-05-04 17:53:31 -0700884
Chang S. Baea5786342018-08-15 23:22:21 +0300885 /* -O0 -> optimizing.level == -1, 0.98 behaviour */
886 /* -O1 -> optimizing.level == 0, 0.98.09 behaviour */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700887 if (opt < 2)
Chang S. Baea5786342018-08-15 23:22:21 +0300888 optimizing.level = opt - 1;
H. Peter Anvin55568c12016-10-03 19:46:49 -0700889 else
Chang S. Baea5786342018-08-15 23:22:21 +0300890 optimizing.level = opt;
H. Peter Anvin55568c12016-10-03 19:46:49 -0700891 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700892
H. Peter Anvin55568c12016-10-03 19:46:49 -0700893 case 'v':
894 case '+':
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400895 param++;
896 opt_verbose_info = true;
897 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700898
H. Peter Anvin55568c12016-10-03 19:46:49 -0700899 case 'x':
900 param++;
Chang S. Baea5786342018-08-15 23:22:21 +0300901 optimizing.level = MAX_OPTIMIZE;
H. Peter Anvin55568c12016-10-03 19:46:49 -0700902 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700903
H. Peter Anvin55568c12016-10-03 19:46:49 -0700904 default:
905 nasm_fatal(0,
906 "unknown optimization option -O%c\n",
907 *param);
908 break;
909 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400910 }
Chang S. Baea5786342018-08-15 23:22:21 +0300911 if (optimizing.level > MAX_OPTIMIZE)
912 optimizing.level = MAX_OPTIMIZE;
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400913 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400914 }
915 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800916
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400917 case 'p': /* pre-include */
918 case 'P':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700919 if (pass == 2)
920 preproc->pre_include(param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400921 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800922
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400923 case 'd': /* pre-define */
924 case 'D':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700925 if (pass == 2)
926 preproc->pre_define(param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400927 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800928
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400929 case 'u': /* un-define */
930 case 'U':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700931 if (pass == 2)
932 preproc->pre_undefine(param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400933 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800934
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400935 case 'i': /* include search path */
936 case 'I':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700937 if (pass == 2)
938 preproc->include_path(param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400939 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800940
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400941 case 'l': /* listing file */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700942 if (pass == 2)
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800943 copy_filename(&listname, param, "listing");
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400944 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800945
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400946 case 'Z': /* error messages file */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700947 if (pass == 1)
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800948 copy_filename(&errname, param, "error");
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400949 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800950
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400951 case 'F': /* specify debug format */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700952 if (pass == 2) {
953 using_debug_info = true;
954 debug_format = param;
955 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400956 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800957
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400958 case 'X': /* specify error reporting format */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700959 if (pass == 1) {
960 if (nasm_stricmp("vc", param) == 0)
961 nasm_set_verror(nasm_verror_vc);
962 else if (nasm_stricmp("gnu", param) == 0)
963 nasm_set_verror(nasm_verror_gnu);
964 else
965 nasm_fatal(ERR_NOFILE | ERR_USAGE,
966 "unrecognized error reporting format `%s'",
967 param);
968 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000969 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800970
H. Peter Anvine2c80182005-01-15 22:15:51 +0000971 case 'g':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700972 if (pass == 2) {
973 using_debug_info = true;
974 if (p[2])
975 debug_format = nasm_skip_spaces(p + 2);
976 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000977 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800978
H. Peter Anvine2c80182005-01-15 22:15:51 +0000979 case 'h':
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700980 help(p[2]);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400981 exit(0); /* never need usage message here */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000982 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800983
H. Peter Anvine2c80182005-01-15 22:15:51 +0000984 case 'y':
985 printf("\nvalid debug formats for '%s' output format are"
986 " ('*' denotes default):\n", ofmt->shortname);
987 dfmt_list(ofmt, stdout);
988 exit(0);
989 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800990
H. Peter Anvine2c80182005-01-15 22:15:51 +0000991 case 't':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700992 if (pass == 2)
993 tasm_compatible_mode = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000994 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800995
H. Peter Anvine2c80182005-01-15 22:15:51 +0000996 case 'v':
Cyrill Gorcunove7438432014-05-09 22:34:34 +0400997 show_version();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000998 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800999
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001000 case 'e': /* preprocess only */
1001 case 'E':
H. Peter Anvin55568c12016-10-03 19:46:49 -07001002 if (pass == 1)
1003 operating_mode = OP_PREPROCESS;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001004 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001005
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001006 case 'a': /* assemble only - don't preprocess */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001007 if (pass == 1)
1008 preproc = &preproc_nop;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001009 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001010
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001011 case 'w':
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001012 case 'W':
H. Peter Anvin55568c12016-10-03 19:46:49 -07001013 if (pass == 2) {
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001014 if (!set_warning_status(param)) {
1015 nasm_error(ERR_WARNING|ERR_NOFILE|ERR_WARN_UNK_WARNING,
1016 "unknown warning option: %s", param);
H. Peter Anvin55568c12016-10-03 19:46:49 -07001017 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001018 }
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001019 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001020
H. Peter Anvine2c80182005-01-15 22:15:51 +00001021 case 'M':
H. Peter Anvin77c9bf62017-08-16 21:14:33 -07001022 if (pass == 1) {
1023 switch (p[2]) {
1024 case 'W':
1025 quote_for_make = quote_for_wmake;
1026 break;
1027 case 'D':
1028 case 'F':
1029 case 'T':
1030 case 'Q':
1031 advance = true;
1032 break;
1033 default:
1034 break;
1035 }
1036 } else {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001037 switch (p[2]) {
1038 case 0:
1039 operating_mode = OP_DEPEND;
1040 break;
1041 case 'G':
1042 operating_mode = OP_DEPEND;
1043 depend_missing_ok = true;
1044 break;
1045 case 'P':
1046 depend_emit_phony = true;
1047 break;
1048 case 'D':
1049 operating_mode = OP_NORMAL;
1050 depend_file = q;
1051 advance = true;
1052 break;
1053 case 'F':
1054 depend_file = q;
1055 advance = true;
1056 break;
1057 case 'T':
1058 depend_target = q;
1059 advance = true;
1060 break;
1061 case 'Q':
1062 depend_target = quote_for_make(q);
1063 advance = true;
1064 break;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -07001065 case 'W':
1066 /* handled in pass 1 */
1067 break;
H. Peter Anvin55568c12016-10-03 19:46:49 -07001068 default:
1069 nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
1070 "unknown dependency option `-M%c'", p[2]);
1071 break;
1072 }
H. Peter Anvin77c9bf62017-08-16 21:14:33 -07001073 }
1074 if (advance && (!q || !q[0])) {
1075 nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
1076 "option `-M%c' requires a parameter", p[2]);
1077 break;
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001078 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001079 break;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001080
H. Peter Anvine2c80182005-01-15 22:15:51 +00001081 case '-':
1082 {
H. Peter Anvin3366e312018-02-07 14:14:36 -08001083 const struct textargs *tx;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001084 size_t olen, plen;
H. Peter Anvinc805fd72018-06-12 14:23:05 -07001085 char *eqsave;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001086
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001087 p += 2;
1088
1089 if (!*p) { /* -- => stop processing options */
H. Peter Anvin3366e312018-02-07 14:14:36 -08001090 stopoptions = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001091 break;
1092 }
Cyrill Gorcunove7438432014-05-09 22:34:34 +04001093
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001094 plen = strlen(p);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001095 for (tx = textopts; tx->label; tx++) {
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001096 olen = strlen(tx->label);
1097
1098 if (olen > plen)
1099 continue;
1100
1101 if (nasm_memicmp(p, tx->label, olen))
1102 continue;
1103
1104 if (tx->label[olen-1] == '-')
1105 break; /* Incomplete option */
1106
1107 if (!p[olen] || p[olen] == '=')
1108 break; /* Complete option */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001109 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001110
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001111 if (!tx->label) {
1112 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
1113 "unrecognized option `--%s'", p);
1114 }
1115
H. Peter Anvinc805fd72018-06-12 14:23:05 -07001116 eqsave = param = strchr(p+olen, '=');
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001117 if (param)
1118 *param++ = '\0';
1119
H. Peter Anvin3366e312018-02-07 14:14:36 -08001120 if (tx->need_arg) {
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001121 if (!param) {
1122 param = q;
1123 advance = true;
1124 }
1125
1126 /* Note: a null string is a valid parameter */
1127 if (!param) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001128 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvin3366e312018-02-07 14:14:36 -08001129 "option `--%s' requires an argument",
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001130 p);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001131 break;
1132 }
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001133 } else {
1134 if (param) {
1135 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
1136 "option `--%s' does not take an argument",
1137 p);
1138
1139 }
H. Peter Anvin3366e312018-02-07 14:14:36 -08001140 }
1141
1142 switch (tx->opt) {
1143 case OPT_VERSION:
1144 show_version();
1145 break;
1146 case OPT_ABORT_ON_PANIC:
1147 abort_on_panic = true;
1148 break;
H. Peter Anvin98578072018-06-01 18:02:54 -07001149 case OPT_MANGLE:
H. Peter Anvin3366e312018-02-07 14:14:36 -08001150 if (pass == 2)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001151 set_label_mangle(tx->pvt, param);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001152 break;
H. Peter Anvin05990342018-06-11 13:32:42 -07001153 case OPT_INCLUDE:
1154 if (pass == 2)
1155 preproc->pre_include(q);
1156 break;
1157 case OPT_PRAGMA:
1158 if (pass == 2)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001159 preproc->pre_command("pragma", param);
H. Peter Anvin05990342018-06-11 13:32:42 -07001160 break;
1161 case OPT_BEFORE:
1162 if (pass == 2)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001163 preproc->pre_command(NULL, param);
H. Peter Anvin05990342018-06-11 13:32:42 -07001164 break;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001165 case OPT_LIMIT:
1166 if (pass == 2)
1167 nasm_set_limit(p+olen, param);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001168 break;
H. Peter Anvin29695c82018-06-14 17:04:32 -07001169 case OPT_KEEP_ALL:
1170 keep_all = true;
1171 break;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001172 case OPT_HELP:
1173 help(0);
1174 exit(0);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001175 default:
1176 panic();
H. Peter Anvine2c80182005-01-15 22:15:51 +00001177 }
H. Peter Anvinc805fd72018-06-12 14:23:05 -07001178
1179 if (eqsave)
1180 *eqsave = '='; /* Restore = argument separator */
1181
H. Peter Anvine2c80182005-01-15 22:15:51 +00001182 break;
1183 }
1184
1185 default:
H. Peter Anvinac061332017-03-31 11:41:16 -07001186 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
1187 "unrecognised option `-%c'", p[1]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001188 break;
1189 }
H. Peter Anvin55568c12016-10-03 19:46:49 -07001190 } else if (pass == 2) {
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001191 /* In theory we could allow multiple input files... */
1192 copy_filename(&inname, p, "input");
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001193 }
1194
1195 return advance;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001196}
1197
H. Peter Anvineba20a72002-04-30 20:53:55 +00001198#define ARG_BUF_DELTA 128
1199
H. Peter Anvin55568c12016-10-03 19:46:49 -07001200static void process_respfile(FILE * rfile, int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001201{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001202 char *buffer, *p, *q, *prevarg;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001203 int bufsize, prevargsize;
1204
1205 bufsize = prevargsize = ARG_BUF_DELTA;
1206 buffer = nasm_malloc(ARG_BUF_DELTA);
1207 prevarg = nasm_malloc(ARG_BUF_DELTA);
1208 prevarg[0] = '\0';
1209
H. Peter Anvine2c80182005-01-15 22:15:51 +00001210 while (1) { /* Loop to handle all lines in file */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001211 p = buffer;
1212 while (1) { /* Loop to handle long lines */
1213 q = fgets(p, bufsize - (p - buffer), rfile);
1214 if (!q)
1215 break;
1216 p += strlen(p);
1217 if (p > buffer && p[-1] == '\n')
1218 break;
1219 if (p - buffer > bufsize - 10) {
1220 int offset;
1221 offset = p - buffer;
1222 bufsize += ARG_BUF_DELTA;
1223 buffer = nasm_realloc(buffer, bufsize);
1224 p = buffer + offset;
1225 }
1226 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001227
H. Peter Anvine2c80182005-01-15 22:15:51 +00001228 if (!q && p == buffer) {
1229 if (prevarg[0])
H. Peter Anvin55568c12016-10-03 19:46:49 -07001230 process_arg(prevarg, NULL, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001231 nasm_free(buffer);
1232 nasm_free(prevarg);
1233 return;
1234 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001235
H. Peter Anvine2c80182005-01-15 22:15:51 +00001236 /*
1237 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
1238 * them are present at the end of the line.
1239 */
1240 *(p = &buffer[strcspn(buffer, "\r\n\032")]) = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001241
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001242 while (p > buffer && nasm_isspace(p[-1]))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001243 *--p = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001244
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +04001245 p = nasm_skip_spaces(buffer);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001246
H. Peter Anvin55568c12016-10-03 19:46:49 -07001247 if (process_arg(prevarg, p, pass))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001248 *p = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001249
Charles Crayne192d5b52007-10-18 19:02:42 -07001250 if ((int) strlen(p) > prevargsize - 10) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001251 prevargsize += ARG_BUF_DELTA;
1252 prevarg = nasm_realloc(prevarg, prevargsize);
1253 }
H. Peter Anvindc242712007-11-18 11:55:10 -08001254 strncpy(prevarg, p, prevargsize);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001255 }
1256}
1257
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001258/* Function to process args from a string of args, rather than the
1259 * argv array. Used by the environment variable and response file
1260 * processing.
1261 */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001262static void process_args(char *args, int pass)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001263{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001264 char *p, *q, *arg, *prevarg;
1265 char separator = ' ';
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001266
1267 p = args;
1268 if (*p && *p != '-')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001269 separator = *p++;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001270 arg = NULL;
1271 while (*p) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001272 q = p;
1273 while (*p && *p != separator)
1274 p++;
1275 while (*p == separator)
1276 *p++ = '\0';
1277 prevarg = arg;
1278 arg = q;
H. Peter Anvin55568c12016-10-03 19:46:49 -07001279 if (process_arg(prevarg, arg, pass))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001280 arg = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001281 }
1282 if (arg)
H. Peter Anvin55568c12016-10-03 19:46:49 -07001283 process_arg(arg, NULL, pass);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001284}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001285
H. Peter Anvin55568c12016-10-03 19:46:49 -07001286static void process_response_file(const char *file, int pass)
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001287{
1288 char str[2048];
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001289 FILE *f = nasm_open_read(file, NF_TEXT);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001290 if (!f) {
Cyrill Gorcunovf1964512013-02-15 12:14:06 +04001291 perror(file);
1292 exit(-1);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001293 }
1294 while (fgets(str, sizeof str, f)) {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001295 process_args(str, pass);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001296 }
1297 fclose(f);
1298}
1299
H. Peter Anvin55568c12016-10-03 19:46:49 -07001300static void parse_cmdline(int argc, char **argv, int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001301{
1302 FILE *rfile;
Cyrill Gorcunovf4941892011-07-17 13:55:25 +04001303 char *envreal, *envcopy = NULL, *p;
H. Peter Anvin972079f2008-09-30 17:01:23 -07001304 int i;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001305
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001306 /* Initialize all the warnings to their default state */
1307 for (i = 0; i < ERR_WARN_ALL; i++) {
1308 warning_state_init[i] = warning_state[i] =
1309 warnings[i].enabled ? WARN_ST_ENABLED : 0;
1310 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001311
1312 /*
H. Peter Anvinff7ccc02002-05-06 19:41:57 +00001313 * First, process the NASMENV environment variable.
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001314 */
H. Peter Anvinff7ccc02002-05-06 19:41:57 +00001315 envreal = getenv("NASMENV");
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001316 if (envreal) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001317 envcopy = nasm_strdup(envreal);
H. Peter Anvin55568c12016-10-03 19:46:49 -07001318 process_args(envcopy, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001319 nasm_free(envcopy);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001320 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001321
1322 /*
1323 * Now process the actual command line.
1324 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001325 while (--argc) {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001326 bool advance;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001327 argv++;
1328 if (argv[0][0] == '@') {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001329 /*
1330 * We have a response file, so process this as a set of
H. Peter Anvine2c80182005-01-15 22:15:51 +00001331 * arguments like the environment variable. This allows us
1332 * to have multiple arguments on a single line, which is
1333 * different to the -@resp file processing below for regular
1334 * NASM.
1335 */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001336 process_response_file(argv[0]+1, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001337 argc--;
1338 argv++;
1339 }
1340 if (!stopoptions && argv[0][0] == '-' && argv[0][1] == '@') {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001341 p = get_param(argv[0], argc > 1 ? argv[1] : NULL, &advance);
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001342 if (p) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001343 rfile = nasm_open_read(p, NF_TEXT);
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001344 if (rfile) {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001345 process_respfile(rfile, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001346 fclose(rfile);
1347 } else
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001348 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001349 "unable to open response file `%s'", p);
1350 }
1351 } else
H. Peter Anvin55568c12016-10-03 19:46:49 -07001352 advance = process_arg(argv[0], argc > 1 ? argv[1] : NULL, pass);
H. Peter Anvin423e3812007-11-15 17:12:29 -08001353 argv += advance, argc -= advance;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001354 }
1355
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001356 /*
1357 * Look for basic command line typos. This definitely doesn't
1358 * catch all errors, but it might help cases of fumbled fingers.
1359 */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001360 if (pass != 2)
1361 return;
1362
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001363 if (!inname)
1364 nasm_fatal(ERR_NOFILE | ERR_USAGE, "no input file specified");
H. Peter Anvin59ddd262007-10-01 11:26:31 -07001365
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001366 else if ((errname && !strcmp(inname, errname)) ||
1367 (outname && !strcmp(inname, outname)) ||
1368 (listname && !strcmp(inname, listname)) ||
1369 (depend_file && !strcmp(inname, depend_file)))
1370 nasm_fatal(ERR_USAGE, "will not overwrite input file");
1371
1372 if (errname) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001373 error_file = nasm_open_write(errname, NF_TEXT);
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001374 if (!error_file) {
1375 error_file = stderr; /* Revert to default! */
H. Peter Anvin41087062016-03-03 14:27:34 -08001376 nasm_fatal(ERR_NOFILE | ERR_USAGE,
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001377 "cannot open file `%s' for error messages",
1378 errname);
1379 }
Charles Craynefcce07f2007-09-30 22:15:36 -07001380 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001381}
1382
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001383static void assemble_file(const char *fname, StrList **depend_ptr)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001384{
H. Peter Anvinc7131682017-03-07 17:45:01 -08001385 char *line;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001386 insn output_ins;
H. Peter Anvinc7131682017-03-07 17:45:01 -08001387 int i;
H. Peter Anvin9c595b62017-03-07 19:44:21 -08001388 uint64_t prev_offset_changed;
H. Peter Anvin79561022018-06-15 17:51:39 -07001389 int64_t stall_count = 0; /* Make sure we make forward progress... */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001390
H. Peter Anvina7ecf262018-02-06 14:43:07 -08001391 switch (cmd_sb) {
1392 case 16:
1393 break;
1394 case 32:
1395 if (!iflag_cpu_level_ok(&cmd_cpu, IF_386))
1396 nasm_fatal(0, "command line: 32-bit segment size requires a higher cpu");
1397 break;
1398 case 64:
1399 if (!iflag_cpu_level_ok(&cmd_cpu, IF_X86_64))
1400 nasm_fatal(0, "command line: 64-bit segment size requires a higher cpu");
1401 break;
1402 default:
1403 panic();
1404 break;
1405 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001406
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001407 prev_offset_changed = nasm_limit[LIMIT_PASSES];
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001408 for (passn = 1; pass0 <= 2; passn++) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001409 pass1 = pass0 == 2 ? 2 : 1; /* 1, 1, 1, ..., 1, 2 */
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001410 pass2 = passn > 1 ? 2 : 1; /* 1, 2, 2, ..., 2, 2 */
1411 /* pass0 0, 0, 0, ..., 1, 2 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00001412
H. Peter Anvincac0b192017-03-28 16:12:30 -07001413 globalbits = cmd_sb; /* set 'bits' to command line default */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001414 cpu = cmd_cpu;
1415 if (pass0 == 2) {
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08001416 lfmt->init(listname);
H. Peter Anvin29695c82018-06-14 17:04:32 -07001417 } else if (passn == 1 && listname && !keep_all) {
H. Peter Anvinaac01ff2017-04-02 19:10:26 -07001418 /* Remove the list file in case we die before the output pass */
1419 remove(listname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001420 }
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001421 in_absolute = false;
Charles Craynec1905c22008-09-11 18:54:06 -07001422 global_offset_changed = 0; /* set by redefine_label */
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001423 if (passn > 1) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001424 saa_rewind(forwrefs);
1425 forwref = saa_rstruct(forwrefs);
1426 raa_free(offsets);
1427 offsets = raa_init();
1428 }
H. Peter Anvin892c4812018-05-30 14:43:46 -07001429 location.segment = NO_SEG;
1430 location.offset = 0;
1431 if (passn == 1)
1432 location.known = true;
1433 ofmt->reset();
1434 switch_segment(ofmt->section(NULL, pass2, &globalbits));
H. Peter Anvin130736c2016-02-17 20:27:41 -08001435 preproc->reset(fname, pass1, pass1 == 2 ? depend_ptr : NULL);
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001436
1437 /* Revert all warnings to the default state */
1438 memcpy(warning_state, warning_state_init, sizeof warning_state);
Victor van den Elzen819703a2008-07-16 15:20:56 +02001439
H. Peter Anvine2c80182005-01-15 22:15:51 +00001440 globallineno = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001441
H. Peter Anvine2c80182005-01-15 22:15:51 +00001442 while ((line = preproc->getline())) {
H. Peter Anvin79561022018-06-15 17:51:39 -07001443 if (++globallineno > nasm_limit[LIMIT_LINES])
1444 nasm_fatal(0,
1445 "overall line count exceeds the maximum %"PRId64"\n",
1446 nasm_limit[LIMIT_LINES]);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001447
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001448 /*
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001449 * Here we parse our directives; this is not handled by the
H. Peter Anvinc7131682017-03-07 17:45:01 -08001450 * main parser.
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001451 */
H. Peter Anvina6e26d92017-03-07 21:32:37 -08001452 if (process_directives(line))
H. Peter Anvinc7131682017-03-07 17:45:01 -08001453 goto end_of_line; /* Just do final cleanup */
H. Peter Anvin62b24d72007-08-29 16:38:05 +00001454
H. Peter Anvinc7131682017-03-07 17:45:01 -08001455 /* Not a directive, or even something that starts with [ */
H. Peter Anvin98578072018-06-01 18:02:54 -07001456 parse_line(pass1, line, &output_ins);
H. Peter Anvinc7131682017-03-07 17:45:01 -08001457
Chang S. Baea5786342018-08-15 23:22:21 +03001458 if (optimizing.level > 0) {
H. Peter Anvinc7131682017-03-07 17:45:01 -08001459 if (forwref != NULL && globallineno == forwref->lineno) {
1460 output_ins.forw_ref = true;
1461 do {
1462 output_ins.oprs[forwref->operand].opflags |= OPFLAG_FORWARD;
1463 forwref = saa_rstruct(forwrefs);
1464 } while (forwref != NULL
1465 && forwref->lineno == globallineno);
1466 } else
1467 output_ins.forw_ref = false;
1468
1469 if (output_ins.forw_ref) {
1470 if (passn == 1) {
1471 for (i = 0; i < output_ins.operands; i++) {
1472 if (output_ins.oprs[i].opflags & OPFLAG_FORWARD) {
1473 struct forwrefinfo *fwinf = (struct forwrefinfo *)saa_wstruct(forwrefs);
1474 fwinf->lineno = globallineno;
1475 fwinf->operand = i;
Cyrill Gorcunovd5f2aef2010-04-20 15:33:45 +04001476 }
1477 }
1478 }
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001479 }
H. Peter Anvinc7131682017-03-07 17:45:01 -08001480 }
H. Peter Anvin8bec2c72009-08-08 13:49:00 -07001481
H. Peter Anvinc7131682017-03-07 17:45:01 -08001482 /* forw_ref */
1483 if (output_ins.opcode == I_EQU) {
H. Peter Anvin98578072018-06-01 18:02:54 -07001484 if (!output_ins.label)
1485 nasm_error(ERR_NONFATAL,
1486 "EQU not preceded by label");
H. Peter Anvin73482482018-06-11 14:54:14 -07001487
H. Peter Anvin98578072018-06-01 18:02:54 -07001488 if (output_ins.operands == 1 &&
1489 (output_ins.oprs[0].type & IMMEDIATE) &&
1490 output_ins.oprs[0].wrt == NO_SEG) {
1491 define_label(output_ins.label,
1492 output_ins.oprs[0].segment,
1493 output_ins.oprs[0].offset, false);
1494 } else if (output_ins.operands == 2
1495 && (output_ins.oprs[0].type & IMMEDIATE)
1496 && (output_ins.oprs[0].type & COLON)
1497 && output_ins.oprs[0].segment == NO_SEG
1498 && output_ins.oprs[0].wrt == NO_SEG
1499 && (output_ins.oprs[1].type & IMMEDIATE)
1500 && output_ins.oprs[1].segment == NO_SEG
1501 && output_ins.oprs[1].wrt == NO_SEG) {
1502 define_label(output_ins.label,
1503 output_ins.oprs[0].offset | SEG_ABS,
1504 output_ins.oprs[1].offset, false);
H. Peter Anvinc7131682017-03-07 17:45:01 -08001505 } else {
H. Peter Anvin98578072018-06-01 18:02:54 -07001506 nasm_error(ERR_NONFATAL, "bad syntax for EQU");
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001507 }
H. Peter Anvinc7131682017-03-07 17:45:01 -08001508 } else { /* instruction isn't an EQU */
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001509 int32_t n;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001510
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001511 nasm_assert(output_ins.times >= 0);
H. Peter Anvinc7131682017-03-07 17:45:01 -08001512
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001513 for (n = 1; n <= output_ins.times; n++) {
1514 if (pass1 == 1) {
H. Peter Anvin892c4812018-05-30 14:43:46 -07001515 int64_t l = insn_size(location.segment,
1516 location.offset,
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001517 globalbits, &output_ins);
1518
1519 /* if (using_debug_info) && output_ins.opcode != -1) */
1520 if (using_debug_info)
1521 { /* fbk 03/25/01 */
H. Peter Anvinc7131682017-03-07 17:45:01 -08001522 /* this is done here so we can do debug type info */
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001523 int32_t typeinfo =
1524 TYS_ELEMENTS(output_ins.operands);
1525 switch (output_ins.opcode) {
1526 case I_RESB:
1527 typeinfo =
1528 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_BYTE;
1529 break;
1530 case I_RESW:
1531 typeinfo =
1532 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_WORD;
1533 break;
1534 case I_RESD:
1535 typeinfo =
1536 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_DWORD;
1537 break;
1538 case I_RESQ:
1539 typeinfo =
1540 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_QWORD;
1541 break;
1542 case I_REST:
1543 typeinfo =
1544 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_TBYTE;
1545 break;
1546 case I_RESO:
1547 typeinfo =
1548 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_OWORD;
1549 break;
1550 case I_RESY:
1551 typeinfo =
1552 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_YWORD;
1553 break;
1554 case I_RESZ:
1555 typeinfo =
1556 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_ZWORD;
1557 break;
1558 case I_DB:
1559 typeinfo |= TY_BYTE;
1560 break;
1561 case I_DW:
1562 typeinfo |= TY_WORD;
1563 break;
1564 case I_DD:
1565 if (output_ins.eops_float)
1566 typeinfo |= TY_FLOAT;
1567 else
1568 typeinfo |= TY_DWORD;
1569 break;
1570 case I_DQ:
1571 typeinfo |= TY_QWORD;
1572 break;
1573 case I_DT:
1574 typeinfo |= TY_TBYTE;
1575 break;
1576 case I_DO:
1577 typeinfo |= TY_OWORD;
1578 break;
1579 case I_DY:
1580 typeinfo |= TY_YWORD;
1581 break;
1582 case I_DZ:
1583 typeinfo |= TY_ZWORD;
1584 break;
1585 default:
1586 typeinfo = TY_LABEL;
1587 break;
1588 }
H. Peter Anvinc7131682017-03-07 17:45:01 -08001589
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001590 dfmt->debug_typevalue(typeinfo);
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001591 }
H. Peter Anvinc7131682017-03-07 17:45:01 -08001592
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001593 /*
1594 * For INCBIN, let the code in assemble
1595 * handle TIMES, so we don't have to read the
1596 * input file over and over.
1597 */
1598 if (l != -1) {
H. Peter Anvin892c4812018-05-30 14:43:46 -07001599 increment_offset(l);
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001600 }
1601 /*
1602 * else l == -1 => invalid instruction, which will be
1603 * flagged as an error on pass 2
1604 */
1605 } else {
1606 if (n == 2)
1607 lfmt->uplevel(LIST_TIMES);
H. Peter Anvin892c4812018-05-30 14:43:46 -07001608 increment_offset(assemble(location.segment,
1609 location.offset,
1610 globalbits, &output_ins));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001611 }
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001612 } /* not an EQU */
1613 }
1614 if (output_ins.times > 1)
1615 lfmt->downlevel(LIST_TIMES);
H. Peter Anvinc7131682017-03-07 17:45:01 -08001616
H. Peter Anvinc7131682017-03-07 17:45:01 -08001617 cleanup_insn(&output_ins);
1618
1619 end_of_line:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001620 nasm_free(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001621 } /* end while (line = preproc->getline... */
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001622
H. Peter Anvin (Intel)b45c03a2018-06-27 21:03:38 -07001623 if (global_offset_changed && !terminate_after_phase) {
1624 switch (pass0) {
1625 case 1:
1626 nasm_error(ERR_WARNING|ERR_WARN_PHASE,
1627 "phase error during stabilization pass, hoping for the best");
1628 break;
1629
1630 case 2:
1631 nasm_error(ERR_NONFATAL,
1632 "phase error during code generation pass");
1633 break;
1634
1635 default:
1636 /* This is normal, we'll keep going... */
1637 break;
1638 }
1639 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001640
H. Peter Anvine2c80182005-01-15 22:15:51 +00001641 if (pass1 == 1)
1642 preproc->cleanup(1);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001643
H. Peter Anvin (Intel)12810fa2018-06-27 20:17:33 -07001644 /*
1645 * Always run at least two optimization passes (pass0 == 0);
1646 * things like subsections will fail miserably without that.
1647 * Once we commit to a stabilization pass (pass0 == 1), we can't
1648 * go back, and if something goes bad, we can only hope
1649 * that we don't end up with a phase error at the end.
1650 */
1651 if ((passn > 1 && !global_offset_changed) || pass0 > 0) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001652 pass0++;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001653 } else if (global_offset_changed &&
1654 global_offset_changed < prev_offset_changed) {
Charles Craynec1905c22008-09-11 18:54:06 -07001655 prev_offset_changed = global_offset_changed;
1656 stall_count = 0;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001657 } else {
1658 stall_count++;
1659 }
Victor van den Elzen42528232008-09-11 15:07:05 +02001660
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001661 if (terminate_after_phase)
1662 break;
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001663
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001664 if ((stall_count > nasm_limit[LIMIT_STALLED]) ||
1665 (passn >= nasm_limit[LIMIT_PASSES])) {
Victor van den Elzen42528232008-09-11 15:07:05 +02001666 /* We get here if the labels don't converge
1667 * Example: FOO equ FOO + 1
1668 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001669 nasm_error(ERR_NONFATAL,
Victor van den Elzen42528232008-09-11 15:07:05 +02001670 "Can't find valid values for all labels "
H. Peter Anvin79561022018-06-15 17:51:39 -07001671 "after %"PRId64" passes, giving up.", passn);
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001672 nasm_error(ERR_NONFATAL,
1673 "Possible causes: recursive EQUs, macro abuse.");
1674 break;
1675 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001676 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001677
H. Peter Anvine2c80182005-01-15 22:15:51 +00001678 preproc->cleanup(0);
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08001679 lfmt->cleanup();
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001680 if (!terminate_after_phase && opt_verbose_info) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001681 /* -On and -Ov switches */
H. Peter Anvin79561022018-06-15 17:51:39 -07001682 fprintf(stdout, "info: assembly required 1+%"PRId64"+1 passes\n",
1683 passn-3);
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001684 }
1685}
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001686
Ed Berosetfa771012002-06-09 20:56:40 +00001687/**
1688 * gnu style error reporting
1689 * This function prints an error message to error_file in the
1690 * style used by GNU. An example would be:
1691 * file.asm:50: error: blah blah blah
H. Peter Anvin70653092007-10-19 14:42:29 -07001692 * where file.asm is the name of the file, 50 is the line number on
Ed Berosetfa771012002-06-09 20:56:40 +00001693 * which the error occurs (or is detected) and "error:" is one of
1694 * the possible optional diagnostics -- it can be "error" or "warning"
H. Peter Anvin70653092007-10-19 14:42:29 -07001695 * or something else. Finally the line terminates with the actual
Ed Berosetfa771012002-06-09 20:56:40 +00001696 * error message.
H. Peter Anvin70653092007-10-19 14:42:29 -07001697 *
1698 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001699 * @param fmt the printf style format string
1700 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001701static void nasm_verror_gnu(int severity, const char *fmt, va_list ap)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001702{
H. Peter Anvin274cda82016-05-10 02:56:29 -07001703 const char *currentfile = NULL;
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001704 int32_t lineno = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001705
Ed Berosetfa771012002-06-09 20:56:40 +00001706 if (is_suppressed_warning(severity))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001707 return;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001708
H. Peter Anvin7a6bf742017-12-20 11:54:31 -08001709 if (!(severity & ERR_NOFILE)) {
H. Peter Anvin4def1a82016-05-09 13:59:44 -07001710 src_get(&lineno, &currentfile);
H. Peter Anvin7a6bf742017-12-20 11:54:31 -08001711 if (!currentfile || (severity & ERR_TOPFILE)) {
1712 currentfile = inname[0] ? inname : outname[0] ? outname : NULL;
1713 lineno = 0;
1714 }
1715 }
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001716
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001717 if (!skip_this_pass(severity)) {
H. Peter Anvin7a6bf742017-12-20 11:54:31 -08001718 if (!lineno)
1719 fprintf(error_file, "%s:", currentfile ? currentfile : "nasm");
H. Peter Anvin883985d2017-12-20 11:32:39 -08001720 else
1721 fprintf(error_file, "%s:%"PRId32": ", currentfile, lineno);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001722 }
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001723
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001724 nasm_verror_common(severity, fmt, ap);
Ed Berosetfa771012002-06-09 20:56:40 +00001725}
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001726
Ed Berosetfa771012002-06-09 20:56:40 +00001727/**
1728 * MS style error reporting
1729 * This function prints an error message to error_file in the
H. Peter Anvin70653092007-10-19 14:42:29 -07001730 * style used by Visual C and some other Microsoft tools. An example
Ed Berosetfa771012002-06-09 20:56:40 +00001731 * would be:
Ed Beroset6e61d0d2002-06-11 03:29:36 +00001732 * file.asm(50) : error: blah blah blah
H. Peter Anvin70653092007-10-19 14:42:29 -07001733 * where file.asm is the name of the file, 50 is the line number on
Ed Beroset6e61d0d2002-06-11 03:29:36 +00001734 * which the error occurs (or is detected) and "error:" is one of
1735 * the possible optional diagnostics -- it can be "error" or "warning"
H. Peter Anvin70653092007-10-19 14:42:29 -07001736 * or something else. Finally the line terminates with the actual
Ed Beroset6e61d0d2002-06-11 03:29:36 +00001737 * error message.
H. Peter Anvin70653092007-10-19 14:42:29 -07001738 *
1739 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001740 * @param fmt the printf style format string
1741 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001742static void nasm_verror_vc(int severity, const char *fmt, va_list ap)
Ed Berosetfa771012002-06-09 20:56:40 +00001743{
H. Peter Anvin274cda82016-05-10 02:56:29 -07001744 const char *currentfile = NULL;
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001745 int32_t lineno = 0;
Ed Berosetfa771012002-06-09 20:56:40 +00001746
H. Peter Anvine2c80182005-01-15 22:15:51 +00001747 if (is_suppressed_warning(severity))
1748 return;
Ed Berosetfa771012002-06-09 20:56:40 +00001749
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001750 if (!(severity & ERR_NOFILE))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001751 src_get(&lineno, &currentfile);
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001752
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001753 if (!skip_this_pass(severity)) {
1754 if (currentfile) {
1755 fprintf(error_file, "%s(%"PRId32") : ", currentfile, lineno);
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001756 } else {
1757 fputs("nasm: ", error_file);
1758 }
Ed Berosetfa771012002-06-09 20:56:40 +00001759 }
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001760
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001761 nasm_verror_common(severity, fmt, ap);
Ed Berosetfa771012002-06-09 20:56:40 +00001762}
1763
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001764/*
1765 * check to see if this is a suppressable warning
1766 */
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001767static inline bool is_valid_warning(int severity)
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001768{
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001769 /* Not a warning at all */
1770 if ((severity & ERR_MASK) != ERR_WARNING)
1771 return false;
1772
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001773 return WARN_IDX(severity) < ERR_WARN_ALL;
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001774}
1775
Ed Berosetfa771012002-06-09 20:56:40 +00001776/**
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001777 * check for suppressed warning
H. Peter Anvin70653092007-10-19 14:42:29 -07001778 * checks for suppressed warning or pass one only warning and we're
Ed Berosetfa771012002-06-09 20:56:40 +00001779 * not in pass 1
1780 *
1781 * @param severity the severity of the warning or error
1782 * @return true if we should abort error/warning printing
1783 */
H. Peter Anvin2b046cf2008-01-22 14:08:36 -08001784static bool is_suppressed_warning(int severity)
Ed Berosetfa771012002-06-09 20:56:40 +00001785{
H. Peter Anvin442a05a2012-02-24 21:50:53 -08001786 /* Might be a warning but suppresed explicitly */
H. Peter Anvinc805fd72018-06-12 14:23:05 -07001787 if (is_valid_warning(severity) && !(severity & ERR_USAGE))
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001788 return !(warning_state[WARN_IDX(severity)] & WARN_ST_ENABLED);
1789 else
1790 return false;
1791}
1792
1793static bool warning_is_error(int severity)
1794{
1795 if (is_valid_warning(severity))
1796 return !!(warning_state[WARN_IDX(severity)] & WARN_ST_ERROR);
H. Peter Anvin442a05a2012-02-24 21:50:53 -08001797 else
1798 return false;
Ed Berosetfa771012002-06-09 20:56:40 +00001799}
1800
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001801static bool skip_this_pass(int severity)
1802{
H. Peter Anvin8f622462017-04-02 19:02:29 -07001803 /*
1804 * See if it's a pass-specific error or warning which should be skipped.
1805 * We cannot skip errors stronger than ERR_NONFATAL as by definition
1806 * they cannot be resumed from.
1807 */
1808 if ((severity & ERR_MASK) > ERR_NONFATAL)
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001809 return false;
1810
H. Peter Anvin934f0472016-05-09 12:00:19 -07001811 /*
1812 * passn is 1 on the very first pass only.
1813 * pass0 is 2 on the code-generation (final) pass only.
1814 * These are the passes we care about in this case.
1815 */
1816 return (((severity & ERR_PASS1) && passn != 1) ||
1817 ((severity & ERR_PASS2) && pass0 != 2));
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001818}
1819
Ed Berosetfa771012002-06-09 20:56:40 +00001820/**
1821 * common error reporting
1822 * This is the common back end of the error reporting schemes currently
H. Peter Anvin70653092007-10-19 14:42:29 -07001823 * implemented. It prints the nature of the warning and then the
Ed Berosetfa771012002-06-09 20:56:40 +00001824 * specific error message to error_file and may or may not return. It
1825 * doesn't return if the error severity is a "panic" or "debug" type.
H. Peter Anvin70653092007-10-19 14:42:29 -07001826 *
1827 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001828 * @param fmt the printf style format string
1829 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001830static void nasm_verror_common(int severity, const char *fmt, va_list args)
Ed Berosetfa771012002-06-09 20:56:40 +00001831{
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001832 char msg[1024];
1833 const char *pfx;
H. Peter Anvin323fcff2009-07-12 12:04:56 -07001834
H. Peter Anvin7df04172008-06-10 18:27:38 -07001835 switch (severity & (ERR_MASK|ERR_NO_SEVERITY)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001836 case ERR_WARNING:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001837 pfx = "warning: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001838 break;
1839 case ERR_NONFATAL:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001840 pfx = "error: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001841 break;
1842 case ERR_FATAL:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001843 pfx = "fatal: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001844 break;
1845 case ERR_PANIC:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001846 pfx = "panic: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001847 break;
1848 case ERR_DEBUG:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001849 pfx = "debug: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001850 break;
H. Peter Anvin7df04172008-06-10 18:27:38 -07001851 default:
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001852 pfx = "";
1853 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001854 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00001855
H. Peter Anvin934f0472016-05-09 12:00:19 -07001856 vsnprintf(msg, sizeof msg - 64, fmt, args);
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001857 if (is_valid_warning(severity) && WARN_IDX(severity) != ERR_WARN_OTHER) {
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001858 char *p = strchr(msg, '\0');
H. Peter Anvin934f0472016-05-09 12:00:19 -07001859 snprintf(p, 64, " [-w+%s]", warnings[WARN_IDX(severity)].name);
1860 }
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001861
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001862 if (!skip_this_pass(severity))
1863 fprintf(error_file, "%s%s\n", pfx, msg);
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001864
H. Peter Anvin4def1a82016-05-09 13:59:44 -07001865 /* Are we recursing from error_list_macros? */
1866 if (severity & ERR_PP_LISTMACRO)
1867 return;
1868
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001869 /*
1870 * Don't suppress this with skip_this_pass(), or we don't get
H. Peter Anvin934f0472016-05-09 12:00:19 -07001871 * pass1 or preprocessor warnings in the list file
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001872 */
H. Peter Anvin0fcb4882016-07-06 11:55:25 -07001873 lfmt->error(severity, pfx, msg);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001874
H. Peter Anvin8f622462017-04-02 19:02:29 -07001875 if (skip_this_pass(severity))
1876 return;
1877
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001878 if (severity & ERR_USAGE)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001879 want_usage = true;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001880
H. Peter Anvin4def1a82016-05-09 13:59:44 -07001881 preproc->error_list_macros(severity);
1882
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001883 switch (severity & ERR_MASK) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001884 case ERR_DEBUG:
1885 /* no further action, by definition */
1886 break;
H. Peter Anvinb030c922007-11-13 11:31:15 -08001887 case ERR_WARNING:
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001888 /* Treat warnings as errors */
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001889 if (warning_is_error(severity))
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001890 terminate_after_phase = true;
1891 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001892 case ERR_NONFATAL:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001893 terminate_after_phase = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001894 break;
1895 case ERR_FATAL:
1896 if (ofile) {
1897 fclose(ofile);
H. Peter Anvin29695c82018-06-14 17:04:32 -07001898 if (!keep_all)
1899 remove(outname);
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001900 ofile = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001901 }
1902 if (want_usage)
1903 usage();
1904 exit(1); /* instantly die */
1905 break; /* placate silly compilers */
1906 case ERR_PANIC:
1907 fflush(NULL);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001908
1909 if (abort_on_panic)
1910 abort(); /* halt, catch fire, dump core/stop debugger */
1911
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001912 if (ofile) {
1913 fclose(ofile);
H. Peter Anvin29695c82018-06-14 17:04:32 -07001914 if (!keep_all)
1915 remove(outname);
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001916 ofile = NULL;
1917 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001918 exit(3);
1919 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001920 }
1921}
1922
H. Peter Anvin734b1882002-04-30 21:01:08 +00001923static void usage(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001924{
H. Peter Anvin620515a2002-04-30 20:57:38 +00001925 fputs("type `nasm -h' for help\n", error_file);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001926}
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001927
1928static void help(const char xopt)
1929{
1930 int i;
1931
1932 printf
1933 ("usage: nasm [-@ response file] [-o outfile] [-f format] "
1934 "[-l listfile]\n"
1935 " [options...] [--] filename\n"
1936 " or nasm -v (or --v) for version info\n\n"
1937 "\n"
1938 "Response files should contain command line parameters,\n"
1939 "one per line.\n"
1940 "\n"
1941 " -t assemble in SciTech TASM compatible mode\n");
1942 printf
1943 (" -E (or -e) preprocess only (writes output to stdout by default)\n"
1944 " -a don't preprocess (assemble only)\n"
1945 " -M generate Makefile dependencies on stdout\n"
1946 " -MG d:o, missing files assumed generated\n"
1947 " -MF file set Makefile dependency file\n"
1948 " -MD file assemble and generate dependencies\n"
1949 " -MT file dependency target name\n"
1950 " -MQ file dependency target name (quoted)\n"
1951 " -MP emit phony target\n\n"
1952 " -Zfile redirect error messages to file\n"
1953 " -s redirect error messages to stdout\n\n"
1954 " -g generate debugging information\n\n"
1955 " -F format select a debugging format\n\n"
1956 " -gformat same as -g -F format\n\n"
1957 " -o outfile write output to an outfile\n\n"
1958 " -f format select an output format\n\n"
1959 " -l listfile write listing to a listfile\n\n"
1960 " -Ipath add a pathname to the include file path\n");
1961 printf
1962 (" -Olevel optimize opcodes, immediates and branch offsets\n"
1963 " -O0 no optimization\n"
1964 " -O1 minimal optimization\n"
1965 " -Ox multipass optimization (default)\n"
1966 " -Pfile pre-include a file (also --include)\n"
1967 " -Dmacro[=str] pre-define a macro\n"
1968 " -Umacro undefine a macro\n"
1969 " -Xformat specifiy error reporting format (gnu or vc)\n"
1970 " -w+foo enable warning foo (equiv. -Wfoo)\n"
1971 " -w-foo disable warning foo (equiv. -Wno-foo)\n"
1972 " -w[+-]error[=foo]\n"
1973 " promote [specific] warnings to errors\n"
Chang S. Bae1af6ef42018-06-20 17:05:12 -07001974 " -h show invocation summary and exit (also --help)\n\n"
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001975 " --pragma str pre-executes a specific %%pragma\n"
1976 " --before str add line (usually a preprocessor statement) before the input\n"
1977 " --prefix str prepend the given string to all the given string\n"
Chang S. Bae1af6ef42018-06-20 17:05:12 -07001978 " to all extern, common and global symbols (also --gprefix)\n"
1979 " --postfix str append the given string to all the given string\n"
1980 " to all extern, common and global symbols (also --gpostfix)\n"
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001981 " --lprefix str prepend the given string to all other symbols\n"
Chang S. Bae1af6ef42018-06-20 17:05:12 -07001982 " --lpostfix str append the given string to all other symbols\n"
1983 " --keep-all output files will not be removed even if an error happens\n"
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001984 " --limit-X val set execution limit X\n");
1985
1986 for (i = 0; i <= LIMIT_MAX; i++) {
1987 printf(" %-15s %s (default ",
1988 limit_info[i].name, limit_info[i].help);
1989 if (nasm_limit[i] < LIMIT_MAX_VAL) {
H. Peter Anvin79561022018-06-15 17:51:39 -07001990 printf("%"PRId64")\n", nasm_limit[i]);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001991 } else {
1992 printf("unlimited)\n");
1993 }
1994 }
1995
1996 printf("\nWarnings for the -W/-w options:\n");
1997
1998 for (i = 0; i <= ERR_WARN_ALL; i++)
1999 printf(" %-23s %s%s\n",
2000 warnings[i].name, warnings[i].help,
2001 i == ERR_WARN_ALL ? "\n" :
2002 warnings[i].enabled ? " (default on)" :
2003 " (default off)");
2004
2005 if (xopt == 'f') {
2006 printf("valid output formats for -f are"
2007 " (`*' denotes default):\n");
2008 ofmt_list(ofmt, stdout);
2009 } else {
2010 printf("For a list of valid output formats, use -hf.\n");
2011 printf("For a list of debug formats, use -f <format> -y.\n");
2012 }
2013}