blob: 8f5fced1462e7e003ddbdb0ae777e989551bbfa9 [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>
H. Peter Anvinfd7dd112007-10-10 14:06:59 -070044#include <limits.h>
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000045
46#include "nasm.h"
47#include "nasmlib.h"
H. Peter Anvin13506202018-11-28 14:55:58 -080048#include "nctype.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);
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +030078static void assemble_file(const char *, struct 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 Gorcunovb7bb5ac2018-11-11 21:33:52 +0300137static struct strlist *include_path;
Cyrill Gorcunov86b2ad02011-07-01 10:38:25 +0400138
H. Peter Anvin34754622018-11-28 12:36:53 -0800139#define OP_NORMAL (1U << 0)
140#define OP_PREPROCESS (1U << 1)
141#define OP_DEPEND (1U << 2)
Cyrill Gorcunove9fc88c2014-06-23 02:22:02 +0400142
143static unsigned int operating_mode;
Cyrill Gorcunov3ed32cb2014-06-22 20:53:12 +0400144
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700145/* Dependency flags */
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700146static bool depend_emit_phony = false;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700147static bool depend_missing_ok = false;
148static const char *depend_target = NULL;
149static const char *depend_file = NULL;
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +0300150struct strlist *depend_list;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000151
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700152static bool want_usage;
153static bool terminate_after_phase;
H. Peter Anvin130736c2016-02-17 20:27:41 -0800154bool user_nolist = false;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000155
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700156static char *quote_for_pmake(const char *str);
157static char *quote_for_wmake(const char *str);
158static char *(*quote_for_make)(const char *) = quote_for_pmake;
H. Peter Anvin55340992012-09-09 17:09:00 -0700159
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700160/*
161 * Execution limits that can be set via a command-line option or %pragma
162 */
163
H. Peter Anvina3d96d02018-06-15 17:51:39 -0700164#define LIMIT_MAX_VAL (INT64_MAX >> 1) /* Effectively unlimited */
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700165
H. Peter Anvina3d96d02018-06-15 17:51:39 -0700166int64_t nasm_limit[LIMIT_MAX+1] =
167{ LIMIT_MAX_VAL, 1000, 1000000, 1000000, 1000000, 2000000000 };
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700168
169struct limit_info {
170 const char *name;
171 const char *help;
172};
173static const struct limit_info limit_info[LIMIT_MAX+1] = {
174 { "passes", "total number of passes" },
175 { "stalled-passes", "number of passes without forward progress" },
176 { "macro-levels", "levels of macro expansion"},
177 { "rep", "%rep count" },
H. Peter Anvina3d96d02018-06-15 17:51:39 -0700178 { "eval", "expression evaluation descent"},
179 { "lines", "total source lines processed"}
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700180};
181
H. Peter Anvinc805fd72018-06-12 14:23:05 -0700182enum directive_result
183nasm_set_limit(const char *limit, const char *valstr)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700184{
185 int i;
186 int64_t val;
187 bool rn_error;
H. Peter Anvinc805fd72018-06-12 14:23:05 -0700188 int errlevel;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700189
190 for (i = 0; i <= LIMIT_MAX; i++) {
191 if (!nasm_stricmp(limit, limit_info[i].name))
192 break;
193 }
194 if (i > LIMIT_MAX) {
H. Peter Anvinc805fd72018-06-12 14:23:05 -0700195 if (passn == 0)
H. Peter Anvinc55702e2018-12-10 22:06:15 -0800196 errlevel = ERR_WARNING|ERR_USAGE;
H. Peter Anvinc805fd72018-06-12 14:23:05 -0700197 else
198 errlevel = ERR_WARNING|ERR_PASS1|ERR_WARN_UNKNOWN_PRAGMA;
199 nasm_error(errlevel, "unknown limit: `%s'", limit);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700200 return DIRR_ERROR;
201 }
202
203 if (!nasm_stricmp(valstr, "unlimited")) {
204 val = LIMIT_MAX_VAL;
205 } else {
206 val = readnum(valstr, &rn_error);
207 if (rn_error || val < 0) {
H. Peter Anvinc805fd72018-06-12 14:23:05 -0700208 if (passn == 0)
H. Peter Anvinc55702e2018-12-10 22:06:15 -0800209 errlevel = ERR_WARNING|ERR_USAGE;
H. Peter Anvinc805fd72018-06-12 14:23:05 -0700210 else
211 errlevel = ERR_WARNING|ERR_PASS1|ERR_WARN_BAD_PRAGMA;
212 nasm_error(errlevel, "invalid limit value: `%s'", limit);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700213 return DIRR_ERROR;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700214 }
H. Peter Anvinc805fd72018-06-12 14:23:05 -0700215 if (val > LIMIT_MAX_VAL)
216 val = LIMIT_MAX_VAL;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700217 }
218
219 nasm_limit[i] = val;
220 return DIRR_OK;
221}
222
H. Peter Anvin892c4812018-05-30 14:43:46 -0700223int64_t switch_segment(int32_t segment)
Cyrill Gorcunov190232f2013-02-15 12:35:04 +0400224{
H. Peter Anvin892c4812018-05-30 14:43:46 -0700225 location.segment = segment;
226 if (segment == NO_SEG) {
227 location.offset = absolute.offset;
228 in_absolute = true;
229 } else {
230 location.offset = raa_read(offsets, segment);
231 in_absolute = false;
232 }
233 return location.offset;
Cyrill Gorcunov190232f2013-02-15 12:35:04 +0400234}
235
236static void set_curr_offs(int64_t l_off)
237{
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800238 if (in_absolute)
239 absolute.offset = l_off;
Cyrill Gorcunov190232f2013-02-15 12:35:04 +0400240 else
241 offsets = raa_write(offsets, location.segment, l_off);
242}
243
H. Peter Anvin892c4812018-05-30 14:43:46 -0700244static void increment_offset(int64_t delta)
245{
246 if (unlikely(delta == 0))
247 return;
248
249 location.offset += delta;
250 set_curr_offs(location.offset);
251}
252
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000253static void nasm_fputs(const char *line, FILE * outfile)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000254{
H. Peter Anvin310b3e12002-05-14 22:38:55 +0000255 if (outfile) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000256 fputs(line, outfile);
H. Peter Anvind1fb15c2007-11-13 09:37:59 -0800257 putc('\n', outfile);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000258 } else
H. Peter Anvine2c80182005-01-15 22:15:51 +0000259 puts(line);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000260}
261
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800262/*
263 * Define system-defined macros that are not part of
264 * macros/standard.mac.
265 */
266static void define_macros(void)
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800267{
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700268 const struct compile_time * const oct = &official_compile_time;
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800269 char temp[128];
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800270
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700271 if (oct->have_local) {
272 strftime(temp, sizeof temp, "__DATE__=\"%Y-%m-%d\"", &oct->local);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400273 preproc->pre_define(temp);
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700274 strftime(temp, sizeof temp, "__DATE_NUM__=%Y%m%d", &oct->local);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400275 preproc->pre_define(temp);
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700276 strftime(temp, sizeof temp, "__TIME__=\"%H:%M:%S\"", &oct->local);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400277 preproc->pre_define(temp);
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700278 strftime(temp, sizeof temp, "__TIME_NUM__=%H%M%S", &oct->local);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400279 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800280 }
281
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700282 if (oct->have_gm) {
283 strftime(temp, sizeof temp, "__UTC_DATE__=\"%Y-%m-%d\"", &oct->gm);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400284 preproc->pre_define(temp);
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700285 strftime(temp, sizeof temp, "__UTC_DATE_NUM__=%Y%m%d", &oct->gm);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400286 preproc->pre_define(temp);
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700287 strftime(temp, sizeof temp, "__UTC_TIME__=\"%H:%M:%S\"", &oct->gm);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400288 preproc->pre_define(temp);
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700289 strftime(temp, sizeof temp, "__UTC_TIME_NUM__=%H%M%S", &oct->gm);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400290 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800291 }
H. Peter Anvind85d2502008-05-04 17:53:31 -0700292
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700293 if (oct->have_posix) {
294 snprintf(temp, sizeof temp, "__POSIX_TIME__=%"PRId64, oct->posix);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400295 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800296 }
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800297
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 Anvinbf6230b2018-11-11 13:25:16 -0800306
307 /*
308 * Output-format specific macros.
309 */
310 if (ofmt->stdmac)
311 preproc->extra_stdmac(ofmt->stdmac);
312
313 /*
314 * Debug format, if any
315 */
316 if (dfmt != &null_debug_form) {
317 snprintf(temp, sizeof(temp), "__DEBUG_FORMAT__=%s", dfmt->shortname);
318 preproc->pre_define(temp);
319 }
320}
321
322/*
323 * Initialize the preprocessor, set up the include path, and define
324 * the system-included macros. This is called between passes 1 and 2
325 * of parsing the command options; ofmt and dfmt are defined at this
326 * point.
327 *
328 * Command-line specified preprocessor directives (-p, -d, -u,
329 * --pragma, --before) are processed after this function.
330 */
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +0300331static void preproc_init(struct strlist *ipath)
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800332{
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800333 preproc->init();
334 define_macros();
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +0300335 preproc->include_path(ipath);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800336}
337
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +0300338static void emit_dependencies(struct strlist *list)
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700339{
340 FILE *deps;
341 int linepos, len;
H. Peter Anvinf05034f2017-08-16 22:08:36 -0700342 bool wmake = (quote_for_make == quote_for_wmake);
343 const char *wrapstr, *nulltarget;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -0700344 struct strlist_entry *l;
345
346 if (!list)
347 return;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700348
H. Peter Anvinf05034f2017-08-16 22:08:36 -0700349 wrapstr = wmake ? " &\n " : " \\\n ";
350 nulltarget = wmake ? "\t%null\n" : "";
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700351
352 if (depend_file && strcmp(depend_file, "-")) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -0700353 deps = nasm_open_write(depend_file, NF_TEXT);
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400354 if (!deps) {
H. Peter Anvinc55702e2018-12-10 22:06:15 -0800355 nasm_nonfatal("unable to write dependency file `%s'", depend_file);
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400356 return;
357 }
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700358 } else {
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400359 deps = stdout;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700360 }
H. Peter Anvin323fcff2009-07-12 12:04:56 -0700361
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700362 linepos = fprintf(deps, "%s :", depend_target);
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -0700363 list_for_each(l, list->head) {
H. Peter Anvin55340992012-09-09 17:09:00 -0700364 char *file = quote_for_make(l->str);
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400365 len = strlen(file);
366 if (linepos + len > 62 && linepos > 1) {
H. Peter Anvinf05034f2017-08-16 22:08:36 -0700367 fputs(wrapstr, deps);
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400368 linepos = 1;
369 }
370 fprintf(deps, " %s", file);
371 linepos += len+1;
H. Peter Anvin55340992012-09-09 17:09:00 -0700372 nasm_free(file);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700373 }
374 fprintf(deps, "\n\n");
H. Peter Anvin323fcff2009-07-12 12:04:56 -0700375
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -0700376 list_for_each(l, list->head) {
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700377 if (depend_emit_phony) {
378 char *file = quote_for_make(l->str);
H. Peter Anvinf05034f2017-08-16 22:08:36 -0700379 fprintf(deps, "%s :\n%s\n", file, nulltarget);
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700380 nasm_free(file);
381 }
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700382 }
383
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -0700384 strlist_free(list);
385
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700386 if (deps != stdout)
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400387 fclose(deps);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700388}
389
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700390/* Convert a struct tm to a POSIX-style time constant */
391static int64_t make_posix_time(const struct tm *tm)
392{
393 int64_t t;
394 int64_t y = tm->tm_year;
395
396 /* See IEEE 1003.1:2004, section 4.14 */
397
398 t = (y-70)*365 + (y-69)/4 - (y-1)/100 + (y+299)/400;
399 t += tm->tm_yday;
400 t *= 24;
401 t += tm->tm_hour;
402 t *= 60;
403 t += tm->tm_min;
404 t *= 60;
405 t += tm->tm_sec;
406
407 return t;
408}
409
410static void timestamp(void)
411{
412 struct compile_time * const oct = &official_compile_time;
413 const struct tm *tp, *best_gm;
414
415 time(&oct->t);
416
417 best_gm = NULL;
418
419 tp = localtime(&oct->t);
420 if (tp) {
421 oct->local = *tp;
422 best_gm = &oct->local;
423 oct->have_local = true;
424 }
425
426 tp = gmtime(&oct->t);
427 if (tp) {
428 oct->gm = *tp;
429 best_gm = &oct->gm;
430 oct->have_gm = true;
431 if (!oct->have_local)
432 oct->local = oct->gm;
433 } else {
434 oct->gm = oct->local;
435 }
436
437 if (best_gm) {
438 oct->posix = make_posix_time(best_gm);
439 oct->have_posix = true;
440 }
441}
442
H. Peter Anvin038d8612007-04-12 16:54:50 +0000443int main(int argc, char **argv)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000444{
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700445 timestamp();
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800446
H. Peter Anvina7ecf262018-02-06 14:43:07 -0800447 iflag_set_default_cpu(&cpu);
448 iflag_set_default_cpu(&cmd_cpu);
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400449
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +0300450 include_path = strlist_alloc();
Cyrill Gorcunove3588512018-11-13 01:09:27 +0300451
Charles Crayne2581c862008-09-10 19:21:52 -0700452 pass0 = 0;
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700453 want_usage = terminate_after_phase = false;
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400454 nasm_set_verror(nasm_verror_gnu);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000455
H. Peter Anvin97e15752007-09-25 08:47:47 -0700456 error_file = stderr;
457
H. Peter Anvin13506202018-11-28 14:55:58 -0800458 nasm_ctype_init();
H. Peter Anvin274cda82016-05-10 02:56:29 -0700459 src_init();
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -0700460
H. Peter Anvin, Intel87d9e622018-06-25 12:58:49 -0700461 /*
462 * We must call init_labels() before the command line parsing,
463 * because we may be setting prefixes/suffixes from the command
464 * line.
465 */
466 init_labels();
467
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000468 offsets = raa_init();
Keith Kaniosb7a89542007-04-12 02:40:54 +0000469 forwrefs = saa_init((int32_t)sizeof(struct forwrefinfo));
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000470
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000471 preproc = &nasmpp;
Cyrill Gorcunov3ed32cb2014-06-22 20:53:12 +0400472 operating_mode = OP_NORMAL;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000473
H. Peter Anvin55568c12016-10-03 19:46:49 -0700474 parse_cmdline(argc, argv, 1);
475 if (terminate_after_phase) {
476 if (want_usage)
477 usage();
478 return 1;
479 }
480
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800481 /* At this point we have ofmt and the name of the desired debug format */
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800482 if (!using_debug_info) {
483 /* No debug info, redirect to the null backend (empty stubs) */
H. Peter Anvina7bc15d2016-02-17 20:55:08 -0800484 dfmt = &null_debug_form;
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800485 } else if (!debug_format) {
486 /* Default debug format for this backend */
H. Peter Anvina7bc15d2016-02-17 20:55:08 -0800487 dfmt = ofmt->default_dfmt;
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800488 } else {
489 dfmt = dfmt_find(ofmt, debug_format);
490 if (!dfmt) {
Cyrill Gorcunovc3527dd2018-11-24 22:17:47 +0300491 nasm_fatalf(ERR_NOFILE | ERR_USAGE,
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800492 "unrecognized debug format `%s' for"
493 " output format `%s'",
494 debug_format, ofmt->shortname);
495 }
496 }
H. Peter Anvinbb88d012003-09-10 23:34:23 +0000497
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +0300498 preproc_init(include_path);
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800499
500 parse_cmdline(argc, argv, 2);
501 if (terminate_after_phase) {
502 if (want_usage)
503 usage();
504 return 1;
505 }
506
507 /* Save away the default state of warnings */
508 memcpy(warning_state_init, warning_state, sizeof warning_state);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000509
H. Peter Anvin34754622018-11-28 12:36:53 -0800510 /* Dependency filename if we are also doing other things */
511 if (!depend_file && (operating_mode & ~OP_DEPEND)) {
512 if (outname)
513 depend_file = nasm_strcat(outname, ".d");
514 else
515 depend_file = filename_set_extension(inname, ".d");
516 }
517
Cyrill Gorcunov69bb0522018-09-22 13:46:45 +0300518 /*
519 * If no output file name provided and this
H. Peter Anvin34754622018-11-28 12:36:53 -0800520 * is preprocess mode, we're perfectly
Cyrill Gorcunovda3780d2018-09-22 14:10:36 +0300521 * fine to output into stdout.
Cyrill Gorcunov69bb0522018-09-22 13:46:45 +0300522 */
H. Peter Anvin (Intel)7b6371b2018-11-20 10:56:57 -0800523 if (!outname && !(operating_mode & OP_PREPROCESS)) {
524 outname = filename_set_extension(inname, ofmt->extension);
525 if (!strcmp(outname, inname)) {
526 outname = "nasm.out";
H. Peter Anvinc55702e2018-12-10 22:06:15 -0800527 nasm_warn("default output file same as input, using `%s' for output\n", outname);
H. Peter Anvin (Intel)7b6371b2018-11-20 10:56:57 -0800528 }
Cyrill Gorcunov69bb0522018-09-22 13:46:45 +0300529 }
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800530
H. Peter Anvinc77f5072018-11-28 12:45:06 -0800531 depend_list = (operating_mode & OP_DEPEND) ? strlist_alloc() : NULL;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700532
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700533 if (!depend_target)
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400534 depend_target = quote_for_make(outname);
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700535
H. Peter Anvin34754622018-11-28 12:36:53 -0800536 if (!(operating_mode & (OP_PREPROCESS|OP_NORMAL))) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000537 char *line;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700538
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400539 if (depend_missing_ok)
540 preproc->include_path(NULL); /* "assume generated" */
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700541
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -0700542 preproc->reset(inname, 0, depend_list);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000543 ofile = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000544 while ((line = preproc->getline()))
545 nasm_free(line);
546 preproc->cleanup(0);
Cyrill Gorcunove9fc88c2014-06-23 02:22:02 +0400547 } else if (operating_mode & OP_PREPROCESS) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000548 char *line;
H. Peter Anvin274cda82016-05-10 02:56:29 -0700549 const char *file_name = NULL;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000550 int32_t prior_linnum = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000551 int lineinc = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000552
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800553 if (outname) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -0700554 ofile = nasm_open_write(outname, NF_TEXT);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000555 if (!ofile)
Cyrill Gorcunovc3527dd2018-11-24 22:17:47 +0300556 nasm_fatalf(ERR_NOFILE,
557 "unable to open output file `%s'",
558 outname);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000559 } else
560 ofile = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000561
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700562 location.known = false;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000563
Cyrill Gorcunovb574b072011-12-05 01:56:40 +0400564 /* pass = 1; */
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -0700565 preproc->reset(inname, 3, depend_list);
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800566
567 /* Revert all warnings to the default state */
568 memcpy(warning_state, warning_state_init, sizeof warning_state);
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700569
H. Peter Anvine2c80182005-01-15 22:15:51 +0000570 while ((line = preproc->getline())) {
571 /*
572 * We generate %line directives if needed for later programs
573 */
Keith Kaniosb7a89542007-04-12 02:40:54 +0000574 int32_t linnum = prior_linnum += lineinc;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000575 int altline = src_get(&linnum, &file_name);
576 if (altline) {
577 if (altline == 1 && lineinc == 1)
578 nasm_fputs("", ofile);
579 else {
580 lineinc = (altline != -1 || lineinc != 1);
581 fprintf(ofile ? ofile : stdout,
Keith Kanios93f2e9a2007-04-14 00:10:59 +0000582 "%%line %"PRId32"+%d %s\n", linnum, lineinc,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000583 file_name);
584 }
585 prior_linnum = linnum;
586 }
587 nasm_fputs(line, ofile);
588 nasm_free(line);
589 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000590 preproc->cleanup(0);
591 if (ofile)
592 fclose(ofile);
H. Peter Anvin29695c82018-06-14 17:04:32 -0700593 if (ofile && terminate_after_phase && !keep_all)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000594 remove(outname);
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400595 ofile = NULL;
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400596 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000597
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400598 if (operating_mode & OP_NORMAL) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -0700599 ofile = nasm_open_write(outname, (ofmt->flags & OFMT_TEXT) ? NF_TEXT : NF_BINARY);
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400600 if (!ofile)
H. Peter Anvinc55702e2018-12-10 22:06:15 -0800601 nasm_fatal("unable to open output file `%s'", outname);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000602
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400603 ofmt->init();
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400604 dfmt->init();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000605
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -0700606 assemble_file(inname, depend_list);
Victor van den Elzenc82c3722008-06-04 15:24:20 +0200607
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400608 if (!terminate_after_phase) {
H. Peter Anvin477ae442016-03-07 22:53:06 -0800609 ofmt->cleanup();
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400610 cleanup_labels();
611 fflush(ofile);
H. Peter Anvinc55702e2018-12-10 22:06:15 -0800612 if (ferror(ofile))
613 nasm_nonfatal("write error on output file `%s'", outname);
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400614 }
615
616 if (ofile) {
617 fclose(ofile);
H. Peter Anvin29695c82018-06-14 17:04:32 -0700618 if (terminate_after_phase && !keep_all)
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400619 remove(outname);
620 ofile = NULL;
621 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000622 }
H. Peter Anvin734b1882002-04-30 21:01:08 +0000623
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700624 if (depend_list && !terminate_after_phase)
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400625 emit_dependencies(depend_list);
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700626
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000627 if (want_usage)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000628 usage();
H. Peter Anvin734b1882002-04-30 21:01:08 +0000629
H. Peter Anvine2c80182005-01-15 22:15:51 +0000630 raa_free(offsets);
631 saa_free(forwrefs);
632 eval_cleanup();
H. Peter Anvin74cc5e52007-08-30 22:35:34 +0000633 stdscan_cleanup();
H. Peter Anvin274cda82016-05-10 02:56:29 -0700634 src_free();
Cyrill Gorcunovfeabd742018-11-13 01:23:47 +0300635 strlist_free(include_path);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000636
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700637 return terminate_after_phase;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000638}
639
H. Peter Anvineba20a72002-04-30 20:53:55 +0000640/*
641 * Get a parameter for a command line option.
642 * First arg must be in the form of e.g. -f...
643 */
H. Peter Anvin423e3812007-11-15 17:12:29 -0800644static char *get_param(char *p, char *q, bool *advance)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000645{
H. Peter Anvin423e3812007-11-15 17:12:29 -0800646 *advance = false;
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +0400647 if (p[2]) /* the parameter's in the option */
648 return nasm_skip_spaces(p + 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000649 if (q && q[0]) {
H. Peter Anvin423e3812007-11-15 17:12:29 -0800650 *advance = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000651 return q;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000652 }
H. Peter Anvinc55702e2018-12-10 22:06:15 -0800653 nasm_nonfatalf(ERR_USAGE, "option `-%c' requires an argument", p[1]);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000654 return NULL;
655}
656
H. Peter Anvindc242712007-11-18 11:55:10 -0800657/*
658 * Copy a filename
659 */
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800660static void copy_filename(const char **dst, const char *src, const char *what)
H. Peter Anvindc242712007-11-18 11:55:10 -0800661{
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800662 if (*dst)
H. Peter Anvinc5136902018-06-15 18:20:17 -0700663 nasm_fatal("more than one %s file specified: %s\n", what, src);
H. Peter Anvindc242712007-11-18 11:55:10 -0800664
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800665 *dst = nasm_strdup(src);
H. Peter Anvindc242712007-11-18 11:55:10 -0800666}
667
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700668/*
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700669 * Convert a string to a POSIX make-safe form
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700670 */
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700671static char *quote_for_pmake(const char *str)
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700672{
673 const char *p;
674 char *os, *q;
675
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400676 size_t n = 1; /* Terminating zero */
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700677 size_t nbs = 0;
678
679 if (!str)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400680 return NULL;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700681
682 for (p = str; *p; p++) {
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400683 switch (*p) {
684 case ' ':
685 case '\t':
686 /* Convert N backslashes + ws -> 2N+1 backslashes + ws */
687 n += nbs + 2;
688 nbs = 0;
689 break;
690 case '$':
691 case '#':
692 nbs = 0;
693 n += 2;
694 break;
695 case '\\':
696 nbs++;
697 n++;
698 break;
699 default:
700 nbs = 0;
701 n++;
702 break;
703 }
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700704 }
705
706 /* Convert N backslashes at the end of filename to 2N backslashes */
707 if (nbs)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400708 n += nbs;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700709
710 os = q = nasm_malloc(n);
711
712 nbs = 0;
713 for (p = str; *p; p++) {
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400714 switch (*p) {
715 case ' ':
716 case '\t':
717 while (nbs--)
718 *q++ = '\\';
719 *q++ = '\\';
720 *q++ = *p;
721 break;
722 case '$':
723 *q++ = *p;
724 *q++ = *p;
725 nbs = 0;
726 break;
727 case '#':
728 *q++ = '\\';
729 *q++ = *p;
730 nbs = 0;
731 break;
732 case '\\':
733 *q++ = *p;
734 nbs++;
735 break;
736 default:
737 *q++ = *p;
738 nbs = 0;
739 break;
740 }
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700741 }
742 while (nbs--)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400743 *q++ = '\\';
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700744
745 *q = '\0';
746
747 return os;
748}
749
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700750/*
751 * Convert a string to a Watcom make-safe form
752 */
753static char *quote_for_wmake(const char *str)
754{
755 const char *p;
756 char *os, *q;
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700757 bool quote = false;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700758
759 size_t n = 1; /* Terminating zero */
760
761 if (!str)
762 return NULL;
763
764 for (p = str; *p; p++) {
765 switch (*p) {
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700766 case ' ':
767 case '\t':
H. Peter Anvin3e30c322017-08-16 22:20:36 -0700768 case '&':
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700769 quote = true;
770 n++;
771 break;
772 case '\"':
773 quote = true;
774 n += 2;
775 break;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700776 case '$':
777 case '#':
778 n += 2;
779 break;
780 default:
781 n++;
782 break;
783 }
784 }
785
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700786 if (quote)
787 n += 2;
788
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700789 os = q = nasm_malloc(n);
790
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700791 if (quote)
792 *q++ = '\"';
793
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700794 for (p = str; *p; p++) {
795 switch (*p) {
796 case '$':
797 case '#':
798 *q++ = '$';
799 *q++ = *p;
800 break;
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700801 case '\"':
802 *q++ = *p;
803 *q++ = *p;
804 break;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700805 default:
806 *q++ = *p;
807 break;
808 }
809 }
810
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700811 if (quote)
812 *q++ = '\"';
813
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700814 *q = '\0';
815
816 return os;
817}
818
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100819enum text_options {
H. Peter Anvin3366e312018-02-07 14:14:36 -0800820 OPT_BOGUS,
821 OPT_VERSION,
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700822 OPT_HELP,
H. Peter Anvin3366e312018-02-07 14:14:36 -0800823 OPT_ABORT_ON_PANIC,
H. Peter Anvin05990342018-06-11 13:32:42 -0700824 OPT_MANGLE,
825 OPT_INCLUDE,
826 OPT_PRAGMA,
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700827 OPT_BEFORE,
H. Peter Anvin29695c82018-06-14 17:04:32 -0700828 OPT_LIMIT,
829 OPT_KEEP_ALL
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100830};
H. Peter Anvin3366e312018-02-07 14:14:36 -0800831struct textargs {
832 const char *label;
833 enum text_options opt;
834 bool need_arg;
H. Peter Anvin98578072018-06-01 18:02:54 -0700835 int pvt;
H. Peter Anvin3366e312018-02-07 14:14:36 -0800836};
H. Peter Anvin2530a102016-02-18 02:28:15 -0800837static const struct textargs textopts[] = {
H. Peter Anvin98578072018-06-01 18:02:54 -0700838 {"v", OPT_VERSION, false, 0},
839 {"version", OPT_VERSION, false, 0},
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700840 {"help", OPT_HELP, false, 0},
H. Peter Anvin98578072018-06-01 18:02:54 -0700841 {"abort-on-panic", OPT_ABORT_ON_PANIC, false, 0},
842 {"prefix", OPT_MANGLE, true, LM_GPREFIX},
843 {"postfix", OPT_MANGLE, true, LM_GSUFFIX},
844 {"gprefix", OPT_MANGLE, true, LM_GPREFIX},
845 {"gpostfix", OPT_MANGLE, true, LM_GSUFFIX},
846 {"lprefix", OPT_MANGLE, true, LM_LPREFIX},
847 {"lpostfix", OPT_MANGLE, true, LM_LSUFFIX},
H. Peter Anvin05990342018-06-11 13:32:42 -0700848 {"include", OPT_INCLUDE, true, 0},
849 {"pragma", OPT_PRAGMA, true, 0},
850 {"before", OPT_BEFORE, true, 0},
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700851 {"limit-", OPT_LIMIT, true, 0},
H. Peter Anvin29695c82018-06-14 17:04:32 -0700852 {"keep-all", OPT_KEEP_ALL, false, 0},
H. Peter Anvin98578072018-06-01 18:02:54 -0700853 {NULL, OPT_BOGUS, false, 0}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000854};
855
Cyrill Gorcunove7438432014-05-09 22:34:34 +0400856static void show_version(void)
857{
858 printf("NASM version %s compiled on %s%s\n",
859 nasm_version, nasm_date, nasm_compile_options);
860 exit(0);
861}
862
H. Peter Anvin423e3812007-11-15 17:12:29 -0800863static bool stopoptions = false;
H. Peter Anvin55568c12016-10-03 19:46:49 -0700864static bool process_arg(char *p, char *q, int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000865{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000866 char *param;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800867 bool advance = false;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000868
869 if (!p || !p[0])
H. Peter Anvin423e3812007-11-15 17:12:29 -0800870 return false;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000871
H. Peter Anvine2c80182005-01-15 22:15:51 +0000872 if (p[0] == '-' && !stopoptions) {
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400873 if (strchr("oOfpPdDiIlFXuUZwW", p[1])) {
874 /* These parameters take values */
875 if (!(param = get_param(p, q, &advance)))
876 return advance;
877 }
H. Peter Anvin423e3812007-11-15 17:12:29 -0800878
H. Peter Anvine2c80182005-01-15 22:15:51 +0000879 switch (p[1]) {
880 case 's':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700881 if (pass == 1)
882 error_file = stdout;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000883 break;
H. Peter Anvin70653092007-10-19 14:42:29 -0700884
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400885 case 'o': /* output file */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700886 if (pass == 2)
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800887 copy_filename(&outname, param, "output");
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400888 break;
H. Peter Anvinfd7dd112007-10-10 14:06:59 -0700889
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400890 case 'f': /* output format */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700891 if (pass == 1) {
892 ofmt = ofmt_find(param, &ofmt_alias);
893 if (!ofmt) {
Cyrill Gorcunovc3527dd2018-11-24 22:17:47 +0300894 nasm_fatalf(ERR_NOFILE | ERR_USAGE,
H. Peter Anvin55568c12016-10-03 19:46:49 -0700895 "unrecognised output format `%s' - "
896 "use -hf for a list", param);
897 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400898 }
899 break;
H. Peter Anvin70653092007-10-19 14:42:29 -0700900
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400901 case 'O': /* Optimization level */
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800902 if (pass == 1) {
H. Peter Anvin55568c12016-10-03 19:46:49 -0700903 int opt;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700904
H. Peter Anvin55568c12016-10-03 19:46:49 -0700905 if (!*param) {
906 /* Naked -O == -Ox */
Chang S. Baea5786342018-08-15 23:22:21 +0300907 optimizing.level = MAX_OPTIMIZE;
H. Peter Anvin55568c12016-10-03 19:46:49 -0700908 } else {
909 while (*param) {
910 switch (*param) {
911 case '0': case '1': case '2': case '3': case '4':
912 case '5': case '6': case '7': case '8': case '9':
913 opt = strtoul(param, &param, 10);
H. Peter Anvind85d2502008-05-04 17:53:31 -0700914
Chang S. Baea5786342018-08-15 23:22:21 +0300915 /* -O0 -> optimizing.level == -1, 0.98 behaviour */
916 /* -O1 -> optimizing.level == 0, 0.98.09 behaviour */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700917 if (opt < 2)
Chang S. Baea5786342018-08-15 23:22:21 +0300918 optimizing.level = opt - 1;
H. Peter Anvin55568c12016-10-03 19:46:49 -0700919 else
Chang S. Baea5786342018-08-15 23:22:21 +0300920 optimizing.level = opt;
H. Peter Anvin55568c12016-10-03 19:46:49 -0700921 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700922
H. Peter Anvin55568c12016-10-03 19:46:49 -0700923 case 'v':
924 case '+':
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400925 param++;
926 opt_verbose_info = true;
927 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700928
H. Peter Anvin55568c12016-10-03 19:46:49 -0700929 case 'x':
930 param++;
Chang S. Baea5786342018-08-15 23:22:21 +0300931 optimizing.level = MAX_OPTIMIZE;
H. Peter Anvin55568c12016-10-03 19:46:49 -0700932 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700933
H. Peter Anvin55568c12016-10-03 19:46:49 -0700934 default:
H. Peter Anvinc5136902018-06-15 18:20:17 -0700935 nasm_fatal("unknown optimization option -O%c\n",
H. Peter Anvin55568c12016-10-03 19:46:49 -0700936 *param);
937 break;
938 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400939 }
Chang S. Baea5786342018-08-15 23:22:21 +0300940 if (optimizing.level > MAX_OPTIMIZE)
941 optimizing.level = MAX_OPTIMIZE;
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400942 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400943 }
944 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800945
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400946 case 'p': /* pre-include */
947 case 'P':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700948 if (pass == 2)
949 preproc->pre_include(param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400950 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800951
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400952 case 'd': /* pre-define */
953 case 'D':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700954 if (pass == 2)
955 preproc->pre_define(param);
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 'u': /* un-define */
959 case 'U':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700960 if (pass == 2)
961 preproc->pre_undefine(param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400962 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800963
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400964 case 'i': /* include search path */
965 case 'I':
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800966 if (pass == 1)
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +0300967 strlist_add(include_path, param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400968 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800969
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400970 case 'l': /* listing file */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700971 if (pass == 2)
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800972 copy_filename(&listname, param, "listing");
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400973 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800974
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400975 case 'Z': /* error messages file */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700976 if (pass == 1)
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800977 copy_filename(&errname, param, "error");
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400978 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800979
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400980 case 'F': /* specify debug format */
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800981 if (pass == 1) {
H. Peter Anvin55568c12016-10-03 19:46:49 -0700982 using_debug_info = true;
983 debug_format = param;
984 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400985 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800986
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400987 case 'X': /* specify error reporting format */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700988 if (pass == 1) {
989 if (nasm_stricmp("vc", param) == 0)
990 nasm_set_verror(nasm_verror_vc);
991 else if (nasm_stricmp("gnu", param) == 0)
992 nasm_set_verror(nasm_verror_gnu);
993 else
Cyrill Gorcunovc3527dd2018-11-24 22:17:47 +0300994 nasm_fatalf(ERR_NOFILE | ERR_USAGE,
995 "unrecognized error reporting format `%s'",
996 param);
H. Peter Anvin55568c12016-10-03 19:46:49 -0700997 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000998 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800999
H. Peter Anvine2c80182005-01-15 22:15:51 +00001000 case 'g':
H. Peter Anvinbf6230b2018-11-11 13:25:16 -08001001 if (pass == 1) {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001002 using_debug_info = true;
1003 if (p[2])
1004 debug_format = nasm_skip_spaces(p + 2);
1005 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001006 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001007
H. Peter Anvine2c80182005-01-15 22:15:51 +00001008 case 'h':
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001009 help(p[2]);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001010 exit(0); /* never need usage message here */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001011 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001012
H. Peter Anvine2c80182005-01-15 22:15:51 +00001013 case 'y':
1014 printf("\nvalid debug formats for '%s' output format are"
1015 " ('*' denotes default):\n", ofmt->shortname);
1016 dfmt_list(ofmt, stdout);
1017 exit(0);
1018 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001019
H. Peter Anvine2c80182005-01-15 22:15:51 +00001020 case 't':
H. Peter Anvin13506202018-11-28 14:55:58 -08001021 if (pass == 2) {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001022 tasm_compatible_mode = true;
H. Peter Anvin13506202018-11-28 14:55:58 -08001023 nasm_ctype_tasm_mode();
1024 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001025 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001026
H. Peter Anvine2c80182005-01-15 22:15:51 +00001027 case 'v':
Cyrill Gorcunove7438432014-05-09 22:34:34 +04001028 show_version();
H. Peter Anvine2c80182005-01-15 22:15:51 +00001029 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001030
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001031 case 'e': /* preprocess only */
1032 case 'E':
H. Peter Anvin55568c12016-10-03 19:46:49 -07001033 if (pass == 1)
1034 operating_mode = OP_PREPROCESS;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001035 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001036
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001037 case 'a': /* assemble only - don't preprocess */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001038 if (pass == 1)
1039 preproc = &preproc_nop;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001040 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001041
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001042 case 'w':
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001043 case 'W':
H. Peter Anvin55568c12016-10-03 19:46:49 -07001044 if (pass == 2) {
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001045 if (!set_warning_status(param)) {
H. Peter Anvinc55702e2018-12-10 22:06:15 -08001046 nasm_warnf(ERR_WARN_UNK_WARNING, "unknown warning option: %s", param);
H. Peter Anvin55568c12016-10-03 19:46:49 -07001047 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001048 }
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001049 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001050
H. Peter Anvine2c80182005-01-15 22:15:51 +00001051 case 'M':
H. Peter Anvin77c9bf62017-08-16 21:14:33 -07001052 if (pass == 1) {
1053 switch (p[2]) {
1054 case 'W':
1055 quote_for_make = quote_for_wmake;
1056 break;
1057 case 'D':
1058 case 'F':
1059 case 'T':
1060 case 'Q':
1061 advance = true;
1062 break;
1063 default:
1064 break;
1065 }
1066 } else {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001067 switch (p[2]) {
1068 case 0:
1069 operating_mode = OP_DEPEND;
1070 break;
1071 case 'G':
1072 operating_mode = OP_DEPEND;
1073 depend_missing_ok = true;
1074 break;
1075 case 'P':
1076 depend_emit_phony = true;
1077 break;
1078 case 'D':
H. Peter Anvin34754622018-11-28 12:36:53 -08001079 operating_mode |= OP_DEPEND;
1080 if (q && (q[0] != '-' || q[1] == '\0')) {
1081 depend_file = q;
1082 advance = true;
1083 }
H. Peter Anvin55568c12016-10-03 19:46:49 -07001084 break;
1085 case 'F':
1086 depend_file = q;
1087 advance = true;
1088 break;
1089 case 'T':
1090 depend_target = q;
1091 advance = true;
1092 break;
1093 case 'Q':
1094 depend_target = quote_for_make(q);
1095 advance = true;
1096 break;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -07001097 case 'W':
1098 /* handled in pass 1 */
1099 break;
H. Peter Anvin55568c12016-10-03 19:46:49 -07001100 default:
H. Peter Anvinc55702e2018-12-10 22:06:15 -08001101 nasm_nonfatalf(ERR_USAGE, "unknown dependency option `-M%c'", p[2]);
H. Peter Anvin55568c12016-10-03 19:46:49 -07001102 break;
1103 }
H. Peter Anvin77c9bf62017-08-16 21:14:33 -07001104 }
1105 if (advance && (!q || !q[0])) {
H. Peter Anvinc55702e2018-12-10 22:06:15 -08001106 nasm_nonfatalf(ERR_USAGE, "option `-M%c' requires a parameter", p[2]);
H. Peter Anvin77c9bf62017-08-16 21:14:33 -07001107 break;
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001108 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001109 break;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001110
H. Peter Anvine2c80182005-01-15 22:15:51 +00001111 case '-':
1112 {
H. Peter Anvin3366e312018-02-07 14:14:36 -08001113 const struct textargs *tx;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001114 size_t olen, plen;
H. Peter Anvinc805fd72018-06-12 14:23:05 -07001115 char *eqsave;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001116
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001117 p += 2;
1118
1119 if (!*p) { /* -- => stop processing options */
H. Peter Anvin3366e312018-02-07 14:14:36 -08001120 stopoptions = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001121 break;
1122 }
Cyrill Gorcunove7438432014-05-09 22:34:34 +04001123
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001124 plen = strlen(p);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001125 for (tx = textopts; tx->label; tx++) {
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001126 olen = strlen(tx->label);
1127
1128 if (olen > plen)
1129 continue;
1130
1131 if (nasm_memicmp(p, tx->label, olen))
1132 continue;
1133
1134 if (tx->label[olen-1] == '-')
1135 break; /* Incomplete option */
1136
1137 if (!p[olen] || p[olen] == '=')
1138 break; /* Complete option */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001139 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001140
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001141 if (!tx->label) {
H. Peter Anvinc55702e2018-12-10 22:06:15 -08001142 nasm_nonfatalf(ERR_USAGE, "unrecognized option `--%s'", p);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001143 }
1144
H. Peter Anvinc805fd72018-06-12 14:23:05 -07001145 eqsave = param = strchr(p+olen, '=');
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001146 if (param)
1147 *param++ = '\0';
1148
H. Peter Anvin3366e312018-02-07 14:14:36 -08001149 if (tx->need_arg) {
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001150 if (!param) {
1151 param = q;
1152 advance = true;
1153 }
1154
1155 /* Note: a null string is a valid parameter */
1156 if (!param) {
H. Peter Anvinc55702e2018-12-10 22:06:15 -08001157 nasm_nonfatalf(ERR_USAGE, "option `--%s' requires an argument", p);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001158 break;
1159 }
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001160 } else {
1161 if (param) {
H. Peter Anvinc55702e2018-12-10 22:06:15 -08001162 nasm_nonfatalf(ERR_USAGE, "option `--%s' does not take an argument", p);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001163 }
H. Peter Anvin3366e312018-02-07 14:14:36 -08001164 }
1165
1166 switch (tx->opt) {
1167 case OPT_VERSION:
1168 show_version();
1169 break;
1170 case OPT_ABORT_ON_PANIC:
1171 abort_on_panic = true;
1172 break;
H. Peter Anvin98578072018-06-01 18:02:54 -07001173 case OPT_MANGLE:
H. Peter Anvin3366e312018-02-07 14:14:36 -08001174 if (pass == 2)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001175 set_label_mangle(tx->pvt, param);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001176 break;
H. Peter Anvin05990342018-06-11 13:32:42 -07001177 case OPT_INCLUDE:
1178 if (pass == 2)
1179 preproc->pre_include(q);
1180 break;
1181 case OPT_PRAGMA:
1182 if (pass == 2)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001183 preproc->pre_command("pragma", param);
H. Peter Anvin05990342018-06-11 13:32:42 -07001184 break;
1185 case OPT_BEFORE:
1186 if (pass == 2)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001187 preproc->pre_command(NULL, param);
H. Peter Anvin05990342018-06-11 13:32:42 -07001188 break;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001189 case OPT_LIMIT:
H. Peter Anvinbf6230b2018-11-11 13:25:16 -08001190 if (pass == 1)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001191 nasm_set_limit(p+olen, param);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001192 break;
H. Peter Anvin29695c82018-06-14 17:04:32 -07001193 case OPT_KEEP_ALL:
1194 keep_all = true;
1195 break;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001196 case OPT_HELP:
1197 help(0);
1198 exit(0);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001199 default:
1200 panic();
H. Peter Anvine2c80182005-01-15 22:15:51 +00001201 }
H. Peter Anvinc805fd72018-06-12 14:23:05 -07001202
1203 if (eqsave)
1204 *eqsave = '='; /* Restore = argument separator */
1205
H. Peter Anvine2c80182005-01-15 22:15:51 +00001206 break;
1207 }
1208
1209 default:
H. Peter Anvinc55702e2018-12-10 22:06:15 -08001210 nasm_nonfatalf(ERR_USAGE, "unrecognised option `-%c'", p[1]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001211 break;
1212 }
H. Peter Anvin55568c12016-10-03 19:46:49 -07001213 } else if (pass == 2) {
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001214 /* In theory we could allow multiple input files... */
1215 copy_filename(&inname, p, "input");
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001216 }
1217
1218 return advance;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001219}
1220
H. Peter Anvineba20a72002-04-30 20:53:55 +00001221#define ARG_BUF_DELTA 128
1222
H. Peter Anvin55568c12016-10-03 19:46:49 -07001223static void process_respfile(FILE * rfile, int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001224{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001225 char *buffer, *p, *q, *prevarg;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001226 int bufsize, prevargsize;
1227
1228 bufsize = prevargsize = ARG_BUF_DELTA;
1229 buffer = nasm_malloc(ARG_BUF_DELTA);
1230 prevarg = nasm_malloc(ARG_BUF_DELTA);
1231 prevarg[0] = '\0';
1232
H. Peter Anvine2c80182005-01-15 22:15:51 +00001233 while (1) { /* Loop to handle all lines in file */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001234 p = buffer;
1235 while (1) { /* Loop to handle long lines */
1236 q = fgets(p, bufsize - (p - buffer), rfile);
1237 if (!q)
1238 break;
1239 p += strlen(p);
1240 if (p > buffer && p[-1] == '\n')
1241 break;
1242 if (p - buffer > bufsize - 10) {
1243 int offset;
1244 offset = p - buffer;
1245 bufsize += ARG_BUF_DELTA;
1246 buffer = nasm_realloc(buffer, bufsize);
1247 p = buffer + offset;
1248 }
1249 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001250
H. Peter Anvine2c80182005-01-15 22:15:51 +00001251 if (!q && p == buffer) {
1252 if (prevarg[0])
H. Peter Anvin55568c12016-10-03 19:46:49 -07001253 process_arg(prevarg, NULL, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001254 nasm_free(buffer);
1255 nasm_free(prevarg);
1256 return;
1257 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001258
H. Peter Anvine2c80182005-01-15 22:15:51 +00001259 /*
1260 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
1261 * them are present at the end of the line.
1262 */
1263 *(p = &buffer[strcspn(buffer, "\r\n\032")]) = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001264
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001265 while (p > buffer && nasm_isspace(p[-1]))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001266 *--p = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001267
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +04001268 p = nasm_skip_spaces(buffer);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001269
H. Peter Anvin55568c12016-10-03 19:46:49 -07001270 if (process_arg(prevarg, p, pass))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001271 *p = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001272
Charles Crayne192d5b52007-10-18 19:02:42 -07001273 if ((int) strlen(p) > prevargsize - 10) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001274 prevargsize += ARG_BUF_DELTA;
1275 prevarg = nasm_realloc(prevarg, prevargsize);
1276 }
H. Peter Anvindc242712007-11-18 11:55:10 -08001277 strncpy(prevarg, p, prevargsize);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001278 }
1279}
1280
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001281/* Function to process args from a string of args, rather than the
1282 * argv array. Used by the environment variable and response file
1283 * processing.
1284 */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001285static void process_args(char *args, int pass)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001286{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001287 char *p, *q, *arg, *prevarg;
1288 char separator = ' ';
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001289
1290 p = args;
1291 if (*p && *p != '-')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001292 separator = *p++;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001293 arg = NULL;
1294 while (*p) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001295 q = p;
1296 while (*p && *p != separator)
1297 p++;
1298 while (*p == separator)
1299 *p++ = '\0';
1300 prevarg = arg;
1301 arg = q;
H. Peter Anvin55568c12016-10-03 19:46:49 -07001302 if (process_arg(prevarg, arg, pass))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001303 arg = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001304 }
1305 if (arg)
H. Peter Anvin55568c12016-10-03 19:46:49 -07001306 process_arg(arg, NULL, pass);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001307}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001308
H. Peter Anvin55568c12016-10-03 19:46:49 -07001309static void process_response_file(const char *file, int pass)
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001310{
1311 char str[2048];
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001312 FILE *f = nasm_open_read(file, NF_TEXT);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001313 if (!f) {
Cyrill Gorcunovf1964512013-02-15 12:14:06 +04001314 perror(file);
1315 exit(-1);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001316 }
1317 while (fgets(str, sizeof str, f)) {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001318 process_args(str, pass);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001319 }
1320 fclose(f);
1321}
1322
H. Peter Anvin55568c12016-10-03 19:46:49 -07001323static void parse_cmdline(int argc, char **argv, int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001324{
1325 FILE *rfile;
Cyrill Gorcunovf4941892011-07-17 13:55:25 +04001326 char *envreal, *envcopy = NULL, *p;
H. Peter Anvin972079f2008-09-30 17:01:23 -07001327 int i;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001328
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001329 /* Initialize all the warnings to their default state */
1330 for (i = 0; i < ERR_WARN_ALL; i++) {
1331 warning_state_init[i] = warning_state[i] =
1332 warnings[i].enabled ? WARN_ST_ENABLED : 0;
1333 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001334
1335 /*
H. Peter Anvinff7ccc02002-05-06 19:41:57 +00001336 * First, process the NASMENV environment variable.
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001337 */
H. Peter Anvinff7ccc02002-05-06 19:41:57 +00001338 envreal = getenv("NASMENV");
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001339 if (envreal) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001340 envcopy = nasm_strdup(envreal);
H. Peter Anvin55568c12016-10-03 19:46:49 -07001341 process_args(envcopy, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001342 nasm_free(envcopy);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001343 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001344
1345 /*
1346 * Now process the actual command line.
1347 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001348 while (--argc) {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001349 bool advance;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001350 argv++;
1351 if (argv[0][0] == '@') {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001352 /*
1353 * We have a response file, so process this as a set of
H. Peter Anvine2c80182005-01-15 22:15:51 +00001354 * arguments like the environment variable. This allows us
1355 * to have multiple arguments on a single line, which is
1356 * different to the -@resp file processing below for regular
1357 * NASM.
1358 */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001359 process_response_file(argv[0]+1, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001360 argc--;
1361 argv++;
1362 }
1363 if (!stopoptions && argv[0][0] == '-' && argv[0][1] == '@') {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001364 p = get_param(argv[0], argc > 1 ? argv[1] : NULL, &advance);
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001365 if (p) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001366 rfile = nasm_open_read(p, NF_TEXT);
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001367 if (rfile) {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001368 process_respfile(rfile, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001369 fclose(rfile);
H. Peter Anvinc55702e2018-12-10 22:06:15 -08001370 } else {
1371 nasm_nonfatalf(ERR_USAGE, "unable to open response file `%s'", p);
1372 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001373 }
1374 } else
H. Peter Anvin55568c12016-10-03 19:46:49 -07001375 advance = process_arg(argv[0], argc > 1 ? argv[1] : NULL, pass);
H. Peter Anvin423e3812007-11-15 17:12:29 -08001376 argv += advance, argc -= advance;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001377 }
1378
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001379 /*
1380 * Look for basic command line typos. This definitely doesn't
1381 * catch all errors, but it might help cases of fumbled fingers.
1382 */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001383 if (pass != 2)
1384 return;
1385
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001386 if (!inname)
Cyrill Gorcunovc3527dd2018-11-24 22:17:47 +03001387 nasm_fatalf(ERR_NOFILE | ERR_USAGE, "no input file specified");
H. Peter Anvin59ddd262007-10-01 11:26:31 -07001388
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001389 else if ((errname && !strcmp(inname, errname)) ||
1390 (outname && !strcmp(inname, outname)) ||
1391 (listname && !strcmp(inname, listname)) ||
1392 (depend_file && !strcmp(inname, depend_file)))
Cyrill Gorcunovc3527dd2018-11-24 22:17:47 +03001393 nasm_fatalf(ERR_USAGE, "will not overwrite input file");
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001394
1395 if (errname) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001396 error_file = nasm_open_write(errname, NF_TEXT);
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001397 if (!error_file) {
1398 error_file = stderr; /* Revert to default! */
Cyrill Gorcunovc3527dd2018-11-24 22:17:47 +03001399 nasm_fatalf(ERR_NOFILE | ERR_USAGE,
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001400 "cannot open file `%s' for error messages",
1401 errname);
1402 }
Charles Craynefcce07f2007-09-30 22:15:36 -07001403 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001404}
1405
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03001406static void assemble_file(const char *fname, struct strlist *depend_list)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001407{
H. Peter Anvinc7131682017-03-07 17:45:01 -08001408 char *line;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001409 insn output_ins;
H. Peter Anvinc7131682017-03-07 17:45:01 -08001410 int i;
H. Peter Anvin9c595b62017-03-07 19:44:21 -08001411 uint64_t prev_offset_changed;
H. Peter Anvina3d96d02018-06-15 17:51:39 -07001412 int64_t stall_count = 0; /* Make sure we make forward progress... */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001413
H. Peter Anvina7ecf262018-02-06 14:43:07 -08001414 switch (cmd_sb) {
1415 case 16:
1416 break;
1417 case 32:
1418 if (!iflag_cpu_level_ok(&cmd_cpu, IF_386))
H. Peter Anvinc5136902018-06-15 18:20:17 -07001419 nasm_fatal("command line: 32-bit segment size requires a higher cpu");
H. Peter Anvina7ecf262018-02-06 14:43:07 -08001420 break;
1421 case 64:
1422 if (!iflag_cpu_level_ok(&cmd_cpu, IF_X86_64))
H. Peter Anvinc5136902018-06-15 18:20:17 -07001423 nasm_fatal("command line: 64-bit segment size requires a higher cpu");
H. Peter Anvina7ecf262018-02-06 14:43:07 -08001424 break;
1425 default:
1426 panic();
1427 break;
1428 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001429
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001430 prev_offset_changed = nasm_limit[LIMIT_PASSES];
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001431 for (passn = 1; pass0 <= 2; passn++) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001432 pass1 = pass0 == 2 ? 2 : 1; /* 1, 1, 1, ..., 1, 2 */
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001433 pass2 = passn > 1 ? 2 : 1; /* 1, 2, 2, ..., 2, 2 */
1434 /* pass0 0, 0, 0, ..., 1, 2 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00001435
H. Peter Anvincac0b192017-03-28 16:12:30 -07001436 globalbits = cmd_sb; /* set 'bits' to command line default */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001437 cpu = cmd_cpu;
1438 if (pass0 == 2) {
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08001439 lfmt->init(listname);
H. Peter Anvin29695c82018-06-14 17:04:32 -07001440 } else if (passn == 1 && listname && !keep_all) {
H. Peter Anvinaac01ff2017-04-02 19:10:26 -07001441 /* Remove the list file in case we die before the output pass */
1442 remove(listname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001443 }
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001444 in_absolute = false;
Charles Craynec1905c22008-09-11 18:54:06 -07001445 global_offset_changed = 0; /* set by redefine_label */
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001446 if (passn > 1) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001447 saa_rewind(forwrefs);
1448 forwref = saa_rstruct(forwrefs);
1449 raa_free(offsets);
1450 offsets = raa_init();
1451 }
H. Peter Anvin892c4812018-05-30 14:43:46 -07001452 location.segment = NO_SEG;
1453 location.offset = 0;
1454 if (passn == 1)
1455 location.known = true;
1456 ofmt->reset();
1457 switch_segment(ofmt->section(NULL, pass2, &globalbits));
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001458 preproc->reset(fname, pass1, pass1 == 2 ? depend_list : NULL);
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001459
1460 /* Revert all warnings to the default state */
1461 memcpy(warning_state, warning_state_init, sizeof warning_state);
Victor van den Elzen819703a2008-07-16 15:20:56 +02001462
H. Peter Anvine2c80182005-01-15 22:15:51 +00001463 globallineno = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001464
H. Peter Anvine2c80182005-01-15 22:15:51 +00001465 while ((line = preproc->getline())) {
H. Peter Anvina3d96d02018-06-15 17:51:39 -07001466 if (++globallineno > nasm_limit[LIMIT_LINES])
H. Peter Anvinc5136902018-06-15 18:20:17 -07001467 nasm_fatal("overall line count exceeds the maximum %"PRId64"\n",
Cyrill Gorcunovf7b44f62018-10-15 22:58:13 +03001468 nasm_limit[LIMIT_LINES]);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001469
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001470 /*
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001471 * Here we parse our directives; this is not handled by the
H. Peter Anvinc7131682017-03-07 17:45:01 -08001472 * main parser.
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001473 */
H. Peter Anvina6e26d92017-03-07 21:32:37 -08001474 if (process_directives(line))
H. Peter Anvinc7131682017-03-07 17:45:01 -08001475 goto end_of_line; /* Just do final cleanup */
H. Peter Anvin62b24d72007-08-29 16:38:05 +00001476
H. Peter Anvinc7131682017-03-07 17:45:01 -08001477 /* Not a directive, or even something that starts with [ */
H. Peter Anvin98578072018-06-01 18:02:54 -07001478 parse_line(pass1, line, &output_ins);
H. Peter Anvinc7131682017-03-07 17:45:01 -08001479
Chang S. Baea5786342018-08-15 23:22:21 +03001480 if (optimizing.level > 0) {
H. Peter Anvinc7131682017-03-07 17:45:01 -08001481 if (forwref != NULL && globallineno == forwref->lineno) {
1482 output_ins.forw_ref = true;
1483 do {
1484 output_ins.oprs[forwref->operand].opflags |= OPFLAG_FORWARD;
1485 forwref = saa_rstruct(forwrefs);
1486 } while (forwref != NULL
1487 && forwref->lineno == globallineno);
1488 } else
1489 output_ins.forw_ref = false;
1490
1491 if (output_ins.forw_ref) {
1492 if (passn == 1) {
1493 for (i = 0; i < output_ins.operands; i++) {
1494 if (output_ins.oprs[i].opflags & OPFLAG_FORWARD) {
1495 struct forwrefinfo *fwinf = (struct forwrefinfo *)saa_wstruct(forwrefs);
1496 fwinf->lineno = globallineno;
1497 fwinf->operand = i;
Cyrill Gorcunovd5f2aef2010-04-20 15:33:45 +04001498 }
1499 }
1500 }
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001501 }
H. Peter Anvinc7131682017-03-07 17:45:01 -08001502 }
H. Peter Anvin8bec2c72009-08-08 13:49:00 -07001503
H. Peter Anvinc7131682017-03-07 17:45:01 -08001504 /* forw_ref */
1505 if (output_ins.opcode == I_EQU) {
Cyrill Gorcunove996d282018-10-13 16:18:16 +03001506 if (!output_ins.label) {
H. Peter Anvinc55702e2018-12-10 22:06:15 -08001507 nasm_nonfatal("EQU not preceded by label");
Cyrill Gorcunove996d282018-10-13 16:18:16 +03001508 } else if (output_ins.operands == 1 &&
1509 (output_ins.oprs[0].type & IMMEDIATE) &&
1510 output_ins.oprs[0].wrt == NO_SEG) {
H. Peter Anvin98578072018-06-01 18:02:54 -07001511 define_label(output_ins.label,
1512 output_ins.oprs[0].segment,
1513 output_ins.oprs[0].offset, false);
1514 } else if (output_ins.operands == 2
1515 && (output_ins.oprs[0].type & IMMEDIATE)
1516 && (output_ins.oprs[0].type & COLON)
1517 && output_ins.oprs[0].segment == NO_SEG
1518 && output_ins.oprs[0].wrt == NO_SEG
1519 && (output_ins.oprs[1].type & IMMEDIATE)
1520 && output_ins.oprs[1].segment == NO_SEG
1521 && output_ins.oprs[1].wrt == NO_SEG) {
1522 define_label(output_ins.label,
1523 output_ins.oprs[0].offset | SEG_ABS,
1524 output_ins.oprs[1].offset, false);
H. Peter Anvinc7131682017-03-07 17:45:01 -08001525 } else {
H. Peter Anvinc55702e2018-12-10 22:06:15 -08001526 nasm_nonfatal("bad syntax for EQU");
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001527 }
H. Peter Anvinc7131682017-03-07 17:45:01 -08001528 } else { /* instruction isn't an EQU */
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001529 int32_t n;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001530
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001531 nasm_assert(output_ins.times >= 0);
H. Peter Anvinc7131682017-03-07 17:45:01 -08001532
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001533 for (n = 1; n <= output_ins.times; n++) {
1534 if (pass1 == 1) {
H. Peter Anvin892c4812018-05-30 14:43:46 -07001535 int64_t l = insn_size(location.segment,
1536 location.offset,
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001537 globalbits, &output_ins);
1538
1539 /* if (using_debug_info) && output_ins.opcode != -1) */
1540 if (using_debug_info)
1541 { /* fbk 03/25/01 */
H. Peter Anvinc7131682017-03-07 17:45:01 -08001542 /* this is done here so we can do debug type info */
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001543 int32_t typeinfo =
1544 TYS_ELEMENTS(output_ins.operands);
1545 switch (output_ins.opcode) {
1546 case I_RESB:
1547 typeinfo =
1548 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_BYTE;
1549 break;
1550 case I_RESW:
1551 typeinfo =
1552 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_WORD;
1553 break;
1554 case I_RESD:
1555 typeinfo =
1556 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_DWORD;
1557 break;
1558 case I_RESQ:
1559 typeinfo =
1560 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_QWORD;
1561 break;
1562 case I_REST:
1563 typeinfo =
1564 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_TBYTE;
1565 break;
1566 case I_RESO:
1567 typeinfo =
1568 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_OWORD;
1569 break;
1570 case I_RESY:
1571 typeinfo =
1572 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_YWORD;
1573 break;
1574 case I_RESZ:
1575 typeinfo =
1576 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_ZWORD;
1577 break;
1578 case I_DB:
1579 typeinfo |= TY_BYTE;
1580 break;
1581 case I_DW:
1582 typeinfo |= TY_WORD;
1583 break;
1584 case I_DD:
1585 if (output_ins.eops_float)
1586 typeinfo |= TY_FLOAT;
1587 else
1588 typeinfo |= TY_DWORD;
1589 break;
1590 case I_DQ:
1591 typeinfo |= TY_QWORD;
1592 break;
1593 case I_DT:
1594 typeinfo |= TY_TBYTE;
1595 break;
1596 case I_DO:
1597 typeinfo |= TY_OWORD;
1598 break;
1599 case I_DY:
1600 typeinfo |= TY_YWORD;
1601 break;
1602 case I_DZ:
1603 typeinfo |= TY_ZWORD;
1604 break;
1605 default:
1606 typeinfo = TY_LABEL;
1607 break;
1608 }
H. Peter Anvinc7131682017-03-07 17:45:01 -08001609
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001610 dfmt->debug_typevalue(typeinfo);
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001611 }
H. Peter Anvinc7131682017-03-07 17:45:01 -08001612
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001613 /*
1614 * For INCBIN, let the code in assemble
1615 * handle TIMES, so we don't have to read the
1616 * input file over and over.
1617 */
1618 if (l != -1) {
H. Peter Anvin892c4812018-05-30 14:43:46 -07001619 increment_offset(l);
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001620 }
1621 /*
1622 * else l == -1 => invalid instruction, which will be
1623 * flagged as an error on pass 2
1624 */
1625 } else {
1626 if (n == 2)
1627 lfmt->uplevel(LIST_TIMES);
H. Peter Anvin892c4812018-05-30 14:43:46 -07001628 increment_offset(assemble(location.segment,
1629 location.offset,
1630 globalbits, &output_ins));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001631 }
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001632 } /* not an EQU */
1633 }
1634 if (output_ins.times > 1)
1635 lfmt->downlevel(LIST_TIMES);
H. Peter Anvinc7131682017-03-07 17:45:01 -08001636
H. Peter Anvinc7131682017-03-07 17:45:01 -08001637 cleanup_insn(&output_ins);
1638
1639 end_of_line:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001640 nasm_free(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001641 } /* end while (line = preproc->getline... */
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001642
H. Peter Anvin (Intel)b45c03a2018-06-27 21:03:38 -07001643 if (global_offset_changed && !terminate_after_phase) {
1644 switch (pass0) {
1645 case 1:
H. Peter Anvinc55702e2018-12-10 22:06:15 -08001646 nasm_warnf(ERR_WARN_PHASE, "phase error during stabilization pass, hoping for the best");
H. Peter Anvin (Intel)b45c03a2018-06-27 21:03:38 -07001647 break;
1648
1649 case 2:
H. Peter Anvinc55702e2018-12-10 22:06:15 -08001650 nasm_nonfatal("phase error during code generation pass");
H. Peter Anvin (Intel)b45c03a2018-06-27 21:03:38 -07001651 break;
1652
1653 default:
1654 /* This is normal, we'll keep going... */
1655 break;
1656 }
1657 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001658
H. Peter Anvine2c80182005-01-15 22:15:51 +00001659 if (pass1 == 1)
1660 preproc->cleanup(1);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001661
H. Peter Anvin (Intel)12810fa2018-06-27 20:17:33 -07001662 /*
1663 * Always run at least two optimization passes (pass0 == 0);
1664 * things like subsections will fail miserably without that.
1665 * Once we commit to a stabilization pass (pass0 == 1), we can't
1666 * go back, and if something goes bad, we can only hope
1667 * that we don't end up with a phase error at the end.
1668 */
1669 if ((passn > 1 && !global_offset_changed) || pass0 > 0) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001670 pass0++;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001671 } else if (global_offset_changed &&
1672 global_offset_changed < prev_offset_changed) {
Charles Craynec1905c22008-09-11 18:54:06 -07001673 prev_offset_changed = global_offset_changed;
1674 stall_count = 0;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001675 } else {
1676 stall_count++;
1677 }
Victor van den Elzen42528232008-09-11 15:07:05 +02001678
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001679 if (terminate_after_phase)
1680 break;
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001681
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001682 if ((stall_count > nasm_limit[LIMIT_STALLED]) ||
1683 (passn >= nasm_limit[LIMIT_PASSES])) {
Victor van den Elzen42528232008-09-11 15:07:05 +02001684 /* We get here if the labels don't converge
1685 * Example: FOO equ FOO + 1
1686 */
H. Peter Anvinc55702e2018-12-10 22:06:15 -08001687 nasm_nonfatal("Can't find valid values for all labels "
H. Peter Anvina3d96d02018-06-15 17:51:39 -07001688 "after %"PRId64" passes, giving up.", passn);
H. Peter Anvinc55702e2018-12-10 22:06:15 -08001689 nasm_note("Possible causes: recursive EQUs, macro abuse.");
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001690 break;
1691 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001692 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001693
H. Peter Anvine2c80182005-01-15 22:15:51 +00001694 preproc->cleanup(0);
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08001695 lfmt->cleanup();
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001696 if (!terminate_after_phase && opt_verbose_info) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001697 /* -On and -Ov switches */
H. Peter Anvina3d96d02018-06-15 17:51:39 -07001698 fprintf(stdout, "info: assembly required 1+%"PRId64"+1 passes\n",
1699 passn-3);
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001700 }
1701}
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001702
Ed Berosetfa771012002-06-09 20:56:40 +00001703/**
1704 * gnu style error reporting
1705 * This function prints an error message to error_file in the
1706 * style used by GNU. An example would be:
1707 * file.asm:50: error: blah blah blah
H. Peter Anvin70653092007-10-19 14:42:29 -07001708 * where file.asm is the name of the file, 50 is the line number on
Ed Berosetfa771012002-06-09 20:56:40 +00001709 * which the error occurs (or is detected) and "error:" is one of
1710 * the possible optional diagnostics -- it can be "error" or "warning"
H. Peter Anvin70653092007-10-19 14:42:29 -07001711 * or something else. Finally the line terminates with the actual
Ed Berosetfa771012002-06-09 20:56:40 +00001712 * error message.
H. Peter Anvin70653092007-10-19 14:42:29 -07001713 *
1714 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001715 * @param fmt the printf style format string
1716 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001717static void nasm_verror_gnu(int severity, const char *fmt, va_list ap)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001718{
H. Peter Anvin274cda82016-05-10 02:56:29 -07001719 const char *currentfile = NULL;
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001720 int32_t lineno = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001721
Ed Berosetfa771012002-06-09 20:56:40 +00001722 if (is_suppressed_warning(severity))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001723 return;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001724
H. Peter Anvin7a6bf742017-12-20 11:54:31 -08001725 if (!(severity & ERR_NOFILE)) {
H. Peter Anvin4def1a82016-05-09 13:59:44 -07001726 src_get(&lineno, &currentfile);
H. Peter Anvin5bdc2352018-12-10 22:11:24 -08001727 if (!currentfile) {
Cyrill Gorcunovf5a48a62018-11-24 12:02:13 +03001728 currentfile = inname && inname[0] ?
1729 inname : outname && outname[0] ?
1730 outname : NULL;
H. Peter Anvin7a6bf742017-12-20 11:54:31 -08001731 lineno = 0;
1732 }
1733 }
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001734
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001735 if (!skip_this_pass(severity)) {
H. Peter Anvin7a6bf742017-12-20 11:54:31 -08001736 if (!lineno)
H. Peter Anvin070c50f2018-12-10 13:04:33 -08001737 fprintf(error_file, "%s: ", currentfile ? currentfile : "nasm");
H. Peter Anvin883985d2017-12-20 11:32:39 -08001738 else
1739 fprintf(error_file, "%s:%"PRId32": ", currentfile, lineno);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001740 }
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001741
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001742 nasm_verror_common(severity, fmt, ap);
Ed Berosetfa771012002-06-09 20:56:40 +00001743}
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001744
Ed Berosetfa771012002-06-09 20:56:40 +00001745/**
1746 * MS style error reporting
1747 * This function prints an error message to error_file in the
H. Peter Anvin70653092007-10-19 14:42:29 -07001748 * style used by Visual C and some other Microsoft tools. An example
Ed Berosetfa771012002-06-09 20:56:40 +00001749 * would be:
Ed Beroset6e61d0d2002-06-11 03:29:36 +00001750 * file.asm(50) : error: blah blah blah
H. Peter Anvin70653092007-10-19 14:42:29 -07001751 * where file.asm is the name of the file, 50 is the line number on
Ed Beroset6e61d0d2002-06-11 03:29:36 +00001752 * which the error occurs (or is detected) and "error:" is one of
1753 * the possible optional diagnostics -- it can be "error" or "warning"
H. Peter Anvin70653092007-10-19 14:42:29 -07001754 * or something else. Finally the line terminates with the actual
Ed Beroset6e61d0d2002-06-11 03:29:36 +00001755 * error message.
H. Peter Anvin70653092007-10-19 14:42:29 -07001756 *
1757 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001758 * @param fmt the printf style format string
1759 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001760static void nasm_verror_vc(int severity, const char *fmt, va_list ap)
Ed Berosetfa771012002-06-09 20:56:40 +00001761{
H. Peter Anvin274cda82016-05-10 02:56:29 -07001762 const char *currentfile = NULL;
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001763 int32_t lineno = 0;
Ed Berosetfa771012002-06-09 20:56:40 +00001764
H. Peter Anvine2c80182005-01-15 22:15:51 +00001765 if (is_suppressed_warning(severity))
1766 return;
Ed Berosetfa771012002-06-09 20:56:40 +00001767
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001768 if (!(severity & ERR_NOFILE))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001769 src_get(&lineno, &currentfile);
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001770
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001771 if (!skip_this_pass(severity)) {
H. Peter Anvin070c50f2018-12-10 13:04:33 -08001772 if (lineno) {
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001773 fprintf(error_file, "%s(%"PRId32") : ", currentfile, lineno);
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001774 } else {
H. Peter Anvin070c50f2018-12-10 13:04:33 -08001775 fprintf(error_file , "%s : ", currentfile ? currentfile : "nasm");
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001776 }
Ed Berosetfa771012002-06-09 20:56:40 +00001777 }
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001778
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001779 nasm_verror_common(severity, fmt, ap);
Ed Berosetfa771012002-06-09 20:56:40 +00001780}
1781
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001782/*
1783 * check to see if this is a suppressable warning
1784 */
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001785static inline bool is_valid_warning(int severity)
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001786{
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001787 /* Not a warning at all */
1788 if ((severity & ERR_MASK) != ERR_WARNING)
1789 return false;
1790
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001791 return WARN_IDX(severity) < ERR_WARN_ALL;
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001792}
1793
Ed Berosetfa771012002-06-09 20:56:40 +00001794/**
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001795 * check for suppressed warning
H. Peter Anvin70653092007-10-19 14:42:29 -07001796 * checks for suppressed warning or pass one only warning and we're
Ed Berosetfa771012002-06-09 20:56:40 +00001797 * not in pass 1
1798 *
1799 * @param severity the severity of the warning or error
1800 * @return true if we should abort error/warning printing
1801 */
H. Peter Anvin2b046cf2008-01-22 14:08:36 -08001802static bool is_suppressed_warning(int severity)
Ed Berosetfa771012002-06-09 20:56:40 +00001803{
H. Peter Anvin442a05a2012-02-24 21:50:53 -08001804 /* Might be a warning but suppresed explicitly */
H. Peter Anvinc805fd72018-06-12 14:23:05 -07001805 if (is_valid_warning(severity) && !(severity & ERR_USAGE))
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001806 return !(warning_state[WARN_IDX(severity)] & WARN_ST_ENABLED);
1807 else
1808 return false;
1809}
1810
1811static bool warning_is_error(int severity)
1812{
1813 if (is_valid_warning(severity))
1814 return !!(warning_state[WARN_IDX(severity)] & WARN_ST_ERROR);
H. Peter Anvin442a05a2012-02-24 21:50:53 -08001815 else
1816 return false;
Ed Berosetfa771012002-06-09 20:56:40 +00001817}
1818
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001819static bool skip_this_pass(int severity)
1820{
H. Peter Anvin8f622462017-04-02 19:02:29 -07001821 /*
1822 * See if it's a pass-specific error or warning which should be skipped.
1823 * We cannot skip errors stronger than ERR_NONFATAL as by definition
1824 * they cannot be resumed from.
1825 */
1826 if ((severity & ERR_MASK) > ERR_NONFATAL)
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001827 return false;
1828
H. Peter Anvin934f0472016-05-09 12:00:19 -07001829 /*
1830 * passn is 1 on the very first pass only.
1831 * pass0 is 2 on the code-generation (final) pass only.
1832 * These are the passes we care about in this case.
1833 */
1834 return (((severity & ERR_PASS1) && passn != 1) ||
1835 ((severity & ERR_PASS2) && pass0 != 2));
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001836}
1837
Ed Berosetfa771012002-06-09 20:56:40 +00001838/**
1839 * common error reporting
1840 * This is the common back end of the error reporting schemes currently
H. Peter Anvin70653092007-10-19 14:42:29 -07001841 * implemented. It prints the nature of the warning and then the
Ed Berosetfa771012002-06-09 20:56:40 +00001842 * specific error message to error_file and may or may not return. It
1843 * doesn't return if the error severity is a "panic" or "debug" type.
H. Peter Anvin70653092007-10-19 14:42:29 -07001844 *
1845 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001846 * @param fmt the printf style format string
1847 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001848static void nasm_verror_common(int severity, const char *fmt, va_list args)
Ed Berosetfa771012002-06-09 20:56:40 +00001849{
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001850 char msg[1024];
1851 const char *pfx;
H. Peter Anvin070c50f2018-12-10 13:04:33 -08001852 bool warn_is_err = warning_is_error(severity);
1853 bool warn_is_other = WARN_IDX(severity) == ERR_WARN_OTHER;
H. Peter Anvin323fcff2009-07-12 12:04:56 -07001854
H. Peter Anvin7df04172008-06-10 18:27:38 -07001855 switch (severity & (ERR_MASK|ERR_NO_SEVERITY)) {
H. Peter Anvind84f9a72018-12-10 13:29:35 -08001856 case ERR_NOTE:
1857 pfx = "note: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001858 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001859 case ERR_WARNING:
H. Peter Anvin070c50f2018-12-10 13:04:33 -08001860 if (!warn_is_err) {
1861 pfx = "warning: ";
1862 break;
1863 }
1864 /* fall through */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001865 case ERR_NONFATAL:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001866 pfx = "error: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001867 break;
1868 case ERR_FATAL:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001869 pfx = "fatal: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001870 break;
1871 case ERR_PANIC:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001872 pfx = "panic: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001873 break;
1874 case ERR_DEBUG:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001875 pfx = "debug: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001876 break;
H. Peter Anvin7df04172008-06-10 18:27:38 -07001877 default:
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001878 pfx = "";
1879 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001880 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00001881
H. Peter Anvin934f0472016-05-09 12:00:19 -07001882 vsnprintf(msg, sizeof msg - 64, fmt, args);
H. Peter Anvin070c50f2018-12-10 13:04:33 -08001883 if (is_valid_warning(severity) && (warn_is_err || !warn_is_other)) {
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001884 char *p = strchr(msg, '\0');
H. Peter Anvin070c50f2018-12-10 13:04:33 -08001885 snprintf(p, 64, " [-w+%s%s]",
1886 warn_is_err ? "error=" : "",
1887 warnings[WARN_IDX(severity)].name);
H. Peter Anvin934f0472016-05-09 12:00:19 -07001888 }
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001889
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001890 if (!skip_this_pass(severity))
1891 fprintf(error_file, "%s%s\n", pfx, msg);
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001892
H. Peter Anvin4def1a82016-05-09 13:59:44 -07001893 /* Are we recursing from error_list_macros? */
1894 if (severity & ERR_PP_LISTMACRO)
1895 return;
1896
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001897 /*
1898 * Don't suppress this with skip_this_pass(), or we don't get
H. Peter Anvin934f0472016-05-09 12:00:19 -07001899 * pass1 or preprocessor warnings in the list file
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001900 */
H. Peter Anvin0fcb4882016-07-06 11:55:25 -07001901 lfmt->error(severity, pfx, msg);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001902
H. Peter Anvin8f622462017-04-02 19:02:29 -07001903 if (skip_this_pass(severity))
1904 return;
1905
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001906 if (severity & ERR_USAGE)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001907 want_usage = true;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001908
H. Peter Anvin4def1a82016-05-09 13:59:44 -07001909 preproc->error_list_macros(severity);
1910
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001911 switch (severity & ERR_MASK) {
H. Peter Anvin54aac9d2018-12-10 21:14:57 -08001912 case ERR_NOTE:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001913 case ERR_DEBUG:
1914 /* no further action, by definition */
1915 break;
H. Peter Anvinb030c922007-11-13 11:31:15 -08001916 case ERR_WARNING:
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001917 /* Treat warnings as errors */
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001918 if (warning_is_error(severity))
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001919 terminate_after_phase = true;
1920 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001921 case ERR_NONFATAL:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001922 terminate_after_phase = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001923 break;
1924 case ERR_FATAL:
1925 if (ofile) {
1926 fclose(ofile);
H. Peter Anvin29695c82018-06-14 17:04:32 -07001927 if (!keep_all)
1928 remove(outname);
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001929 ofile = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001930 }
1931 if (want_usage)
1932 usage();
1933 exit(1); /* instantly die */
1934 break; /* placate silly compilers */
1935 case ERR_PANIC:
1936 fflush(NULL);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001937
1938 if (abort_on_panic)
1939 abort(); /* halt, catch fire, dump core/stop debugger */
1940
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001941 if (ofile) {
1942 fclose(ofile);
H. Peter Anvin29695c82018-06-14 17:04:32 -07001943 if (!keep_all)
1944 remove(outname);
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001945 ofile = NULL;
1946 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001947 exit(3);
1948 break;
H. Peter Anvin54aac9d2018-12-10 21:14:57 -08001949 default:
1950 break; /* ??? */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001951 }
1952}
1953
H. Peter Anvin734b1882002-04-30 21:01:08 +00001954static void usage(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001955{
H. Peter Anvin620515a2002-04-30 20:57:38 +00001956 fputs("type `nasm -h' for help\n", error_file);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001957}
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001958
1959static void help(const char xopt)
1960{
1961 int i;
1962
1963 printf
1964 ("usage: nasm [-@ response file] [-o outfile] [-f format] "
1965 "[-l listfile]\n"
1966 " [options...] [--] filename\n"
1967 " or nasm -v (or --v) for version info\n\n"
1968 "\n"
1969 "Response files should contain command line parameters,\n"
1970 "one per line.\n"
1971 "\n"
1972 " -t assemble in SciTech TASM compatible mode\n");
1973 printf
1974 (" -E (or -e) preprocess only (writes output to stdout by default)\n"
1975 " -a don't preprocess (assemble only)\n"
1976 " -M generate Makefile dependencies on stdout\n"
1977 " -MG d:o, missing files assumed generated\n"
1978 " -MF file set Makefile dependency file\n"
1979 " -MD file assemble and generate dependencies\n"
1980 " -MT file dependency target name\n"
1981 " -MQ file dependency target name (quoted)\n"
1982 " -MP emit phony target\n\n"
1983 " -Zfile redirect error messages to file\n"
1984 " -s redirect error messages to stdout\n\n"
1985 " -g generate debugging information\n\n"
1986 " -F format select a debugging format\n\n"
1987 " -gformat same as -g -F format\n\n"
1988 " -o outfile write output to an outfile\n\n"
1989 " -f format select an output format\n\n"
1990 " -l listfile write listing to a listfile\n\n"
1991 " -Ipath add a pathname to the include file path\n");
1992 printf
1993 (" -Olevel optimize opcodes, immediates and branch offsets\n"
1994 " -O0 no optimization\n"
1995 " -O1 minimal optimization\n"
1996 " -Ox multipass optimization (default)\n"
1997 " -Pfile pre-include a file (also --include)\n"
1998 " -Dmacro[=str] pre-define a macro\n"
1999 " -Umacro undefine a macro\n"
2000 " -Xformat specifiy error reporting format (gnu or vc)\n"
2001 " -w+foo enable warning foo (equiv. -Wfoo)\n"
2002 " -w-foo disable warning foo (equiv. -Wno-foo)\n"
2003 " -w[+-]error[=foo]\n"
2004 " promote [specific] warnings to errors\n"
Chang S. Bae1af6ef42018-06-20 17:05:12 -07002005 " -h show invocation summary and exit (also --help)\n\n"
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002006 " --pragma str pre-executes a specific %%pragma\n"
2007 " --before str add line (usually a preprocessor statement) before the input\n"
2008 " --prefix str prepend the given string to all the given string\n"
Chang S. Bae1af6ef42018-06-20 17:05:12 -07002009 " to all extern, common and global symbols (also --gprefix)\n"
2010 " --postfix str append the given string to all the given string\n"
2011 " to all extern, common and global symbols (also --gpostfix)\n"
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002012 " --lprefix str prepend the given string to all other symbols\n"
Chang S. Bae1af6ef42018-06-20 17:05:12 -07002013 " --lpostfix str append the given string to all other symbols\n"
2014 " --keep-all output files will not be removed even if an error happens\n"
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002015 " --limit-X val set execution limit X\n");
2016
2017 for (i = 0; i <= LIMIT_MAX; i++) {
2018 printf(" %-15s %s (default ",
2019 limit_info[i].name, limit_info[i].help);
2020 if (nasm_limit[i] < LIMIT_MAX_VAL) {
H. Peter Anvina3d96d02018-06-15 17:51:39 -07002021 printf("%"PRId64")\n", nasm_limit[i]);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002022 } else {
2023 printf("unlimited)\n");
2024 }
2025 }
2026
2027 printf("\nWarnings for the -W/-w options:\n");
2028
2029 for (i = 0; i <= ERR_WARN_ALL; i++)
2030 printf(" %-23s %s%s\n",
2031 warnings[i].name, warnings[i].help,
2032 i == ERR_WARN_ALL ? "\n" :
2033 warnings[i].enabled ? " (default on)" :
2034 " (default off)");
2035
2036 if (xopt == 'f') {
2037 printf("valid output formats for -f are"
2038 " (`*' denotes default):\n");
2039 ofmt_list(ofmt, stdout);
2040 } else {
2041 printf("For a list of valid output formats, use -hf.\n");
2042 printf("For a list of debug formats, use -f <format> -y.\n");
2043 }
2044}