blob: 213cfffbc054a5e0b99710c0eb25967aa41813aa [file] [log] [blame]
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07001/* ----------------------------------------------------------------------- *
H. Peter Anvin323fcff2009-07-12 12:04:56 -07002 *
H. Peter Anvinc7131682017-03-07 17:45:01 -08003 * Copyright 1996-2017 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 Anvin9e1f5282008-05-29 21:38:00 -070078static void assemble_file(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);
85
H. Peter Anvin283b3fb2016-03-07 23:18:30 -080086static bool using_debug_info, opt_verbose_info;
87static const char *debug_format;
88
H. Peter Anvin6867acc2007-10-10 14:58:45 -070089bool tasm_compatible_mode = false;
H. Peter Anvin00835fe2008-01-08 23:03:57 -080090int pass0, passn;
H. Peter Anvinc7131682017-03-07 17:45:01 -080091static int pass1, pass2; /* XXX: Get rid of these, they are redundant */
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +000092int globalrel = 0;
Jin Kyu Songb287ff02013-12-04 20:05:55 -080093int globalbnd = 0;
H. Peter Anvineba20a72002-04-30 20:53:55 +000094
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -070095struct compile_time official_compile_time;
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -080096
Keith Kaniosa6dfa782007-04-13 16:47:53 +000097static char inname[FILENAME_MAX];
98static char outname[FILENAME_MAX];
99static char listname[FILENAME_MAX];
Charles Craynefcce07f2007-09-30 22:15:36 -0700100static char errname[FILENAME_MAX];
H. Peter Anvine2c80182005-01-15 22:15:51 +0000101static int globallineno; /* for forward-reference tracking */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000102/* static int pass = 0; */
H. Peter Anvin338656c2016-02-17 20:59:22 -0800103const struct ofmt *ofmt = &OF_DEFAULT;
104const struct ofmt_alias *ofmt_alias = NULL;
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400105const struct dfmt *dfmt;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000106
H. Peter Anvine2c80182005-01-15 22:15:51 +0000107static FILE *error_file; /* Where to write error messages */
H. Peter Anvin620515a2002-04-30 20:57:38 +0000108
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400109FILE *ofile = NULL;
H. Peter Anvin31387b22010-07-15 18:28:52 -0700110int optimizing = MAX_OPTIMIZE; /* number of optimization passes to take */
Martin Lindhe8cc93f52016-11-16 16:48:13 +0100111static int cmd_sb = 16; /* by default */
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400112
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800113iflag_t cpu;
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400114static iflag_t cmd_cpu;
115
H. Peter Anvincd7893d2016-02-18 01:25:46 -0800116struct location location;
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800117bool in_absolute; /* Flag we are in ABSOLUTE seg */
118struct location absolute; /* Segment/offset inside ABSOLUTE */
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000119
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000120static struct RAA *offsets;
H. Peter Anvinea838272002-04-30 20:51:53 +0000121
H. Peter Anvine2c80182005-01-15 22:15:51 +0000122static struct SAA *forwrefs; /* keep track of forward references */
H. Peter Anvin9d637df2007-10-04 13:42:56 -0700123static const struct forwrefinfo *forwref;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000124
H. Peter Anvine7469712016-02-18 02:20:59 -0800125static const struct preproc_ops *preproc;
Cyrill Gorcunov86b2ad02011-07-01 10:38:25 +0400126
Cyrill Gorcunove9fc88c2014-06-23 02:22:02 +0400127#define OP_NORMAL (1u << 0)
128#define OP_PREPROCESS (1u << 1)
129#define OP_DEPEND (1u << 2)
130
131static unsigned int operating_mode;
Cyrill Gorcunov3ed32cb2014-06-22 20:53:12 +0400132
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700133/* Dependency flags */
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700134static bool depend_emit_phony = false;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700135static bool depend_missing_ok = false;
136static const char *depend_target = NULL;
137static const char *depend_file = NULL;
H. Peter Anvina771be82017-08-16 14:53:18 -0700138StrList *depend_list;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000139
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700140static bool want_usage;
141static bool terminate_after_phase;
H. Peter Anvin130736c2016-02-17 20:27:41 -0800142bool user_nolist = false;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000143
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700144static char *quote_for_pmake(const char *str);
145static char *quote_for_wmake(const char *str);
146static char *(*quote_for_make)(const char *) = quote_for_pmake;
H. Peter Anvin55340992012-09-09 17:09:00 -0700147
Cyrill Gorcunov190232f2013-02-15 12:35:04 +0400148static int64_t get_curr_offs(void)
149{
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800150 return in_absolute ? absolute.offset : raa_read(offsets, location.segment);
Cyrill Gorcunov190232f2013-02-15 12:35:04 +0400151}
152
153static void set_curr_offs(int64_t l_off)
154{
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800155 if (in_absolute)
156 absolute.offset = l_off;
Cyrill Gorcunov190232f2013-02-15 12:35:04 +0400157 else
158 offsets = raa_write(offsets, location.segment, l_off);
159}
160
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000161static void nasm_fputs(const char *line, FILE * outfile)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000162{
H. Peter Anvin310b3e12002-05-14 22:38:55 +0000163 if (outfile) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000164 fputs(line, outfile);
H. Peter Anvind1fb15c2007-11-13 09:37:59 -0800165 putc('\n', outfile);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000166 } else
H. Peter Anvine2c80182005-01-15 22:15:51 +0000167 puts(line);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000168}
169
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800170static void define_macros_early(void)
171{
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700172 const struct compile_time * const oct = &official_compile_time;
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800173 char temp[128];
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800174
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700175 if (oct->have_local) {
176 strftime(temp, sizeof temp, "__DATE__=\"%Y-%m-%d\"", &oct->local);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400177 preproc->pre_define(temp);
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700178 strftime(temp, sizeof temp, "__DATE_NUM__=%Y%m%d", &oct->local);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400179 preproc->pre_define(temp);
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700180 strftime(temp, sizeof temp, "__TIME__=\"%H:%M:%S\"", &oct->local);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400181 preproc->pre_define(temp);
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700182 strftime(temp, sizeof temp, "__TIME_NUM__=%H%M%S", &oct->local);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400183 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800184 }
185
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700186 if (oct->have_gm) {
187 strftime(temp, sizeof temp, "__UTC_DATE__=\"%Y-%m-%d\"", &oct->gm);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400188 preproc->pre_define(temp);
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700189 strftime(temp, sizeof temp, "__UTC_DATE_NUM__=%Y%m%d", &oct->gm);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400190 preproc->pre_define(temp);
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700191 strftime(temp, sizeof temp, "__UTC_TIME__=\"%H:%M:%S\"", &oct->gm);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400192 preproc->pre_define(temp);
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700193 strftime(temp, sizeof temp, "__UTC_TIME_NUM__=%H%M%S", &oct->gm);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400194 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800195 }
H. Peter Anvind85d2502008-05-04 17:53:31 -0700196
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700197 if (oct->have_posix) {
198 snprintf(temp, sizeof temp, "__POSIX_TIME__=%"PRId64, oct->posix);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400199 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800200 }
201}
202
203static void define_macros_late(void)
204{
205 char temp[128];
206
Cyrill Gorcunovc1936da2011-04-06 18:32:15 +0400207 /*
208 * In case if output format is defined by alias
209 * we have to put shortname of the alias itself here
210 * otherwise ABI backward compatibility gets broken.
211 */
Cyrill Gorcunov69ce7502010-07-12 15:19:17 +0400212 snprintf(temp, sizeof(temp), "__OUTPUT_FORMAT__=%s",
Cyrill Gorcunovc1936da2011-04-06 18:32:15 +0400213 ofmt_alias ? ofmt_alias->shortname : ofmt->shortname);
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +0400214 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800215}
216
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700217static void emit_dependencies(StrList *list)
218{
219 FILE *deps;
220 int linepos, len;
221 StrList *l, *nl;
H. Peter Anvinf05034f2017-08-16 22:08:36 -0700222 bool wmake = (quote_for_make == quote_for_wmake);
223 const char *wrapstr, *nulltarget;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700224
H. Peter Anvinf05034f2017-08-16 22:08:36 -0700225 wrapstr = wmake ? " &\n " : " \\\n ";
226 nulltarget = wmake ? "\t%null\n" : "";
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700227
228 if (depend_file && strcmp(depend_file, "-")) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -0700229 deps = nasm_open_write(depend_file, NF_TEXT);
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400230 if (!deps) {
231 nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
232 "unable to write dependency file `%s'", depend_file);
233 return;
234 }
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700235 } else {
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400236 deps = stdout;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700237 }
H. Peter Anvin323fcff2009-07-12 12:04:56 -0700238
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700239 linepos = fprintf(deps, "%s :", depend_target);
Cyrill Gorcunovfcd0a742009-07-27 16:26:26 +0400240 list_for_each(l, list) {
H. Peter Anvin55340992012-09-09 17:09:00 -0700241 char *file = quote_for_make(l->str);
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400242 len = strlen(file);
243 if (linepos + len > 62 && linepos > 1) {
H. Peter Anvinf05034f2017-08-16 22:08:36 -0700244 fputs(wrapstr, deps);
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400245 linepos = 1;
246 }
247 fprintf(deps, " %s", file);
248 linepos += len+1;
H. Peter Anvin55340992012-09-09 17:09:00 -0700249 nasm_free(file);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700250 }
251 fprintf(deps, "\n\n");
H. Peter Anvin323fcff2009-07-12 12:04:56 -0700252
Cyrill Gorcunovfcd0a742009-07-27 16:26:26 +0400253 list_for_each_safe(l, nl, list) {
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700254 if (depend_emit_phony) {
255 char *file = quote_for_make(l->str);
H. Peter Anvinf05034f2017-08-16 22:08:36 -0700256 fprintf(deps, "%s :\n%s\n", file, nulltarget);
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700257 nasm_free(file);
258 }
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400259 nasm_free(l);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700260 }
261
262 if (deps != stdout)
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400263 fclose(deps);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700264}
265
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700266/* Convert a struct tm to a POSIX-style time constant */
267static int64_t make_posix_time(const struct tm *tm)
268{
269 int64_t t;
270 int64_t y = tm->tm_year;
271
272 /* See IEEE 1003.1:2004, section 4.14 */
273
274 t = (y-70)*365 + (y-69)/4 - (y-1)/100 + (y+299)/400;
275 t += tm->tm_yday;
276 t *= 24;
277 t += tm->tm_hour;
278 t *= 60;
279 t += tm->tm_min;
280 t *= 60;
281 t += tm->tm_sec;
282
283 return t;
284}
285
286static void timestamp(void)
287{
288 struct compile_time * const oct = &official_compile_time;
289 const struct tm *tp, *best_gm;
290
291 time(&oct->t);
292
293 best_gm = NULL;
294
295 tp = localtime(&oct->t);
296 if (tp) {
297 oct->local = *tp;
298 best_gm = &oct->local;
299 oct->have_local = true;
300 }
301
302 tp = gmtime(&oct->t);
303 if (tp) {
304 oct->gm = *tp;
305 best_gm = &oct->gm;
306 oct->have_gm = true;
307 if (!oct->have_local)
308 oct->local = oct->gm;
309 } else {
310 oct->gm = oct->local;
311 }
312
313 if (best_gm) {
314 oct->posix = make_posix_time(best_gm);
315 oct->have_posix = true;
316 }
317}
318
H. Peter Anvin038d8612007-04-12 16:54:50 +0000319int main(int argc, char **argv)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000320{
H. Peter Anvina771be82017-08-16 14:53:18 -0700321 StrList **depend_ptr;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700322
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700323 timestamp();
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800324
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400325 iflag_set(&cpu, IF_PLEVEL);
326 iflag_set(&cmd_cpu, IF_PLEVEL);
327
Charles Crayne2581c862008-09-10 19:21:52 -0700328 pass0 = 0;
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700329 want_usage = terminate_after_phase = false;
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400330 nasm_set_verror(nasm_verror_gnu);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000331
H. Peter Anvin97e15752007-09-25 08:47:47 -0700332 error_file = stderr;
333
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -0700334 tolower_init();
H. Peter Anvin274cda82016-05-10 02:56:29 -0700335 src_init();
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -0700336
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000337 offsets = raa_init();
Keith Kaniosb7a89542007-04-12 02:40:54 +0000338 forwrefs = saa_init((int32_t)sizeof(struct forwrefinfo));
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000339
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000340 preproc = &nasmpp;
Cyrill Gorcunov3ed32cb2014-06-22 20:53:12 +0400341 operating_mode = OP_NORMAL;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000342
H. Peter Anvin55568c12016-10-03 19:46:49 -0700343 parse_cmdline(argc, argv, 1);
344 if (terminate_after_phase) {
345 if (want_usage)
346 usage();
347 return 1;
348 }
349
H. Peter Anvin169ac7c2016-09-25 17:08:05 -0700350 /*
351 * Define some macros dependent on the runtime, but not
H. Peter Anvin55568c12016-10-03 19:46:49 -0700352 * on the command line (as those are scanned in cmdline pass 2.)
H. Peter Anvin169ac7c2016-09-25 17:08:05 -0700353 */
354 preproc->init();
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800355 define_macros_early();
356
H. Peter Anvin55568c12016-10-03 19:46:49 -0700357 parse_cmdline(argc, argv, 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000358 if (terminate_after_phase) {
359 if (want_usage)
360 usage();
361 return 1;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000362 }
363
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800364 /* Save away the default state of warnings */
365 memcpy(warning_state_init, warning_state, sizeof warning_state);
366
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800367 if (!using_debug_info) {
368 /* No debug info, redirect to the null backend (empty stubs) */
H. Peter Anvina7bc15d2016-02-17 20:55:08 -0800369 dfmt = &null_debug_form;
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800370 } else if (!debug_format) {
371 /* Default debug format for this backend */
H. Peter Anvina7bc15d2016-02-17 20:55:08 -0800372 dfmt = ofmt->default_dfmt;
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800373 } else {
374 dfmt = dfmt_find(ofmt, debug_format);
375 if (!dfmt) {
376 nasm_fatal(ERR_NOFILE | ERR_USAGE,
377 "unrecognized debug format `%s' for"
378 " output format `%s'",
379 debug_format, ofmt->shortname);
380 }
381 }
H. Peter Anvinbb88d012003-09-10 23:34:23 +0000382
H. Peter Anvin76690a12002-04-30 20:52:49 +0000383 if (ofmt->stdmac)
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +0400384 preproc->extra_stdmac(ofmt->stdmac);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000385
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000386 /* define some macros dependent of command-line */
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800387 define_macros_late();
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000388
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400389 depend_ptr = (depend_file || (operating_mode & OP_DEPEND)) ? &depend_list : NULL;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700390
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700391 if (!depend_target)
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400392 depend_target = quote_for_make(outname);
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700393
Cyrill Gorcunove9fc88c2014-06-23 02:22:02 +0400394 if (operating_mode & OP_DEPEND) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000395 char *line;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700396
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400397 if (depend_missing_ok)
398 preproc->include_path(NULL); /* "assume generated" */
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700399
H. Peter Anvin130736c2016-02-17 20:27:41 -0800400 preproc->reset(inname, 0, depend_ptr);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000401 if (outname[0] == '\0')
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400402 ofmt->filename(inname, outname);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000403 ofile = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000404 while ((line = preproc->getline()))
405 nasm_free(line);
406 preproc->cleanup(0);
Cyrill Gorcunove9fc88c2014-06-23 02:22:02 +0400407 } else if (operating_mode & OP_PREPROCESS) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000408 char *line;
H. Peter Anvin274cda82016-05-10 02:56:29 -0700409 const char *file_name = NULL;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000410 int32_t prior_linnum = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000411 int lineinc = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000412
H. Peter Anvine2c80182005-01-15 22:15:51 +0000413 if (*outname) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -0700414 ofile = nasm_open_write(outname, NF_TEXT);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000415 if (!ofile)
H. Peter Anvin41087062016-03-03 14:27:34 -0800416 nasm_fatal(ERR_NOFILE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000417 "unable to open output file `%s'",
418 outname);
419 } else
420 ofile = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000421
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700422 location.known = false;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000423
Cyrill Gorcunovb574b072011-12-05 01:56:40 +0400424 /* pass = 1; */
H. Peter Anvin130736c2016-02-17 20:27:41 -0800425 preproc->reset(inname, 3, depend_ptr);
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800426
427 /* Revert all warnings to the default state */
428 memcpy(warning_state, warning_state_init, sizeof warning_state);
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700429
H. Peter Anvine2c80182005-01-15 22:15:51 +0000430 while ((line = preproc->getline())) {
431 /*
432 * We generate %line directives if needed for later programs
433 */
Keith Kaniosb7a89542007-04-12 02:40:54 +0000434 int32_t linnum = prior_linnum += lineinc;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000435 int altline = src_get(&linnum, &file_name);
436 if (altline) {
437 if (altline == 1 && lineinc == 1)
438 nasm_fputs("", ofile);
439 else {
440 lineinc = (altline != -1 || lineinc != 1);
441 fprintf(ofile ? ofile : stdout,
Keith Kanios93f2e9a2007-04-14 00:10:59 +0000442 "%%line %"PRId32"+%d %s\n", linnum, lineinc,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000443 file_name);
444 }
445 prior_linnum = linnum;
446 }
447 nasm_fputs(line, ofile);
448 nasm_free(line);
449 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000450 preproc->cleanup(0);
451 if (ofile)
452 fclose(ofile);
453 if (ofile && terminate_after_phase)
454 remove(outname);
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400455 ofile = NULL;
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400456 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000457
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400458 if (operating_mode & OP_NORMAL) {
459 /*
460 * We must call ofmt->filename _anyway_, even if the user
461 * has specified their own output file, because some
462 * formats (eg OBJ and COFF) use ofmt->filename to find out
463 * the name of the input file and then put that inside the
464 * file.
465 */
466 ofmt->filename(inname, outname);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000467
H. Peter Anvin3e83cec2016-05-25 04:28:46 -0700468 ofile = nasm_open_write(outname, (ofmt->flags & OFMT_TEXT) ? NF_TEXT : NF_BINARY);
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400469 if (!ofile)
H. Peter Anvin41087062016-03-03 14:27:34 -0800470 nasm_fatal(ERR_NOFILE,
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400471 "unable to open output file `%s'", outname);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000472
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400473 /*
474 * We must call init_labels() before ofmt->init() since
475 * some object formats will want to define labels in their
476 * init routines. (eg OS/2 defines the FLAT group)
477 */
478 init_labels();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000479
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400480 ofmt->init();
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400481 dfmt->init();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000482
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400483 assemble_file(inname, depend_ptr);
Victor van den Elzenc82c3722008-06-04 15:24:20 +0200484
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400485 if (!terminate_after_phase) {
H. Peter Anvin477ae442016-03-07 22:53:06 -0800486 ofmt->cleanup();
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400487 cleanup_labels();
488 fflush(ofile);
H. Peter Anvin274cda82016-05-10 02:56:29 -0700489 if (ferror(ofile)) {
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400490 nasm_error(ERR_NONFATAL|ERR_NOFILE,
491 "write error on output file `%s'", outname);
H. Peter Anvin274cda82016-05-10 02:56:29 -0700492 terminate_after_phase = true;
493 }
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400494 }
495
496 if (ofile) {
497 fclose(ofile);
498 if (terminate_after_phase)
499 remove(outname);
500 ofile = NULL;
501 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000502 }
H. Peter Anvin734b1882002-04-30 21:01:08 +0000503
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700504 if (depend_list && !terminate_after_phase)
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400505 emit_dependencies(depend_list);
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700506
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000507 if (want_usage)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000508 usage();
H. Peter Anvin734b1882002-04-30 21:01:08 +0000509
H. Peter Anvine2c80182005-01-15 22:15:51 +0000510 raa_free(offsets);
511 saa_free(forwrefs);
512 eval_cleanup();
H. Peter Anvin74cc5e52007-08-30 22:35:34 +0000513 stdscan_cleanup();
H. Peter Anvin274cda82016-05-10 02:56:29 -0700514 src_free();
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000515
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700516 return terminate_after_phase;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000517}
518
H. Peter Anvineba20a72002-04-30 20:53:55 +0000519/*
520 * Get a parameter for a command line option.
521 * First arg must be in the form of e.g. -f...
522 */
H. Peter Anvin423e3812007-11-15 17:12:29 -0800523static char *get_param(char *p, char *q, bool *advance)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000524{
H. Peter Anvin423e3812007-11-15 17:12:29 -0800525 *advance = false;
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +0400526 if (p[2]) /* the parameter's in the option */
527 return nasm_skip_spaces(p + 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000528 if (q && q[0]) {
H. Peter Anvin423e3812007-11-15 17:12:29 -0800529 *advance = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000530 return q;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000531 }
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400532 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000533 "option `-%c' requires an argument", p[1]);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000534 return NULL;
535}
536
H. Peter Anvindc242712007-11-18 11:55:10 -0800537/*
538 * Copy a filename
539 */
540static void copy_filename(char *dst, const char *src)
541{
542 size_t len = strlen(src);
543
544 if (len >= (size_t)FILENAME_MAX) {
H. Peter Anvin41087062016-03-03 14:27:34 -0800545 nasm_fatal(ERR_NOFILE, "file name too long");
Cyrill Gorcunov45aa1182013-02-15 12:22:59 +0400546 return;
H. Peter Anvindc242712007-11-18 11:55:10 -0800547 }
548 strncpy(dst, src, FILENAME_MAX);
549}
550
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700551/*
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700552 * Convert a string to a POSIX make-safe form
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700553 */
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700554static char *quote_for_pmake(const char *str)
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700555{
556 const char *p;
557 char *os, *q;
558
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400559 size_t n = 1; /* Terminating zero */
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700560 size_t nbs = 0;
561
562 if (!str)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400563 return NULL;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700564
565 for (p = str; *p; p++) {
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400566 switch (*p) {
567 case ' ':
568 case '\t':
569 /* Convert N backslashes + ws -> 2N+1 backslashes + ws */
570 n += nbs + 2;
571 nbs = 0;
572 break;
573 case '$':
574 case '#':
575 nbs = 0;
576 n += 2;
577 break;
578 case '\\':
579 nbs++;
580 n++;
581 break;
582 default:
583 nbs = 0;
584 n++;
585 break;
586 }
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700587 }
588
589 /* Convert N backslashes at the end of filename to 2N backslashes */
590 if (nbs)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400591 n += nbs;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700592
593 os = q = nasm_malloc(n);
594
595 nbs = 0;
596 for (p = str; *p; p++) {
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400597 switch (*p) {
598 case ' ':
599 case '\t':
600 while (nbs--)
601 *q++ = '\\';
602 *q++ = '\\';
603 *q++ = *p;
604 break;
605 case '$':
606 *q++ = *p;
607 *q++ = *p;
608 nbs = 0;
609 break;
610 case '#':
611 *q++ = '\\';
612 *q++ = *p;
613 nbs = 0;
614 break;
615 case '\\':
616 *q++ = *p;
617 nbs++;
618 break;
619 default:
620 *q++ = *p;
621 nbs = 0;
622 break;
623 }
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700624 }
625 while (nbs--)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400626 *q++ = '\\';
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700627
628 *q = '\0';
629
630 return os;
631}
632
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700633/*
634 * Convert a string to a Watcom make-safe form
635 */
636static char *quote_for_wmake(const char *str)
637{
638 const char *p;
639 char *os, *q;
640
641 size_t n = 1; /* Terminating zero */
642
643 if (!str)
644 return NULL;
645
646 for (p = str; *p; p++) {
647 switch (*p) {
648 case '$':
649 case '#':
650 n += 2;
651 break;
652 default:
653 n++;
654 break;
655 }
656 }
657
658 os = q = nasm_malloc(n);
659
660 for (p = str; *p; p++) {
661 switch (*p) {
662 case '$':
663 case '#':
664 *q++ = '$';
665 *q++ = *p;
666 break;
667 default:
668 *q++ = *p;
669 break;
670 }
671 }
672
673 *q = '\0';
674
675 return os;
676}
677
H. Peter Anvine2c80182005-01-15 22:15:51 +0000678struct textargs {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000679 const char *label;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000680 int value;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000681};
682
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100683enum text_options {
684 OPT_PREFIX,
H. Peter Anvin7214d182016-03-01 22:43:51 -0800685 OPT_POSTFIX
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100686};
H. Peter Anvin2530a102016-02-18 02:28:15 -0800687static const struct textargs textopts[] = {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000688 {"prefix", OPT_PREFIX},
689 {"postfix", OPT_POSTFIX},
690 {NULL, 0}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000691};
692
Cyrill Gorcunove7438432014-05-09 22:34:34 +0400693static void show_version(void)
694{
695 printf("NASM version %s compiled on %s%s\n",
696 nasm_version, nasm_date, nasm_compile_options);
697 exit(0);
698}
699
H. Peter Anvin423e3812007-11-15 17:12:29 -0800700static bool stopoptions = false;
H. Peter Anvin55568c12016-10-03 19:46:49 -0700701static bool process_arg(char *p, char *q, int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000702{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000703 char *param;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800704 int i;
705 bool advance = false;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000706
707 if (!p || !p[0])
H. Peter Anvin423e3812007-11-15 17:12:29 -0800708 return false;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000709
H. Peter Anvine2c80182005-01-15 22:15:51 +0000710 if (p[0] == '-' && !stopoptions) {
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400711 if (strchr("oOfpPdDiIlFXuUZwW", p[1])) {
712 /* These parameters take values */
713 if (!(param = get_param(p, q, &advance)))
714 return advance;
715 }
H. Peter Anvin423e3812007-11-15 17:12:29 -0800716
H. Peter Anvine2c80182005-01-15 22:15:51 +0000717 switch (p[1]) {
718 case 's':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700719 if (pass == 1)
720 error_file = stdout;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000721 break;
H. Peter Anvin70653092007-10-19 14:42:29 -0700722
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400723 case 'o': /* output file */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700724 if (pass == 2)
725 copy_filename(outname, param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400726 break;
H. Peter Anvinfd7dd112007-10-10 14:06:59 -0700727
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400728 case 'f': /* output format */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700729 if (pass == 1) {
730 ofmt = ofmt_find(param, &ofmt_alias);
731 if (!ofmt) {
732 nasm_fatal(ERR_NOFILE | ERR_USAGE,
733 "unrecognised output format `%s' - "
734 "use -hf for a list", param);
735 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400736 }
737 break;
H. Peter Anvin70653092007-10-19 14:42:29 -0700738
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400739 case 'O': /* Optimization level */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700740 if (pass == 2) {
741 int opt;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700742
H. Peter Anvin55568c12016-10-03 19:46:49 -0700743 if (!*param) {
744 /* Naked -O == -Ox */
745 optimizing = MAX_OPTIMIZE;
746 } else {
747 while (*param) {
748 switch (*param) {
749 case '0': case '1': case '2': case '3': case '4':
750 case '5': case '6': case '7': case '8': case '9':
751 opt = strtoul(param, &param, 10);
H. Peter Anvind85d2502008-05-04 17:53:31 -0700752
H. Peter Anvin55568c12016-10-03 19:46:49 -0700753 /* -O0 -> optimizing == -1, 0.98 behaviour */
754 /* -O1 -> optimizing == 0, 0.98.09 behaviour */
755 if (opt < 2)
756 optimizing = opt - 1;
757 else
758 optimizing = opt;
759 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700760
H. Peter Anvin55568c12016-10-03 19:46:49 -0700761 case 'v':
762 case '+':
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400763 param++;
764 opt_verbose_info = true;
765 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700766
H. Peter Anvin55568c12016-10-03 19:46:49 -0700767 case 'x':
768 param++;
769 optimizing = MAX_OPTIMIZE;
770 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700771
H. Peter Anvin55568c12016-10-03 19:46:49 -0700772 default:
773 nasm_fatal(0,
774 "unknown optimization option -O%c\n",
775 *param);
776 break;
777 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400778 }
H. Peter Anvin55568c12016-10-03 19:46:49 -0700779 if (optimizing > MAX_OPTIMIZE)
780 optimizing = MAX_OPTIMIZE;
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400781 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400782 }
783 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800784
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400785 case 'p': /* pre-include */
786 case 'P':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700787 if (pass == 2)
788 preproc->pre_include(param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400789 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800790
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400791 case 'd': /* pre-define */
792 case 'D':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700793 if (pass == 2)
794 preproc->pre_define(param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400795 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800796
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400797 case 'u': /* un-define */
798 case 'U':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700799 if (pass == 2)
800 preproc->pre_undefine(param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400801 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800802
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400803 case 'i': /* include search path */
804 case 'I':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700805 if (pass == 2)
806 preproc->include_path(param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400807 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800808
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400809 case 'l': /* listing file */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700810 if (pass == 2)
811 copy_filename(listname, param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400812 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800813
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400814 case 'Z': /* error messages file */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700815 if (pass == 1)
816 copy_filename(errname, param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400817 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800818
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400819 case 'F': /* specify debug format */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700820 if (pass == 2) {
821 using_debug_info = true;
822 debug_format = param;
823 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400824 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800825
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400826 case 'X': /* specify error reporting format */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700827 if (pass == 1) {
828 if (nasm_stricmp("vc", param) == 0)
829 nasm_set_verror(nasm_verror_vc);
830 else if (nasm_stricmp("gnu", param) == 0)
831 nasm_set_verror(nasm_verror_gnu);
832 else
833 nasm_fatal(ERR_NOFILE | ERR_USAGE,
834 "unrecognized error reporting format `%s'",
835 param);
836 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000837 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800838
H. Peter Anvine2c80182005-01-15 22:15:51 +0000839 case 'g':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700840 if (pass == 2) {
841 using_debug_info = true;
842 if (p[2])
843 debug_format = nasm_skip_spaces(p + 2);
844 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000845 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800846
H. Peter Anvine2c80182005-01-15 22:15:51 +0000847 case 'h':
848 printf
849 ("usage: nasm [-@ response file] [-o outfile] [-f format] "
850 "[-l listfile]\n"
851 " [options...] [--] filename\n"
Cyrill Gorcunove7438432014-05-09 22:34:34 +0400852 " or nasm -v (or --v) for version info\n\n"
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800853 " -t assemble in SciTech TASM compatible mode\n");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000854 printf
H. Peter Anvin413fb902007-09-26 15:19:28 -0700855 (" -E (or -e) preprocess only (writes output to stdout by default)\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000856 " -a don't preprocess (assemble only)\n"
H. Peter Anvin37a321f2007-09-24 13:41:58 -0700857 " -M generate Makefile dependencies on stdout\n"
Cyrill Gorcunov984c4db2009-07-31 00:32:17 +0400858 " -MG d:o, missing files assumed generated\n"
859 " -MF <file> set Makefile dependency file\n"
860 " -MD <file> assemble and generate dependencies\n"
861 " -MT <file> dependency target name\n"
862 " -MQ <file> dependency target name (quoted)\n"
863 " -MP emit phony target\n\n"
H. Peter Anvin413fb902007-09-26 15:19:28 -0700864 " -Z<file> redirect error messages to file\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000865 " -s redirect error messages to stdout\n\n"
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800866 " -g generate debugging information\n\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000867 " -F format select a debugging format\n\n"
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800868 " -gformat same as -g -F format\n\n"
Cyrill Gorcunovdeb082d2013-04-20 20:37:17 +0400869 " -o outfile write output to an outfile\n\n"
870 " -f format select an output format\n\n"
871 " -l listfile write listing to a listfile\n\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000872 " -I<path> adds a pathname to the include file path\n");
873 printf
H. Peter Anvinab5bd052010-07-25 12:43:30 -0700874 (" -O<digit> optimize branch offsets\n"
Cyrill Gorcunov5bc6d8e2012-03-11 14:19:17 +0400875 " -O0: No optimization\n"
Cyrill Gorcunov73b87c62009-07-31 10:26:55 +0400876 " -O1: Minimal optimization\n"
Cyrill Gorcunov5bc6d8e2012-03-11 14:19:17 +0400877 " -Ox: Multipass optimization (default)\n\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000878 " -P<file> pre-includes a file\n"
879 " -D<macro>[=<value>] pre-defines a macro\n"
880 " -U<macro> undefines a macro\n"
881 " -X<format> specifies error reporting format (gnu or vc)\n"
H. Peter Anvinb030c922007-11-13 11:31:15 -0800882 " -w+foo enables warning foo (equiv. -Wfoo)\n"
Cyrill Gorcunov984c4db2009-07-31 00:32:17 +0400883 " -w-foo disable warning foo (equiv. -Wno-foo)\n\n"
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800884 " -w[+-]error[=foo] can be used to promote warnings to errors\n"
885 " -h show invocation summary and exit\n\n"
Cyrill Gorcunov984c4db2009-07-31 00:32:17 +0400886 "--prefix,--postfix\n"
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800887 " these options prepend or append the given string\n"
888 " to all extern and global variables\n"
889 "\n"
890 "Response files should contain command line parameters,\n"
891 "one per line.\n"
892 "\n"
893 "Warnings for the -W/-w options:\n");
894 for (i = 0; i <= ERR_WARN_ALL; i++)
895 printf(" %-23s %s%s\n",
H. Peter Anvin972079f2008-09-30 17:01:23 -0700896 warnings[i].name, warnings[i].help,
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800897 i == ERR_WARN_ALL ? "\n" :
898 warnings[i].enabled ? " (default on)" :
899 " (default off)");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000900 if (p[2] == 'f') {
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800901 printf("valid output formats for -f are"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000902 " (`*' denotes default):\n");
903 ofmt_list(ofmt, stdout);
904 } else {
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800905 printf("For a list of valid output formats, use -hf.\n");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000906 printf("For a list of debug formats, use -f <form> -y.\n");
907 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400908 exit(0); /* never need usage message here */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000909 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800910
H. Peter Anvine2c80182005-01-15 22:15:51 +0000911 case 'y':
912 printf("\nvalid debug formats for '%s' output format are"
913 " ('*' denotes default):\n", ofmt->shortname);
914 dfmt_list(ofmt, stdout);
915 exit(0);
916 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800917
H. Peter Anvine2c80182005-01-15 22:15:51 +0000918 case 't':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700919 if (pass == 2)
920 tasm_compatible_mode = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000921 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800922
H. Peter Anvine2c80182005-01-15 22:15:51 +0000923 case 'v':
Cyrill Gorcunove7438432014-05-09 22:34:34 +0400924 show_version();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000925 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800926
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400927 case 'e': /* preprocess only */
928 case 'E':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700929 if (pass == 1)
930 operating_mode = OP_PREPROCESS;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000931 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800932
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400933 case 'a': /* assemble only - don't preprocess */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700934 if (pass == 1)
935 preproc = &preproc_nop;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000936 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800937
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800938 case 'w':
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400939 case 'W':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700940 if (pass == 2) {
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800941 if (!set_warning_status(param)) {
942 nasm_error(ERR_WARNING|ERR_NOFILE|ERR_WARN_UNK_WARNING,
943 "unknown warning option: %s", param);
H. Peter Anvin55568c12016-10-03 19:46:49 -0700944 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400945 }
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800946 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800947
H. Peter Anvine2c80182005-01-15 22:15:51 +0000948 case 'M':
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700949 if (pass == 1) {
950 switch (p[2]) {
951 case 'W':
952 quote_for_make = quote_for_wmake;
953 break;
954 case 'D':
955 case 'F':
956 case 'T':
957 case 'Q':
958 advance = true;
959 break;
960 default:
961 break;
962 }
963 } else {
H. Peter Anvin55568c12016-10-03 19:46:49 -0700964 switch (p[2]) {
965 case 0:
966 operating_mode = OP_DEPEND;
967 break;
968 case 'G':
969 operating_mode = OP_DEPEND;
970 depend_missing_ok = true;
971 break;
972 case 'P':
973 depend_emit_phony = true;
974 break;
975 case 'D':
976 operating_mode = OP_NORMAL;
977 depend_file = q;
978 advance = true;
979 break;
980 case 'F':
981 depend_file = q;
982 advance = true;
983 break;
984 case 'T':
985 depend_target = q;
986 advance = true;
987 break;
988 case 'Q':
989 depend_target = quote_for_make(q);
990 advance = true;
991 break;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700992 case 'W':
993 /* handled in pass 1 */
994 break;
H. Peter Anvin55568c12016-10-03 19:46:49 -0700995 default:
996 nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
997 "unknown dependency option `-M%c'", p[2]);
998 break;
999 }
H. Peter Anvin77c9bf62017-08-16 21:14:33 -07001000 }
1001 if (advance && (!q || !q[0])) {
1002 nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
1003 "option `-M%c' requires a parameter", p[2]);
1004 break;
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001005 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001006 break;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001007
H. Peter Anvine2c80182005-01-15 22:15:51 +00001008 case '-':
1009 {
1010 int s;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001011
H. Peter Anvine2c80182005-01-15 22:15:51 +00001012 if (p[2] == 0) { /* -- => stop processing options */
1013 stopoptions = 1;
1014 break;
1015 }
Cyrill Gorcunove7438432014-05-09 22:34:34 +04001016
1017 if (!nasm_stricmp(p, "--v"))
1018 show_version();
1019
Andy Willis3f546032016-09-13 00:02:21 +03001020 if (!nasm_stricmp(p, "--version"))
1021 show_version();
1022
H. Peter Anvine2c80182005-01-15 22:15:51 +00001023 for (s = 0; textopts[s].label; s++) {
1024 if (!nasm_stricmp(p + 2, textopts[s].label)) {
1025 break;
1026 }
1027 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001028
H. Peter Anvine2c80182005-01-15 22:15:51 +00001029 switch (s) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001030 case OPT_PREFIX:
1031 case OPT_POSTFIX:
1032 {
1033 if (!q) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001034 nasm_error(ERR_NONFATAL | ERR_NOFILE |
H. Peter Anvine2c80182005-01-15 22:15:51 +00001035 ERR_USAGE,
1036 "option `--%s' requires an argument",
1037 p + 2);
1038 break;
1039 } else {
1040 advance = 1, param = q;
1041 }
1042
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +01001043 switch (s) {
1044 case OPT_PREFIX:
H. Peter Anvin55568c12016-10-03 19:46:49 -07001045 if (pass == 2)
1046 strlcpy(lprefix, param, PREFIX_MAX);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001047 break;
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +01001048 case OPT_POSTFIX:
H. Peter Anvin55568c12016-10-03 19:46:49 -07001049 if (pass == 2)
1050 strlcpy(lpostfix, param, POSTFIX_MAX);
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +01001051 break;
1052 default:
H. Peter Anvinac061332017-03-31 11:41:16 -07001053 panic();
H. Peter Anvine2c80182005-01-15 22:15:51 +00001054 break;
1055 }
1056 break;
1057 }
H. Peter Anvind24dd5f2016-02-08 10:32:13 -08001058
H. Peter Anvine2c80182005-01-15 22:15:51 +00001059 default:
1060 {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001061 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001062 "unrecognised option `--%s'", p + 2);
1063 break;
1064 }
1065 }
1066 break;
1067 }
1068
1069 default:
H. Peter Anvinac061332017-03-31 11:41:16 -07001070 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
1071 "unrecognised option `-%c'", p[1]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001072 break;
1073 }
H. Peter Anvin55568c12016-10-03 19:46:49 -07001074 } else if (pass == 2) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001075 if (*inname) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001076 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001077 "more than one input file specified");
H. Peter Anvindc242712007-11-18 11:55:10 -08001078 } else {
1079 copy_filename(inname, p);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001080 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001081 }
1082
1083 return advance;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001084}
1085
H. Peter Anvineba20a72002-04-30 20:53:55 +00001086#define ARG_BUF_DELTA 128
1087
H. Peter Anvin55568c12016-10-03 19:46:49 -07001088static void process_respfile(FILE * rfile, int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001089{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001090 char *buffer, *p, *q, *prevarg;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001091 int bufsize, prevargsize;
1092
1093 bufsize = prevargsize = ARG_BUF_DELTA;
1094 buffer = nasm_malloc(ARG_BUF_DELTA);
1095 prevarg = nasm_malloc(ARG_BUF_DELTA);
1096 prevarg[0] = '\0';
1097
H. Peter Anvine2c80182005-01-15 22:15:51 +00001098 while (1) { /* Loop to handle all lines in file */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001099 p = buffer;
1100 while (1) { /* Loop to handle long lines */
1101 q = fgets(p, bufsize - (p - buffer), rfile);
1102 if (!q)
1103 break;
1104 p += strlen(p);
1105 if (p > buffer && p[-1] == '\n')
1106 break;
1107 if (p - buffer > bufsize - 10) {
1108 int offset;
1109 offset = p - buffer;
1110 bufsize += ARG_BUF_DELTA;
1111 buffer = nasm_realloc(buffer, bufsize);
1112 p = buffer + offset;
1113 }
1114 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001115
H. Peter Anvine2c80182005-01-15 22:15:51 +00001116 if (!q && p == buffer) {
1117 if (prevarg[0])
H. Peter Anvin55568c12016-10-03 19:46:49 -07001118 process_arg(prevarg, NULL, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001119 nasm_free(buffer);
1120 nasm_free(prevarg);
1121 return;
1122 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001123
H. Peter Anvine2c80182005-01-15 22:15:51 +00001124 /*
1125 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
1126 * them are present at the end of the line.
1127 */
1128 *(p = &buffer[strcspn(buffer, "\r\n\032")]) = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001129
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001130 while (p > buffer && nasm_isspace(p[-1]))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001131 *--p = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001132
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +04001133 p = nasm_skip_spaces(buffer);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001134
H. Peter Anvin55568c12016-10-03 19:46:49 -07001135 if (process_arg(prevarg, p, pass))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001136 *p = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001137
Charles Crayne192d5b52007-10-18 19:02:42 -07001138 if ((int) strlen(p) > prevargsize - 10) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001139 prevargsize += ARG_BUF_DELTA;
1140 prevarg = nasm_realloc(prevarg, prevargsize);
1141 }
H. Peter Anvindc242712007-11-18 11:55:10 -08001142 strncpy(prevarg, p, prevargsize);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001143 }
1144}
1145
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001146/* Function to process args from a string of args, rather than the
1147 * argv array. Used by the environment variable and response file
1148 * processing.
1149 */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001150static void process_args(char *args, int pass)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001151{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001152 char *p, *q, *arg, *prevarg;
1153 char separator = ' ';
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001154
1155 p = args;
1156 if (*p && *p != '-')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001157 separator = *p++;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001158 arg = NULL;
1159 while (*p) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001160 q = p;
1161 while (*p && *p != separator)
1162 p++;
1163 while (*p == separator)
1164 *p++ = '\0';
1165 prevarg = arg;
1166 arg = q;
H. Peter Anvin55568c12016-10-03 19:46:49 -07001167 if (process_arg(prevarg, arg, pass))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001168 arg = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001169 }
1170 if (arg)
H. Peter Anvin55568c12016-10-03 19:46:49 -07001171 process_arg(arg, NULL, pass);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001172}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001173
H. Peter Anvin55568c12016-10-03 19:46:49 -07001174static void process_response_file(const char *file, int pass)
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001175{
1176 char str[2048];
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001177 FILE *f = nasm_open_read(file, NF_TEXT);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001178 if (!f) {
Cyrill Gorcunovf1964512013-02-15 12:14:06 +04001179 perror(file);
1180 exit(-1);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001181 }
1182 while (fgets(str, sizeof str, f)) {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001183 process_args(str, pass);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001184 }
1185 fclose(f);
1186}
1187
H. Peter Anvin55568c12016-10-03 19:46:49 -07001188static void parse_cmdline(int argc, char **argv, int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001189{
1190 FILE *rfile;
Cyrill Gorcunovf4941892011-07-17 13:55:25 +04001191 char *envreal, *envcopy = NULL, *p;
H. Peter Anvin972079f2008-09-30 17:01:23 -07001192 int i;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001193
Charles Craynefcce07f2007-09-30 22:15:36 -07001194 *inname = *outname = *listname = *errname = '\0';
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001195
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001196 /* Initialize all the warnings to their default state */
1197 for (i = 0; i < ERR_WARN_ALL; i++) {
1198 warning_state_init[i] = warning_state[i] =
1199 warnings[i].enabled ? WARN_ST_ENABLED : 0;
1200 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001201
1202 /*
H. Peter Anvinff7ccc02002-05-06 19:41:57 +00001203 * First, process the NASMENV environment variable.
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001204 */
H. Peter Anvinff7ccc02002-05-06 19:41:57 +00001205 envreal = getenv("NASMENV");
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001206 if (envreal) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001207 envcopy = nasm_strdup(envreal);
H. Peter Anvin55568c12016-10-03 19:46:49 -07001208 process_args(envcopy, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001209 nasm_free(envcopy);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001210 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001211
1212 /*
1213 * Now process the actual command line.
1214 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001215 while (--argc) {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001216 bool advance;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001217 argv++;
1218 if (argv[0][0] == '@') {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001219 /*
1220 * We have a response file, so process this as a set of
H. Peter Anvine2c80182005-01-15 22:15:51 +00001221 * arguments like the environment variable. This allows us
1222 * to have multiple arguments on a single line, which is
1223 * different to the -@resp file processing below for regular
1224 * NASM.
1225 */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001226 process_response_file(argv[0]+1, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001227 argc--;
1228 argv++;
1229 }
1230 if (!stopoptions && argv[0][0] == '-' && argv[0][1] == '@') {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001231 p = get_param(argv[0], argc > 1 ? argv[1] : NULL, &advance);
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001232 if (p) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001233 rfile = nasm_open_read(p, NF_TEXT);
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001234 if (rfile) {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001235 process_respfile(rfile, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001236 fclose(rfile);
1237 } else
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001238 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001239 "unable to open response file `%s'", p);
1240 }
1241 } else
H. Peter Anvin55568c12016-10-03 19:46:49 -07001242 advance = process_arg(argv[0], argc > 1 ? argv[1] : NULL, pass);
H. Peter Anvin423e3812007-11-15 17:12:29 -08001243 argv += advance, argc -= advance;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001244 }
1245
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001246 /*
1247 * Look for basic command line typos. This definitely doesn't
1248 * catch all errors, but it might help cases of fumbled fingers.
1249 */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001250 if (pass != 2)
1251 return;
1252
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001253 if (!*inname)
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001254 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001255 "no input file specified");
1256 else if (!strcmp(inname, errname) ||
1257 !strcmp(inname, outname) ||
1258 !strcmp(inname, listname) ||
1259 (depend_file && !strcmp(inname, depend_file)))
H. Peter Anvin41087062016-03-03 14:27:34 -08001260 nasm_fatal(ERR_NOFILE | ERR_USAGE,
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001261 "file `%s' is both input and output file",
1262 inname);
H. Peter Anvin59ddd262007-10-01 11:26:31 -07001263
H. Peter Anvin70653092007-10-19 14:42:29 -07001264 if (*errname) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001265 error_file = nasm_open_write(errname, NF_TEXT);
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001266 if (!error_file) {
1267 error_file = stderr; /* Revert to default! */
H. Peter Anvin41087062016-03-03 14:27:34 -08001268 nasm_fatal(ERR_NOFILE | ERR_USAGE,
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001269 "cannot open file `%s' for error messages",
1270 errname);
1271 }
Charles Craynefcce07f2007-09-30 22:15:36 -07001272 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001273}
1274
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001275static void assemble_file(char *fname, StrList **depend_ptr)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001276{
H. Peter Anvinc7131682017-03-07 17:45:01 -08001277 char *line;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001278 insn output_ins;
H. Peter Anvinc7131682017-03-07 17:45:01 -08001279 int i;
Charles Crayne5fbbc8c2007-11-07 19:03:46 -08001280 int64_t offs;
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001281 int pass_max;
H. Peter Anvin9c595b62017-03-07 19:44:21 -08001282 uint64_t prev_offset_changed;
1283 unsigned int stall_count = 0; /* Make sure we make forward progress... */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001284
Cyrill Gorcunov08359152013-11-09 22:16:11 +04001285 if (cmd_sb == 32 && iflag_ffs(&cmd_cpu) < IF_386)
H. Peter Anvin130736c2016-02-17 20:27:41 -08001286 nasm_fatal(0, "command line: 32-bit segment size requires a higher cpu");
H. Peter Anvineba20a72002-04-30 20:53:55 +00001287
Charles Craynec1905c22008-09-11 18:54:06 -07001288 pass_max = prev_offset_changed = (INT_MAX >> 1) + 2; /* Almost unlimited */
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001289 for (passn = 1; pass0 <= 2; passn++) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001290 ldfunc def_label;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001291
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001292 pass1 = pass0 == 2 ? 2 : 1; /* 1, 1, 1, ..., 1, 2 */
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001293 pass2 = passn > 1 ? 2 : 1; /* 1, 2, 2, ..., 2, 2 */
1294 /* pass0 0, 0, 0, ..., 1, 2 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00001295
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001296 def_label = passn > 1 ? redefine_label : define_label;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001297
H. Peter Anvincac0b192017-03-28 16:12:30 -07001298 globalbits = cmd_sb; /* set 'bits' to command line default */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001299 cpu = cmd_cpu;
1300 if (pass0 == 2) {
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08001301 lfmt->init(listname);
H. Peter Anvinaac01ff2017-04-02 19:10:26 -07001302 } else if (passn == 1 && *listname) {
1303 /* Remove the list file in case we die before the output pass */
1304 remove(listname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001305 }
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001306 in_absolute = false;
Charles Craynec1905c22008-09-11 18:54:06 -07001307 global_offset_changed = 0; /* set by redefine_label */
H. Peter Anvincac0b192017-03-28 16:12:30 -07001308 location.segment = ofmt->section(NULL, pass2, &globalbits);
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001309 if (passn > 1) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001310 saa_rewind(forwrefs);
1311 forwref = saa_rstruct(forwrefs);
1312 raa_free(offsets);
1313 offsets = raa_init();
1314 }
H. Peter Anvin130736c2016-02-17 20:27:41 -08001315 preproc->reset(fname, pass1, pass1 == 2 ? depend_ptr : NULL);
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001316
1317 /* Revert all warnings to the default state */
1318 memcpy(warning_state, warning_state_init, sizeof warning_state);
Victor van den Elzen819703a2008-07-16 15:20:56 +02001319
H. Peter Anvine2c80182005-01-15 22:15:51 +00001320 globallineno = 0;
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001321 if (passn == 1)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001322 location.known = true;
Cyrill Gorcunov190232f2013-02-15 12:35:04 +04001323 location.offset = offs = get_curr_offs();
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001324
H. Peter Anvine2c80182005-01-15 22:15:51 +00001325 while ((line = preproc->getline())) {
1326 globallineno++;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001327
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001328 /*
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001329 * Here we parse our directives; this is not handled by the
H. Peter Anvinc7131682017-03-07 17:45:01 -08001330 * main parser.
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001331 */
H. Peter Anvina6e26d92017-03-07 21:32:37 -08001332 if (process_directives(line))
H. Peter Anvinc7131682017-03-07 17:45:01 -08001333 goto end_of_line; /* Just do final cleanup */
H. Peter Anvin62b24d72007-08-29 16:38:05 +00001334
H. Peter Anvinc7131682017-03-07 17:45:01 -08001335 /* Not a directive, or even something that starts with [ */
1336
1337 parse_line(pass1, line, &output_ins, def_label);
1338
1339 if (optimizing > 0) {
1340 if (forwref != NULL && globallineno == forwref->lineno) {
1341 output_ins.forw_ref = true;
1342 do {
1343 output_ins.oprs[forwref->operand].opflags |= OPFLAG_FORWARD;
1344 forwref = saa_rstruct(forwrefs);
1345 } while (forwref != NULL
1346 && forwref->lineno == globallineno);
1347 } else
1348 output_ins.forw_ref = false;
1349
1350 if (output_ins.forw_ref) {
1351 if (passn == 1) {
1352 for (i = 0; i < output_ins.operands; i++) {
1353 if (output_ins.oprs[i].opflags & OPFLAG_FORWARD) {
1354 struct forwrefinfo *fwinf = (struct forwrefinfo *)saa_wstruct(forwrefs);
1355 fwinf->lineno = globallineno;
1356 fwinf->operand = i;
Cyrill Gorcunovd5f2aef2010-04-20 15:33:45 +04001357 }
1358 }
1359 }
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001360 }
H. Peter Anvinc7131682017-03-07 17:45:01 -08001361 }
H. Peter Anvin8bec2c72009-08-08 13:49:00 -07001362
H. Peter Anvinc7131682017-03-07 17:45:01 -08001363 /* forw_ref */
1364 if (output_ins.opcode == I_EQU) {
1365 if (pass1 == 1) {
1366 /*
1367 * Special `..' EQUs get processed in pass two,
1368 * except `..@' macro-processor EQUs which are done
1369 * in the normal place.
1370 */
1371 if (!output_ins.label)
1372 nasm_error(ERR_NONFATAL,
1373 "EQU not preceded by label");
1374
1375 else if (output_ins.label[0] != '.' ||
1376 output_ins.label[1] != '.' ||
1377 output_ins.label[2] == '@') {
1378 if (output_ins.operands == 1 &&
1379 (output_ins.oprs[0].type & IMMEDIATE) &&
1380 output_ins.oprs[0].wrt == NO_SEG) {
1381 bool isext = !!(output_ins.oprs[0].opflags & OPFLAG_EXTERN);
1382 def_label(output_ins.label,
1383 output_ins.oprs[0].segment,
1384 output_ins.oprs[0].offset, NULL,
1385 false, isext);
1386 } else if (output_ins.operands == 2
1387 && (output_ins.oprs[0].type & IMMEDIATE)
1388 && (output_ins.oprs[0].type & COLON)
1389 && output_ins.oprs[0].segment == NO_SEG
1390 && output_ins.oprs[0].wrt == NO_SEG
1391 && (output_ins.oprs[1].type & IMMEDIATE)
1392 && output_ins.oprs[1].segment == NO_SEG
1393 && output_ins.oprs[1].wrt == NO_SEG) {
1394 def_label(output_ins.label,
1395 output_ins.oprs[0].offset | SEG_ABS,
1396 output_ins.oprs[1].offset,
1397 NULL, false, false);
1398 } else
1399 nasm_error(ERR_NONFATAL,
1400 "bad syntax for EQU");
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001401 }
H. Peter Anvinc7131682017-03-07 17:45:01 -08001402 } else {
1403 /*
1404 * Special `..' EQUs get processed here, except
1405 * `..@' macro processor EQUs which are done above.
1406 */
1407 if (output_ins.label[0] == '.' &&
1408 output_ins.label[1] == '.' &&
1409 output_ins.label[2] != '@') {
1410 if (output_ins.operands == 1 &&
1411 (output_ins.oprs[0].type & IMMEDIATE)) {
1412 define_label(output_ins.label,
1413 output_ins.oprs[0].segment,
1414 output_ins.oprs[0].offset,
1415 NULL, false, false);
1416 } else if (output_ins.operands == 2
1417 && (output_ins.oprs[0].type & IMMEDIATE)
1418 && (output_ins.oprs[0].type & COLON)
1419 && output_ins.oprs[0].segment == NO_SEG
1420 && (output_ins.oprs[1].type & IMMEDIATE)
1421 && output_ins.oprs[1].segment == NO_SEG) {
1422 define_label(output_ins.label,
1423 output_ins.oprs[0].offset | SEG_ABS,
1424 output_ins.oprs[1].offset,
1425 NULL, false, false);
1426 } else
1427 nasm_error(ERR_NONFATAL,
1428 "bad syntax for EQU");
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001429 }
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001430 }
H. Peter Anvinc7131682017-03-07 17:45:01 -08001431 } else { /* instruction isn't an EQU */
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001432 int32_t n;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001433
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001434 nasm_assert(output_ins.times >= 0);
H. Peter Anvinc7131682017-03-07 17:45:01 -08001435
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001436 for (n = 1; n <= output_ins.times; n++) {
1437 if (pass1 == 1) {
1438 int64_t l = insn_size(location.segment, offs,
1439 globalbits, &output_ins);
1440
1441 /* if (using_debug_info) && output_ins.opcode != -1) */
1442 if (using_debug_info)
1443 { /* fbk 03/25/01 */
H. Peter Anvinc7131682017-03-07 17:45:01 -08001444 /* this is done here so we can do debug type info */
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001445 int32_t typeinfo =
1446 TYS_ELEMENTS(output_ins.operands);
1447 switch (output_ins.opcode) {
1448 case I_RESB:
1449 typeinfo =
1450 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_BYTE;
1451 break;
1452 case I_RESW:
1453 typeinfo =
1454 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_WORD;
1455 break;
1456 case I_RESD:
1457 typeinfo =
1458 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_DWORD;
1459 break;
1460 case I_RESQ:
1461 typeinfo =
1462 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_QWORD;
1463 break;
1464 case I_REST:
1465 typeinfo =
1466 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_TBYTE;
1467 break;
1468 case I_RESO:
1469 typeinfo =
1470 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_OWORD;
1471 break;
1472 case I_RESY:
1473 typeinfo =
1474 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_YWORD;
1475 break;
1476 case I_RESZ:
1477 typeinfo =
1478 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_ZWORD;
1479 break;
1480 case I_DB:
1481 typeinfo |= TY_BYTE;
1482 break;
1483 case I_DW:
1484 typeinfo |= TY_WORD;
1485 break;
1486 case I_DD:
1487 if (output_ins.eops_float)
1488 typeinfo |= TY_FLOAT;
1489 else
1490 typeinfo |= TY_DWORD;
1491 break;
1492 case I_DQ:
1493 typeinfo |= TY_QWORD;
1494 break;
1495 case I_DT:
1496 typeinfo |= TY_TBYTE;
1497 break;
1498 case I_DO:
1499 typeinfo |= TY_OWORD;
1500 break;
1501 case I_DY:
1502 typeinfo |= TY_YWORD;
1503 break;
1504 case I_DZ:
1505 typeinfo |= TY_ZWORD;
1506 break;
1507 default:
1508 typeinfo = TY_LABEL;
1509 break;
1510 }
H. Peter Anvinc7131682017-03-07 17:45:01 -08001511
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001512 dfmt->debug_typevalue(typeinfo);
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001513 }
H. Peter Anvinc7131682017-03-07 17:45:01 -08001514
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001515 /*
1516 * For INCBIN, let the code in assemble
1517 * handle TIMES, so we don't have to read the
1518 * input file over and over.
1519 */
1520 if (l != -1) {
1521 offs += l;
1522 set_curr_offs(offs);
1523 }
1524 /*
1525 * else l == -1 => invalid instruction, which will be
1526 * flagged as an error on pass 2
1527 */
1528 } else {
1529 if (n == 2)
1530 lfmt->uplevel(LIST_TIMES);
1531 offs += assemble(location.segment, offs,
1532 globalbits, &output_ins);
Cyrill Gorcunov190232f2013-02-15 12:35:04 +04001533 set_curr_offs(offs);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001534 }
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001535 } /* not an EQU */
1536 }
1537 if (output_ins.times > 1)
1538 lfmt->downlevel(LIST_TIMES);
H. Peter Anvinc7131682017-03-07 17:45:01 -08001539
H. Peter Anvinc7131682017-03-07 17:45:01 -08001540 cleanup_insn(&output_ins);
1541
1542 end_of_line:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001543 nasm_free(line);
Cyrill Gorcunov190232f2013-02-15 12:35:04 +04001544 location.offset = offs = get_curr_offs();
H. Peter Anvine2c80182005-01-15 22:15:51 +00001545 } /* end while (line = preproc->getline... */
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001546
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001547 if (pass0 == 2 && global_offset_changed && !terminate_after_phase)
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001548 nasm_error(ERR_NONFATAL,
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001549 "phase error detected at end of assembly.");
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001550
H. Peter Anvine2c80182005-01-15 22:15:51 +00001551 if (pass1 == 1)
1552 preproc->cleanup(1);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001553
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001554 if ((passn > 1 && !global_offset_changed) || pass0 == 2) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001555 pass0++;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001556 } else if (global_offset_changed &&
1557 global_offset_changed < prev_offset_changed) {
Charles Craynec1905c22008-09-11 18:54:06 -07001558 prev_offset_changed = global_offset_changed;
1559 stall_count = 0;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001560 } else {
1561 stall_count++;
1562 }
Victor van den Elzen42528232008-09-11 15:07:05 +02001563
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001564 if (terminate_after_phase)
1565 break;
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001566
H. Peter Anvin9c595b62017-03-07 19:44:21 -08001567 if ((stall_count > 997U) || (passn >= pass_max)) {
Victor van den Elzen42528232008-09-11 15:07:05 +02001568 /* We get here if the labels don't converge
1569 * Example: FOO equ FOO + 1
1570 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001571 nasm_error(ERR_NONFATAL,
Victor van den Elzen42528232008-09-11 15:07:05 +02001572 "Can't find valid values for all labels "
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001573 "after %d passes, giving up.", passn);
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001574 nasm_error(ERR_NONFATAL,
1575 "Possible causes: recursive EQUs, macro abuse.");
1576 break;
1577 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001578 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001579
H. Peter Anvine2c80182005-01-15 22:15:51 +00001580 preproc->cleanup(0);
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08001581 lfmt->cleanup();
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001582 if (!terminate_after_phase && opt_verbose_info) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001583 /* -On and -Ov switches */
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001584 fprintf(stdout, "info: assembly required 1+%d+1 passes\n", passn-3);
1585 }
1586}
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001587
Ed Berosetfa771012002-06-09 20:56:40 +00001588/**
1589 * gnu style error reporting
1590 * This function prints an error message to error_file in the
1591 * style used by GNU. An example would be:
1592 * file.asm:50: error: blah blah blah
H. Peter Anvin70653092007-10-19 14:42:29 -07001593 * where file.asm is the name of the file, 50 is the line number on
Ed Berosetfa771012002-06-09 20:56:40 +00001594 * which the error occurs (or is detected) and "error:" is one of
1595 * the possible optional diagnostics -- it can be "error" or "warning"
H. Peter Anvin70653092007-10-19 14:42:29 -07001596 * or something else. Finally the line terminates with the actual
Ed Berosetfa771012002-06-09 20:56:40 +00001597 * error message.
H. Peter Anvin70653092007-10-19 14:42:29 -07001598 *
1599 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001600 * @param fmt the printf style format string
1601 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001602static void nasm_verror_gnu(int severity, const char *fmt, va_list ap)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001603{
H. Peter Anvin274cda82016-05-10 02:56:29 -07001604 const char *currentfile = NULL;
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001605 int32_t lineno = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001606
Ed Berosetfa771012002-06-09 20:56:40 +00001607 if (is_suppressed_warning(severity))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001608 return;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001609
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001610 if (!(severity & ERR_NOFILE))
H. Peter Anvin4def1a82016-05-09 13:59:44 -07001611 src_get(&lineno, &currentfile);
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001612
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001613 if (!skip_this_pass(severity)) {
1614 if (currentfile) {
1615 fprintf(error_file, "%s:%"PRId32": ", currentfile, lineno);
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001616 } else {
1617 fputs("nasm: ", error_file);
1618 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001619 }
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001620
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001621 nasm_verror_common(severity, fmt, ap);
Ed Berosetfa771012002-06-09 20:56:40 +00001622}
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001623
Ed Berosetfa771012002-06-09 20:56:40 +00001624/**
1625 * MS style error reporting
1626 * This function prints an error message to error_file in the
H. Peter Anvin70653092007-10-19 14:42:29 -07001627 * style used by Visual C and some other Microsoft tools. An example
Ed Berosetfa771012002-06-09 20:56:40 +00001628 * would be:
Ed Beroset6e61d0d2002-06-11 03:29:36 +00001629 * file.asm(50) : error: blah blah blah
H. Peter Anvin70653092007-10-19 14:42:29 -07001630 * where file.asm is the name of the file, 50 is the line number on
Ed Beroset6e61d0d2002-06-11 03:29:36 +00001631 * which the error occurs (or is detected) and "error:" is one of
1632 * the possible optional diagnostics -- it can be "error" or "warning"
H. Peter Anvin70653092007-10-19 14:42:29 -07001633 * or something else. Finally the line terminates with the actual
Ed Beroset6e61d0d2002-06-11 03:29:36 +00001634 * error message.
H. Peter Anvin70653092007-10-19 14:42:29 -07001635 *
1636 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001637 * @param fmt the printf style format string
1638 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001639static void nasm_verror_vc(int severity, const char *fmt, va_list ap)
Ed Berosetfa771012002-06-09 20:56:40 +00001640{
H. Peter Anvin274cda82016-05-10 02:56:29 -07001641 const char *currentfile = NULL;
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001642 int32_t lineno = 0;
Ed Berosetfa771012002-06-09 20:56:40 +00001643
H. Peter Anvine2c80182005-01-15 22:15:51 +00001644 if (is_suppressed_warning(severity))
1645 return;
Ed Berosetfa771012002-06-09 20:56:40 +00001646
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001647 if (!(severity & ERR_NOFILE))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001648 src_get(&lineno, &currentfile);
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001649
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001650 if (!skip_this_pass(severity)) {
1651 if (currentfile) {
1652 fprintf(error_file, "%s(%"PRId32") : ", currentfile, lineno);
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001653 } else {
1654 fputs("nasm: ", error_file);
1655 }
Ed Berosetfa771012002-06-09 20:56:40 +00001656 }
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001657
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001658 nasm_verror_common(severity, fmt, ap);
Ed Berosetfa771012002-06-09 20:56:40 +00001659}
1660
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001661/*
1662 * check to see if this is a suppressable warning
1663 */
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001664static inline bool is_valid_warning(int severity)
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001665{
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001666 /* Not a warning at all */
1667 if ((severity & ERR_MASK) != ERR_WARNING)
1668 return false;
1669
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001670 return WARN_IDX(severity) < ERR_WARN_ALL;
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001671}
1672
Ed Berosetfa771012002-06-09 20:56:40 +00001673/**
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001674 * check for suppressed warning
H. Peter Anvin70653092007-10-19 14:42:29 -07001675 * checks for suppressed warning or pass one only warning and we're
Ed Berosetfa771012002-06-09 20:56:40 +00001676 * not in pass 1
1677 *
1678 * @param severity the severity of the warning or error
1679 * @return true if we should abort error/warning printing
1680 */
H. Peter Anvin2b046cf2008-01-22 14:08:36 -08001681static bool is_suppressed_warning(int severity)
Ed Berosetfa771012002-06-09 20:56:40 +00001682{
H. Peter Anvin442a05a2012-02-24 21:50:53 -08001683 /* Might be a warning but suppresed explicitly */
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001684 if (is_valid_warning(severity))
1685 return !(warning_state[WARN_IDX(severity)] & WARN_ST_ENABLED);
1686 else
1687 return false;
1688}
1689
1690static bool warning_is_error(int severity)
1691{
1692 if (is_valid_warning(severity))
1693 return !!(warning_state[WARN_IDX(severity)] & WARN_ST_ERROR);
H. Peter Anvin442a05a2012-02-24 21:50:53 -08001694 else
1695 return false;
Ed Berosetfa771012002-06-09 20:56:40 +00001696}
1697
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001698static bool skip_this_pass(int severity)
1699{
H. Peter Anvin8f622462017-04-02 19:02:29 -07001700 /*
1701 * See if it's a pass-specific error or warning which should be skipped.
1702 * We cannot skip errors stronger than ERR_NONFATAL as by definition
1703 * they cannot be resumed from.
1704 */
1705 if ((severity & ERR_MASK) > ERR_NONFATAL)
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001706 return false;
1707
H. Peter Anvin934f0472016-05-09 12:00:19 -07001708 /*
1709 * passn is 1 on the very first pass only.
1710 * pass0 is 2 on the code-generation (final) pass only.
1711 * These are the passes we care about in this case.
1712 */
1713 return (((severity & ERR_PASS1) && passn != 1) ||
1714 ((severity & ERR_PASS2) && pass0 != 2));
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001715}
1716
Ed Berosetfa771012002-06-09 20:56:40 +00001717/**
1718 * common error reporting
1719 * This is the common back end of the error reporting schemes currently
H. Peter Anvin70653092007-10-19 14:42:29 -07001720 * implemented. It prints the nature of the warning and then the
Ed Berosetfa771012002-06-09 20:56:40 +00001721 * specific error message to error_file and may or may not return. It
1722 * doesn't return if the error severity is a "panic" or "debug" type.
H. Peter Anvin70653092007-10-19 14:42:29 -07001723 *
1724 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001725 * @param fmt the printf style format string
1726 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001727static void nasm_verror_common(int severity, const char *fmt, va_list args)
Ed Berosetfa771012002-06-09 20:56:40 +00001728{
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001729 char msg[1024];
1730 const char *pfx;
H. Peter Anvin323fcff2009-07-12 12:04:56 -07001731
H. Peter Anvin7df04172008-06-10 18:27:38 -07001732 switch (severity & (ERR_MASK|ERR_NO_SEVERITY)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001733 case ERR_WARNING:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001734 pfx = "warning: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001735 break;
1736 case ERR_NONFATAL:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001737 pfx = "error: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001738 break;
1739 case ERR_FATAL:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001740 pfx = "fatal: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001741 break;
1742 case ERR_PANIC:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001743 pfx = "panic: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001744 break;
1745 case ERR_DEBUG:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001746 pfx = "debug: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001747 break;
H. Peter Anvin7df04172008-06-10 18:27:38 -07001748 default:
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001749 pfx = "";
1750 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001751 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00001752
H. Peter Anvin934f0472016-05-09 12:00:19 -07001753 vsnprintf(msg, sizeof msg - 64, fmt, args);
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001754 if (is_valid_warning(severity) && WARN_IDX(severity) != ERR_WARN_OTHER) {
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001755 char *p = strchr(msg, '\0');
H. Peter Anvin934f0472016-05-09 12:00:19 -07001756 snprintf(p, 64, " [-w+%s]", warnings[WARN_IDX(severity)].name);
1757 }
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001758
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001759 if (!skip_this_pass(severity))
1760 fprintf(error_file, "%s%s\n", pfx, msg);
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001761
H. Peter Anvin4def1a82016-05-09 13:59:44 -07001762 /* Are we recursing from error_list_macros? */
1763 if (severity & ERR_PP_LISTMACRO)
1764 return;
1765
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001766 /*
1767 * Don't suppress this with skip_this_pass(), or we don't get
H. Peter Anvin934f0472016-05-09 12:00:19 -07001768 * pass1 or preprocessor warnings in the list file
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001769 */
H. Peter Anvin0fcb4882016-07-06 11:55:25 -07001770 lfmt->error(severity, pfx, msg);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001771
H. Peter Anvin8f622462017-04-02 19:02:29 -07001772 if (skip_this_pass(severity))
1773 return;
1774
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001775 if (severity & ERR_USAGE)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001776 want_usage = true;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001777
H. Peter Anvin4def1a82016-05-09 13:59:44 -07001778 preproc->error_list_macros(severity);
1779
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001780 switch (severity & ERR_MASK) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001781 case ERR_DEBUG:
1782 /* no further action, by definition */
1783 break;
H. Peter Anvinb030c922007-11-13 11:31:15 -08001784 case ERR_WARNING:
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001785 /* Treat warnings as errors */
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001786 if (warning_is_error(severity))
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001787 terminate_after_phase = true;
1788 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001789 case ERR_NONFATAL:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001790 terminate_after_phase = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001791 break;
1792 case ERR_FATAL:
1793 if (ofile) {
1794 fclose(ofile);
1795 remove(outname);
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001796 ofile = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001797 }
1798 if (want_usage)
1799 usage();
1800 exit(1); /* instantly die */
1801 break; /* placate silly compilers */
1802 case ERR_PANIC:
1803 fflush(NULL);
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001804 /* abort(); */ /* halt, catch fire, and dump core */
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001805 if (ofile) {
1806 fclose(ofile);
1807 remove(outname);
1808 ofile = NULL;
1809 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001810 exit(3);
1811 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001812 }
1813}
1814
H. Peter Anvin734b1882002-04-30 21:01:08 +00001815static void usage(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001816{
H. Peter Anvin620515a2002-04-30 20:57:38 +00001817 fputs("type `nasm -h' for help\n", error_file);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001818}