blob: 0c1df9fbdb22c6246ed61b6998be5284ae323992 [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 Anvin130736c2016-02-17 20:27:41 -080079static bool is_suppressed_warning(int severity);
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -080080static bool skip_this_pass(int severity);
H. Peter Anvin9bd15062009-07-18 21:07:17 -040081static void nasm_verror_gnu(int severity, const char *fmt, va_list args);
82static void nasm_verror_vc(int severity, const char *fmt, va_list args);
83static void nasm_verror_common(int severity, const char *fmt, va_list args);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000084static void usage(void);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -070085static void help(char xopt);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000086
H. Peter Anvin283b3fb2016-03-07 23:18:30 -080087static bool using_debug_info, opt_verbose_info;
88static const char *debug_format;
89
H. Peter Anvin3366e312018-02-07 14:14:36 -080090#ifndef ABORT_ON_PANIC
91# define ABORT_ON_PANIC 0
92#endif
93static bool abort_on_panic = ABORT_ON_PANIC;
H. Peter Anvin29695c82018-06-14 17:04:32 -070094static bool keep_all;
H. Peter Anvin3366e312018-02-07 14:14:36 -080095
H. Peter Anvin6867acc2007-10-10 14:58:45 -070096bool tasm_compatible_mode = false;
H. Peter Anvin79561022018-06-15 17:51:39 -070097int pass0;
98int64_t passn;
H. Peter Anvinc7131682017-03-07 17:45:01 -080099static int pass1, pass2; /* XXX: Get rid of these, they are redundant */
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +0000100int globalrel = 0;
Jin Kyu Songb287ff02013-12-04 20:05:55 -0800101int globalbnd = 0;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000102
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700103struct compile_time official_compile_time;
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800104
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800105const char *inname;
106const char *outname;
107static const char *listname;
108static const char *errname;
109
H. Peter Anvin79561022018-06-15 17:51:39 -0700110static int64_t globallineno; /* for forward-reference tracking */
Chang S. Baef0ceb1e2018-05-02 08:07:53 -0700111
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000112/* static int pass = 0; */
H. Peter Anvin338656c2016-02-17 20:59:22 -0800113const struct ofmt *ofmt = &OF_DEFAULT;
114const struct ofmt_alias *ofmt_alias = NULL;
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400115const struct dfmt *dfmt;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000116
H. Peter Anvine2c80182005-01-15 22:15:51 +0000117static FILE *error_file; /* Where to write error messages */
H. Peter Anvin620515a2002-04-30 20:57:38 +0000118
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400119FILE *ofile = NULL;
Chang S. Baea5786342018-08-15 23:22:21 +0300120struct optimization optimizing =
121 { MAX_OPTIMIZE, OPTIM_ALL_ENABLED }; /* number of optimization passes to take */
Martin Lindhe8cc93f52016-11-16 16:48:13 +0100122static int cmd_sb = 16; /* by default */
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400123
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800124iflag_t cpu;
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400125static iflag_t cmd_cpu;
126
H. Peter Anvincd7893d2016-02-18 01:25:46 -0800127struct location location;
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800128bool in_absolute; /* Flag we are in ABSOLUTE seg */
129struct location absolute; /* Segment/offset inside ABSOLUTE */
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000130
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000131static struct RAA *offsets;
H. Peter Anvinea838272002-04-30 20:51:53 +0000132
H. Peter Anvine2c80182005-01-15 22:15:51 +0000133static struct SAA *forwrefs; /* keep track of forward references */
H. Peter Anvin9d637df2007-10-04 13:42:56 -0700134static const struct forwrefinfo *forwref;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000135
H. Peter Anvine7469712016-02-18 02:20:59 -0800136static const struct preproc_ops *preproc;
Cyrill Gorcunov86b2ad02011-07-01 10:38:25 +0400137
Cyrill Gorcunove9fc88c2014-06-23 02:22:02 +0400138#define OP_NORMAL (1u << 0)
139#define OP_PREPROCESS (1u << 1)
140#define OP_DEPEND (1u << 2)
141
142static unsigned int operating_mode;
Cyrill Gorcunov3ed32cb2014-06-22 20:53:12 +0400143
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700144/* Dependency flags */
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700145static bool depend_emit_phony = false;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700146static bool depend_missing_ok = false;
147static const char *depend_target = NULL;
148static const char *depend_file = NULL;
H. Peter Anvina771be82017-08-16 14:53:18 -0700149StrList *depend_list;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000150
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700151static bool want_usage;
152static bool terminate_after_phase;
H. Peter Anvin130736c2016-02-17 20:27:41 -0800153bool user_nolist = false;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000154
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700155static char *quote_for_pmake(const char *str);
156static char *quote_for_wmake(const char *str);
157static char *(*quote_for_make)(const char *) = quote_for_pmake;
H. Peter Anvin55340992012-09-09 17:09:00 -0700158
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700159/*
160 * Execution limits that can be set via a command-line option or %pragma
161 */
162
H. Peter 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
197 errlevel = ERR_WARNING|ERR_PASS1|ERR_WARN_UNKNOWN_PRAGMA;
198 nasm_error(errlevel, "unknown limit: `%s'", limit);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700199 return DIRR_ERROR;
200 }
201
202 if (!nasm_stricmp(valstr, "unlimited")) {
203 val = LIMIT_MAX_VAL;
204 } else {
205 val = readnum(valstr, &rn_error);
206 if (rn_error || val < 0) {
H. Peter Anvinc805fd72018-06-12 14:23:05 -0700207 if (passn == 0)
208 errlevel = ERR_WARNING|ERR_NOFILE|ERR_USAGE;
209 else
210 errlevel = ERR_WARNING|ERR_PASS1|ERR_WARN_BAD_PRAGMA;
211 nasm_error(errlevel, "invalid limit value: `%s'", limit);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700212 return DIRR_ERROR;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700213 }
H. Peter Anvinc805fd72018-06-12 14:23:05 -0700214 if (val > LIMIT_MAX_VAL)
215 val = LIMIT_MAX_VAL;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700216 }
217
218 nasm_limit[i] = val;
219 return DIRR_OK;
220}
221
H. Peter Anvin892c4812018-05-30 14:43:46 -0700222int64_t switch_segment(int32_t segment)
Cyrill Gorcunov190232f2013-02-15 12:35:04 +0400223{
H. Peter Anvin892c4812018-05-30 14:43:46 -0700224 location.segment = segment;
225 if (segment == NO_SEG) {
226 location.offset = absolute.offset;
227 in_absolute = true;
228 } else {
229 location.offset = raa_read(offsets, segment);
230 in_absolute = false;
231 }
232 return location.offset;
Cyrill Gorcunov190232f2013-02-15 12:35:04 +0400233}
234
235static void set_curr_offs(int64_t l_off)
236{
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800237 if (in_absolute)
238 absolute.offset = l_off;
Cyrill Gorcunov190232f2013-02-15 12:35:04 +0400239 else
240 offsets = raa_write(offsets, location.segment, l_off);
241}
242
H. Peter Anvin892c4812018-05-30 14:43:46 -0700243static void increment_offset(int64_t delta)
244{
245 if (unlikely(delta == 0))
246 return;
247
248 location.offset += delta;
249 set_curr_offs(location.offset);
250}
251
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000252static void nasm_fputs(const char *line, FILE * outfile)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000253{
H. Peter Anvin310b3e12002-05-14 22:38:55 +0000254 if (outfile) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000255 fputs(line, outfile);
H. Peter Anvind1fb15c2007-11-13 09:37:59 -0800256 putc('\n', outfile);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000257 } else
H. Peter Anvine2c80182005-01-15 22:15:51 +0000258 puts(line);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000259}
260
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800261static void define_macros_early(void)
262{
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700263 const struct compile_time * const oct = &official_compile_time;
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800264 char temp[128];
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800265
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700266 if (oct->have_local) {
267 strftime(temp, sizeof temp, "__DATE__=\"%Y-%m-%d\"", &oct->local);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400268 preproc->pre_define(temp);
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700269 strftime(temp, sizeof temp, "__DATE_NUM__=%Y%m%d", &oct->local);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400270 preproc->pre_define(temp);
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700271 strftime(temp, sizeof temp, "__TIME__=\"%H:%M:%S\"", &oct->local);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400272 preproc->pre_define(temp);
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700273 strftime(temp, sizeof temp, "__TIME_NUM__=%H%M%S", &oct->local);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400274 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800275 }
276
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700277 if (oct->have_gm) {
278 strftime(temp, sizeof temp, "__UTC_DATE__=\"%Y-%m-%d\"", &oct->gm);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400279 preproc->pre_define(temp);
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700280 strftime(temp, sizeof temp, "__UTC_DATE_NUM__=%Y%m%d", &oct->gm);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400281 preproc->pre_define(temp);
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700282 strftime(temp, sizeof temp, "__UTC_TIME__=\"%H:%M:%S\"", &oct->gm);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400283 preproc->pre_define(temp);
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700284 strftime(temp, sizeof temp, "__UTC_TIME_NUM__=%H%M%S", &oct->gm);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400285 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800286 }
H. Peter Anvind85d2502008-05-04 17:53:31 -0700287
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700288 if (oct->have_posix) {
289 snprintf(temp, sizeof temp, "__POSIX_TIME__=%"PRId64, oct->posix);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400290 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800291 }
292}
293
294static void define_macros_late(void)
295{
296 char temp[128];
297
Cyrill Gorcunovc1936da2011-04-06 18:32:15 +0400298 /*
299 * In case if output format is defined by alias
300 * we have to put shortname of the alias itself here
301 * otherwise ABI backward compatibility gets broken.
302 */
Cyrill Gorcunov69ce7502010-07-12 15:19:17 +0400303 snprintf(temp, sizeof(temp), "__OUTPUT_FORMAT__=%s",
Cyrill Gorcunovc1936da2011-04-06 18:32:15 +0400304 ofmt_alias ? ofmt_alias->shortname : ofmt->shortname);
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +0400305 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800306}
307
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700308static void emit_dependencies(StrList *list)
309{
310 FILE *deps;
311 int linepos, len;
312 StrList *l, *nl;
H. Peter Anvinf05034f2017-08-16 22:08:36 -0700313 bool wmake = (quote_for_make == quote_for_wmake);
314 const char *wrapstr, *nulltarget;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700315
H. Peter Anvinf05034f2017-08-16 22:08:36 -0700316 wrapstr = wmake ? " &\n " : " \\\n ";
317 nulltarget = wmake ? "\t%null\n" : "";
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700318
319 if (depend_file && strcmp(depend_file, "-")) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -0700320 deps = nasm_open_write(depend_file, NF_TEXT);
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400321 if (!deps) {
322 nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
323 "unable to write dependency file `%s'", depend_file);
324 return;
325 }
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700326 } else {
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400327 deps = stdout;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700328 }
H. Peter Anvin323fcff2009-07-12 12:04:56 -0700329
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700330 linepos = fprintf(deps, "%s :", depend_target);
Cyrill Gorcunovfcd0a742009-07-27 16:26:26 +0400331 list_for_each(l, list) {
H. Peter Anvin55340992012-09-09 17:09:00 -0700332 char *file = quote_for_make(l->str);
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400333 len = strlen(file);
334 if (linepos + len > 62 && linepos > 1) {
H. Peter Anvinf05034f2017-08-16 22:08:36 -0700335 fputs(wrapstr, deps);
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400336 linepos = 1;
337 }
338 fprintf(deps, " %s", file);
339 linepos += len+1;
H. Peter Anvin55340992012-09-09 17:09:00 -0700340 nasm_free(file);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700341 }
342 fprintf(deps, "\n\n");
H. Peter Anvin323fcff2009-07-12 12:04:56 -0700343
Cyrill Gorcunovfcd0a742009-07-27 16:26:26 +0400344 list_for_each_safe(l, nl, list) {
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700345 if (depend_emit_phony) {
346 char *file = quote_for_make(l->str);
H. Peter Anvinf05034f2017-08-16 22:08:36 -0700347 fprintf(deps, "%s :\n%s\n", file, nulltarget);
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700348 nasm_free(file);
349 }
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400350 nasm_free(l);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700351 }
352
353 if (deps != stdout)
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400354 fclose(deps);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700355}
356
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700357/* Convert a struct tm to a POSIX-style time constant */
358static int64_t make_posix_time(const struct tm *tm)
359{
360 int64_t t;
361 int64_t y = tm->tm_year;
362
363 /* See IEEE 1003.1:2004, section 4.14 */
364
365 t = (y-70)*365 + (y-69)/4 - (y-1)/100 + (y+299)/400;
366 t += tm->tm_yday;
367 t *= 24;
368 t += tm->tm_hour;
369 t *= 60;
370 t += tm->tm_min;
371 t *= 60;
372 t += tm->tm_sec;
373
374 return t;
375}
376
377static void timestamp(void)
378{
379 struct compile_time * const oct = &official_compile_time;
380 const struct tm *tp, *best_gm;
381
382 time(&oct->t);
383
384 best_gm = NULL;
385
386 tp = localtime(&oct->t);
387 if (tp) {
388 oct->local = *tp;
389 best_gm = &oct->local;
390 oct->have_local = true;
391 }
392
393 tp = gmtime(&oct->t);
394 if (tp) {
395 oct->gm = *tp;
396 best_gm = &oct->gm;
397 oct->have_gm = true;
398 if (!oct->have_local)
399 oct->local = oct->gm;
400 } else {
401 oct->gm = oct->local;
402 }
403
404 if (best_gm) {
405 oct->posix = make_posix_time(best_gm);
406 oct->have_posix = true;
407 }
408}
409
H. Peter Anvin038d8612007-04-12 16:54:50 +0000410int main(int argc, char **argv)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000411{
H. Peter Anvina771be82017-08-16 14:53:18 -0700412 StrList **depend_ptr;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700413
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700414 timestamp();
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800415
H. Peter Anvina7ecf262018-02-06 14:43:07 -0800416 iflag_set_default_cpu(&cpu);
417 iflag_set_default_cpu(&cmd_cpu);
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400418
Charles Crayne2581c862008-09-10 19:21:52 -0700419 pass0 = 0;
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700420 want_usage = terminate_after_phase = false;
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400421 nasm_set_verror(nasm_verror_gnu);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000422
H. Peter Anvin97e15752007-09-25 08:47:47 -0700423 error_file = stderr;
424
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -0700425 tolower_init();
H. Peter Anvin274cda82016-05-10 02:56:29 -0700426 src_init();
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -0700427
H. Peter Anvin, Intel87d9e622018-06-25 12:58:49 -0700428 /*
429 * We must call init_labels() before the command line parsing,
430 * because we may be setting prefixes/suffixes from the command
431 * line.
432 */
433 init_labels();
434
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000435 offsets = raa_init();
Keith Kaniosb7a89542007-04-12 02:40:54 +0000436 forwrefs = saa_init((int32_t)sizeof(struct forwrefinfo));
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000437
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000438 preproc = &nasmpp;
Cyrill Gorcunov3ed32cb2014-06-22 20:53:12 +0400439 operating_mode = OP_NORMAL;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000440
H. Peter Anvin55568c12016-10-03 19:46:49 -0700441 parse_cmdline(argc, argv, 1);
442 if (terminate_after_phase) {
443 if (want_usage)
444 usage();
445 return 1;
446 }
447
H. Peter Anvin169ac7c2016-09-25 17:08:05 -0700448 /*
449 * Define some macros dependent on the runtime, but not
H. Peter Anvin55568c12016-10-03 19:46:49 -0700450 * on the command line (as those are scanned in cmdline pass 2.)
H. Peter Anvin169ac7c2016-09-25 17:08:05 -0700451 */
452 preproc->init();
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800453 define_macros_early();
454
H. Peter Anvin55568c12016-10-03 19:46:49 -0700455 parse_cmdline(argc, argv, 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000456 if (terminate_after_phase) {
457 if (want_usage)
458 usage();
459 return 1;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000460 }
461
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800462 /* Save away the default state of warnings */
463 memcpy(warning_state_init, warning_state, sizeof warning_state);
464
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800465 if (!using_debug_info) {
466 /* No debug info, redirect to the null backend (empty stubs) */
H. Peter Anvina7bc15d2016-02-17 20:55:08 -0800467 dfmt = &null_debug_form;
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800468 } else if (!debug_format) {
469 /* Default debug format for this backend */
H. Peter Anvina7bc15d2016-02-17 20:55:08 -0800470 dfmt = ofmt->default_dfmt;
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800471 } else {
472 dfmt = dfmt_find(ofmt, debug_format);
473 if (!dfmt) {
474 nasm_fatal(ERR_NOFILE | ERR_USAGE,
475 "unrecognized debug format `%s' for"
476 " output format `%s'",
477 debug_format, ofmt->shortname);
478 }
479 }
H. Peter Anvinbb88d012003-09-10 23:34:23 +0000480
H. Peter Anvin76690a12002-04-30 20:52:49 +0000481 if (ofmt->stdmac)
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +0400482 preproc->extra_stdmac(ofmt->stdmac);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000483
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800484 /* no output file name? */
485 if (!outname)
486 outname = filename_set_extension(inname, ofmt->extension);
487
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000488 /* define some macros dependent of command-line */
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800489 define_macros_late();
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000490
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400491 depend_ptr = (depend_file || (operating_mode & OP_DEPEND)) ? &depend_list : NULL;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700492
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700493 if (!depend_target)
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400494 depend_target = quote_for_make(outname);
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700495
Cyrill Gorcunove9fc88c2014-06-23 02:22:02 +0400496 if (operating_mode & OP_DEPEND) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000497 char *line;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700498
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400499 if (depend_missing_ok)
500 preproc->include_path(NULL); /* "assume generated" */
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700501
H. Peter Anvin130736c2016-02-17 20:27:41 -0800502 preproc->reset(inname, 0, depend_ptr);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000503 ofile = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000504 while ((line = preproc->getline()))
505 nasm_free(line);
506 preproc->cleanup(0);
Cyrill Gorcunove9fc88c2014-06-23 02:22:02 +0400507 } else if (operating_mode & OP_PREPROCESS) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000508 char *line;
H. Peter Anvin274cda82016-05-10 02:56:29 -0700509 const char *file_name = NULL;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000510 int32_t prior_linnum = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000511 int lineinc = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000512
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800513 if (outname) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -0700514 ofile = nasm_open_write(outname, NF_TEXT);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000515 if (!ofile)
H. Peter Anvin41087062016-03-03 14:27:34 -0800516 nasm_fatal(ERR_NOFILE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000517 "unable to open output file `%s'",
518 outname);
519 } else
520 ofile = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000521
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700522 location.known = false;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000523
Cyrill Gorcunovb574b072011-12-05 01:56:40 +0400524 /* pass = 1; */
H. Peter Anvin130736c2016-02-17 20:27:41 -0800525 preproc->reset(inname, 3, depend_ptr);
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800526
527 /* Revert all warnings to the default state */
528 memcpy(warning_state, warning_state_init, sizeof warning_state);
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700529
H. Peter Anvine2c80182005-01-15 22:15:51 +0000530 while ((line = preproc->getline())) {
531 /*
532 * We generate %line directives if needed for later programs
533 */
Keith Kaniosb7a89542007-04-12 02:40:54 +0000534 int32_t linnum = prior_linnum += lineinc;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000535 int altline = src_get(&linnum, &file_name);
536 if (altline) {
537 if (altline == 1 && lineinc == 1)
538 nasm_fputs("", ofile);
539 else {
540 lineinc = (altline != -1 || lineinc != 1);
541 fprintf(ofile ? ofile : stdout,
Keith Kanios93f2e9a2007-04-14 00:10:59 +0000542 "%%line %"PRId32"+%d %s\n", linnum, lineinc,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000543 file_name);
544 }
545 prior_linnum = linnum;
546 }
547 nasm_fputs(line, ofile);
548 nasm_free(line);
549 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000550 preproc->cleanup(0);
551 if (ofile)
552 fclose(ofile);
H. Peter Anvin29695c82018-06-14 17:04:32 -0700553 if (ofile && terminate_after_phase && !keep_all)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000554 remove(outname);
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400555 ofile = NULL;
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400556 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000557
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400558 if (operating_mode & OP_NORMAL) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -0700559 ofile = nasm_open_write(outname, (ofmt->flags & OFMT_TEXT) ? NF_TEXT : NF_BINARY);
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400560 if (!ofile)
H. Peter Anvin41087062016-03-03 14:27:34 -0800561 nasm_fatal(ERR_NOFILE,
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400562 "unable to open output file `%s'", outname);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000563
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400564 ofmt->init();
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400565 dfmt->init();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000566
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400567 assemble_file(inname, depend_ptr);
Victor van den Elzenc82c3722008-06-04 15:24:20 +0200568
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400569 if (!terminate_after_phase) {
H. Peter Anvin477ae442016-03-07 22:53:06 -0800570 ofmt->cleanup();
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400571 cleanup_labels();
572 fflush(ofile);
H. Peter Anvin274cda82016-05-10 02:56:29 -0700573 if (ferror(ofile)) {
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400574 nasm_error(ERR_NONFATAL|ERR_NOFILE,
575 "write error on output file `%s'", outname);
H. Peter Anvin274cda82016-05-10 02:56:29 -0700576 terminate_after_phase = true;
577 }
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400578 }
579
580 if (ofile) {
581 fclose(ofile);
H. Peter Anvin29695c82018-06-14 17:04:32 -0700582 if (terminate_after_phase && !keep_all)
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400583 remove(outname);
584 ofile = NULL;
585 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000586 }
H. Peter Anvin734b1882002-04-30 21:01:08 +0000587
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700588 if (depend_list && !terminate_after_phase)
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400589 emit_dependencies(depend_list);
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700590
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000591 if (want_usage)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000592 usage();
H. Peter Anvin734b1882002-04-30 21:01:08 +0000593
H. Peter Anvine2c80182005-01-15 22:15:51 +0000594 raa_free(offsets);
595 saa_free(forwrefs);
596 eval_cleanup();
H. Peter Anvin74cc5e52007-08-30 22:35:34 +0000597 stdscan_cleanup();
H. Peter Anvin274cda82016-05-10 02:56:29 -0700598 src_free();
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000599
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700600 return terminate_after_phase;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000601}
602
H. Peter Anvineba20a72002-04-30 20:53:55 +0000603/*
604 * Get a parameter for a command line option.
605 * First arg must be in the form of e.g. -f...
606 */
H. Peter Anvin423e3812007-11-15 17:12:29 -0800607static char *get_param(char *p, char *q, bool *advance)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000608{
H. Peter Anvin423e3812007-11-15 17:12:29 -0800609 *advance = false;
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +0400610 if (p[2]) /* the parameter's in the option */
611 return nasm_skip_spaces(p + 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000612 if (q && q[0]) {
H. Peter Anvin423e3812007-11-15 17:12:29 -0800613 *advance = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000614 return q;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000615 }
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400616 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000617 "option `-%c' requires an argument", p[1]);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000618 return NULL;
619}
620
H. Peter Anvindc242712007-11-18 11:55:10 -0800621/*
622 * Copy a filename
623 */
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800624static void copy_filename(const char **dst, const char *src, const char *what)
H. Peter Anvindc242712007-11-18 11:55:10 -0800625{
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800626 if (*dst)
627 nasm_fatal(0, "more than one %s file specified: %s\n", what, src);
H. Peter Anvindc242712007-11-18 11:55:10 -0800628
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800629 *dst = nasm_strdup(src);
H. Peter Anvindc242712007-11-18 11:55:10 -0800630}
631
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700632/*
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700633 * Convert a string to a POSIX make-safe form
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700634 */
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700635static char *quote_for_pmake(const char *str)
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700636{
637 const char *p;
638 char *os, *q;
639
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400640 size_t n = 1; /* Terminating zero */
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700641 size_t nbs = 0;
642
643 if (!str)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400644 return NULL;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700645
646 for (p = str; *p; p++) {
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400647 switch (*p) {
648 case ' ':
649 case '\t':
650 /* Convert N backslashes + ws -> 2N+1 backslashes + ws */
651 n += nbs + 2;
652 nbs = 0;
653 break;
654 case '$':
655 case '#':
656 nbs = 0;
657 n += 2;
658 break;
659 case '\\':
660 nbs++;
661 n++;
662 break;
663 default:
664 nbs = 0;
665 n++;
666 break;
667 }
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700668 }
669
670 /* Convert N backslashes at the end of filename to 2N backslashes */
671 if (nbs)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400672 n += nbs;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700673
674 os = q = nasm_malloc(n);
675
676 nbs = 0;
677 for (p = str; *p; p++) {
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400678 switch (*p) {
679 case ' ':
680 case '\t':
681 while (nbs--)
682 *q++ = '\\';
683 *q++ = '\\';
684 *q++ = *p;
685 break;
686 case '$':
687 *q++ = *p;
688 *q++ = *p;
689 nbs = 0;
690 break;
691 case '#':
692 *q++ = '\\';
693 *q++ = *p;
694 nbs = 0;
695 break;
696 case '\\':
697 *q++ = *p;
698 nbs++;
699 break;
700 default:
701 *q++ = *p;
702 nbs = 0;
703 break;
704 }
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700705 }
706 while (nbs--)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400707 *q++ = '\\';
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700708
709 *q = '\0';
710
711 return os;
712}
713
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700714/*
715 * Convert a string to a Watcom make-safe form
716 */
717static char *quote_for_wmake(const char *str)
718{
719 const char *p;
720 char *os, *q;
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700721 bool quote = false;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700722
723 size_t n = 1; /* Terminating zero */
724
725 if (!str)
726 return NULL;
727
728 for (p = str; *p; p++) {
729 switch (*p) {
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700730 case ' ':
731 case '\t':
H. Peter Anvin3e30c322017-08-16 22:20:36 -0700732 case '&':
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700733 quote = true;
734 n++;
735 break;
736 case '\"':
737 quote = true;
738 n += 2;
739 break;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700740 case '$':
741 case '#':
742 n += 2;
743 break;
744 default:
745 n++;
746 break;
747 }
748 }
749
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700750 if (quote)
751 n += 2;
752
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700753 os = q = nasm_malloc(n);
754
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700755 if (quote)
756 *q++ = '\"';
757
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700758 for (p = str; *p; p++) {
759 switch (*p) {
760 case '$':
761 case '#':
762 *q++ = '$';
763 *q++ = *p;
764 break;
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700765 case '\"':
766 *q++ = *p;
767 *q++ = *p;
768 break;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700769 default:
770 *q++ = *p;
771 break;
772 }
773 }
774
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700775 if (quote)
776 *q++ = '\"';
777
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700778 *q = '\0';
779
780 return os;
781}
782
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100783enum text_options {
H. Peter Anvin3366e312018-02-07 14:14:36 -0800784 OPT_BOGUS,
785 OPT_VERSION,
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700786 OPT_HELP,
H. Peter Anvin3366e312018-02-07 14:14:36 -0800787 OPT_ABORT_ON_PANIC,
H. Peter Anvin05990342018-06-11 13:32:42 -0700788 OPT_MANGLE,
789 OPT_INCLUDE,
790 OPT_PRAGMA,
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700791 OPT_BEFORE,
H. Peter Anvin29695c82018-06-14 17:04:32 -0700792 OPT_LIMIT,
793 OPT_KEEP_ALL
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100794};
H. Peter Anvin3366e312018-02-07 14:14:36 -0800795struct textargs {
796 const char *label;
797 enum text_options opt;
798 bool need_arg;
H. Peter Anvin98578072018-06-01 18:02:54 -0700799 int pvt;
H. Peter Anvin3366e312018-02-07 14:14:36 -0800800};
H. Peter Anvin2530a102016-02-18 02:28:15 -0800801static const struct textargs textopts[] = {
H. Peter Anvin98578072018-06-01 18:02:54 -0700802 {"v", OPT_VERSION, false, 0},
803 {"version", OPT_VERSION, false, 0},
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700804 {"help", OPT_HELP, false, 0},
H. Peter Anvin98578072018-06-01 18:02:54 -0700805 {"abort-on-panic", OPT_ABORT_ON_PANIC, false, 0},
806 {"prefix", OPT_MANGLE, true, LM_GPREFIX},
807 {"postfix", OPT_MANGLE, true, LM_GSUFFIX},
808 {"gprefix", OPT_MANGLE, true, LM_GPREFIX},
809 {"gpostfix", OPT_MANGLE, true, LM_GSUFFIX},
810 {"lprefix", OPT_MANGLE, true, LM_LPREFIX},
811 {"lpostfix", OPT_MANGLE, true, LM_LSUFFIX},
H. Peter Anvin05990342018-06-11 13:32:42 -0700812 {"include", OPT_INCLUDE, true, 0},
813 {"pragma", OPT_PRAGMA, true, 0},
814 {"before", OPT_BEFORE, true, 0},
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700815 {"limit-", OPT_LIMIT, true, 0},
H. Peter Anvin29695c82018-06-14 17:04:32 -0700816 {"keep-all", OPT_KEEP_ALL, false, 0},
H. Peter Anvin98578072018-06-01 18:02:54 -0700817 {NULL, OPT_BOGUS, false, 0}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000818};
819
Cyrill Gorcunove7438432014-05-09 22:34:34 +0400820static void show_version(void)
821{
822 printf("NASM version %s compiled on %s%s\n",
823 nasm_version, nasm_date, nasm_compile_options);
824 exit(0);
825}
826
H. Peter Anvin423e3812007-11-15 17:12:29 -0800827static bool stopoptions = false;
H. Peter Anvin55568c12016-10-03 19:46:49 -0700828static bool process_arg(char *p, char *q, int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000829{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000830 char *param;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800831 bool advance = false;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000832
833 if (!p || !p[0])
H. Peter Anvin423e3812007-11-15 17:12:29 -0800834 return false;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000835
H. Peter Anvine2c80182005-01-15 22:15:51 +0000836 if (p[0] == '-' && !stopoptions) {
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400837 if (strchr("oOfpPdDiIlFXuUZwW", p[1])) {
838 /* These parameters take values */
839 if (!(param = get_param(p, q, &advance)))
840 return advance;
841 }
H. Peter Anvin423e3812007-11-15 17:12:29 -0800842
H. Peter Anvine2c80182005-01-15 22:15:51 +0000843 switch (p[1]) {
844 case 's':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700845 if (pass == 1)
846 error_file = stdout;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000847 break;
H. Peter Anvin70653092007-10-19 14:42:29 -0700848
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400849 case 'o': /* output file */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700850 if (pass == 2)
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800851 copy_filename(&outname, param, "output");
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400852 break;
H. Peter Anvinfd7dd112007-10-10 14:06:59 -0700853
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400854 case 'f': /* output format */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700855 if (pass == 1) {
856 ofmt = ofmt_find(param, &ofmt_alias);
857 if (!ofmt) {
858 nasm_fatal(ERR_NOFILE | ERR_USAGE,
859 "unrecognised output format `%s' - "
860 "use -hf for a list", param);
861 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400862 }
863 break;
H. Peter Anvin70653092007-10-19 14:42:29 -0700864
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400865 case 'O': /* Optimization level */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700866 if (pass == 2) {
867 int opt;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700868
H. Peter Anvin55568c12016-10-03 19:46:49 -0700869 if (!*param) {
870 /* Naked -O == -Ox */
Chang S. Baea5786342018-08-15 23:22:21 +0300871 optimizing.level = MAX_OPTIMIZE;
H. Peter Anvin55568c12016-10-03 19:46:49 -0700872 } else {
873 while (*param) {
874 switch (*param) {
875 case '0': case '1': case '2': case '3': case '4':
876 case '5': case '6': case '7': case '8': case '9':
877 opt = strtoul(param, &param, 10);
H. Peter Anvind85d2502008-05-04 17:53:31 -0700878
Chang S. Baea5786342018-08-15 23:22:21 +0300879 /* -O0 -> optimizing.level == -1, 0.98 behaviour */
880 /* -O1 -> optimizing.level == 0, 0.98.09 behaviour */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700881 if (opt < 2)
Chang S. Baea5786342018-08-15 23:22:21 +0300882 optimizing.level = opt - 1;
H. Peter Anvin55568c12016-10-03 19:46:49 -0700883 else
Chang S. Baea5786342018-08-15 23:22:21 +0300884 optimizing.level = opt;
H. Peter Anvin55568c12016-10-03 19:46:49 -0700885 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700886
H. Peter Anvin55568c12016-10-03 19:46:49 -0700887 case 'v':
888 case '+':
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400889 param++;
890 opt_verbose_info = true;
891 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700892
H. Peter Anvin55568c12016-10-03 19:46:49 -0700893 case 'x':
894 param++;
Chang S. Baea5786342018-08-15 23:22:21 +0300895 optimizing.level = MAX_OPTIMIZE;
H. Peter Anvin55568c12016-10-03 19:46:49 -0700896 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700897
H. Peter Anvin55568c12016-10-03 19:46:49 -0700898 default:
899 nasm_fatal(0,
900 "unknown optimization option -O%c\n",
901 *param);
902 break;
903 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400904 }
Chang S. Baea5786342018-08-15 23:22:21 +0300905 if (optimizing.level > MAX_OPTIMIZE)
906 optimizing.level = MAX_OPTIMIZE;
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400907 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400908 }
909 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800910
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400911 case 'p': /* pre-include */
912 case 'P':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700913 if (pass == 2)
914 preproc->pre_include(param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400915 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800916
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400917 case 'd': /* pre-define */
918 case 'D':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700919 if (pass == 2)
920 preproc->pre_define(param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400921 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800922
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400923 case 'u': /* un-define */
924 case 'U':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700925 if (pass == 2)
926 preproc->pre_undefine(param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400927 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800928
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400929 case 'i': /* include search path */
930 case 'I':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700931 if (pass == 2)
932 preproc->include_path(param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400933 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800934
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400935 case 'l': /* listing file */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700936 if (pass == 2)
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800937 copy_filename(&listname, param, "listing");
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400938 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800939
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400940 case 'Z': /* error messages file */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700941 if (pass == 1)
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800942 copy_filename(&errname, param, "error");
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400943 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800944
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400945 case 'F': /* specify debug format */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700946 if (pass == 2) {
947 using_debug_info = true;
948 debug_format = param;
949 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400950 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800951
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400952 case 'X': /* specify error reporting format */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700953 if (pass == 1) {
954 if (nasm_stricmp("vc", param) == 0)
955 nasm_set_verror(nasm_verror_vc);
956 else if (nasm_stricmp("gnu", param) == 0)
957 nasm_set_verror(nasm_verror_gnu);
958 else
959 nasm_fatal(ERR_NOFILE | ERR_USAGE,
960 "unrecognized error reporting format `%s'",
961 param);
962 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000963 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800964
H. Peter Anvine2c80182005-01-15 22:15:51 +0000965 case 'g':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700966 if (pass == 2) {
967 using_debug_info = true;
968 if (p[2])
969 debug_format = nasm_skip_spaces(p + 2);
970 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000971 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800972
H. Peter Anvine2c80182005-01-15 22:15:51 +0000973 case 'h':
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700974 help(p[2]);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400975 exit(0); /* never need usage message here */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000976 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800977
H. Peter Anvine2c80182005-01-15 22:15:51 +0000978 case 'y':
979 printf("\nvalid debug formats for '%s' output format are"
980 " ('*' denotes default):\n", ofmt->shortname);
981 dfmt_list(ofmt, stdout);
982 exit(0);
983 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800984
H. Peter Anvine2c80182005-01-15 22:15:51 +0000985 case 't':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700986 if (pass == 2)
987 tasm_compatible_mode = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000988 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800989
H. Peter Anvine2c80182005-01-15 22:15:51 +0000990 case 'v':
Cyrill Gorcunove7438432014-05-09 22:34:34 +0400991 show_version();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000992 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800993
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400994 case 'e': /* preprocess only */
995 case 'E':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700996 if (pass == 1)
997 operating_mode = OP_PREPROCESS;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000998 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800999
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001000 case 'a': /* assemble only - don't preprocess */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001001 if (pass == 1)
1002 preproc = &preproc_nop;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001003 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001004
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001005 case 'w':
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001006 case 'W':
H. Peter Anvin55568c12016-10-03 19:46:49 -07001007 if (pass == 2) {
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001008 if (!set_warning_status(param)) {
1009 nasm_error(ERR_WARNING|ERR_NOFILE|ERR_WARN_UNK_WARNING,
1010 "unknown warning option: %s", param);
H. Peter Anvin55568c12016-10-03 19:46:49 -07001011 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001012 }
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001013 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001014
H. Peter Anvine2c80182005-01-15 22:15:51 +00001015 case 'M':
H. Peter Anvin77c9bf62017-08-16 21:14:33 -07001016 if (pass == 1) {
1017 switch (p[2]) {
1018 case 'W':
1019 quote_for_make = quote_for_wmake;
1020 break;
1021 case 'D':
1022 case 'F':
1023 case 'T':
1024 case 'Q':
1025 advance = true;
1026 break;
1027 default:
1028 break;
1029 }
1030 } else {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001031 switch (p[2]) {
1032 case 0:
1033 operating_mode = OP_DEPEND;
1034 break;
1035 case 'G':
1036 operating_mode = OP_DEPEND;
1037 depend_missing_ok = true;
1038 break;
1039 case 'P':
1040 depend_emit_phony = true;
1041 break;
1042 case 'D':
1043 operating_mode = OP_NORMAL;
1044 depend_file = q;
1045 advance = true;
1046 break;
1047 case 'F':
1048 depend_file = q;
1049 advance = true;
1050 break;
1051 case 'T':
1052 depend_target = q;
1053 advance = true;
1054 break;
1055 case 'Q':
1056 depend_target = quote_for_make(q);
1057 advance = true;
1058 break;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -07001059 case 'W':
1060 /* handled in pass 1 */
1061 break;
H. Peter Anvin55568c12016-10-03 19:46:49 -07001062 default:
1063 nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
1064 "unknown dependency option `-M%c'", p[2]);
1065 break;
1066 }
H. Peter Anvin77c9bf62017-08-16 21:14:33 -07001067 }
1068 if (advance && (!q || !q[0])) {
1069 nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
1070 "option `-M%c' requires a parameter", p[2]);
1071 break;
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001072 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001073 break;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001074
H. Peter Anvine2c80182005-01-15 22:15:51 +00001075 case '-':
1076 {
H. Peter Anvin3366e312018-02-07 14:14:36 -08001077 const struct textargs *tx;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001078 size_t olen, plen;
H. Peter Anvinc805fd72018-06-12 14:23:05 -07001079 char *eqsave;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001080
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001081 p += 2;
1082
1083 if (!*p) { /* -- => stop processing options */
H. Peter Anvin3366e312018-02-07 14:14:36 -08001084 stopoptions = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001085 break;
1086 }
Cyrill Gorcunove7438432014-05-09 22:34:34 +04001087
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001088 plen = strlen(p);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001089 for (tx = textopts; tx->label; tx++) {
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001090 olen = strlen(tx->label);
1091
1092 if (olen > plen)
1093 continue;
1094
1095 if (nasm_memicmp(p, tx->label, olen))
1096 continue;
1097
1098 if (tx->label[olen-1] == '-')
1099 break; /* Incomplete option */
1100
1101 if (!p[olen] || p[olen] == '=')
1102 break; /* Complete option */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001103 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001104
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001105 if (!tx->label) {
1106 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
1107 "unrecognized option `--%s'", p);
1108 }
1109
H. Peter Anvinc805fd72018-06-12 14:23:05 -07001110 eqsave = param = strchr(p+olen, '=');
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001111 if (param)
1112 *param++ = '\0';
1113
H. Peter Anvin3366e312018-02-07 14:14:36 -08001114 if (tx->need_arg) {
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001115 if (!param) {
1116 param = q;
1117 advance = true;
1118 }
1119
1120 /* Note: a null string is a valid parameter */
1121 if (!param) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001122 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvin3366e312018-02-07 14:14:36 -08001123 "option `--%s' requires an argument",
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001124 p);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001125 break;
1126 }
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001127 } else {
1128 if (param) {
1129 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
1130 "option `--%s' does not take an argument",
1131 p);
1132
1133 }
H. Peter Anvin3366e312018-02-07 14:14:36 -08001134 }
1135
1136 switch (tx->opt) {
1137 case OPT_VERSION:
1138 show_version();
1139 break;
1140 case OPT_ABORT_ON_PANIC:
1141 abort_on_panic = true;
1142 break;
H. Peter Anvin98578072018-06-01 18:02:54 -07001143 case OPT_MANGLE:
H. Peter Anvin3366e312018-02-07 14:14:36 -08001144 if (pass == 2)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001145 set_label_mangle(tx->pvt, param);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001146 break;
H. Peter Anvin05990342018-06-11 13:32:42 -07001147 case OPT_INCLUDE:
1148 if (pass == 2)
1149 preproc->pre_include(q);
1150 break;
1151 case OPT_PRAGMA:
1152 if (pass == 2)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001153 preproc->pre_command("pragma", param);
H. Peter Anvin05990342018-06-11 13:32:42 -07001154 break;
1155 case OPT_BEFORE:
1156 if (pass == 2)
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001157 preproc->pre_command(NULL, param);
H. Peter Anvin05990342018-06-11 13:32:42 -07001158 break;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001159 case OPT_LIMIT:
1160 if (pass == 2)
1161 nasm_set_limit(p+olen, param);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001162 break;
H. Peter Anvin29695c82018-06-14 17:04:32 -07001163 case OPT_KEEP_ALL:
1164 keep_all = true;
1165 break;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001166 case OPT_HELP:
1167 help(0);
1168 exit(0);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001169 default:
1170 panic();
H. Peter Anvine2c80182005-01-15 22:15:51 +00001171 }
H. Peter Anvinc805fd72018-06-12 14:23:05 -07001172
1173 if (eqsave)
1174 *eqsave = '='; /* Restore = argument separator */
1175
H. Peter Anvine2c80182005-01-15 22:15:51 +00001176 break;
1177 }
1178
1179 default:
H. Peter Anvinac061332017-03-31 11:41:16 -07001180 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
1181 "unrecognised option `-%c'", p[1]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001182 break;
1183 }
H. Peter Anvin55568c12016-10-03 19:46:49 -07001184 } else if (pass == 2) {
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001185 /* In theory we could allow multiple input files... */
1186 copy_filename(&inname, p, "input");
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001187 }
1188
1189 return advance;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001190}
1191
H. Peter Anvineba20a72002-04-30 20:53:55 +00001192#define ARG_BUF_DELTA 128
1193
H. Peter Anvin55568c12016-10-03 19:46:49 -07001194static void process_respfile(FILE * rfile, int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001195{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001196 char *buffer, *p, *q, *prevarg;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001197 int bufsize, prevargsize;
1198
1199 bufsize = prevargsize = ARG_BUF_DELTA;
1200 buffer = nasm_malloc(ARG_BUF_DELTA);
1201 prevarg = nasm_malloc(ARG_BUF_DELTA);
1202 prevarg[0] = '\0';
1203
H. Peter Anvine2c80182005-01-15 22:15:51 +00001204 while (1) { /* Loop to handle all lines in file */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001205 p = buffer;
1206 while (1) { /* Loop to handle long lines */
1207 q = fgets(p, bufsize - (p - buffer), rfile);
1208 if (!q)
1209 break;
1210 p += strlen(p);
1211 if (p > buffer && p[-1] == '\n')
1212 break;
1213 if (p - buffer > bufsize - 10) {
1214 int offset;
1215 offset = p - buffer;
1216 bufsize += ARG_BUF_DELTA;
1217 buffer = nasm_realloc(buffer, bufsize);
1218 p = buffer + offset;
1219 }
1220 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001221
H. Peter Anvine2c80182005-01-15 22:15:51 +00001222 if (!q && p == buffer) {
1223 if (prevarg[0])
H. Peter Anvin55568c12016-10-03 19:46:49 -07001224 process_arg(prevarg, NULL, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001225 nasm_free(buffer);
1226 nasm_free(prevarg);
1227 return;
1228 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001229
H. Peter Anvine2c80182005-01-15 22:15:51 +00001230 /*
1231 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
1232 * them are present at the end of the line.
1233 */
1234 *(p = &buffer[strcspn(buffer, "\r\n\032")]) = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001235
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001236 while (p > buffer && nasm_isspace(p[-1]))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001237 *--p = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001238
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +04001239 p = nasm_skip_spaces(buffer);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001240
H. Peter Anvin55568c12016-10-03 19:46:49 -07001241 if (process_arg(prevarg, p, pass))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001242 *p = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001243
Charles Crayne192d5b52007-10-18 19:02:42 -07001244 if ((int) strlen(p) > prevargsize - 10) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001245 prevargsize += ARG_BUF_DELTA;
1246 prevarg = nasm_realloc(prevarg, prevargsize);
1247 }
H. Peter Anvindc242712007-11-18 11:55:10 -08001248 strncpy(prevarg, p, prevargsize);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001249 }
1250}
1251
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001252/* Function to process args from a string of args, rather than the
1253 * argv array. Used by the environment variable and response file
1254 * processing.
1255 */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001256static void process_args(char *args, int pass)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001257{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001258 char *p, *q, *arg, *prevarg;
1259 char separator = ' ';
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001260
1261 p = args;
1262 if (*p && *p != '-')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001263 separator = *p++;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001264 arg = NULL;
1265 while (*p) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001266 q = p;
1267 while (*p && *p != separator)
1268 p++;
1269 while (*p == separator)
1270 *p++ = '\0';
1271 prevarg = arg;
1272 arg = q;
H. Peter Anvin55568c12016-10-03 19:46:49 -07001273 if (process_arg(prevarg, arg, pass))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001274 arg = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001275 }
1276 if (arg)
H. Peter Anvin55568c12016-10-03 19:46:49 -07001277 process_arg(arg, NULL, pass);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001278}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001279
H. Peter Anvin55568c12016-10-03 19:46:49 -07001280static void process_response_file(const char *file, int pass)
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001281{
1282 char str[2048];
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001283 FILE *f = nasm_open_read(file, NF_TEXT);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001284 if (!f) {
Cyrill Gorcunovf1964512013-02-15 12:14:06 +04001285 perror(file);
1286 exit(-1);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001287 }
1288 while (fgets(str, sizeof str, f)) {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001289 process_args(str, pass);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001290 }
1291 fclose(f);
1292}
1293
H. Peter Anvin55568c12016-10-03 19:46:49 -07001294static void parse_cmdline(int argc, char **argv, int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001295{
1296 FILE *rfile;
Cyrill Gorcunovf4941892011-07-17 13:55:25 +04001297 char *envreal, *envcopy = NULL, *p;
H. Peter Anvin972079f2008-09-30 17:01:23 -07001298 int i;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001299
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001300 /* Initialize all the warnings to their default state */
1301 for (i = 0; i < ERR_WARN_ALL; i++) {
1302 warning_state_init[i] = warning_state[i] =
1303 warnings[i].enabled ? WARN_ST_ENABLED : 0;
1304 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001305
1306 /*
H. Peter Anvinff7ccc02002-05-06 19:41:57 +00001307 * First, process the NASMENV environment variable.
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001308 */
H. Peter Anvinff7ccc02002-05-06 19:41:57 +00001309 envreal = getenv("NASMENV");
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001310 if (envreal) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001311 envcopy = nasm_strdup(envreal);
H. Peter Anvin55568c12016-10-03 19:46:49 -07001312 process_args(envcopy, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001313 nasm_free(envcopy);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001314 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001315
1316 /*
1317 * Now process the actual command line.
1318 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001319 while (--argc) {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001320 bool advance;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001321 argv++;
1322 if (argv[0][0] == '@') {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001323 /*
1324 * We have a response file, so process this as a set of
H. Peter Anvine2c80182005-01-15 22:15:51 +00001325 * arguments like the environment variable. This allows us
1326 * to have multiple arguments on a single line, which is
1327 * different to the -@resp file processing below for regular
1328 * NASM.
1329 */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001330 process_response_file(argv[0]+1, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001331 argc--;
1332 argv++;
1333 }
1334 if (!stopoptions && argv[0][0] == '-' && argv[0][1] == '@') {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001335 p = get_param(argv[0], argc > 1 ? argv[1] : NULL, &advance);
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001336 if (p) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001337 rfile = nasm_open_read(p, NF_TEXT);
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001338 if (rfile) {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001339 process_respfile(rfile, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001340 fclose(rfile);
1341 } else
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001342 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001343 "unable to open response file `%s'", p);
1344 }
1345 } else
H. Peter Anvin55568c12016-10-03 19:46:49 -07001346 advance = process_arg(argv[0], argc > 1 ? argv[1] : NULL, pass);
H. Peter Anvin423e3812007-11-15 17:12:29 -08001347 argv += advance, argc -= advance;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001348 }
1349
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001350 /*
1351 * Look for basic command line typos. This definitely doesn't
1352 * catch all errors, but it might help cases of fumbled fingers.
1353 */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001354 if (pass != 2)
1355 return;
1356
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001357 if (!inname)
1358 nasm_fatal(ERR_NOFILE | ERR_USAGE, "no input file specified");
H. Peter Anvin59ddd262007-10-01 11:26:31 -07001359
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001360 else if ((errname && !strcmp(inname, errname)) ||
1361 (outname && !strcmp(inname, outname)) ||
1362 (listname && !strcmp(inname, listname)) ||
1363 (depend_file && !strcmp(inname, depend_file)))
1364 nasm_fatal(ERR_USAGE, "will not overwrite input file");
1365
1366 if (errname) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001367 error_file = nasm_open_write(errname, NF_TEXT);
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001368 if (!error_file) {
1369 error_file = stderr; /* Revert to default! */
H. Peter Anvin41087062016-03-03 14:27:34 -08001370 nasm_fatal(ERR_NOFILE | ERR_USAGE,
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001371 "cannot open file `%s' for error messages",
1372 errname);
1373 }
Charles Craynefcce07f2007-09-30 22:15:36 -07001374 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001375}
1376
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001377static void assemble_file(const char *fname, StrList **depend_ptr)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001378{
H. Peter Anvinc7131682017-03-07 17:45:01 -08001379 char *line;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001380 insn output_ins;
H. Peter Anvinc7131682017-03-07 17:45:01 -08001381 int i;
H. Peter Anvin9c595b62017-03-07 19:44:21 -08001382 uint64_t prev_offset_changed;
H. Peter Anvin79561022018-06-15 17:51:39 -07001383 int64_t stall_count = 0; /* Make sure we make forward progress... */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001384
H. Peter Anvina7ecf262018-02-06 14:43:07 -08001385 switch (cmd_sb) {
1386 case 16:
1387 break;
1388 case 32:
1389 if (!iflag_cpu_level_ok(&cmd_cpu, IF_386))
1390 nasm_fatal(0, "command line: 32-bit segment size requires a higher cpu");
1391 break;
1392 case 64:
1393 if (!iflag_cpu_level_ok(&cmd_cpu, IF_X86_64))
1394 nasm_fatal(0, "command line: 64-bit segment size requires a higher cpu");
1395 break;
1396 default:
1397 panic();
1398 break;
1399 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001400
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001401 prev_offset_changed = nasm_limit[LIMIT_PASSES];
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001402 for (passn = 1; pass0 <= 2; passn++) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001403 pass1 = pass0 == 2 ? 2 : 1; /* 1, 1, 1, ..., 1, 2 */
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001404 pass2 = passn > 1 ? 2 : 1; /* 1, 2, 2, ..., 2, 2 */
1405 /* pass0 0, 0, 0, ..., 1, 2 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00001406
H. Peter Anvincac0b192017-03-28 16:12:30 -07001407 globalbits = cmd_sb; /* set 'bits' to command line default */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001408 cpu = cmd_cpu;
1409 if (pass0 == 2) {
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08001410 lfmt->init(listname);
H. Peter Anvin29695c82018-06-14 17:04:32 -07001411 } else if (passn == 1 && listname && !keep_all) {
H. Peter Anvinaac01ff2017-04-02 19:10:26 -07001412 /* Remove the list file in case we die before the output pass */
1413 remove(listname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001414 }
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001415 in_absolute = false;
Charles Craynec1905c22008-09-11 18:54:06 -07001416 global_offset_changed = 0; /* set by redefine_label */
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001417 if (passn > 1) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001418 saa_rewind(forwrefs);
1419 forwref = saa_rstruct(forwrefs);
1420 raa_free(offsets);
1421 offsets = raa_init();
1422 }
H. Peter Anvin892c4812018-05-30 14:43:46 -07001423 location.segment = NO_SEG;
1424 location.offset = 0;
1425 if (passn == 1)
1426 location.known = true;
1427 ofmt->reset();
1428 switch_segment(ofmt->section(NULL, pass2, &globalbits));
H. Peter Anvin130736c2016-02-17 20:27:41 -08001429 preproc->reset(fname, pass1, pass1 == 2 ? depend_ptr : NULL);
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001430
1431 /* Revert all warnings to the default state */
1432 memcpy(warning_state, warning_state_init, sizeof warning_state);
Victor van den Elzen819703a2008-07-16 15:20:56 +02001433
H. Peter Anvine2c80182005-01-15 22:15:51 +00001434 globallineno = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001435
H. Peter Anvine2c80182005-01-15 22:15:51 +00001436 while ((line = preproc->getline())) {
H. Peter Anvin79561022018-06-15 17:51:39 -07001437 if (++globallineno > nasm_limit[LIMIT_LINES])
1438 nasm_fatal(0,
1439 "overall line count exceeds the maximum %"PRId64"\n",
1440 nasm_limit[LIMIT_LINES]);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001441
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001442 /*
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001443 * Here we parse our directives; this is not handled by the
H. Peter Anvinc7131682017-03-07 17:45:01 -08001444 * main parser.
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001445 */
H. Peter Anvina6e26d92017-03-07 21:32:37 -08001446 if (process_directives(line))
H. Peter Anvinc7131682017-03-07 17:45:01 -08001447 goto end_of_line; /* Just do final cleanup */
H. Peter Anvin62b24d72007-08-29 16:38:05 +00001448
H. Peter Anvinc7131682017-03-07 17:45:01 -08001449 /* Not a directive, or even something that starts with [ */
H. Peter Anvin98578072018-06-01 18:02:54 -07001450 parse_line(pass1, line, &output_ins);
H. Peter Anvinc7131682017-03-07 17:45:01 -08001451
Chang S. Baea5786342018-08-15 23:22:21 +03001452 if (optimizing.level > 0) {
H. Peter Anvinc7131682017-03-07 17:45:01 -08001453 if (forwref != NULL && globallineno == forwref->lineno) {
1454 output_ins.forw_ref = true;
1455 do {
1456 output_ins.oprs[forwref->operand].opflags |= OPFLAG_FORWARD;
1457 forwref = saa_rstruct(forwrefs);
1458 } while (forwref != NULL
1459 && forwref->lineno == globallineno);
1460 } else
1461 output_ins.forw_ref = false;
1462
1463 if (output_ins.forw_ref) {
1464 if (passn == 1) {
1465 for (i = 0; i < output_ins.operands; i++) {
1466 if (output_ins.oprs[i].opflags & OPFLAG_FORWARD) {
1467 struct forwrefinfo *fwinf = (struct forwrefinfo *)saa_wstruct(forwrefs);
1468 fwinf->lineno = globallineno;
1469 fwinf->operand = i;
Cyrill Gorcunovd5f2aef2010-04-20 15:33:45 +04001470 }
1471 }
1472 }
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001473 }
H. Peter Anvinc7131682017-03-07 17:45:01 -08001474 }
H. Peter Anvin8bec2c72009-08-08 13:49:00 -07001475
H. Peter Anvinc7131682017-03-07 17:45:01 -08001476 /* forw_ref */
1477 if (output_ins.opcode == I_EQU) {
H. Peter Anvin98578072018-06-01 18:02:54 -07001478 if (!output_ins.label)
1479 nasm_error(ERR_NONFATAL,
1480 "EQU not preceded by label");
H. Peter Anvin73482482018-06-11 14:54:14 -07001481
H. Peter Anvin98578072018-06-01 18:02:54 -07001482 if (output_ins.operands == 1 &&
1483 (output_ins.oprs[0].type & IMMEDIATE) &&
1484 output_ins.oprs[0].wrt == NO_SEG) {
1485 define_label(output_ins.label,
1486 output_ins.oprs[0].segment,
1487 output_ins.oprs[0].offset, false);
1488 } else if (output_ins.operands == 2
1489 && (output_ins.oprs[0].type & IMMEDIATE)
1490 && (output_ins.oprs[0].type & COLON)
1491 && output_ins.oprs[0].segment == NO_SEG
1492 && output_ins.oprs[0].wrt == NO_SEG
1493 && (output_ins.oprs[1].type & IMMEDIATE)
1494 && output_ins.oprs[1].segment == NO_SEG
1495 && output_ins.oprs[1].wrt == NO_SEG) {
1496 define_label(output_ins.label,
1497 output_ins.oprs[0].offset | SEG_ABS,
1498 output_ins.oprs[1].offset, false);
H. Peter Anvinc7131682017-03-07 17:45:01 -08001499 } else {
H. Peter Anvin98578072018-06-01 18:02:54 -07001500 nasm_error(ERR_NONFATAL, "bad syntax for EQU");
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001501 }
H. Peter Anvinc7131682017-03-07 17:45:01 -08001502 } else { /* instruction isn't an EQU */
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001503 int32_t n;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001504
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001505 nasm_assert(output_ins.times >= 0);
H. Peter Anvinc7131682017-03-07 17:45:01 -08001506
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001507 for (n = 1; n <= output_ins.times; n++) {
1508 if (pass1 == 1) {
H. Peter Anvin892c4812018-05-30 14:43:46 -07001509 int64_t l = insn_size(location.segment,
1510 location.offset,
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001511 globalbits, &output_ins);
1512
1513 /* if (using_debug_info) && output_ins.opcode != -1) */
1514 if (using_debug_info)
1515 { /* fbk 03/25/01 */
H. Peter Anvinc7131682017-03-07 17:45:01 -08001516 /* this is done here so we can do debug type info */
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001517 int32_t typeinfo =
1518 TYS_ELEMENTS(output_ins.operands);
1519 switch (output_ins.opcode) {
1520 case I_RESB:
1521 typeinfo =
1522 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_BYTE;
1523 break;
1524 case I_RESW:
1525 typeinfo =
1526 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_WORD;
1527 break;
1528 case I_RESD:
1529 typeinfo =
1530 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_DWORD;
1531 break;
1532 case I_RESQ:
1533 typeinfo =
1534 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_QWORD;
1535 break;
1536 case I_REST:
1537 typeinfo =
1538 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_TBYTE;
1539 break;
1540 case I_RESO:
1541 typeinfo =
1542 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_OWORD;
1543 break;
1544 case I_RESY:
1545 typeinfo =
1546 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_YWORD;
1547 break;
1548 case I_RESZ:
1549 typeinfo =
1550 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_ZWORD;
1551 break;
1552 case I_DB:
1553 typeinfo |= TY_BYTE;
1554 break;
1555 case I_DW:
1556 typeinfo |= TY_WORD;
1557 break;
1558 case I_DD:
1559 if (output_ins.eops_float)
1560 typeinfo |= TY_FLOAT;
1561 else
1562 typeinfo |= TY_DWORD;
1563 break;
1564 case I_DQ:
1565 typeinfo |= TY_QWORD;
1566 break;
1567 case I_DT:
1568 typeinfo |= TY_TBYTE;
1569 break;
1570 case I_DO:
1571 typeinfo |= TY_OWORD;
1572 break;
1573 case I_DY:
1574 typeinfo |= TY_YWORD;
1575 break;
1576 case I_DZ:
1577 typeinfo |= TY_ZWORD;
1578 break;
1579 default:
1580 typeinfo = TY_LABEL;
1581 break;
1582 }
H. Peter Anvinc7131682017-03-07 17:45:01 -08001583
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001584 dfmt->debug_typevalue(typeinfo);
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001585 }
H. Peter Anvinc7131682017-03-07 17:45:01 -08001586
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001587 /*
1588 * For INCBIN, let the code in assemble
1589 * handle TIMES, so we don't have to read the
1590 * input file over and over.
1591 */
1592 if (l != -1) {
H. Peter Anvin892c4812018-05-30 14:43:46 -07001593 increment_offset(l);
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001594 }
1595 /*
1596 * else l == -1 => invalid instruction, which will be
1597 * flagged as an error on pass 2
1598 */
1599 } else {
1600 if (n == 2)
1601 lfmt->uplevel(LIST_TIMES);
H. Peter Anvin892c4812018-05-30 14:43:46 -07001602 increment_offset(assemble(location.segment,
1603 location.offset,
1604 globalbits, &output_ins));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001605 }
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001606 } /* not an EQU */
1607 }
1608 if (output_ins.times > 1)
1609 lfmt->downlevel(LIST_TIMES);
H. Peter Anvinc7131682017-03-07 17:45:01 -08001610
H. Peter Anvinc7131682017-03-07 17:45:01 -08001611 cleanup_insn(&output_ins);
1612
1613 end_of_line:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001614 nasm_free(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001615 } /* end while (line = preproc->getline... */
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001616
H. Peter Anvin (Intel)b45c03a2018-06-27 21:03:38 -07001617 if (global_offset_changed && !terminate_after_phase) {
1618 switch (pass0) {
1619 case 1:
1620 nasm_error(ERR_WARNING|ERR_WARN_PHASE,
1621 "phase error during stabilization pass, hoping for the best");
1622 break;
1623
1624 case 2:
1625 nasm_error(ERR_NONFATAL,
1626 "phase error during code generation pass");
1627 break;
1628
1629 default:
1630 /* This is normal, we'll keep going... */
1631 break;
1632 }
1633 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001634
H. Peter Anvine2c80182005-01-15 22:15:51 +00001635 if (pass1 == 1)
1636 preproc->cleanup(1);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001637
H. Peter Anvin (Intel)12810fa2018-06-27 20:17:33 -07001638 /*
1639 * Always run at least two optimization passes (pass0 == 0);
1640 * things like subsections will fail miserably without that.
1641 * Once we commit to a stabilization pass (pass0 == 1), we can't
1642 * go back, and if something goes bad, we can only hope
1643 * that we don't end up with a phase error at the end.
1644 */
1645 if ((passn > 1 && !global_offset_changed) || pass0 > 0) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001646 pass0++;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001647 } else if (global_offset_changed &&
1648 global_offset_changed < prev_offset_changed) {
Charles Craynec1905c22008-09-11 18:54:06 -07001649 prev_offset_changed = global_offset_changed;
1650 stall_count = 0;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001651 } else {
1652 stall_count++;
1653 }
Victor van den Elzen42528232008-09-11 15:07:05 +02001654
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001655 if (terminate_after_phase)
1656 break;
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001657
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001658 if ((stall_count > nasm_limit[LIMIT_STALLED]) ||
1659 (passn >= nasm_limit[LIMIT_PASSES])) {
Victor van den Elzen42528232008-09-11 15:07:05 +02001660 /* We get here if the labels don't converge
1661 * Example: FOO equ FOO + 1
1662 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001663 nasm_error(ERR_NONFATAL,
Victor van den Elzen42528232008-09-11 15:07:05 +02001664 "Can't find valid values for all labels "
H. Peter Anvin79561022018-06-15 17:51:39 -07001665 "after %"PRId64" passes, giving up.", passn);
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001666 nasm_error(ERR_NONFATAL,
1667 "Possible causes: recursive EQUs, macro abuse.");
1668 break;
1669 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001670 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001671
H. Peter Anvine2c80182005-01-15 22:15:51 +00001672 preproc->cleanup(0);
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08001673 lfmt->cleanup();
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001674 if (!terminate_after_phase && opt_verbose_info) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001675 /* -On and -Ov switches */
H. Peter Anvin79561022018-06-15 17:51:39 -07001676 fprintf(stdout, "info: assembly required 1+%"PRId64"+1 passes\n",
1677 passn-3);
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001678 }
1679}
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001680
Ed Berosetfa771012002-06-09 20:56:40 +00001681/**
1682 * gnu style error reporting
1683 * This function prints an error message to error_file in the
1684 * style used by GNU. An example would be:
1685 * file.asm:50: error: blah blah blah
H. Peter Anvin70653092007-10-19 14:42:29 -07001686 * where file.asm is the name of the file, 50 is the line number on
Ed Berosetfa771012002-06-09 20:56:40 +00001687 * which the error occurs (or is detected) and "error:" is one of
1688 * the possible optional diagnostics -- it can be "error" or "warning"
H. Peter Anvin70653092007-10-19 14:42:29 -07001689 * or something else. Finally the line terminates with the actual
Ed Berosetfa771012002-06-09 20:56:40 +00001690 * error message.
H. Peter Anvin70653092007-10-19 14:42:29 -07001691 *
1692 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001693 * @param fmt the printf style format string
1694 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001695static void nasm_verror_gnu(int severity, const char *fmt, va_list ap)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001696{
H. Peter Anvin274cda82016-05-10 02:56:29 -07001697 const char *currentfile = NULL;
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001698 int32_t lineno = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001699
Ed Berosetfa771012002-06-09 20:56:40 +00001700 if (is_suppressed_warning(severity))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001701 return;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001702
H. Peter Anvin7a6bf742017-12-20 11:54:31 -08001703 if (!(severity & ERR_NOFILE)) {
H. Peter Anvin4def1a82016-05-09 13:59:44 -07001704 src_get(&lineno, &currentfile);
H. Peter Anvin7a6bf742017-12-20 11:54:31 -08001705 if (!currentfile || (severity & ERR_TOPFILE)) {
1706 currentfile = inname[0] ? inname : outname[0] ? outname : NULL;
1707 lineno = 0;
1708 }
1709 }
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001710
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001711 if (!skip_this_pass(severity)) {
H. Peter Anvin7a6bf742017-12-20 11:54:31 -08001712 if (!lineno)
1713 fprintf(error_file, "%s:", currentfile ? currentfile : "nasm");
H. Peter Anvin883985d2017-12-20 11:32:39 -08001714 else
1715 fprintf(error_file, "%s:%"PRId32": ", currentfile, lineno);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001716 }
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001717
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001718 nasm_verror_common(severity, fmt, ap);
Ed Berosetfa771012002-06-09 20:56:40 +00001719}
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001720
Ed Berosetfa771012002-06-09 20:56:40 +00001721/**
1722 * MS style error reporting
1723 * This function prints an error message to error_file in the
H. Peter Anvin70653092007-10-19 14:42:29 -07001724 * style used by Visual C and some other Microsoft tools. An example
Ed Berosetfa771012002-06-09 20:56:40 +00001725 * would be:
Ed Beroset6e61d0d2002-06-11 03:29:36 +00001726 * file.asm(50) : error: blah blah blah
H. Peter Anvin70653092007-10-19 14:42:29 -07001727 * where file.asm is the name of the file, 50 is the line number on
Ed Beroset6e61d0d2002-06-11 03:29:36 +00001728 * which the error occurs (or is detected) and "error:" is one of
1729 * the possible optional diagnostics -- it can be "error" or "warning"
H. Peter Anvin70653092007-10-19 14:42:29 -07001730 * or something else. Finally the line terminates with the actual
Ed Beroset6e61d0d2002-06-11 03:29:36 +00001731 * error message.
H. Peter Anvin70653092007-10-19 14:42:29 -07001732 *
1733 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001734 * @param fmt the printf style format string
1735 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001736static void nasm_verror_vc(int severity, const char *fmt, va_list ap)
Ed Berosetfa771012002-06-09 20:56:40 +00001737{
H. Peter Anvin274cda82016-05-10 02:56:29 -07001738 const char *currentfile = NULL;
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001739 int32_t lineno = 0;
Ed Berosetfa771012002-06-09 20:56:40 +00001740
H. Peter Anvine2c80182005-01-15 22:15:51 +00001741 if (is_suppressed_warning(severity))
1742 return;
Ed Berosetfa771012002-06-09 20:56:40 +00001743
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001744 if (!(severity & ERR_NOFILE))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001745 src_get(&lineno, &currentfile);
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001746
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001747 if (!skip_this_pass(severity)) {
1748 if (currentfile) {
1749 fprintf(error_file, "%s(%"PRId32") : ", currentfile, lineno);
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001750 } else {
1751 fputs("nasm: ", error_file);
1752 }
Ed Berosetfa771012002-06-09 20:56:40 +00001753 }
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001754
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001755 nasm_verror_common(severity, fmt, ap);
Ed Berosetfa771012002-06-09 20:56:40 +00001756}
1757
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001758/*
1759 * check to see if this is a suppressable warning
1760 */
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001761static inline bool is_valid_warning(int severity)
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001762{
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001763 /* Not a warning at all */
1764 if ((severity & ERR_MASK) != ERR_WARNING)
1765 return false;
1766
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001767 return WARN_IDX(severity) < ERR_WARN_ALL;
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001768}
1769
Ed Berosetfa771012002-06-09 20:56:40 +00001770/**
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001771 * check for suppressed warning
H. Peter Anvin70653092007-10-19 14:42:29 -07001772 * checks for suppressed warning or pass one only warning and we're
Ed Berosetfa771012002-06-09 20:56:40 +00001773 * not in pass 1
1774 *
1775 * @param severity the severity of the warning or error
1776 * @return true if we should abort error/warning printing
1777 */
H. Peter Anvin2b046cf2008-01-22 14:08:36 -08001778static bool is_suppressed_warning(int severity)
Ed Berosetfa771012002-06-09 20:56:40 +00001779{
H. Peter Anvin442a05a2012-02-24 21:50:53 -08001780 /* Might be a warning but suppresed explicitly */
H. Peter Anvinc805fd72018-06-12 14:23:05 -07001781 if (is_valid_warning(severity) && !(severity & ERR_USAGE))
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001782 return !(warning_state[WARN_IDX(severity)] & WARN_ST_ENABLED);
1783 else
1784 return false;
1785}
1786
1787static bool warning_is_error(int severity)
1788{
1789 if (is_valid_warning(severity))
1790 return !!(warning_state[WARN_IDX(severity)] & WARN_ST_ERROR);
H. Peter Anvin442a05a2012-02-24 21:50:53 -08001791 else
1792 return false;
Ed Berosetfa771012002-06-09 20:56:40 +00001793}
1794
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001795static bool skip_this_pass(int severity)
1796{
H. Peter Anvin8f622462017-04-02 19:02:29 -07001797 /*
1798 * See if it's a pass-specific error or warning which should be skipped.
1799 * We cannot skip errors stronger than ERR_NONFATAL as by definition
1800 * they cannot be resumed from.
1801 */
1802 if ((severity & ERR_MASK) > ERR_NONFATAL)
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001803 return false;
1804
H. Peter Anvin934f0472016-05-09 12:00:19 -07001805 /*
1806 * passn is 1 on the very first pass only.
1807 * pass0 is 2 on the code-generation (final) pass only.
1808 * These are the passes we care about in this case.
1809 */
1810 return (((severity & ERR_PASS1) && passn != 1) ||
1811 ((severity & ERR_PASS2) && pass0 != 2));
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001812}
1813
Ed Berosetfa771012002-06-09 20:56:40 +00001814/**
1815 * common error reporting
1816 * This is the common back end of the error reporting schemes currently
H. Peter Anvin70653092007-10-19 14:42:29 -07001817 * implemented. It prints the nature of the warning and then the
Ed Berosetfa771012002-06-09 20:56:40 +00001818 * specific error message to error_file and may or may not return. It
1819 * doesn't return if the error severity is a "panic" or "debug" type.
H. Peter Anvin70653092007-10-19 14:42:29 -07001820 *
1821 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001822 * @param fmt the printf style format string
1823 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001824static void nasm_verror_common(int severity, const char *fmt, va_list args)
Ed Berosetfa771012002-06-09 20:56:40 +00001825{
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001826 char msg[1024];
1827 const char *pfx;
H. Peter Anvin323fcff2009-07-12 12:04:56 -07001828
H. Peter Anvin7df04172008-06-10 18:27:38 -07001829 switch (severity & (ERR_MASK|ERR_NO_SEVERITY)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001830 case ERR_WARNING:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001831 pfx = "warning: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001832 break;
1833 case ERR_NONFATAL:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001834 pfx = "error: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001835 break;
1836 case ERR_FATAL:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001837 pfx = "fatal: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001838 break;
1839 case ERR_PANIC:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001840 pfx = "panic: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001841 break;
1842 case ERR_DEBUG:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001843 pfx = "debug: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001844 break;
H. Peter Anvin7df04172008-06-10 18:27:38 -07001845 default:
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001846 pfx = "";
1847 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001848 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00001849
H. Peter Anvin934f0472016-05-09 12:00:19 -07001850 vsnprintf(msg, sizeof msg - 64, fmt, args);
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001851 if (is_valid_warning(severity) && WARN_IDX(severity) != ERR_WARN_OTHER) {
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001852 char *p = strchr(msg, '\0');
H. Peter Anvin934f0472016-05-09 12:00:19 -07001853 snprintf(p, 64, " [-w+%s]", warnings[WARN_IDX(severity)].name);
1854 }
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001855
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001856 if (!skip_this_pass(severity))
1857 fprintf(error_file, "%s%s\n", pfx, msg);
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001858
H. Peter Anvin4def1a82016-05-09 13:59:44 -07001859 /* Are we recursing from error_list_macros? */
1860 if (severity & ERR_PP_LISTMACRO)
1861 return;
1862
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001863 /*
1864 * Don't suppress this with skip_this_pass(), or we don't get
H. Peter Anvin934f0472016-05-09 12:00:19 -07001865 * pass1 or preprocessor warnings in the list file
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001866 */
H. Peter Anvin0fcb4882016-07-06 11:55:25 -07001867 lfmt->error(severity, pfx, msg);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001868
H. Peter Anvin8f622462017-04-02 19:02:29 -07001869 if (skip_this_pass(severity))
1870 return;
1871
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001872 if (severity & ERR_USAGE)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001873 want_usage = true;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001874
H. Peter Anvin4def1a82016-05-09 13:59:44 -07001875 preproc->error_list_macros(severity);
1876
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001877 switch (severity & ERR_MASK) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001878 case ERR_DEBUG:
1879 /* no further action, by definition */
1880 break;
H. Peter Anvinb030c922007-11-13 11:31:15 -08001881 case ERR_WARNING:
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001882 /* Treat warnings as errors */
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001883 if (warning_is_error(severity))
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001884 terminate_after_phase = true;
1885 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001886 case ERR_NONFATAL:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001887 terminate_after_phase = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001888 break;
1889 case ERR_FATAL:
1890 if (ofile) {
1891 fclose(ofile);
H. Peter Anvin29695c82018-06-14 17:04:32 -07001892 if (!keep_all)
1893 remove(outname);
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001894 ofile = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001895 }
1896 if (want_usage)
1897 usage();
1898 exit(1); /* instantly die */
1899 break; /* placate silly compilers */
1900 case ERR_PANIC:
1901 fflush(NULL);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001902
1903 if (abort_on_panic)
1904 abort(); /* halt, catch fire, dump core/stop debugger */
1905
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001906 if (ofile) {
1907 fclose(ofile);
H. Peter Anvin29695c82018-06-14 17:04:32 -07001908 if (!keep_all)
1909 remove(outname);
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001910 ofile = NULL;
1911 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001912 exit(3);
1913 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001914 }
1915}
1916
H. Peter Anvin734b1882002-04-30 21:01:08 +00001917static void usage(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001918{
H. Peter Anvin620515a2002-04-30 20:57:38 +00001919 fputs("type `nasm -h' for help\n", error_file);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001920}
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001921
1922static void help(const char xopt)
1923{
1924 int i;
1925
1926 printf
1927 ("usage: nasm [-@ response file] [-o outfile] [-f format] "
1928 "[-l listfile]\n"
1929 " [options...] [--] filename\n"
1930 " or nasm -v (or --v) for version info\n\n"
1931 "\n"
1932 "Response files should contain command line parameters,\n"
1933 "one per line.\n"
1934 "\n"
1935 " -t assemble in SciTech TASM compatible mode\n");
1936 printf
1937 (" -E (or -e) preprocess only (writes output to stdout by default)\n"
1938 " -a don't preprocess (assemble only)\n"
1939 " -M generate Makefile dependencies on stdout\n"
1940 " -MG d:o, missing files assumed generated\n"
1941 " -MF file set Makefile dependency file\n"
1942 " -MD file assemble and generate dependencies\n"
1943 " -MT file dependency target name\n"
1944 " -MQ file dependency target name (quoted)\n"
1945 " -MP emit phony target\n\n"
1946 " -Zfile redirect error messages to file\n"
1947 " -s redirect error messages to stdout\n\n"
1948 " -g generate debugging information\n\n"
1949 " -F format select a debugging format\n\n"
1950 " -gformat same as -g -F format\n\n"
1951 " -o outfile write output to an outfile\n\n"
1952 " -f format select an output format\n\n"
1953 " -l listfile write listing to a listfile\n\n"
1954 " -Ipath add a pathname to the include file path\n");
1955 printf
1956 (" -Olevel optimize opcodes, immediates and branch offsets\n"
1957 " -O0 no optimization\n"
1958 " -O1 minimal optimization\n"
1959 " -Ox multipass optimization (default)\n"
1960 " -Pfile pre-include a file (also --include)\n"
1961 " -Dmacro[=str] pre-define a macro\n"
1962 " -Umacro undefine a macro\n"
1963 " -Xformat specifiy error reporting format (gnu or vc)\n"
1964 " -w+foo enable warning foo (equiv. -Wfoo)\n"
1965 " -w-foo disable warning foo (equiv. -Wno-foo)\n"
1966 " -w[+-]error[=foo]\n"
1967 " promote [specific] warnings to errors\n"
Chang S. Bae1af6ef42018-06-20 17:05:12 -07001968 " -h show invocation summary and exit (also --help)\n\n"
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001969 " --pragma str pre-executes a specific %%pragma\n"
1970 " --before str add line (usually a preprocessor statement) before the input\n"
1971 " --prefix str prepend the given string to all the given string\n"
Chang S. Bae1af6ef42018-06-20 17:05:12 -07001972 " to all extern, common and global symbols (also --gprefix)\n"
1973 " --postfix str append the given string to all the given string\n"
1974 " to all extern, common and global symbols (also --gpostfix)\n"
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001975 " --lprefix str prepend the given string to all other symbols\n"
Chang S. Bae1af6ef42018-06-20 17:05:12 -07001976 " --lpostfix str append the given string to all other symbols\n"
1977 " --keep-all output files will not be removed even if an error happens\n"
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001978 " --limit-X val set execution limit X\n");
1979
1980 for (i = 0; i <= LIMIT_MAX; i++) {
1981 printf(" %-15s %s (default ",
1982 limit_info[i].name, limit_info[i].help);
1983 if (nasm_limit[i] < LIMIT_MAX_VAL) {
H. Peter Anvin79561022018-06-15 17:51:39 -07001984 printf("%"PRId64")\n", nasm_limit[i]);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001985 } else {
1986 printf("unlimited)\n");
1987 }
1988 }
1989
1990 printf("\nWarnings for the -W/-w options:\n");
1991
1992 for (i = 0; i <= ERR_WARN_ALL; i++)
1993 printf(" %-23s %s%s\n",
1994 warnings[i].name, warnings[i].help,
1995 i == ERR_WARN_ALL ? "\n" :
1996 warnings[i].enabled ? " (default on)" :
1997 " (default off)");
1998
1999 if (xopt == 'f') {
2000 printf("valid output formats for -f are"
2001 " (`*' denotes default):\n");
2002 ofmt_list(ofmt, stdout);
2003 } else {
2004 printf("For a list of valid output formats, use -hf.\n");
2005 printf("For a list of debug formats, use -f <format> -y.\n");
2006 }
2007}