blob: a30831dce323cd3de8d75c5d142e8833af33ea42 [file] [log] [blame]
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07001/* ----------------------------------------------------------------------- *
H. Peter Anvin323fcff2009-07-12 12:04:56 -07002 *
H. Peter Anvin3366e312018-02-07 14:14:36 -08003 * Copyright 1996-2018 The NASM Authors - All Rights Reserved
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07004 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00006 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07007 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
H. Peter Anvin323fcff2009-07-12 12:04:56 -070017 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -070018 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 * ----------------------------------------------------------------------- */
33
H. Peter Anvin323fcff2009-07-12 12:04:56 -070034/*
H. Peter Anvin9e6747c2009-06-28 17:13:04 -070035 * The Netwide Assembler main program module
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000036 */
37
H. Peter Anvinfe501952007-10-02 21:53:51 -070038#include "compiler.h"
39
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000040
41#include "nasm.h"
42#include "nasmlib.h"
H. Peter Anvin13506202018-11-28 14:55:58 -080043#include "nctype.h"
H. Peter Anvinb20bc732017-03-07 19:23:03 -080044#include "error.h"
H. Peter Anvin1803ded2008-06-09 17:32:43 -070045#include "saa.h"
H. Peter Anvinfcb89092008-06-09 17:40:16 -070046#include "raa.h"
H. Peter Anvinf6c9e652007-10-16 14:40:27 -070047#include "float.h"
H. Peter Anvin74cc5e52007-08-30 22:35:34 +000048#include "stdscan.h"
H. Peter Anvinaf535c12002-04-30 20:59:21 +000049#include "insns.h"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000050#include "preproc.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000051#include "parser.h"
H. Peter Anvin76690a12002-04-30 20:52:49 +000052#include "eval.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000053#include "assemble.h"
54#include "labels.h"
H. Peter Anvine1f985c2016-05-25 12:06:29 -070055#include "outform.h"
H. Peter Anvin6768eb72002-04-30 20:52:26 +000056#include "listing.h"
Cyrill Gorcunov08359152013-11-09 22:16:11 +040057#include "iflag.h"
H. Peter Anvin2bc0ab32016-03-08 02:17:36 -080058#include "ver.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000059
H. Peter Anvin31387b22010-07-15 18:28:52 -070060/*
61 * This is the maximum number of optimization passes to do. If we ever
62 * find a case where the optimizer doesn't naturally converge, we might
63 * have to drop this value so the assembler doesn't appear to just hang.
64 */
65#define MAX_OPTIMIZE (INT_MAX >> 1)
66
H. Peter Anvine2c80182005-01-15 22:15:51 +000067struct forwrefinfo { /* info held on forward refs. */
H. Peter Anvineba20a72002-04-30 20:53:55 +000068 int lineno;
69 int operand;
70};
71
H. Peter Anvin322bee02019-08-10 01:38:06 -070072const char *_progname;
73
H. Peter Anvin55568c12016-10-03 19:46:49 -070074static void parse_cmdline(int, char **, int);
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +030075static void assemble_file(const char *, struct strlist *);
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -080076static bool skip_this_pass(errflags severity);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000077static void usage(void);
H. Peter Anvin322bee02019-08-10 01:38:06 -070078static void help(FILE *);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000079
H. Peter Anvin77016c82018-12-10 22:29:49 -080080struct error_format {
81 const char *beforeline; /* Before line number, if present */
82 const char *afterline; /* After line number, if present */
83 const char *beforemsg; /* Before actual message */
84};
85
86static const struct error_format errfmt_gnu = { ":", "", ": " };
87static const struct error_format errfmt_msvc = { "(", ")", " : " };
88static const struct error_format *errfmt = &errfmt_gnu;
H. Peter Anvin (Intel)374312c2018-12-14 00:17:13 -080089static struct strlist *warn_list;
H. Peter Anvin77016c82018-12-10 22:29:49 -080090
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -070091unsigned int debug_nasm; /* Debugging messages? */
92
H. Peter Anvin283b3fb2016-03-07 23:18:30 -080093static bool using_debug_info, opt_verbose_info;
94static const char *debug_format;
95
H. Peter Anvin3366e312018-02-07 14:14:36 -080096#ifndef ABORT_ON_PANIC
97# define ABORT_ON_PANIC 0
98#endif
99static bool abort_on_panic = ABORT_ON_PANIC;
H. Peter Anvin29695c82018-06-14 17:04:32 -0700100static bool keep_all;
H. Peter Anvin3366e312018-02-07 14:14:36 -0800101
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700102bool tasm_compatible_mode = false;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800103enum pass_type _pass_type;
104const char * const _pass_types[] =
105{
106 "init", "first", "optimize", "stabilize", "final"
107};
108int64_t _passn;
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +0000109int globalrel = 0;
Jin Kyu Songb287ff02013-12-04 20:05:55 -0800110int globalbnd = 0;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000111
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700112struct compile_time official_compile_time;
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800113
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800114const char *inname;
115const char *outname;
116static const char *listname;
117static const char *errname;
118
H. Peter Anvina3d96d02018-06-15 17:51:39 -0700119static int64_t globallineno; /* for forward-reference tracking */
Chang S. Baef0ceb1e2018-05-02 08:07:53 -0700120
H. Peter Anvin338656c2016-02-17 20:59:22 -0800121const struct ofmt *ofmt = &OF_DEFAULT;
122const struct ofmt_alias *ofmt_alias = NULL;
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400123const struct dfmt *dfmt;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000124
H. Peter Anvin (Intel)3b91f4c2018-12-13 13:55:25 -0800125FILE *error_file; /* Where to write error messages */
H. Peter Anvin620515a2002-04-30 20:57:38 +0000126
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400127FILE *ofile = NULL;
Chang S. Baea5786342018-08-15 23:22:21 +0300128struct optimization optimizing =
129 { MAX_OPTIMIZE, OPTIM_ALL_ENABLED }; /* number of optimization passes to take */
Martin Lindhe8cc93f52016-11-16 16:48:13 +0100130static int cmd_sb = 16; /* by default */
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400131
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800132iflag_t cpu;
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400133static iflag_t cmd_cpu;
134
H. Peter Anvincd7893d2016-02-18 01:25:46 -0800135struct location location;
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800136bool in_absolute; /* Flag we are in ABSOLUTE seg */
137struct location absolute; /* Segment/offset inside ABSOLUTE */
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000138
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000139static struct RAA *offsets;
H. Peter Anvinea838272002-04-30 20:51:53 +0000140
H. Peter Anvine2c80182005-01-15 22:15:51 +0000141static struct SAA *forwrefs; /* keep track of forward references */
H. Peter Anvin9d637df2007-10-04 13:42:56 -0700142static const struct forwrefinfo *forwref;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000143
H. Peter Anvine7469712016-02-18 02:20:59 -0800144static const struct preproc_ops *preproc;
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +0300145static struct strlist *include_path;
H. Peter Anvin (Intel)800c1682018-12-14 12:22:11 -0800146bool pp_noline; /* Ignore %line directives */
Cyrill Gorcunov86b2ad02011-07-01 10:38:25 +0400147
H. Peter Anvin34754622018-11-28 12:36:53 -0800148#define OP_NORMAL (1U << 0)
149#define OP_PREPROCESS (1U << 1)
150#define OP_DEPEND (1U << 2)
Cyrill Gorcunove9fc88c2014-06-23 02:22:02 +0400151
152static unsigned int operating_mode;
Cyrill Gorcunov3ed32cb2014-06-22 20:53:12 +0400153
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700154/* Dependency flags */
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700155static bool depend_emit_phony = false;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700156static bool depend_missing_ok = false;
157static const char *depend_target = NULL;
158static const char *depend_file = NULL;
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +0300159struct strlist *depend_list;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000160
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700161static bool want_usage;
162static bool terminate_after_phase;
H. Peter Anvin130736c2016-02-17 20:27:41 -0800163bool user_nolist = false;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000164
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700165static char *quote_for_pmake(const char *str);
166static char *quote_for_wmake(const char *str);
167static char *(*quote_for_make)(const char *) = quote_for_pmake;
H. Peter Anvin55340992012-09-09 17:09:00 -0700168
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700169/*
170 * Execution limits that can be set via a command-line option or %pragma
171 */
172
H. Peter Anvin322bee02019-08-10 01:38:06 -0700173/*
174 * This is really unlimited; it would take far longer than the
175 * current age of the universe for this limit to be reached even on
176 * much faster CPUs than currently exist.
177*/
178#define LIMIT_MAX_VAL (INT64_MAX >> 1)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700179
H. Peter Anvin322bee02019-08-10 01:38:06 -0700180int64_t nasm_limit[LIMIT_MAX+1];
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700181
182struct limit_info {
183 const char *name;
184 const char *help;
H. Peter Anvin322bee02019-08-10 01:38:06 -0700185 int64_t default_val;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700186};
H. Peter Anvin322bee02019-08-10 01:38:06 -0700187/* The order here must match enum nasm_limit in nasm.h */
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700188static const struct limit_info limit_info[LIMIT_MAX+1] = {
H. Peter Anvin322bee02019-08-10 01:38:06 -0700189 { "passes", "total number of passes", LIMIT_MAX_VAL },
190 { "stalled-passes", "number of passes without forward progress", 1000 },
191 { "macro-levels", "levels of macro expansion", 10000 },
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -0700192 { "macro-tokens", "tokens processed during single-lime macro expansion", 10000000 },
193 { "mmacros", "multi-line macros before final return", 100000 },
H. Peter Anvin322bee02019-08-10 01:38:06 -0700194 { "rep", "%rep count", 1000000 },
195 { "eval", "expression evaluation descent", 1000000},
196 { "lines", "total source lines processed", 2000000000 }
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700197};
198
H. Peter Anvin322bee02019-08-10 01:38:06 -0700199static void set_default_limits(void)
200{
201 int i;
202 for (i = 0; i <= LIMIT_MAX; i++)
203 nasm_limit[i] = limit_info[i].default_val;
204}
205
H. Peter Anvinc805fd72018-06-12 14:23:05 -0700206enum directive_result
207nasm_set_limit(const char *limit, const char *valstr)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700208{
209 int i;
210 int64_t val;
211 bool rn_error;
H. Peter Anvinc805fd72018-06-12 14:23:05 -0700212 int errlevel;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700213
H. Peter Anvin (Intel)93d41d82019-08-16 01:12:54 -0700214 if (!limit)
215 limit = "";
216 if (!valstr)
217 valstr = "";
218
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700219 for (i = 0; i <= LIMIT_MAX; i++) {
220 if (!nasm_stricmp(limit, limit_info[i].name))
221 break;
222 }
223 if (i > LIMIT_MAX) {
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800224 if (not_started())
H. Peter Anvin (Intel)c3c6cea2018-12-14 13:44:35 -0800225 errlevel = ERR_WARNING|WARN_OTHER|ERR_USAGE;
H. Peter Anvinc805fd72018-06-12 14:23:05 -0700226 else
H. Peter Anvinfdeb3b02019-06-06 20:53:17 -0700227 errlevel = ERR_WARNING|WARN_PRAGMA_UNKNOWN;
H. Peter Anvinc805fd72018-06-12 14:23:05 -0700228 nasm_error(errlevel, "unknown limit: `%s'", limit);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700229 return DIRR_ERROR;
230 }
231
232 if (!nasm_stricmp(valstr, "unlimited")) {
233 val = LIMIT_MAX_VAL;
234 } else {
235 val = readnum(valstr, &rn_error);
236 if (rn_error || val < 0) {
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800237 if (not_started())
H. Peter Anvin (Intel)c3c6cea2018-12-14 13:44:35 -0800238 errlevel = ERR_WARNING|WARN_OTHER|ERR_USAGE;
H. Peter Anvinc805fd72018-06-12 14:23:05 -0700239 else
H. Peter Anvinfdeb3b02019-06-06 20:53:17 -0700240 errlevel = ERR_WARNING|WARN_PRAGMA_BAD;
H. Peter Anvin (Intel)93d41d82019-08-16 01:12:54 -0700241 nasm_error(errlevel, "invalid limit value: `%s'", valstr);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700242 return DIRR_ERROR;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700243 }
H. Peter Anvinc805fd72018-06-12 14:23:05 -0700244 if (val > LIMIT_MAX_VAL)
245 val = LIMIT_MAX_VAL;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700246 }
247
248 nasm_limit[i] = val;
249 return DIRR_OK;
250}
251
H. Peter Anvin892c4812018-05-30 14:43:46 -0700252int64_t switch_segment(int32_t segment)
Cyrill Gorcunov190232f2013-02-15 12:35:04 +0400253{
H. Peter Anvin892c4812018-05-30 14:43:46 -0700254 location.segment = segment;
255 if (segment == NO_SEG) {
256 location.offset = absolute.offset;
257 in_absolute = true;
258 } else {
259 location.offset = raa_read(offsets, segment);
260 in_absolute = false;
261 }
262 return location.offset;
Cyrill Gorcunov190232f2013-02-15 12:35:04 +0400263}
264
265static void set_curr_offs(int64_t l_off)
266{
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800267 if (in_absolute)
268 absolute.offset = l_off;
Cyrill Gorcunov190232f2013-02-15 12:35:04 +0400269 else
270 offsets = raa_write(offsets, location.segment, l_off);
271}
272
H. Peter Anvin892c4812018-05-30 14:43:46 -0700273static void increment_offset(int64_t delta)
274{
275 if (unlikely(delta == 0))
276 return;
277
278 location.offset += delta;
279 set_curr_offs(location.offset);
280}
281
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000282static void nasm_fputs(const char *line, FILE * outfile)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000283{
H. Peter Anvin310b3e12002-05-14 22:38:55 +0000284 if (outfile) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000285 fputs(line, outfile);
H. Peter Anvind1fb15c2007-11-13 09:37:59 -0800286 putc('\n', outfile);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000287 } else
H. Peter Anvine2c80182005-01-15 22:15:51 +0000288 puts(line);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000289}
290
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800291/*
292 * Define system-defined macros that are not part of
293 * macros/standard.mac.
294 */
295static void define_macros(void)
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800296{
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700297 const struct compile_time * const oct = &official_compile_time;
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800298 char temp[128];
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800299
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700300 if (oct->have_local) {
H. Peter Anvind2354082019-08-27 16:38:48 -0700301 strftime(temp, sizeof temp, "__?DATE?__=\"%Y-%m-%d\"", &oct->local);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400302 preproc->pre_define(temp);
H. Peter Anvind2354082019-08-27 16:38:48 -0700303 strftime(temp, sizeof temp, "__?DATE_NUM?__=%Y%m%d", &oct->local);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400304 preproc->pre_define(temp);
H. Peter Anvind2354082019-08-27 16:38:48 -0700305 strftime(temp, sizeof temp, "__?TIME?__=\"%H:%M:%S\"", &oct->local);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400306 preproc->pre_define(temp);
H. Peter Anvind2354082019-08-27 16:38:48 -0700307 strftime(temp, sizeof temp, "__?TIME_NUM?__=%H%M%S", &oct->local);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400308 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800309 }
310
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700311 if (oct->have_gm) {
H. Peter Anvind2354082019-08-27 16:38:48 -0700312 strftime(temp, sizeof temp, "__?UTC_DATE?__=\"%Y-%m-%d\"", &oct->gm);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400313 preproc->pre_define(temp);
H. Peter Anvind2354082019-08-27 16:38:48 -0700314 strftime(temp, sizeof temp, "__?UTC_DATE_NUM?__=%Y%m%d", &oct->gm);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400315 preproc->pre_define(temp);
H. Peter Anvind2354082019-08-27 16:38:48 -0700316 strftime(temp, sizeof temp, "__?UTC_TIME?__=\"%H:%M:%S\"", &oct->gm);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400317 preproc->pre_define(temp);
H. Peter Anvind2354082019-08-27 16:38:48 -0700318 strftime(temp, sizeof temp, "__?UTC_TIME_NUM?__=%H%M%S", &oct->gm);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400319 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800320 }
H. Peter Anvind85d2502008-05-04 17:53:31 -0700321
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700322 if (oct->have_posix) {
H. Peter Anvind2354082019-08-27 16:38:48 -0700323 snprintf(temp, sizeof temp, "__?POSIX_TIME?__=%"PRId64, oct->posix);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400324 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800325 }
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800326
Cyrill Gorcunovc1936da2011-04-06 18:32:15 +0400327 /*
328 * In case if output format is defined by alias
329 * we have to put shortname of the alias itself here
330 * otherwise ABI backward compatibility gets broken.
331 */
H. Peter Anvind2354082019-08-27 16:38:48 -0700332 snprintf(temp, sizeof(temp), "__?OUTPUT_FORMAT?__=%s",
Cyrill Gorcunovc1936da2011-04-06 18:32:15 +0400333 ofmt_alias ? ofmt_alias->shortname : ofmt->shortname);
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +0400334 preproc->pre_define(temp);
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800335
336 /*
337 * Output-format specific macros.
338 */
339 if (ofmt->stdmac)
340 preproc->extra_stdmac(ofmt->stdmac);
341
342 /*
343 * Debug format, if any
344 */
345 if (dfmt != &null_debug_form) {
H. Peter Anvind2354082019-08-27 16:38:48 -0700346 snprintf(temp, sizeof(temp), "__?DEBUG_FORMAT?__=%s", dfmt->shortname);
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800347 preproc->pre_define(temp);
348 }
349}
350
351/*
352 * Initialize the preprocessor, set up the include path, and define
353 * the system-included macros. This is called between passes 1 and 2
354 * of parsing the command options; ofmt and dfmt are defined at this
355 * point.
356 *
357 * Command-line specified preprocessor directives (-p, -d, -u,
358 * --pragma, --before) are processed after this function.
359 */
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +0300360static void preproc_init(struct strlist *ipath)
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800361{
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800362 preproc->init();
363 define_macros();
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +0300364 preproc->include_path(ipath);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800365}
366
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +0300367static void emit_dependencies(struct strlist *list)
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700368{
369 FILE *deps;
370 int linepos, len;
H. Peter Anvinf05034f2017-08-16 22:08:36 -0700371 bool wmake = (quote_for_make == quote_for_wmake);
372 const char *wrapstr, *nulltarget;
H. Peter Anvin (Intel)64471092018-12-11 13:06:14 -0800373 const struct strlist_entry *l;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -0700374
375 if (!list)
376 return;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700377
H. Peter Anvinf05034f2017-08-16 22:08:36 -0700378 wrapstr = wmake ? " &\n " : " \\\n ";
379 nulltarget = wmake ? "\t%null\n" : "";
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700380
381 if (depend_file && strcmp(depend_file, "-")) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -0700382 deps = nasm_open_write(depend_file, NF_TEXT);
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400383 if (!deps) {
H. Peter Anvinc55702e2018-12-10 22:06:15 -0800384 nasm_nonfatal("unable to write dependency file `%s'", depend_file);
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400385 return;
386 }
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700387 } else {
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400388 deps = stdout;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700389 }
H. Peter Anvin323fcff2009-07-12 12:04:56 -0700390
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700391 linepos = fprintf(deps, "%s :", depend_target);
H. Peter Anvin (Intel)64471092018-12-11 13:06:14 -0800392 strlist_for_each(l, list) {
H. Peter Anvin55340992012-09-09 17:09:00 -0700393 char *file = quote_for_make(l->str);
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400394 len = strlen(file);
395 if (linepos + len > 62 && linepos > 1) {
H. Peter Anvinf05034f2017-08-16 22:08:36 -0700396 fputs(wrapstr, deps);
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400397 linepos = 1;
398 }
399 fprintf(deps, " %s", file);
400 linepos += len+1;
H. Peter Anvin55340992012-09-09 17:09:00 -0700401 nasm_free(file);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700402 }
H. Peter Anvin322bee02019-08-10 01:38:06 -0700403 fputs("\n\n", deps);
H. Peter Anvin323fcff2009-07-12 12:04:56 -0700404
H. Peter Anvin (Intel)64471092018-12-11 13:06:14 -0800405 strlist_for_each(l, list) {
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700406 if (depend_emit_phony) {
407 char *file = quote_for_make(l->str);
H. Peter Anvinf05034f2017-08-16 22:08:36 -0700408 fprintf(deps, "%s :\n%s\n", file, nulltarget);
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700409 nasm_free(file);
410 }
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700411 }
412
H. Peter Anvin (Intel)374312c2018-12-14 00:17:13 -0800413 strlist_free(&list);
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -0700414
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700415 if (deps != stdout)
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400416 fclose(deps);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700417}
418
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700419/* Convert a struct tm to a POSIX-style time constant */
420static int64_t make_posix_time(const struct tm *tm)
421{
422 int64_t t;
423 int64_t y = tm->tm_year;
424
425 /* See IEEE 1003.1:2004, section 4.14 */
426
427 t = (y-70)*365 + (y-69)/4 - (y-1)/100 + (y+299)/400;
428 t += tm->tm_yday;
429 t *= 24;
430 t += tm->tm_hour;
431 t *= 60;
432 t += tm->tm_min;
433 t *= 60;
434 t += tm->tm_sec;
435
436 return t;
437}
438
439static void timestamp(void)
440{
441 struct compile_time * const oct = &official_compile_time;
442 const struct tm *tp, *best_gm;
443
444 time(&oct->t);
445
446 best_gm = NULL;
447
448 tp = localtime(&oct->t);
449 if (tp) {
450 oct->local = *tp;
451 best_gm = &oct->local;
452 oct->have_local = true;
453 }
454
455 tp = gmtime(&oct->t);
456 if (tp) {
457 oct->gm = *tp;
458 best_gm = &oct->gm;
459 oct->have_gm = true;
460 if (!oct->have_local)
461 oct->local = oct->gm;
462 } else {
463 oct->gm = oct->local;
464 }
465
466 if (best_gm) {
467 oct->posix = make_posix_time(best_gm);
468 oct->have_posix = true;
469 }
470}
471
H. Peter Anvin038d8612007-04-12 16:54:50 +0000472int main(int argc, char **argv)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000473{
H. Peter Anvin6a4353c2019-08-28 18:32:46 -0700474 /* Do these as early as possible */
475 error_file = stderr;
H. Peter Anvin322bee02019-08-10 01:38:06 -0700476 _progname = argv[0];
477 if (!_progname || !_progname[0])
478 _progname = "nasm";
479
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700480 timestamp();
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800481
H. Peter Anvina7ecf262018-02-06 14:43:07 -0800482 iflag_set_default_cpu(&cpu);
483 iflag_set_default_cpu(&cmd_cpu);
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400484
H. Peter Anvin322bee02019-08-10 01:38:06 -0700485 set_default_limits();
486
H. Peter Anvin (Intel)7bb13ea2018-12-13 22:48:14 -0800487 include_path = strlist_alloc(true);
Cyrill Gorcunove3588512018-11-13 01:09:27 +0300488
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800489 _pass_type = PASS_INIT;
490 _passn = 0;
491
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700492 want_usage = terminate_after_phase = false;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000493
H. Peter Anvin13506202018-11-28 14:55:58 -0800494 nasm_ctype_init();
H. Peter Anvin274cda82016-05-10 02:56:29 -0700495 src_init();
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -0700496
H. Peter Anvin, Intel87d9e622018-06-25 12:58:49 -0700497 /*
498 * We must call init_labels() before the command line parsing,
499 * because we may be setting prefixes/suffixes from the command
500 * line.
501 */
502 init_labels();
503
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000504 offsets = raa_init();
Keith Kaniosb7a89542007-04-12 02:40:54 +0000505 forwrefs = saa_init((int32_t)sizeof(struct forwrefinfo));
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000506
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000507 preproc = &nasmpp;
Cyrill Gorcunov3ed32cb2014-06-22 20:53:12 +0400508 operating_mode = OP_NORMAL;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000509
H. Peter Anvin55568c12016-10-03 19:46:49 -0700510 parse_cmdline(argc, argv, 1);
511 if (terminate_after_phase) {
512 if (want_usage)
513 usage();
514 return 1;
515 }
516
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800517 /* At this point we have ofmt and the name of the desired debug format */
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800518 if (!using_debug_info) {
519 /* No debug info, redirect to the null backend (empty stubs) */
H. Peter Anvina7bc15d2016-02-17 20:55:08 -0800520 dfmt = &null_debug_form;
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800521 } else if (!debug_format) {
522 /* Default debug format for this backend */
Cyrill Gorcunov988cc122018-12-15 23:44:46 +0300523 dfmt = ofmt->default_dfmt;
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800524 } else {
525 dfmt = dfmt_find(ofmt, debug_format);
526 if (!dfmt) {
H. Peter Anvin77016c82018-12-10 22:29:49 -0800527 nasm_fatalf(ERR_USAGE, "unrecognized debug format `%s' for output format `%s'",
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800528 debug_format, ofmt->shortname);
529 }
530 }
H. Peter Anvinbb88d012003-09-10 23:34:23 +0000531
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +0300532 preproc_init(include_path);
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800533
534 parse_cmdline(argc, argv, 2);
535 if (terminate_after_phase) {
536 if (want_usage)
537 usage();
538 return 1;
539 }
540
541 /* Save away the default state of warnings */
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -0800542 init_warnings();
H. Peter Anvin76690a12002-04-30 20:52:49 +0000543
H. Peter Anvin34754622018-11-28 12:36:53 -0800544 /* Dependency filename if we are also doing other things */
545 if (!depend_file && (operating_mode & ~OP_DEPEND)) {
546 if (outname)
547 depend_file = nasm_strcat(outname, ".d");
548 else
549 depend_file = filename_set_extension(inname, ".d");
550 }
551
Cyrill Gorcunov69bb0522018-09-22 13:46:45 +0300552 /*
553 * If no output file name provided and this
H. Peter Anvin34754622018-11-28 12:36:53 -0800554 * is preprocess mode, we're perfectly
Cyrill Gorcunovda3780d2018-09-22 14:10:36 +0300555 * fine to output into stdout.
Cyrill Gorcunov69bb0522018-09-22 13:46:45 +0300556 */
H. Peter Anvin (Intel)7b6371b2018-11-20 10:56:57 -0800557 if (!outname && !(operating_mode & OP_PREPROCESS)) {
558 outname = filename_set_extension(inname, ofmt->extension);
559 if (!strcmp(outname, inname)) {
560 outname = "nasm.out";
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -0800561 nasm_warn(WARN_OTHER, "default output file same as input, using `%s' for output\n", outname);
H. Peter Anvin (Intel)7b6371b2018-11-20 10:56:57 -0800562 }
Cyrill Gorcunov69bb0522018-09-22 13:46:45 +0300563 }
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800564
H. Peter Anvin (Intel)7bb13ea2018-12-13 22:48:14 -0800565 depend_list = (operating_mode & OP_DEPEND) ? strlist_alloc(true) : NULL;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700566
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700567 if (!depend_target)
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400568 depend_target = quote_for_make(outname);
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700569
H. Peter Anvin34754622018-11-28 12:36:53 -0800570 if (!(operating_mode & (OP_PREPROCESS|OP_NORMAL))) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000571 char *line;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700572
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400573 if (depend_missing_ok)
574 preproc->include_path(NULL); /* "assume generated" */
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700575
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800576 preproc->reset(inname, PP_DEPS, depend_list);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000577 ofile = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000578 while ((line = preproc->getline()))
579 nasm_free(line);
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800580 preproc->cleanup_pass();
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -0800581 reset_warnings();
Cyrill Gorcunove9fc88c2014-06-23 02:22:02 +0400582 } else if (operating_mode & OP_PREPROCESS) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000583 char *line;
H. Peter Anvin274cda82016-05-10 02:56:29 -0700584 const char *file_name = NULL;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000585 int32_t prior_linnum = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000586 int lineinc = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000587
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800588 if (outname) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -0700589 ofile = nasm_open_write(outname, NF_TEXT);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000590 if (!ofile)
H. Peter Anvin77016c82018-12-10 22:29:49 -0800591 nasm_fatal("unable to open output file `%s'", outname);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000592 } else
593 ofile = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000594
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700595 location.known = false;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000596
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800597 _pass_type = PASS_FIRST; /* We emulate this assembly pass */
598 preproc->reset(inname, PP_PREPROC, depend_list);
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800599
H. Peter Anvine2c80182005-01-15 22:15:51 +0000600 while ((line = preproc->getline())) {
601 /*
602 * We generate %line directives if needed for later programs
603 */
Keith Kaniosb7a89542007-04-12 02:40:54 +0000604 int32_t linnum = prior_linnum += lineinc;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000605 int altline = src_get(&linnum, &file_name);
606 if (altline) {
607 if (altline == 1 && lineinc == 1)
608 nasm_fputs("", ofile);
609 else {
610 lineinc = (altline != -1 || lineinc != 1);
611 fprintf(ofile ? ofile : stdout,
Keith Kanios93f2e9a2007-04-14 00:10:59 +0000612 "%%line %"PRId32"+%d %s\n", linnum, lineinc,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000613 file_name);
614 }
615 prior_linnum = linnum;
616 }
617 nasm_fputs(line, ofile);
618 nasm_free(line);
619 }
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800620 preproc->cleanup_pass();
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -0800621 reset_warnings();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000622 if (ofile)
623 fclose(ofile);
H. Peter Anvin29695c82018-06-14 17:04:32 -0700624 if (ofile && terminate_after_phase && !keep_all)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000625 remove(outname);
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400626 ofile = NULL;
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400627 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000628
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400629 if (operating_mode & OP_NORMAL) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -0700630 ofile = nasm_open_write(outname, (ofmt->flags & OFMT_TEXT) ? NF_TEXT : NF_BINARY);
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400631 if (!ofile)
H. Peter Anvinc55702e2018-12-10 22:06:15 -0800632 nasm_fatal("unable to open output file `%s'", outname);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000633
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400634 ofmt->init();
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400635 dfmt->init();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000636
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -0700637 assemble_file(inname, depend_list);
Victor van den Elzenc82c3722008-06-04 15:24:20 +0200638
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400639 if (!terminate_after_phase) {
H. Peter Anvin477ae442016-03-07 22:53:06 -0800640 ofmt->cleanup();
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400641 cleanup_labels();
642 fflush(ofile);
H. Peter Anvinc55702e2018-12-10 22:06:15 -0800643 if (ferror(ofile))
644 nasm_nonfatal("write error on output file `%s'", outname);
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400645 }
646
647 if (ofile) {
648 fclose(ofile);
H. Peter Anvin29695c82018-06-14 17:04:32 -0700649 if (terminate_after_phase && !keep_all)
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400650 remove(outname);
651 ofile = NULL;
652 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000653 }
H. Peter Anvin734b1882002-04-30 21:01:08 +0000654
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800655 preproc->cleanup_session();
656
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700657 if (depend_list && !terminate_after_phase)
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400658 emit_dependencies(depend_list);
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700659
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000660 if (want_usage)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000661 usage();
H. Peter Anvin734b1882002-04-30 21:01:08 +0000662
H. Peter Anvine2c80182005-01-15 22:15:51 +0000663 raa_free(offsets);
664 saa_free(forwrefs);
665 eval_cleanup();
H. Peter Anvin74cc5e52007-08-30 22:35:34 +0000666 stdscan_cleanup();
H. Peter Anvin274cda82016-05-10 02:56:29 -0700667 src_free();
H. Peter Anvin (Intel)374312c2018-12-14 00:17:13 -0800668 strlist_free(&include_path);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000669
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700670 return terminate_after_phase;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000671}
672
H. Peter Anvineba20a72002-04-30 20:53:55 +0000673/*
674 * Get a parameter for a command line option.
675 * First arg must be in the form of e.g. -f...
676 */
H. Peter Anvin423e3812007-11-15 17:12:29 -0800677static char *get_param(char *p, char *q, bool *advance)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000678{
H. Peter Anvin423e3812007-11-15 17:12:29 -0800679 *advance = false;
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +0400680 if (p[2]) /* the parameter's in the option */
681 return nasm_skip_spaces(p + 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000682 if (q && q[0]) {
H. Peter Anvin423e3812007-11-15 17:12:29 -0800683 *advance = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000684 return q;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000685 }
H. Peter Anvinc55702e2018-12-10 22:06:15 -0800686 nasm_nonfatalf(ERR_USAGE, "option `-%c' requires an argument", p[1]);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000687 return NULL;
688}
689
H. Peter Anvindc242712007-11-18 11:55:10 -0800690/*
691 * Copy a filename
692 */
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800693static void copy_filename(const char **dst, const char *src, const char *what)
H. Peter Anvindc242712007-11-18 11:55:10 -0800694{
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800695 if (*dst)
H. Peter Anvinc5136902018-06-15 18:20:17 -0700696 nasm_fatal("more than one %s file specified: %s\n", what, src);
H. Peter Anvindc242712007-11-18 11:55:10 -0800697
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800698 *dst = nasm_strdup(src);
H. Peter Anvindc242712007-11-18 11:55:10 -0800699}
700
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700701/*
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700702 * Convert a string to a POSIX make-safe form
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700703 */
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700704static char *quote_for_pmake(const char *str)
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700705{
706 const char *p;
707 char *os, *q;
708
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400709 size_t n = 1; /* Terminating zero */
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700710 size_t nbs = 0;
711
712 if (!str)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400713 return NULL;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700714
715 for (p = str; *p; p++) {
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400716 switch (*p) {
717 case ' ':
718 case '\t':
719 /* Convert N backslashes + ws -> 2N+1 backslashes + ws */
720 n += nbs + 2;
721 nbs = 0;
722 break;
723 case '$':
724 case '#':
725 nbs = 0;
726 n += 2;
727 break;
728 case '\\':
729 nbs++;
730 n++;
731 break;
732 default:
733 nbs = 0;
734 n++;
735 break;
736 }
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700737 }
738
739 /* Convert N backslashes at the end of filename to 2N backslashes */
740 if (nbs)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400741 n += nbs;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700742
743 os = q = nasm_malloc(n);
744
745 nbs = 0;
746 for (p = str; *p; p++) {
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400747 switch (*p) {
748 case ' ':
749 case '\t':
750 while (nbs--)
751 *q++ = '\\';
752 *q++ = '\\';
753 *q++ = *p;
754 break;
755 case '$':
756 *q++ = *p;
757 *q++ = *p;
758 nbs = 0;
759 break;
760 case '#':
761 *q++ = '\\';
762 *q++ = *p;
763 nbs = 0;
764 break;
765 case '\\':
766 *q++ = *p;
767 nbs++;
768 break;
769 default:
770 *q++ = *p;
771 nbs = 0;
772 break;
773 }
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700774 }
775 while (nbs--)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400776 *q++ = '\\';
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700777
778 *q = '\0';
779
780 return os;
781}
782
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700783/*
784 * Convert a string to a Watcom make-safe form
785 */
786static char *quote_for_wmake(const char *str)
787{
788 const char *p;
789 char *os, *q;
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700790 bool quote = false;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700791
792 size_t n = 1; /* Terminating zero */
793
794 if (!str)
795 return NULL;
796
797 for (p = str; *p; p++) {
798 switch (*p) {
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700799 case ' ':
800 case '\t':
H. Peter Anvin3e30c322017-08-16 22:20:36 -0700801 case '&':
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700802 quote = true;
803 n++;
804 break;
805 case '\"':
806 quote = true;
807 n += 2;
808 break;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700809 case '$':
810 case '#':
811 n += 2;
812 break;
813 default:
814 n++;
815 break;
816 }
817 }
818
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700819 if (quote)
820 n += 2;
821
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700822 os = q = nasm_malloc(n);
823
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700824 if (quote)
825 *q++ = '\"';
826
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700827 for (p = str; *p; p++) {
828 switch (*p) {
829 case '$':
830 case '#':
831 *q++ = '$';
832 *q++ = *p;
833 break;
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700834 case '\"':
835 *q++ = *p;
836 *q++ = *p;
837 break;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700838 default:
839 *q++ = *p;
840 break;
841 }
842 }
843
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700844 if (quote)
845 *q++ = '\"';
846
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700847 *q = '\0';
848
849 return os;
850}
851
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100852enum text_options {
H. Peter Anvin3366e312018-02-07 14:14:36 -0800853 OPT_BOGUS,
854 OPT_VERSION,
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700855 OPT_HELP,
H. Peter Anvin3366e312018-02-07 14:14:36 -0800856 OPT_ABORT_ON_PANIC,
H. Peter Anvin05990342018-06-11 13:32:42 -0700857 OPT_MANGLE,
858 OPT_INCLUDE,
859 OPT_PRAGMA,
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700860 OPT_BEFORE,
H. Peter Anvin29695c82018-06-14 17:04:32 -0700861 OPT_LIMIT,
H. Peter Anvin (Intel)800c1682018-12-14 12:22:11 -0800862 OPT_KEEP_ALL,
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -0700863 OPT_NO_LINE,
864 OPT_DEBUG
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100865};
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -0700866enum need_arg {
867 ARG_NO,
868 ARG_YES,
869 ARG_MAYBE
870};
871
H. Peter Anvin3366e312018-02-07 14:14:36 -0800872struct textargs {
873 const char *label;
874 enum text_options opt;
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -0700875 enum need_arg need_arg;
H. Peter Anvin98578072018-06-01 18:02:54 -0700876 int pvt;
H. Peter Anvin3366e312018-02-07 14:14:36 -0800877};
H. Peter Anvin2530a102016-02-18 02:28:15 -0800878static const struct textargs textopts[] = {
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -0700879 {"v", OPT_VERSION, ARG_NO, 0},
880 {"version", OPT_VERSION, ARG_NO, 0},
881 {"help", OPT_HELP, ARG_NO, 0},
882 {"abort-on-panic", OPT_ABORT_ON_PANIC, ARG_NO, 0},
883 {"prefix", OPT_MANGLE, ARG_YES, LM_GPREFIX},
884 {"postfix", OPT_MANGLE, ARG_YES, LM_GSUFFIX},
885 {"gprefix", OPT_MANGLE, ARG_YES, LM_GPREFIX},
886 {"gpostfix", OPT_MANGLE, ARG_YES, LM_GSUFFIX},
887 {"lprefix", OPT_MANGLE, ARG_YES, LM_LPREFIX},
888 {"lpostfix", OPT_MANGLE, ARG_YES, LM_LSUFFIX},
889 {"include", OPT_INCLUDE, ARG_YES, 0},
890 {"pragma", OPT_PRAGMA, ARG_YES, 0},
891 {"before", OPT_BEFORE, ARG_YES, 0},
892 {"limit-", OPT_LIMIT, ARG_YES, 0},
893 {"keep-all", OPT_KEEP_ALL, ARG_NO, 0},
894 {"no-line", OPT_NO_LINE, ARG_NO, 0},
895 {"debug", OPT_DEBUG, ARG_MAYBE, 0},
896 {NULL, OPT_BOGUS, ARG_NO, 0}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000897};
898
Cyrill Gorcunove7438432014-05-09 22:34:34 +0400899static void show_version(void)
900{
901 printf("NASM version %s compiled on %s%s\n",
902 nasm_version, nasm_date, nasm_compile_options);
903 exit(0);
904}
905
H. Peter Anvin423e3812007-11-15 17:12:29 -0800906static bool stopoptions = false;
H. Peter Anvin55568c12016-10-03 19:46:49 -0700907static bool process_arg(char *p, char *q, int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000908{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000909 char *param;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800910 bool advance = false;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000911
912 if (!p || !p[0])
H. Peter Anvin423e3812007-11-15 17:12:29 -0800913 return false;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000914
H. Peter Anvine2c80182005-01-15 22:15:51 +0000915 if (p[0] == '-' && !stopoptions) {
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -0700916 if (strchr("oOfpPdDiIlLFXuUZwW", p[1])) {
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400917 /* These parameters take values */
918 if (!(param = get_param(p, q, &advance)))
919 return advance;
920 }
H. Peter Anvin423e3812007-11-15 17:12:29 -0800921
H. Peter Anvine2c80182005-01-15 22:15:51 +0000922 switch (p[1]) {
923 case 's':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700924 if (pass == 1)
925 error_file = stdout;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000926 break;
H. Peter Anvin70653092007-10-19 14:42:29 -0700927
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400928 case 'o': /* output file */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700929 if (pass == 2)
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800930 copy_filename(&outname, param, "output");
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400931 break;
H. Peter Anvinfd7dd112007-10-10 14:06:59 -0700932
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400933 case 'f': /* output format */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700934 if (pass == 1) {
935 ofmt = ofmt_find(param, &ofmt_alias);
936 if (!ofmt) {
H. Peter Anvin77016c82018-12-10 22:29:49 -0800937 nasm_fatalf(ERR_USAGE, "unrecognised output format `%s' - use -hf for a list", param);
H. Peter Anvin55568c12016-10-03 19:46:49 -0700938 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400939 }
940 break;
H. Peter Anvin70653092007-10-19 14:42:29 -0700941
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400942 case 'O': /* Optimization level */
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800943 if (pass == 1) {
H. Peter Anvin55568c12016-10-03 19:46:49 -0700944 int opt;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700945
H. Peter Anvin55568c12016-10-03 19:46:49 -0700946 if (!*param) {
947 /* Naked -O == -Ox */
Chang S. Baea5786342018-08-15 23:22:21 +0300948 optimizing.level = MAX_OPTIMIZE;
H. Peter Anvin55568c12016-10-03 19:46:49 -0700949 } else {
950 while (*param) {
951 switch (*param) {
952 case '0': case '1': case '2': case '3': case '4':
953 case '5': case '6': case '7': case '8': case '9':
954 opt = strtoul(param, &param, 10);
H. Peter Anvind85d2502008-05-04 17:53:31 -0700955
Chang S. Baea5786342018-08-15 23:22:21 +0300956 /* -O0 -> optimizing.level == -1, 0.98 behaviour */
957 /* -O1 -> optimizing.level == 0, 0.98.09 behaviour */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700958 if (opt < 2)
Chang S. Baea5786342018-08-15 23:22:21 +0300959 optimizing.level = opt - 1;
H. Peter Anvin55568c12016-10-03 19:46:49 -0700960 else
Chang S. Baea5786342018-08-15 23:22:21 +0300961 optimizing.level = opt;
H. Peter Anvin55568c12016-10-03 19:46:49 -0700962 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700963
H. Peter Anvin55568c12016-10-03 19:46:49 -0700964 case 'v':
965 case '+':
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400966 param++;
967 opt_verbose_info = true;
968 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700969
H. Peter Anvin55568c12016-10-03 19:46:49 -0700970 case 'x':
971 param++;
Chang S. Baea5786342018-08-15 23:22:21 +0300972 optimizing.level = MAX_OPTIMIZE;
H. Peter Anvin55568c12016-10-03 19:46:49 -0700973 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700974
H. Peter Anvin55568c12016-10-03 19:46:49 -0700975 default:
H. Peter Anvinc5136902018-06-15 18:20:17 -0700976 nasm_fatal("unknown optimization option -O%c\n",
H. Peter Anvin55568c12016-10-03 19:46:49 -0700977 *param);
978 break;
979 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400980 }
Chang S. Baea5786342018-08-15 23:22:21 +0300981 if (optimizing.level > MAX_OPTIMIZE)
982 optimizing.level = MAX_OPTIMIZE;
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400983 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400984 }
985 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800986
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400987 case 'p': /* pre-include */
988 case 'P':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700989 if (pass == 2)
990 preproc->pre_include(param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400991 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800992
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400993 case 'd': /* pre-define */
994 case 'D':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700995 if (pass == 2)
996 preproc->pre_define(param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400997 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800998
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400999 case 'u': /* un-define */
1000 case 'U':
H. Peter Anvin55568c12016-10-03 19:46:49 -07001001 if (pass == 2)
1002 preproc->pre_undefine(param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001003 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001004
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001005 case 'i': /* include search path */
1006 case 'I':
H. Peter Anvinbf6230b2018-11-11 13:25:16 -08001007 if (pass == 1)
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03001008 strlist_add(include_path, param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001009 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001010
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001011 case 'l': /* listing file */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001012 if (pass == 2)
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001013 copy_filename(&listname, param, "listing");
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001014 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001015
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07001016 case 'L': /* listing options */
1017 if (pass == 2) {
H. Peter Anvind91519a2019-08-10 18:04:04 -07001018 while (*param)
1019 list_options |= list_option_mask(*param++);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07001020 }
1021 break;
1022
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001023 case 'Z': /* error messages file */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001024 if (pass == 1)
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001025 copy_filename(&errname, param, "error");
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001026 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001027
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001028 case 'F': /* specify debug format */
H. Peter Anvinbf6230b2018-11-11 13:25:16 -08001029 if (pass == 1) {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001030 using_debug_info = true;
1031 debug_format = param;
1032 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001033 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001034
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001035 case 'X': /* specify error reporting format */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001036 if (pass == 1) {
H. Peter Anvin77016c82018-12-10 22:29:49 -08001037 if (!nasm_stricmp("vc", param) || !nasm_stricmp("msvc", param) || !nasm_stricmp("ms", param))
1038 errfmt = &errfmt_msvc;
1039 else if (!nasm_stricmp("gnu", param) || !nasm_stricmp("gcc", param))
1040 errfmt = &errfmt_gnu;
H. Peter Anvin55568c12016-10-03 19:46:49 -07001041 else
H. Peter Anvin77016c82018-12-10 22:29:49 -08001042 nasm_fatalf(ERR_USAGE, "unrecognized error reporting format `%s'", param);
H. Peter Anvin55568c12016-10-03 19:46:49 -07001043 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001044 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001045
H. Peter Anvine2c80182005-01-15 22:15:51 +00001046 case 'g':
H. Peter Anvinbf6230b2018-11-11 13:25:16 -08001047 if (pass == 1) {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001048 using_debug_info = true;
1049 if (p[2])
1050 debug_format = nasm_skip_spaces(p + 2);
1051 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001052 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001053
H. Peter Anvine2c80182005-01-15 22:15:51 +00001054 case 'h':
H. Peter Anvin322bee02019-08-10 01:38:06 -07001055 help(stdout);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001056 exit(0); /* never need usage message here */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001057 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001058
H. Peter Anvine2c80182005-01-15 22:15:51 +00001059 case 'y':
H. Peter Anvin322bee02019-08-10 01:38:06 -07001060 /* legacy option */
1061 dfmt_list(stdout);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001062 exit(0);
1063 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001064
H. Peter Anvine2c80182005-01-15 22:15:51 +00001065 case 't':
H. Peter Anvin13506202018-11-28 14:55:58 -08001066 if (pass == 2) {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001067 tasm_compatible_mode = true;
H. Peter Anvin13506202018-11-28 14:55:58 -08001068 nasm_ctype_tasm_mode();
1069 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001070 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001071
H. Peter Anvine2c80182005-01-15 22:15:51 +00001072 case 'v':
Cyrill Gorcunove7438432014-05-09 22:34:34 +04001073 show_version();
H. Peter Anvine2c80182005-01-15 22:15:51 +00001074 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001075
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001076 case 'e': /* preprocess only */
1077 case 'E':
H. Peter Anvin55568c12016-10-03 19:46:49 -07001078 if (pass == 1)
1079 operating_mode = OP_PREPROCESS;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001080 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001081
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001082 case 'a': /* assemble only - don't preprocess */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001083 if (pass == 1)
1084 preproc = &preproc_nop;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001085 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001086
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001087 case 'w':
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001088 case 'W':
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08001089 if (pass == 2)
1090 set_warning_status(param);
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001091 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001092
H. Peter Anvine2c80182005-01-15 22:15:51 +00001093 case 'M':
H. Peter Anvin77c9bf62017-08-16 21:14:33 -07001094 if (pass == 1) {
1095 switch (p[2]) {
1096 case 'W':
1097 quote_for_make = quote_for_wmake;
1098 break;
1099 case 'D':
1100 case 'F':
1101 case 'T':
1102 case 'Q':
1103 advance = true;
1104 break;
1105 default:
1106 break;
1107 }
1108 } else {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001109 switch (p[2]) {
1110 case 0:
1111 operating_mode = OP_DEPEND;
1112 break;
1113 case 'G':
1114 operating_mode = OP_DEPEND;
1115 depend_missing_ok = true;
1116 break;
1117 case 'P':
1118 depend_emit_phony = true;
1119 break;
1120 case 'D':
H. Peter Anvin34754622018-11-28 12:36:53 -08001121 operating_mode |= OP_DEPEND;
1122 if (q && (q[0] != '-' || q[1] == '\0')) {
1123 depend_file = q;
1124 advance = true;
1125 }
H. Peter Anvin55568c12016-10-03 19:46:49 -07001126 break;
1127 case 'F':
1128 depend_file = q;
1129 advance = true;
1130 break;
1131 case 'T':
1132 depend_target = q;
1133 advance = true;
1134 break;
1135 case 'Q':
1136 depend_target = quote_for_make(q);
1137 advance = true;
1138 break;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -07001139 case 'W':
1140 /* handled in pass 1 */
1141 break;
H. Peter Anvin55568c12016-10-03 19:46:49 -07001142 default:
H. Peter Anvinc55702e2018-12-10 22:06:15 -08001143 nasm_nonfatalf(ERR_USAGE, "unknown dependency option `-M%c'", p[2]);
H. Peter Anvin55568c12016-10-03 19:46:49 -07001144 break;
1145 }
H. Peter Anvin77c9bf62017-08-16 21:14:33 -07001146 }
1147 if (advance && (!q || !q[0])) {
H. Peter Anvinc55702e2018-12-10 22:06:15 -08001148 nasm_nonfatalf(ERR_USAGE, "option `-M%c' requires a parameter", p[2]);
H. Peter Anvin77c9bf62017-08-16 21:14:33 -07001149 break;
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001150 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001151 break;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001152
H. Peter Anvine2c80182005-01-15 22:15:51 +00001153 case '-':
1154 {
H. Peter Anvin3366e312018-02-07 14:14:36 -08001155 const struct textargs *tx;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001156 size_t olen, plen;
H. Peter Anvinc805fd72018-06-12 14:23:05 -07001157 char *eqsave;
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -07001158 enum text_options opt;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001159
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001160 p += 2;
1161
1162 if (!*p) { /* -- => stop processing options */
H. Peter Anvin3366e312018-02-07 14:14:36 -08001163 stopoptions = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001164 break;
1165 }
Cyrill Gorcunove7438432014-05-09 22:34:34 +04001166
H. Peter Anvin (Intel)1e2358b2018-12-14 13:02:39 -08001167 olen = 0; /* Placate gcc at lower optimization levels */
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001168 plen = strlen(p);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001169 for (tx = textopts; tx->label; tx++) {
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001170 olen = strlen(tx->label);
1171
1172 if (olen > plen)
1173 continue;
1174
1175 if (nasm_memicmp(p, tx->label, olen))
1176 continue;
1177
1178 if (tx->label[olen-1] == '-')
1179 break; /* Incomplete option */
1180
1181 if (!p[olen] || p[olen] == '=')
1182 break; /* Complete option */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001183 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001184
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001185 if (!tx->label) {
H. Peter Anvinc55702e2018-12-10 22:06:15 -08001186 nasm_nonfatalf(ERR_USAGE, "unrecognized option `--%s'", p);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001187 }
1188
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -07001189 opt = tx->opt;
1190
H. Peter Anvinc805fd72018-06-12 14:23:05 -07001191 eqsave = param = strchr(p+olen, '=');
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001192 if (param)
1193 *param++ = '\0';
1194
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -07001195 switch (tx->need_arg) {
1196 case ARG_YES: /* Argument required, and may be standalone */
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001197 if (!param) {
1198 param = q;
1199 advance = true;
1200 }
1201
1202 /* Note: a null string is a valid parameter */
1203 if (!param) {
H. Peter Anvinc55702e2018-12-10 22:06:15 -08001204 nasm_nonfatalf(ERR_USAGE, "option `--%s' requires an argument", p);
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -07001205 opt = OPT_BOGUS;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001206 }
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -07001207 break;
1208
1209 case ARG_NO: /* Argument prohibited */
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001210 if (param) {
H. Peter Anvinc55702e2018-12-10 22:06:15 -08001211 nasm_nonfatalf(ERR_USAGE, "option `--%s' does not take an argument", p);
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -07001212 opt = OPT_BOGUS;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001213 }
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -07001214 break;
1215
1216 case ARG_MAYBE: /* Argument permitted, but must be attached with = */
1217 break;
H. Peter Anvin3366e312018-02-07 14:14:36 -08001218 }
1219
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -07001220 switch (opt) {
1221 case OPT_BOGUS:
1222 break; /* We have already errored out */
H. Peter Anvin3366e312018-02-07 14:14:36 -08001223 case OPT_VERSION:
1224 show_version();
1225 break;
1226 case OPT_ABORT_ON_PANIC:
1227 abort_on_panic = true;
1228 break;
H. Peter Anvin98578072018-06-01 18:02:54 -07001229 case OPT_MANGLE:
H. Peter Anvin3366e312018-02-07 14:14:36 -08001230 if (pass == 2)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001231 set_label_mangle(tx->pvt, param);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001232 break;
H. Peter Anvin05990342018-06-11 13:32:42 -07001233 case OPT_INCLUDE:
1234 if (pass == 2)
1235 preproc->pre_include(q);
1236 break;
1237 case OPT_PRAGMA:
1238 if (pass == 2)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001239 preproc->pre_command("pragma", param);
H. Peter Anvin05990342018-06-11 13:32:42 -07001240 break;
1241 case OPT_BEFORE:
1242 if (pass == 2)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001243 preproc->pre_command(NULL, param);
H. Peter Anvin05990342018-06-11 13:32:42 -07001244 break;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001245 case OPT_LIMIT:
H. Peter Anvinbf6230b2018-11-11 13:25:16 -08001246 if (pass == 1)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001247 nasm_set_limit(p+olen, param);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001248 break;
H. Peter Anvin29695c82018-06-14 17:04:32 -07001249 case OPT_KEEP_ALL:
1250 keep_all = true;
1251 break;
H. Peter Anvin (Intel)800c1682018-12-14 12:22:11 -08001252 case OPT_NO_LINE:
1253 pp_noline = true;
1254 break;
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001255 case OPT_DEBUG:
H. Peter Anvin (Intel)5067fde2019-08-09 16:10:17 -07001256 debug_nasm = param ? strtoul(param, NULL, 10) : debug_nasm+1;
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001257 break;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001258 case OPT_HELP:
H. Peter Anvin322bee02019-08-10 01:38:06 -07001259 help(stdout);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001260 exit(0);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001261 default:
1262 panic();
H. Peter Anvine2c80182005-01-15 22:15:51 +00001263 }
H. Peter Anvinc805fd72018-06-12 14:23:05 -07001264
1265 if (eqsave)
1266 *eqsave = '='; /* Restore = argument separator */
1267
H. Peter Anvine2c80182005-01-15 22:15:51 +00001268 break;
1269 }
1270
1271 default:
H. Peter Anvinc55702e2018-12-10 22:06:15 -08001272 nasm_nonfatalf(ERR_USAGE, "unrecognised option `-%c'", p[1]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001273 break;
1274 }
H. Peter Anvin55568c12016-10-03 19:46:49 -07001275 } else if (pass == 2) {
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001276 /* In theory we could allow multiple input files... */
1277 copy_filename(&inname, p, "input");
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001278 }
1279
1280 return advance;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001281}
1282
H. Peter Anvineba20a72002-04-30 20:53:55 +00001283#define ARG_BUF_DELTA 128
1284
H. Peter Anvin55568c12016-10-03 19:46:49 -07001285static void process_respfile(FILE * rfile, int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001286{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001287 char *buffer, *p, *q, *prevarg;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001288 int bufsize, prevargsize;
1289
1290 bufsize = prevargsize = ARG_BUF_DELTA;
1291 buffer = nasm_malloc(ARG_BUF_DELTA);
1292 prevarg = nasm_malloc(ARG_BUF_DELTA);
1293 prevarg[0] = '\0';
1294
H. Peter Anvine2c80182005-01-15 22:15:51 +00001295 while (1) { /* Loop to handle all lines in file */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001296 p = buffer;
1297 while (1) { /* Loop to handle long lines */
1298 q = fgets(p, bufsize - (p - buffer), rfile);
1299 if (!q)
1300 break;
1301 p += strlen(p);
1302 if (p > buffer && p[-1] == '\n')
1303 break;
1304 if (p - buffer > bufsize - 10) {
1305 int offset;
1306 offset = p - buffer;
1307 bufsize += ARG_BUF_DELTA;
1308 buffer = nasm_realloc(buffer, bufsize);
1309 p = buffer + offset;
1310 }
1311 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001312
H. Peter Anvine2c80182005-01-15 22:15:51 +00001313 if (!q && p == buffer) {
1314 if (prevarg[0])
H. Peter Anvin55568c12016-10-03 19:46:49 -07001315 process_arg(prevarg, NULL, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001316 nasm_free(buffer);
1317 nasm_free(prevarg);
1318 return;
1319 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001320
H. Peter Anvine2c80182005-01-15 22:15:51 +00001321 /*
1322 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
1323 * them are present at the end of the line.
1324 */
1325 *(p = &buffer[strcspn(buffer, "\r\n\032")]) = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001326
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001327 while (p > buffer && nasm_isspace(p[-1]))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001328 *--p = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001329
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +04001330 p = nasm_skip_spaces(buffer);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001331
H. Peter Anvin55568c12016-10-03 19:46:49 -07001332 if (process_arg(prevarg, p, pass))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001333 *p = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001334
Charles Crayne192d5b52007-10-18 19:02:42 -07001335 if ((int) strlen(p) > prevargsize - 10) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001336 prevargsize += ARG_BUF_DELTA;
1337 prevarg = nasm_realloc(prevarg, prevargsize);
1338 }
H. Peter Anvindc242712007-11-18 11:55:10 -08001339 strncpy(prevarg, p, prevargsize);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001340 }
1341}
1342
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001343/* Function to process args from a string of args, rather than the
1344 * argv array. Used by the environment variable and response file
1345 * processing.
1346 */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001347static void process_args(char *args, int pass)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001348{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001349 char *p, *q, *arg, *prevarg;
1350 char separator = ' ';
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001351
1352 p = args;
1353 if (*p && *p != '-')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001354 separator = *p++;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001355 arg = NULL;
1356 while (*p) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001357 q = p;
1358 while (*p && *p != separator)
1359 p++;
1360 while (*p == separator)
1361 *p++ = '\0';
1362 prevarg = arg;
1363 arg = q;
H. Peter Anvin55568c12016-10-03 19:46:49 -07001364 if (process_arg(prevarg, arg, pass))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001365 arg = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001366 }
1367 if (arg)
H. Peter Anvin55568c12016-10-03 19:46:49 -07001368 process_arg(arg, NULL, pass);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001369}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001370
H. Peter Anvin55568c12016-10-03 19:46:49 -07001371static void process_response_file(const char *file, int pass)
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001372{
1373 char str[2048];
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001374 FILE *f = nasm_open_read(file, NF_TEXT);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001375 if (!f) {
Cyrill Gorcunovf1964512013-02-15 12:14:06 +04001376 perror(file);
1377 exit(-1);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001378 }
1379 while (fgets(str, sizeof str, f)) {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001380 process_args(str, pass);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001381 }
1382 fclose(f);
1383}
1384
H. Peter Anvin55568c12016-10-03 19:46:49 -07001385static void parse_cmdline(int argc, char **argv, int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001386{
1387 FILE *rfile;
Cyrill Gorcunovf4941892011-07-17 13:55:25 +04001388 char *envreal, *envcopy = NULL, *p;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001389
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001390 /*
1391 * Initialize all the warnings to their default state, including
1392 * warning index 0 used for "always on".
1393 */
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -08001394 memcpy(warning_state, warning_default, sizeof warning_state);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001395
1396 /*
H. Peter Anvinff7ccc02002-05-06 19:41:57 +00001397 * First, process the NASMENV environment variable.
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001398 */
H. Peter Anvinff7ccc02002-05-06 19:41:57 +00001399 envreal = getenv("NASMENV");
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001400 if (envreal) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001401 envcopy = nasm_strdup(envreal);
H. Peter Anvin55568c12016-10-03 19:46:49 -07001402 process_args(envcopy, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001403 nasm_free(envcopy);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001404 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001405
1406 /*
1407 * Now process the actual command line.
1408 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001409 while (--argc) {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001410 bool advance;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001411 argv++;
1412 if (argv[0][0] == '@') {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001413 /*
1414 * We have a response file, so process this as a set of
H. Peter Anvine2c80182005-01-15 22:15:51 +00001415 * arguments like the environment variable. This allows us
1416 * to have multiple arguments on a single line, which is
1417 * different to the -@resp file processing below for regular
1418 * NASM.
1419 */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001420 process_response_file(argv[0]+1, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001421 argc--;
1422 argv++;
1423 }
1424 if (!stopoptions && argv[0][0] == '-' && argv[0][1] == '@') {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001425 p = get_param(argv[0], argc > 1 ? argv[1] : NULL, &advance);
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001426 if (p) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001427 rfile = nasm_open_read(p, NF_TEXT);
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001428 if (rfile) {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001429 process_respfile(rfile, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001430 fclose(rfile);
H. Peter Anvinc55702e2018-12-10 22:06:15 -08001431 } else {
1432 nasm_nonfatalf(ERR_USAGE, "unable to open response file `%s'", p);
1433 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001434 }
1435 } else
H. Peter Anvin55568c12016-10-03 19:46:49 -07001436 advance = process_arg(argv[0], argc > 1 ? argv[1] : NULL, pass);
H. Peter Anvin423e3812007-11-15 17:12:29 -08001437 argv += advance, argc -= advance;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001438 }
1439
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001440 /*
1441 * Look for basic command line typos. This definitely doesn't
1442 * catch all errors, but it might help cases of fumbled fingers.
1443 */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001444 if (pass != 2)
1445 return;
1446
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001447 if (!inname)
H. Peter Anvin77016c82018-12-10 22:29:49 -08001448 nasm_fatalf(ERR_USAGE, "no input file specified");
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001449 else if ((errname && !strcmp(inname, errname)) ||
1450 (outname && !strcmp(inname, outname)) ||
1451 (listname && !strcmp(inname, listname)) ||
1452 (depend_file && !strcmp(inname, depend_file)))
Cyrill Gorcunovc3527dd2018-11-24 22:17:47 +03001453 nasm_fatalf(ERR_USAGE, "will not overwrite input file");
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001454
1455 if (errname) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001456 error_file = nasm_open_write(errname, NF_TEXT);
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001457 if (!error_file) {
1458 error_file = stderr; /* Revert to default! */
H. Peter Anvin77016c82018-12-10 22:29:49 -08001459 nasm_fatalf(ERR_USAGE, "cannot open file `%s' for error messages", errname);
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001460 }
Charles Craynefcce07f2007-09-30 22:15:36 -07001461 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001462}
1463
H. Peter Anvin29651542018-12-18 19:14:40 -08001464static void forward_refs(insn *instruction)
1465{
1466 int i;
1467 struct forwrefinfo *fwinf;
1468
1469 instruction->forw_ref = false;
1470
1471 if (!optimizing.level)
1472 return; /* For -O0 don't bother */
1473
1474 if (!forwref)
1475 return;
1476
1477 if (forwref->lineno != globallineno)
1478 return;
1479
1480 instruction->forw_ref = true;
1481 do {
1482 instruction->oprs[forwref->operand].opflags |= OPFLAG_FORWARD;
1483 forwref = saa_rstruct(forwrefs);
1484 } while (forwref && forwref->lineno == globallineno);
1485
1486 if (!pass_first())
1487 return;
1488
1489 for (i = 0; i < instruction->operands; i++) {
1490 if (instruction->oprs[i].opflags & OPFLAG_FORWARD) {
1491 fwinf = saa_wstruct(forwrefs);
1492 fwinf->lineno = globallineno;
1493 fwinf->operand = i;
1494 }
1495 }
1496}
1497
1498static void process_insn(insn *instruction)
1499{
1500 int32_t n;
1501 int64_t l;
1502
1503 if (!instruction->times)
1504 return; /* Nothing to do... */
1505
1506 nasm_assert(instruction->times > 0);
1507
1508 /*
1509 * NOTE: insn_size() can change instruction->times
1510 * (usually to 1) when called.
1511 */
1512 if (!pass_final()) {
H. Peter Anvina2c1c7d2019-08-10 02:45:41 -07001513 int64_t start = location.offset;
H. Peter Anvin29651542018-12-18 19:14:40 -08001514 for (n = 1; n <= instruction->times; n++) {
1515 l = insn_size(location.segment, location.offset,
1516 globalbits, instruction);
H. Peter Anvin322bee02019-08-10 01:38:06 -07001517 /* l == -1 -> invalid instruction */
1518 if (l != -1)
H. Peter Anvin29651542018-12-18 19:14:40 -08001519 increment_offset(l);
1520 }
H. Peter Anvina2c1c7d2019-08-10 02:45:41 -07001521 if (list_option('p')) {
1522 struct out_data dummy;
1523 memset(&dummy, 0, sizeof dummy);
1524 dummy.type = OUT_RAWDATA; /* Handled specially with .data NULL */
1525 dummy.offset = start;
1526 dummy.size = location.offset - start;
1527 lfmt->output(&dummy);
1528 }
H. Peter Anvin29651542018-12-18 19:14:40 -08001529 } else {
1530 l = assemble(location.segment, location.offset,
1531 globalbits, instruction);
1532 /* We can't get an invalid instruction here */
1533 increment_offset(l);
1534
1535 if (instruction->times > 1) {
H. Peter Anvin0d4d4312019-08-07 00:46:27 -07001536 lfmt->uplevel(LIST_TIMES, instruction->times);
H. Peter Anvin29651542018-12-18 19:14:40 -08001537 for (n = 2; n <= instruction->times; n++) {
1538 l = assemble(location.segment, location.offset,
1539 globalbits, instruction);
1540 increment_offset(l);
1541 }
1542 lfmt->downlevel(LIST_TIMES);
1543 }
1544 }
1545}
1546
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03001547static void assemble_file(const char *fname, struct strlist *depend_list)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001548{
H. Peter Anvinc7131682017-03-07 17:45:01 -08001549 char *line;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001550 insn output_ins;
H. Peter Anvin9c595b62017-03-07 19:44:21 -08001551 uint64_t prev_offset_changed;
H. Peter Anvina3d96d02018-06-15 17:51:39 -07001552 int64_t stall_count = 0; /* Make sure we make forward progress... */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001553
H. Peter Anvina7ecf262018-02-06 14:43:07 -08001554 switch (cmd_sb) {
1555 case 16:
1556 break;
1557 case 32:
1558 if (!iflag_cpu_level_ok(&cmd_cpu, IF_386))
H. Peter Anvinc5136902018-06-15 18:20:17 -07001559 nasm_fatal("command line: 32-bit segment size requires a higher cpu");
H. Peter Anvina7ecf262018-02-06 14:43:07 -08001560 break;
1561 case 64:
1562 if (!iflag_cpu_level_ok(&cmd_cpu, IF_X86_64))
H. Peter Anvinc5136902018-06-15 18:20:17 -07001563 nasm_fatal("command line: 64-bit segment size requires a higher cpu");
H. Peter Anvina7ecf262018-02-06 14:43:07 -08001564 break;
1565 default:
1566 panic();
1567 break;
1568 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001569
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001570 prev_offset_changed = INT64_MAX;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001571
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001572 if (listname && !keep_all) {
1573 /* Remove the list file in case we die before the output pass */
1574 remove(listname);
1575 }
1576
1577 while (!terminate_after_phase && !pass_final()) {
1578 _passn++;
1579 if (pass_type() != PASS_OPT || !global_offset_changed)
1580 _pass_type++;
1581 global_offset_changed = 0;
1582
1583 /*
1584 * Create a warning buffer list unless we are in
1585 * pass 2 (everything will be emitted immediately in pass 2.)
1586 */
1587 if (warn_list) {
1588 if (warn_list->nstr || pass_final())
H. Peter Anvin (Intel)374312c2018-12-14 00:17:13 -08001589 strlist_free(&warn_list);
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03001590 }
H. Peter Anvin (Intel)374312c2018-12-14 00:17:13 -08001591
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001592 if (!pass_final() && !warn_list)
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03001593 warn_list = strlist_alloc(false);
H. Peter Anvin (Intel)374312c2018-12-14 00:17:13 -08001594
H. Peter Anvincac0b192017-03-28 16:12:30 -07001595 globalbits = cmd_sb; /* set 'bits' to command line default */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001596 cpu = cmd_cpu;
H. Peter Anvin59d4ccc2019-08-10 06:45:12 -07001597 if (listname) {
1598 if (pass_final() || list_on_every_pass()) {
1599 active_list_options = list_options;
1600 lfmt->init(listname);
1601 } else if (active_list_options) {
1602 /*
1603 * Looks like we used the list engine on a previous pass,
1604 * but now it is turned off, presumably via %pragma -p
1605 */
1606 lfmt->cleanup();
1607 if (!keep_all)
1608 remove(listname);
1609 active_list_options = 0;
1610 }
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07001611 }
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001612
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001613 in_absolute = false;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001614 if (!pass_first()) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001615 saa_rewind(forwrefs);
1616 forwref = saa_rstruct(forwrefs);
1617 raa_free(offsets);
1618 offsets = raa_init();
1619 }
H. Peter Anvin892c4812018-05-30 14:43:46 -07001620 location.segment = NO_SEG;
1621 location.offset = 0;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001622 if (pass_first())
H. Peter Anvin892c4812018-05-30 14:43:46 -07001623 location.known = true;
1624 ofmt->reset();
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001625 switch_segment(ofmt->section(NULL, &globalbits));
1626 preproc->reset(fname, PP_NORMAL, pass_final() ? depend_list : NULL);
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001627
H. Peter Anvine2c80182005-01-15 22:15:51 +00001628 globallineno = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001629
H. Peter Anvine2c80182005-01-15 22:15:51 +00001630 while ((line = preproc->getline())) {
H. Peter Anvina3d96d02018-06-15 17:51:39 -07001631 if (++globallineno > nasm_limit[LIMIT_LINES])
H. Peter Anvinc5136902018-06-15 18:20:17 -07001632 nasm_fatal("overall line count exceeds the maximum %"PRId64"\n",
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03001633 nasm_limit[LIMIT_LINES]);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001634
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001635 /*
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001636 * Here we parse our directives; this is not handled by the
H. Peter Anvinc7131682017-03-07 17:45:01 -08001637 * main parser.
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001638 */
H. Peter Anvina6e26d92017-03-07 21:32:37 -08001639 if (process_directives(line))
H. Peter Anvinc7131682017-03-07 17:45:01 -08001640 goto end_of_line; /* Just do final cleanup */
H. Peter Anvin62b24d72007-08-29 16:38:05 +00001641
H. Peter Anvinc7131682017-03-07 17:45:01 -08001642 /* Not a directive, or even something that starts with [ */
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001643 parse_line(line, &output_ins);
H. Peter Anvin29651542018-12-18 19:14:40 -08001644 forward_refs(&output_ins);
1645 process_insn(&output_ins);
H. Peter Anvinc7131682017-03-07 17:45:01 -08001646 cleanup_insn(&output_ins);
1647
1648 end_of_line:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001649 nasm_free(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001650 } /* end while (line = preproc->getline... */
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001651
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001652 preproc->cleanup_pass();
1653
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001654 if (global_offset_changed) {
1655 switch (pass_type()) {
1656 case PASS_OPT:
1657 /*
1658 * This is the only pass type that can be executed more
1659 * than once, and therefore has the ability to stall.
1660 */
1661 if (global_offset_changed < prev_offset_changed) {
1662 prev_offset_changed = global_offset_changed;
1663 stall_count = 0;
1664 } else {
1665 stall_count++;
1666 }
1667
1668 if (stall_count > nasm_limit[LIMIT_STALLED] ||
1669 pass_count() >= nasm_limit[LIMIT_PASSES]) {
1670 /* No convergence, almost certainly dead */
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -08001671 nasm_nonfatalf(ERR_UNDEAD,
1672 "unable to find valid values for all labels "
1673 "after %"PRId64" passes; "
1674 "stalled for %"PRId64", giving up.",
1675 pass_count(), stall_count);
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001676 nasm_nonfatalf(ERR_UNDEAD,
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -08001677 "Possible causes: recursive EQUs, macro abuse.");
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001678 }
1679 break;
1680
1681 case PASS_STAB:
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08001682 /*!
1683 *!phase [off] phase error during stabilization
1684 *! warns about symbols having changed values during
1685 *! the second-to-last assembly pass. This is not
1686 *! inherently fatal, but may be a source of bugs.
1687 */
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -08001688 nasm_warn(WARN_PHASE|ERR_UNDEAD,
1689 "phase error during stabilization "
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001690 "pass, hoping for the best");
H. Peter Anvin (Intel)b45c03a2018-06-27 21:03:38 -07001691 break;
1692
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001693 case PASS_FINAL:
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -08001694 nasm_nonfatalf(ERR_UNDEAD,
1695 "phase error during code generation pass");
H. Peter Anvin (Intel)b45c03a2018-06-27 21:03:38 -07001696 break;
1697
1698 default:
1699 /* This is normal, we'll keep going... */
1700 break;
1701 }
1702 }
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -08001703
1704 reset_warnings();
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001705 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001706
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08001707 if (opt_verbose_info && pass_final()) {
1708 /* -On and -Ov switches */
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001709 nasm_info("assembly required 1+%"PRId64"+2 passes\n", pass_count()-3);
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001710 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001711
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08001712 lfmt->cleanup();
H. Peter Anvin59d4ccc2019-08-10 06:45:12 -07001713 strlist_free(&warn_list);
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001714}
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001715
Ed Berosetfa771012002-06-09 20:56:40 +00001716/**
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001717 * get warning index; 0 if this is non-suppressible.
Ed Berosetfa771012002-06-09 20:56:40 +00001718 */
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08001719static size_t warn_index(errflags severity)
Ed Berosetfa771012002-06-09 20:56:40 +00001720{
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001721 size_t index;
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001722
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001723 if ((severity & ERR_MASK) >= ERR_FATAL)
1724 return 0; /* Fatal errors are never suppressible */
1725
H. Peter Anvin (Intel)c3c6cea2018-12-14 13:44:35 -08001726 /* Warnings MUST HAVE a warning category specifier! */
1727 nasm_assert((severity & (ERR_MASK|WARN_MASK)) != ERR_WARNING);
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001728
1729 index = WARN_IDX(severity);
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08001730 nasm_assert(index < WARN_IDX_ALL);
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001731
1732 return index;
Ed Berosetfa771012002-06-09 20:56:40 +00001733}
1734
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08001735static bool skip_this_pass(errflags severity)
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001736{
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001737 errflags type = severity & ERR_MASK;
1738
H. Peter Anvin8f622462017-04-02 19:02:29 -07001739 /*
1740 * See if it's a pass-specific error or warning which should be skipped.
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001741 * We can never skip fatal errors as by definition they cannot be
1742 * resumed from.
H. Peter Anvin8f622462017-04-02 19:02:29 -07001743 */
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001744 if (type >= ERR_FATAL)
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03001745 return false;
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001746
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001747 /*
1748 * ERR_LISTMSG messages are always skipped; the list file
1749 * receives them anyway as this function is not consulted
1750 * for sending to the list file.
1751 */
1752 if (type == ERR_LISTMSG)
1753 return true;
1754
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08001755 /* This message not applicable unless pass_final */
1756 return (severity & ERR_PASS2) && !pass_final();
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001757}
1758
Ed Berosetfa771012002-06-09 20:56:40 +00001759/**
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001760 * check for suppressed message (usually warnings or notes)
1761 *
1762 * @param severity the severity of the warning or error
1763 * @return true if we should abort error/warning printing
1764 */
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08001765static bool is_suppressed(errflags severity)
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001766{
H. Peter Anvin4cf86dd2018-12-27 11:24:17 -08001767 /* Fatal errors must never be suppressed */
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08001768 if ((severity & ERR_MASK) >= ERR_FATAL)
H. Peter Anvin4cf86dd2018-12-27 11:24:17 -08001769 return false;
1770
1771 /* This error/warning is pointless if we are dead anyway */
1772 if ((severity & ERR_UNDEAD) && terminate_after_phase)
1773 return true;
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08001774
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07001775 if (!(warning_state[warn_index(severity)] & WARN_ST_ENABLED))
1776 return true;
1777
1778 if (preproc && !(severity & ERR_PP_LISTMACRO))
1779 return preproc->suppress_error(severity);
1780
1781 return false;
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001782}
1783
1784/**
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08001785 * Return the true error type (the ERR_MASK part) of the given
1786 * severity, accounting for warnings that may need to be promoted to
1787 * error.
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001788 *
1789 * @param severity the severity of the warning or error
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08001790 * @return true if we should error out
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001791 */
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08001792static errflags true_error_type(errflags severity)
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001793{
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08001794 const uint8_t warn_is_err = WARN_ST_ENABLED|WARN_ST_ERROR;
1795 int type;
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001796
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08001797 type = severity & ERR_MASK;
1798
1799 /* Promote warning to error? */
1800 if (type == ERR_WARNING) {
1801 uint8_t state = warning_state[warn_index(severity)];
1802 if ((state & warn_is_err) == warn_is_err)
1803 type = ERR_NONFATAL;
1804 }
1805
1806 return type;
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001807}
1808
H. Peter Anvin6a4353c2019-08-28 18:32:46 -07001809/*
1810 * The various error type prefixes
1811 */
1812static const char * const error_pfx_table[ERR_MASK+1] = {
1813 ";;; ", "debug: ", "info: ", "warning: ",
1814 "error: ", "fatal: ", "critical: ", "panic: "
1815};
1816static const char no_file_name[] = "nasm"; /* What to print if no file name */
1817
1818/*
1819 * For fatal/critical/panic errors, kill this process.
1820 */
1821static fatal_func die_hard(errflags true_type, errflags severity)
1822{
1823 fflush(NULL);
1824
1825 if (true_type == ERR_PANIC && abort_on_panic)
1826 abort();
1827
1828 if (ofile) {
1829 fclose(ofile);
1830 if (!keep_all)
1831 remove(outname);
1832 ofile = NULL;
1833 }
1834
1835 if (severity & ERR_USAGE)
1836 usage();
1837
1838 /* Terminate immediately */
1839 exit(true_type - ERR_FATAL + 1);
1840}
1841
1842/*
1843 * error reporting for critical and panic errors: minimize
1844 * the amount of system dependencies for getting a message out,
1845 * and in particular try to avoid memory allocations.
1846 */
1847fatal_func nasm_verror_critical(errflags severity, const char *fmt, va_list args)
1848{
1849 const char *currentfile = no_file_name;
1850 int32_t lineno = 0;
1851 errflags true_type = severity & ERR_MASK;
1852 static bool been_here = false;
1853
1854 if (unlikely(been_here))
1855 abort(); /* Recursive error... just die */
1856
1857 been_here = true;
1858
1859 if (!(severity & ERR_NOFILE)) {
1860 src_get(&lineno, &currentfile);
1861 if (!currentfile) {
1862 currentfile =
1863 inname && inname[0] ? inname :
1864 outname && outname[0] ? outname :
1865 no_file_name;
1866 lineno = 0;
1867 }
1868 }
1869
1870 fputs(error_pfx_table[severity], error_file);
1871 fputs(currentfile, error_file);
1872 if (lineno) {
1873 fprintf(error_file, "%s%"PRId32"%s",
1874 errfmt->beforeline, lineno, errfmt->afterline);
1875 }
1876 fputs(errfmt->beforemsg, error_file);
1877 vfprintf(error_file, fmt, args);
1878 fputc('\n', error_file);
1879
1880 die_hard(true_type, severity);
1881}
1882
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001883/**
Ed Berosetfa771012002-06-09 20:56:40 +00001884 * common error reporting
1885 * This is the common back end of the error reporting schemes currently
H. Peter Anvin70653092007-10-19 14:42:29 -07001886 * implemented. It prints the nature of the warning and then the
Ed Berosetfa771012002-06-09 20:56:40 +00001887 * specific error message to error_file and may or may not return. It
1888 * doesn't return if the error severity is a "panic" or "debug" type.
H. Peter Anvin70653092007-10-19 14:42:29 -07001889 *
1890 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001891 * @param fmt the printf style format string
1892 */
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07001893void nasm_verror(errflags severity, const char *fmt, va_list args)
Ed Berosetfa771012002-06-09 20:56:40 +00001894{
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001895 char msg[1024];
H. Peter Anvinddb29062018-12-11 00:06:29 -08001896 char warnsuf[64];
1897 char linestr[64];
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001898 const char *pfx;
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08001899 errflags true_type = true_error_type(severity);
H. Peter Anvin77016c82018-12-10 22:29:49 -08001900 const char *currentfile = NULL;
1901 int32_t lineno = 0;
H. Peter Anvin6a4353c2019-08-28 18:32:46 -07001902
1903 if (true_type >= ERR_CRITICAL)
1904 nasm_verror_critical(severity, fmt, args);
H. Peter Anvin77016c82018-12-10 22:29:49 -08001905
H. Peter Anvinc0b32a32018-12-10 22:29:49 -08001906 if (is_suppressed(severity))
H. Peter Anvin77016c82018-12-10 22:29:49 -08001907 return;
1908
1909 if (!(severity & ERR_NOFILE)) {
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03001910 src_get(&lineno, &currentfile);
H. Peter Anvin77016c82018-12-10 22:29:49 -08001911 if (!currentfile) {
H. Peter Anvin6a4353c2019-08-28 18:32:46 -07001912 currentfile =
H. Peter Anvin77016c82018-12-10 22:29:49 -08001913 inname && inname[0] ? inname :
1914 outname && outname[0] ? outname :
1915 NULL;
H. Peter Anvin6a4353c2019-08-28 18:32:46 -07001916 lineno = 0;
H. Peter Anvin77016c82018-12-10 22:29:49 -08001917 }
1918 }
H. Peter Anvin323fcff2009-07-12 12:04:56 -07001919
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08001920 if (severity & ERR_NO_SEVERITY)
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001921 pfx = "";
H. Peter Anvine2f5edb2018-12-11 00:06:29 -08001922 else
H. Peter Anvin6a4353c2019-08-28 18:32:46 -07001923 pfx = error_pfx_table[true_type];
H. Peter Anvinddb29062018-12-11 00:06:29 -08001924
1925 vsnprintf(msg, sizeof msg, fmt, args);
1926 *warnsuf = 0;
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001927 if ((severity & (ERR_MASK|ERR_HERE|ERR_PP_LISTMACRO)) == ERR_WARNING) {
1928 /*
1929 * It's a warning without ERR_HERE defined, and we are not already
1930 * unwinding the macros that led us here.
1931 */
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03001932 snprintf(warnsuf, sizeof warnsuf, " [-w+%s%s]",
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08001933 (true_type >= ERR_NONFATAL) ? "error=" : "",
1934 warning_name[warn_index(severity)]);
H. Peter Anvin934f0472016-05-09 12:00:19 -07001935 }
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001936
H. Peter Anvinddb29062018-12-11 00:06:29 -08001937 *linestr = 0;
1938 if (lineno) {
1939 snprintf(linestr, sizeof linestr, "%s%"PRId32"%s",
1940 errfmt->beforeline, lineno, errfmt->afterline);
1941 }
1942
H. Peter Anvin77016c82018-12-10 22:29:49 -08001943 if (!skip_this_pass(severity)) {
H. Peter Anvin6a4353c2019-08-28 18:32:46 -07001944 const char *file = currentfile ? currentfile : no_file_name;
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001945 const char *here = "";
1946
1947 if (severity & ERR_HERE)
1948 here = currentfile ? " here" : " in an unknown location";
H. Peter Anvin (Intel)374312c2018-12-14 00:17:13 -08001949
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08001950 if (warn_list && true_type < ERR_NONFATAL &&
1951 !(pass_first() && (severity & ERR_PASS1)))
1952 {
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03001953 /*
1954 * Buffer up warnings until we either get an error
1955 * or we are on the code-generation pass.
1956 */
1957 strlist_printf(warn_list, "%s%s%s%s%s%s%s",
1958 file, linestr, errfmt->beforemsg,
1959 pfx, msg, here, warnsuf);
1960 } else {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08001961 /*
1962 * If we have buffered warnings, and this is a non-warning,
1963 * output them now.
1964 */
1965 if (true_type >= ERR_NONFATAL && warn_list) {
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03001966 strlist_write(warn_list, "\n", error_file);
1967 strlist_free(&warn_list);
1968 }
H. Peter Anvin (Intel)374312c2018-12-14 00:17:13 -08001969
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03001970 fprintf(error_file, "%s%s%s%s%s%s%s\n",
1971 file, linestr, errfmt->beforemsg,
1972 pfx, msg, here, warnsuf);
H. Peter Anvin (Intel)374312c2018-12-14 00:17:13 -08001973
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03001974 }
H. Peter Anvin77016c82018-12-10 22:29:49 -08001975 }
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001976
H. Peter Anvin4def1a82016-05-09 13:59:44 -07001977 /* Are we recursing from error_list_macros? */
1978 if (severity & ERR_PP_LISTMACRO)
Cyrill Gorcunov988cc122018-12-15 23:44:46 +03001979 return;
H. Peter Anvin4def1a82016-05-09 13:59:44 -07001980
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001981 /*
1982 * Don't suppress this with skip_this_pass(), or we don't get
H. Peter Anvin934f0472016-05-09 12:00:19 -07001983 * pass1 or preprocessor warnings in the list file
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001984 */
H. Peter Anvinddb29062018-12-11 00:06:29 -08001985 if (severity & ERR_HERE) {
1986 if (lineno)
1987 lfmt->error(severity, "%s%s at %s:%"PRId32"%s",
1988 pfx, msg, currentfile, lineno, warnsuf);
1989 else if (currentfile)
1990 lfmt->error(severity, "%s%s in file %s%s",
1991 pfx, msg, currentfile, warnsuf);
1992 else
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07001993 lfmt->error(severity, "%s%s in an unknown location%s",
H. Peter Anvinddb29062018-12-11 00:06:29 -08001994 pfx, msg, warnsuf);
1995 } else {
1996 lfmt->error(severity, "%s%s%s", pfx, msg, warnsuf);
1997 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001998
H. Peter Anvin8f622462017-04-02 19:02:29 -07001999 if (skip_this_pass(severity))
2000 return;
2001
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -07002002 /* error_list_macros can for obvious reasons not work with ERR_HERE */
2003 if (!(severity & ERR_HERE))
H. Peter Anvin2201ceb2019-08-27 17:19:07 -07002004 if (preproc)
2005 preproc->error_list_macros(severity);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07002006
H. Peter Anvin6a4353c2019-08-28 18:32:46 -07002007 if (true_type >= ERR_FATAL)
2008 die_hard(true_type, severity);
2009 else if (true_type >= ERR_NONFATAL)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002010 terminate_after_phase = true;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002011}
2012
H. Peter Anvin734b1882002-04-30 21:01:08 +00002013static void usage(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002014{
H. Peter Anvin355bfb82019-08-10 01:55:00 -07002015 fprintf(error_file, "Type %s -h for help.\n", _progname);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002016}
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002017
H. Peter Anvin322bee02019-08-10 01:38:06 -07002018static void help(FILE *out)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002019{
2020 int i;
2021
H. Peter Anvin322bee02019-08-10 01:38:06 -07002022 fprintf(out,
H. Peter Anvin355bfb82019-08-10 01:55:00 -07002023 "Usage: %s [-@ response_file] [options...] [--] filename\n"
2024 " %s -v (or --v)\n",
H. Peter Anvin322bee02019-08-10 01:38:06 -07002025 _progname, _progname);
2026 fputs(
2027 "\n"
H. Peter Anvin355bfb82019-08-10 01:55:00 -07002028 "Options (values in brackets indicate defaults):\n"
H. Peter Anvin322bee02019-08-10 01:38:06 -07002029 "\n"
2030 " -h show this text and exit (also --help)\n"
H. Peter Anvin355bfb82019-08-10 01:55:00 -07002031 " -v (or --v) print the NASM version number and exit\n"
2032 " -@ file response file; one command line option per line\n"
H. Peter Anvin322bee02019-08-10 01:38:06 -07002033 "\n"
2034 " -o outfile write output to outfile\n"
2035 " --keep-all output files will not be removed even if an error happens\n"
2036 "\n"
2037 " -Xformat specifiy error reporting format (gnu or vc)\n"
2038 " -s redirect error messages to stdout\n"
2039 " -Zfile redirect error messages to file\n"
2040 "\n"
2041 " -M generate Makefile dependencies on stdout\n"
2042 " -MG d:o, missing files assumed generated\n"
2043 " -MF file set Makefile dependency file\n"
2044 " -MD file assemble and generate dependencies\n"
2045 " -MT file dependency target name\n"
2046 " -MQ file dependency target name (quoted)\n"
2047 " -MP emit phony targets\n"
2048 "\n"
2049 " -f format select output file format\n"
2050 , out);
2051 ofmt_list(ofmt, out);
2052 fputs(
2053 "\n"
2054 " -g generate debugging information\n"
2055 " -F format select a debugging format (output format dependent)\n"
2056 " -gformat same as -g -F format\n"
2057 , out);
2058 dfmt_list(out);
2059 fputs(
2060 "\n"
2061 " -l listfile write listing to a list file\n"
2062 " -Lflags... add optional information to the list file\n"
H. Peter Anvin6686de22019-08-10 05:33:14 -07002063 " -Lb show builtin macro packages (standard and %use)\n"
H. Peter Anvin322bee02019-08-10 01:38:06 -07002064 " -Ld show byte and repeat counts in decimal, not hex\n"
2065 " -Le show the preprocessed output\n"
H. Peter Anvin6686de22019-08-10 05:33:14 -07002066 " -Lf ignore .nolist (force output)\n"
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07002067 " -Lm show multi-line macro calls with expanded parmeters\n"
H. Peter Anvin322bee02019-08-10 01:38:06 -07002068 " -Lp output a list file every pass, in case of errors\n"
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07002069 " -Ls show all single-line macro definitions\n"
H. Peter Anvin (Intel)0741eb62019-10-23 12:45:08 -07002070 " -Lw flush the output after every line\n"
H. Peter Anvin (Intel)b8362132019-08-19 13:09:46 -07002071 " -L+ enable all listing options (very verbose!)\n"
H. Peter Anvin322bee02019-08-10 01:38:06 -07002072 "\n"
2073 " -Oflags... optimize opcodes, immediates and branch offsets\n"
2074 " -O0 no optimization\n"
2075 " -O1 minimal optimization\n"
2076 " -Ox multipass optimization (default)\n"
2077 " -Ov display the number of passes executed at the end\n"
2078 " -t assemble in limited SciTech TASM compatible mode\n"
2079 "\n"
2080 " -E (or -e) preprocess only (writes output to stdout by default)\n"
2081 " -a don't preprocess (assemble only)\n"
2082 " -Ipath add a pathname to the include file path\n"
2083 " -Pfile pre-include a file (also --include)\n"
2084 " -Dmacro[=str] pre-define a macro\n"
2085 " -Umacro undefine a macro\n"
2086 " --pragma str pre-executes a specific %%pragma\n"
2087 " --before str add line (usually a preprocessor statement) before the input\n"
2088 " --no-line ignore %line directives in input\n"
2089 "\n"
2090 " --prefix str prepend the given string to the names of all extern,\n"
2091 " common and global symbols (also --gprefix)\n"
2092 " --suffix str append the given string to the names of all extern,\n"
2093 " common and global symbols (also --gprefix)\n"
2094 " --lprefix str prepend the given string to local symbols\n"
2095 " --lpostfix str append the given string to local symbols\n"
2096 "\n"
2097 " -w+x enable warning x (also -Wx)\n"
2098 " -w-x disable warning x (also -Wno-x)\n"
2099 " -w[+-]error promote all warnings to errors (also -Werror)\n"
2100 " -w[+-]error=x promote warning x to errors (also -Werror=x)\n"
2101 , out);
2102
2103 fprintf(out, " %-20s %s\n",
2104 warning_name[WARN_IDX_ALL], warning_help[WARN_IDX_ALL]);
2105
2106 for (i = 1; i < WARN_IDX_ALL; i++) {
2107 const char *me = warning_name[i];
2108 const char *prev = warning_name[i-1];
2109 const char *next = warning_name[i+1];
2110
2111 if (prev) {
2112 int prev_len = strlen(prev);
2113 const char *dash = me;
2114
2115 while ((dash = strchr(dash+1, '-'))) {
2116 int prefix_len = dash - me; /* Not including final dash */
2117 if (strncmp(next, me, prefix_len+1)) {
2118 /* Only one or last option with this prefix */
2119 break;
2120 }
2121 if (prefix_len >= prev_len ||
2122 strncmp(prev, me, prefix_len) ||
2123 (prev[prefix_len] != '-' && prev[prefix_len] != '\0')) {
2124 /* This prefix is different from the previous option */
2125 fprintf(out, " %-20.*s all warnings prefixed with \"%.*s\"\n",
2126 prefix_len, me, prefix_len+1, me);
2127 }
2128 }
2129 }
2130
2131 fprintf(out, " %-20s %s%s\n",
2132 warning_name[i], warning_help[i],
2133 (warning_default[i] & WARN_ST_ERROR) ? " [error]" :
2134 (warning_default[i] & WARN_ST_ENABLED) ? " [on]" : " [off]");
2135 }
2136
2137 fputs(
2138 "\n"
2139 " --limit-X val set execution limit X\n"
2140 , out);
2141
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002142
2143 for (i = 0; i <= LIMIT_MAX; i++) {
H. Peter Anvin322bee02019-08-10 01:38:06 -07002144 fprintf(out, " %-20s %s [",
2145 limit_info[i].name, limit_info[i].help);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002146 if (nasm_limit[i] < LIMIT_MAX_VAL) {
H. Peter Anvin322bee02019-08-10 01:38:06 -07002147 fprintf(out, "%"PRId64"]\n", nasm_limit[i]);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002148 } else {
H. Peter Anvin322bee02019-08-10 01:38:06 -07002149 fputs("unlimited]\n", out);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002150 }
2151 }
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002152}