blob: f1283e4c8a56ebd3ebc11a577668910024041daa [file] [log] [blame]
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07001/* ----------------------------------------------------------------------- *
H. Peter Anvin323fcff2009-07-12 12:04:56 -07002 *
H. Peter Anvin3366e312018-02-07 14:14:36 -08003 * Copyright 1996-2018 The NASM Authors - All Rights Reserved
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07004 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00006 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07007 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
H. Peter Anvin323fcff2009-07-12 12:04:56 -070017 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -070018 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 * ----------------------------------------------------------------------- */
33
H. Peter Anvin323fcff2009-07-12 12:04:56 -070034/*
H. Peter Anvin9e6747c2009-06-28 17:13:04 -070035 * The Netwide Assembler main program module
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000036 */
37
H. Peter Anvinfe501952007-10-02 21:53:51 -070038#include "compiler.h"
39
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000040#include <stdio.h>
41#include <stdarg.h>
42#include <stdlib.h>
43#include <string.h>
44#include <ctype.h>
H. Peter Anvinfd7dd112007-10-10 14:06:59 -070045#include <limits.h>
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000046
47#include "nasm.h"
48#include "nasmlib.h"
H. Peter Anvinb20bc732017-03-07 19:23:03 -080049#include "error.h"
H. Peter Anvin1803ded2008-06-09 17:32:43 -070050#include "saa.h"
H. Peter Anvinfcb89092008-06-09 17:40:16 -070051#include "raa.h"
H. Peter Anvinf6c9e652007-10-16 14:40:27 -070052#include "float.h"
H. Peter Anvin74cc5e52007-08-30 22:35:34 +000053#include "stdscan.h"
H. Peter Anvinaf535c12002-04-30 20:59:21 +000054#include "insns.h"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000055#include "preproc.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000056#include "parser.h"
H. Peter Anvin76690a12002-04-30 20:52:49 +000057#include "eval.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000058#include "assemble.h"
59#include "labels.h"
H. Peter Anvine1f985c2016-05-25 12:06:29 -070060#include "outform.h"
H. Peter Anvin6768eb72002-04-30 20:52:26 +000061#include "listing.h"
Cyrill Gorcunov08359152013-11-09 22:16:11 +040062#include "iflag.h"
H. Peter Anvin2bc0ab32016-03-08 02:17:36 -080063#include "ver.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000064
H. Peter Anvin31387b22010-07-15 18:28:52 -070065/*
66 * This is the maximum number of optimization passes to do. If we ever
67 * find a case where the optimizer doesn't naturally converge, we might
68 * have to drop this value so the assembler doesn't appear to just hang.
69 */
70#define MAX_OPTIMIZE (INT_MAX >> 1)
71
H. Peter Anvine2c80182005-01-15 22:15:51 +000072struct forwrefinfo { /* info held on forward refs. */
H. Peter Anvineba20a72002-04-30 20:53:55 +000073 int lineno;
74 int operand;
75};
76
H. Peter Anvin55568c12016-10-03 19:46:49 -070077static void parse_cmdline(int, char **, int);
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -070078static void assemble_file(const char *, StrList *);
H. Peter Anvin130736c2016-02-17 20:27:41 -080079static bool is_suppressed_warning(int severity);
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -080080static bool skip_this_pass(int severity);
H. Peter Anvin9bd15062009-07-18 21:07:17 -040081static void nasm_verror_gnu(int severity, const char *fmt, va_list args);
82static void nasm_verror_vc(int severity, const char *fmt, va_list args);
83static void nasm_verror_common(int severity, const char *fmt, va_list args);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000084static void usage(void);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -070085static void help(char xopt);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000086
H. Peter Anvin283b3fb2016-03-07 23:18:30 -080087static bool using_debug_info, opt_verbose_info;
88static const char *debug_format;
89
H. Peter Anvin3366e312018-02-07 14:14:36 -080090#ifndef ABORT_ON_PANIC
91# define ABORT_ON_PANIC 0
92#endif
93static bool abort_on_panic = ABORT_ON_PANIC;
H. Peter Anvin29695c82018-06-14 17:04:32 -070094static bool keep_all;
H. Peter Anvin3366e312018-02-07 14:14:36 -080095
H. Peter Anvin6867acc2007-10-10 14:58:45 -070096bool tasm_compatible_mode = false;
H. Peter Anvina3d96d02018-06-15 17:51:39 -070097int pass0;
98int64_t passn;
H. Peter Anvinc7131682017-03-07 17:45:01 -080099static int pass1, pass2; /* XXX: Get rid of these, they are redundant */
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +0000100int globalrel = 0;
Jin Kyu Songb287ff02013-12-04 20:05:55 -0800101int globalbnd = 0;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000102
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700103struct compile_time official_compile_time;
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800104
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800105const char *inname;
106const char *outname;
107static const char *listname;
108static const char *errname;
109
H. Peter Anvina3d96d02018-06-15 17:51:39 -0700110static int64_t globallineno; /* for forward-reference tracking */
Chang S. Baef0ceb1e2018-05-02 08:07:53 -0700111
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000112/* static int pass = 0; */
H. Peter Anvin338656c2016-02-17 20:59:22 -0800113const struct ofmt *ofmt = &OF_DEFAULT;
114const struct ofmt_alias *ofmt_alias = NULL;
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400115const struct dfmt *dfmt;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000116
H. Peter Anvine2c80182005-01-15 22:15:51 +0000117static FILE *error_file; /* Where to write error messages */
H. Peter Anvin620515a2002-04-30 20:57:38 +0000118
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400119FILE *ofile = NULL;
Chang S. Baea5786342018-08-15 23:22:21 +0300120struct optimization optimizing =
121 { MAX_OPTIMIZE, OPTIM_ALL_ENABLED }; /* number of optimization passes to take */
Martin Lindhe8cc93f52016-11-16 16:48:13 +0100122static int cmd_sb = 16; /* by default */
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400123
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800124iflag_t cpu;
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400125static iflag_t cmd_cpu;
126
H. Peter Anvincd7893d2016-02-18 01:25:46 -0800127struct location location;
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800128bool in_absolute; /* Flag we are in ABSOLUTE seg */
129struct location absolute; /* Segment/offset inside ABSOLUTE */
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000130
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000131static struct RAA *offsets;
H. Peter Anvinea838272002-04-30 20:51:53 +0000132
H. Peter Anvine2c80182005-01-15 22:15:51 +0000133static struct SAA *forwrefs; /* keep track of forward references */
H. Peter Anvin9d637df2007-10-04 13:42:56 -0700134static const struct forwrefinfo *forwref;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000135
H. Peter Anvine7469712016-02-18 02:20:59 -0800136static const struct preproc_ops *preproc;
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800137static StrList *include_path;
Cyrill Gorcunov86b2ad02011-07-01 10:38:25 +0400138
Cyrill Gorcunove9fc88c2014-06-23 02:22:02 +0400139#define OP_NORMAL (1u << 0)
140#define OP_PREPROCESS (1u << 1)
141#define OP_DEPEND (1u << 2)
142
143static unsigned int operating_mode;
Cyrill Gorcunov3ed32cb2014-06-22 20:53:12 +0400144
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700145/* Dependency flags */
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700146static bool depend_emit_phony = false;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700147static bool depend_missing_ok = false;
148static const char *depend_target = NULL;
149static const char *depend_file = NULL;
H. Peter Anvina771be82017-08-16 14:53:18 -0700150StrList *depend_list;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000151
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700152static bool want_usage;
153static bool terminate_after_phase;
H. Peter Anvin130736c2016-02-17 20:27:41 -0800154bool user_nolist = false;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000155
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700156static char *quote_for_pmake(const char *str);
157static char *quote_for_wmake(const char *str);
158static char *(*quote_for_make)(const char *) = quote_for_pmake;
H. Peter Anvin55340992012-09-09 17:09:00 -0700159
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700160/*
161 * Execution limits that can be set via a command-line option or %pragma
162 */
163
H. Peter Anvina3d96d02018-06-15 17:51:39 -0700164#define LIMIT_MAX_VAL (INT64_MAX >> 1) /* Effectively unlimited */
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700165
H. Peter Anvina3d96d02018-06-15 17:51:39 -0700166int64_t nasm_limit[LIMIT_MAX+1] =
167{ LIMIT_MAX_VAL, 1000, 1000000, 1000000, 1000000, 2000000000 };
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700168
169struct limit_info {
170 const char *name;
171 const char *help;
172};
173static const struct limit_info limit_info[LIMIT_MAX+1] = {
174 { "passes", "total number of passes" },
175 { "stalled-passes", "number of passes without forward progress" },
176 { "macro-levels", "levels of macro expansion"},
177 { "rep", "%rep count" },
H. Peter Anvina3d96d02018-06-15 17:51:39 -0700178 { "eval", "expression evaluation descent"},
179 { "lines", "total source lines processed"}
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700180};
181
H. Peter Anvinc805fd72018-06-12 14:23:05 -0700182enum directive_result
183nasm_set_limit(const char *limit, const char *valstr)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700184{
185 int i;
186 int64_t val;
187 bool rn_error;
H. Peter Anvinc805fd72018-06-12 14:23:05 -0700188 int errlevel;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700189
190 for (i = 0; i <= LIMIT_MAX; i++) {
191 if (!nasm_stricmp(limit, limit_info[i].name))
192 break;
193 }
194 if (i > LIMIT_MAX) {
H. Peter Anvinc805fd72018-06-12 14:23:05 -0700195 if (passn == 0)
196 errlevel = ERR_WARNING|ERR_NOFILE|ERR_USAGE;
197 else
198 errlevel = ERR_WARNING|ERR_PASS1|ERR_WARN_UNKNOWN_PRAGMA;
199 nasm_error(errlevel, "unknown limit: `%s'", limit);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700200 return DIRR_ERROR;
201 }
202
203 if (!nasm_stricmp(valstr, "unlimited")) {
204 val = LIMIT_MAX_VAL;
205 } else {
206 val = readnum(valstr, &rn_error);
207 if (rn_error || val < 0) {
H. Peter Anvinc805fd72018-06-12 14:23:05 -0700208 if (passn == 0)
209 errlevel = ERR_WARNING|ERR_NOFILE|ERR_USAGE;
210 else
211 errlevel = ERR_WARNING|ERR_PASS1|ERR_WARN_BAD_PRAGMA;
212 nasm_error(errlevel, "invalid limit value: `%s'", limit);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700213 return DIRR_ERROR;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700214 }
H. Peter Anvinc805fd72018-06-12 14:23:05 -0700215 if (val > LIMIT_MAX_VAL)
216 val = LIMIT_MAX_VAL;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700217 }
218
219 nasm_limit[i] = val;
220 return DIRR_OK;
221}
222
H. Peter Anvin892c4812018-05-30 14:43:46 -0700223int64_t switch_segment(int32_t segment)
Cyrill Gorcunov190232f2013-02-15 12:35:04 +0400224{
H. Peter Anvin892c4812018-05-30 14:43:46 -0700225 location.segment = segment;
226 if (segment == NO_SEG) {
227 location.offset = absolute.offset;
228 in_absolute = true;
229 } else {
230 location.offset = raa_read(offsets, segment);
231 in_absolute = false;
232 }
233 return location.offset;
Cyrill Gorcunov190232f2013-02-15 12:35:04 +0400234}
235
236static void set_curr_offs(int64_t l_off)
237{
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800238 if (in_absolute)
239 absolute.offset = l_off;
Cyrill Gorcunov190232f2013-02-15 12:35:04 +0400240 else
241 offsets = raa_write(offsets, location.segment, l_off);
242}
243
H. Peter Anvin892c4812018-05-30 14:43:46 -0700244static void increment_offset(int64_t delta)
245{
246 if (unlikely(delta == 0))
247 return;
248
249 location.offset += delta;
250 set_curr_offs(location.offset);
251}
252
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000253static void nasm_fputs(const char *line, FILE * outfile)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000254{
H. Peter Anvin310b3e12002-05-14 22:38:55 +0000255 if (outfile) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000256 fputs(line, outfile);
H. Peter Anvind1fb15c2007-11-13 09:37:59 -0800257 putc('\n', outfile);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000258 } else
H. Peter Anvine2c80182005-01-15 22:15:51 +0000259 puts(line);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000260}
261
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800262/*
263 * Define system-defined macros that are not part of
264 * macros/standard.mac.
265 */
266static void define_macros(void)
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800267{
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700268 const struct compile_time * const oct = &official_compile_time;
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800269 char temp[128];
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800270
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700271 if (oct->have_local) {
272 strftime(temp, sizeof temp, "__DATE__=\"%Y-%m-%d\"", &oct->local);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400273 preproc->pre_define(temp);
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700274 strftime(temp, sizeof temp, "__DATE_NUM__=%Y%m%d", &oct->local);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400275 preproc->pre_define(temp);
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700276 strftime(temp, sizeof temp, "__TIME__=\"%H:%M:%S\"", &oct->local);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400277 preproc->pre_define(temp);
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700278 strftime(temp, sizeof temp, "__TIME_NUM__=%H%M%S", &oct->local);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400279 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800280 }
281
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700282 if (oct->have_gm) {
283 strftime(temp, sizeof temp, "__UTC_DATE__=\"%Y-%m-%d\"", &oct->gm);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400284 preproc->pre_define(temp);
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700285 strftime(temp, sizeof temp, "__UTC_DATE_NUM__=%Y%m%d", &oct->gm);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400286 preproc->pre_define(temp);
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700287 strftime(temp, sizeof temp, "__UTC_TIME__=\"%H:%M:%S\"", &oct->gm);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400288 preproc->pre_define(temp);
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700289 strftime(temp, sizeof temp, "__UTC_TIME_NUM__=%H%M%S", &oct->gm);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400290 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800291 }
H. Peter Anvind85d2502008-05-04 17:53:31 -0700292
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700293 if (oct->have_posix) {
294 snprintf(temp, sizeof temp, "__POSIX_TIME__=%"PRId64, oct->posix);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400295 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800296 }
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800297
Cyrill Gorcunovc1936da2011-04-06 18:32:15 +0400298 /*
299 * In case if output format is defined by alias
300 * we have to put shortname of the alias itself here
301 * otherwise ABI backward compatibility gets broken.
302 */
Cyrill Gorcunov69ce7502010-07-12 15:19:17 +0400303 snprintf(temp, sizeof(temp), "__OUTPUT_FORMAT__=%s",
Cyrill Gorcunovc1936da2011-04-06 18:32:15 +0400304 ofmt_alias ? ofmt_alias->shortname : ofmt->shortname);
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +0400305 preproc->pre_define(temp);
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800306
307 /*
308 * Output-format specific macros.
309 */
310 if (ofmt->stdmac)
311 preproc->extra_stdmac(ofmt->stdmac);
312
313 /*
314 * Debug format, if any
315 */
316 if (dfmt != &null_debug_form) {
317 snprintf(temp, sizeof(temp), "__DEBUG_FORMAT__=%s", dfmt->shortname);
318 preproc->pre_define(temp);
319 }
320}
321
322/*
323 * Initialize the preprocessor, set up the include path, and define
324 * the system-included macros. This is called between passes 1 and 2
325 * of parsing the command options; ofmt and dfmt are defined at this
326 * point.
327 *
328 * Command-line specified preprocessor directives (-p, -d, -u,
329 * --pragma, --before) are processed after this function.
330 */
Cyrill Gorcunovfeabd742018-11-13 01:23:47 +0300331static void preproc_init(StrList **ipath)
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800332{
Cyrill Gorcunove3588512018-11-13 01:09:27 +0300333 struct strlist_entry *l;
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800334
335 preproc->init();
336 define_macros();
Cyrill Gorcunove3588512018-11-13 01:09:27 +0300337
Cyrill Gorcunovfeabd742018-11-13 01:23:47 +0300338 list_for_each(l, (*ipath)->head)
Cyrill Gorcunove3588512018-11-13 01:09:27 +0300339 preproc->include_path(l->str);
Cyrill Gorcunovfeabd742018-11-13 01:23:47 +0300340
341 strlist_free(*ipath);
342 *ipath = strlist_allocate();
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800343}
344
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700345static void emit_dependencies(StrList *list)
346{
347 FILE *deps;
348 int linepos, len;
H. Peter Anvinf05034f2017-08-16 22:08:36 -0700349 bool wmake = (quote_for_make == quote_for_wmake);
350 const char *wrapstr, *nulltarget;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -0700351 struct strlist_entry *l;
352
353 if (!list)
354 return;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700355
H. Peter Anvinf05034f2017-08-16 22:08:36 -0700356 wrapstr = wmake ? " &\n " : " \\\n ";
357 nulltarget = wmake ? "\t%null\n" : "";
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700358
359 if (depend_file && strcmp(depend_file, "-")) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -0700360 deps = nasm_open_write(depend_file, NF_TEXT);
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400361 if (!deps) {
362 nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
363 "unable to write dependency file `%s'", depend_file);
364 return;
365 }
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700366 } else {
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400367 deps = stdout;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700368 }
H. Peter Anvin323fcff2009-07-12 12:04:56 -0700369
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700370 linepos = fprintf(deps, "%s :", depend_target);
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -0700371 list_for_each(l, list->head) {
H. Peter Anvin55340992012-09-09 17:09:00 -0700372 char *file = quote_for_make(l->str);
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400373 len = strlen(file);
374 if (linepos + len > 62 && linepos > 1) {
H. Peter Anvinf05034f2017-08-16 22:08:36 -0700375 fputs(wrapstr, deps);
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400376 linepos = 1;
377 }
378 fprintf(deps, " %s", file);
379 linepos += len+1;
H. Peter Anvin55340992012-09-09 17:09:00 -0700380 nasm_free(file);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700381 }
382 fprintf(deps, "\n\n");
H. Peter Anvin323fcff2009-07-12 12:04:56 -0700383
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -0700384 list_for_each(l, list->head) {
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700385 if (depend_emit_phony) {
386 char *file = quote_for_make(l->str);
H. Peter Anvinf05034f2017-08-16 22:08:36 -0700387 fprintf(deps, "%s :\n%s\n", file, nulltarget);
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700388 nasm_free(file);
389 }
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700390 }
391
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -0700392 strlist_free(list);
393
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700394 if (deps != stdout)
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400395 fclose(deps);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700396}
397
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700398/* Convert a struct tm to a POSIX-style time constant */
399static int64_t make_posix_time(const struct tm *tm)
400{
401 int64_t t;
402 int64_t y = tm->tm_year;
403
404 /* See IEEE 1003.1:2004, section 4.14 */
405
406 t = (y-70)*365 + (y-69)/4 - (y-1)/100 + (y+299)/400;
407 t += tm->tm_yday;
408 t *= 24;
409 t += tm->tm_hour;
410 t *= 60;
411 t += tm->tm_min;
412 t *= 60;
413 t += tm->tm_sec;
414
415 return t;
416}
417
418static void timestamp(void)
419{
420 struct compile_time * const oct = &official_compile_time;
421 const struct tm *tp, *best_gm;
422
423 time(&oct->t);
424
425 best_gm = NULL;
426
427 tp = localtime(&oct->t);
428 if (tp) {
429 oct->local = *tp;
430 best_gm = &oct->local;
431 oct->have_local = true;
432 }
433
434 tp = gmtime(&oct->t);
435 if (tp) {
436 oct->gm = *tp;
437 best_gm = &oct->gm;
438 oct->have_gm = true;
439 if (!oct->have_local)
440 oct->local = oct->gm;
441 } else {
442 oct->gm = oct->local;
443 }
444
445 if (best_gm) {
446 oct->posix = make_posix_time(best_gm);
447 oct->have_posix = true;
448 }
449}
450
H. Peter Anvin038d8612007-04-12 16:54:50 +0000451int main(int argc, char **argv)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000452{
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700453 timestamp();
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800454
H. Peter Anvina7ecf262018-02-06 14:43:07 -0800455 iflag_set_default_cpu(&cpu);
456 iflag_set_default_cpu(&cmd_cpu);
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400457
Cyrill Gorcunove3588512018-11-13 01:09:27 +0300458 include_path = strlist_allocate();
459
Charles Crayne2581c862008-09-10 19:21:52 -0700460 pass0 = 0;
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700461 want_usage = terminate_after_phase = false;
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400462 nasm_set_verror(nasm_verror_gnu);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000463
H. Peter Anvin97e15752007-09-25 08:47:47 -0700464 error_file = stderr;
465
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -0700466 tolower_init();
H. Peter Anvin274cda82016-05-10 02:56:29 -0700467 src_init();
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -0700468
H. Peter Anvin, Intel87d9e622018-06-25 12:58:49 -0700469 /*
470 * We must call init_labels() before the command line parsing,
471 * because we may be setting prefixes/suffixes from the command
472 * line.
473 */
474 init_labels();
475
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000476 offsets = raa_init();
Keith Kaniosb7a89542007-04-12 02:40:54 +0000477 forwrefs = saa_init((int32_t)sizeof(struct forwrefinfo));
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000478
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000479 preproc = &nasmpp;
Cyrill Gorcunov3ed32cb2014-06-22 20:53:12 +0400480 operating_mode = OP_NORMAL;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000481
H. Peter Anvin55568c12016-10-03 19:46:49 -0700482 parse_cmdline(argc, argv, 1);
483 if (terminate_after_phase) {
484 if (want_usage)
485 usage();
486 return 1;
487 }
488
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800489 /* At this point we have ofmt and the name of the desired debug format */
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800490 if (!using_debug_info) {
491 /* No debug info, redirect to the null backend (empty stubs) */
H. Peter Anvina7bc15d2016-02-17 20:55:08 -0800492 dfmt = &null_debug_form;
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800493 } else if (!debug_format) {
494 /* Default debug format for this backend */
H. Peter Anvina7bc15d2016-02-17 20:55:08 -0800495 dfmt = ofmt->default_dfmt;
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800496 } else {
497 dfmt = dfmt_find(ofmt, debug_format);
498 if (!dfmt) {
H. Peter Anvinc5136902018-06-15 18:20:17 -0700499 nasm_fatal_fl(ERR_NOFILE | ERR_USAGE,
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800500 "unrecognized debug format `%s' for"
501 " output format `%s'",
502 debug_format, ofmt->shortname);
503 }
504 }
H. Peter Anvinbb88d012003-09-10 23:34:23 +0000505
Cyrill Gorcunovfeabd742018-11-13 01:23:47 +0300506 preproc_init(&include_path);
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800507
508 parse_cmdline(argc, argv, 2);
509 if (terminate_after_phase) {
510 if (want_usage)
511 usage();
512 return 1;
513 }
514
515 /* Save away the default state of warnings */
516 memcpy(warning_state_init, warning_state, sizeof warning_state);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000517
Cyrill Gorcunov69bb0522018-09-22 13:46:45 +0300518 /*
519 * If no output file name provided and this
Cyrill Gorcunovda3780d2018-09-22 14:10:36 +0300520 * is a preprocess mode, we're perfectly
521 * fine to output into stdout.
Cyrill Gorcunov69bb0522018-09-22 13:46:45 +0300522 */
H. Peter Anvin (Intel)7b6371b2018-11-20 10:56:57 -0800523 if (!outname && !(operating_mode & OP_PREPROCESS)) {
524 outname = filename_set_extension(inname, ofmt->extension);
525 if (!strcmp(outname, inname)) {
526 outname = "nasm.out";
527 nasm_error(ERR_WARNING,
Cyrill Gorcunov8e0acaa2018-11-24 12:03:31 +0300528 "default output file same as input `%s', using `%s' for output\n",
H. Peter Anvin (Intel)7b6371b2018-11-20 10:56:57 -0800529 inname, outname);
530 }
Cyrill Gorcunov69bb0522018-09-22 13:46:45 +0300531 }
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800532
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -0700533 if (depend_file || (operating_mode & OP_DEPEND))
534 depend_list = strlist_allocate();
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700535
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700536 if (!depend_target)
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400537 depend_target = quote_for_make(outname);
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700538
Cyrill Gorcunove9fc88c2014-06-23 02:22:02 +0400539 if (operating_mode & OP_DEPEND) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000540 char *line;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700541
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400542 if (depend_missing_ok)
543 preproc->include_path(NULL); /* "assume generated" */
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700544
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -0700545 preproc->reset(inname, 0, depend_list);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000546 ofile = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000547 while ((line = preproc->getline()))
548 nasm_free(line);
549 preproc->cleanup(0);
Cyrill Gorcunove9fc88c2014-06-23 02:22:02 +0400550 } else if (operating_mode & OP_PREPROCESS) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000551 char *line;
H. Peter Anvin274cda82016-05-10 02:56:29 -0700552 const char *file_name = NULL;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000553 int32_t prior_linnum = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000554 int lineinc = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000555
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800556 if (outname) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -0700557 ofile = nasm_open_write(outname, NF_TEXT);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000558 if (!ofile)
H. Peter Anvinc5136902018-06-15 18:20:17 -0700559 nasm_fatal_fl(ERR_NOFILE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000560 "unable to open output file `%s'",
561 outname);
562 } else
563 ofile = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000564
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700565 location.known = false;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000566
Cyrill Gorcunovb574b072011-12-05 01:56:40 +0400567 /* pass = 1; */
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -0700568 preproc->reset(inname, 3, depend_list);
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800569
570 /* Revert all warnings to the default state */
571 memcpy(warning_state, warning_state_init, sizeof warning_state);
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700572
H. Peter Anvine2c80182005-01-15 22:15:51 +0000573 while ((line = preproc->getline())) {
574 /*
575 * We generate %line directives if needed for later programs
576 */
Keith Kaniosb7a89542007-04-12 02:40:54 +0000577 int32_t linnum = prior_linnum += lineinc;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000578 int altline = src_get(&linnum, &file_name);
579 if (altline) {
580 if (altline == 1 && lineinc == 1)
581 nasm_fputs("", ofile);
582 else {
583 lineinc = (altline != -1 || lineinc != 1);
584 fprintf(ofile ? ofile : stdout,
Keith Kanios93f2e9a2007-04-14 00:10:59 +0000585 "%%line %"PRId32"+%d %s\n", linnum, lineinc,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000586 file_name);
587 }
588 prior_linnum = linnum;
589 }
590 nasm_fputs(line, ofile);
591 nasm_free(line);
592 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000593 preproc->cleanup(0);
594 if (ofile)
595 fclose(ofile);
H. Peter Anvin29695c82018-06-14 17:04:32 -0700596 if (ofile && terminate_after_phase && !keep_all)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000597 remove(outname);
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400598 ofile = NULL;
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400599 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000600
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400601 if (operating_mode & OP_NORMAL) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -0700602 ofile = nasm_open_write(outname, (ofmt->flags & OFMT_TEXT) ? NF_TEXT : NF_BINARY);
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400603 if (!ofile)
H. Peter Anvinc5136902018-06-15 18:20:17 -0700604 nasm_fatal_fl(ERR_NOFILE,
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400605 "unable to open output file `%s'", outname);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000606
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400607 ofmt->init();
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400608 dfmt->init();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000609
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -0700610 assemble_file(inname, depend_list);
Victor van den Elzenc82c3722008-06-04 15:24:20 +0200611
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400612 if (!terminate_after_phase) {
H. Peter Anvin477ae442016-03-07 22:53:06 -0800613 ofmt->cleanup();
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400614 cleanup_labels();
615 fflush(ofile);
H. Peter Anvin274cda82016-05-10 02:56:29 -0700616 if (ferror(ofile)) {
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400617 nasm_error(ERR_NONFATAL|ERR_NOFILE,
618 "write error on output file `%s'", outname);
H. Peter Anvin274cda82016-05-10 02:56:29 -0700619 terminate_after_phase = true;
620 }
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400621 }
622
623 if (ofile) {
624 fclose(ofile);
H. Peter Anvin29695c82018-06-14 17:04:32 -0700625 if (terminate_after_phase && !keep_all)
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400626 remove(outname);
627 ofile = NULL;
628 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000629 }
H. Peter Anvin734b1882002-04-30 21:01:08 +0000630
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700631 if (depend_list && !terminate_after_phase)
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400632 emit_dependencies(depend_list);
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700633
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000634 if (want_usage)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000635 usage();
H. Peter Anvin734b1882002-04-30 21:01:08 +0000636
H. Peter Anvine2c80182005-01-15 22:15:51 +0000637 raa_free(offsets);
638 saa_free(forwrefs);
639 eval_cleanup();
H. Peter Anvin74cc5e52007-08-30 22:35:34 +0000640 stdscan_cleanup();
H. Peter Anvin274cda82016-05-10 02:56:29 -0700641 src_free();
Cyrill Gorcunovfeabd742018-11-13 01:23:47 +0300642 strlist_free(include_path);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000643
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700644 return terminate_after_phase;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000645}
646
H. Peter Anvineba20a72002-04-30 20:53:55 +0000647/*
648 * Get a parameter for a command line option.
649 * First arg must be in the form of e.g. -f...
650 */
H. Peter Anvin423e3812007-11-15 17:12:29 -0800651static char *get_param(char *p, char *q, bool *advance)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000652{
H. Peter Anvin423e3812007-11-15 17:12:29 -0800653 *advance = false;
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +0400654 if (p[2]) /* the parameter's in the option */
655 return nasm_skip_spaces(p + 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000656 if (q && q[0]) {
H. Peter Anvin423e3812007-11-15 17:12:29 -0800657 *advance = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000658 return q;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000659 }
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400660 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000661 "option `-%c' requires an argument", p[1]);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000662 return NULL;
663}
664
H. Peter Anvindc242712007-11-18 11:55:10 -0800665/*
666 * Copy a filename
667 */
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800668static void copy_filename(const char **dst, const char *src, const char *what)
H. Peter Anvindc242712007-11-18 11:55:10 -0800669{
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800670 if (*dst)
H. Peter Anvinc5136902018-06-15 18:20:17 -0700671 nasm_fatal("more than one %s file specified: %s\n", what, src);
H. Peter Anvindc242712007-11-18 11:55:10 -0800672
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800673 *dst = nasm_strdup(src);
H. Peter Anvindc242712007-11-18 11:55:10 -0800674}
675
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700676/*
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700677 * Convert a string to a POSIX make-safe form
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700678 */
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700679static char *quote_for_pmake(const char *str)
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700680{
681 const char *p;
682 char *os, *q;
683
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400684 size_t n = 1; /* Terminating zero */
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700685 size_t nbs = 0;
686
687 if (!str)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400688 return NULL;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700689
690 for (p = str; *p; p++) {
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400691 switch (*p) {
692 case ' ':
693 case '\t':
694 /* Convert N backslashes + ws -> 2N+1 backslashes + ws */
695 n += nbs + 2;
696 nbs = 0;
697 break;
698 case '$':
699 case '#':
700 nbs = 0;
701 n += 2;
702 break;
703 case '\\':
704 nbs++;
705 n++;
706 break;
707 default:
708 nbs = 0;
709 n++;
710 break;
711 }
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700712 }
713
714 /* Convert N backslashes at the end of filename to 2N backslashes */
715 if (nbs)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400716 n += nbs;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700717
718 os = q = nasm_malloc(n);
719
720 nbs = 0;
721 for (p = str; *p; p++) {
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400722 switch (*p) {
723 case ' ':
724 case '\t':
725 while (nbs--)
726 *q++ = '\\';
727 *q++ = '\\';
728 *q++ = *p;
729 break;
730 case '$':
731 *q++ = *p;
732 *q++ = *p;
733 nbs = 0;
734 break;
735 case '#':
736 *q++ = '\\';
737 *q++ = *p;
738 nbs = 0;
739 break;
740 case '\\':
741 *q++ = *p;
742 nbs++;
743 break;
744 default:
745 *q++ = *p;
746 nbs = 0;
747 break;
748 }
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700749 }
750 while (nbs--)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400751 *q++ = '\\';
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700752
753 *q = '\0';
754
755 return os;
756}
757
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700758/*
759 * Convert a string to a Watcom make-safe form
760 */
761static char *quote_for_wmake(const char *str)
762{
763 const char *p;
764 char *os, *q;
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700765 bool quote = false;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700766
767 size_t n = 1; /* Terminating zero */
768
769 if (!str)
770 return NULL;
771
772 for (p = str; *p; p++) {
773 switch (*p) {
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700774 case ' ':
775 case '\t':
H. Peter Anvin3e30c322017-08-16 22:20:36 -0700776 case '&':
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700777 quote = true;
778 n++;
779 break;
780 case '\"':
781 quote = true;
782 n += 2;
783 break;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700784 case '$':
785 case '#':
786 n += 2;
787 break;
788 default:
789 n++;
790 break;
791 }
792 }
793
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700794 if (quote)
795 n += 2;
796
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700797 os = q = nasm_malloc(n);
798
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700799 if (quote)
800 *q++ = '\"';
801
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700802 for (p = str; *p; p++) {
803 switch (*p) {
804 case '$':
805 case '#':
806 *q++ = '$';
807 *q++ = *p;
808 break;
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700809 case '\"':
810 *q++ = *p;
811 *q++ = *p;
812 break;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700813 default:
814 *q++ = *p;
815 break;
816 }
817 }
818
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700819 if (quote)
820 *q++ = '\"';
821
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700822 *q = '\0';
823
824 return os;
825}
826
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100827enum text_options {
H. Peter Anvin3366e312018-02-07 14:14:36 -0800828 OPT_BOGUS,
829 OPT_VERSION,
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700830 OPT_HELP,
H. Peter Anvin3366e312018-02-07 14:14:36 -0800831 OPT_ABORT_ON_PANIC,
H. Peter Anvin05990342018-06-11 13:32:42 -0700832 OPT_MANGLE,
833 OPT_INCLUDE,
834 OPT_PRAGMA,
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700835 OPT_BEFORE,
H. Peter Anvin29695c82018-06-14 17:04:32 -0700836 OPT_LIMIT,
837 OPT_KEEP_ALL
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100838};
H. Peter Anvin3366e312018-02-07 14:14:36 -0800839struct textargs {
840 const char *label;
841 enum text_options opt;
842 bool need_arg;
H. Peter Anvin98578072018-06-01 18:02:54 -0700843 int pvt;
H. Peter Anvin3366e312018-02-07 14:14:36 -0800844};
H. Peter Anvin2530a102016-02-18 02:28:15 -0800845static const struct textargs textopts[] = {
H. Peter Anvin98578072018-06-01 18:02:54 -0700846 {"v", OPT_VERSION, false, 0},
847 {"version", OPT_VERSION, false, 0},
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700848 {"help", OPT_HELP, false, 0},
H. Peter Anvin98578072018-06-01 18:02:54 -0700849 {"abort-on-panic", OPT_ABORT_ON_PANIC, false, 0},
850 {"prefix", OPT_MANGLE, true, LM_GPREFIX},
851 {"postfix", OPT_MANGLE, true, LM_GSUFFIX},
852 {"gprefix", OPT_MANGLE, true, LM_GPREFIX},
853 {"gpostfix", OPT_MANGLE, true, LM_GSUFFIX},
854 {"lprefix", OPT_MANGLE, true, LM_LPREFIX},
855 {"lpostfix", OPT_MANGLE, true, LM_LSUFFIX},
H. Peter Anvin05990342018-06-11 13:32:42 -0700856 {"include", OPT_INCLUDE, true, 0},
857 {"pragma", OPT_PRAGMA, true, 0},
858 {"before", OPT_BEFORE, true, 0},
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700859 {"limit-", OPT_LIMIT, true, 0},
H. Peter Anvin29695c82018-06-14 17:04:32 -0700860 {"keep-all", OPT_KEEP_ALL, false, 0},
H. Peter Anvin98578072018-06-01 18:02:54 -0700861 {NULL, OPT_BOGUS, false, 0}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000862};
863
Cyrill Gorcunove7438432014-05-09 22:34:34 +0400864static void show_version(void)
865{
866 printf("NASM version %s compiled on %s%s\n",
867 nasm_version, nasm_date, nasm_compile_options);
868 exit(0);
869}
870
H. Peter Anvin423e3812007-11-15 17:12:29 -0800871static bool stopoptions = false;
H. Peter Anvin55568c12016-10-03 19:46:49 -0700872static bool process_arg(char *p, char *q, int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000873{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000874 char *param;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800875 bool advance = false;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000876
877 if (!p || !p[0])
H. Peter Anvin423e3812007-11-15 17:12:29 -0800878 return false;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000879
H. Peter Anvine2c80182005-01-15 22:15:51 +0000880 if (p[0] == '-' && !stopoptions) {
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400881 if (strchr("oOfpPdDiIlFXuUZwW", p[1])) {
882 /* These parameters take values */
883 if (!(param = get_param(p, q, &advance)))
884 return advance;
885 }
H. Peter Anvin423e3812007-11-15 17:12:29 -0800886
H. Peter Anvine2c80182005-01-15 22:15:51 +0000887 switch (p[1]) {
888 case 's':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700889 if (pass == 1)
890 error_file = stdout;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000891 break;
H. Peter Anvin70653092007-10-19 14:42:29 -0700892
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400893 case 'o': /* output file */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700894 if (pass == 2)
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800895 copy_filename(&outname, param, "output");
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400896 break;
H. Peter Anvinfd7dd112007-10-10 14:06:59 -0700897
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400898 case 'f': /* output format */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700899 if (pass == 1) {
900 ofmt = ofmt_find(param, &ofmt_alias);
901 if (!ofmt) {
H. Peter Anvinc5136902018-06-15 18:20:17 -0700902 nasm_fatal_fl(ERR_NOFILE | ERR_USAGE,
H. Peter Anvin55568c12016-10-03 19:46:49 -0700903 "unrecognised output format `%s' - "
904 "use -hf for a list", param);
905 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400906 }
907 break;
H. Peter Anvin70653092007-10-19 14:42:29 -0700908
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400909 case 'O': /* Optimization level */
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800910 if (pass == 1) {
H. Peter Anvin55568c12016-10-03 19:46:49 -0700911 int opt;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700912
H. Peter Anvin55568c12016-10-03 19:46:49 -0700913 if (!*param) {
914 /* Naked -O == -Ox */
Chang S. Baea5786342018-08-15 23:22:21 +0300915 optimizing.level = MAX_OPTIMIZE;
H. Peter Anvin55568c12016-10-03 19:46:49 -0700916 } else {
917 while (*param) {
918 switch (*param) {
919 case '0': case '1': case '2': case '3': case '4':
920 case '5': case '6': case '7': case '8': case '9':
921 opt = strtoul(param, &param, 10);
H. Peter Anvind85d2502008-05-04 17:53:31 -0700922
Chang S. Baea5786342018-08-15 23:22:21 +0300923 /* -O0 -> optimizing.level == -1, 0.98 behaviour */
924 /* -O1 -> optimizing.level == 0, 0.98.09 behaviour */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700925 if (opt < 2)
Chang S. Baea5786342018-08-15 23:22:21 +0300926 optimizing.level = opt - 1;
H. Peter Anvin55568c12016-10-03 19:46:49 -0700927 else
Chang S. Baea5786342018-08-15 23:22:21 +0300928 optimizing.level = opt;
H. Peter Anvin55568c12016-10-03 19:46:49 -0700929 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700930
H. Peter Anvin55568c12016-10-03 19:46:49 -0700931 case 'v':
932 case '+':
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400933 param++;
934 opt_verbose_info = true;
935 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700936
H. Peter Anvin55568c12016-10-03 19:46:49 -0700937 case 'x':
938 param++;
Chang S. Baea5786342018-08-15 23:22:21 +0300939 optimizing.level = MAX_OPTIMIZE;
H. Peter Anvin55568c12016-10-03 19:46:49 -0700940 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700941
H. Peter Anvin55568c12016-10-03 19:46:49 -0700942 default:
H. Peter Anvinc5136902018-06-15 18:20:17 -0700943 nasm_fatal("unknown optimization option -O%c\n",
H. Peter Anvin55568c12016-10-03 19:46:49 -0700944 *param);
945 break;
946 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400947 }
Chang S. Baea5786342018-08-15 23:22:21 +0300948 if (optimizing.level > MAX_OPTIMIZE)
949 optimizing.level = MAX_OPTIMIZE;
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400950 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400951 }
952 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800953
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400954 case 'p': /* pre-include */
955 case 'P':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700956 if (pass == 2)
957 preproc->pre_include(param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400958 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800959
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400960 case 'd': /* pre-define */
961 case 'D':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700962 if (pass == 2)
963 preproc->pre_define(param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400964 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800965
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400966 case 'u': /* un-define */
967 case 'U':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700968 if (pass == 2)
969 preproc->pre_undefine(param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400970 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800971
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400972 case 'i': /* include search path */
973 case 'I':
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800974 if (pass == 1)
Cyrill Gorcunove3588512018-11-13 01:09:27 +0300975 strlist_add_string(include_path, param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400976 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800977
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400978 case 'l': /* listing file */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700979 if (pass == 2)
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800980 copy_filename(&listname, param, "listing");
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400981 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800982
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400983 case 'Z': /* error messages file */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700984 if (pass == 1)
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800985 copy_filename(&errname, param, "error");
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400986 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800987
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400988 case 'F': /* specify debug format */
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800989 if (pass == 1) {
H. Peter Anvin55568c12016-10-03 19:46:49 -0700990 using_debug_info = true;
991 debug_format = param;
992 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400993 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800994
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400995 case 'X': /* specify error reporting format */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700996 if (pass == 1) {
997 if (nasm_stricmp("vc", param) == 0)
998 nasm_set_verror(nasm_verror_vc);
999 else if (nasm_stricmp("gnu", param) == 0)
1000 nasm_set_verror(nasm_verror_gnu);
1001 else
H. Peter Anvinc5136902018-06-15 18:20:17 -07001002 nasm_fatal_fl(ERR_NOFILE | ERR_USAGE,
H. Peter Anvin55568c12016-10-03 19:46:49 -07001003 "unrecognized error reporting format `%s'",
1004 param);
1005 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001006 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001007
H. Peter Anvine2c80182005-01-15 22:15:51 +00001008 case 'g':
H. Peter Anvinbf6230b2018-11-11 13:25:16 -08001009 if (pass == 1) {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001010 using_debug_info = true;
1011 if (p[2])
1012 debug_format = nasm_skip_spaces(p + 2);
1013 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001014 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001015
H. Peter Anvine2c80182005-01-15 22:15:51 +00001016 case 'h':
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001017 help(p[2]);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001018 exit(0); /* never need usage message here */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001019 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001020
H. Peter Anvine2c80182005-01-15 22:15:51 +00001021 case 'y':
1022 printf("\nvalid debug formats for '%s' output format are"
1023 " ('*' denotes default):\n", ofmt->shortname);
1024 dfmt_list(ofmt, stdout);
1025 exit(0);
1026 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001027
H. Peter Anvine2c80182005-01-15 22:15:51 +00001028 case 't':
H. Peter Anvin55568c12016-10-03 19:46:49 -07001029 if (pass == 2)
1030 tasm_compatible_mode = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001031 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001032
H. Peter Anvine2c80182005-01-15 22:15:51 +00001033 case 'v':
Cyrill Gorcunove7438432014-05-09 22:34:34 +04001034 show_version();
H. Peter Anvine2c80182005-01-15 22:15:51 +00001035 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001036
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001037 case 'e': /* preprocess only */
1038 case 'E':
H. Peter Anvin55568c12016-10-03 19:46:49 -07001039 if (pass == 1)
1040 operating_mode = OP_PREPROCESS;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001041 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001042
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001043 case 'a': /* assemble only - don't preprocess */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001044 if (pass == 1)
1045 preproc = &preproc_nop;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001046 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001047
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001048 case 'w':
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001049 case 'W':
H. Peter Anvin55568c12016-10-03 19:46:49 -07001050 if (pass == 2) {
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001051 if (!set_warning_status(param)) {
1052 nasm_error(ERR_WARNING|ERR_NOFILE|ERR_WARN_UNK_WARNING,
1053 "unknown warning option: %s", param);
H. Peter Anvin55568c12016-10-03 19:46:49 -07001054 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001055 }
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001056 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001057
H. Peter Anvine2c80182005-01-15 22:15:51 +00001058 case 'M':
H. Peter Anvin77c9bf62017-08-16 21:14:33 -07001059 if (pass == 1) {
1060 switch (p[2]) {
1061 case 'W':
1062 quote_for_make = quote_for_wmake;
1063 break;
1064 case 'D':
1065 case 'F':
1066 case 'T':
1067 case 'Q':
1068 advance = true;
1069 break;
1070 default:
1071 break;
1072 }
1073 } else {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001074 switch (p[2]) {
1075 case 0:
1076 operating_mode = OP_DEPEND;
1077 break;
1078 case 'G':
1079 operating_mode = OP_DEPEND;
1080 depend_missing_ok = true;
1081 break;
1082 case 'P':
1083 depend_emit_phony = true;
1084 break;
1085 case 'D':
1086 operating_mode = OP_NORMAL;
1087 depend_file = q;
1088 advance = true;
1089 break;
1090 case 'F':
1091 depend_file = q;
1092 advance = true;
1093 break;
1094 case 'T':
1095 depend_target = q;
1096 advance = true;
1097 break;
1098 case 'Q':
1099 depend_target = quote_for_make(q);
1100 advance = true;
1101 break;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -07001102 case 'W':
1103 /* handled in pass 1 */
1104 break;
H. Peter Anvin55568c12016-10-03 19:46:49 -07001105 default:
1106 nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
1107 "unknown dependency option `-M%c'", p[2]);
1108 break;
1109 }
H. Peter Anvin77c9bf62017-08-16 21:14:33 -07001110 }
1111 if (advance && (!q || !q[0])) {
1112 nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
1113 "option `-M%c' requires a parameter", p[2]);
1114 break;
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001115 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001116 break;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001117
H. Peter Anvine2c80182005-01-15 22:15:51 +00001118 case '-':
1119 {
H. Peter Anvin3366e312018-02-07 14:14:36 -08001120 const struct textargs *tx;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001121 size_t olen, plen;
H. Peter Anvinc805fd72018-06-12 14:23:05 -07001122 char *eqsave;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001123
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001124 p += 2;
1125
1126 if (!*p) { /* -- => stop processing options */
H. Peter Anvin3366e312018-02-07 14:14:36 -08001127 stopoptions = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001128 break;
1129 }
Cyrill Gorcunove7438432014-05-09 22:34:34 +04001130
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001131 plen = strlen(p);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001132 for (tx = textopts; tx->label; tx++) {
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001133 olen = strlen(tx->label);
1134
1135 if (olen > plen)
1136 continue;
1137
1138 if (nasm_memicmp(p, tx->label, olen))
1139 continue;
1140
1141 if (tx->label[olen-1] == '-')
1142 break; /* Incomplete option */
1143
1144 if (!p[olen] || p[olen] == '=')
1145 break; /* Complete option */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001146 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001147
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001148 if (!tx->label) {
1149 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
1150 "unrecognized option `--%s'", p);
1151 }
1152
H. Peter Anvinc805fd72018-06-12 14:23:05 -07001153 eqsave = param = strchr(p+olen, '=');
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001154 if (param)
1155 *param++ = '\0';
1156
H. Peter Anvin3366e312018-02-07 14:14:36 -08001157 if (tx->need_arg) {
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001158 if (!param) {
1159 param = q;
1160 advance = true;
1161 }
1162
1163 /* Note: a null string is a valid parameter */
1164 if (!param) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001165 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvin3366e312018-02-07 14:14:36 -08001166 "option `--%s' requires an argument",
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001167 p);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001168 break;
1169 }
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001170 } else {
1171 if (param) {
1172 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
1173 "option `--%s' does not take an argument",
1174 p);
1175
1176 }
H. Peter Anvin3366e312018-02-07 14:14:36 -08001177 }
1178
1179 switch (tx->opt) {
1180 case OPT_VERSION:
1181 show_version();
1182 break;
1183 case OPT_ABORT_ON_PANIC:
1184 abort_on_panic = true;
1185 break;
H. Peter Anvin98578072018-06-01 18:02:54 -07001186 case OPT_MANGLE:
H. Peter Anvin3366e312018-02-07 14:14:36 -08001187 if (pass == 2)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001188 set_label_mangle(tx->pvt, param);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001189 break;
H. Peter Anvin05990342018-06-11 13:32:42 -07001190 case OPT_INCLUDE:
1191 if (pass == 2)
1192 preproc->pre_include(q);
1193 break;
1194 case OPT_PRAGMA:
1195 if (pass == 2)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001196 preproc->pre_command("pragma", param);
H. Peter Anvin05990342018-06-11 13:32:42 -07001197 break;
1198 case OPT_BEFORE:
1199 if (pass == 2)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001200 preproc->pre_command(NULL, param);
H. Peter Anvin05990342018-06-11 13:32:42 -07001201 break;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001202 case OPT_LIMIT:
H. Peter Anvinbf6230b2018-11-11 13:25:16 -08001203 if (pass == 1)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001204 nasm_set_limit(p+olen, param);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001205 break;
H. Peter Anvin29695c82018-06-14 17:04:32 -07001206 case OPT_KEEP_ALL:
1207 keep_all = true;
1208 break;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001209 case OPT_HELP:
1210 help(0);
1211 exit(0);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001212 default:
1213 panic();
H. Peter Anvine2c80182005-01-15 22:15:51 +00001214 }
H. Peter Anvinc805fd72018-06-12 14:23:05 -07001215
1216 if (eqsave)
1217 *eqsave = '='; /* Restore = argument separator */
1218
H. Peter Anvine2c80182005-01-15 22:15:51 +00001219 break;
1220 }
1221
1222 default:
H. Peter Anvinac061332017-03-31 11:41:16 -07001223 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
1224 "unrecognised option `-%c'", p[1]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001225 break;
1226 }
H. Peter Anvin55568c12016-10-03 19:46:49 -07001227 } else if (pass == 2) {
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001228 /* In theory we could allow multiple input files... */
1229 copy_filename(&inname, p, "input");
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001230 }
1231
1232 return advance;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001233}
1234
H. Peter Anvineba20a72002-04-30 20:53:55 +00001235#define ARG_BUF_DELTA 128
1236
H. Peter Anvin55568c12016-10-03 19:46:49 -07001237static void process_respfile(FILE * rfile, int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001238{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001239 char *buffer, *p, *q, *prevarg;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001240 int bufsize, prevargsize;
1241
1242 bufsize = prevargsize = ARG_BUF_DELTA;
1243 buffer = nasm_malloc(ARG_BUF_DELTA);
1244 prevarg = nasm_malloc(ARG_BUF_DELTA);
1245 prevarg[0] = '\0';
1246
H. Peter Anvine2c80182005-01-15 22:15:51 +00001247 while (1) { /* Loop to handle all lines in file */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001248 p = buffer;
1249 while (1) { /* Loop to handle long lines */
1250 q = fgets(p, bufsize - (p - buffer), rfile);
1251 if (!q)
1252 break;
1253 p += strlen(p);
1254 if (p > buffer && p[-1] == '\n')
1255 break;
1256 if (p - buffer > bufsize - 10) {
1257 int offset;
1258 offset = p - buffer;
1259 bufsize += ARG_BUF_DELTA;
1260 buffer = nasm_realloc(buffer, bufsize);
1261 p = buffer + offset;
1262 }
1263 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001264
H. Peter Anvine2c80182005-01-15 22:15:51 +00001265 if (!q && p == buffer) {
1266 if (prevarg[0])
H. Peter Anvin55568c12016-10-03 19:46:49 -07001267 process_arg(prevarg, NULL, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001268 nasm_free(buffer);
1269 nasm_free(prevarg);
1270 return;
1271 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001272
H. Peter Anvine2c80182005-01-15 22:15:51 +00001273 /*
1274 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
1275 * them are present at the end of the line.
1276 */
1277 *(p = &buffer[strcspn(buffer, "\r\n\032")]) = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001278
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001279 while (p > buffer && nasm_isspace(p[-1]))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001280 *--p = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001281
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +04001282 p = nasm_skip_spaces(buffer);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001283
H. Peter Anvin55568c12016-10-03 19:46:49 -07001284 if (process_arg(prevarg, p, pass))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001285 *p = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001286
Charles Crayne192d5b52007-10-18 19:02:42 -07001287 if ((int) strlen(p) > prevargsize - 10) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001288 prevargsize += ARG_BUF_DELTA;
1289 prevarg = nasm_realloc(prevarg, prevargsize);
1290 }
H. Peter Anvindc242712007-11-18 11:55:10 -08001291 strncpy(prevarg, p, prevargsize);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001292 }
1293}
1294
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001295/* Function to process args from a string of args, rather than the
1296 * argv array. Used by the environment variable and response file
1297 * processing.
1298 */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001299static void process_args(char *args, int pass)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001300{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001301 char *p, *q, *arg, *prevarg;
1302 char separator = ' ';
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001303
1304 p = args;
1305 if (*p && *p != '-')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001306 separator = *p++;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001307 arg = NULL;
1308 while (*p) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001309 q = p;
1310 while (*p && *p != separator)
1311 p++;
1312 while (*p == separator)
1313 *p++ = '\0';
1314 prevarg = arg;
1315 arg = q;
H. Peter Anvin55568c12016-10-03 19:46:49 -07001316 if (process_arg(prevarg, arg, pass))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001317 arg = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001318 }
1319 if (arg)
H. Peter Anvin55568c12016-10-03 19:46:49 -07001320 process_arg(arg, NULL, pass);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001321}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001322
H. Peter Anvin55568c12016-10-03 19:46:49 -07001323static void process_response_file(const char *file, int pass)
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001324{
1325 char str[2048];
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001326 FILE *f = nasm_open_read(file, NF_TEXT);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001327 if (!f) {
Cyrill Gorcunovf1964512013-02-15 12:14:06 +04001328 perror(file);
1329 exit(-1);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001330 }
1331 while (fgets(str, sizeof str, f)) {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001332 process_args(str, pass);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001333 }
1334 fclose(f);
1335}
1336
H. Peter Anvin55568c12016-10-03 19:46:49 -07001337static void parse_cmdline(int argc, char **argv, int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001338{
1339 FILE *rfile;
Cyrill Gorcunovf4941892011-07-17 13:55:25 +04001340 char *envreal, *envcopy = NULL, *p;
H. Peter Anvin972079f2008-09-30 17:01:23 -07001341 int i;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001342
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001343 /* Initialize all the warnings to their default state */
1344 for (i = 0; i < ERR_WARN_ALL; i++) {
1345 warning_state_init[i] = warning_state[i] =
1346 warnings[i].enabled ? WARN_ST_ENABLED : 0;
1347 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001348
1349 /*
H. Peter Anvinff7ccc02002-05-06 19:41:57 +00001350 * First, process the NASMENV environment variable.
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001351 */
H. Peter Anvinff7ccc02002-05-06 19:41:57 +00001352 envreal = getenv("NASMENV");
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001353 if (envreal) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001354 envcopy = nasm_strdup(envreal);
H. Peter Anvin55568c12016-10-03 19:46:49 -07001355 process_args(envcopy, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001356 nasm_free(envcopy);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001357 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001358
1359 /*
1360 * Now process the actual command line.
1361 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001362 while (--argc) {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001363 bool advance;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001364 argv++;
1365 if (argv[0][0] == '@') {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001366 /*
1367 * We have a response file, so process this as a set of
H. Peter Anvine2c80182005-01-15 22:15:51 +00001368 * arguments like the environment variable. This allows us
1369 * to have multiple arguments on a single line, which is
1370 * different to the -@resp file processing below for regular
1371 * NASM.
1372 */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001373 process_response_file(argv[0]+1, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001374 argc--;
1375 argv++;
1376 }
1377 if (!stopoptions && argv[0][0] == '-' && argv[0][1] == '@') {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001378 p = get_param(argv[0], argc > 1 ? argv[1] : NULL, &advance);
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001379 if (p) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001380 rfile = nasm_open_read(p, NF_TEXT);
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001381 if (rfile) {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001382 process_respfile(rfile, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001383 fclose(rfile);
1384 } else
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001385 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001386 "unable to open response file `%s'", p);
1387 }
1388 } else
H. Peter Anvin55568c12016-10-03 19:46:49 -07001389 advance = process_arg(argv[0], argc > 1 ? argv[1] : NULL, pass);
H. Peter Anvin423e3812007-11-15 17:12:29 -08001390 argv += advance, argc -= advance;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001391 }
1392
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001393 /*
1394 * Look for basic command line typos. This definitely doesn't
1395 * catch all errors, but it might help cases of fumbled fingers.
1396 */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001397 if (pass != 2)
1398 return;
1399
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001400 if (!inname)
H. Peter Anvinc5136902018-06-15 18:20:17 -07001401 nasm_fatal_fl(ERR_NOFILE | ERR_USAGE, "no input file specified");
H. Peter Anvin59ddd262007-10-01 11:26:31 -07001402
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001403 else if ((errname && !strcmp(inname, errname)) ||
1404 (outname && !strcmp(inname, outname)) ||
1405 (listname && !strcmp(inname, listname)) ||
1406 (depend_file && !strcmp(inname, depend_file)))
H. Peter Anvinc5136902018-06-15 18:20:17 -07001407 nasm_fatal_fl(ERR_USAGE, "will not overwrite input file");
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001408
1409 if (errname) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001410 error_file = nasm_open_write(errname, NF_TEXT);
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001411 if (!error_file) {
1412 error_file = stderr; /* Revert to default! */
H. Peter Anvinc5136902018-06-15 18:20:17 -07001413 nasm_fatal_fl(ERR_NOFILE | ERR_USAGE,
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001414 "cannot open file `%s' for error messages",
1415 errname);
1416 }
Charles Craynefcce07f2007-09-30 22:15:36 -07001417 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001418}
1419
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001420static void assemble_file(const char *fname, StrList *depend_list)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001421{
H. Peter Anvinc7131682017-03-07 17:45:01 -08001422 char *line;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001423 insn output_ins;
H. Peter Anvinc7131682017-03-07 17:45:01 -08001424 int i;
H. Peter Anvin9c595b62017-03-07 19:44:21 -08001425 uint64_t prev_offset_changed;
H. Peter Anvina3d96d02018-06-15 17:51:39 -07001426 int64_t stall_count = 0; /* Make sure we make forward progress... */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001427
H. Peter Anvina7ecf262018-02-06 14:43:07 -08001428 switch (cmd_sb) {
1429 case 16:
1430 break;
1431 case 32:
1432 if (!iflag_cpu_level_ok(&cmd_cpu, IF_386))
H. Peter Anvinc5136902018-06-15 18:20:17 -07001433 nasm_fatal("command line: 32-bit segment size requires a higher cpu");
H. Peter Anvina7ecf262018-02-06 14:43:07 -08001434 break;
1435 case 64:
1436 if (!iflag_cpu_level_ok(&cmd_cpu, IF_X86_64))
H. Peter Anvinc5136902018-06-15 18:20:17 -07001437 nasm_fatal("command line: 64-bit segment size requires a higher cpu");
H. Peter Anvina7ecf262018-02-06 14:43:07 -08001438 break;
1439 default:
1440 panic();
1441 break;
1442 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001443
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001444 prev_offset_changed = nasm_limit[LIMIT_PASSES];
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001445 for (passn = 1; pass0 <= 2; passn++) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001446 pass1 = pass0 == 2 ? 2 : 1; /* 1, 1, 1, ..., 1, 2 */
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001447 pass2 = passn > 1 ? 2 : 1; /* 1, 2, 2, ..., 2, 2 */
1448 /* pass0 0, 0, 0, ..., 1, 2 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00001449
H. Peter Anvincac0b192017-03-28 16:12:30 -07001450 globalbits = cmd_sb; /* set 'bits' to command line default */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001451 cpu = cmd_cpu;
1452 if (pass0 == 2) {
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08001453 lfmt->init(listname);
H. Peter Anvin29695c82018-06-14 17:04:32 -07001454 } else if (passn == 1 && listname && !keep_all) {
H. Peter Anvinaac01ff2017-04-02 19:10:26 -07001455 /* Remove the list file in case we die before the output pass */
1456 remove(listname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001457 }
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001458 in_absolute = false;
Charles Craynec1905c22008-09-11 18:54:06 -07001459 global_offset_changed = 0; /* set by redefine_label */
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001460 if (passn > 1) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001461 saa_rewind(forwrefs);
1462 forwref = saa_rstruct(forwrefs);
1463 raa_free(offsets);
1464 offsets = raa_init();
1465 }
H. Peter Anvin892c4812018-05-30 14:43:46 -07001466 location.segment = NO_SEG;
1467 location.offset = 0;
1468 if (passn == 1)
1469 location.known = true;
1470 ofmt->reset();
1471 switch_segment(ofmt->section(NULL, pass2, &globalbits));
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001472 preproc->reset(fname, pass1, pass1 == 2 ? depend_list : NULL);
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001473
1474 /* Revert all warnings to the default state */
1475 memcpy(warning_state, warning_state_init, sizeof warning_state);
Victor van den Elzen819703a2008-07-16 15:20:56 +02001476
H. Peter Anvine2c80182005-01-15 22:15:51 +00001477 globallineno = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001478
H. Peter Anvine2c80182005-01-15 22:15:51 +00001479 while ((line = preproc->getline())) {
H. Peter Anvina3d96d02018-06-15 17:51:39 -07001480 if (++globallineno > nasm_limit[LIMIT_LINES])
H. Peter Anvinc5136902018-06-15 18:20:17 -07001481 nasm_fatal("overall line count exceeds the maximum %"PRId64"\n",
Cyrill Gorcunovf7b44f62018-10-15 22:58:13 +03001482 nasm_limit[LIMIT_LINES]);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001483
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001484 /*
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001485 * Here we parse our directives; this is not handled by the
H. Peter Anvinc7131682017-03-07 17:45:01 -08001486 * main parser.
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001487 */
H. Peter Anvina6e26d92017-03-07 21:32:37 -08001488 if (process_directives(line))
H. Peter Anvinc7131682017-03-07 17:45:01 -08001489 goto end_of_line; /* Just do final cleanup */
H. Peter Anvin62b24d72007-08-29 16:38:05 +00001490
H. Peter Anvinc7131682017-03-07 17:45:01 -08001491 /* Not a directive, or even something that starts with [ */
H. Peter Anvin98578072018-06-01 18:02:54 -07001492 parse_line(pass1, line, &output_ins);
H. Peter Anvinc7131682017-03-07 17:45:01 -08001493
Chang S. Baea5786342018-08-15 23:22:21 +03001494 if (optimizing.level > 0) {
H. Peter Anvinc7131682017-03-07 17:45:01 -08001495 if (forwref != NULL && globallineno == forwref->lineno) {
1496 output_ins.forw_ref = true;
1497 do {
1498 output_ins.oprs[forwref->operand].opflags |= OPFLAG_FORWARD;
1499 forwref = saa_rstruct(forwrefs);
1500 } while (forwref != NULL
1501 && forwref->lineno == globallineno);
1502 } else
1503 output_ins.forw_ref = false;
1504
1505 if (output_ins.forw_ref) {
1506 if (passn == 1) {
1507 for (i = 0; i < output_ins.operands; i++) {
1508 if (output_ins.oprs[i].opflags & OPFLAG_FORWARD) {
1509 struct forwrefinfo *fwinf = (struct forwrefinfo *)saa_wstruct(forwrefs);
1510 fwinf->lineno = globallineno;
1511 fwinf->operand = i;
Cyrill Gorcunovd5f2aef2010-04-20 15:33:45 +04001512 }
1513 }
1514 }
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001515 }
H. Peter Anvinc7131682017-03-07 17:45:01 -08001516 }
H. Peter Anvin8bec2c72009-08-08 13:49:00 -07001517
H. Peter Anvinc7131682017-03-07 17:45:01 -08001518 /* forw_ref */
1519 if (output_ins.opcode == I_EQU) {
Cyrill Gorcunove996d282018-10-13 16:18:16 +03001520 if (!output_ins.label) {
1521 nasm_error(ERR_NONFATAL, "EQU not preceded by label");
1522 } else if (output_ins.operands == 1 &&
1523 (output_ins.oprs[0].type & IMMEDIATE) &&
1524 output_ins.oprs[0].wrt == NO_SEG) {
H. Peter Anvin98578072018-06-01 18:02:54 -07001525 define_label(output_ins.label,
1526 output_ins.oprs[0].segment,
1527 output_ins.oprs[0].offset, false);
1528 } else if (output_ins.operands == 2
1529 && (output_ins.oprs[0].type & IMMEDIATE)
1530 && (output_ins.oprs[0].type & COLON)
1531 && output_ins.oprs[0].segment == NO_SEG
1532 && output_ins.oprs[0].wrt == NO_SEG
1533 && (output_ins.oprs[1].type & IMMEDIATE)
1534 && output_ins.oprs[1].segment == NO_SEG
1535 && output_ins.oprs[1].wrt == NO_SEG) {
1536 define_label(output_ins.label,
1537 output_ins.oprs[0].offset | SEG_ABS,
1538 output_ins.oprs[1].offset, false);
H. Peter Anvinc7131682017-03-07 17:45:01 -08001539 } else {
H. Peter Anvin98578072018-06-01 18:02:54 -07001540 nasm_error(ERR_NONFATAL, "bad syntax for EQU");
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001541 }
H. Peter Anvinc7131682017-03-07 17:45:01 -08001542 } else { /* instruction isn't an EQU */
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001543 int32_t n;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001544
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001545 nasm_assert(output_ins.times >= 0);
H. Peter Anvinc7131682017-03-07 17:45:01 -08001546
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001547 for (n = 1; n <= output_ins.times; n++) {
1548 if (pass1 == 1) {
H. Peter Anvin892c4812018-05-30 14:43:46 -07001549 int64_t l = insn_size(location.segment,
1550 location.offset,
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001551 globalbits, &output_ins);
1552
1553 /* if (using_debug_info) && output_ins.opcode != -1) */
1554 if (using_debug_info)
1555 { /* fbk 03/25/01 */
H. Peter Anvinc7131682017-03-07 17:45:01 -08001556 /* this is done here so we can do debug type info */
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001557 int32_t typeinfo =
1558 TYS_ELEMENTS(output_ins.operands);
1559 switch (output_ins.opcode) {
1560 case I_RESB:
1561 typeinfo =
1562 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_BYTE;
1563 break;
1564 case I_RESW:
1565 typeinfo =
1566 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_WORD;
1567 break;
1568 case I_RESD:
1569 typeinfo =
1570 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_DWORD;
1571 break;
1572 case I_RESQ:
1573 typeinfo =
1574 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_QWORD;
1575 break;
1576 case I_REST:
1577 typeinfo =
1578 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_TBYTE;
1579 break;
1580 case I_RESO:
1581 typeinfo =
1582 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_OWORD;
1583 break;
1584 case I_RESY:
1585 typeinfo =
1586 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_YWORD;
1587 break;
1588 case I_RESZ:
1589 typeinfo =
1590 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_ZWORD;
1591 break;
1592 case I_DB:
1593 typeinfo |= TY_BYTE;
1594 break;
1595 case I_DW:
1596 typeinfo |= TY_WORD;
1597 break;
1598 case I_DD:
1599 if (output_ins.eops_float)
1600 typeinfo |= TY_FLOAT;
1601 else
1602 typeinfo |= TY_DWORD;
1603 break;
1604 case I_DQ:
1605 typeinfo |= TY_QWORD;
1606 break;
1607 case I_DT:
1608 typeinfo |= TY_TBYTE;
1609 break;
1610 case I_DO:
1611 typeinfo |= TY_OWORD;
1612 break;
1613 case I_DY:
1614 typeinfo |= TY_YWORD;
1615 break;
1616 case I_DZ:
1617 typeinfo |= TY_ZWORD;
1618 break;
1619 default:
1620 typeinfo = TY_LABEL;
1621 break;
1622 }
H. Peter Anvinc7131682017-03-07 17:45:01 -08001623
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001624 dfmt->debug_typevalue(typeinfo);
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001625 }
H. Peter Anvinc7131682017-03-07 17:45:01 -08001626
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001627 /*
1628 * For INCBIN, let the code in assemble
1629 * handle TIMES, so we don't have to read the
1630 * input file over and over.
1631 */
1632 if (l != -1) {
H. Peter Anvin892c4812018-05-30 14:43:46 -07001633 increment_offset(l);
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001634 }
1635 /*
1636 * else l == -1 => invalid instruction, which will be
1637 * flagged as an error on pass 2
1638 */
1639 } else {
1640 if (n == 2)
1641 lfmt->uplevel(LIST_TIMES);
H. Peter Anvin892c4812018-05-30 14:43:46 -07001642 increment_offset(assemble(location.segment,
1643 location.offset,
1644 globalbits, &output_ins));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001645 }
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001646 } /* not an EQU */
1647 }
1648 if (output_ins.times > 1)
1649 lfmt->downlevel(LIST_TIMES);
H. Peter Anvinc7131682017-03-07 17:45:01 -08001650
H. Peter Anvinc7131682017-03-07 17:45:01 -08001651 cleanup_insn(&output_ins);
1652
1653 end_of_line:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001654 nasm_free(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001655 } /* end while (line = preproc->getline... */
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001656
H. Peter Anvin (Intel)b45c03a2018-06-27 21:03:38 -07001657 if (global_offset_changed && !terminate_after_phase) {
1658 switch (pass0) {
1659 case 1:
1660 nasm_error(ERR_WARNING|ERR_WARN_PHASE,
1661 "phase error during stabilization pass, hoping for the best");
1662 break;
1663
1664 case 2:
1665 nasm_error(ERR_NONFATAL,
1666 "phase error during code generation pass");
1667 break;
1668
1669 default:
1670 /* This is normal, we'll keep going... */
1671 break;
1672 }
1673 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001674
H. Peter Anvine2c80182005-01-15 22:15:51 +00001675 if (pass1 == 1)
1676 preproc->cleanup(1);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001677
H. Peter Anvin (Intel)12810fa2018-06-27 20:17:33 -07001678 /*
1679 * Always run at least two optimization passes (pass0 == 0);
1680 * things like subsections will fail miserably without that.
1681 * Once we commit to a stabilization pass (pass0 == 1), we can't
1682 * go back, and if something goes bad, we can only hope
1683 * that we don't end up with a phase error at the end.
1684 */
1685 if ((passn > 1 && !global_offset_changed) || pass0 > 0) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001686 pass0++;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001687 } else if (global_offset_changed &&
1688 global_offset_changed < prev_offset_changed) {
Charles Craynec1905c22008-09-11 18:54:06 -07001689 prev_offset_changed = global_offset_changed;
1690 stall_count = 0;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001691 } else {
1692 stall_count++;
1693 }
Victor van den Elzen42528232008-09-11 15:07:05 +02001694
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001695 if (terminate_after_phase)
1696 break;
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001697
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001698 if ((stall_count > nasm_limit[LIMIT_STALLED]) ||
1699 (passn >= nasm_limit[LIMIT_PASSES])) {
Victor van den Elzen42528232008-09-11 15:07:05 +02001700 /* We get here if the labels don't converge
1701 * Example: FOO equ FOO + 1
1702 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001703 nasm_error(ERR_NONFATAL,
Victor van den Elzen42528232008-09-11 15:07:05 +02001704 "Can't find valid values for all labels "
H. Peter Anvina3d96d02018-06-15 17:51:39 -07001705 "after %"PRId64" passes, giving up.", passn);
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001706 nasm_error(ERR_NONFATAL,
1707 "Possible causes: recursive EQUs, macro abuse.");
1708 break;
1709 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001710 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001711
H. Peter Anvine2c80182005-01-15 22:15:51 +00001712 preproc->cleanup(0);
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08001713 lfmt->cleanup();
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001714 if (!terminate_after_phase && opt_verbose_info) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001715 /* -On and -Ov switches */
H. Peter Anvina3d96d02018-06-15 17:51:39 -07001716 fprintf(stdout, "info: assembly required 1+%"PRId64"+1 passes\n",
1717 passn-3);
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001718 }
1719}
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001720
Ed Berosetfa771012002-06-09 20:56:40 +00001721/**
1722 * gnu style error reporting
1723 * This function prints an error message to error_file in the
1724 * style used by GNU. An example would be:
1725 * file.asm:50: error: blah blah blah
H. Peter Anvin70653092007-10-19 14:42:29 -07001726 * where file.asm is the name of the file, 50 is the line number on
Ed Berosetfa771012002-06-09 20:56:40 +00001727 * which the error occurs (or is detected) and "error:" is one of
1728 * the possible optional diagnostics -- it can be "error" or "warning"
H. Peter Anvin70653092007-10-19 14:42:29 -07001729 * or something else. Finally the line terminates with the actual
Ed Berosetfa771012002-06-09 20:56:40 +00001730 * error message.
H. Peter Anvin70653092007-10-19 14:42:29 -07001731 *
1732 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001733 * @param fmt the printf style format string
1734 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001735static void nasm_verror_gnu(int severity, const char *fmt, va_list ap)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001736{
H. Peter Anvin274cda82016-05-10 02:56:29 -07001737 const char *currentfile = NULL;
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001738 int32_t lineno = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001739
Ed Berosetfa771012002-06-09 20:56:40 +00001740 if (is_suppressed_warning(severity))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001741 return;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001742
H. Peter Anvin7a6bf742017-12-20 11:54:31 -08001743 if (!(severity & ERR_NOFILE)) {
H. Peter Anvin4def1a82016-05-09 13:59:44 -07001744 src_get(&lineno, &currentfile);
H. Peter Anvin7a6bf742017-12-20 11:54:31 -08001745 if (!currentfile || (severity & ERR_TOPFILE)) {
Cyrill Gorcunovf5a48a62018-11-24 12:02:13 +03001746 currentfile = inname && inname[0] ?
1747 inname : outname && outname[0] ?
1748 outname : NULL;
H. Peter Anvin7a6bf742017-12-20 11:54:31 -08001749 lineno = 0;
1750 }
1751 }
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001752
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001753 if (!skip_this_pass(severity)) {
H. Peter Anvin7a6bf742017-12-20 11:54:31 -08001754 if (!lineno)
1755 fprintf(error_file, "%s:", currentfile ? currentfile : "nasm");
H. Peter Anvin883985d2017-12-20 11:32:39 -08001756 else
1757 fprintf(error_file, "%s:%"PRId32": ", currentfile, lineno);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001758 }
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001759
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001760 nasm_verror_common(severity, fmt, ap);
Ed Berosetfa771012002-06-09 20:56:40 +00001761}
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001762
Ed Berosetfa771012002-06-09 20:56:40 +00001763/**
1764 * MS style error reporting
1765 * This function prints an error message to error_file in the
H. Peter Anvin70653092007-10-19 14:42:29 -07001766 * style used by Visual C and some other Microsoft tools. An example
Ed Berosetfa771012002-06-09 20:56:40 +00001767 * would be:
Ed Beroset6e61d0d2002-06-11 03:29:36 +00001768 * file.asm(50) : error: blah blah blah
H. Peter Anvin70653092007-10-19 14:42:29 -07001769 * where file.asm is the name of the file, 50 is the line number on
Ed Beroset6e61d0d2002-06-11 03:29:36 +00001770 * which the error occurs (or is detected) and "error:" is one of
1771 * the possible optional diagnostics -- it can be "error" or "warning"
H. Peter Anvin70653092007-10-19 14:42:29 -07001772 * or something else. Finally the line terminates with the actual
Ed Beroset6e61d0d2002-06-11 03:29:36 +00001773 * error message.
H. Peter Anvin70653092007-10-19 14:42:29 -07001774 *
1775 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001776 * @param fmt the printf style format string
1777 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001778static void nasm_verror_vc(int severity, const char *fmt, va_list ap)
Ed Berosetfa771012002-06-09 20:56:40 +00001779{
H. Peter Anvin274cda82016-05-10 02:56:29 -07001780 const char *currentfile = NULL;
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001781 int32_t lineno = 0;
Ed Berosetfa771012002-06-09 20:56:40 +00001782
H. Peter Anvine2c80182005-01-15 22:15:51 +00001783 if (is_suppressed_warning(severity))
1784 return;
Ed Berosetfa771012002-06-09 20:56:40 +00001785
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001786 if (!(severity & ERR_NOFILE))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001787 src_get(&lineno, &currentfile);
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001788
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001789 if (!skip_this_pass(severity)) {
1790 if (currentfile) {
1791 fprintf(error_file, "%s(%"PRId32") : ", currentfile, lineno);
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001792 } else {
1793 fputs("nasm: ", error_file);
1794 }
Ed Berosetfa771012002-06-09 20:56:40 +00001795 }
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001796
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001797 nasm_verror_common(severity, fmt, ap);
Ed Berosetfa771012002-06-09 20:56:40 +00001798}
1799
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001800/*
1801 * check to see if this is a suppressable warning
1802 */
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001803static inline bool is_valid_warning(int severity)
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001804{
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001805 /* Not a warning at all */
1806 if ((severity & ERR_MASK) != ERR_WARNING)
1807 return false;
1808
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001809 return WARN_IDX(severity) < ERR_WARN_ALL;
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001810}
1811
Ed Berosetfa771012002-06-09 20:56:40 +00001812/**
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001813 * check for suppressed warning
H. Peter Anvin70653092007-10-19 14:42:29 -07001814 * checks for suppressed warning or pass one only warning and we're
Ed Berosetfa771012002-06-09 20:56:40 +00001815 * not in pass 1
1816 *
1817 * @param severity the severity of the warning or error
1818 * @return true if we should abort error/warning printing
1819 */
H. Peter Anvin2b046cf2008-01-22 14:08:36 -08001820static bool is_suppressed_warning(int severity)
Ed Berosetfa771012002-06-09 20:56:40 +00001821{
H. Peter Anvin442a05a2012-02-24 21:50:53 -08001822 /* Might be a warning but suppresed explicitly */
H. Peter Anvinc805fd72018-06-12 14:23:05 -07001823 if (is_valid_warning(severity) && !(severity & ERR_USAGE))
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001824 return !(warning_state[WARN_IDX(severity)] & WARN_ST_ENABLED);
1825 else
1826 return false;
1827}
1828
1829static bool warning_is_error(int severity)
1830{
1831 if (is_valid_warning(severity))
1832 return !!(warning_state[WARN_IDX(severity)] & WARN_ST_ERROR);
H. Peter Anvin442a05a2012-02-24 21:50:53 -08001833 else
1834 return false;
Ed Berosetfa771012002-06-09 20:56:40 +00001835}
1836
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001837static bool skip_this_pass(int severity)
1838{
H. Peter Anvin8f622462017-04-02 19:02:29 -07001839 /*
1840 * See if it's a pass-specific error or warning which should be skipped.
1841 * We cannot skip errors stronger than ERR_NONFATAL as by definition
1842 * they cannot be resumed from.
1843 */
1844 if ((severity & ERR_MASK) > ERR_NONFATAL)
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001845 return false;
1846
H. Peter Anvin934f0472016-05-09 12:00:19 -07001847 /*
1848 * passn is 1 on the very first pass only.
1849 * pass0 is 2 on the code-generation (final) pass only.
1850 * These are the passes we care about in this case.
1851 */
1852 return (((severity & ERR_PASS1) && passn != 1) ||
1853 ((severity & ERR_PASS2) && pass0 != 2));
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001854}
1855
Ed Berosetfa771012002-06-09 20:56:40 +00001856/**
1857 * common error reporting
1858 * This is the common back end of the error reporting schemes currently
H. Peter Anvin70653092007-10-19 14:42:29 -07001859 * implemented. It prints the nature of the warning and then the
Ed Berosetfa771012002-06-09 20:56:40 +00001860 * specific error message to error_file and may or may not return. It
1861 * doesn't return if the error severity is a "panic" or "debug" type.
H. Peter Anvin70653092007-10-19 14:42:29 -07001862 *
1863 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001864 * @param fmt the printf style format string
1865 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001866static void nasm_verror_common(int severity, const char *fmt, va_list args)
Ed Berosetfa771012002-06-09 20:56:40 +00001867{
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001868 char msg[1024];
1869 const char *pfx;
H. Peter Anvin323fcff2009-07-12 12:04:56 -07001870
H. Peter Anvin7df04172008-06-10 18:27:38 -07001871 switch (severity & (ERR_MASK|ERR_NO_SEVERITY)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001872 case ERR_WARNING:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001873 pfx = "warning: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001874 break;
1875 case ERR_NONFATAL:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001876 pfx = "error: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001877 break;
1878 case ERR_FATAL:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001879 pfx = "fatal: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001880 break;
1881 case ERR_PANIC:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001882 pfx = "panic: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001883 break;
1884 case ERR_DEBUG:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001885 pfx = "debug: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001886 break;
H. Peter Anvin7df04172008-06-10 18:27:38 -07001887 default:
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001888 pfx = "";
1889 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001890 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00001891
H. Peter Anvin934f0472016-05-09 12:00:19 -07001892 vsnprintf(msg, sizeof msg - 64, fmt, args);
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001893 if (is_valid_warning(severity) && WARN_IDX(severity) != ERR_WARN_OTHER) {
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001894 char *p = strchr(msg, '\0');
H. Peter Anvin934f0472016-05-09 12:00:19 -07001895 snprintf(p, 64, " [-w+%s]", warnings[WARN_IDX(severity)].name);
1896 }
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001897
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001898 if (!skip_this_pass(severity))
1899 fprintf(error_file, "%s%s\n", pfx, msg);
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001900
H. Peter Anvin4def1a82016-05-09 13:59:44 -07001901 /* Are we recursing from error_list_macros? */
1902 if (severity & ERR_PP_LISTMACRO)
1903 return;
1904
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001905 /*
1906 * Don't suppress this with skip_this_pass(), or we don't get
H. Peter Anvin934f0472016-05-09 12:00:19 -07001907 * pass1 or preprocessor warnings in the list file
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001908 */
H. Peter Anvin0fcb4882016-07-06 11:55:25 -07001909 lfmt->error(severity, pfx, msg);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001910
H. Peter Anvin8f622462017-04-02 19:02:29 -07001911 if (skip_this_pass(severity))
1912 return;
1913
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001914 if (severity & ERR_USAGE)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001915 want_usage = true;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001916
H. Peter Anvin4def1a82016-05-09 13:59:44 -07001917 preproc->error_list_macros(severity);
1918
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001919 switch (severity & ERR_MASK) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001920 case ERR_DEBUG:
1921 /* no further action, by definition */
1922 break;
H. Peter Anvinb030c922007-11-13 11:31:15 -08001923 case ERR_WARNING:
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001924 /* Treat warnings as errors */
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001925 if (warning_is_error(severity))
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001926 terminate_after_phase = true;
1927 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001928 case ERR_NONFATAL:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001929 terminate_after_phase = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001930 break;
1931 case ERR_FATAL:
1932 if (ofile) {
1933 fclose(ofile);
H. Peter Anvin29695c82018-06-14 17:04:32 -07001934 if (!keep_all)
1935 remove(outname);
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001936 ofile = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001937 }
1938 if (want_usage)
1939 usage();
1940 exit(1); /* instantly die */
1941 break; /* placate silly compilers */
1942 case ERR_PANIC:
1943 fflush(NULL);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001944
1945 if (abort_on_panic)
1946 abort(); /* halt, catch fire, dump core/stop debugger */
1947
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001948 if (ofile) {
1949 fclose(ofile);
H. Peter Anvin29695c82018-06-14 17:04:32 -07001950 if (!keep_all)
1951 remove(outname);
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001952 ofile = NULL;
1953 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001954 exit(3);
1955 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001956 }
1957}
1958
H. Peter Anvin734b1882002-04-30 21:01:08 +00001959static void usage(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001960{
H. Peter Anvin620515a2002-04-30 20:57:38 +00001961 fputs("type `nasm -h' for help\n", error_file);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001962}
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001963
1964static void help(const char xopt)
1965{
1966 int i;
1967
1968 printf
1969 ("usage: nasm [-@ response file] [-o outfile] [-f format] "
1970 "[-l listfile]\n"
1971 " [options...] [--] filename\n"
1972 " or nasm -v (or --v) for version info\n\n"
1973 "\n"
1974 "Response files should contain command line parameters,\n"
1975 "one per line.\n"
1976 "\n"
1977 " -t assemble in SciTech TASM compatible mode\n");
1978 printf
1979 (" -E (or -e) preprocess only (writes output to stdout by default)\n"
1980 " -a don't preprocess (assemble only)\n"
1981 " -M generate Makefile dependencies on stdout\n"
1982 " -MG d:o, missing files assumed generated\n"
1983 " -MF file set Makefile dependency file\n"
1984 " -MD file assemble and generate dependencies\n"
1985 " -MT file dependency target name\n"
1986 " -MQ file dependency target name (quoted)\n"
1987 " -MP emit phony target\n\n"
1988 " -Zfile redirect error messages to file\n"
1989 " -s redirect error messages to stdout\n\n"
1990 " -g generate debugging information\n\n"
1991 " -F format select a debugging format\n\n"
1992 " -gformat same as -g -F format\n\n"
1993 " -o outfile write output to an outfile\n\n"
1994 " -f format select an output format\n\n"
1995 " -l listfile write listing to a listfile\n\n"
1996 " -Ipath add a pathname to the include file path\n");
1997 printf
1998 (" -Olevel optimize opcodes, immediates and branch offsets\n"
1999 " -O0 no optimization\n"
2000 " -O1 minimal optimization\n"
2001 " -Ox multipass optimization (default)\n"
2002 " -Pfile pre-include a file (also --include)\n"
2003 " -Dmacro[=str] pre-define a macro\n"
2004 " -Umacro undefine a macro\n"
2005 " -Xformat specifiy error reporting format (gnu or vc)\n"
2006 " -w+foo enable warning foo (equiv. -Wfoo)\n"
2007 " -w-foo disable warning foo (equiv. -Wno-foo)\n"
2008 " -w[+-]error[=foo]\n"
2009 " promote [specific] warnings to errors\n"
Chang S. Bae1af6ef42018-06-20 17:05:12 -07002010 " -h show invocation summary and exit (also --help)\n\n"
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002011 " --pragma str pre-executes a specific %%pragma\n"
2012 " --before str add line (usually a preprocessor statement) before the input\n"
2013 " --prefix str prepend the given string to all the given string\n"
Chang S. Bae1af6ef42018-06-20 17:05:12 -07002014 " to all extern, common and global symbols (also --gprefix)\n"
2015 " --postfix str append the given string to all the given string\n"
2016 " to all extern, common and global symbols (also --gpostfix)\n"
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002017 " --lprefix str prepend the given string to all other symbols\n"
Chang S. Bae1af6ef42018-06-20 17:05:12 -07002018 " --lpostfix str append the given string to all other symbols\n"
2019 " --keep-all output files will not be removed even if an error happens\n"
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002020 " --limit-X val set execution limit X\n");
2021
2022 for (i = 0; i <= LIMIT_MAX; i++) {
2023 printf(" %-15s %s (default ",
2024 limit_info[i].name, limit_info[i].help);
2025 if (nasm_limit[i] < LIMIT_MAX_VAL) {
H. Peter Anvina3d96d02018-06-15 17:51:39 -07002026 printf("%"PRId64")\n", nasm_limit[i]);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002027 } else {
2028 printf("unlimited)\n");
2029 }
2030 }
2031
2032 printf("\nWarnings for the -W/-w options:\n");
2033
2034 for (i = 0; i <= ERR_WARN_ALL; i++)
2035 printf(" %-23s %s%s\n",
2036 warnings[i].name, warnings[i].help,
2037 i == ERR_WARN_ALL ? "\n" :
2038 warnings[i].enabled ? " (default on)" :
2039 " (default off)");
2040
2041 if (xopt == 'f') {
2042 printf("valid output formats for -f are"
2043 " (`*' denotes default):\n");
2044 ofmt_list(ofmt, stdout);
2045 } else {
2046 printf("For a list of valid output formats, use -hf.\n");
2047 printf("For a list of debug formats, use -f <format> -y.\n");
2048 }
2049}