blob: 545a81b182310a87f0230fcb510dc5b301d8f11d [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 Anvina3d96d02018-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 Anvina3d96d02018-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 Anvina3d96d02018-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 Anvina3d96d02018-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 Anvina3d96d02018-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) {
H. Peter Anvinc5136902018-06-15 18:20:17 -0700474 nasm_fatal_fl(ERR_NOFILE | ERR_USAGE,
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800475 "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 Anvinc5136902018-06-15 18:20:17 -0700522 nasm_fatal_fl(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 Anvinc5136902018-06-15 18:20:17 -0700567 nasm_fatal_fl(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)
H. Peter Anvinc5136902018-06-15 18:20:17 -0700633 nasm_fatal("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) {
H. Peter Anvinc5136902018-06-15 18:20:17 -0700864 nasm_fatal_fl(ERR_NOFILE | ERR_USAGE,
H. Peter Anvin55568c12016-10-03 19:46:49 -0700865 "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:
H. Peter Anvinc5136902018-06-15 18:20:17 -0700905 nasm_fatal("unknown optimization option -O%c\n",
H. Peter Anvin55568c12016-10-03 19:46:49 -0700906 *param);
907 break;
908 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400909 }
Chang S. Baea5786342018-08-15 23:22:21 +0300910 if (optimizing.level > MAX_OPTIMIZE)
911 optimizing.level = MAX_OPTIMIZE;
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400912 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400913 }
914 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800915
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400916 case 'p': /* pre-include */
917 case 'P':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700918 if (pass == 2)
919 preproc->pre_include(param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400920 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800921
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400922 case 'd': /* pre-define */
923 case 'D':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700924 if (pass == 2)
925 preproc->pre_define(param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400926 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800927
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400928 case 'u': /* un-define */
929 case 'U':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700930 if (pass == 2)
931 preproc->pre_undefine(param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400932 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800933
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400934 case 'i': /* include search path */
935 case 'I':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700936 if (pass == 2)
937 preproc->include_path(param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400938 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800939
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400940 case 'l': /* listing file */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700941 if (pass == 2)
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800942 copy_filename(&listname, param, "listing");
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400943 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800944
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400945 case 'Z': /* error messages file */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700946 if (pass == 1)
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800947 copy_filename(&errname, param, "error");
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400948 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800949
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400950 case 'F': /* specify debug format */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700951 if (pass == 2) {
952 using_debug_info = true;
953 debug_format = param;
954 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400955 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800956
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400957 case 'X': /* specify error reporting format */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700958 if (pass == 1) {
959 if (nasm_stricmp("vc", param) == 0)
960 nasm_set_verror(nasm_verror_vc);
961 else if (nasm_stricmp("gnu", param) == 0)
962 nasm_set_verror(nasm_verror_gnu);
963 else
H. Peter Anvinc5136902018-06-15 18:20:17 -0700964 nasm_fatal_fl(ERR_NOFILE | ERR_USAGE,
H. Peter Anvin55568c12016-10-03 19:46:49 -0700965 "unrecognized error reporting format `%s'",
966 param);
967 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000968 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800969
H. Peter Anvine2c80182005-01-15 22:15:51 +0000970 case 'g':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700971 if (pass == 2) {
972 using_debug_info = true;
973 if (p[2])
974 debug_format = nasm_skip_spaces(p + 2);
975 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000976 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800977
H. Peter Anvine2c80182005-01-15 22:15:51 +0000978 case 'h':
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700979 help(p[2]);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400980 exit(0); /* never need usage message here */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000981 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800982
H. Peter Anvine2c80182005-01-15 22:15:51 +0000983 case 'y':
984 printf("\nvalid debug formats for '%s' output format are"
985 " ('*' denotes default):\n", ofmt->shortname);
986 dfmt_list(ofmt, stdout);
987 exit(0);
988 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800989
H. Peter Anvine2c80182005-01-15 22:15:51 +0000990 case 't':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700991 if (pass == 2)
992 tasm_compatible_mode = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000993 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800994
H. Peter Anvine2c80182005-01-15 22:15:51 +0000995 case 'v':
Cyrill Gorcunove7438432014-05-09 22:34:34 +0400996 show_version();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000997 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800998
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400999 case 'e': /* preprocess only */
1000 case 'E':
H. Peter Anvin55568c12016-10-03 19:46:49 -07001001 if (pass == 1)
1002 operating_mode = OP_PREPROCESS;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001003 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001004
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001005 case 'a': /* assemble only - don't preprocess */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001006 if (pass == 1)
1007 preproc = &preproc_nop;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001008 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001009
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001010 case 'w':
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001011 case 'W':
H. Peter Anvin55568c12016-10-03 19:46:49 -07001012 if (pass == 2) {
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001013 if (!set_warning_status(param)) {
1014 nasm_error(ERR_WARNING|ERR_NOFILE|ERR_WARN_UNK_WARNING,
1015 "unknown warning option: %s", param);
H. Peter Anvin55568c12016-10-03 19:46:49 -07001016 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001017 }
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001018 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001019
H. Peter Anvine2c80182005-01-15 22:15:51 +00001020 case 'M':
H. Peter Anvin77c9bf62017-08-16 21:14:33 -07001021 if (pass == 1) {
1022 switch (p[2]) {
1023 case 'W':
1024 quote_for_make = quote_for_wmake;
1025 break;
1026 case 'D':
1027 case 'F':
1028 case 'T':
1029 case 'Q':
1030 advance = true;
1031 break;
1032 default:
1033 break;
1034 }
1035 } else {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001036 switch (p[2]) {
1037 case 0:
1038 operating_mode = OP_DEPEND;
1039 break;
1040 case 'G':
1041 operating_mode = OP_DEPEND;
1042 depend_missing_ok = true;
1043 break;
1044 case 'P':
1045 depend_emit_phony = true;
1046 break;
1047 case 'D':
1048 operating_mode = OP_NORMAL;
1049 depend_file = q;
1050 advance = true;
1051 break;
1052 case 'F':
1053 depend_file = q;
1054 advance = true;
1055 break;
1056 case 'T':
1057 depend_target = q;
1058 advance = true;
1059 break;
1060 case 'Q':
1061 depend_target = quote_for_make(q);
1062 advance = true;
1063 break;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -07001064 case 'W':
1065 /* handled in pass 1 */
1066 break;
H. Peter Anvin55568c12016-10-03 19:46:49 -07001067 default:
1068 nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
1069 "unknown dependency option `-M%c'", p[2]);
1070 break;
1071 }
H. Peter Anvin77c9bf62017-08-16 21:14:33 -07001072 }
1073 if (advance && (!q || !q[0])) {
1074 nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
1075 "option `-M%c' requires a parameter", p[2]);
1076 break;
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001077 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001078 break;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001079
H. Peter Anvine2c80182005-01-15 22:15:51 +00001080 case '-':
1081 {
H. Peter Anvin3366e312018-02-07 14:14:36 -08001082 const struct textargs *tx;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001083 size_t olen, plen;
H. Peter Anvinc805fd72018-06-12 14:23:05 -07001084 char *eqsave;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001085
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001086 p += 2;
1087
1088 if (!*p) { /* -- => stop processing options */
H. Peter Anvin3366e312018-02-07 14:14:36 -08001089 stopoptions = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001090 break;
1091 }
Cyrill Gorcunove7438432014-05-09 22:34:34 +04001092
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001093 plen = strlen(p);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001094 for (tx = textopts; tx->label; tx++) {
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001095 olen = strlen(tx->label);
1096
1097 if (olen > plen)
1098 continue;
1099
1100 if (nasm_memicmp(p, tx->label, olen))
1101 continue;
1102
1103 if (tx->label[olen-1] == '-')
1104 break; /* Incomplete option */
1105
1106 if (!p[olen] || p[olen] == '=')
1107 break; /* Complete option */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001108 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001109
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001110 if (!tx->label) {
1111 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
1112 "unrecognized option `--%s'", p);
1113 }
1114
H. Peter Anvinc805fd72018-06-12 14:23:05 -07001115 eqsave = param = strchr(p+olen, '=');
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001116 if (param)
1117 *param++ = '\0';
1118
H. Peter Anvin3366e312018-02-07 14:14:36 -08001119 if (tx->need_arg) {
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001120 if (!param) {
1121 param = q;
1122 advance = true;
1123 }
1124
1125 /* Note: a null string is a valid parameter */
1126 if (!param) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001127 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvin3366e312018-02-07 14:14:36 -08001128 "option `--%s' requires an argument",
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001129 p);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001130 break;
1131 }
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001132 } else {
1133 if (param) {
1134 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
1135 "option `--%s' does not take an argument",
1136 p);
1137
1138 }
H. Peter Anvin3366e312018-02-07 14:14:36 -08001139 }
1140
1141 switch (tx->opt) {
1142 case OPT_VERSION:
1143 show_version();
1144 break;
1145 case OPT_ABORT_ON_PANIC:
1146 abort_on_panic = true;
1147 break;
H. Peter Anvin98578072018-06-01 18:02:54 -07001148 case OPT_MANGLE:
H. Peter Anvin3366e312018-02-07 14:14:36 -08001149 if (pass == 2)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001150 set_label_mangle(tx->pvt, param);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001151 break;
H. Peter Anvin05990342018-06-11 13:32:42 -07001152 case OPT_INCLUDE:
1153 if (pass == 2)
1154 preproc->pre_include(q);
1155 break;
1156 case OPT_PRAGMA:
1157 if (pass == 2)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001158 preproc->pre_command("pragma", param);
H. Peter Anvin05990342018-06-11 13:32:42 -07001159 break;
1160 case OPT_BEFORE:
1161 if (pass == 2)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001162 preproc->pre_command(NULL, param);
H. Peter Anvin05990342018-06-11 13:32:42 -07001163 break;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001164 case OPT_LIMIT:
1165 if (pass == 2)
1166 nasm_set_limit(p+olen, param);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001167 break;
H. Peter Anvin29695c82018-06-14 17:04:32 -07001168 case OPT_KEEP_ALL:
1169 keep_all = true;
1170 break;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001171 case OPT_HELP:
1172 help(0);
1173 exit(0);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001174 default:
1175 panic();
H. Peter Anvine2c80182005-01-15 22:15:51 +00001176 }
H. Peter Anvinc805fd72018-06-12 14:23:05 -07001177
1178 if (eqsave)
1179 *eqsave = '='; /* Restore = argument separator */
1180
H. Peter Anvine2c80182005-01-15 22:15:51 +00001181 break;
1182 }
1183
1184 default:
H. Peter Anvinac061332017-03-31 11:41:16 -07001185 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
1186 "unrecognised option `-%c'", p[1]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001187 break;
1188 }
H. Peter Anvin55568c12016-10-03 19:46:49 -07001189 } else if (pass == 2) {
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001190 /* In theory we could allow multiple input files... */
1191 copy_filename(&inname, p, "input");
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001192 }
1193
1194 return advance;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001195}
1196
H. Peter Anvineba20a72002-04-30 20:53:55 +00001197#define ARG_BUF_DELTA 128
1198
H. Peter Anvin55568c12016-10-03 19:46:49 -07001199static void process_respfile(FILE * rfile, int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001200{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001201 char *buffer, *p, *q, *prevarg;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001202 int bufsize, prevargsize;
1203
1204 bufsize = prevargsize = ARG_BUF_DELTA;
1205 buffer = nasm_malloc(ARG_BUF_DELTA);
1206 prevarg = nasm_malloc(ARG_BUF_DELTA);
1207 prevarg[0] = '\0';
1208
H. Peter Anvine2c80182005-01-15 22:15:51 +00001209 while (1) { /* Loop to handle all lines in file */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001210 p = buffer;
1211 while (1) { /* Loop to handle long lines */
1212 q = fgets(p, bufsize - (p - buffer), rfile);
1213 if (!q)
1214 break;
1215 p += strlen(p);
1216 if (p > buffer && p[-1] == '\n')
1217 break;
1218 if (p - buffer > bufsize - 10) {
1219 int offset;
1220 offset = p - buffer;
1221 bufsize += ARG_BUF_DELTA;
1222 buffer = nasm_realloc(buffer, bufsize);
1223 p = buffer + offset;
1224 }
1225 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001226
H. Peter Anvine2c80182005-01-15 22:15:51 +00001227 if (!q && p == buffer) {
1228 if (prevarg[0])
H. Peter Anvin55568c12016-10-03 19:46:49 -07001229 process_arg(prevarg, NULL, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001230 nasm_free(buffer);
1231 nasm_free(prevarg);
1232 return;
1233 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001234
H. Peter Anvine2c80182005-01-15 22:15:51 +00001235 /*
1236 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
1237 * them are present at the end of the line.
1238 */
1239 *(p = &buffer[strcspn(buffer, "\r\n\032")]) = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001240
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001241 while (p > buffer && nasm_isspace(p[-1]))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001242 *--p = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001243
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +04001244 p = nasm_skip_spaces(buffer);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001245
H. Peter Anvin55568c12016-10-03 19:46:49 -07001246 if (process_arg(prevarg, p, pass))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001247 *p = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001248
Charles Crayne192d5b52007-10-18 19:02:42 -07001249 if ((int) strlen(p) > prevargsize - 10) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001250 prevargsize += ARG_BUF_DELTA;
1251 prevarg = nasm_realloc(prevarg, prevargsize);
1252 }
H. Peter Anvindc242712007-11-18 11:55:10 -08001253 strncpy(prevarg, p, prevargsize);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001254 }
1255}
1256
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001257/* Function to process args from a string of args, rather than the
1258 * argv array. Used by the environment variable and response file
1259 * processing.
1260 */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001261static void process_args(char *args, int pass)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001262{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001263 char *p, *q, *arg, *prevarg;
1264 char separator = ' ';
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001265
1266 p = args;
1267 if (*p && *p != '-')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001268 separator = *p++;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001269 arg = NULL;
1270 while (*p) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001271 q = p;
1272 while (*p && *p != separator)
1273 p++;
1274 while (*p == separator)
1275 *p++ = '\0';
1276 prevarg = arg;
1277 arg = q;
H. Peter Anvin55568c12016-10-03 19:46:49 -07001278 if (process_arg(prevarg, arg, pass))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001279 arg = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001280 }
1281 if (arg)
H. Peter Anvin55568c12016-10-03 19:46:49 -07001282 process_arg(arg, NULL, pass);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001283}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001284
H. Peter Anvin55568c12016-10-03 19:46:49 -07001285static void process_response_file(const char *file, int pass)
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001286{
1287 char str[2048];
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001288 FILE *f = nasm_open_read(file, NF_TEXT);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001289 if (!f) {
Cyrill Gorcunovf1964512013-02-15 12:14:06 +04001290 perror(file);
1291 exit(-1);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001292 }
1293 while (fgets(str, sizeof str, f)) {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001294 process_args(str, pass);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001295 }
1296 fclose(f);
1297}
1298
H. Peter Anvin55568c12016-10-03 19:46:49 -07001299static void parse_cmdline(int argc, char **argv, int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001300{
1301 FILE *rfile;
Cyrill Gorcunovf4941892011-07-17 13:55:25 +04001302 char *envreal, *envcopy = NULL, *p;
H. Peter Anvin972079f2008-09-30 17:01:23 -07001303 int i;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001304
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001305 /* Initialize all the warnings to their default state */
1306 for (i = 0; i < ERR_WARN_ALL; i++) {
1307 warning_state_init[i] = warning_state[i] =
1308 warnings[i].enabled ? WARN_ST_ENABLED : 0;
1309 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001310
1311 /*
H. Peter Anvinff7ccc02002-05-06 19:41:57 +00001312 * First, process the NASMENV environment variable.
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001313 */
H. Peter Anvinff7ccc02002-05-06 19:41:57 +00001314 envreal = getenv("NASMENV");
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001315 if (envreal) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001316 envcopy = nasm_strdup(envreal);
H. Peter Anvin55568c12016-10-03 19:46:49 -07001317 process_args(envcopy, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001318 nasm_free(envcopy);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001319 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001320
1321 /*
1322 * Now process the actual command line.
1323 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001324 while (--argc) {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001325 bool advance;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001326 argv++;
1327 if (argv[0][0] == '@') {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001328 /*
1329 * We have a response file, so process this as a set of
H. Peter Anvine2c80182005-01-15 22:15:51 +00001330 * arguments like the environment variable. This allows us
1331 * to have multiple arguments on a single line, which is
1332 * different to the -@resp file processing below for regular
1333 * NASM.
1334 */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001335 process_response_file(argv[0]+1, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001336 argc--;
1337 argv++;
1338 }
1339 if (!stopoptions && argv[0][0] == '-' && argv[0][1] == '@') {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001340 p = get_param(argv[0], argc > 1 ? argv[1] : NULL, &advance);
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001341 if (p) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001342 rfile = nasm_open_read(p, NF_TEXT);
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001343 if (rfile) {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001344 process_respfile(rfile, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001345 fclose(rfile);
1346 } else
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001347 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001348 "unable to open response file `%s'", p);
1349 }
1350 } else
H. Peter Anvin55568c12016-10-03 19:46:49 -07001351 advance = process_arg(argv[0], argc > 1 ? argv[1] : NULL, pass);
H. Peter Anvin423e3812007-11-15 17:12:29 -08001352 argv += advance, argc -= advance;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001353 }
1354
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001355 /*
1356 * Look for basic command line typos. This definitely doesn't
1357 * catch all errors, but it might help cases of fumbled fingers.
1358 */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001359 if (pass != 2)
1360 return;
1361
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001362 if (!inname)
H. Peter Anvinc5136902018-06-15 18:20:17 -07001363 nasm_fatal_fl(ERR_NOFILE | ERR_USAGE, "no input file specified");
H. Peter Anvin59ddd262007-10-01 11:26:31 -07001364
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001365 else if ((errname && !strcmp(inname, errname)) ||
1366 (outname && !strcmp(inname, outname)) ||
1367 (listname && !strcmp(inname, listname)) ||
1368 (depend_file && !strcmp(inname, depend_file)))
H. Peter Anvinc5136902018-06-15 18:20:17 -07001369 nasm_fatal_fl(ERR_USAGE, "will not overwrite input file");
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001370
1371 if (errname) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001372 error_file = nasm_open_write(errname, NF_TEXT);
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001373 if (!error_file) {
1374 error_file = stderr; /* Revert to default! */
H. Peter Anvinc5136902018-06-15 18:20:17 -07001375 nasm_fatal_fl(ERR_NOFILE | ERR_USAGE,
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001376 "cannot open file `%s' for error messages",
1377 errname);
1378 }
Charles Craynefcce07f2007-09-30 22:15:36 -07001379 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001380}
1381
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001382static void assemble_file(const char *fname, StrList **depend_ptr)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001383{
H. Peter Anvinc7131682017-03-07 17:45:01 -08001384 char *line;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001385 insn output_ins;
H. Peter Anvinc7131682017-03-07 17:45:01 -08001386 int i;
H. Peter Anvin9c595b62017-03-07 19:44:21 -08001387 uint64_t prev_offset_changed;
H. Peter Anvina3d96d02018-06-15 17:51:39 -07001388 int64_t stall_count = 0; /* Make sure we make forward progress... */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001389
H. Peter Anvina7ecf262018-02-06 14:43:07 -08001390 switch (cmd_sb) {
1391 case 16:
1392 break;
1393 case 32:
1394 if (!iflag_cpu_level_ok(&cmd_cpu, IF_386))
H. Peter Anvinc5136902018-06-15 18:20:17 -07001395 nasm_fatal("command line: 32-bit segment size requires a higher cpu");
H. Peter Anvina7ecf262018-02-06 14:43:07 -08001396 break;
1397 case 64:
1398 if (!iflag_cpu_level_ok(&cmd_cpu, IF_X86_64))
H. Peter Anvinc5136902018-06-15 18:20:17 -07001399 nasm_fatal("command line: 64-bit segment size requires a higher cpu");
H. Peter Anvina7ecf262018-02-06 14:43:07 -08001400 break;
1401 default:
1402 panic();
1403 break;
1404 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001405
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001406 prev_offset_changed = nasm_limit[LIMIT_PASSES];
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001407 for (passn = 1; pass0 <= 2; passn++) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001408 pass1 = pass0 == 2 ? 2 : 1; /* 1, 1, 1, ..., 1, 2 */
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001409 pass2 = passn > 1 ? 2 : 1; /* 1, 2, 2, ..., 2, 2 */
1410 /* pass0 0, 0, 0, ..., 1, 2 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00001411
H. Peter Anvincac0b192017-03-28 16:12:30 -07001412 globalbits = cmd_sb; /* set 'bits' to command line default */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001413 cpu = cmd_cpu;
1414 if (pass0 == 2) {
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08001415 lfmt->init(listname);
H. Peter Anvin29695c82018-06-14 17:04:32 -07001416 } else if (passn == 1 && listname && !keep_all) {
H. Peter Anvinaac01ff2017-04-02 19:10:26 -07001417 /* Remove the list file in case we die before the output pass */
1418 remove(listname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001419 }
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001420 in_absolute = false;
Charles Craynec1905c22008-09-11 18:54:06 -07001421 global_offset_changed = 0; /* set by redefine_label */
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001422 if (passn > 1) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001423 saa_rewind(forwrefs);
1424 forwref = saa_rstruct(forwrefs);
1425 raa_free(offsets);
1426 offsets = raa_init();
1427 }
H. Peter Anvin892c4812018-05-30 14:43:46 -07001428 location.segment = NO_SEG;
1429 location.offset = 0;
1430 if (passn == 1)
1431 location.known = true;
1432 ofmt->reset();
1433 switch_segment(ofmt->section(NULL, pass2, &globalbits));
H. Peter Anvin130736c2016-02-17 20:27:41 -08001434 preproc->reset(fname, pass1, pass1 == 2 ? depend_ptr : NULL);
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001435
1436 /* Revert all warnings to the default state */
1437 memcpy(warning_state, warning_state_init, sizeof warning_state);
Victor van den Elzen819703a2008-07-16 15:20:56 +02001438
H. Peter Anvine2c80182005-01-15 22:15:51 +00001439 globallineno = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001440
H. Peter Anvine2c80182005-01-15 22:15:51 +00001441 while ((line = preproc->getline())) {
H. Peter Anvina3d96d02018-06-15 17:51:39 -07001442 if (++globallineno > nasm_limit[LIMIT_LINES])
H. Peter Anvinc5136902018-06-15 18:20:17 -07001443 nasm_fatal("overall line count exceeds the maximum %"PRId64"\n",
Cyrill Gorcunovf7b44f62018-10-15 22:58:13 +03001444 nasm_limit[LIMIT_LINES]);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001445
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001446 /*
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001447 * Here we parse our directives; this is not handled by the
H. Peter Anvinc7131682017-03-07 17:45:01 -08001448 * main parser.
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001449 */
H. Peter Anvina6e26d92017-03-07 21:32:37 -08001450 if (process_directives(line))
H. Peter Anvinc7131682017-03-07 17:45:01 -08001451 goto end_of_line; /* Just do final cleanup */
H. Peter Anvin62b24d72007-08-29 16:38:05 +00001452
H. Peter Anvinc7131682017-03-07 17:45:01 -08001453 /* Not a directive, or even something that starts with [ */
H. Peter Anvin98578072018-06-01 18:02:54 -07001454 parse_line(pass1, line, &output_ins);
H. Peter Anvinc7131682017-03-07 17:45:01 -08001455
Chang S. Baea5786342018-08-15 23:22:21 +03001456 if (optimizing.level > 0) {
H. Peter Anvinc7131682017-03-07 17:45:01 -08001457 if (forwref != NULL && globallineno == forwref->lineno) {
1458 output_ins.forw_ref = true;
1459 do {
1460 output_ins.oprs[forwref->operand].opflags |= OPFLAG_FORWARD;
1461 forwref = saa_rstruct(forwrefs);
1462 } while (forwref != NULL
1463 && forwref->lineno == globallineno);
1464 } else
1465 output_ins.forw_ref = false;
1466
1467 if (output_ins.forw_ref) {
1468 if (passn == 1) {
1469 for (i = 0; i < output_ins.operands; i++) {
1470 if (output_ins.oprs[i].opflags & OPFLAG_FORWARD) {
1471 struct forwrefinfo *fwinf = (struct forwrefinfo *)saa_wstruct(forwrefs);
1472 fwinf->lineno = globallineno;
1473 fwinf->operand = i;
Cyrill Gorcunovd5f2aef2010-04-20 15:33:45 +04001474 }
1475 }
1476 }
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001477 }
H. Peter Anvinc7131682017-03-07 17:45:01 -08001478 }
H. Peter Anvin8bec2c72009-08-08 13:49:00 -07001479
H. Peter Anvinc7131682017-03-07 17:45:01 -08001480 /* forw_ref */
1481 if (output_ins.opcode == I_EQU) {
Cyrill Gorcunove996d282018-10-13 16:18:16 +03001482 if (!output_ins.label) {
1483 nasm_error(ERR_NONFATAL, "EQU not preceded by label");
1484 } else if (output_ins.operands == 1 &&
1485 (output_ins.oprs[0].type & IMMEDIATE) &&
1486 output_ins.oprs[0].wrt == NO_SEG) {
H. Peter Anvin98578072018-06-01 18:02:54 -07001487 define_label(output_ins.label,
1488 output_ins.oprs[0].segment,
1489 output_ins.oprs[0].offset, false);
1490 } else if (output_ins.operands == 2
1491 && (output_ins.oprs[0].type & IMMEDIATE)
1492 && (output_ins.oprs[0].type & COLON)
1493 && output_ins.oprs[0].segment == NO_SEG
1494 && output_ins.oprs[0].wrt == NO_SEG
1495 && (output_ins.oprs[1].type & IMMEDIATE)
1496 && output_ins.oprs[1].segment == NO_SEG
1497 && output_ins.oprs[1].wrt == NO_SEG) {
1498 define_label(output_ins.label,
1499 output_ins.oprs[0].offset | SEG_ABS,
1500 output_ins.oprs[1].offset, false);
H. Peter Anvinc7131682017-03-07 17:45:01 -08001501 } else {
H. Peter Anvin98578072018-06-01 18:02:54 -07001502 nasm_error(ERR_NONFATAL, "bad syntax for EQU");
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001503 }
H. Peter Anvinc7131682017-03-07 17:45:01 -08001504 } else { /* instruction isn't an EQU */
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001505 int32_t n;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001506
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001507 nasm_assert(output_ins.times >= 0);
H. Peter Anvinc7131682017-03-07 17:45:01 -08001508
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001509 for (n = 1; n <= output_ins.times; n++) {
1510 if (pass1 == 1) {
H. Peter Anvin892c4812018-05-30 14:43:46 -07001511 int64_t l = insn_size(location.segment,
1512 location.offset,
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001513 globalbits, &output_ins);
1514
1515 /* if (using_debug_info) && output_ins.opcode != -1) */
1516 if (using_debug_info)
1517 { /* fbk 03/25/01 */
H. Peter Anvinc7131682017-03-07 17:45:01 -08001518 /* this is done here so we can do debug type info */
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001519 int32_t typeinfo =
1520 TYS_ELEMENTS(output_ins.operands);
1521 switch (output_ins.opcode) {
1522 case I_RESB:
1523 typeinfo =
1524 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_BYTE;
1525 break;
1526 case I_RESW:
1527 typeinfo =
1528 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_WORD;
1529 break;
1530 case I_RESD:
1531 typeinfo =
1532 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_DWORD;
1533 break;
1534 case I_RESQ:
1535 typeinfo =
1536 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_QWORD;
1537 break;
1538 case I_REST:
1539 typeinfo =
1540 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_TBYTE;
1541 break;
1542 case I_RESO:
1543 typeinfo =
1544 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_OWORD;
1545 break;
1546 case I_RESY:
1547 typeinfo =
1548 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_YWORD;
1549 break;
1550 case I_RESZ:
1551 typeinfo =
1552 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_ZWORD;
1553 break;
1554 case I_DB:
1555 typeinfo |= TY_BYTE;
1556 break;
1557 case I_DW:
1558 typeinfo |= TY_WORD;
1559 break;
1560 case I_DD:
1561 if (output_ins.eops_float)
1562 typeinfo |= TY_FLOAT;
1563 else
1564 typeinfo |= TY_DWORD;
1565 break;
1566 case I_DQ:
1567 typeinfo |= TY_QWORD;
1568 break;
1569 case I_DT:
1570 typeinfo |= TY_TBYTE;
1571 break;
1572 case I_DO:
1573 typeinfo |= TY_OWORD;
1574 break;
1575 case I_DY:
1576 typeinfo |= TY_YWORD;
1577 break;
1578 case I_DZ:
1579 typeinfo |= TY_ZWORD;
1580 break;
1581 default:
1582 typeinfo = TY_LABEL;
1583 break;
1584 }
H. Peter Anvinc7131682017-03-07 17:45:01 -08001585
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001586 dfmt->debug_typevalue(typeinfo);
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001587 }
H. Peter Anvinc7131682017-03-07 17:45:01 -08001588
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001589 /*
1590 * For INCBIN, let the code in assemble
1591 * handle TIMES, so we don't have to read the
1592 * input file over and over.
1593 */
1594 if (l != -1) {
H. Peter Anvin892c4812018-05-30 14:43:46 -07001595 increment_offset(l);
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001596 }
1597 /*
1598 * else l == -1 => invalid instruction, which will be
1599 * flagged as an error on pass 2
1600 */
1601 } else {
1602 if (n == 2)
1603 lfmt->uplevel(LIST_TIMES);
H. Peter Anvin892c4812018-05-30 14:43:46 -07001604 increment_offset(assemble(location.segment,
1605 location.offset,
1606 globalbits, &output_ins));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001607 }
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001608 } /* not an EQU */
1609 }
1610 if (output_ins.times > 1)
1611 lfmt->downlevel(LIST_TIMES);
H. Peter Anvinc7131682017-03-07 17:45:01 -08001612
H. Peter Anvinc7131682017-03-07 17:45:01 -08001613 cleanup_insn(&output_ins);
1614
1615 end_of_line:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001616 nasm_free(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001617 } /* end while (line = preproc->getline... */
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001618
H. Peter Anvin (Intel)b45c03a2018-06-27 21:03:38 -07001619 if (global_offset_changed && !terminate_after_phase) {
1620 switch (pass0) {
1621 case 1:
1622 nasm_error(ERR_WARNING|ERR_WARN_PHASE,
1623 "phase error during stabilization pass, hoping for the best");
1624 break;
1625
1626 case 2:
1627 nasm_error(ERR_NONFATAL,
1628 "phase error during code generation pass");
1629 break;
1630
1631 default:
1632 /* This is normal, we'll keep going... */
1633 break;
1634 }
1635 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001636
H. Peter Anvine2c80182005-01-15 22:15:51 +00001637 if (pass1 == 1)
1638 preproc->cleanup(1);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001639
H. Peter Anvin (Intel)12810fa2018-06-27 20:17:33 -07001640 /*
1641 * Always run at least two optimization passes (pass0 == 0);
1642 * things like subsections will fail miserably without that.
1643 * Once we commit to a stabilization pass (pass0 == 1), we can't
1644 * go back, and if something goes bad, we can only hope
1645 * that we don't end up with a phase error at the end.
1646 */
1647 if ((passn > 1 && !global_offset_changed) || pass0 > 0) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001648 pass0++;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001649 } else if (global_offset_changed &&
1650 global_offset_changed < prev_offset_changed) {
Charles Craynec1905c22008-09-11 18:54:06 -07001651 prev_offset_changed = global_offset_changed;
1652 stall_count = 0;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001653 } else {
1654 stall_count++;
1655 }
Victor van den Elzen42528232008-09-11 15:07:05 +02001656
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001657 if (terminate_after_phase)
1658 break;
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001659
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001660 if ((stall_count > nasm_limit[LIMIT_STALLED]) ||
1661 (passn >= nasm_limit[LIMIT_PASSES])) {
Victor van den Elzen42528232008-09-11 15:07:05 +02001662 /* We get here if the labels don't converge
1663 * Example: FOO equ FOO + 1
1664 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001665 nasm_error(ERR_NONFATAL,
Victor van den Elzen42528232008-09-11 15:07:05 +02001666 "Can't find valid values for all labels "
H. Peter Anvina3d96d02018-06-15 17:51:39 -07001667 "after %"PRId64" passes, giving up.", passn);
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001668 nasm_error(ERR_NONFATAL,
1669 "Possible causes: recursive EQUs, macro abuse.");
1670 break;
1671 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001672 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001673
H. Peter Anvine2c80182005-01-15 22:15:51 +00001674 preproc->cleanup(0);
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08001675 lfmt->cleanup();
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001676 if (!terminate_after_phase && opt_verbose_info) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001677 /* -On and -Ov switches */
H. Peter Anvina3d96d02018-06-15 17:51:39 -07001678 fprintf(stdout, "info: assembly required 1+%"PRId64"+1 passes\n",
1679 passn-3);
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001680 }
1681}
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001682
Ed Berosetfa771012002-06-09 20:56:40 +00001683/**
1684 * gnu style error reporting
1685 * This function prints an error message to error_file in the
1686 * style used by GNU. An example would be:
1687 * file.asm:50: error: blah blah blah
H. Peter Anvin70653092007-10-19 14:42:29 -07001688 * where file.asm is the name of the file, 50 is the line number on
Ed Berosetfa771012002-06-09 20:56:40 +00001689 * which the error occurs (or is detected) and "error:" is one of
1690 * the possible optional diagnostics -- it can be "error" or "warning"
H. Peter Anvin70653092007-10-19 14:42:29 -07001691 * or something else. Finally the line terminates with the actual
Ed Berosetfa771012002-06-09 20:56:40 +00001692 * error message.
H. Peter Anvin70653092007-10-19 14:42:29 -07001693 *
1694 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001695 * @param fmt the printf style format string
1696 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001697static void nasm_verror_gnu(int severity, const char *fmt, va_list ap)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001698{
H. Peter Anvin274cda82016-05-10 02:56:29 -07001699 const char *currentfile = NULL;
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001700 int32_t lineno = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001701
Ed Berosetfa771012002-06-09 20:56:40 +00001702 if (is_suppressed_warning(severity))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001703 return;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001704
H. Peter Anvin7a6bf742017-12-20 11:54:31 -08001705 if (!(severity & ERR_NOFILE)) {
H. Peter Anvin4def1a82016-05-09 13:59:44 -07001706 src_get(&lineno, &currentfile);
H. Peter Anvin7a6bf742017-12-20 11:54:31 -08001707 if (!currentfile || (severity & ERR_TOPFILE)) {
1708 currentfile = inname[0] ? inname : outname[0] ? outname : NULL;
1709 lineno = 0;
1710 }
1711 }
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001712
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001713 if (!skip_this_pass(severity)) {
H. Peter Anvin7a6bf742017-12-20 11:54:31 -08001714 if (!lineno)
1715 fprintf(error_file, "%s:", currentfile ? currentfile : "nasm");
H. Peter Anvin883985d2017-12-20 11:32:39 -08001716 else
1717 fprintf(error_file, "%s:%"PRId32": ", currentfile, lineno);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001718 }
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001719
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001720 nasm_verror_common(severity, fmt, ap);
Ed Berosetfa771012002-06-09 20:56:40 +00001721}
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001722
Ed Berosetfa771012002-06-09 20:56:40 +00001723/**
1724 * MS style error reporting
1725 * This function prints an error message to error_file in the
H. Peter Anvin70653092007-10-19 14:42:29 -07001726 * style used by Visual C and some other Microsoft tools. An example
Ed Berosetfa771012002-06-09 20:56:40 +00001727 * would be:
Ed Beroset6e61d0d2002-06-11 03:29:36 +00001728 * file.asm(50) : error: blah blah blah
H. Peter Anvin70653092007-10-19 14:42:29 -07001729 * where file.asm is the name of the file, 50 is the line number on
Ed Beroset6e61d0d2002-06-11 03:29:36 +00001730 * which the error occurs (or is detected) and "error:" is one of
1731 * the possible optional diagnostics -- it can be "error" or "warning"
H. Peter Anvin70653092007-10-19 14:42:29 -07001732 * or something else. Finally the line terminates with the actual
Ed Beroset6e61d0d2002-06-11 03:29:36 +00001733 * error message.
H. Peter Anvin70653092007-10-19 14:42:29 -07001734 *
1735 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001736 * @param fmt the printf style format string
1737 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001738static void nasm_verror_vc(int severity, const char *fmt, va_list ap)
Ed Berosetfa771012002-06-09 20:56:40 +00001739{
H. Peter Anvin274cda82016-05-10 02:56:29 -07001740 const char *currentfile = NULL;
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001741 int32_t lineno = 0;
Ed Berosetfa771012002-06-09 20:56:40 +00001742
H. Peter Anvine2c80182005-01-15 22:15:51 +00001743 if (is_suppressed_warning(severity))
1744 return;
Ed Berosetfa771012002-06-09 20:56:40 +00001745
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001746 if (!(severity & ERR_NOFILE))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001747 src_get(&lineno, &currentfile);
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001748
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001749 if (!skip_this_pass(severity)) {
1750 if (currentfile) {
1751 fprintf(error_file, "%s(%"PRId32") : ", currentfile, lineno);
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001752 } else {
1753 fputs("nasm: ", error_file);
1754 }
Ed Berosetfa771012002-06-09 20:56:40 +00001755 }
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001756
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001757 nasm_verror_common(severity, fmt, ap);
Ed Berosetfa771012002-06-09 20:56:40 +00001758}
1759
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001760/*
1761 * check to see if this is a suppressable warning
1762 */
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001763static inline bool is_valid_warning(int severity)
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001764{
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001765 /* Not a warning at all */
1766 if ((severity & ERR_MASK) != ERR_WARNING)
1767 return false;
1768
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001769 return WARN_IDX(severity) < ERR_WARN_ALL;
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001770}
1771
Ed Berosetfa771012002-06-09 20:56:40 +00001772/**
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001773 * check for suppressed warning
H. Peter Anvin70653092007-10-19 14:42:29 -07001774 * checks for suppressed warning or pass one only warning and we're
Ed Berosetfa771012002-06-09 20:56:40 +00001775 * not in pass 1
1776 *
1777 * @param severity the severity of the warning or error
1778 * @return true if we should abort error/warning printing
1779 */
H. Peter Anvin2b046cf2008-01-22 14:08:36 -08001780static bool is_suppressed_warning(int severity)
Ed Berosetfa771012002-06-09 20:56:40 +00001781{
H. Peter Anvin442a05a2012-02-24 21:50:53 -08001782 /* Might be a warning but suppresed explicitly */
H. Peter Anvinc805fd72018-06-12 14:23:05 -07001783 if (is_valid_warning(severity) && !(severity & ERR_USAGE))
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001784 return !(warning_state[WARN_IDX(severity)] & WARN_ST_ENABLED);
1785 else
1786 return false;
1787}
1788
1789static bool warning_is_error(int severity)
1790{
1791 if (is_valid_warning(severity))
1792 return !!(warning_state[WARN_IDX(severity)] & WARN_ST_ERROR);
H. Peter Anvin442a05a2012-02-24 21:50:53 -08001793 else
1794 return false;
Ed Berosetfa771012002-06-09 20:56:40 +00001795}
1796
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001797static bool skip_this_pass(int severity)
1798{
H. Peter Anvin8f622462017-04-02 19:02:29 -07001799 /*
1800 * See if it's a pass-specific error or warning which should be skipped.
1801 * We cannot skip errors stronger than ERR_NONFATAL as by definition
1802 * they cannot be resumed from.
1803 */
1804 if ((severity & ERR_MASK) > ERR_NONFATAL)
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001805 return false;
1806
H. Peter Anvin934f0472016-05-09 12:00:19 -07001807 /*
1808 * passn is 1 on the very first pass only.
1809 * pass0 is 2 on the code-generation (final) pass only.
1810 * These are the passes we care about in this case.
1811 */
1812 return (((severity & ERR_PASS1) && passn != 1) ||
1813 ((severity & ERR_PASS2) && pass0 != 2));
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001814}
1815
Ed Berosetfa771012002-06-09 20:56:40 +00001816/**
1817 * common error reporting
1818 * This is the common back end of the error reporting schemes currently
H. Peter Anvin70653092007-10-19 14:42:29 -07001819 * implemented. It prints the nature of the warning and then the
Ed Berosetfa771012002-06-09 20:56:40 +00001820 * specific error message to error_file and may or may not return. It
1821 * doesn't return if the error severity is a "panic" or "debug" type.
H. Peter Anvin70653092007-10-19 14:42:29 -07001822 *
1823 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001824 * @param fmt the printf style format string
1825 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001826static void nasm_verror_common(int severity, const char *fmt, va_list args)
Ed Berosetfa771012002-06-09 20:56:40 +00001827{
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001828 char msg[1024];
1829 const char *pfx;
H. Peter Anvin323fcff2009-07-12 12:04:56 -07001830
H. Peter Anvin7df04172008-06-10 18:27:38 -07001831 switch (severity & (ERR_MASK|ERR_NO_SEVERITY)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001832 case ERR_WARNING:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001833 pfx = "warning: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001834 break;
1835 case ERR_NONFATAL:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001836 pfx = "error: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001837 break;
1838 case ERR_FATAL:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001839 pfx = "fatal: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001840 break;
1841 case ERR_PANIC:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001842 pfx = "panic: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001843 break;
1844 case ERR_DEBUG:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001845 pfx = "debug: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001846 break;
H. Peter Anvin7df04172008-06-10 18:27:38 -07001847 default:
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001848 pfx = "";
1849 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001850 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00001851
H. Peter Anvin934f0472016-05-09 12:00:19 -07001852 vsnprintf(msg, sizeof msg - 64, fmt, args);
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001853 if (is_valid_warning(severity) && WARN_IDX(severity) != ERR_WARN_OTHER) {
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001854 char *p = strchr(msg, '\0');
H. Peter Anvin934f0472016-05-09 12:00:19 -07001855 snprintf(p, 64, " [-w+%s]", warnings[WARN_IDX(severity)].name);
1856 }
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001857
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001858 if (!skip_this_pass(severity))
1859 fprintf(error_file, "%s%s\n", pfx, msg);
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001860
H. Peter Anvin4def1a82016-05-09 13:59:44 -07001861 /* Are we recursing from error_list_macros? */
1862 if (severity & ERR_PP_LISTMACRO)
1863 return;
1864
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001865 /*
1866 * Don't suppress this with skip_this_pass(), or we don't get
H. Peter Anvin934f0472016-05-09 12:00:19 -07001867 * pass1 or preprocessor warnings in the list file
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001868 */
H. Peter Anvin0fcb4882016-07-06 11:55:25 -07001869 lfmt->error(severity, pfx, msg);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001870
H. Peter Anvin8f622462017-04-02 19:02:29 -07001871 if (skip_this_pass(severity))
1872 return;
1873
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001874 if (severity & ERR_USAGE)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001875 want_usage = true;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001876
H. Peter Anvin4def1a82016-05-09 13:59:44 -07001877 preproc->error_list_macros(severity);
1878
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001879 switch (severity & ERR_MASK) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001880 case ERR_DEBUG:
1881 /* no further action, by definition */
1882 break;
H. Peter Anvinb030c922007-11-13 11:31:15 -08001883 case ERR_WARNING:
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001884 /* Treat warnings as errors */
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001885 if (warning_is_error(severity))
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001886 terminate_after_phase = true;
1887 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001888 case ERR_NONFATAL:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001889 terminate_after_phase = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001890 break;
1891 case ERR_FATAL:
1892 if (ofile) {
1893 fclose(ofile);
H. Peter Anvin29695c82018-06-14 17:04:32 -07001894 if (!keep_all)
1895 remove(outname);
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001896 ofile = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001897 }
1898 if (want_usage)
1899 usage();
1900 exit(1); /* instantly die */
1901 break; /* placate silly compilers */
1902 case ERR_PANIC:
1903 fflush(NULL);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001904
1905 if (abort_on_panic)
1906 abort(); /* halt, catch fire, dump core/stop debugger */
1907
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001908 if (ofile) {
1909 fclose(ofile);
H. Peter Anvin29695c82018-06-14 17:04:32 -07001910 if (!keep_all)
1911 remove(outname);
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001912 ofile = NULL;
1913 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001914 exit(3);
1915 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001916 }
1917}
1918
H. Peter Anvin734b1882002-04-30 21:01:08 +00001919static void usage(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001920{
H. Peter Anvin620515a2002-04-30 20:57:38 +00001921 fputs("type `nasm -h' for help\n", error_file);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001922}
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001923
1924static void help(const char xopt)
1925{
1926 int i;
1927
1928 printf
1929 ("usage: nasm [-@ response file] [-o outfile] [-f format] "
1930 "[-l listfile]\n"
1931 " [options...] [--] filename\n"
1932 " or nasm -v (or --v) for version info\n\n"
1933 "\n"
1934 "Response files should contain command line parameters,\n"
1935 "one per line.\n"
1936 "\n"
1937 " -t assemble in SciTech TASM compatible mode\n");
1938 printf
1939 (" -E (or -e) preprocess only (writes output to stdout by default)\n"
1940 " -a don't preprocess (assemble only)\n"
1941 " -M generate Makefile dependencies on stdout\n"
1942 " -MG d:o, missing files assumed generated\n"
1943 " -MF file set Makefile dependency file\n"
1944 " -MD file assemble and generate dependencies\n"
1945 " -MT file dependency target name\n"
1946 " -MQ file dependency target name (quoted)\n"
1947 " -MP emit phony target\n\n"
1948 " -Zfile redirect error messages to file\n"
1949 " -s redirect error messages to stdout\n\n"
1950 " -g generate debugging information\n\n"
1951 " -F format select a debugging format\n\n"
1952 " -gformat same as -g -F format\n\n"
1953 " -o outfile write output to an outfile\n\n"
1954 " -f format select an output format\n\n"
1955 " -l listfile write listing to a listfile\n\n"
1956 " -Ipath add a pathname to the include file path\n");
1957 printf
1958 (" -Olevel optimize opcodes, immediates and branch offsets\n"
1959 " -O0 no optimization\n"
1960 " -O1 minimal optimization\n"
1961 " -Ox multipass optimization (default)\n"
1962 " -Pfile pre-include a file (also --include)\n"
1963 " -Dmacro[=str] pre-define a macro\n"
1964 " -Umacro undefine a macro\n"
1965 " -Xformat specifiy error reporting format (gnu or vc)\n"
1966 " -w+foo enable warning foo (equiv. -Wfoo)\n"
1967 " -w-foo disable warning foo (equiv. -Wno-foo)\n"
1968 " -w[+-]error[=foo]\n"
1969 " promote [specific] warnings to errors\n"
Chang S. Bae1af6ef42018-06-20 17:05:12 -07001970 " -h show invocation summary and exit (also --help)\n\n"
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001971 " --pragma str pre-executes a specific %%pragma\n"
1972 " --before str add line (usually a preprocessor statement) before the input\n"
1973 " --prefix str prepend the given string to all the given string\n"
Chang S. Bae1af6ef42018-06-20 17:05:12 -07001974 " to all extern, common and global symbols (also --gprefix)\n"
1975 " --postfix str append the given string to all the given string\n"
1976 " to all extern, common and global symbols (also --gpostfix)\n"
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001977 " --lprefix str prepend the given string to all other symbols\n"
Chang S. Bae1af6ef42018-06-20 17:05:12 -07001978 " --lpostfix str append the given string to all other symbols\n"
1979 " --keep-all output files will not be removed even if an error happens\n"
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001980 " --limit-X val set execution limit X\n");
1981
1982 for (i = 0; i <= LIMIT_MAX; i++) {
1983 printf(" %-15s %s (default ",
1984 limit_info[i].name, limit_info[i].help);
1985 if (nasm_limit[i] < LIMIT_MAX_VAL) {
H. Peter Anvina3d96d02018-06-15 17:51:39 -07001986 printf("%"PRId64")\n", nasm_limit[i]);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001987 } else {
1988 printf("unlimited)\n");
1989 }
1990 }
1991
1992 printf("\nWarnings for the -W/-w options:\n");
1993
1994 for (i = 0; i <= ERR_WARN_ALL; i++)
1995 printf(" %-23s %s%s\n",
1996 warnings[i].name, warnings[i].help,
1997 i == ERR_WARN_ALL ? "\n" :
1998 warnings[i].enabled ? " (default on)" :
1999 " (default off)");
2000
2001 if (xopt == 'f') {
2002 printf("valid output formats for -f are"
2003 " (`*' denotes default):\n");
2004 ofmt_list(ofmt, stdout);
2005 } else {
2006 printf("For a list of valid output formats, use -hf.\n");
2007 printf("For a list of debug formats, use -f <format> -y.\n");
2008 }
2009}