blob: ad05e44fa7fe9e0473fd2cbc2b7ff20748c223c6 [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 Anvin (Intel)f7106d02018-10-25 12:33:58 -070078static 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;
H. Peter Anvinf05034f2017-08-16 22:08:36 -0700312 bool wmake = (quote_for_make == quote_for_wmake);
313 const char *wrapstr, *nulltarget;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -0700314 struct strlist_entry *l;
315
316 if (!list)
317 return;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700318
H. Peter Anvinf05034f2017-08-16 22:08:36 -0700319 wrapstr = wmake ? " &\n " : " \\\n ";
320 nulltarget = wmake ? "\t%null\n" : "";
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700321
322 if (depend_file && strcmp(depend_file, "-")) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -0700323 deps = nasm_open_write(depend_file, NF_TEXT);
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400324 if (!deps) {
325 nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
326 "unable to write dependency file `%s'", depend_file);
327 return;
328 }
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700329 } else {
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400330 deps = stdout;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700331 }
H. Peter Anvin323fcff2009-07-12 12:04:56 -0700332
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700333 linepos = fprintf(deps, "%s :", depend_target);
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -0700334 list_for_each(l, list->head) {
H. Peter Anvin55340992012-09-09 17:09:00 -0700335 char *file = quote_for_make(l->str);
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400336 len = strlen(file);
337 if (linepos + len > 62 && linepos > 1) {
H. Peter Anvinf05034f2017-08-16 22:08:36 -0700338 fputs(wrapstr, deps);
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400339 linepos = 1;
340 }
341 fprintf(deps, " %s", file);
342 linepos += len+1;
H. Peter Anvin55340992012-09-09 17:09:00 -0700343 nasm_free(file);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700344 }
345 fprintf(deps, "\n\n");
H. Peter Anvin323fcff2009-07-12 12:04:56 -0700346
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -0700347 list_for_each(l, list->head) {
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700348 if (depend_emit_phony) {
349 char *file = quote_for_make(l->str);
H. Peter Anvinf05034f2017-08-16 22:08:36 -0700350 fprintf(deps, "%s :\n%s\n", file, nulltarget);
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700351 nasm_free(file);
352 }
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700353 }
354
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -0700355 strlist_free(list);
356
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700357 if (deps != stdout)
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400358 fclose(deps);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700359}
360
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700361/* Convert a struct tm to a POSIX-style time constant */
362static int64_t make_posix_time(const struct tm *tm)
363{
364 int64_t t;
365 int64_t y = tm->tm_year;
366
367 /* See IEEE 1003.1:2004, section 4.14 */
368
369 t = (y-70)*365 + (y-69)/4 - (y-1)/100 + (y+299)/400;
370 t += tm->tm_yday;
371 t *= 24;
372 t += tm->tm_hour;
373 t *= 60;
374 t += tm->tm_min;
375 t *= 60;
376 t += tm->tm_sec;
377
378 return t;
379}
380
381static void timestamp(void)
382{
383 struct compile_time * const oct = &official_compile_time;
Dale Curtis4ee6a692018-11-19 13:52:05 -0800384#if 1
385 // Chromium patch: Builds should be deterministic and not embed timestamps.
386 memset(oct, 0, sizeof(official_compile_time));
387#else
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700388 const struct tm *tp, *best_gm;
389
390 time(&oct->t);
391
392 best_gm = NULL;
393
394 tp = localtime(&oct->t);
395 if (tp) {
396 oct->local = *tp;
397 best_gm = &oct->local;
398 oct->have_local = true;
399 }
400
401 tp = gmtime(&oct->t);
402 if (tp) {
403 oct->gm = *tp;
404 best_gm = &oct->gm;
405 oct->have_gm = true;
406 if (!oct->have_local)
407 oct->local = oct->gm;
408 } else {
409 oct->gm = oct->local;
410 }
411
412 if (best_gm) {
413 oct->posix = make_posix_time(best_gm);
414 oct->have_posix = true;
415 }
Dale Curtis4ee6a692018-11-19 13:52:05 -0800416#endif
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700417}
418
H. Peter Anvin038d8612007-04-12 16:54:50 +0000419int main(int argc, char **argv)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000420{
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700421 timestamp();
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800422
H. Peter Anvina7ecf262018-02-06 14:43:07 -0800423 iflag_set_default_cpu(&cpu);
424 iflag_set_default_cpu(&cmd_cpu);
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400425
Charles Crayne2581c862008-09-10 19:21:52 -0700426 pass0 = 0;
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700427 want_usage = terminate_after_phase = false;
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400428 nasm_set_verror(nasm_verror_gnu);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000429
H. Peter Anvin97e15752007-09-25 08:47:47 -0700430 error_file = stderr;
431
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -0700432 tolower_init();
H. Peter Anvin274cda82016-05-10 02:56:29 -0700433 src_init();
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -0700434
H. Peter Anvin, Intel87d9e622018-06-25 12:58:49 -0700435 /*
436 * We must call init_labels() before the command line parsing,
437 * because we may be setting prefixes/suffixes from the command
438 * line.
439 */
440 init_labels();
441
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000442 offsets = raa_init();
Keith Kaniosb7a89542007-04-12 02:40:54 +0000443 forwrefs = saa_init((int32_t)sizeof(struct forwrefinfo));
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000444
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000445 preproc = &nasmpp;
Cyrill Gorcunov3ed32cb2014-06-22 20:53:12 +0400446 operating_mode = OP_NORMAL;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000447
H. Peter Anvin55568c12016-10-03 19:46:49 -0700448 parse_cmdline(argc, argv, 1);
449 if (terminate_after_phase) {
450 if (want_usage)
451 usage();
452 return 1;
453 }
454
H. Peter Anvin169ac7c2016-09-25 17:08:05 -0700455 /*
456 * Define some macros dependent on the runtime, but not
H. Peter Anvin55568c12016-10-03 19:46:49 -0700457 * on the command line (as those are scanned in cmdline pass 2.)
H. Peter Anvin169ac7c2016-09-25 17:08:05 -0700458 */
459 preproc->init();
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800460 define_macros_early();
461
H. Peter Anvin55568c12016-10-03 19:46:49 -0700462 parse_cmdline(argc, argv, 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000463 if (terminate_after_phase) {
464 if (want_usage)
465 usage();
466 return 1;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000467 }
468
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800469 /* Save away the default state of warnings */
470 memcpy(warning_state_init, warning_state, sizeof warning_state);
471
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800472 if (!using_debug_info) {
473 /* No debug info, redirect to the null backend (empty stubs) */
H. Peter Anvina7bc15d2016-02-17 20:55:08 -0800474 dfmt = &null_debug_form;
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800475 } else if (!debug_format) {
476 /* Default debug format for this backend */
H. Peter Anvina7bc15d2016-02-17 20:55:08 -0800477 dfmt = ofmt->default_dfmt;
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800478 } else {
479 dfmt = dfmt_find(ofmt, debug_format);
480 if (!dfmt) {
H. Peter Anvinc5136902018-06-15 18:20:17 -0700481 nasm_fatal_fl(ERR_NOFILE | ERR_USAGE,
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800482 "unrecognized debug format `%s' for"
483 " output format `%s'",
484 debug_format, ofmt->shortname);
485 }
486 }
H. Peter Anvinbb88d012003-09-10 23:34:23 +0000487
H. Peter Anvin76690a12002-04-30 20:52:49 +0000488 if (ofmt->stdmac)
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +0400489 preproc->extra_stdmac(ofmt->stdmac);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000490
Cyrill Gorcunov69bb0522018-09-22 13:46:45 +0300491 /*
492 * If no output file name provided and this
Cyrill Gorcunovda3780d2018-09-22 14:10:36 +0300493 * is a preprocess mode, we're perfectly
494 * fine to output into stdout.
Cyrill Gorcunov69bb0522018-09-22 13:46:45 +0300495 */
496 if (!outname) {
497 if (!(operating_mode & OP_PREPROCESS))
498 outname = filename_set_extension(inname, ofmt->extension);
499 }
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800500
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000501 /* define some macros dependent of command-line */
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800502 define_macros_late();
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000503
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -0700504 if (depend_file || (operating_mode & OP_DEPEND))
505 depend_list = strlist_allocate();
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700506
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700507 if (!depend_target)
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400508 depend_target = quote_for_make(outname);
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700509
Cyrill Gorcunove9fc88c2014-06-23 02:22:02 +0400510 if (operating_mode & OP_DEPEND) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000511 char *line;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700512
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400513 if (depend_missing_ok)
514 preproc->include_path(NULL); /* "assume generated" */
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700515
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -0700516 preproc->reset(inname, 0, depend_list);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000517 ofile = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000518 while ((line = preproc->getline()))
519 nasm_free(line);
520 preproc->cleanup(0);
Cyrill Gorcunove9fc88c2014-06-23 02:22:02 +0400521 } else if (operating_mode & OP_PREPROCESS) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000522 char *line;
H. Peter Anvin274cda82016-05-10 02:56:29 -0700523 const char *file_name = NULL;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000524 int32_t prior_linnum = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000525 int lineinc = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000526
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800527 if (outname) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -0700528 ofile = nasm_open_write(outname, NF_TEXT);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000529 if (!ofile)
H. Peter Anvinc5136902018-06-15 18:20:17 -0700530 nasm_fatal_fl(ERR_NOFILE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000531 "unable to open output file `%s'",
532 outname);
533 } else
534 ofile = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000535
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700536 location.known = false;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000537
Cyrill Gorcunovb574b072011-12-05 01:56:40 +0400538 /* pass = 1; */
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -0700539 preproc->reset(inname, 3, depend_list);
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800540
541 /* Revert all warnings to the default state */
542 memcpy(warning_state, warning_state_init, sizeof warning_state);
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700543
H. Peter Anvine2c80182005-01-15 22:15:51 +0000544 while ((line = preproc->getline())) {
545 /*
546 * We generate %line directives if needed for later programs
547 */
Keith Kaniosb7a89542007-04-12 02:40:54 +0000548 int32_t linnum = prior_linnum += lineinc;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000549 int altline = src_get(&linnum, &file_name);
550 if (altline) {
551 if (altline == 1 && lineinc == 1)
552 nasm_fputs("", ofile);
553 else {
554 lineinc = (altline != -1 || lineinc != 1);
555 fprintf(ofile ? ofile : stdout,
Keith Kanios93f2e9a2007-04-14 00:10:59 +0000556 "%%line %"PRId32"+%d %s\n", linnum, lineinc,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000557 file_name);
558 }
559 prior_linnum = linnum;
560 }
561 nasm_fputs(line, ofile);
562 nasm_free(line);
563 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000564 preproc->cleanup(0);
565 if (ofile)
566 fclose(ofile);
H. Peter Anvin29695c82018-06-14 17:04:32 -0700567 if (ofile && terminate_after_phase && !keep_all)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000568 remove(outname);
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400569 ofile = NULL;
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400570 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000571
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400572 if (operating_mode & OP_NORMAL) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -0700573 ofile = nasm_open_write(outname, (ofmt->flags & OFMT_TEXT) ? NF_TEXT : NF_BINARY);
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400574 if (!ofile)
H. Peter Anvinc5136902018-06-15 18:20:17 -0700575 nasm_fatal_fl(ERR_NOFILE,
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400576 "unable to open output file `%s'", outname);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000577
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400578 ofmt->init();
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400579 dfmt->init();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000580
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -0700581 assemble_file(inname, depend_list);
Victor van den Elzenc82c3722008-06-04 15:24:20 +0200582
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400583 if (!terminate_after_phase) {
H. Peter Anvin477ae442016-03-07 22:53:06 -0800584 ofmt->cleanup();
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400585 cleanup_labels();
586 fflush(ofile);
H. Peter Anvin274cda82016-05-10 02:56:29 -0700587 if (ferror(ofile)) {
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400588 nasm_error(ERR_NONFATAL|ERR_NOFILE,
589 "write error on output file `%s'", outname);
H. Peter Anvin274cda82016-05-10 02:56:29 -0700590 terminate_after_phase = true;
591 }
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400592 }
593
594 if (ofile) {
595 fclose(ofile);
H. Peter Anvin29695c82018-06-14 17:04:32 -0700596 if (terminate_after_phase && !keep_all)
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400597 remove(outname);
598 ofile = NULL;
599 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000600 }
H. Peter Anvin734b1882002-04-30 21:01:08 +0000601
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700602 if (depend_list && !terminate_after_phase)
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400603 emit_dependencies(depend_list);
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700604
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000605 if (want_usage)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000606 usage();
H. Peter Anvin734b1882002-04-30 21:01:08 +0000607
H. Peter Anvine2c80182005-01-15 22:15:51 +0000608 raa_free(offsets);
609 saa_free(forwrefs);
610 eval_cleanup();
H. Peter Anvin74cc5e52007-08-30 22:35:34 +0000611 stdscan_cleanup();
H. Peter Anvin274cda82016-05-10 02:56:29 -0700612 src_free();
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000613
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700614 return terminate_after_phase;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000615}
616
H. Peter Anvineba20a72002-04-30 20:53:55 +0000617/*
618 * Get a parameter for a command line option.
619 * First arg must be in the form of e.g. -f...
620 */
H. Peter Anvin423e3812007-11-15 17:12:29 -0800621static char *get_param(char *p, char *q, bool *advance)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000622{
H. Peter Anvin423e3812007-11-15 17:12:29 -0800623 *advance = false;
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +0400624 if (p[2]) /* the parameter's in the option */
625 return nasm_skip_spaces(p + 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000626 if (q && q[0]) {
H. Peter Anvin423e3812007-11-15 17:12:29 -0800627 *advance = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000628 return q;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000629 }
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400630 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000631 "option `-%c' requires an argument", p[1]);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000632 return NULL;
633}
634
H. Peter Anvindc242712007-11-18 11:55:10 -0800635/*
636 * Copy a filename
637 */
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800638static void copy_filename(const char **dst, const char *src, const char *what)
H. Peter Anvindc242712007-11-18 11:55:10 -0800639{
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800640 if (*dst)
H. Peter Anvinc5136902018-06-15 18:20:17 -0700641 nasm_fatal("more than one %s file specified: %s\n", what, src);
H. Peter Anvindc242712007-11-18 11:55:10 -0800642
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800643 *dst = nasm_strdup(src);
H. Peter Anvindc242712007-11-18 11:55:10 -0800644}
645
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700646/*
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700647 * Convert a string to a POSIX make-safe form
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700648 */
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700649static char *quote_for_pmake(const char *str)
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700650{
651 const char *p;
652 char *os, *q;
653
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400654 size_t n = 1; /* Terminating zero */
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700655 size_t nbs = 0;
656
657 if (!str)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400658 return NULL;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700659
660 for (p = str; *p; p++) {
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400661 switch (*p) {
662 case ' ':
663 case '\t':
664 /* Convert N backslashes + ws -> 2N+1 backslashes + ws */
665 n += nbs + 2;
666 nbs = 0;
667 break;
668 case '$':
669 case '#':
670 nbs = 0;
671 n += 2;
672 break;
673 case '\\':
674 nbs++;
675 n++;
676 break;
677 default:
678 nbs = 0;
679 n++;
680 break;
681 }
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700682 }
683
684 /* Convert N backslashes at the end of filename to 2N backslashes */
685 if (nbs)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400686 n += nbs;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700687
688 os = q = nasm_malloc(n);
689
690 nbs = 0;
691 for (p = str; *p; p++) {
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400692 switch (*p) {
693 case ' ':
694 case '\t':
695 while (nbs--)
696 *q++ = '\\';
697 *q++ = '\\';
698 *q++ = *p;
699 break;
700 case '$':
701 *q++ = *p;
702 *q++ = *p;
703 nbs = 0;
704 break;
705 case '#':
706 *q++ = '\\';
707 *q++ = *p;
708 nbs = 0;
709 break;
710 case '\\':
711 *q++ = *p;
712 nbs++;
713 break;
714 default:
715 *q++ = *p;
716 nbs = 0;
717 break;
718 }
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700719 }
720 while (nbs--)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400721 *q++ = '\\';
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700722
723 *q = '\0';
724
725 return os;
726}
727
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700728/*
729 * Convert a string to a Watcom make-safe form
730 */
731static char *quote_for_wmake(const char *str)
732{
733 const char *p;
734 char *os, *q;
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700735 bool quote = false;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700736
737 size_t n = 1; /* Terminating zero */
738
739 if (!str)
740 return NULL;
741
742 for (p = str; *p; p++) {
743 switch (*p) {
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700744 case ' ':
745 case '\t':
H. Peter Anvin3e30c322017-08-16 22:20:36 -0700746 case '&':
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700747 quote = true;
748 n++;
749 break;
750 case '\"':
751 quote = true;
752 n += 2;
753 break;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700754 case '$':
755 case '#':
756 n += 2;
757 break;
758 default:
759 n++;
760 break;
761 }
762 }
763
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700764 if (quote)
765 n += 2;
766
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700767 os = q = nasm_malloc(n);
768
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700769 if (quote)
770 *q++ = '\"';
771
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700772 for (p = str; *p; p++) {
773 switch (*p) {
774 case '$':
775 case '#':
776 *q++ = '$';
777 *q++ = *p;
778 break;
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700779 case '\"':
780 *q++ = *p;
781 *q++ = *p;
782 break;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700783 default:
784 *q++ = *p;
785 break;
786 }
787 }
788
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700789 if (quote)
790 *q++ = '\"';
791
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700792 *q = '\0';
793
794 return os;
795}
796
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100797enum text_options {
H. Peter Anvin3366e312018-02-07 14:14:36 -0800798 OPT_BOGUS,
799 OPT_VERSION,
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700800 OPT_HELP,
H. Peter Anvin3366e312018-02-07 14:14:36 -0800801 OPT_ABORT_ON_PANIC,
H. Peter Anvin05990342018-06-11 13:32:42 -0700802 OPT_MANGLE,
803 OPT_INCLUDE,
804 OPT_PRAGMA,
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700805 OPT_BEFORE,
H. Peter Anvin29695c82018-06-14 17:04:32 -0700806 OPT_LIMIT,
807 OPT_KEEP_ALL
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100808};
H. Peter Anvin3366e312018-02-07 14:14:36 -0800809struct textargs {
810 const char *label;
811 enum text_options opt;
812 bool need_arg;
H. Peter Anvin98578072018-06-01 18:02:54 -0700813 int pvt;
H. Peter Anvin3366e312018-02-07 14:14:36 -0800814};
H. Peter Anvin2530a102016-02-18 02:28:15 -0800815static const struct textargs textopts[] = {
H. Peter Anvin98578072018-06-01 18:02:54 -0700816 {"v", OPT_VERSION, false, 0},
817 {"version", OPT_VERSION, false, 0},
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700818 {"help", OPT_HELP, false, 0},
H. Peter Anvin98578072018-06-01 18:02:54 -0700819 {"abort-on-panic", OPT_ABORT_ON_PANIC, false, 0},
820 {"prefix", OPT_MANGLE, true, LM_GPREFIX},
821 {"postfix", OPT_MANGLE, true, LM_GSUFFIX},
822 {"gprefix", OPT_MANGLE, true, LM_GPREFIX},
823 {"gpostfix", OPT_MANGLE, true, LM_GSUFFIX},
824 {"lprefix", OPT_MANGLE, true, LM_LPREFIX},
825 {"lpostfix", OPT_MANGLE, true, LM_LSUFFIX},
H. Peter Anvin05990342018-06-11 13:32:42 -0700826 {"include", OPT_INCLUDE, true, 0},
827 {"pragma", OPT_PRAGMA, true, 0},
828 {"before", OPT_BEFORE, true, 0},
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700829 {"limit-", OPT_LIMIT, true, 0},
H. Peter Anvin29695c82018-06-14 17:04:32 -0700830 {"keep-all", OPT_KEEP_ALL, false, 0},
H. Peter Anvin98578072018-06-01 18:02:54 -0700831 {NULL, OPT_BOGUS, false, 0}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000832};
833
Cyrill Gorcunove7438432014-05-09 22:34:34 +0400834static void show_version(void)
835{
Dale Curtis10760492018-10-31 13:03:37 -0700836 printf("NASM version %s%s\n",
837 nasm_version, nasm_compile_options);
Cyrill Gorcunove7438432014-05-09 22:34:34 +0400838 exit(0);
839}
840
H. Peter Anvin423e3812007-11-15 17:12:29 -0800841static bool stopoptions = false;
H. Peter Anvin55568c12016-10-03 19:46:49 -0700842static bool process_arg(char *p, char *q, int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000843{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000844 char *param;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800845 bool advance = false;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000846
847 if (!p || !p[0])
H. Peter Anvin423e3812007-11-15 17:12:29 -0800848 return false;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000849
H. Peter Anvine2c80182005-01-15 22:15:51 +0000850 if (p[0] == '-' && !stopoptions) {
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400851 if (strchr("oOfpPdDiIlFXuUZwW", p[1])) {
852 /* These parameters take values */
853 if (!(param = get_param(p, q, &advance)))
854 return advance;
855 }
H. Peter Anvin423e3812007-11-15 17:12:29 -0800856
H. Peter Anvine2c80182005-01-15 22:15:51 +0000857 switch (p[1]) {
858 case 's':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700859 if (pass == 1)
860 error_file = stdout;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000861 break;
H. Peter Anvin70653092007-10-19 14:42:29 -0700862
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400863 case 'o': /* output file */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700864 if (pass == 2)
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800865 copy_filename(&outname, param, "output");
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400866 break;
H. Peter Anvinfd7dd112007-10-10 14:06:59 -0700867
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400868 case 'f': /* output format */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700869 if (pass == 1) {
870 ofmt = ofmt_find(param, &ofmt_alias);
871 if (!ofmt) {
H. Peter Anvinc5136902018-06-15 18:20:17 -0700872 nasm_fatal_fl(ERR_NOFILE | ERR_USAGE,
H. Peter Anvin55568c12016-10-03 19:46:49 -0700873 "unrecognised output format `%s' - "
874 "use -hf for a list", param);
875 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400876 }
877 break;
H. Peter Anvin70653092007-10-19 14:42:29 -0700878
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400879 case 'O': /* Optimization level */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700880 if (pass == 2) {
881 int opt;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700882
H. Peter Anvin55568c12016-10-03 19:46:49 -0700883 if (!*param) {
884 /* Naked -O == -Ox */
Chang S. Baea5786342018-08-15 23:22:21 +0300885 optimizing.level = MAX_OPTIMIZE;
H. Peter Anvin55568c12016-10-03 19:46:49 -0700886 } else {
887 while (*param) {
888 switch (*param) {
889 case '0': case '1': case '2': case '3': case '4':
890 case '5': case '6': case '7': case '8': case '9':
891 opt = strtoul(param, &param, 10);
H. Peter Anvind85d2502008-05-04 17:53:31 -0700892
Chang S. Baea5786342018-08-15 23:22:21 +0300893 /* -O0 -> optimizing.level == -1, 0.98 behaviour */
894 /* -O1 -> optimizing.level == 0, 0.98.09 behaviour */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700895 if (opt < 2)
Chang S. Baea5786342018-08-15 23:22:21 +0300896 optimizing.level = opt - 1;
H. Peter Anvin55568c12016-10-03 19:46:49 -0700897 else
Chang S. Baea5786342018-08-15 23:22:21 +0300898 optimizing.level = opt;
H. Peter Anvin55568c12016-10-03 19:46:49 -0700899 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700900
H. Peter Anvin55568c12016-10-03 19:46:49 -0700901 case 'v':
902 case '+':
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400903 param++;
904 opt_verbose_info = true;
905 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700906
H. Peter Anvin55568c12016-10-03 19:46:49 -0700907 case 'x':
908 param++;
Chang S. Baea5786342018-08-15 23:22:21 +0300909 optimizing.level = MAX_OPTIMIZE;
H. Peter Anvin55568c12016-10-03 19:46:49 -0700910 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700911
H. Peter Anvin55568c12016-10-03 19:46:49 -0700912 default:
H. Peter Anvinc5136902018-06-15 18:20:17 -0700913 nasm_fatal("unknown optimization option -O%c\n",
H. Peter Anvin55568c12016-10-03 19:46:49 -0700914 *param);
915 break;
916 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400917 }
Chang S. Baea5786342018-08-15 23:22:21 +0300918 if (optimizing.level > MAX_OPTIMIZE)
919 optimizing.level = MAX_OPTIMIZE;
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400920 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400921 }
922 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800923
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400924 case 'p': /* pre-include */
925 case 'P':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700926 if (pass == 2)
927 preproc->pre_include(param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400928 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800929
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400930 case 'd': /* pre-define */
931 case 'D':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700932 if (pass == 2)
933 preproc->pre_define(param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400934 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800935
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400936 case 'u': /* un-define */
937 case 'U':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700938 if (pass == 2)
939 preproc->pre_undefine(param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400940 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800941
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400942 case 'i': /* include search path */
943 case 'I':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700944 if (pass == 2)
945 preproc->include_path(param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400946 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800947
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400948 case 'l': /* listing file */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700949 if (pass == 2)
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800950 copy_filename(&listname, param, "listing");
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400951 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800952
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400953 case 'Z': /* error messages file */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700954 if (pass == 1)
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800955 copy_filename(&errname, param, "error");
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400956 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800957
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400958 case 'F': /* specify debug format */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700959 if (pass == 2) {
960 using_debug_info = true;
961 debug_format = param;
962 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400963 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800964
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400965 case 'X': /* specify error reporting format */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700966 if (pass == 1) {
967 if (nasm_stricmp("vc", param) == 0)
968 nasm_set_verror(nasm_verror_vc);
969 else if (nasm_stricmp("gnu", param) == 0)
970 nasm_set_verror(nasm_verror_gnu);
971 else
H. Peter Anvinc5136902018-06-15 18:20:17 -0700972 nasm_fatal_fl(ERR_NOFILE | ERR_USAGE,
H. Peter Anvin55568c12016-10-03 19:46:49 -0700973 "unrecognized error reporting format `%s'",
974 param);
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 'g':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700979 if (pass == 2) {
980 using_debug_info = true;
981 if (p[2])
982 debug_format = nasm_skip_spaces(p + 2);
983 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000984 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800985
H. Peter Anvine2c80182005-01-15 22:15:51 +0000986 case 'h':
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700987 help(p[2]);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400988 exit(0); /* never need usage message here */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000989 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800990
H. Peter Anvine2c80182005-01-15 22:15:51 +0000991 case 'y':
992 printf("\nvalid debug formats for '%s' output format are"
993 " ('*' denotes default):\n", ofmt->shortname);
994 dfmt_list(ofmt, stdout);
995 exit(0);
996 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800997
H. Peter Anvine2c80182005-01-15 22:15:51 +0000998 case 't':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700999 if (pass == 2)
1000 tasm_compatible_mode = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001001 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001002
H. Peter Anvine2c80182005-01-15 22:15:51 +00001003 case 'v':
Cyrill Gorcunove7438432014-05-09 22:34:34 +04001004 show_version();
H. Peter Anvine2c80182005-01-15 22:15:51 +00001005 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001006
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001007 case 'e': /* preprocess only */
1008 case 'E':
H. Peter Anvin55568c12016-10-03 19:46:49 -07001009 if (pass == 1)
1010 operating_mode = OP_PREPROCESS;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001011 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001012
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001013 case 'a': /* assemble only - don't preprocess */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001014 if (pass == 1)
1015 preproc = &preproc_nop;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001016 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001017
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001018 case 'w':
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001019 case 'W':
H. Peter Anvin55568c12016-10-03 19:46:49 -07001020 if (pass == 2) {
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001021 if (!set_warning_status(param)) {
1022 nasm_error(ERR_WARNING|ERR_NOFILE|ERR_WARN_UNK_WARNING,
1023 "unknown warning option: %s", param);
H. Peter Anvin55568c12016-10-03 19:46:49 -07001024 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001025 }
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001026 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001027
H. Peter Anvine2c80182005-01-15 22:15:51 +00001028 case 'M':
H. Peter Anvin77c9bf62017-08-16 21:14:33 -07001029 if (pass == 1) {
1030 switch (p[2]) {
1031 case 'W':
1032 quote_for_make = quote_for_wmake;
1033 break;
1034 case 'D':
1035 case 'F':
1036 case 'T':
1037 case 'Q':
1038 advance = true;
1039 break;
1040 default:
1041 break;
1042 }
1043 } else {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001044 switch (p[2]) {
1045 case 0:
1046 operating_mode = OP_DEPEND;
1047 break;
1048 case 'G':
1049 operating_mode = OP_DEPEND;
1050 depend_missing_ok = true;
1051 break;
1052 case 'P':
1053 depend_emit_phony = true;
1054 break;
1055 case 'D':
1056 operating_mode = OP_NORMAL;
1057 depend_file = q;
1058 advance = true;
1059 break;
1060 case 'F':
1061 depend_file = q;
1062 advance = true;
1063 break;
1064 case 'T':
1065 depend_target = q;
1066 advance = true;
1067 break;
1068 case 'Q':
1069 depend_target = quote_for_make(q);
1070 advance = true;
1071 break;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -07001072 case 'W':
1073 /* handled in pass 1 */
1074 break;
H. Peter Anvin55568c12016-10-03 19:46:49 -07001075 default:
1076 nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
1077 "unknown dependency option `-M%c'", p[2]);
1078 break;
1079 }
H. Peter Anvin77c9bf62017-08-16 21:14:33 -07001080 }
1081 if (advance && (!q || !q[0])) {
1082 nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
1083 "option `-M%c' requires a parameter", p[2]);
1084 break;
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001085 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001086 break;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001087
H. Peter Anvine2c80182005-01-15 22:15:51 +00001088 case '-':
1089 {
H. Peter Anvin3366e312018-02-07 14:14:36 -08001090 const struct textargs *tx;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001091 size_t olen, plen;
H. Peter Anvinc805fd72018-06-12 14:23:05 -07001092 char *eqsave;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001093
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001094 p += 2;
1095
1096 if (!*p) { /* -- => stop processing options */
H. Peter Anvin3366e312018-02-07 14:14:36 -08001097 stopoptions = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001098 break;
1099 }
Cyrill Gorcunove7438432014-05-09 22:34:34 +04001100
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001101 plen = strlen(p);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001102 for (tx = textopts; tx->label; tx++) {
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001103 olen = strlen(tx->label);
1104
1105 if (olen > plen)
1106 continue;
1107
1108 if (nasm_memicmp(p, tx->label, olen))
1109 continue;
1110
1111 if (tx->label[olen-1] == '-')
1112 break; /* Incomplete option */
1113
1114 if (!p[olen] || p[olen] == '=')
1115 break; /* Complete option */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001116 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001117
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001118 if (!tx->label) {
1119 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
1120 "unrecognized option `--%s'", p);
1121 }
1122
H. Peter Anvinc805fd72018-06-12 14:23:05 -07001123 eqsave = param = strchr(p+olen, '=');
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001124 if (param)
1125 *param++ = '\0';
1126
H. Peter Anvin3366e312018-02-07 14:14:36 -08001127 if (tx->need_arg) {
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001128 if (!param) {
1129 param = q;
1130 advance = true;
1131 }
1132
1133 /* Note: a null string is a valid parameter */
1134 if (!param) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001135 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvin3366e312018-02-07 14:14:36 -08001136 "option `--%s' requires an argument",
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001137 p);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001138 break;
1139 }
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001140 } else {
1141 if (param) {
1142 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
1143 "option `--%s' does not take an argument",
1144 p);
1145
1146 }
H. Peter Anvin3366e312018-02-07 14:14:36 -08001147 }
1148
1149 switch (tx->opt) {
1150 case OPT_VERSION:
1151 show_version();
1152 break;
1153 case OPT_ABORT_ON_PANIC:
1154 abort_on_panic = true;
1155 break;
H. Peter Anvin98578072018-06-01 18:02:54 -07001156 case OPT_MANGLE:
H. Peter Anvin3366e312018-02-07 14:14:36 -08001157 if (pass == 2)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001158 set_label_mangle(tx->pvt, param);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001159 break;
H. Peter Anvin05990342018-06-11 13:32:42 -07001160 case OPT_INCLUDE:
1161 if (pass == 2)
1162 preproc->pre_include(q);
1163 break;
1164 case OPT_PRAGMA:
1165 if (pass == 2)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001166 preproc->pre_command("pragma", param);
H. Peter Anvin05990342018-06-11 13:32:42 -07001167 break;
1168 case OPT_BEFORE:
1169 if (pass == 2)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001170 preproc->pre_command(NULL, param);
H. Peter Anvin05990342018-06-11 13:32:42 -07001171 break;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001172 case OPT_LIMIT:
1173 if (pass == 2)
1174 nasm_set_limit(p+olen, param);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001175 break;
H. Peter Anvin29695c82018-06-14 17:04:32 -07001176 case OPT_KEEP_ALL:
1177 keep_all = true;
1178 break;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001179 case OPT_HELP:
1180 help(0);
1181 exit(0);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001182 default:
1183 panic();
H. Peter Anvine2c80182005-01-15 22:15:51 +00001184 }
H. Peter Anvinc805fd72018-06-12 14:23:05 -07001185
1186 if (eqsave)
1187 *eqsave = '='; /* Restore = argument separator */
1188
H. Peter Anvine2c80182005-01-15 22:15:51 +00001189 break;
1190 }
1191
1192 default:
H. Peter Anvinac061332017-03-31 11:41:16 -07001193 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
1194 "unrecognised option `-%c'", p[1]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001195 break;
1196 }
H. Peter Anvin55568c12016-10-03 19:46:49 -07001197 } else if (pass == 2) {
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001198 /* In theory we could allow multiple input files... */
1199 copy_filename(&inname, p, "input");
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001200 }
1201
1202 return advance;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001203}
1204
H. Peter Anvineba20a72002-04-30 20:53:55 +00001205#define ARG_BUF_DELTA 128
1206
H. Peter Anvin55568c12016-10-03 19:46:49 -07001207static void process_respfile(FILE * rfile, int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001208{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001209 char *buffer, *p, *q, *prevarg;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001210 int bufsize, prevargsize;
1211
1212 bufsize = prevargsize = ARG_BUF_DELTA;
1213 buffer = nasm_malloc(ARG_BUF_DELTA);
1214 prevarg = nasm_malloc(ARG_BUF_DELTA);
1215 prevarg[0] = '\0';
1216
H. Peter Anvine2c80182005-01-15 22:15:51 +00001217 while (1) { /* Loop to handle all lines in file */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001218 p = buffer;
1219 while (1) { /* Loop to handle long lines */
1220 q = fgets(p, bufsize - (p - buffer), rfile);
1221 if (!q)
1222 break;
1223 p += strlen(p);
1224 if (p > buffer && p[-1] == '\n')
1225 break;
1226 if (p - buffer > bufsize - 10) {
1227 int offset;
1228 offset = p - buffer;
1229 bufsize += ARG_BUF_DELTA;
1230 buffer = nasm_realloc(buffer, bufsize);
1231 p = buffer + offset;
1232 }
1233 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001234
H. Peter Anvine2c80182005-01-15 22:15:51 +00001235 if (!q && p == buffer) {
1236 if (prevarg[0])
H. Peter Anvin55568c12016-10-03 19:46:49 -07001237 process_arg(prevarg, NULL, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001238 nasm_free(buffer);
1239 nasm_free(prevarg);
1240 return;
1241 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001242
H. Peter Anvine2c80182005-01-15 22:15:51 +00001243 /*
1244 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
1245 * them are present at the end of the line.
1246 */
1247 *(p = &buffer[strcspn(buffer, "\r\n\032")]) = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001248
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001249 while (p > buffer && nasm_isspace(p[-1]))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001250 *--p = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001251
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +04001252 p = nasm_skip_spaces(buffer);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001253
H. Peter Anvin55568c12016-10-03 19:46:49 -07001254 if (process_arg(prevarg, p, pass))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001255 *p = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001256
Charles Crayne192d5b52007-10-18 19:02:42 -07001257 if ((int) strlen(p) > prevargsize - 10) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001258 prevargsize += ARG_BUF_DELTA;
1259 prevarg = nasm_realloc(prevarg, prevargsize);
1260 }
H. Peter Anvindc242712007-11-18 11:55:10 -08001261 strncpy(prevarg, p, prevargsize);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001262 }
1263}
1264
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001265/* Function to process args from a string of args, rather than the
1266 * argv array. Used by the environment variable and response file
1267 * processing.
1268 */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001269static void process_args(char *args, int pass)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001270{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001271 char *p, *q, *arg, *prevarg;
1272 char separator = ' ';
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001273
1274 p = args;
1275 if (*p && *p != '-')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001276 separator = *p++;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001277 arg = NULL;
1278 while (*p) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001279 q = p;
1280 while (*p && *p != separator)
1281 p++;
1282 while (*p == separator)
1283 *p++ = '\0';
1284 prevarg = arg;
1285 arg = q;
H. Peter Anvin55568c12016-10-03 19:46:49 -07001286 if (process_arg(prevarg, arg, pass))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001287 arg = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001288 }
1289 if (arg)
H. Peter Anvin55568c12016-10-03 19:46:49 -07001290 process_arg(arg, NULL, pass);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001291}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001292
H. Peter Anvin55568c12016-10-03 19:46:49 -07001293static void process_response_file(const char *file, int pass)
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001294{
1295 char str[2048];
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001296 FILE *f = nasm_open_read(file, NF_TEXT);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001297 if (!f) {
Cyrill Gorcunovf1964512013-02-15 12:14:06 +04001298 perror(file);
1299 exit(-1);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001300 }
1301 while (fgets(str, sizeof str, f)) {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001302 process_args(str, pass);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001303 }
1304 fclose(f);
1305}
1306
H. Peter Anvin55568c12016-10-03 19:46:49 -07001307static void parse_cmdline(int argc, char **argv, int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001308{
1309 FILE *rfile;
Cyrill Gorcunovf4941892011-07-17 13:55:25 +04001310 char *envreal, *envcopy = NULL, *p;
H. Peter Anvin972079f2008-09-30 17:01:23 -07001311 int i;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001312
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001313 /* Initialize all the warnings to their default state */
1314 for (i = 0; i < ERR_WARN_ALL; i++) {
1315 warning_state_init[i] = warning_state[i] =
1316 warnings[i].enabled ? WARN_ST_ENABLED : 0;
1317 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001318
1319 /*
H. Peter Anvinff7ccc02002-05-06 19:41:57 +00001320 * First, process the NASMENV environment variable.
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001321 */
H. Peter Anvinff7ccc02002-05-06 19:41:57 +00001322 envreal = getenv("NASMENV");
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001323 if (envreal) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001324 envcopy = nasm_strdup(envreal);
H. Peter Anvin55568c12016-10-03 19:46:49 -07001325 process_args(envcopy, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001326 nasm_free(envcopy);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001327 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001328
1329 /*
1330 * Now process the actual command line.
1331 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001332 while (--argc) {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001333 bool advance;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001334 argv++;
1335 if (argv[0][0] == '@') {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001336 /*
1337 * We have a response file, so process this as a set of
H. Peter Anvine2c80182005-01-15 22:15:51 +00001338 * arguments like the environment variable. This allows us
1339 * to have multiple arguments on a single line, which is
1340 * different to the -@resp file processing below for regular
1341 * NASM.
1342 */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001343 process_response_file(argv[0]+1, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001344 argc--;
1345 argv++;
1346 }
1347 if (!stopoptions && argv[0][0] == '-' && argv[0][1] == '@') {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001348 p = get_param(argv[0], argc > 1 ? argv[1] : NULL, &advance);
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001349 if (p) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001350 rfile = nasm_open_read(p, NF_TEXT);
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001351 if (rfile) {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001352 process_respfile(rfile, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001353 fclose(rfile);
1354 } else
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001355 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001356 "unable to open response file `%s'", p);
1357 }
1358 } else
H. Peter Anvin55568c12016-10-03 19:46:49 -07001359 advance = process_arg(argv[0], argc > 1 ? argv[1] : NULL, pass);
H. Peter Anvin423e3812007-11-15 17:12:29 -08001360 argv += advance, argc -= advance;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001361 }
1362
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001363 /*
1364 * Look for basic command line typos. This definitely doesn't
1365 * catch all errors, but it might help cases of fumbled fingers.
1366 */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001367 if (pass != 2)
1368 return;
1369
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001370 if (!inname)
H. Peter Anvinc5136902018-06-15 18:20:17 -07001371 nasm_fatal_fl(ERR_NOFILE | ERR_USAGE, "no input file specified");
H. Peter Anvin59ddd262007-10-01 11:26:31 -07001372
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001373 else if ((errname && !strcmp(inname, errname)) ||
1374 (outname && !strcmp(inname, outname)) ||
1375 (listname && !strcmp(inname, listname)) ||
1376 (depend_file && !strcmp(inname, depend_file)))
H. Peter Anvinc5136902018-06-15 18:20:17 -07001377 nasm_fatal_fl(ERR_USAGE, "will not overwrite input file");
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001378
1379 if (errname) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001380 error_file = nasm_open_write(errname, NF_TEXT);
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001381 if (!error_file) {
1382 error_file = stderr; /* Revert to default! */
H. Peter Anvinc5136902018-06-15 18:20:17 -07001383 nasm_fatal_fl(ERR_NOFILE | ERR_USAGE,
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001384 "cannot open file `%s' for error messages",
1385 errname);
1386 }
Charles Craynefcce07f2007-09-30 22:15:36 -07001387 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001388}
1389
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001390static void assemble_file(const char *fname, StrList *depend_list)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001391{
H. Peter Anvinc7131682017-03-07 17:45:01 -08001392 char *line;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001393 insn output_ins;
H. Peter Anvinc7131682017-03-07 17:45:01 -08001394 int i;
H. Peter Anvin9c595b62017-03-07 19:44:21 -08001395 uint64_t prev_offset_changed;
H. Peter Anvina3d96d02018-06-15 17:51:39 -07001396 int64_t stall_count = 0; /* Make sure we make forward progress... */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001397
H. Peter Anvina7ecf262018-02-06 14:43:07 -08001398 switch (cmd_sb) {
1399 case 16:
1400 break;
1401 case 32:
1402 if (!iflag_cpu_level_ok(&cmd_cpu, IF_386))
H. Peter Anvinc5136902018-06-15 18:20:17 -07001403 nasm_fatal("command line: 32-bit segment size requires a higher cpu");
H. Peter Anvina7ecf262018-02-06 14:43:07 -08001404 break;
1405 case 64:
1406 if (!iflag_cpu_level_ok(&cmd_cpu, IF_X86_64))
H. Peter Anvinc5136902018-06-15 18:20:17 -07001407 nasm_fatal("command line: 64-bit segment size requires a higher cpu");
H. Peter Anvina7ecf262018-02-06 14:43:07 -08001408 break;
1409 default:
1410 panic();
1411 break;
1412 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001413
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001414 prev_offset_changed = nasm_limit[LIMIT_PASSES];
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001415 for (passn = 1; pass0 <= 2; passn++) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001416 pass1 = pass0 == 2 ? 2 : 1; /* 1, 1, 1, ..., 1, 2 */
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001417 pass2 = passn > 1 ? 2 : 1; /* 1, 2, 2, ..., 2, 2 */
1418 /* pass0 0, 0, 0, ..., 1, 2 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00001419
H. Peter Anvincac0b192017-03-28 16:12:30 -07001420 globalbits = cmd_sb; /* set 'bits' to command line default */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001421 cpu = cmd_cpu;
1422 if (pass0 == 2) {
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08001423 lfmt->init(listname);
H. Peter Anvin29695c82018-06-14 17:04:32 -07001424 } else if (passn == 1 && listname && !keep_all) {
H. Peter Anvinaac01ff2017-04-02 19:10:26 -07001425 /* Remove the list file in case we die before the output pass */
1426 remove(listname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001427 }
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001428 in_absolute = false;
Charles Craynec1905c22008-09-11 18:54:06 -07001429 global_offset_changed = 0; /* set by redefine_label */
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001430 if (passn > 1) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001431 saa_rewind(forwrefs);
1432 forwref = saa_rstruct(forwrefs);
1433 raa_free(offsets);
1434 offsets = raa_init();
1435 }
H. Peter Anvin892c4812018-05-30 14:43:46 -07001436 location.segment = NO_SEG;
1437 location.offset = 0;
1438 if (passn == 1)
1439 location.known = true;
1440 ofmt->reset();
1441 switch_segment(ofmt->section(NULL, pass2, &globalbits));
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001442 preproc->reset(fname, pass1, pass1 == 2 ? depend_list : NULL);
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001443
1444 /* Revert all warnings to the default state */
1445 memcpy(warning_state, warning_state_init, sizeof warning_state);
Victor van den Elzen819703a2008-07-16 15:20:56 +02001446
H. Peter Anvine2c80182005-01-15 22:15:51 +00001447 globallineno = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001448
H. Peter Anvine2c80182005-01-15 22:15:51 +00001449 while ((line = preproc->getline())) {
H. Peter Anvina3d96d02018-06-15 17:51:39 -07001450 if (++globallineno > nasm_limit[LIMIT_LINES])
H. Peter Anvinc5136902018-06-15 18:20:17 -07001451 nasm_fatal("overall line count exceeds the maximum %"PRId64"\n",
Cyrill Gorcunovf7b44f62018-10-15 22:58:13 +03001452 nasm_limit[LIMIT_LINES]);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001453
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001454 /*
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001455 * Here we parse our directives; this is not handled by the
H. Peter Anvinc7131682017-03-07 17:45:01 -08001456 * main parser.
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001457 */
H. Peter Anvina6e26d92017-03-07 21:32:37 -08001458 if (process_directives(line))
H. Peter Anvinc7131682017-03-07 17:45:01 -08001459 goto end_of_line; /* Just do final cleanup */
H. Peter Anvin62b24d72007-08-29 16:38:05 +00001460
H. Peter Anvinc7131682017-03-07 17:45:01 -08001461 /* Not a directive, or even something that starts with [ */
H. Peter Anvin98578072018-06-01 18:02:54 -07001462 parse_line(pass1, line, &output_ins);
H. Peter Anvinc7131682017-03-07 17:45:01 -08001463
Chang S. Baea5786342018-08-15 23:22:21 +03001464 if (optimizing.level > 0) {
H. Peter Anvinc7131682017-03-07 17:45:01 -08001465 if (forwref != NULL && globallineno == forwref->lineno) {
1466 output_ins.forw_ref = true;
1467 do {
1468 output_ins.oprs[forwref->operand].opflags |= OPFLAG_FORWARD;
1469 forwref = saa_rstruct(forwrefs);
1470 } while (forwref != NULL
1471 && forwref->lineno == globallineno);
1472 } else
1473 output_ins.forw_ref = false;
1474
1475 if (output_ins.forw_ref) {
1476 if (passn == 1) {
1477 for (i = 0; i < output_ins.operands; i++) {
1478 if (output_ins.oprs[i].opflags & OPFLAG_FORWARD) {
1479 struct forwrefinfo *fwinf = (struct forwrefinfo *)saa_wstruct(forwrefs);
1480 fwinf->lineno = globallineno;
1481 fwinf->operand = i;
Cyrill Gorcunovd5f2aef2010-04-20 15:33:45 +04001482 }
1483 }
1484 }
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001485 }
H. Peter Anvinc7131682017-03-07 17:45:01 -08001486 }
H. Peter Anvin8bec2c72009-08-08 13:49:00 -07001487
H. Peter Anvinc7131682017-03-07 17:45:01 -08001488 /* forw_ref */
1489 if (output_ins.opcode == I_EQU) {
Cyrill Gorcunove996d282018-10-13 16:18:16 +03001490 if (!output_ins.label) {
1491 nasm_error(ERR_NONFATAL, "EQU not preceded by label");
1492 } else if (output_ins.operands == 1 &&
1493 (output_ins.oprs[0].type & IMMEDIATE) &&
1494 output_ins.oprs[0].wrt == NO_SEG) {
H. Peter Anvin98578072018-06-01 18:02:54 -07001495 define_label(output_ins.label,
1496 output_ins.oprs[0].segment,
1497 output_ins.oprs[0].offset, false);
1498 } else if (output_ins.operands == 2
1499 && (output_ins.oprs[0].type & IMMEDIATE)
1500 && (output_ins.oprs[0].type & COLON)
1501 && output_ins.oprs[0].segment == NO_SEG
1502 && output_ins.oprs[0].wrt == NO_SEG
1503 && (output_ins.oprs[1].type & IMMEDIATE)
1504 && output_ins.oprs[1].segment == NO_SEG
1505 && output_ins.oprs[1].wrt == NO_SEG) {
1506 define_label(output_ins.label,
1507 output_ins.oprs[0].offset | SEG_ABS,
1508 output_ins.oprs[1].offset, false);
H. Peter Anvinc7131682017-03-07 17:45:01 -08001509 } else {
H. Peter Anvin98578072018-06-01 18:02:54 -07001510 nasm_error(ERR_NONFATAL, "bad syntax for EQU");
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001511 }
H. Peter Anvinc7131682017-03-07 17:45:01 -08001512 } else { /* instruction isn't an EQU */
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001513 int32_t n;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001514
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001515 nasm_assert(output_ins.times >= 0);
H. Peter Anvinc7131682017-03-07 17:45:01 -08001516
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001517 for (n = 1; n <= output_ins.times; n++) {
1518 if (pass1 == 1) {
H. Peter Anvin892c4812018-05-30 14:43:46 -07001519 int64_t l = insn_size(location.segment,
1520 location.offset,
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001521 globalbits, &output_ins);
1522
1523 /* if (using_debug_info) && output_ins.opcode != -1) */
1524 if (using_debug_info)
1525 { /* fbk 03/25/01 */
H. Peter Anvinc7131682017-03-07 17:45:01 -08001526 /* this is done here so we can do debug type info */
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001527 int32_t typeinfo =
1528 TYS_ELEMENTS(output_ins.operands);
1529 switch (output_ins.opcode) {
1530 case I_RESB:
1531 typeinfo =
1532 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_BYTE;
1533 break;
1534 case I_RESW:
1535 typeinfo =
1536 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_WORD;
1537 break;
1538 case I_RESD:
1539 typeinfo =
1540 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_DWORD;
1541 break;
1542 case I_RESQ:
1543 typeinfo =
1544 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_QWORD;
1545 break;
1546 case I_REST:
1547 typeinfo =
1548 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_TBYTE;
1549 break;
1550 case I_RESO:
1551 typeinfo =
1552 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_OWORD;
1553 break;
1554 case I_RESY:
1555 typeinfo =
1556 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_YWORD;
1557 break;
1558 case I_RESZ:
1559 typeinfo =
1560 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_ZWORD;
1561 break;
1562 case I_DB:
1563 typeinfo |= TY_BYTE;
1564 break;
1565 case I_DW:
1566 typeinfo |= TY_WORD;
1567 break;
1568 case I_DD:
1569 if (output_ins.eops_float)
1570 typeinfo |= TY_FLOAT;
1571 else
1572 typeinfo |= TY_DWORD;
1573 break;
1574 case I_DQ:
1575 typeinfo |= TY_QWORD;
1576 break;
1577 case I_DT:
1578 typeinfo |= TY_TBYTE;
1579 break;
1580 case I_DO:
1581 typeinfo |= TY_OWORD;
1582 break;
1583 case I_DY:
1584 typeinfo |= TY_YWORD;
1585 break;
1586 case I_DZ:
1587 typeinfo |= TY_ZWORD;
1588 break;
1589 default:
1590 typeinfo = TY_LABEL;
1591 break;
1592 }
H. Peter Anvinc7131682017-03-07 17:45:01 -08001593
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001594 dfmt->debug_typevalue(typeinfo);
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001595 }
H. Peter Anvinc7131682017-03-07 17:45:01 -08001596
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001597 /*
1598 * For INCBIN, let the code in assemble
1599 * handle TIMES, so we don't have to read the
1600 * input file over and over.
1601 */
1602 if (l != -1) {
H. Peter Anvin892c4812018-05-30 14:43:46 -07001603 increment_offset(l);
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001604 }
1605 /*
1606 * else l == -1 => invalid instruction, which will be
1607 * flagged as an error on pass 2
1608 */
1609 } else {
1610 if (n == 2)
1611 lfmt->uplevel(LIST_TIMES);
H. Peter Anvin892c4812018-05-30 14:43:46 -07001612 increment_offset(assemble(location.segment,
1613 location.offset,
1614 globalbits, &output_ins));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001615 }
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001616 } /* not an EQU */
1617 }
1618 if (output_ins.times > 1)
1619 lfmt->downlevel(LIST_TIMES);
H. Peter Anvinc7131682017-03-07 17:45:01 -08001620
H. Peter Anvinc7131682017-03-07 17:45:01 -08001621 cleanup_insn(&output_ins);
1622
1623 end_of_line:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001624 nasm_free(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001625 } /* end while (line = preproc->getline... */
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001626
H. Peter Anvin (Intel)b45c03a2018-06-27 21:03:38 -07001627 if (global_offset_changed && !terminate_after_phase) {
1628 switch (pass0) {
1629 case 1:
1630 nasm_error(ERR_WARNING|ERR_WARN_PHASE,
1631 "phase error during stabilization pass, hoping for the best");
1632 break;
1633
1634 case 2:
1635 nasm_error(ERR_NONFATAL,
1636 "phase error during code generation pass");
1637 break;
1638
1639 default:
1640 /* This is normal, we'll keep going... */
1641 break;
1642 }
1643 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001644
H. Peter Anvine2c80182005-01-15 22:15:51 +00001645 if (pass1 == 1)
1646 preproc->cleanup(1);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001647
H. Peter Anvin (Intel)12810fa2018-06-27 20:17:33 -07001648 /*
1649 * Always run at least two optimization passes (pass0 == 0);
1650 * things like subsections will fail miserably without that.
1651 * Once we commit to a stabilization pass (pass0 == 1), we can't
1652 * go back, and if something goes bad, we can only hope
1653 * that we don't end up with a phase error at the end.
1654 */
1655 if ((passn > 1 && !global_offset_changed) || pass0 > 0) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001656 pass0++;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001657 } else if (global_offset_changed &&
1658 global_offset_changed < prev_offset_changed) {
Charles Craynec1905c22008-09-11 18:54:06 -07001659 prev_offset_changed = global_offset_changed;
1660 stall_count = 0;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001661 } else {
1662 stall_count++;
1663 }
Victor van den Elzen42528232008-09-11 15:07:05 +02001664
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001665 if (terminate_after_phase)
1666 break;
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001667
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001668 if ((stall_count > nasm_limit[LIMIT_STALLED]) ||
1669 (passn >= nasm_limit[LIMIT_PASSES])) {
Victor van den Elzen42528232008-09-11 15:07:05 +02001670 /* We get here if the labels don't converge
1671 * Example: FOO equ FOO + 1
1672 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001673 nasm_error(ERR_NONFATAL,
Victor van den Elzen42528232008-09-11 15:07:05 +02001674 "Can't find valid values for all labels "
H. Peter Anvina3d96d02018-06-15 17:51:39 -07001675 "after %"PRId64" passes, giving up.", passn);
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001676 nasm_error(ERR_NONFATAL,
1677 "Possible causes: recursive EQUs, macro abuse.");
1678 break;
1679 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001680 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001681
H. Peter Anvine2c80182005-01-15 22:15:51 +00001682 preproc->cleanup(0);
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08001683 lfmt->cleanup();
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001684 if (!terminate_after_phase && opt_verbose_info) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001685 /* -On and -Ov switches */
H. Peter Anvina3d96d02018-06-15 17:51:39 -07001686 fprintf(stdout, "info: assembly required 1+%"PRId64"+1 passes\n",
1687 passn-3);
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001688 }
1689}
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001690
Ed Berosetfa771012002-06-09 20:56:40 +00001691/**
1692 * gnu style error reporting
1693 * This function prints an error message to error_file in the
1694 * style used by GNU. An example would be:
1695 * file.asm:50: error: blah blah blah
H. Peter Anvin70653092007-10-19 14:42:29 -07001696 * where file.asm is the name of the file, 50 is the line number on
Ed Berosetfa771012002-06-09 20:56:40 +00001697 * which the error occurs (or is detected) and "error:" is one of
1698 * the possible optional diagnostics -- it can be "error" or "warning"
H. Peter Anvin70653092007-10-19 14:42:29 -07001699 * or something else. Finally the line terminates with the actual
Ed Berosetfa771012002-06-09 20:56:40 +00001700 * error message.
H. Peter Anvin70653092007-10-19 14:42:29 -07001701 *
1702 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001703 * @param fmt the printf style format string
1704 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001705static void nasm_verror_gnu(int severity, const char *fmt, va_list ap)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001706{
H. Peter Anvin274cda82016-05-10 02:56:29 -07001707 const char *currentfile = NULL;
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001708 int32_t lineno = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001709
Ed Berosetfa771012002-06-09 20:56:40 +00001710 if (is_suppressed_warning(severity))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001711 return;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001712
H. Peter Anvin7a6bf742017-12-20 11:54:31 -08001713 if (!(severity & ERR_NOFILE)) {
H. Peter Anvin4def1a82016-05-09 13:59:44 -07001714 src_get(&lineno, &currentfile);
H. Peter Anvin7a6bf742017-12-20 11:54:31 -08001715 if (!currentfile || (severity & ERR_TOPFILE)) {
1716 currentfile = inname[0] ? inname : outname[0] ? outname : NULL;
1717 lineno = 0;
1718 }
1719 }
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001720
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001721 if (!skip_this_pass(severity)) {
H. Peter Anvin7a6bf742017-12-20 11:54:31 -08001722 if (!lineno)
1723 fprintf(error_file, "%s:", currentfile ? currentfile : "nasm");
H. Peter Anvin883985d2017-12-20 11:32:39 -08001724 else
1725 fprintf(error_file, "%s:%"PRId32": ", currentfile, lineno);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001726 }
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001727
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001728 nasm_verror_common(severity, fmt, ap);
Ed Berosetfa771012002-06-09 20:56:40 +00001729}
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001730
Ed Berosetfa771012002-06-09 20:56:40 +00001731/**
1732 * MS style error reporting
1733 * This function prints an error message to error_file in the
H. Peter Anvin70653092007-10-19 14:42:29 -07001734 * style used by Visual C and some other Microsoft tools. An example
Ed Berosetfa771012002-06-09 20:56:40 +00001735 * would be:
Ed Beroset6e61d0d2002-06-11 03:29:36 +00001736 * file.asm(50) : error: blah blah blah
H. Peter Anvin70653092007-10-19 14:42:29 -07001737 * where file.asm is the name of the file, 50 is the line number on
Ed Beroset6e61d0d2002-06-11 03:29:36 +00001738 * which the error occurs (or is detected) and "error:" is one of
1739 * the possible optional diagnostics -- it can be "error" or "warning"
H. Peter Anvin70653092007-10-19 14:42:29 -07001740 * or something else. Finally the line terminates with the actual
Ed Beroset6e61d0d2002-06-11 03:29:36 +00001741 * error message.
H. Peter Anvin70653092007-10-19 14:42:29 -07001742 *
1743 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001744 * @param fmt the printf style format string
1745 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001746static void nasm_verror_vc(int severity, const char *fmt, va_list ap)
Ed Berosetfa771012002-06-09 20:56:40 +00001747{
H. Peter Anvin274cda82016-05-10 02:56:29 -07001748 const char *currentfile = NULL;
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001749 int32_t lineno = 0;
Ed Berosetfa771012002-06-09 20:56:40 +00001750
H. Peter Anvine2c80182005-01-15 22:15:51 +00001751 if (is_suppressed_warning(severity))
1752 return;
Ed Berosetfa771012002-06-09 20:56:40 +00001753
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001754 if (!(severity & ERR_NOFILE))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001755 src_get(&lineno, &currentfile);
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001756
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001757 if (!skip_this_pass(severity)) {
1758 if (currentfile) {
1759 fprintf(error_file, "%s(%"PRId32") : ", currentfile, lineno);
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001760 } else {
1761 fputs("nasm: ", error_file);
1762 }
Ed Berosetfa771012002-06-09 20:56:40 +00001763 }
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001764
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001765 nasm_verror_common(severity, fmt, ap);
Ed Berosetfa771012002-06-09 20:56:40 +00001766}
1767
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001768/*
1769 * check to see if this is a suppressable warning
1770 */
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001771static inline bool is_valid_warning(int severity)
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001772{
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001773 /* Not a warning at all */
1774 if ((severity & ERR_MASK) != ERR_WARNING)
1775 return false;
1776
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001777 return WARN_IDX(severity) < ERR_WARN_ALL;
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001778}
1779
Ed Berosetfa771012002-06-09 20:56:40 +00001780/**
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001781 * check for suppressed warning
H. Peter Anvin70653092007-10-19 14:42:29 -07001782 * checks for suppressed warning or pass one only warning and we're
Ed Berosetfa771012002-06-09 20:56:40 +00001783 * not in pass 1
1784 *
1785 * @param severity the severity of the warning or error
1786 * @return true if we should abort error/warning printing
1787 */
H. Peter Anvin2b046cf2008-01-22 14:08:36 -08001788static bool is_suppressed_warning(int severity)
Ed Berosetfa771012002-06-09 20:56:40 +00001789{
H. Peter Anvin442a05a2012-02-24 21:50:53 -08001790 /* Might be a warning but suppresed explicitly */
H. Peter Anvinc805fd72018-06-12 14:23:05 -07001791 if (is_valid_warning(severity) && !(severity & ERR_USAGE))
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001792 return !(warning_state[WARN_IDX(severity)] & WARN_ST_ENABLED);
1793 else
1794 return false;
1795}
1796
1797static bool warning_is_error(int severity)
1798{
1799 if (is_valid_warning(severity))
1800 return !!(warning_state[WARN_IDX(severity)] & WARN_ST_ERROR);
H. Peter Anvin442a05a2012-02-24 21:50:53 -08001801 else
1802 return false;
Ed Berosetfa771012002-06-09 20:56:40 +00001803}
1804
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001805static bool skip_this_pass(int severity)
1806{
H. Peter Anvin8f622462017-04-02 19:02:29 -07001807 /*
1808 * See if it's a pass-specific error or warning which should be skipped.
1809 * We cannot skip errors stronger than ERR_NONFATAL as by definition
1810 * they cannot be resumed from.
1811 */
1812 if ((severity & ERR_MASK) > ERR_NONFATAL)
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001813 return false;
1814
H. Peter Anvin934f0472016-05-09 12:00:19 -07001815 /*
1816 * passn is 1 on the very first pass only.
1817 * pass0 is 2 on the code-generation (final) pass only.
1818 * These are the passes we care about in this case.
1819 */
1820 return (((severity & ERR_PASS1) && passn != 1) ||
1821 ((severity & ERR_PASS2) && pass0 != 2));
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001822}
1823
Ed Berosetfa771012002-06-09 20:56:40 +00001824/**
1825 * common error reporting
1826 * This is the common back end of the error reporting schemes currently
H. Peter Anvin70653092007-10-19 14:42:29 -07001827 * implemented. It prints the nature of the warning and then the
Ed Berosetfa771012002-06-09 20:56:40 +00001828 * specific error message to error_file and may or may not return. It
1829 * doesn't return if the error severity is a "panic" or "debug" type.
H. Peter Anvin70653092007-10-19 14:42:29 -07001830 *
1831 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001832 * @param fmt the printf style format string
1833 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001834static void nasm_verror_common(int severity, const char *fmt, va_list args)
Ed Berosetfa771012002-06-09 20:56:40 +00001835{
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001836 char msg[1024];
1837 const char *pfx;
H. Peter Anvin323fcff2009-07-12 12:04:56 -07001838
H. Peter Anvin7df04172008-06-10 18:27:38 -07001839 switch (severity & (ERR_MASK|ERR_NO_SEVERITY)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001840 case ERR_WARNING:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001841 pfx = "warning: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001842 break;
1843 case ERR_NONFATAL:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001844 pfx = "error: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001845 break;
1846 case ERR_FATAL:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001847 pfx = "fatal: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001848 break;
1849 case ERR_PANIC:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001850 pfx = "panic: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001851 break;
1852 case ERR_DEBUG:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001853 pfx = "debug: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001854 break;
H. Peter Anvin7df04172008-06-10 18:27:38 -07001855 default:
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001856 pfx = "";
1857 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001858 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00001859
H. Peter Anvin934f0472016-05-09 12:00:19 -07001860 vsnprintf(msg, sizeof msg - 64, fmt, args);
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001861 if (is_valid_warning(severity) && WARN_IDX(severity) != ERR_WARN_OTHER) {
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001862 char *p = strchr(msg, '\0');
H. Peter Anvin934f0472016-05-09 12:00:19 -07001863 snprintf(p, 64, " [-w+%s]", warnings[WARN_IDX(severity)].name);
1864 }
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001865
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001866 if (!skip_this_pass(severity))
1867 fprintf(error_file, "%s%s\n", pfx, msg);
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001868
H. Peter Anvin4def1a82016-05-09 13:59:44 -07001869 /* Are we recursing from error_list_macros? */
1870 if (severity & ERR_PP_LISTMACRO)
1871 return;
1872
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001873 /*
1874 * Don't suppress this with skip_this_pass(), or we don't get
H. Peter Anvin934f0472016-05-09 12:00:19 -07001875 * pass1 or preprocessor warnings in the list file
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001876 */
H. Peter Anvin0fcb4882016-07-06 11:55:25 -07001877 lfmt->error(severity, pfx, msg);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001878
H. Peter Anvin8f622462017-04-02 19:02:29 -07001879 if (skip_this_pass(severity))
1880 return;
1881
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001882 if (severity & ERR_USAGE)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001883 want_usage = true;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001884
H. Peter Anvin4def1a82016-05-09 13:59:44 -07001885 preproc->error_list_macros(severity);
1886
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001887 switch (severity & ERR_MASK) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001888 case ERR_DEBUG:
1889 /* no further action, by definition */
1890 break;
H. Peter Anvinb030c922007-11-13 11:31:15 -08001891 case ERR_WARNING:
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001892 /* Treat warnings as errors */
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001893 if (warning_is_error(severity))
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001894 terminate_after_phase = true;
1895 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001896 case ERR_NONFATAL:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001897 terminate_after_phase = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001898 break;
1899 case ERR_FATAL:
1900 if (ofile) {
1901 fclose(ofile);
H. Peter Anvin29695c82018-06-14 17:04:32 -07001902 if (!keep_all)
1903 remove(outname);
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001904 ofile = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001905 }
1906 if (want_usage)
1907 usage();
1908 exit(1); /* instantly die */
1909 break; /* placate silly compilers */
1910 case ERR_PANIC:
1911 fflush(NULL);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001912
1913 if (abort_on_panic)
1914 abort(); /* halt, catch fire, dump core/stop debugger */
1915
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001916 if (ofile) {
1917 fclose(ofile);
H. Peter Anvin29695c82018-06-14 17:04:32 -07001918 if (!keep_all)
1919 remove(outname);
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001920 ofile = NULL;
1921 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001922 exit(3);
1923 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001924 }
1925}
1926
H. Peter Anvin734b1882002-04-30 21:01:08 +00001927static void usage(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001928{
H. Peter Anvin620515a2002-04-30 20:57:38 +00001929 fputs("type `nasm -h' for help\n", error_file);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001930}
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001931
1932static void help(const char xopt)
1933{
1934 int i;
1935
1936 printf
1937 ("usage: nasm [-@ response file] [-o outfile] [-f format] "
1938 "[-l listfile]\n"
1939 " [options...] [--] filename\n"
1940 " or nasm -v (or --v) for version info\n\n"
1941 "\n"
1942 "Response files should contain command line parameters,\n"
1943 "one per line.\n"
1944 "\n"
1945 " -t assemble in SciTech TASM compatible mode\n");
1946 printf
1947 (" -E (or -e) preprocess only (writes output to stdout by default)\n"
1948 " -a don't preprocess (assemble only)\n"
1949 " -M generate Makefile dependencies on stdout\n"
1950 " -MG d:o, missing files assumed generated\n"
1951 " -MF file set Makefile dependency file\n"
1952 " -MD file assemble and generate dependencies\n"
1953 " -MT file dependency target name\n"
1954 " -MQ file dependency target name (quoted)\n"
1955 " -MP emit phony target\n\n"
1956 " -Zfile redirect error messages to file\n"
1957 " -s redirect error messages to stdout\n\n"
1958 " -g generate debugging information\n\n"
1959 " -F format select a debugging format\n\n"
1960 " -gformat same as -g -F format\n\n"
1961 " -o outfile write output to an outfile\n\n"
1962 " -f format select an output format\n\n"
1963 " -l listfile write listing to a listfile\n\n"
1964 " -Ipath add a pathname to the include file path\n");
1965 printf
1966 (" -Olevel optimize opcodes, immediates and branch offsets\n"
1967 " -O0 no optimization\n"
1968 " -O1 minimal optimization\n"
1969 " -Ox multipass optimization (default)\n"
1970 " -Pfile pre-include a file (also --include)\n"
1971 " -Dmacro[=str] pre-define a macro\n"
1972 " -Umacro undefine a macro\n"
1973 " -Xformat specifiy error reporting format (gnu or vc)\n"
1974 " -w+foo enable warning foo (equiv. -Wfoo)\n"
1975 " -w-foo disable warning foo (equiv. -Wno-foo)\n"
1976 " -w[+-]error[=foo]\n"
1977 " promote [specific] warnings to errors\n"
Chang S. Bae1af6ef42018-06-20 17:05:12 -07001978 " -h show invocation summary and exit (also --help)\n\n"
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001979 " --pragma str pre-executes a specific %%pragma\n"
1980 " --before str add line (usually a preprocessor statement) before the input\n"
1981 " --prefix str prepend the given string to all the given string\n"
Chang S. Bae1af6ef42018-06-20 17:05:12 -07001982 " to all extern, common and global symbols (also --gprefix)\n"
1983 " --postfix str append the given string to all the given string\n"
1984 " to all extern, common and global symbols (also --gpostfix)\n"
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001985 " --lprefix str prepend the given string to all other symbols\n"
Chang S. Bae1af6ef42018-06-20 17:05:12 -07001986 " --lpostfix str append the given string to all other symbols\n"
1987 " --keep-all output files will not be removed even if an error happens\n"
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001988 " --limit-X val set execution limit X\n");
1989
1990 for (i = 0; i <= LIMIT_MAX; i++) {
1991 printf(" %-15s %s (default ",
1992 limit_info[i].name, limit_info[i].help);
1993 if (nasm_limit[i] < LIMIT_MAX_VAL) {
H. Peter Anvina3d96d02018-06-15 17:51:39 -07001994 printf("%"PRId64")\n", nasm_limit[i]);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001995 } else {
1996 printf("unlimited)\n");
1997 }
1998 }
1999
2000 printf("\nWarnings for the -W/-w options:\n");
2001
2002 for (i = 0; i <= ERR_WARN_ALL; i++)
2003 printf(" %-23s %s%s\n",
2004 warnings[i].name, warnings[i].help,
2005 i == ERR_WARN_ALL ? "\n" :
2006 warnings[i].enabled ? " (default on)" :
2007 " (default off)");
2008
2009 if (xopt == 'f') {
2010 printf("valid output formats for -f are"
2011 " (`*' denotes default):\n");
2012 ofmt_list(ofmt, stdout);
2013 } else {
2014 printf("For a list of valid output formats, use -hf.\n");
2015 printf("For a list of debug formats, use -f <format> -y.\n");
2016 }
2017}