blob: bf76fb97ecfe0158197cbf41eaa6971dd96220bd [file] [log] [blame]
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07001/* ----------------------------------------------------------------------- *
H. Peter Anvin323fcff2009-07-12 12:04:56 -07002 *
H. Peter Anvin3366e312018-02-07 14:14:36 -08003 * Copyright 1996-2018 The NASM Authors - All Rights Reserved
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07004 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00006 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07007 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
H. Peter Anvin323fcff2009-07-12 12:04:56 -070017 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -070018 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 * ----------------------------------------------------------------------- */
33
H. Peter Anvin323fcff2009-07-12 12:04:56 -070034/*
H. Peter Anvin9e6747c2009-06-28 17:13:04 -070035 * The Netwide Assembler main program module
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000036 */
37
H. Peter Anvinfe501952007-10-02 21:53:51 -070038#include "compiler.h"
39
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000040#include <stdio.h>
41#include <stdarg.h>
42#include <stdlib.h>
43#include <string.h>
44#include <ctype.h>
H. Peter Anvinfd7dd112007-10-10 14:06:59 -070045#include <limits.h>
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000046
47#include "nasm.h"
48#include "nasmlib.h"
H. Peter Anvinb20bc732017-03-07 19:23:03 -080049#include "error.h"
H. Peter Anvin1803ded2008-06-09 17:32:43 -070050#include "saa.h"
H. Peter Anvinfcb89092008-06-09 17:40:16 -070051#include "raa.h"
H. Peter Anvinf6c9e652007-10-16 14:40:27 -070052#include "float.h"
H. Peter Anvin74cc5e52007-08-30 22:35:34 +000053#include "stdscan.h"
H. Peter Anvinaf535c12002-04-30 20:59:21 +000054#include "insns.h"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000055#include "preproc.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000056#include "parser.h"
H. Peter Anvin76690a12002-04-30 20:52:49 +000057#include "eval.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000058#include "assemble.h"
59#include "labels.h"
H. Peter Anvine1f985c2016-05-25 12:06:29 -070060#include "outform.h"
H. Peter Anvin6768eb72002-04-30 20:52:26 +000061#include "listing.h"
Cyrill Gorcunov08359152013-11-09 22:16:11 +040062#include "iflag.h"
H. Peter Anvin2bc0ab32016-03-08 02:17:36 -080063#include "ver.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000064
H. Peter Anvin31387b22010-07-15 18:28:52 -070065/*
66 * This is the maximum number of optimization passes to do. If we ever
67 * find a case where the optimizer doesn't naturally converge, we might
68 * have to drop this value so the assembler doesn't appear to just hang.
69 */
70#define MAX_OPTIMIZE (INT_MAX >> 1)
71
H. Peter Anvine2c80182005-01-15 22:15:51 +000072struct forwrefinfo { /* info held on forward refs. */
H. Peter Anvineba20a72002-04-30 20:53:55 +000073 int lineno;
74 int operand;
75};
76
H. Peter Anvin55568c12016-10-03 19:46:49 -070077static void parse_cmdline(int, char **, int);
H. Peter Anvin81b62b92017-12-20 13:33:49 -080078static void assemble_file(const char *, StrList **);
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -080079static bool skip_this_pass(int severity);
H. Peter Anvin9bd15062009-07-18 21:07:17 -040080static void nasm_verror_gnu(int severity, const char *fmt, va_list args);
81static void nasm_verror_vc(int severity, const char *fmt, va_list args);
82static void nasm_verror_common(int severity, const char *fmt, va_list args);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000083static void usage(void);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -070084static void help(char xopt);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000085
H. Peter Anvin283b3fb2016-03-07 23:18:30 -080086static bool using_debug_info, opt_verbose_info;
87static const char *debug_format;
88
H. Peter Anvin3366e312018-02-07 14:14:36 -080089#ifndef ABORT_ON_PANIC
90# define ABORT_ON_PANIC 0
91#endif
92static bool abort_on_panic = ABORT_ON_PANIC;
H. Peter Anvin29695c82018-06-14 17:04:32 -070093static bool keep_all;
H. Peter Anvin3366e312018-02-07 14:14:36 -080094
H. Peter Anvin6867acc2007-10-10 14:58:45 -070095bool tasm_compatible_mode = false;
H. Peter Anvin79561022018-06-15 17:51:39 -070096int pass0;
97int64_t passn;
H. Peter Anvinc7131682017-03-07 17:45:01 -080098static int pass1, pass2; /* XXX: Get rid of these, they are redundant */
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +000099int globalrel = 0;
Jin Kyu Songb287ff02013-12-04 20:05:55 -0800100int globalbnd = 0;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000101
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700102struct compile_time official_compile_time;
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800103
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800104const char *inname;
105const char *outname;
106static const char *listname;
107static const char *errname;
108
H. Peter Anvin79561022018-06-15 17:51:39 -0700109static int64_t globallineno; /* for forward-reference tracking */
Chang S. Baef0ceb1e2018-05-02 08:07:53 -0700110
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000111/* static int pass = 0; */
H. Peter Anvin338656c2016-02-17 20:59:22 -0800112const struct ofmt *ofmt = &OF_DEFAULT;
113const struct ofmt_alias *ofmt_alias = NULL;
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400114const struct dfmt *dfmt;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000115
H. Peter Anvine2c80182005-01-15 22:15:51 +0000116static FILE *error_file; /* Where to write error messages */
H. Peter Anvin620515a2002-04-30 20:57:38 +0000117
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400118FILE *ofile = NULL;
Chang S. Baea5786342018-08-15 23:22:21 +0300119struct optimization optimizing =
120 { MAX_OPTIMIZE, OPTIM_ALL_ENABLED }; /* number of optimization passes to take */
Martin Lindhe8cc93f52016-11-16 16:48:13 +0100121static int cmd_sb = 16; /* by default */
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400122
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800123iflag_t cpu;
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400124static iflag_t cmd_cpu;
125
H. Peter Anvincd7893d2016-02-18 01:25:46 -0800126struct location location;
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800127bool in_absolute; /* Flag we are in ABSOLUTE seg */
128struct location absolute; /* Segment/offset inside ABSOLUTE */
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000129
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000130static struct RAA *offsets;
H. Peter Anvinea838272002-04-30 20:51:53 +0000131
H. Peter Anvine2c80182005-01-15 22:15:51 +0000132static struct SAA *forwrefs; /* keep track of forward references */
H. Peter Anvin9d637df2007-10-04 13:42:56 -0700133static const struct forwrefinfo *forwref;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000134
H. Peter Anvine7469712016-02-18 02:20:59 -0800135static const struct preproc_ops *preproc;
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800136static StrList *include_path;
Cyrill Gorcunov86b2ad02011-07-01 10:38:25 +0400137
H. Peter Anvin34754622018-11-28 12:36:53 -0800138#define OP_NORMAL (1U << 0)
139#define OP_PREPROCESS (1U << 1)
140#define OP_DEPEND (1U << 2)
Cyrill Gorcunove9fc88c2014-06-23 02:22:02 +0400141
142static unsigned int operating_mode;
Cyrill Gorcunov3ed32cb2014-06-22 20:53:12 +0400143
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700144/* Dependency flags */
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700145static bool depend_emit_phony = false;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700146static bool depend_missing_ok = false;
147static const char *depend_target = NULL;
148static const char *depend_file = NULL;
H. Peter Anvina771be82017-08-16 14:53:18 -0700149StrList *depend_list;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000150
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700151static bool want_usage;
152static bool terminate_after_phase;
H. Peter Anvin130736c2016-02-17 20:27:41 -0800153bool user_nolist = false;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000154
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700155static char *quote_for_pmake(const char *str);
156static char *quote_for_wmake(const char *str);
157static char *(*quote_for_make)(const char *) = quote_for_pmake;
H. Peter Anvin55340992012-09-09 17:09:00 -0700158
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700159/*
160 * Execution limits that can be set via a command-line option or %pragma
161 */
162
H. Peter Anvin79561022018-06-15 17:51:39 -0700163#define LIMIT_MAX_VAL (INT64_MAX >> 1) /* Effectively unlimited */
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700164
H. Peter Anvin79561022018-06-15 17:51:39 -0700165int64_t nasm_limit[LIMIT_MAX+1] =
166{ LIMIT_MAX_VAL, 1000, 1000000, 1000000, 1000000, 2000000000 };
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700167
168struct limit_info {
169 const char *name;
170 const char *help;
171};
172static const struct limit_info limit_info[LIMIT_MAX+1] = {
173 { "passes", "total number of passes" },
174 { "stalled-passes", "number of passes without forward progress" },
175 { "macro-levels", "levels of macro expansion"},
176 { "rep", "%rep count" },
H. Peter Anvin79561022018-06-15 17:51:39 -0700177 { "eval", "expression evaluation descent"},
178 { "lines", "total source lines processed"}
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700179};
180
H. Peter Anvinc805fd72018-06-12 14:23:05 -0700181enum directive_result
182nasm_set_limit(const char *limit, const char *valstr)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700183{
184 int i;
185 int64_t val;
186 bool rn_error;
H. Peter Anvinc805fd72018-06-12 14:23:05 -0700187 int errlevel;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700188
189 for (i = 0; i <= LIMIT_MAX; i++) {
190 if (!nasm_stricmp(limit, limit_info[i].name))
191 break;
192 }
193 if (i > LIMIT_MAX) {
H. Peter Anvinc805fd72018-06-12 14:23:05 -0700194 if (passn == 0)
195 errlevel = ERR_WARNING|ERR_NOFILE|ERR_USAGE;
196 else
H. Peter Anvin (Intel)77f53ba2018-12-12 14:38:50 -0800197 errlevel = ERR_WARNING|ERR_PASS1|WARN_UNKNOWN_PRAGMA;
H. Peter Anvinc805fd72018-06-12 14:23:05 -0700198 nasm_error(errlevel, "unknown limit: `%s'", limit);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700199 return DIRR_ERROR;
200 }
201
202 if (!nasm_stricmp(valstr, "unlimited")) {
203 val = LIMIT_MAX_VAL;
204 } else {
205 val = readnum(valstr, &rn_error);
206 if (rn_error || val < 0) {
H. Peter Anvinc805fd72018-06-12 14:23:05 -0700207 if (passn == 0)
208 errlevel = ERR_WARNING|ERR_NOFILE|ERR_USAGE;
209 else
H. Peter Anvin (Intel)77f53ba2018-12-12 14:38:50 -0800210 errlevel = ERR_WARNING|ERR_PASS1|WARN_BAD_PRAGMA;
H. Peter Anvinc805fd72018-06-12 14:23:05 -0700211 nasm_error(errlevel, "invalid limit value: `%s'", limit);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700212 return DIRR_ERROR;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700213 }
H. Peter Anvinc805fd72018-06-12 14:23:05 -0700214 if (val > LIMIT_MAX_VAL)
215 val = LIMIT_MAX_VAL;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700216 }
217
218 nasm_limit[i] = val;
219 return DIRR_OK;
220}
221
H. Peter Anvin892c4812018-05-30 14:43:46 -0700222int64_t switch_segment(int32_t segment)
Cyrill Gorcunov190232f2013-02-15 12:35:04 +0400223{
H. Peter Anvin892c4812018-05-30 14:43:46 -0700224 location.segment = segment;
225 if (segment == NO_SEG) {
226 location.offset = absolute.offset;
227 in_absolute = true;
228 } else {
229 location.offset = raa_read(offsets, segment);
230 in_absolute = false;
231 }
232 return location.offset;
Cyrill Gorcunov190232f2013-02-15 12:35:04 +0400233}
234
235static void set_curr_offs(int64_t l_off)
236{
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800237 if (in_absolute)
238 absolute.offset = l_off;
Cyrill Gorcunov190232f2013-02-15 12:35:04 +0400239 else
240 offsets = raa_write(offsets, location.segment, l_off);
241}
242
H. Peter Anvin892c4812018-05-30 14:43:46 -0700243static void increment_offset(int64_t delta)
244{
245 if (unlikely(delta == 0))
246 return;
247
248 location.offset += delta;
249 set_curr_offs(location.offset);
250}
251
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000252static void nasm_fputs(const char *line, FILE * outfile)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000253{
H. Peter Anvin310b3e12002-05-14 22:38:55 +0000254 if (outfile) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000255 fputs(line, outfile);
H. Peter Anvind1fb15c2007-11-13 09:37:59 -0800256 putc('\n', outfile);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000257 } else
H. Peter Anvine2c80182005-01-15 22:15:51 +0000258 puts(line);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000259}
260
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800261/*
262 * Define system-defined macros that are not part of
263 * macros/standard.mac.
264 */
265static void define_macros(void)
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800266{
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700267 const struct compile_time * const oct = &official_compile_time;
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800268 char temp[128];
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800269
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700270 if (oct->have_local) {
271 strftime(temp, sizeof temp, "__DATE__=\"%Y-%m-%d\"", &oct->local);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400272 preproc->pre_define(temp);
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700273 strftime(temp, sizeof temp, "__DATE_NUM__=%Y%m%d", &oct->local);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400274 preproc->pre_define(temp);
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700275 strftime(temp, sizeof temp, "__TIME__=\"%H:%M:%S\"", &oct->local);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400276 preproc->pre_define(temp);
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700277 strftime(temp, sizeof temp, "__TIME_NUM__=%H%M%S", &oct->local);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400278 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800279 }
280
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700281 if (oct->have_gm) {
282 strftime(temp, sizeof temp, "__UTC_DATE__=\"%Y-%m-%d\"", &oct->gm);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400283 preproc->pre_define(temp);
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700284 strftime(temp, sizeof temp, "__UTC_DATE_NUM__=%Y%m%d", &oct->gm);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400285 preproc->pre_define(temp);
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700286 strftime(temp, sizeof temp, "__UTC_TIME__=\"%H:%M:%S\"", &oct->gm);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400287 preproc->pre_define(temp);
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700288 strftime(temp, sizeof temp, "__UTC_TIME_NUM__=%H%M%S", &oct->gm);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400289 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800290 }
H. Peter Anvind85d2502008-05-04 17:53:31 -0700291
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700292 if (oct->have_posix) {
293 snprintf(temp, sizeof temp, "__POSIX_TIME__=%"PRId64, oct->posix);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400294 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800295 }
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800296
Cyrill Gorcunovc1936da2011-04-06 18:32:15 +0400297 /*
298 * In case if output format is defined by alias
299 * we have to put shortname of the alias itself here
300 * otherwise ABI backward compatibility gets broken.
301 */
Cyrill Gorcunov69ce7502010-07-12 15:19:17 +0400302 snprintf(temp, sizeof(temp), "__OUTPUT_FORMAT__=%s",
Cyrill Gorcunovc1936da2011-04-06 18:32:15 +0400303 ofmt_alias ? ofmt_alias->shortname : ofmt->shortname);
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +0400304 preproc->pre_define(temp);
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800305
306 /*
307 * Output-format specific macros.
308 */
309 if (ofmt->stdmac)
310 preproc->extra_stdmac(ofmt->stdmac);
311
312 /*
313 * Debug format, if any
314 */
315 if (dfmt != &null_debug_form) {
316 snprintf(temp, sizeof(temp), "__DEBUG_FORMAT__=%s", dfmt->shortname);
317 preproc->pre_define(temp);
318 }
319}
320
321/*
322 * Initialize the preprocessor, set up the include path, and define
323 * the system-included macros. This is called between passes 1 and 2
324 * of parsing the command options; ofmt and dfmt are defined at this
325 * point.
326 *
327 * Command-line specified preprocessor directives (-p, -d, -u,
328 * --pragma, --before) are processed after this function.
329 */
330static void preproc_init(void)
331{
332 StrList *ip, *iptmp;
333
334 preproc->init();
335 define_macros();
336 list_for_each_safe(ip, iptmp, include_path) {
337 preproc->include_path(ip->str);
338 nasm_free(ip);
339 }
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800340}
341
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700342static void emit_dependencies(StrList *list)
343{
344 FILE *deps;
345 int linepos, len;
346 StrList *l, *nl;
H. Peter Anvinf05034f2017-08-16 22:08:36 -0700347 bool wmake = (quote_for_make == quote_for_wmake);
348 const char *wrapstr, *nulltarget;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700349
H. Peter Anvinf05034f2017-08-16 22:08:36 -0700350 wrapstr = wmake ? " &\n " : " \\\n ";
351 nulltarget = wmake ? "\t%null\n" : "";
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700352
353 if (depend_file && strcmp(depend_file, "-")) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -0700354 deps = nasm_open_write(depend_file, NF_TEXT);
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400355 if (!deps) {
356 nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
357 "unable to write dependency file `%s'", depend_file);
358 return;
359 }
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700360 } else {
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400361 deps = stdout;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700362 }
H. Peter Anvin323fcff2009-07-12 12:04:56 -0700363
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700364 linepos = fprintf(deps, "%s :", depend_target);
Cyrill Gorcunovfcd0a742009-07-27 16:26:26 +0400365 list_for_each(l, list) {
H. Peter Anvin55340992012-09-09 17:09:00 -0700366 char *file = quote_for_make(l->str);
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400367 len = strlen(file);
368 if (linepos + len > 62 && linepos > 1) {
H. Peter Anvinf05034f2017-08-16 22:08:36 -0700369 fputs(wrapstr, deps);
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400370 linepos = 1;
371 }
372 fprintf(deps, " %s", file);
373 linepos += len+1;
H. Peter Anvin55340992012-09-09 17:09:00 -0700374 nasm_free(file);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700375 }
376 fprintf(deps, "\n\n");
H. Peter Anvin323fcff2009-07-12 12:04:56 -0700377
Cyrill Gorcunovfcd0a742009-07-27 16:26:26 +0400378 list_for_each_safe(l, nl, list) {
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700379 if (depend_emit_phony) {
380 char *file = quote_for_make(l->str);
H. Peter Anvinf05034f2017-08-16 22:08:36 -0700381 fprintf(deps, "%s :\n%s\n", file, nulltarget);
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700382 nasm_free(file);
383 }
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400384 nasm_free(l);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700385 }
386
387 if (deps != stdout)
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400388 fclose(deps);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700389}
390
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700391/* Convert a struct tm to a POSIX-style time constant */
392static int64_t make_posix_time(const struct tm *tm)
393{
394 int64_t t;
395 int64_t y = tm->tm_year;
396
397 /* See IEEE 1003.1:2004, section 4.14 */
398
399 t = (y-70)*365 + (y-69)/4 - (y-1)/100 + (y+299)/400;
400 t += tm->tm_yday;
401 t *= 24;
402 t += tm->tm_hour;
403 t *= 60;
404 t += tm->tm_min;
405 t *= 60;
406 t += tm->tm_sec;
407
408 return t;
409}
410
411static void timestamp(void)
412{
413 struct compile_time * const oct = &official_compile_time;
414 const struct tm *tp, *best_gm;
415
416 time(&oct->t);
417
418 best_gm = NULL;
419
420 tp = localtime(&oct->t);
421 if (tp) {
422 oct->local = *tp;
423 best_gm = &oct->local;
424 oct->have_local = true;
425 }
426
427 tp = gmtime(&oct->t);
428 if (tp) {
429 oct->gm = *tp;
430 best_gm = &oct->gm;
431 oct->have_gm = true;
432 if (!oct->have_local)
433 oct->local = oct->gm;
434 } else {
435 oct->gm = oct->local;
436 }
437
438 if (best_gm) {
439 oct->posix = make_posix_time(best_gm);
440 oct->have_posix = true;
441 }
442}
443
H. Peter Anvin038d8612007-04-12 16:54:50 +0000444int main(int argc, char **argv)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000445{
H. Peter Anvina771be82017-08-16 14:53:18 -0700446 StrList **depend_ptr;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700447
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700448 timestamp();
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800449
H. Peter Anvina7ecf262018-02-06 14:43:07 -0800450 iflag_set_default_cpu(&cpu);
451 iflag_set_default_cpu(&cmd_cpu);
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400452
Charles Crayne2581c862008-09-10 19:21:52 -0700453 pass0 = 0;
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700454 want_usage = terminate_after_phase = false;
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400455 nasm_set_verror(nasm_verror_gnu);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000456
H. Peter Anvin97e15752007-09-25 08:47:47 -0700457 error_file = stderr;
458
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -0700459 tolower_init();
H. Peter Anvin274cda82016-05-10 02:56:29 -0700460 src_init();
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -0700461
H. Peter Anvin, Intel87d9e622018-06-25 12:58:49 -0700462 /*
463 * We must call init_labels() before the command line parsing,
464 * because we may be setting prefixes/suffixes from the command
465 * line.
466 */
467 init_labels();
468
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000469 offsets = raa_init();
Keith Kaniosb7a89542007-04-12 02:40:54 +0000470 forwrefs = saa_init((int32_t)sizeof(struct forwrefinfo));
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000471
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000472 preproc = &nasmpp;
Cyrill Gorcunov3ed32cb2014-06-22 20:53:12 +0400473 operating_mode = OP_NORMAL;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000474
H. Peter Anvin55568c12016-10-03 19:46:49 -0700475 parse_cmdline(argc, argv, 1);
476 if (terminate_after_phase) {
477 if (want_usage)
478 usage();
479 return 1;
480 }
481
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800482 /* At this point we have ofmt and the name of the desired debug format */
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800483 if (!using_debug_info) {
484 /* No debug info, redirect to the null backend (empty stubs) */
H. Peter Anvina7bc15d2016-02-17 20:55:08 -0800485 dfmt = &null_debug_form;
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800486 } else if (!debug_format) {
487 /* Default debug format for this backend */
H. Peter Anvina7bc15d2016-02-17 20:55:08 -0800488 dfmt = ofmt->default_dfmt;
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800489 } else {
490 dfmt = dfmt_find(ofmt, debug_format);
491 if (!dfmt) {
492 nasm_fatal(ERR_NOFILE | ERR_USAGE,
493 "unrecognized debug format `%s' for"
494 " output format `%s'",
495 debug_format, ofmt->shortname);
496 }
497 }
H. Peter Anvinbb88d012003-09-10 23:34:23 +0000498
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800499 preproc_init();
500
501 parse_cmdline(argc, argv, 2);
502 if (terminate_after_phase) {
503 if (want_usage)
504 usage();
505 return 1;
506 }
507
508 /* Save away the default state of warnings */
509 memcpy(warning_state_init, warning_state, sizeof warning_state);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000510
H. Peter Anvin34754622018-11-28 12:36:53 -0800511 /* Dependency filename if we are also doing other things */
512 if (!depend_file && (operating_mode & ~OP_DEPEND)) {
513 if (outname)
514 depend_file = nasm_strcat(outname, ".d");
515 else
516 depend_file = filename_set_extension(inname, ".d");
517 }
518
Cyrill Gorcunov69bb0522018-09-22 13:46:45 +0300519 /*
520 * If no output file name provided and this
H. Peter Anvin34754622018-11-28 12:36:53 -0800521 * is preprocess mode, we're perfectly
Cyrill Gorcunovda3780d2018-09-22 14:10:36 +0300522 * fine to output into stdout.
Cyrill Gorcunov69bb0522018-09-22 13:46:45 +0300523 */
H. Peter Anvin (Intel)7b6371b2018-11-20 10:56:57 -0800524 if (!outname && !(operating_mode & OP_PREPROCESS)) {
525 outname = filename_set_extension(inname, ofmt->extension);
526 if (!strcmp(outname, inname)) {
527 outname = "nasm.out";
528 nasm_error(ERR_WARNING,
529 "default output file same as input, using `%s' for output\n",
H. Peter Anvinda794322018-11-26 14:15:46 -0800530 outname);
H. Peter Anvin (Intel)7b6371b2018-11-20 10:56:57 -0800531 }
Cyrill Gorcunov69bb0522018-09-22 13:46:45 +0300532 }
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800533
H. Peter Anvin34754622018-11-28 12:36:53 -0800534 depend_ptr = (operating_mode & OP_DEPEND) ? &depend_list : NULL;
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
H. Peter Anvin34754622018-11-28 12:36:53 -0800539 if (!(operating_mode & (OP_PREPROCESS|OP_NORMAL))) {
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 Anvin130736c2016-02-17 20:27:41 -0800545 preproc->reset(inname, 0, depend_ptr);
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 Anvin41087062016-03-03 14:27:34 -0800559 nasm_fatal(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 Anvin130736c2016-02-17 20:27:41 -0800568 preproc->reset(inname, 3, depend_ptr);
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 Anvin41087062016-03-03 14:27:34 -0800604 nasm_fatal(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
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400610 assemble_file(inname, depend_ptr);
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();
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000642
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700643 return terminate_after_phase;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000644}
645
H. Peter Anvineba20a72002-04-30 20:53:55 +0000646/*
647 * Get a parameter for a command line option.
648 * First arg must be in the form of e.g. -f...
649 */
H. Peter Anvin423e3812007-11-15 17:12:29 -0800650static char *get_param(char *p, char *q, bool *advance)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000651{
H. Peter Anvin423e3812007-11-15 17:12:29 -0800652 *advance = false;
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +0400653 if (p[2]) /* the parameter's in the option */
654 return nasm_skip_spaces(p + 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000655 if (q && q[0]) {
H. Peter Anvin423e3812007-11-15 17:12:29 -0800656 *advance = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000657 return q;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000658 }
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400659 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000660 "option `-%c' requires an argument", p[1]);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000661 return NULL;
662}
663
H. Peter Anvindc242712007-11-18 11:55:10 -0800664/*
665 * Copy a filename
666 */
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800667static void copy_filename(const char **dst, const char *src, const char *what)
H. Peter Anvindc242712007-11-18 11:55:10 -0800668{
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800669 if (*dst)
670 nasm_fatal(0, "more than one %s file specified: %s\n", what, src);
H. Peter Anvindc242712007-11-18 11:55:10 -0800671
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800672 *dst = nasm_strdup(src);
H. Peter Anvindc242712007-11-18 11:55:10 -0800673}
674
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700675/*
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700676 * Convert a string to a POSIX make-safe form
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700677 */
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700678static char *quote_for_pmake(const char *str)
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700679{
680 const char *p;
681 char *os, *q;
682
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400683 size_t n = 1; /* Terminating zero */
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700684 size_t nbs = 0;
685
686 if (!str)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400687 return NULL;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700688
689 for (p = str; *p; p++) {
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400690 switch (*p) {
691 case ' ':
692 case '\t':
693 /* Convert N backslashes + ws -> 2N+1 backslashes + ws */
694 n += nbs + 2;
695 nbs = 0;
696 break;
697 case '$':
698 case '#':
699 nbs = 0;
700 n += 2;
701 break;
702 case '\\':
703 nbs++;
704 n++;
705 break;
706 default:
707 nbs = 0;
708 n++;
709 break;
710 }
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700711 }
712
713 /* Convert N backslashes at the end of filename to 2N backslashes */
714 if (nbs)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400715 n += nbs;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700716
717 os = q = nasm_malloc(n);
718
719 nbs = 0;
720 for (p = str; *p; p++) {
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400721 switch (*p) {
722 case ' ':
723 case '\t':
724 while (nbs--)
725 *q++ = '\\';
726 *q++ = '\\';
727 *q++ = *p;
728 break;
729 case '$':
730 *q++ = *p;
731 *q++ = *p;
732 nbs = 0;
733 break;
734 case '#':
735 *q++ = '\\';
736 *q++ = *p;
737 nbs = 0;
738 break;
739 case '\\':
740 *q++ = *p;
741 nbs++;
742 break;
743 default:
744 *q++ = *p;
745 nbs = 0;
746 break;
747 }
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700748 }
749 while (nbs--)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400750 *q++ = '\\';
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700751
752 *q = '\0';
753
754 return os;
755}
756
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700757/*
758 * Convert a string to a Watcom make-safe form
759 */
760static char *quote_for_wmake(const char *str)
761{
762 const char *p;
763 char *os, *q;
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700764 bool quote = false;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700765
766 size_t n = 1; /* Terminating zero */
767
768 if (!str)
769 return NULL;
770
771 for (p = str; *p; p++) {
772 switch (*p) {
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700773 case ' ':
774 case '\t':
H. Peter Anvin3e30c322017-08-16 22:20:36 -0700775 case '&':
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700776 quote = true;
777 n++;
778 break;
779 case '\"':
780 quote = true;
781 n += 2;
782 break;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700783 case '$':
784 case '#':
785 n += 2;
786 break;
787 default:
788 n++;
789 break;
790 }
791 }
792
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700793 if (quote)
794 n += 2;
795
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700796 os = q = nasm_malloc(n);
797
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700798 if (quote)
799 *q++ = '\"';
800
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700801 for (p = str; *p; p++) {
802 switch (*p) {
803 case '$':
804 case '#':
805 *q++ = '$';
806 *q++ = *p;
807 break;
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700808 case '\"':
809 *q++ = *p;
810 *q++ = *p;
811 break;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700812 default:
813 *q++ = *p;
814 break;
815 }
816 }
817
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700818 if (quote)
819 *q++ = '\"';
820
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700821 *q = '\0';
822
823 return os;
824}
825
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100826enum text_options {
H. Peter Anvin3366e312018-02-07 14:14:36 -0800827 OPT_BOGUS,
828 OPT_VERSION,
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700829 OPT_HELP,
H. Peter Anvin3366e312018-02-07 14:14:36 -0800830 OPT_ABORT_ON_PANIC,
H. Peter Anvin05990342018-06-11 13:32:42 -0700831 OPT_MANGLE,
832 OPT_INCLUDE,
833 OPT_PRAGMA,
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700834 OPT_BEFORE,
H. Peter Anvin29695c82018-06-14 17:04:32 -0700835 OPT_LIMIT,
836 OPT_KEEP_ALL
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100837};
H. Peter Anvin3366e312018-02-07 14:14:36 -0800838struct textargs {
839 const char *label;
840 enum text_options opt;
841 bool need_arg;
H. Peter Anvin98578072018-06-01 18:02:54 -0700842 int pvt;
H. Peter Anvin3366e312018-02-07 14:14:36 -0800843};
H. Peter Anvin2530a102016-02-18 02:28:15 -0800844static const struct textargs textopts[] = {
H. Peter Anvin98578072018-06-01 18:02:54 -0700845 {"v", OPT_VERSION, false, 0},
846 {"version", OPT_VERSION, false, 0},
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700847 {"help", OPT_HELP, false, 0},
H. Peter Anvin98578072018-06-01 18:02:54 -0700848 {"abort-on-panic", OPT_ABORT_ON_PANIC, false, 0},
849 {"prefix", OPT_MANGLE, true, LM_GPREFIX},
850 {"postfix", OPT_MANGLE, true, LM_GSUFFIX},
851 {"gprefix", OPT_MANGLE, true, LM_GPREFIX},
852 {"gpostfix", OPT_MANGLE, true, LM_GSUFFIX},
853 {"lprefix", OPT_MANGLE, true, LM_LPREFIX},
854 {"lpostfix", OPT_MANGLE, true, LM_LSUFFIX},
H. Peter Anvin05990342018-06-11 13:32:42 -0700855 {"include", OPT_INCLUDE, true, 0},
856 {"pragma", OPT_PRAGMA, true, 0},
857 {"before", OPT_BEFORE, true, 0},
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700858 {"limit-", OPT_LIMIT, true, 0},
H. Peter Anvin29695c82018-06-14 17:04:32 -0700859 {"keep-all", OPT_KEEP_ALL, false, 0},
H. Peter Anvin98578072018-06-01 18:02:54 -0700860 {NULL, OPT_BOGUS, false, 0}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000861};
862
Cyrill Gorcunove7438432014-05-09 22:34:34 +0400863static void show_version(void)
864{
865 printf("NASM version %s compiled on %s%s\n",
866 nasm_version, nasm_date, nasm_compile_options);
867 exit(0);
868}
869
H. Peter Anvin423e3812007-11-15 17:12:29 -0800870static bool stopoptions = false;
H. Peter Anvin55568c12016-10-03 19:46:49 -0700871static bool process_arg(char *p, char *q, int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000872{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000873 char *param;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800874 bool advance = false;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000875
876 if (!p || !p[0])
H. Peter Anvin423e3812007-11-15 17:12:29 -0800877 return false;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000878
H. Peter Anvine2c80182005-01-15 22:15:51 +0000879 if (p[0] == '-' && !stopoptions) {
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400880 if (strchr("oOfpPdDiIlFXuUZwW", p[1])) {
881 /* These parameters take values */
882 if (!(param = get_param(p, q, &advance)))
883 return advance;
884 }
H. Peter Anvin423e3812007-11-15 17:12:29 -0800885
H. Peter Anvine2c80182005-01-15 22:15:51 +0000886 switch (p[1]) {
887 case 's':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700888 if (pass == 1)
889 error_file = stdout;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000890 break;
H. Peter Anvin70653092007-10-19 14:42:29 -0700891
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400892 case 'o': /* output file */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700893 if (pass == 2)
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800894 copy_filename(&outname, param, "output");
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400895 break;
H. Peter Anvinfd7dd112007-10-10 14:06:59 -0700896
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400897 case 'f': /* output format */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700898 if (pass == 1) {
899 ofmt = ofmt_find(param, &ofmt_alias);
900 if (!ofmt) {
901 nasm_fatal(ERR_NOFILE | ERR_USAGE,
902 "unrecognised output format `%s' - "
903 "use -hf for a list", param);
904 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400905 }
906 break;
H. Peter Anvin70653092007-10-19 14:42:29 -0700907
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400908 case 'O': /* Optimization level */
H. Peter Anvinbf6230b2018-11-11 13:25:16 -0800909 if (pass == 1) {
H. Peter Anvin55568c12016-10-03 19:46:49 -0700910 int opt;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700911
H. Peter Anvin55568c12016-10-03 19:46:49 -0700912 if (!*param) {
913 /* Naked -O == -Ox */
Chang S. Baea5786342018-08-15 23:22:21 +0300914 optimizing.level = MAX_OPTIMIZE;
H. Peter Anvin55568c12016-10-03 19:46:49 -0700915 } else {
916 while (*param) {
917 switch (*param) {
918 case '0': case '1': case '2': case '3': case '4':
919 case '5': case '6': case '7': case '8': case '9':
920 opt = strtoul(param, &param, 10);
H. Peter Anvind85d2502008-05-04 17:53:31 -0700921
Chang S. Baea5786342018-08-15 23:22:21 +0300922 /* -O0 -> optimizing.level == -1, 0.98 behaviour */
923 /* -O1 -> optimizing.level == 0, 0.98.09 behaviour */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700924 if (opt < 2)
Chang S. Baea5786342018-08-15 23:22:21 +0300925 optimizing.level = opt - 1;
H. Peter Anvin55568c12016-10-03 19:46:49 -0700926 else
Chang S. Baea5786342018-08-15 23:22:21 +0300927 optimizing.level = opt;
H. Peter Anvin55568c12016-10-03 19:46:49 -0700928 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700929
H. Peter Anvin55568c12016-10-03 19:46:49 -0700930 case 'v':
931 case '+':
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400932 param++;
933 opt_verbose_info = true;
934 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700935
H. Peter Anvin55568c12016-10-03 19:46:49 -0700936 case 'x':
937 param++;
Chang S. Baea5786342018-08-15 23:22:21 +0300938 optimizing.level = MAX_OPTIMIZE;
H. Peter Anvin55568c12016-10-03 19:46:49 -0700939 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700940
H. Peter Anvin55568c12016-10-03 19:46:49 -0700941 default:
942 nasm_fatal(0,
943 "unknown optimization option -O%c\n",
944 *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)
975 nasm_add_string_to_strlist(&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
1002 nasm_fatal(ERR_NOFILE | ERR_USAGE,
1003 "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)) {
H. Peter Anvin (Intel)77f53ba2018-12-12 14:38:50 -08001052 nasm_error(ERR_WARNING|ERR_NOFILE|WARN_UNK_WARNING,
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001053 "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':
H. Peter Anvin34754622018-11-28 12:36:53 -08001086 operating_mode |= OP_DEPEND;
1087 if (q && (q[0] != '-' || q[1] == '\0')) {
1088 depend_file = q;
1089 advance = true;
1090 }
H. Peter Anvin55568c12016-10-03 19:46:49 -07001091 break;
1092 case 'F':
1093 depend_file = q;
1094 advance = true;
1095 break;
1096 case 'T':
1097 depend_target = q;
1098 advance = true;
1099 break;
1100 case 'Q':
1101 depend_target = quote_for_make(q);
1102 advance = true;
1103 break;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -07001104 case 'W':
1105 /* handled in pass 1 */
1106 break;
H. Peter Anvin55568c12016-10-03 19:46:49 -07001107 default:
1108 nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
1109 "unknown dependency option `-M%c'", p[2]);
1110 break;
1111 }
H. Peter Anvin77c9bf62017-08-16 21:14:33 -07001112 }
1113 if (advance && (!q || !q[0])) {
1114 nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
1115 "option `-M%c' requires a parameter", p[2]);
1116 break;
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001117 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001118 break;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001119
H. Peter Anvine2c80182005-01-15 22:15:51 +00001120 case '-':
1121 {
H. Peter Anvin3366e312018-02-07 14:14:36 -08001122 const struct textargs *tx;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001123 size_t olen, plen;
H. Peter Anvinc805fd72018-06-12 14:23:05 -07001124 char *eqsave;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001125
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001126 p += 2;
1127
1128 if (!*p) { /* -- => stop processing options */
H. Peter Anvin3366e312018-02-07 14:14:36 -08001129 stopoptions = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001130 break;
1131 }
Cyrill Gorcunove7438432014-05-09 22:34:34 +04001132
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001133 plen = strlen(p);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001134 for (tx = textopts; tx->label; tx++) {
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001135 olen = strlen(tx->label);
1136
1137 if (olen > plen)
1138 continue;
1139
1140 if (nasm_memicmp(p, tx->label, olen))
1141 continue;
1142
1143 if (tx->label[olen-1] == '-')
1144 break; /* Incomplete option */
1145
1146 if (!p[olen] || p[olen] == '=')
1147 break; /* Complete option */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001148 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001149
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001150 if (!tx->label) {
1151 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
1152 "unrecognized option `--%s'", p);
1153 }
1154
H. Peter Anvinc805fd72018-06-12 14:23:05 -07001155 eqsave = param = strchr(p+olen, '=');
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001156 if (param)
1157 *param++ = '\0';
1158
H. Peter Anvin3366e312018-02-07 14:14:36 -08001159 if (tx->need_arg) {
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001160 if (!param) {
1161 param = q;
1162 advance = true;
1163 }
1164
1165 /* Note: a null string is a valid parameter */
1166 if (!param) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001167 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvin3366e312018-02-07 14:14:36 -08001168 "option `--%s' requires an argument",
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001169 p);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001170 break;
1171 }
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001172 } else {
1173 if (param) {
1174 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
1175 "option `--%s' does not take an argument",
1176 p);
1177
1178 }
H. Peter Anvin3366e312018-02-07 14:14:36 -08001179 }
1180
1181 switch (tx->opt) {
1182 case OPT_VERSION:
1183 show_version();
1184 break;
1185 case OPT_ABORT_ON_PANIC:
1186 abort_on_panic = true;
1187 break;
H. Peter Anvin98578072018-06-01 18:02:54 -07001188 case OPT_MANGLE:
H. Peter Anvin3366e312018-02-07 14:14:36 -08001189 if (pass == 2)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001190 set_label_mangle(tx->pvt, param);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001191 break;
H. Peter Anvin05990342018-06-11 13:32:42 -07001192 case OPT_INCLUDE:
1193 if (pass == 2)
1194 preproc->pre_include(q);
1195 break;
1196 case OPT_PRAGMA:
1197 if (pass == 2)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001198 preproc->pre_command("pragma", param);
H. Peter Anvin05990342018-06-11 13:32:42 -07001199 break;
1200 case OPT_BEFORE:
1201 if (pass == 2)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001202 preproc->pre_command(NULL, param);
H. Peter Anvin05990342018-06-11 13:32:42 -07001203 break;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001204 case OPT_LIMIT:
H. Peter Anvinbf6230b2018-11-11 13:25:16 -08001205 if (pass == 1)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001206 nasm_set_limit(p+olen, param);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001207 break;
H. Peter Anvin29695c82018-06-14 17:04:32 -07001208 case OPT_KEEP_ALL:
1209 keep_all = true;
1210 break;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001211 case OPT_HELP:
1212 help(0);
1213 exit(0);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001214 default:
1215 panic();
H. Peter Anvine2c80182005-01-15 22:15:51 +00001216 }
H. Peter Anvinc805fd72018-06-12 14:23:05 -07001217
1218 if (eqsave)
1219 *eqsave = '='; /* Restore = argument separator */
1220
H. Peter Anvine2c80182005-01-15 22:15:51 +00001221 break;
1222 }
1223
1224 default:
H. Peter Anvinac061332017-03-31 11:41:16 -07001225 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
1226 "unrecognised option `-%c'", p[1]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001227 break;
1228 }
H. Peter Anvin55568c12016-10-03 19:46:49 -07001229 } else if (pass == 2) {
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001230 /* In theory we could allow multiple input files... */
1231 copy_filename(&inname, p, "input");
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001232 }
1233
1234 return advance;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001235}
1236
H. Peter Anvineba20a72002-04-30 20:53:55 +00001237#define ARG_BUF_DELTA 128
1238
H. Peter Anvin55568c12016-10-03 19:46:49 -07001239static void process_respfile(FILE * rfile, int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001240{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001241 char *buffer, *p, *q, *prevarg;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001242 int bufsize, prevargsize;
1243
1244 bufsize = prevargsize = ARG_BUF_DELTA;
1245 buffer = nasm_malloc(ARG_BUF_DELTA);
1246 prevarg = nasm_malloc(ARG_BUF_DELTA);
1247 prevarg[0] = '\0';
1248
H. Peter Anvine2c80182005-01-15 22:15:51 +00001249 while (1) { /* Loop to handle all lines in file */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001250 p = buffer;
1251 while (1) { /* Loop to handle long lines */
1252 q = fgets(p, bufsize - (p - buffer), rfile);
1253 if (!q)
1254 break;
1255 p += strlen(p);
1256 if (p > buffer && p[-1] == '\n')
1257 break;
1258 if (p - buffer > bufsize - 10) {
1259 int offset;
1260 offset = p - buffer;
1261 bufsize += ARG_BUF_DELTA;
1262 buffer = nasm_realloc(buffer, bufsize);
1263 p = buffer + offset;
1264 }
1265 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001266
H. Peter Anvine2c80182005-01-15 22:15:51 +00001267 if (!q && p == buffer) {
1268 if (prevarg[0])
H. Peter Anvin55568c12016-10-03 19:46:49 -07001269 process_arg(prevarg, NULL, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001270 nasm_free(buffer);
1271 nasm_free(prevarg);
1272 return;
1273 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001274
H. Peter Anvine2c80182005-01-15 22:15:51 +00001275 /*
1276 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
1277 * them are present at the end of the line.
1278 */
1279 *(p = &buffer[strcspn(buffer, "\r\n\032")]) = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001280
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001281 while (p > buffer && nasm_isspace(p[-1]))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001282 *--p = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001283
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +04001284 p = nasm_skip_spaces(buffer);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001285
H. Peter Anvin55568c12016-10-03 19:46:49 -07001286 if (process_arg(prevarg, p, pass))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001287 *p = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001288
Charles Crayne192d5b52007-10-18 19:02:42 -07001289 if ((int) strlen(p) > prevargsize - 10) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001290 prevargsize += ARG_BUF_DELTA;
1291 prevarg = nasm_realloc(prevarg, prevargsize);
1292 }
H. Peter Anvindc242712007-11-18 11:55:10 -08001293 strncpy(prevarg, p, prevargsize);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001294 }
1295}
1296
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001297/* Function to process args from a string of args, rather than the
1298 * argv array. Used by the environment variable and response file
1299 * processing.
1300 */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001301static void process_args(char *args, int pass)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001302{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001303 char *p, *q, *arg, *prevarg;
1304 char separator = ' ';
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001305
1306 p = args;
1307 if (*p && *p != '-')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001308 separator = *p++;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001309 arg = NULL;
1310 while (*p) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001311 q = p;
1312 while (*p && *p != separator)
1313 p++;
1314 while (*p == separator)
1315 *p++ = '\0';
1316 prevarg = arg;
1317 arg = q;
H. Peter Anvin55568c12016-10-03 19:46:49 -07001318 if (process_arg(prevarg, arg, pass))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001319 arg = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001320 }
1321 if (arg)
H. Peter Anvin55568c12016-10-03 19:46:49 -07001322 process_arg(arg, NULL, pass);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001323}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001324
H. Peter Anvin55568c12016-10-03 19:46:49 -07001325static void process_response_file(const char *file, int pass)
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001326{
1327 char str[2048];
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001328 FILE *f = nasm_open_read(file, NF_TEXT);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001329 if (!f) {
Cyrill Gorcunovf1964512013-02-15 12:14:06 +04001330 perror(file);
1331 exit(-1);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001332 }
1333 while (fgets(str, sizeof str, f)) {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001334 process_args(str, pass);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001335 }
1336 fclose(f);
1337}
1338
H. Peter Anvin55568c12016-10-03 19:46:49 -07001339static void parse_cmdline(int argc, char **argv, int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001340{
1341 FILE *rfile;
Cyrill Gorcunovf4941892011-07-17 13:55:25 +04001342 char *envreal, *envcopy = NULL, *p;
H. Peter Anvin972079f2008-09-30 17:01:23 -07001343 int i;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001344
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001345 /*
1346 * Initialize all the warnings to their default state, including
1347 * warning index 0 used for "always on".
1348 */
H. Peter Anvin (Intel)77f53ba2018-12-12 14:38:50 -08001349 for (i = 0; i < WARN_ALL; i++) {
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001350 warning_state_init[i] = warning_state[i] =
1351 warnings[i].enabled ? WARN_ST_ENABLED : 0;
1352 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001353
1354 /*
H. Peter Anvinff7ccc02002-05-06 19:41:57 +00001355 * First, process the NASMENV environment variable.
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001356 */
H. Peter Anvinff7ccc02002-05-06 19:41:57 +00001357 envreal = getenv("NASMENV");
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001358 if (envreal) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001359 envcopy = nasm_strdup(envreal);
H. Peter Anvin55568c12016-10-03 19:46:49 -07001360 process_args(envcopy, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001361 nasm_free(envcopy);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001362 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001363
1364 /*
1365 * Now process the actual command line.
1366 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001367 while (--argc) {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001368 bool advance;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001369 argv++;
1370 if (argv[0][0] == '@') {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001371 /*
1372 * We have a response file, so process this as a set of
H. Peter Anvine2c80182005-01-15 22:15:51 +00001373 * arguments like the environment variable. This allows us
1374 * to have multiple arguments on a single line, which is
1375 * different to the -@resp file processing below for regular
1376 * NASM.
1377 */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001378 process_response_file(argv[0]+1, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001379 argc--;
1380 argv++;
1381 }
1382 if (!stopoptions && argv[0][0] == '-' && argv[0][1] == '@') {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001383 p = get_param(argv[0], argc > 1 ? argv[1] : NULL, &advance);
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001384 if (p) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001385 rfile = nasm_open_read(p, NF_TEXT);
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001386 if (rfile) {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001387 process_respfile(rfile, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001388 fclose(rfile);
1389 } else
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001390 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001391 "unable to open response file `%s'", p);
1392 }
1393 } else
H. Peter Anvin55568c12016-10-03 19:46:49 -07001394 advance = process_arg(argv[0], argc > 1 ? argv[1] : NULL, pass);
H. Peter Anvin423e3812007-11-15 17:12:29 -08001395 argv += advance, argc -= advance;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001396 }
1397
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001398 /*
1399 * Look for basic command line typos. This definitely doesn't
1400 * catch all errors, but it might help cases of fumbled fingers.
1401 */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001402 if (pass != 2)
1403 return;
1404
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001405 if (!inname)
1406 nasm_fatal(ERR_NOFILE | ERR_USAGE, "no input file specified");
H. Peter Anvin59ddd262007-10-01 11:26:31 -07001407
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001408 else if ((errname && !strcmp(inname, errname)) ||
1409 (outname && !strcmp(inname, outname)) ||
1410 (listname && !strcmp(inname, listname)) ||
1411 (depend_file && !strcmp(inname, depend_file)))
1412 nasm_fatal(ERR_USAGE, "will not overwrite input file");
1413
1414 if (errname) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001415 error_file = nasm_open_write(errname, NF_TEXT);
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001416 if (!error_file) {
1417 error_file = stderr; /* Revert to default! */
H. Peter Anvin41087062016-03-03 14:27:34 -08001418 nasm_fatal(ERR_NOFILE | ERR_USAGE,
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001419 "cannot open file `%s' for error messages",
1420 errname);
1421 }
Charles Craynefcce07f2007-09-30 22:15:36 -07001422 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001423}
1424
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001425static void assemble_file(const char *fname, StrList **depend_ptr)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001426{
H. Peter Anvinc7131682017-03-07 17:45:01 -08001427 char *line;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001428 insn output_ins;
H. Peter Anvinc7131682017-03-07 17:45:01 -08001429 int i;
H. Peter Anvin9c595b62017-03-07 19:44:21 -08001430 uint64_t prev_offset_changed;
H. Peter Anvin79561022018-06-15 17:51:39 -07001431 int64_t stall_count = 0; /* Make sure we make forward progress... */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001432
H. Peter Anvina7ecf262018-02-06 14:43:07 -08001433 switch (cmd_sb) {
1434 case 16:
1435 break;
1436 case 32:
1437 if (!iflag_cpu_level_ok(&cmd_cpu, IF_386))
1438 nasm_fatal(0, "command line: 32-bit segment size requires a higher cpu");
1439 break;
1440 case 64:
1441 if (!iflag_cpu_level_ok(&cmd_cpu, IF_X86_64))
1442 nasm_fatal(0, "command line: 64-bit segment size requires a higher cpu");
1443 break;
1444 default:
1445 panic();
1446 break;
1447 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001448
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001449 prev_offset_changed = nasm_limit[LIMIT_PASSES];
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001450 for (passn = 1; pass0 <= 2; passn++) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001451 pass1 = pass0 == 2 ? 2 : 1; /* 1, 1, 1, ..., 1, 2 */
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001452 pass2 = passn > 1 ? 2 : 1; /* 1, 2, 2, ..., 2, 2 */
1453 /* pass0 0, 0, 0, ..., 1, 2 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00001454
H. Peter Anvincac0b192017-03-28 16:12:30 -07001455 globalbits = cmd_sb; /* set 'bits' to command line default */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001456 cpu = cmd_cpu;
1457 if (pass0 == 2) {
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08001458 lfmt->init(listname);
H. Peter Anvin29695c82018-06-14 17:04:32 -07001459 } else if (passn == 1 && listname && !keep_all) {
H. Peter Anvinaac01ff2017-04-02 19:10:26 -07001460 /* Remove the list file in case we die before the output pass */
1461 remove(listname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001462 }
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001463 in_absolute = false;
Charles Craynec1905c22008-09-11 18:54:06 -07001464 global_offset_changed = 0; /* set by redefine_label */
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001465 if (passn > 1) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001466 saa_rewind(forwrefs);
1467 forwref = saa_rstruct(forwrefs);
1468 raa_free(offsets);
1469 offsets = raa_init();
1470 }
H. Peter Anvin892c4812018-05-30 14:43:46 -07001471 location.segment = NO_SEG;
1472 location.offset = 0;
1473 if (passn == 1)
1474 location.known = true;
1475 ofmt->reset();
1476 switch_segment(ofmt->section(NULL, pass2, &globalbits));
H. Peter Anvin130736c2016-02-17 20:27:41 -08001477 preproc->reset(fname, pass1, pass1 == 2 ? depend_ptr : NULL);
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001478
1479 /* Revert all warnings to the default state */
1480 memcpy(warning_state, warning_state_init, sizeof warning_state);
Victor van den Elzen819703a2008-07-16 15:20:56 +02001481
H. Peter Anvine2c80182005-01-15 22:15:51 +00001482 globallineno = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001483
H. Peter Anvine2c80182005-01-15 22:15:51 +00001484 while ((line = preproc->getline())) {
H. Peter Anvin79561022018-06-15 17:51:39 -07001485 if (++globallineno > nasm_limit[LIMIT_LINES])
1486 nasm_fatal(0,
1487 "overall line count exceeds the maximum %"PRId64"\n",
1488 nasm_limit[LIMIT_LINES]);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001489
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001490 /*
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001491 * Here we parse our directives; this is not handled by the
H. Peter Anvinc7131682017-03-07 17:45:01 -08001492 * main parser.
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001493 */
H. Peter Anvina6e26d92017-03-07 21:32:37 -08001494 if (process_directives(line))
H. Peter Anvinc7131682017-03-07 17:45:01 -08001495 goto end_of_line; /* Just do final cleanup */
H. Peter Anvin62b24d72007-08-29 16:38:05 +00001496
H. Peter Anvinc7131682017-03-07 17:45:01 -08001497 /* Not a directive, or even something that starts with [ */
H. Peter Anvin98578072018-06-01 18:02:54 -07001498 parse_line(pass1, line, &output_ins);
H. Peter Anvinc7131682017-03-07 17:45:01 -08001499
Chang S. Baea5786342018-08-15 23:22:21 +03001500 if (optimizing.level > 0) {
H. Peter Anvinc7131682017-03-07 17:45:01 -08001501 if (forwref != NULL && globallineno == forwref->lineno) {
1502 output_ins.forw_ref = true;
1503 do {
1504 output_ins.oprs[forwref->operand].opflags |= OPFLAG_FORWARD;
1505 forwref = saa_rstruct(forwrefs);
1506 } while (forwref != NULL
1507 && forwref->lineno == globallineno);
1508 } else
1509 output_ins.forw_ref = false;
1510
1511 if (output_ins.forw_ref) {
1512 if (passn == 1) {
1513 for (i = 0; i < output_ins.operands; i++) {
1514 if (output_ins.oprs[i].opflags & OPFLAG_FORWARD) {
1515 struct forwrefinfo *fwinf = (struct forwrefinfo *)saa_wstruct(forwrefs);
1516 fwinf->lineno = globallineno;
1517 fwinf->operand = i;
Cyrill Gorcunovd5f2aef2010-04-20 15:33:45 +04001518 }
1519 }
1520 }
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001521 }
H. Peter Anvinc7131682017-03-07 17:45:01 -08001522 }
H. Peter Anvin8bec2c72009-08-08 13:49:00 -07001523
H. Peter Anvinc7131682017-03-07 17:45:01 -08001524 /* forw_ref */
1525 if (output_ins.opcode == I_EQU) {
Cyrill Gorcunove996d282018-10-13 16:18:16 +03001526 if (!output_ins.label) {
1527 nasm_error(ERR_NONFATAL, "EQU not preceded by label");
1528 } else if (output_ins.operands == 1 &&
1529 (output_ins.oprs[0].type & IMMEDIATE) &&
1530 output_ins.oprs[0].wrt == NO_SEG) {
H. Peter Anvin98578072018-06-01 18:02:54 -07001531 define_label(output_ins.label,
1532 output_ins.oprs[0].segment,
1533 output_ins.oprs[0].offset, false);
1534 } else if (output_ins.operands == 2
1535 && (output_ins.oprs[0].type & IMMEDIATE)
1536 && (output_ins.oprs[0].type & COLON)
1537 && output_ins.oprs[0].segment == NO_SEG
1538 && output_ins.oprs[0].wrt == NO_SEG
1539 && (output_ins.oprs[1].type & IMMEDIATE)
1540 && output_ins.oprs[1].segment == NO_SEG
1541 && output_ins.oprs[1].wrt == NO_SEG) {
1542 define_label(output_ins.label,
1543 output_ins.oprs[0].offset | SEG_ABS,
1544 output_ins.oprs[1].offset, false);
H. Peter Anvinc7131682017-03-07 17:45:01 -08001545 } else {
H. Peter Anvin98578072018-06-01 18:02:54 -07001546 nasm_error(ERR_NONFATAL, "bad syntax for EQU");
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001547 }
H. Peter Anvinc7131682017-03-07 17:45:01 -08001548 } else { /* instruction isn't an EQU */
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001549 int32_t n;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001550
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001551 nasm_assert(output_ins.times >= 0);
H. Peter Anvinc7131682017-03-07 17:45:01 -08001552
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001553 for (n = 1; n <= output_ins.times; n++) {
1554 if (pass1 == 1) {
H. Peter Anvin892c4812018-05-30 14:43:46 -07001555 int64_t l = insn_size(location.segment,
1556 location.offset,
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001557 globalbits, &output_ins);
1558
1559 /* if (using_debug_info) && output_ins.opcode != -1) */
1560 if (using_debug_info)
1561 { /* fbk 03/25/01 */
H. Peter Anvinc7131682017-03-07 17:45:01 -08001562 /* this is done here so we can do debug type info */
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001563 int32_t typeinfo =
1564 TYS_ELEMENTS(output_ins.operands);
1565 switch (output_ins.opcode) {
1566 case I_RESB:
1567 typeinfo =
1568 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_BYTE;
1569 break;
1570 case I_RESW:
1571 typeinfo =
1572 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_WORD;
1573 break;
1574 case I_RESD:
1575 typeinfo =
1576 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_DWORD;
1577 break;
1578 case I_RESQ:
1579 typeinfo =
1580 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_QWORD;
1581 break;
1582 case I_REST:
1583 typeinfo =
1584 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_TBYTE;
1585 break;
1586 case I_RESO:
1587 typeinfo =
1588 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_OWORD;
1589 break;
1590 case I_RESY:
1591 typeinfo =
1592 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_YWORD;
1593 break;
1594 case I_RESZ:
1595 typeinfo =
1596 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_ZWORD;
1597 break;
1598 case I_DB:
1599 typeinfo |= TY_BYTE;
1600 break;
1601 case I_DW:
1602 typeinfo |= TY_WORD;
1603 break;
1604 case I_DD:
1605 if (output_ins.eops_float)
1606 typeinfo |= TY_FLOAT;
1607 else
1608 typeinfo |= TY_DWORD;
1609 break;
1610 case I_DQ:
1611 typeinfo |= TY_QWORD;
1612 break;
1613 case I_DT:
1614 typeinfo |= TY_TBYTE;
1615 break;
1616 case I_DO:
1617 typeinfo |= TY_OWORD;
1618 break;
1619 case I_DY:
1620 typeinfo |= TY_YWORD;
1621 break;
1622 case I_DZ:
1623 typeinfo |= TY_ZWORD;
1624 break;
1625 default:
1626 typeinfo = TY_LABEL;
1627 break;
1628 }
H. Peter Anvinc7131682017-03-07 17:45:01 -08001629
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001630 dfmt->debug_typevalue(typeinfo);
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001631 }
H. Peter Anvinc7131682017-03-07 17:45:01 -08001632
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001633 /*
1634 * For INCBIN, let the code in assemble
1635 * handle TIMES, so we don't have to read the
1636 * input file over and over.
1637 */
1638 if (l != -1) {
H. Peter Anvin892c4812018-05-30 14:43:46 -07001639 increment_offset(l);
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001640 }
1641 /*
1642 * else l == -1 => invalid instruction, which will be
1643 * flagged as an error on pass 2
1644 */
1645 } else {
1646 if (n == 2)
1647 lfmt->uplevel(LIST_TIMES);
H. Peter Anvin892c4812018-05-30 14:43:46 -07001648 increment_offset(assemble(location.segment,
1649 location.offset,
1650 globalbits, &output_ins));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001651 }
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001652 } /* not an EQU */
1653 }
1654 if (output_ins.times > 1)
1655 lfmt->downlevel(LIST_TIMES);
H. Peter Anvinc7131682017-03-07 17:45:01 -08001656
H. Peter Anvinc7131682017-03-07 17:45:01 -08001657 cleanup_insn(&output_ins);
1658
1659 end_of_line:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001660 nasm_free(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001661 } /* end while (line = preproc->getline... */
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001662
H. Peter Anvin (Intel)b45c03a2018-06-27 21:03:38 -07001663 if (global_offset_changed && !terminate_after_phase) {
1664 switch (pass0) {
1665 case 1:
H. Peter Anvin (Intel)77f53ba2018-12-12 14:38:50 -08001666 nasm_error(ERR_WARNING|WARN_PHASE,
H. Peter Anvin (Intel)b45c03a2018-06-27 21:03:38 -07001667 "phase error during stabilization pass, hoping for the best");
1668 break;
1669
1670 case 2:
1671 nasm_error(ERR_NONFATAL,
1672 "phase error during code generation pass");
1673 break;
1674
1675 default:
1676 /* This is normal, we'll keep going... */
1677 break;
1678 }
1679 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001680
H. Peter Anvine2c80182005-01-15 22:15:51 +00001681 if (pass1 == 1)
1682 preproc->cleanup(1);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001683
H. Peter Anvin (Intel)12810fa2018-06-27 20:17:33 -07001684 /*
1685 * Always run at least two optimization passes (pass0 == 0);
1686 * things like subsections will fail miserably without that.
1687 * Once we commit to a stabilization pass (pass0 == 1), we can't
1688 * go back, and if something goes bad, we can only hope
1689 * that we don't end up with a phase error at the end.
1690 */
1691 if ((passn > 1 && !global_offset_changed) || pass0 > 0) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001692 pass0++;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001693 } else if (global_offset_changed &&
1694 global_offset_changed < prev_offset_changed) {
Charles Craynec1905c22008-09-11 18:54:06 -07001695 prev_offset_changed = global_offset_changed;
1696 stall_count = 0;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001697 } else {
1698 stall_count++;
1699 }
Victor van den Elzen42528232008-09-11 15:07:05 +02001700
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001701 if (terminate_after_phase)
1702 break;
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001703
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001704 if ((stall_count > nasm_limit[LIMIT_STALLED]) ||
1705 (passn >= nasm_limit[LIMIT_PASSES])) {
Victor van den Elzen42528232008-09-11 15:07:05 +02001706 /* We get here if the labels don't converge
1707 * Example: FOO equ FOO + 1
1708 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001709 nasm_error(ERR_NONFATAL,
Victor van den Elzen42528232008-09-11 15:07:05 +02001710 "Can't find valid values for all labels "
H. Peter Anvin79561022018-06-15 17:51:39 -07001711 "after %"PRId64" passes, giving up.", passn);
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001712 nasm_error(ERR_NONFATAL,
1713 "Possible causes: recursive EQUs, macro abuse.");
1714 break;
1715 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001716 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001717
H. Peter Anvine2c80182005-01-15 22:15:51 +00001718 preproc->cleanup(0);
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08001719 lfmt->cleanup();
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001720 if (!terminate_after_phase && opt_verbose_info) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001721 /* -On and -Ov switches */
H. Peter Anvin79561022018-06-15 17:51:39 -07001722 fprintf(stdout, "info: assembly required 1+%"PRId64"+1 passes\n",
1723 passn-3);
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001724 }
1725}
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001726
Ed Berosetfa771012002-06-09 20:56:40 +00001727/**
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001728 * get warning index; 0 if this is non-suppressible.
1729 */
1730static size_t warn_index(int severity)
1731{
1732 size_t index;
1733
1734 if ((severity & ERR_MASK) >= ERR_FATAL)
1735 return 0; /* Fatal errors are never suppressible */
1736
1737 /* If this is a warning and no index is provided, it is WARN_OTHER */
1738 if ((severity & (ERR_MASK|WARN_MASK)) == ERR_WARNING)
1739 severity |= WARN_OTHER;
1740
1741 index = WARN_IDX(severity);
1742 nasm_assert(index < WARN_ALL);
1743
1744 return index;
1745}
1746
1747static bool skip_this_pass(int severity)
1748{
1749 /*
1750 * See if it's a pass-specific error or warning which should be skipped.
1751 * We can never skip fatal errors as by definition they cannot be
1752 * resumed from.
1753 */
1754 if ((severity & ERR_MASK) >= ERR_FATAL)
1755 return false;
1756
1757 /*
1758 * passn is 1 on the very first pass only.
1759 * pass0 is 2 on the code-generation (final) pass only.
1760 * These are the passes we care about in this case.
1761 */
1762 return (((severity & ERR_PASS1) && passn != 1) ||
1763 ((severity & ERR_PASS2) && pass0 != 2));
1764}
1765
1766/**
1767 * check for suppressed message (usually warnings or notes)
1768 *
1769 * @param severity the severity of the warning or error
1770 * @return true if we should abort error/warning printing
1771 */
1772static bool is_suppressed(int severity)
1773{
1774 return !(warning_state[warn_index(severity)] & WARN_ST_ENABLED);
1775}
1776
1777/**
1778 * check if we have a warning that should be promoted to an error
1779 *
1780 * @param severity the severity of the warning or error
1781 * @return true if we should promote to error
1782 */
1783static bool warning_is_error(int severity)
1784{
1785 if ((severity & ERR_MASK) != ERR_WARNING)
1786 return false; /* Other message types */
1787
1788 return !!(warning_state[warn_index(severity)] & WARN_ST_ERROR);
1789}
1790
1791/**
Ed Berosetfa771012002-06-09 20:56:40 +00001792 * gnu style error reporting
1793 * This function prints an error message to error_file in the
1794 * style used by GNU. An example would be:
1795 * file.asm:50: error: blah blah blah
H. Peter Anvin70653092007-10-19 14:42:29 -07001796 * where file.asm is the name of the file, 50 is the line number on
Ed Berosetfa771012002-06-09 20:56:40 +00001797 * which the error occurs (or is detected) and "error:" is one of
1798 * the possible optional diagnostics -- it can be "error" or "warning"
H. Peter Anvin70653092007-10-19 14:42:29 -07001799 * or something else. Finally the line terminates with the actual
Ed Berosetfa771012002-06-09 20:56:40 +00001800 * error message.
H. Peter Anvin70653092007-10-19 14:42:29 -07001801 *
1802 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001803 * @param fmt the printf style format string
1804 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001805static void nasm_verror_gnu(int severity, const char *fmt, va_list ap)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001806{
H. Peter Anvin274cda82016-05-10 02:56:29 -07001807 const char *currentfile = NULL;
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001808 int32_t lineno = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001809
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001810 if (is_suppressed(severity))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001811 return;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001812
H. Peter Anvin7a6bf742017-12-20 11:54:31 -08001813 if (!(severity & ERR_NOFILE)) {
H. Peter Anvin4def1a82016-05-09 13:59:44 -07001814 src_get(&lineno, &currentfile);
H. Peter Anvin7a6bf742017-12-20 11:54:31 -08001815 if (!currentfile || (severity & ERR_TOPFILE)) {
1816 currentfile = inname[0] ? inname : outname[0] ? outname : NULL;
1817 lineno = 0;
1818 }
1819 }
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001820
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001821 if (!skip_this_pass(severity)) {
H. Peter Anvin7a6bf742017-12-20 11:54:31 -08001822 if (!lineno)
H. Peter Anvin070c50f2018-12-10 13:04:33 -08001823 fprintf(error_file, "%s: ", currentfile ? currentfile : "nasm");
H. Peter Anvin883985d2017-12-20 11:32:39 -08001824 else
1825 fprintf(error_file, "%s:%"PRId32": ", currentfile, lineno);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001826 }
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001827
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001828 nasm_verror_common(severity, fmt, ap);
Ed Berosetfa771012002-06-09 20:56:40 +00001829}
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001830
Ed Berosetfa771012002-06-09 20:56:40 +00001831/**
1832 * MS style error reporting
1833 * This function prints an error message to error_file in the
H. Peter Anvin70653092007-10-19 14:42:29 -07001834 * style used by Visual C and some other Microsoft tools. An example
Ed Berosetfa771012002-06-09 20:56:40 +00001835 * would be:
Ed Beroset6e61d0d2002-06-11 03:29:36 +00001836 * file.asm(50) : error: blah blah blah
H. Peter Anvin70653092007-10-19 14:42:29 -07001837 * where file.asm is the name of the file, 50 is the line number on
Ed Beroset6e61d0d2002-06-11 03:29:36 +00001838 * which the error occurs (or is detected) and "error:" is one of
1839 * the possible optional diagnostics -- it can be "error" or "warning"
H. Peter Anvin70653092007-10-19 14:42:29 -07001840 * or something else. Finally the line terminates with the actual
Ed Beroset6e61d0d2002-06-11 03:29:36 +00001841 * error message.
H. Peter Anvin70653092007-10-19 14:42:29 -07001842 *
1843 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001844 * @param fmt the printf style format string
1845 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001846static void nasm_verror_vc(int severity, const char *fmt, va_list ap)
Ed Berosetfa771012002-06-09 20:56:40 +00001847{
H. Peter Anvin274cda82016-05-10 02:56:29 -07001848 const char *currentfile = NULL;
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001849 int32_t lineno = 0;
Ed Berosetfa771012002-06-09 20:56:40 +00001850
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001851 if (is_suppressed(severity))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001852 return;
Ed Berosetfa771012002-06-09 20:56:40 +00001853
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001854 if (!(severity & ERR_NOFILE))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001855 src_get(&lineno, &currentfile);
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001856
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001857 if (!skip_this_pass(severity)) {
H. Peter Anvin070c50f2018-12-10 13:04:33 -08001858 if (lineno) {
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001859 fprintf(error_file, "%s(%"PRId32") : ", currentfile, lineno);
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001860 } else {
H. Peter Anvin070c50f2018-12-10 13:04:33 -08001861 fprintf(error_file , "%s : ", currentfile ? currentfile : "nasm");
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001862 }
Ed Berosetfa771012002-06-09 20:56:40 +00001863 }
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001864
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001865 nasm_verror_common(severity, fmt, ap);
Ed Berosetfa771012002-06-09 20:56:40 +00001866}
1867
Ed Berosetfa771012002-06-09 20:56:40 +00001868/**
1869 * common error reporting
1870 * This is the common back end of the error reporting schemes currently
H. Peter Anvin70653092007-10-19 14:42:29 -07001871 * implemented. It prints the nature of the warning and then the
Ed Berosetfa771012002-06-09 20:56:40 +00001872 * specific error message to error_file and may or may not return. It
1873 * doesn't return if the error severity is a "panic" or "debug" type.
H. Peter Anvin70653092007-10-19 14:42:29 -07001874 *
1875 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001876 * @param fmt the printf style format string
1877 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001878static void nasm_verror_common(int severity, const char *fmt, va_list args)
Ed Berosetfa771012002-06-09 20:56:40 +00001879{
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001880 char msg[1024];
1881 const char *pfx;
H. Peter Anvin070c50f2018-12-10 13:04:33 -08001882 bool warn_is_err = warning_is_error(severity);
H. Peter Anvin323fcff2009-07-12 12:04:56 -07001883
H. Peter Anvin7df04172008-06-10 18:27:38 -07001884 switch (severity & (ERR_MASK|ERR_NO_SEVERITY)) {
H. Peter Anvind84f9a72018-12-10 13:29:35 -08001885 case ERR_NOTE:
1886 pfx = "note: ";
1887 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001888 case ERR_WARNING:
H. Peter Anvin070c50f2018-12-10 13:04:33 -08001889 if (!warn_is_err) {
1890 pfx = "warning: ";
1891 break;
1892 }
1893 /* fall through */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001894 case ERR_NONFATAL:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001895 pfx = "error: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001896 break;
1897 case ERR_FATAL:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001898 pfx = "fatal: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001899 break;
1900 case ERR_PANIC:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001901 pfx = "panic: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001902 break;
1903 case ERR_DEBUG:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001904 pfx = "debug: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001905 break;
H. Peter Anvin7df04172008-06-10 18:27:38 -07001906 default:
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001907 pfx = "";
1908 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001909 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00001910
H. Peter Anvin934f0472016-05-09 12:00:19 -07001911 vsnprintf(msg, sizeof msg - 64, fmt, args);
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001912 if ((severity & ERR_MASK) == ERR_WARNING && !is_suppressed(severity)) {
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001913 char *p = strchr(msg, '\0');
H. Peter Anvin070c50f2018-12-10 13:04:33 -08001914 snprintf(p, 64, " [-w+%s%s]",
1915 warn_is_err ? "error=" : "",
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08001916 warnings[warn_index(severity)].name);
H. Peter Anvin934f0472016-05-09 12:00:19 -07001917 }
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001918
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001919 if (!skip_this_pass(severity))
1920 fprintf(error_file, "%s%s\n", pfx, msg);
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001921
H. Peter Anvin4def1a82016-05-09 13:59:44 -07001922 /* Are we recursing from error_list_macros? */
1923 if (severity & ERR_PP_LISTMACRO)
1924 return;
1925
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001926 /*
1927 * Don't suppress this with skip_this_pass(), or we don't get
H. Peter Anvin934f0472016-05-09 12:00:19 -07001928 * pass1 or preprocessor warnings in the list file
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001929 */
H. Peter Anvin0fcb4882016-07-06 11:55:25 -07001930 lfmt->error(severity, pfx, msg);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001931
H. Peter Anvin8f622462017-04-02 19:02:29 -07001932 if (skip_this_pass(severity))
1933 return;
1934
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001935 if (severity & ERR_USAGE)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001936 want_usage = true;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001937
H. Peter Anvin4def1a82016-05-09 13:59:44 -07001938 preproc->error_list_macros(severity);
1939
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001940 switch (severity & ERR_MASK) {
H. Peter Anvin54aac9d2018-12-10 21:14:57 -08001941 case ERR_NOTE:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001942 case ERR_DEBUG:
1943 /* no further action, by definition */
1944 break;
H. Peter Anvinb030c922007-11-13 11:31:15 -08001945 case ERR_WARNING:
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001946 /* Treat warnings as errors */
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001947 if (warning_is_error(severity))
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001948 terminate_after_phase = true;
1949 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001950 case ERR_NONFATAL:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001951 terminate_after_phase = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001952 break;
1953 case ERR_FATAL:
1954 if (ofile) {
1955 fclose(ofile);
H. Peter Anvin29695c82018-06-14 17:04:32 -07001956 if (!keep_all)
1957 remove(outname);
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001958 ofile = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001959 }
1960 if (want_usage)
1961 usage();
1962 exit(1); /* instantly die */
1963 break; /* placate silly compilers */
1964 case ERR_PANIC:
1965 fflush(NULL);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001966
1967 if (abort_on_panic)
1968 abort(); /* halt, catch fire, dump core/stop debugger */
1969
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001970 if (ofile) {
1971 fclose(ofile);
H. Peter Anvin29695c82018-06-14 17:04:32 -07001972 if (!keep_all)
1973 remove(outname);
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001974 ofile = NULL;
1975 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001976 exit(3);
1977 break;
H. Peter Anvin54aac9d2018-12-10 21:14:57 -08001978 default:
1979 break; /* ??? */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001980 }
1981}
1982
H. Peter Anvin734b1882002-04-30 21:01:08 +00001983static void usage(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001984{
H. Peter Anvin620515a2002-04-30 20:57:38 +00001985 fputs("type `nasm -h' for help\n", error_file);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001986}
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001987
1988static void help(const char xopt)
1989{
1990 int i;
1991
1992 printf
1993 ("usage: nasm [-@ response file] [-o outfile] [-f format] "
1994 "[-l listfile]\n"
1995 " [options...] [--] filename\n"
1996 " or nasm -v (or --v) for version info\n\n"
1997 "\n"
1998 "Response files should contain command line parameters,\n"
1999 "one per line.\n"
2000 "\n"
2001 " -t assemble in SciTech TASM compatible mode\n");
2002 printf
2003 (" -E (or -e) preprocess only (writes output to stdout by default)\n"
2004 " -a don't preprocess (assemble only)\n"
2005 " -M generate Makefile dependencies on stdout\n"
2006 " -MG d:o, missing files assumed generated\n"
2007 " -MF file set Makefile dependency file\n"
2008 " -MD file assemble and generate dependencies\n"
2009 " -MT file dependency target name\n"
2010 " -MQ file dependency target name (quoted)\n"
2011 " -MP emit phony target\n\n"
2012 " -Zfile redirect error messages to file\n"
2013 " -s redirect error messages to stdout\n\n"
2014 " -g generate debugging information\n\n"
2015 " -F format select a debugging format\n\n"
2016 " -gformat same as -g -F format\n\n"
2017 " -o outfile write output to an outfile\n\n"
2018 " -f format select an output format\n\n"
2019 " -l listfile write listing to a listfile\n\n"
2020 " -Ipath add a pathname to the include file path\n");
2021 printf
2022 (" -Olevel optimize opcodes, immediates and branch offsets\n"
2023 " -O0 no optimization\n"
2024 " -O1 minimal optimization\n"
2025 " -Ox multipass optimization (default)\n"
2026 " -Pfile pre-include a file (also --include)\n"
2027 " -Dmacro[=str] pre-define a macro\n"
2028 " -Umacro undefine a macro\n"
2029 " -Xformat specifiy error reporting format (gnu or vc)\n"
2030 " -w+foo enable warning foo (equiv. -Wfoo)\n"
2031 " -w-foo disable warning foo (equiv. -Wno-foo)\n"
2032 " -w[+-]error[=foo]\n"
2033 " promote [specific] warnings to errors\n"
Chang S. Bae1af6ef42018-06-20 17:05:12 -07002034 " -h show invocation summary and exit (also --help)\n\n"
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002035 " --pragma str pre-executes a specific %%pragma\n"
2036 " --before str add line (usually a preprocessor statement) before the input\n"
2037 " --prefix str prepend the given string to all the given string\n"
Chang S. Bae1af6ef42018-06-20 17:05:12 -07002038 " to all extern, common and global symbols (also --gprefix)\n"
2039 " --postfix str append the given string to all the given string\n"
2040 " to all extern, common and global symbols (also --gpostfix)\n"
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002041 " --lprefix str prepend the given string to all other symbols\n"
Chang S. Bae1af6ef42018-06-20 17:05:12 -07002042 " --lpostfix str append the given string to all other symbols\n"
2043 " --keep-all output files will not be removed even if an error happens\n"
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002044 " --limit-X val set execution limit X\n");
2045
2046 for (i = 0; i <= LIMIT_MAX; i++) {
2047 printf(" %-15s %s (default ",
2048 limit_info[i].name, limit_info[i].help);
2049 if (nasm_limit[i] < LIMIT_MAX_VAL) {
H. Peter Anvin79561022018-06-15 17:51:39 -07002050 printf("%"PRId64")\n", nasm_limit[i]);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002051 } else {
2052 printf("unlimited)\n");
2053 }
2054 }
2055
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08002056 printf("\nWarnings for the -W/-w options: (default in brackets)\n");
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002057
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08002058 for (i = 1; i <= WARN_ALL; i++)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002059 printf(" %-23s %s%s\n",
2060 warnings[i].name, warnings[i].help,
H. Peter Anvin (Intel)77f53ba2018-12-12 14:38:50 -08002061 i == WARN_ALL ? "\n" :
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -08002062 warnings[i].enabled ? " [on]" : " [off]");
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002063
2064 if (xopt == 'f') {
2065 printf("valid output formats for -f are"
2066 " (`*' denotes default):\n");
2067 ofmt_list(ofmt, stdout);
2068 } else {
2069 printf("For a list of valid output formats, use -hf.\n");
2070 printf("For a list of debug formats, use -f <format> -y.\n");
2071 }
2072}