blob: 6c252a3f7c6f0ac83b070baaf51951db437a1664 [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 Anvin55340992012-09-09 17:09:00 -0700144static char *quote_for_make(const char *str);
145
Cyrill Gorcunov190232f2013-02-15 12:35:04 +0400146static int64_t get_curr_offs(void)
147{
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800148 return in_absolute ? absolute.offset : raa_read(offsets, location.segment);
Cyrill Gorcunov190232f2013-02-15 12:35:04 +0400149}
150
151static void set_curr_offs(int64_t l_off)
152{
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800153 if (in_absolute)
154 absolute.offset = l_off;
Cyrill Gorcunov190232f2013-02-15 12:35:04 +0400155 else
156 offsets = raa_write(offsets, location.segment, l_off);
157}
158
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000159static void nasm_fputs(const char *line, FILE * outfile)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000160{
H. Peter Anvin310b3e12002-05-14 22:38:55 +0000161 if (outfile) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000162 fputs(line, outfile);
H. Peter Anvind1fb15c2007-11-13 09:37:59 -0800163 putc('\n', outfile);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000164 } else
H. Peter Anvine2c80182005-01-15 22:15:51 +0000165 puts(line);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000166}
167
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800168static void define_macros_early(void)
169{
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700170 const struct compile_time * const oct = &official_compile_time;
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800171 char temp[128];
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800172
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700173 if (oct->have_local) {
174 strftime(temp, sizeof temp, "__DATE__=\"%Y-%m-%d\"", &oct->local);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400175 preproc->pre_define(temp);
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700176 strftime(temp, sizeof temp, "__DATE_NUM__=%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, "__TIME__=\"%H:%M:%S\"", &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_NUM__=%H%M%S", &oct->local);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400181 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800182 }
183
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700184 if (oct->have_gm) {
185 strftime(temp, sizeof temp, "__UTC_DATE__=\"%Y-%m-%d\"", &oct->gm);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400186 preproc->pre_define(temp);
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700187 strftime(temp, sizeof temp, "__UTC_DATE_NUM__=%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_TIME__=\"%H:%M:%S\"", &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_NUM__=%H%M%S", &oct->gm);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400192 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800193 }
H. Peter Anvind85d2502008-05-04 17:53:31 -0700194
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700195 if (oct->have_posix) {
196 snprintf(temp, sizeof temp, "__POSIX_TIME__=%"PRId64, oct->posix);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400197 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800198 }
199}
200
201static void define_macros_late(void)
202{
203 char temp[128];
204
Cyrill Gorcunovc1936da2011-04-06 18:32:15 +0400205 /*
206 * In case if output format is defined by alias
207 * we have to put shortname of the alias itself here
208 * otherwise ABI backward compatibility gets broken.
209 */
Cyrill Gorcunov69ce7502010-07-12 15:19:17 +0400210 snprintf(temp, sizeof(temp), "__OUTPUT_FORMAT__=%s",
Cyrill Gorcunovc1936da2011-04-06 18:32:15 +0400211 ofmt_alias ? ofmt_alias->shortname : ofmt->shortname);
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +0400212 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800213}
214
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700215static void emit_dependencies(StrList *list)
216{
217 FILE *deps;
218 int linepos, len;
219 StrList *l, *nl;
220
221 if (depend_file && strcmp(depend_file, "-")) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -0700222 deps = nasm_open_write(depend_file, NF_TEXT);
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400223 if (!deps) {
224 nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
225 "unable to write dependency file `%s'", depend_file);
226 return;
227 }
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700228 } else {
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400229 deps = stdout;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700230 }
H. Peter Anvin323fcff2009-07-12 12:04:56 -0700231
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700232 linepos = fprintf(deps, "%s:", depend_target);
Cyrill Gorcunovfcd0a742009-07-27 16:26:26 +0400233 list_for_each(l, list) {
H. Peter Anvin55340992012-09-09 17:09:00 -0700234 char *file = quote_for_make(l->str);
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400235 len = strlen(file);
236 if (linepos + len > 62 && linepos > 1) {
237 fprintf(deps, " \\\n ");
238 linepos = 1;
239 }
240 fprintf(deps, " %s", file);
241 linepos += len+1;
H. Peter Anvin55340992012-09-09 17:09:00 -0700242 nasm_free(file);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700243 }
244 fprintf(deps, "\n\n");
H. Peter Anvin323fcff2009-07-12 12:04:56 -0700245
Cyrill Gorcunovfcd0a742009-07-27 16:26:26 +0400246 list_for_each_safe(l, nl, list) {
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400247 if (depend_emit_phony)
248 fprintf(deps, "%s:\n\n", l->str);
249 nasm_free(l);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700250 }
251
252 if (deps != stdout)
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400253 fclose(deps);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700254}
255
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700256/* Convert a struct tm to a POSIX-style time constant */
257static int64_t make_posix_time(const struct tm *tm)
258{
259 int64_t t;
260 int64_t y = tm->tm_year;
261
262 /* See IEEE 1003.1:2004, section 4.14 */
263
264 t = (y-70)*365 + (y-69)/4 - (y-1)/100 + (y+299)/400;
265 t += tm->tm_yday;
266 t *= 24;
267 t += tm->tm_hour;
268 t *= 60;
269 t += tm->tm_min;
270 t *= 60;
271 t += tm->tm_sec;
272
273 return t;
274}
275
276static void timestamp(void)
277{
278 struct compile_time * const oct = &official_compile_time;
279 const struct tm *tp, *best_gm;
280
281 time(&oct->t);
282
283 best_gm = NULL;
284
285 tp = localtime(&oct->t);
286 if (tp) {
287 oct->local = *tp;
288 best_gm = &oct->local;
289 oct->have_local = true;
290 }
291
292 tp = gmtime(&oct->t);
293 if (tp) {
294 oct->gm = *tp;
295 best_gm = &oct->gm;
296 oct->have_gm = true;
297 if (!oct->have_local)
298 oct->local = oct->gm;
299 } else {
300 oct->gm = oct->local;
301 }
302
303 if (best_gm) {
304 oct->posix = make_posix_time(best_gm);
305 oct->have_posix = true;
306 }
307}
308
H. Peter Anvin038d8612007-04-12 16:54:50 +0000309int main(int argc, char **argv)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000310{
H. Peter Anvina771be82017-08-16 14:53:18 -0700311 StrList **depend_ptr;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700312
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700313 timestamp();
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800314
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400315 iflag_set(&cpu, IF_PLEVEL);
316 iflag_set(&cmd_cpu, IF_PLEVEL);
317
Charles Crayne2581c862008-09-10 19:21:52 -0700318 pass0 = 0;
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700319 want_usage = terminate_after_phase = false;
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400320 nasm_set_verror(nasm_verror_gnu);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000321
H. Peter Anvin97e15752007-09-25 08:47:47 -0700322 error_file = stderr;
323
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -0700324 tolower_init();
H. Peter Anvin274cda82016-05-10 02:56:29 -0700325 src_init();
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -0700326
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000327 offsets = raa_init();
Keith Kaniosb7a89542007-04-12 02:40:54 +0000328 forwrefs = saa_init((int32_t)sizeof(struct forwrefinfo));
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000329
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000330 preproc = &nasmpp;
Cyrill Gorcunov3ed32cb2014-06-22 20:53:12 +0400331 operating_mode = OP_NORMAL;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000332
H. Peter Anvin55568c12016-10-03 19:46:49 -0700333 parse_cmdline(argc, argv, 1);
334 if (terminate_after_phase) {
335 if (want_usage)
336 usage();
337 return 1;
338 }
339
H. Peter Anvin169ac7c2016-09-25 17:08:05 -0700340 /*
341 * Define some macros dependent on the runtime, but not
H. Peter Anvin55568c12016-10-03 19:46:49 -0700342 * on the command line (as those are scanned in cmdline pass 2.)
H. Peter Anvin169ac7c2016-09-25 17:08:05 -0700343 */
344 preproc->init();
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800345 define_macros_early();
346
H. Peter Anvin55568c12016-10-03 19:46:49 -0700347 parse_cmdline(argc, argv, 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000348 if (terminate_after_phase) {
349 if (want_usage)
350 usage();
351 return 1;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000352 }
353
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800354 /* Save away the default state of warnings */
355 memcpy(warning_state_init, warning_state, sizeof warning_state);
356
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800357 if (!using_debug_info) {
358 /* No debug info, redirect to the null backend (empty stubs) */
H. Peter Anvina7bc15d2016-02-17 20:55:08 -0800359 dfmt = &null_debug_form;
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800360 } else if (!debug_format) {
361 /* Default debug format for this backend */
H. Peter Anvina7bc15d2016-02-17 20:55:08 -0800362 dfmt = ofmt->default_dfmt;
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800363 } else {
364 dfmt = dfmt_find(ofmt, debug_format);
365 if (!dfmt) {
366 nasm_fatal(ERR_NOFILE | ERR_USAGE,
367 "unrecognized debug format `%s' for"
368 " output format `%s'",
369 debug_format, ofmt->shortname);
370 }
371 }
H. Peter Anvinbb88d012003-09-10 23:34:23 +0000372
H. Peter Anvin76690a12002-04-30 20:52:49 +0000373 if (ofmt->stdmac)
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +0400374 preproc->extra_stdmac(ofmt->stdmac);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000375
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000376 /* define some macros dependent of command-line */
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800377 define_macros_late();
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000378
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400379 depend_ptr = (depend_file || (operating_mode & OP_DEPEND)) ? &depend_list : NULL;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700380 if (!depend_target)
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400381 depend_target = quote_for_make(outname);
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700382
Cyrill Gorcunove9fc88c2014-06-23 02:22:02 +0400383 if (operating_mode & OP_DEPEND) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000384 char *line;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700385
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400386 if (depend_missing_ok)
387 preproc->include_path(NULL); /* "assume generated" */
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700388
H. Peter Anvin130736c2016-02-17 20:27:41 -0800389 preproc->reset(inname, 0, depend_ptr);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000390 if (outname[0] == '\0')
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400391 ofmt->filename(inname, outname);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000392 ofile = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000393 while ((line = preproc->getline()))
394 nasm_free(line);
395 preproc->cleanup(0);
Cyrill Gorcunove9fc88c2014-06-23 02:22:02 +0400396 } else if (operating_mode & OP_PREPROCESS) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000397 char *line;
H. Peter Anvin274cda82016-05-10 02:56:29 -0700398 const char *file_name = NULL;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000399 int32_t prior_linnum = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000400 int lineinc = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000401
H. Peter Anvine2c80182005-01-15 22:15:51 +0000402 if (*outname) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -0700403 ofile = nasm_open_write(outname, NF_TEXT);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000404 if (!ofile)
H. Peter Anvin41087062016-03-03 14:27:34 -0800405 nasm_fatal(ERR_NOFILE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000406 "unable to open output file `%s'",
407 outname);
408 } else
409 ofile = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000410
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700411 location.known = false;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000412
Cyrill Gorcunovb574b072011-12-05 01:56:40 +0400413 /* pass = 1; */
H. Peter Anvin130736c2016-02-17 20:27:41 -0800414 preproc->reset(inname, 3, depend_ptr);
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800415
416 /* Revert all warnings to the default state */
417 memcpy(warning_state, warning_state_init, sizeof warning_state);
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700418
H. Peter Anvine2c80182005-01-15 22:15:51 +0000419 while ((line = preproc->getline())) {
420 /*
421 * We generate %line directives if needed for later programs
422 */
Keith Kaniosb7a89542007-04-12 02:40:54 +0000423 int32_t linnum = prior_linnum += lineinc;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000424 int altline = src_get(&linnum, &file_name);
425 if (altline) {
426 if (altline == 1 && lineinc == 1)
427 nasm_fputs("", ofile);
428 else {
429 lineinc = (altline != -1 || lineinc != 1);
430 fprintf(ofile ? ofile : stdout,
Keith Kanios93f2e9a2007-04-14 00:10:59 +0000431 "%%line %"PRId32"+%d %s\n", linnum, lineinc,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000432 file_name);
433 }
434 prior_linnum = linnum;
435 }
436 nasm_fputs(line, ofile);
437 nasm_free(line);
438 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000439 preproc->cleanup(0);
440 if (ofile)
441 fclose(ofile);
442 if (ofile && terminate_after_phase)
443 remove(outname);
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400444 ofile = NULL;
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400445 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000446
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400447 if (operating_mode & OP_NORMAL) {
448 /*
449 * We must call ofmt->filename _anyway_, even if the user
450 * has specified their own output file, because some
451 * formats (eg OBJ and COFF) use ofmt->filename to find out
452 * the name of the input file and then put that inside the
453 * file.
454 */
455 ofmt->filename(inname, outname);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000456
H. Peter Anvin3e83cec2016-05-25 04:28:46 -0700457 ofile = nasm_open_write(outname, (ofmt->flags & OFMT_TEXT) ? NF_TEXT : NF_BINARY);
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400458 if (!ofile)
H. Peter Anvin41087062016-03-03 14:27:34 -0800459 nasm_fatal(ERR_NOFILE,
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400460 "unable to open output file `%s'", outname);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000461
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400462 /*
463 * We must call init_labels() before ofmt->init() since
464 * some object formats will want to define labels in their
465 * init routines. (eg OS/2 defines the FLAT group)
466 */
467 init_labels();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000468
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400469 ofmt->init();
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400470 dfmt->init();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000471
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400472 assemble_file(inname, depend_ptr);
Victor van den Elzenc82c3722008-06-04 15:24:20 +0200473
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400474 if (!terminate_after_phase) {
H. Peter Anvin477ae442016-03-07 22:53:06 -0800475 ofmt->cleanup();
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400476 cleanup_labels();
477 fflush(ofile);
H. Peter Anvin274cda82016-05-10 02:56:29 -0700478 if (ferror(ofile)) {
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400479 nasm_error(ERR_NONFATAL|ERR_NOFILE,
480 "write error on output file `%s'", outname);
H. Peter Anvin274cda82016-05-10 02:56:29 -0700481 terminate_after_phase = true;
482 }
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400483 }
484
485 if (ofile) {
486 fclose(ofile);
487 if (terminate_after_phase)
488 remove(outname);
489 ofile = NULL;
490 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000491 }
H. Peter Anvin734b1882002-04-30 21:01:08 +0000492
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700493 if (depend_list && !terminate_after_phase)
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400494 emit_dependencies(depend_list);
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700495
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000496 if (want_usage)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000497 usage();
H. Peter Anvin734b1882002-04-30 21:01:08 +0000498
H. Peter Anvine2c80182005-01-15 22:15:51 +0000499 raa_free(offsets);
500 saa_free(forwrefs);
501 eval_cleanup();
H. Peter Anvin74cc5e52007-08-30 22:35:34 +0000502 stdscan_cleanup();
H. Peter Anvin274cda82016-05-10 02:56:29 -0700503 src_free();
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000504
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700505 return terminate_after_phase;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000506}
507
H. Peter Anvineba20a72002-04-30 20:53:55 +0000508/*
509 * Get a parameter for a command line option.
510 * First arg must be in the form of e.g. -f...
511 */
H. Peter Anvin423e3812007-11-15 17:12:29 -0800512static char *get_param(char *p, char *q, bool *advance)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000513{
H. Peter Anvin423e3812007-11-15 17:12:29 -0800514 *advance = false;
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +0400515 if (p[2]) /* the parameter's in the option */
516 return nasm_skip_spaces(p + 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000517 if (q && q[0]) {
H. Peter Anvin423e3812007-11-15 17:12:29 -0800518 *advance = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000519 return q;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000520 }
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400521 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000522 "option `-%c' requires an argument", p[1]);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000523 return NULL;
524}
525
H. Peter Anvindc242712007-11-18 11:55:10 -0800526/*
527 * Copy a filename
528 */
529static void copy_filename(char *dst, const char *src)
530{
531 size_t len = strlen(src);
532
533 if (len >= (size_t)FILENAME_MAX) {
H. Peter Anvin41087062016-03-03 14:27:34 -0800534 nasm_fatal(ERR_NOFILE, "file name too long");
Cyrill Gorcunov45aa1182013-02-15 12:22:59 +0400535 return;
H. Peter Anvindc242712007-11-18 11:55:10 -0800536 }
537 strncpy(dst, src, FILENAME_MAX);
538}
539
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700540/*
541 * Convert a string to Make-safe form
542 */
543static char *quote_for_make(const char *str)
544{
545 const char *p;
546 char *os, *q;
547
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400548 size_t n = 1; /* Terminating zero */
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700549 size_t nbs = 0;
550
551 if (!str)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400552 return NULL;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700553
554 for (p = str; *p; p++) {
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400555 switch (*p) {
556 case ' ':
557 case '\t':
558 /* Convert N backslashes + ws -> 2N+1 backslashes + ws */
559 n += nbs + 2;
560 nbs = 0;
561 break;
562 case '$':
563 case '#':
564 nbs = 0;
565 n += 2;
566 break;
567 case '\\':
568 nbs++;
569 n++;
570 break;
571 default:
572 nbs = 0;
573 n++;
574 break;
575 }
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700576 }
577
578 /* Convert N backslashes at the end of filename to 2N backslashes */
579 if (nbs)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400580 n += nbs;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700581
582 os = q = nasm_malloc(n);
583
584 nbs = 0;
585 for (p = str; *p; p++) {
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400586 switch (*p) {
587 case ' ':
588 case '\t':
589 while (nbs--)
590 *q++ = '\\';
591 *q++ = '\\';
592 *q++ = *p;
593 break;
594 case '$':
595 *q++ = *p;
596 *q++ = *p;
597 nbs = 0;
598 break;
599 case '#':
600 *q++ = '\\';
601 *q++ = *p;
602 nbs = 0;
603 break;
604 case '\\':
605 *q++ = *p;
606 nbs++;
607 break;
608 default:
609 *q++ = *p;
610 nbs = 0;
611 break;
612 }
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700613 }
614 while (nbs--)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400615 *q++ = '\\';
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700616
617 *q = '\0';
618
619 return os;
620}
621
H. Peter Anvine2c80182005-01-15 22:15:51 +0000622struct textargs {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000623 const char *label;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000624 int value;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000625};
626
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100627enum text_options {
628 OPT_PREFIX,
H. Peter Anvin7214d182016-03-01 22:43:51 -0800629 OPT_POSTFIX
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100630};
H. Peter Anvin2530a102016-02-18 02:28:15 -0800631static const struct textargs textopts[] = {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000632 {"prefix", OPT_PREFIX},
633 {"postfix", OPT_POSTFIX},
634 {NULL, 0}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000635};
636
Cyrill Gorcunove7438432014-05-09 22:34:34 +0400637static void show_version(void)
638{
639 printf("NASM version %s compiled on %s%s\n",
640 nasm_version, nasm_date, nasm_compile_options);
641 exit(0);
642}
643
H. Peter Anvin423e3812007-11-15 17:12:29 -0800644static bool stopoptions = false;
H. Peter Anvin55568c12016-10-03 19:46:49 -0700645static bool process_arg(char *p, char *q, int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000646{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000647 char *param;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800648 int i;
649 bool advance = false;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000650
651 if (!p || !p[0])
H. Peter Anvin423e3812007-11-15 17:12:29 -0800652 return false;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000653
H. Peter Anvine2c80182005-01-15 22:15:51 +0000654 if (p[0] == '-' && !stopoptions) {
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400655 if (strchr("oOfpPdDiIlFXuUZwW", p[1])) {
656 /* These parameters take values */
657 if (!(param = get_param(p, q, &advance)))
658 return advance;
659 }
H. Peter Anvin423e3812007-11-15 17:12:29 -0800660
H. Peter Anvine2c80182005-01-15 22:15:51 +0000661 switch (p[1]) {
662 case 's':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700663 if (pass == 1)
664 error_file = stdout;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000665 break;
H. Peter Anvin70653092007-10-19 14:42:29 -0700666
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400667 case 'o': /* output file */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700668 if (pass == 2)
669 copy_filename(outname, param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400670 break;
H. Peter Anvinfd7dd112007-10-10 14:06:59 -0700671
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400672 case 'f': /* output format */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700673 if (pass == 1) {
674 ofmt = ofmt_find(param, &ofmt_alias);
675 if (!ofmt) {
676 nasm_fatal(ERR_NOFILE | ERR_USAGE,
677 "unrecognised output format `%s' - "
678 "use -hf for a list", param);
679 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400680 }
681 break;
H. Peter Anvin70653092007-10-19 14:42:29 -0700682
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400683 case 'O': /* Optimization level */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700684 if (pass == 2) {
685 int opt;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700686
H. Peter Anvin55568c12016-10-03 19:46:49 -0700687 if (!*param) {
688 /* Naked -O == -Ox */
689 optimizing = MAX_OPTIMIZE;
690 } else {
691 while (*param) {
692 switch (*param) {
693 case '0': case '1': case '2': case '3': case '4':
694 case '5': case '6': case '7': case '8': case '9':
695 opt = strtoul(param, &param, 10);
H. Peter Anvind85d2502008-05-04 17:53:31 -0700696
H. Peter Anvin55568c12016-10-03 19:46:49 -0700697 /* -O0 -> optimizing == -1, 0.98 behaviour */
698 /* -O1 -> optimizing == 0, 0.98.09 behaviour */
699 if (opt < 2)
700 optimizing = opt - 1;
701 else
702 optimizing = opt;
703 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700704
H. Peter Anvin55568c12016-10-03 19:46:49 -0700705 case 'v':
706 case '+':
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400707 param++;
708 opt_verbose_info = true;
709 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700710
H. Peter Anvin55568c12016-10-03 19:46:49 -0700711 case 'x':
712 param++;
713 optimizing = MAX_OPTIMIZE;
714 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700715
H. Peter Anvin55568c12016-10-03 19:46:49 -0700716 default:
717 nasm_fatal(0,
718 "unknown optimization option -O%c\n",
719 *param);
720 break;
721 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400722 }
H. Peter Anvin55568c12016-10-03 19:46:49 -0700723 if (optimizing > MAX_OPTIMIZE)
724 optimizing = MAX_OPTIMIZE;
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400725 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400726 }
727 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800728
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400729 case 'p': /* pre-include */
730 case 'P':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700731 if (pass == 2)
732 preproc->pre_include(param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400733 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800734
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400735 case 'd': /* pre-define */
736 case 'D':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700737 if (pass == 2)
738 preproc->pre_define(param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400739 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800740
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400741 case 'u': /* un-define */
742 case 'U':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700743 if (pass == 2)
744 preproc->pre_undefine(param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400745 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800746
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400747 case 'i': /* include search path */
748 case 'I':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700749 if (pass == 2)
750 preproc->include_path(param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400751 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800752
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400753 case 'l': /* listing file */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700754 if (pass == 2)
755 copy_filename(listname, param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400756 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800757
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400758 case 'Z': /* error messages file */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700759 if (pass == 1)
760 copy_filename(errname, param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400761 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800762
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400763 case 'F': /* specify debug format */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700764 if (pass == 2) {
765 using_debug_info = true;
766 debug_format = param;
767 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400768 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800769
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400770 case 'X': /* specify error reporting format */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700771 if (pass == 1) {
772 if (nasm_stricmp("vc", param) == 0)
773 nasm_set_verror(nasm_verror_vc);
774 else if (nasm_stricmp("gnu", param) == 0)
775 nasm_set_verror(nasm_verror_gnu);
776 else
777 nasm_fatal(ERR_NOFILE | ERR_USAGE,
778 "unrecognized error reporting format `%s'",
779 param);
780 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000781 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800782
H. Peter Anvine2c80182005-01-15 22:15:51 +0000783 case 'g':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700784 if (pass == 2) {
785 using_debug_info = true;
786 if (p[2])
787 debug_format = nasm_skip_spaces(p + 2);
788 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000789 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800790
H. Peter Anvine2c80182005-01-15 22:15:51 +0000791 case 'h':
792 printf
793 ("usage: nasm [-@ response file] [-o outfile] [-f format] "
794 "[-l listfile]\n"
795 " [options...] [--] filename\n"
Cyrill Gorcunove7438432014-05-09 22:34:34 +0400796 " or nasm -v (or --v) for version info\n\n"
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800797 " -t assemble in SciTech TASM compatible mode\n");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000798 printf
H. Peter Anvin413fb902007-09-26 15:19:28 -0700799 (" -E (or -e) preprocess only (writes output to stdout by default)\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000800 " -a don't preprocess (assemble only)\n"
H. Peter Anvin37a321f2007-09-24 13:41:58 -0700801 " -M generate Makefile dependencies on stdout\n"
Cyrill Gorcunov984c4db2009-07-31 00:32:17 +0400802 " -MG d:o, missing files assumed generated\n"
803 " -MF <file> set Makefile dependency file\n"
804 " -MD <file> assemble and generate dependencies\n"
805 " -MT <file> dependency target name\n"
806 " -MQ <file> dependency target name (quoted)\n"
807 " -MP emit phony target\n\n"
H. Peter Anvin413fb902007-09-26 15:19:28 -0700808 " -Z<file> redirect error messages to file\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000809 " -s redirect error messages to stdout\n\n"
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800810 " -g generate debugging information\n\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000811 " -F format select a debugging format\n\n"
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800812 " -gformat same as -g -F format\n\n"
Cyrill Gorcunovdeb082d2013-04-20 20:37:17 +0400813 " -o outfile write output to an outfile\n\n"
814 " -f format select an output format\n\n"
815 " -l listfile write listing to a listfile\n\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000816 " -I<path> adds a pathname to the include file path\n");
817 printf
H. Peter Anvinab5bd052010-07-25 12:43:30 -0700818 (" -O<digit> optimize branch offsets\n"
Cyrill Gorcunov5bc6d8e2012-03-11 14:19:17 +0400819 " -O0: No optimization\n"
Cyrill Gorcunov73b87c62009-07-31 10:26:55 +0400820 " -O1: Minimal optimization\n"
Cyrill Gorcunov5bc6d8e2012-03-11 14:19:17 +0400821 " -Ox: Multipass optimization (default)\n\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000822 " -P<file> pre-includes a file\n"
823 " -D<macro>[=<value>] pre-defines a macro\n"
824 " -U<macro> undefines a macro\n"
825 " -X<format> specifies error reporting format (gnu or vc)\n"
H. Peter Anvinb030c922007-11-13 11:31:15 -0800826 " -w+foo enables warning foo (equiv. -Wfoo)\n"
Cyrill Gorcunov984c4db2009-07-31 00:32:17 +0400827 " -w-foo disable warning foo (equiv. -Wno-foo)\n\n"
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800828 " -w[+-]error[=foo] can be used to promote warnings to errors\n"
829 " -h show invocation summary and exit\n\n"
Cyrill Gorcunov984c4db2009-07-31 00:32:17 +0400830 "--prefix,--postfix\n"
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800831 " these options prepend or append the given string\n"
832 " to all extern and global variables\n"
833 "\n"
834 "Response files should contain command line parameters,\n"
835 "one per line.\n"
836 "\n"
837 "Warnings for the -W/-w options:\n");
838 for (i = 0; i <= ERR_WARN_ALL; i++)
839 printf(" %-23s %s%s\n",
H. Peter Anvin972079f2008-09-30 17:01:23 -0700840 warnings[i].name, warnings[i].help,
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800841 i == ERR_WARN_ALL ? "\n" :
842 warnings[i].enabled ? " (default on)" :
843 " (default off)");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000844 if (p[2] == 'f') {
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800845 printf("valid output formats for -f are"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000846 " (`*' denotes default):\n");
847 ofmt_list(ofmt, stdout);
848 } else {
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800849 printf("For a list of valid output formats, use -hf.\n");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000850 printf("For a list of debug formats, use -f <form> -y.\n");
851 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400852 exit(0); /* never need usage message here */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000853 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800854
H. Peter Anvine2c80182005-01-15 22:15:51 +0000855 case 'y':
856 printf("\nvalid debug formats for '%s' output format are"
857 " ('*' denotes default):\n", ofmt->shortname);
858 dfmt_list(ofmt, stdout);
859 exit(0);
860 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800861
H. Peter Anvine2c80182005-01-15 22:15:51 +0000862 case 't':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700863 if (pass == 2)
864 tasm_compatible_mode = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000865 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800866
H. Peter Anvine2c80182005-01-15 22:15:51 +0000867 case 'v':
Cyrill Gorcunove7438432014-05-09 22:34:34 +0400868 show_version();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000869 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800870
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400871 case 'e': /* preprocess only */
872 case 'E':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700873 if (pass == 1)
874 operating_mode = OP_PREPROCESS;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000875 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800876
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400877 case 'a': /* assemble only - don't preprocess */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700878 if (pass == 1)
879 preproc = &preproc_nop;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000880 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800881
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800882 case 'w':
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400883 case 'W':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700884 if (pass == 2) {
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800885 if (!set_warning_status(param)) {
886 nasm_error(ERR_WARNING|ERR_NOFILE|ERR_WARN_UNK_WARNING,
887 "unknown warning option: %s", param);
H. Peter Anvin55568c12016-10-03 19:46:49 -0700888 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400889 }
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800890 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800891
H. Peter Anvine2c80182005-01-15 22:15:51 +0000892 case 'M':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700893 if (pass == 2) {
894 switch (p[2]) {
895 case 0:
896 operating_mode = OP_DEPEND;
897 break;
898 case 'G':
899 operating_mode = OP_DEPEND;
900 depend_missing_ok = true;
901 break;
902 case 'P':
903 depend_emit_phony = true;
904 break;
905 case 'D':
906 operating_mode = OP_NORMAL;
907 depend_file = q;
908 advance = true;
909 break;
910 case 'F':
911 depend_file = q;
912 advance = true;
913 break;
914 case 'T':
915 depend_target = q;
916 advance = true;
917 break;
918 case 'Q':
919 depend_target = quote_for_make(q);
920 advance = true;
921 break;
922 default:
923 nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
924 "unknown dependency option `-M%c'", p[2]);
925 break;
926 }
927 if (advance && (!q || !q[0])) {
928 nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
929 "option `-M%c' requires a parameter", p[2]);
930 break;
931 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400932 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000933 break;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000934
H. Peter Anvine2c80182005-01-15 22:15:51 +0000935 case '-':
936 {
937 int s;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000938
H. Peter Anvine2c80182005-01-15 22:15:51 +0000939 if (p[2] == 0) { /* -- => stop processing options */
940 stopoptions = 1;
941 break;
942 }
Cyrill Gorcunove7438432014-05-09 22:34:34 +0400943
944 if (!nasm_stricmp(p, "--v"))
945 show_version();
946
Andy Willis3f546032016-09-13 00:02:21 +0300947 if (!nasm_stricmp(p, "--version"))
948 show_version();
949
H. Peter Anvine2c80182005-01-15 22:15:51 +0000950 for (s = 0; textopts[s].label; s++) {
951 if (!nasm_stricmp(p + 2, textopts[s].label)) {
952 break;
953 }
954 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000955
H. Peter Anvine2c80182005-01-15 22:15:51 +0000956 switch (s) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000957 case OPT_PREFIX:
958 case OPT_POSTFIX:
959 {
960 if (!q) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400961 nasm_error(ERR_NONFATAL | ERR_NOFILE |
H. Peter Anvine2c80182005-01-15 22:15:51 +0000962 ERR_USAGE,
963 "option `--%s' requires an argument",
964 p + 2);
965 break;
966 } else {
967 advance = 1, param = q;
968 }
969
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100970 switch (s) {
971 case OPT_PREFIX:
H. Peter Anvin55568c12016-10-03 19:46:49 -0700972 if (pass == 2)
973 strlcpy(lprefix, param, PREFIX_MAX);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000974 break;
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100975 case OPT_POSTFIX:
H. Peter Anvin55568c12016-10-03 19:46:49 -0700976 if (pass == 2)
977 strlcpy(lpostfix, param, POSTFIX_MAX);
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100978 break;
979 default:
H. Peter Anvinac061332017-03-31 11:41:16 -0700980 panic();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000981 break;
982 }
983 break;
984 }
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800985
H. Peter Anvine2c80182005-01-15 22:15:51 +0000986 default:
987 {
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400988 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000989 "unrecognised option `--%s'", p + 2);
990 break;
991 }
992 }
993 break;
994 }
995
996 default:
H. Peter Anvinac061332017-03-31 11:41:16 -0700997 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
998 "unrecognised option `-%c'", p[1]);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000999 break;
1000 }
H. Peter Anvin55568c12016-10-03 19:46:49 -07001001 } else if (pass == 2) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001002 if (*inname) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001003 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001004 "more than one input file specified");
H. Peter Anvindc242712007-11-18 11:55:10 -08001005 } else {
1006 copy_filename(inname, p);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001007 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001008 }
1009
1010 return advance;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001011}
1012
H. Peter Anvineba20a72002-04-30 20:53:55 +00001013#define ARG_BUF_DELTA 128
1014
H. Peter Anvin55568c12016-10-03 19:46:49 -07001015static void process_respfile(FILE * rfile, int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001016{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001017 char *buffer, *p, *q, *prevarg;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001018 int bufsize, prevargsize;
1019
1020 bufsize = prevargsize = ARG_BUF_DELTA;
1021 buffer = nasm_malloc(ARG_BUF_DELTA);
1022 prevarg = nasm_malloc(ARG_BUF_DELTA);
1023 prevarg[0] = '\0';
1024
H. Peter Anvine2c80182005-01-15 22:15:51 +00001025 while (1) { /* Loop to handle all lines in file */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001026 p = buffer;
1027 while (1) { /* Loop to handle long lines */
1028 q = fgets(p, bufsize - (p - buffer), rfile);
1029 if (!q)
1030 break;
1031 p += strlen(p);
1032 if (p > buffer && p[-1] == '\n')
1033 break;
1034 if (p - buffer > bufsize - 10) {
1035 int offset;
1036 offset = p - buffer;
1037 bufsize += ARG_BUF_DELTA;
1038 buffer = nasm_realloc(buffer, bufsize);
1039 p = buffer + offset;
1040 }
1041 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001042
H. Peter Anvine2c80182005-01-15 22:15:51 +00001043 if (!q && p == buffer) {
1044 if (prevarg[0])
H. Peter Anvin55568c12016-10-03 19:46:49 -07001045 process_arg(prevarg, NULL, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001046 nasm_free(buffer);
1047 nasm_free(prevarg);
1048 return;
1049 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001050
H. Peter Anvine2c80182005-01-15 22:15:51 +00001051 /*
1052 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
1053 * them are present at the end of the line.
1054 */
1055 *(p = &buffer[strcspn(buffer, "\r\n\032")]) = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001056
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001057 while (p > buffer && nasm_isspace(p[-1]))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001058 *--p = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001059
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +04001060 p = nasm_skip_spaces(buffer);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001061
H. Peter Anvin55568c12016-10-03 19:46:49 -07001062 if (process_arg(prevarg, p, pass))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001063 *p = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001064
Charles Crayne192d5b52007-10-18 19:02:42 -07001065 if ((int) strlen(p) > prevargsize - 10) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001066 prevargsize += ARG_BUF_DELTA;
1067 prevarg = nasm_realloc(prevarg, prevargsize);
1068 }
H. Peter Anvindc242712007-11-18 11:55:10 -08001069 strncpy(prevarg, p, prevargsize);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001070 }
1071}
1072
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001073/* Function to process args from a string of args, rather than the
1074 * argv array. Used by the environment variable and response file
1075 * processing.
1076 */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001077static void process_args(char *args, int pass)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001078{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001079 char *p, *q, *arg, *prevarg;
1080 char separator = ' ';
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001081
1082 p = args;
1083 if (*p && *p != '-')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001084 separator = *p++;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001085 arg = NULL;
1086 while (*p) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001087 q = p;
1088 while (*p && *p != separator)
1089 p++;
1090 while (*p == separator)
1091 *p++ = '\0';
1092 prevarg = arg;
1093 arg = q;
H. Peter Anvin55568c12016-10-03 19:46:49 -07001094 if (process_arg(prevarg, arg, pass))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001095 arg = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001096 }
1097 if (arg)
H. Peter Anvin55568c12016-10-03 19:46:49 -07001098 process_arg(arg, NULL, pass);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001099}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001100
H. Peter Anvin55568c12016-10-03 19:46:49 -07001101static void process_response_file(const char *file, int pass)
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001102{
1103 char str[2048];
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001104 FILE *f = nasm_open_read(file, NF_TEXT);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001105 if (!f) {
Cyrill Gorcunovf1964512013-02-15 12:14:06 +04001106 perror(file);
1107 exit(-1);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001108 }
1109 while (fgets(str, sizeof str, f)) {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001110 process_args(str, pass);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001111 }
1112 fclose(f);
1113}
1114
H. Peter Anvin55568c12016-10-03 19:46:49 -07001115static void parse_cmdline(int argc, char **argv, int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001116{
1117 FILE *rfile;
Cyrill Gorcunovf4941892011-07-17 13:55:25 +04001118 char *envreal, *envcopy = NULL, *p;
H. Peter Anvin972079f2008-09-30 17:01:23 -07001119 int i;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001120
Charles Craynefcce07f2007-09-30 22:15:36 -07001121 *inname = *outname = *listname = *errname = '\0';
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001122
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001123 /* Initialize all the warnings to their default state */
1124 for (i = 0; i < ERR_WARN_ALL; i++) {
1125 warning_state_init[i] = warning_state[i] =
1126 warnings[i].enabled ? WARN_ST_ENABLED : 0;
1127 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001128
1129 /*
H. Peter Anvinff7ccc02002-05-06 19:41:57 +00001130 * First, process the NASMENV environment variable.
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001131 */
H. Peter Anvinff7ccc02002-05-06 19:41:57 +00001132 envreal = getenv("NASMENV");
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001133 if (envreal) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001134 envcopy = nasm_strdup(envreal);
H. Peter Anvin55568c12016-10-03 19:46:49 -07001135 process_args(envcopy, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001136 nasm_free(envcopy);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001137 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001138
1139 /*
1140 * Now process the actual command line.
1141 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001142 while (--argc) {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001143 bool advance;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001144 argv++;
1145 if (argv[0][0] == '@') {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001146 /*
1147 * We have a response file, so process this as a set of
H. Peter Anvine2c80182005-01-15 22:15:51 +00001148 * arguments like the environment variable. This allows us
1149 * to have multiple arguments on a single line, which is
1150 * different to the -@resp file processing below for regular
1151 * NASM.
1152 */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001153 process_response_file(argv[0]+1, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001154 argc--;
1155 argv++;
1156 }
1157 if (!stopoptions && argv[0][0] == '-' && argv[0][1] == '@') {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001158 p = get_param(argv[0], argc > 1 ? argv[1] : NULL, &advance);
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001159 if (p) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001160 rfile = nasm_open_read(p, NF_TEXT);
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001161 if (rfile) {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001162 process_respfile(rfile, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001163 fclose(rfile);
1164 } else
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001165 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001166 "unable to open response file `%s'", p);
1167 }
1168 } else
H. Peter Anvin55568c12016-10-03 19:46:49 -07001169 advance = process_arg(argv[0], argc > 1 ? argv[1] : NULL, pass);
H. Peter Anvin423e3812007-11-15 17:12:29 -08001170 argv += advance, argc -= advance;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001171 }
1172
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001173 /*
1174 * Look for basic command line typos. This definitely doesn't
1175 * catch all errors, but it might help cases of fumbled fingers.
1176 */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001177 if (pass != 2)
1178 return;
1179
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001180 if (!*inname)
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001181 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001182 "no input file specified");
1183 else if (!strcmp(inname, errname) ||
1184 !strcmp(inname, outname) ||
1185 !strcmp(inname, listname) ||
1186 (depend_file && !strcmp(inname, depend_file)))
H. Peter Anvin41087062016-03-03 14:27:34 -08001187 nasm_fatal(ERR_NOFILE | ERR_USAGE,
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001188 "file `%s' is both input and output file",
1189 inname);
H. Peter Anvin59ddd262007-10-01 11:26:31 -07001190
H. Peter Anvin70653092007-10-19 14:42:29 -07001191 if (*errname) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001192 error_file = nasm_open_write(errname, NF_TEXT);
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001193 if (!error_file) {
1194 error_file = stderr; /* Revert to default! */
H. Peter Anvin41087062016-03-03 14:27:34 -08001195 nasm_fatal(ERR_NOFILE | ERR_USAGE,
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001196 "cannot open file `%s' for error messages",
1197 errname);
1198 }
Charles Craynefcce07f2007-09-30 22:15:36 -07001199 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001200}
1201
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001202static void assemble_file(char *fname, StrList **depend_ptr)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001203{
H. Peter Anvinc7131682017-03-07 17:45:01 -08001204 char *line;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001205 insn output_ins;
H. Peter Anvinc7131682017-03-07 17:45:01 -08001206 int i;
Charles Crayne5fbbc8c2007-11-07 19:03:46 -08001207 int64_t offs;
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001208 int pass_max;
H. Peter Anvin9c595b62017-03-07 19:44:21 -08001209 uint64_t prev_offset_changed;
1210 unsigned int stall_count = 0; /* Make sure we make forward progress... */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001211
Cyrill Gorcunov08359152013-11-09 22:16:11 +04001212 if (cmd_sb == 32 && iflag_ffs(&cmd_cpu) < IF_386)
H. Peter Anvin130736c2016-02-17 20:27:41 -08001213 nasm_fatal(0, "command line: 32-bit segment size requires a higher cpu");
H. Peter Anvineba20a72002-04-30 20:53:55 +00001214
Charles Craynec1905c22008-09-11 18:54:06 -07001215 pass_max = prev_offset_changed = (INT_MAX >> 1) + 2; /* Almost unlimited */
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001216 for (passn = 1; pass0 <= 2; passn++) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001217 ldfunc def_label;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001218
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001219 pass1 = pass0 == 2 ? 2 : 1; /* 1, 1, 1, ..., 1, 2 */
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001220 pass2 = passn > 1 ? 2 : 1; /* 1, 2, 2, ..., 2, 2 */
1221 /* pass0 0, 0, 0, ..., 1, 2 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00001222
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001223 def_label = passn > 1 ? redefine_label : define_label;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001224
H. Peter Anvincac0b192017-03-28 16:12:30 -07001225 globalbits = cmd_sb; /* set 'bits' to command line default */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001226 cpu = cmd_cpu;
1227 if (pass0 == 2) {
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08001228 lfmt->init(listname);
H. Peter Anvinaac01ff2017-04-02 19:10:26 -07001229 } else if (passn == 1 && *listname) {
1230 /* Remove the list file in case we die before the output pass */
1231 remove(listname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001232 }
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001233 in_absolute = false;
Charles Craynec1905c22008-09-11 18:54:06 -07001234 global_offset_changed = 0; /* set by redefine_label */
H. Peter Anvincac0b192017-03-28 16:12:30 -07001235 location.segment = ofmt->section(NULL, pass2, &globalbits);
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001236 if (passn > 1) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001237 saa_rewind(forwrefs);
1238 forwref = saa_rstruct(forwrefs);
1239 raa_free(offsets);
1240 offsets = raa_init();
1241 }
H. Peter Anvin130736c2016-02-17 20:27:41 -08001242 preproc->reset(fname, pass1, pass1 == 2 ? depend_ptr : NULL);
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001243
1244 /* Revert all warnings to the default state */
1245 memcpy(warning_state, warning_state_init, sizeof warning_state);
Victor van den Elzen819703a2008-07-16 15:20:56 +02001246
H. Peter Anvine2c80182005-01-15 22:15:51 +00001247 globallineno = 0;
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001248 if (passn == 1)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001249 location.known = true;
Cyrill Gorcunov190232f2013-02-15 12:35:04 +04001250 location.offset = offs = get_curr_offs();
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001251
H. Peter Anvine2c80182005-01-15 22:15:51 +00001252 while ((line = preproc->getline())) {
1253 globallineno++;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001254
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001255 /*
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001256 * Here we parse our directives; this is not handled by the
H. Peter Anvinc7131682017-03-07 17:45:01 -08001257 * main parser.
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001258 */
H. Peter Anvina6e26d92017-03-07 21:32:37 -08001259 if (process_directives(line))
H. Peter Anvinc7131682017-03-07 17:45:01 -08001260 goto end_of_line; /* Just do final cleanup */
H. Peter Anvin62b24d72007-08-29 16:38:05 +00001261
H. Peter Anvinc7131682017-03-07 17:45:01 -08001262 /* Not a directive, or even something that starts with [ */
1263
1264 parse_line(pass1, line, &output_ins, def_label);
1265
1266 if (optimizing > 0) {
1267 if (forwref != NULL && globallineno == forwref->lineno) {
1268 output_ins.forw_ref = true;
1269 do {
1270 output_ins.oprs[forwref->operand].opflags |= OPFLAG_FORWARD;
1271 forwref = saa_rstruct(forwrefs);
1272 } while (forwref != NULL
1273 && forwref->lineno == globallineno);
1274 } else
1275 output_ins.forw_ref = false;
1276
1277 if (output_ins.forw_ref) {
1278 if (passn == 1) {
1279 for (i = 0; i < output_ins.operands; i++) {
1280 if (output_ins.oprs[i].opflags & OPFLAG_FORWARD) {
1281 struct forwrefinfo *fwinf = (struct forwrefinfo *)saa_wstruct(forwrefs);
1282 fwinf->lineno = globallineno;
1283 fwinf->operand = i;
Cyrill Gorcunovd5f2aef2010-04-20 15:33:45 +04001284 }
1285 }
1286 }
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001287 }
H. Peter Anvinc7131682017-03-07 17:45:01 -08001288 }
H. Peter Anvin8bec2c72009-08-08 13:49:00 -07001289
H. Peter Anvinc7131682017-03-07 17:45:01 -08001290 /* forw_ref */
1291 if (output_ins.opcode == I_EQU) {
1292 if (pass1 == 1) {
1293 /*
1294 * Special `..' EQUs get processed in pass two,
1295 * except `..@' macro-processor EQUs which are done
1296 * in the normal place.
1297 */
1298 if (!output_ins.label)
1299 nasm_error(ERR_NONFATAL,
1300 "EQU not preceded by label");
1301
1302 else if (output_ins.label[0] != '.' ||
1303 output_ins.label[1] != '.' ||
1304 output_ins.label[2] == '@') {
1305 if (output_ins.operands == 1 &&
1306 (output_ins.oprs[0].type & IMMEDIATE) &&
1307 output_ins.oprs[0].wrt == NO_SEG) {
1308 bool isext = !!(output_ins.oprs[0].opflags & OPFLAG_EXTERN);
1309 def_label(output_ins.label,
1310 output_ins.oprs[0].segment,
1311 output_ins.oprs[0].offset, NULL,
1312 false, isext);
1313 } else if (output_ins.operands == 2
1314 && (output_ins.oprs[0].type & IMMEDIATE)
1315 && (output_ins.oprs[0].type & COLON)
1316 && output_ins.oprs[0].segment == NO_SEG
1317 && output_ins.oprs[0].wrt == NO_SEG
1318 && (output_ins.oprs[1].type & IMMEDIATE)
1319 && output_ins.oprs[1].segment == NO_SEG
1320 && output_ins.oprs[1].wrt == NO_SEG) {
1321 def_label(output_ins.label,
1322 output_ins.oprs[0].offset | SEG_ABS,
1323 output_ins.oprs[1].offset,
1324 NULL, false, false);
1325 } else
1326 nasm_error(ERR_NONFATAL,
1327 "bad syntax for EQU");
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001328 }
H. Peter Anvinc7131682017-03-07 17:45:01 -08001329 } else {
1330 /*
1331 * Special `..' EQUs get processed here, except
1332 * `..@' macro processor EQUs which are done above.
1333 */
1334 if (output_ins.label[0] == '.' &&
1335 output_ins.label[1] == '.' &&
1336 output_ins.label[2] != '@') {
1337 if (output_ins.operands == 1 &&
1338 (output_ins.oprs[0].type & IMMEDIATE)) {
1339 define_label(output_ins.label,
1340 output_ins.oprs[0].segment,
1341 output_ins.oprs[0].offset,
1342 NULL, false, false);
1343 } else if (output_ins.operands == 2
1344 && (output_ins.oprs[0].type & IMMEDIATE)
1345 && (output_ins.oprs[0].type & COLON)
1346 && output_ins.oprs[0].segment == NO_SEG
1347 && (output_ins.oprs[1].type & IMMEDIATE)
1348 && output_ins.oprs[1].segment == NO_SEG) {
1349 define_label(output_ins.label,
1350 output_ins.oprs[0].offset | SEG_ABS,
1351 output_ins.oprs[1].offset,
1352 NULL, false, false);
1353 } else
1354 nasm_error(ERR_NONFATAL,
1355 "bad syntax for EQU");
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001356 }
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001357 }
H. Peter Anvinc7131682017-03-07 17:45:01 -08001358 } else { /* instruction isn't an EQU */
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001359 int32_t n;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001360
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001361 nasm_assert(output_ins.times >= 0);
H. Peter Anvinc7131682017-03-07 17:45:01 -08001362
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001363 for (n = 1; n <= output_ins.times; n++) {
1364 if (pass1 == 1) {
1365 int64_t l = insn_size(location.segment, offs,
1366 globalbits, &output_ins);
1367
1368 /* if (using_debug_info) && output_ins.opcode != -1) */
1369 if (using_debug_info)
1370 { /* fbk 03/25/01 */
H. Peter Anvinc7131682017-03-07 17:45:01 -08001371 /* this is done here so we can do debug type info */
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001372 int32_t typeinfo =
1373 TYS_ELEMENTS(output_ins.operands);
1374 switch (output_ins.opcode) {
1375 case I_RESB:
1376 typeinfo =
1377 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_BYTE;
1378 break;
1379 case I_RESW:
1380 typeinfo =
1381 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_WORD;
1382 break;
1383 case I_RESD:
1384 typeinfo =
1385 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_DWORD;
1386 break;
1387 case I_RESQ:
1388 typeinfo =
1389 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_QWORD;
1390 break;
1391 case I_REST:
1392 typeinfo =
1393 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_TBYTE;
1394 break;
1395 case I_RESO:
1396 typeinfo =
1397 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_OWORD;
1398 break;
1399 case I_RESY:
1400 typeinfo =
1401 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_YWORD;
1402 break;
1403 case I_RESZ:
1404 typeinfo =
1405 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_ZWORD;
1406 break;
1407 case I_DB:
1408 typeinfo |= TY_BYTE;
1409 break;
1410 case I_DW:
1411 typeinfo |= TY_WORD;
1412 break;
1413 case I_DD:
1414 if (output_ins.eops_float)
1415 typeinfo |= TY_FLOAT;
1416 else
1417 typeinfo |= TY_DWORD;
1418 break;
1419 case I_DQ:
1420 typeinfo |= TY_QWORD;
1421 break;
1422 case I_DT:
1423 typeinfo |= TY_TBYTE;
1424 break;
1425 case I_DO:
1426 typeinfo |= TY_OWORD;
1427 break;
1428 case I_DY:
1429 typeinfo |= TY_YWORD;
1430 break;
1431 case I_DZ:
1432 typeinfo |= TY_ZWORD;
1433 break;
1434 default:
1435 typeinfo = TY_LABEL;
1436 break;
1437 }
H. Peter Anvinc7131682017-03-07 17:45:01 -08001438
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001439 dfmt->debug_typevalue(typeinfo);
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001440 }
H. Peter Anvinc7131682017-03-07 17:45:01 -08001441
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001442 /*
1443 * For INCBIN, let the code in assemble
1444 * handle TIMES, so we don't have to read the
1445 * input file over and over.
1446 */
1447 if (l != -1) {
1448 offs += l;
1449 set_curr_offs(offs);
1450 }
1451 /*
1452 * else l == -1 => invalid instruction, which will be
1453 * flagged as an error on pass 2
1454 */
1455 } else {
1456 if (n == 2)
1457 lfmt->uplevel(LIST_TIMES);
1458 offs += assemble(location.segment, offs,
1459 globalbits, &output_ins);
Cyrill Gorcunov190232f2013-02-15 12:35:04 +04001460 set_curr_offs(offs);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001461 }
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001462 } /* not an EQU */
1463 }
1464 if (output_ins.times > 1)
1465 lfmt->downlevel(LIST_TIMES);
H. Peter Anvinc7131682017-03-07 17:45:01 -08001466
H. Peter Anvinc7131682017-03-07 17:45:01 -08001467 cleanup_insn(&output_ins);
1468
1469 end_of_line:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001470 nasm_free(line);
Cyrill Gorcunov190232f2013-02-15 12:35:04 +04001471 location.offset = offs = get_curr_offs();
H. Peter Anvine2c80182005-01-15 22:15:51 +00001472 } /* end while (line = preproc->getline... */
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001473
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001474 if (pass0 == 2 && global_offset_changed && !terminate_after_phase)
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001475 nasm_error(ERR_NONFATAL,
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001476 "phase error detected at end of assembly.");
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001477
H. Peter Anvine2c80182005-01-15 22:15:51 +00001478 if (pass1 == 1)
1479 preproc->cleanup(1);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001480
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001481 if ((passn > 1 && !global_offset_changed) || pass0 == 2) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001482 pass0++;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001483 } else if (global_offset_changed &&
1484 global_offset_changed < prev_offset_changed) {
Charles Craynec1905c22008-09-11 18:54:06 -07001485 prev_offset_changed = global_offset_changed;
1486 stall_count = 0;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001487 } else {
1488 stall_count++;
1489 }
Victor van den Elzen42528232008-09-11 15:07:05 +02001490
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001491 if (terminate_after_phase)
1492 break;
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001493
H. Peter Anvin9c595b62017-03-07 19:44:21 -08001494 if ((stall_count > 997U) || (passn >= pass_max)) {
Victor van den Elzen42528232008-09-11 15:07:05 +02001495 /* We get here if the labels don't converge
1496 * Example: FOO equ FOO + 1
1497 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001498 nasm_error(ERR_NONFATAL,
Victor van den Elzen42528232008-09-11 15:07:05 +02001499 "Can't find valid values for all labels "
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001500 "after %d passes, giving up.", passn);
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001501 nasm_error(ERR_NONFATAL,
1502 "Possible causes: recursive EQUs, macro abuse.");
1503 break;
1504 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001505 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001506
H. Peter Anvine2c80182005-01-15 22:15:51 +00001507 preproc->cleanup(0);
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08001508 lfmt->cleanup();
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001509 if (!terminate_after_phase && opt_verbose_info) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001510 /* -On and -Ov switches */
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001511 fprintf(stdout, "info: assembly required 1+%d+1 passes\n", passn-3);
1512 }
1513}
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001514
Ed Berosetfa771012002-06-09 20:56:40 +00001515/**
1516 * gnu style error reporting
1517 * This function prints an error message to error_file in the
1518 * style used by GNU. An example would be:
1519 * file.asm:50: error: blah blah blah
H. Peter Anvin70653092007-10-19 14:42:29 -07001520 * where file.asm is the name of the file, 50 is the line number on
Ed Berosetfa771012002-06-09 20:56:40 +00001521 * which the error occurs (or is detected) and "error:" is one of
1522 * the possible optional diagnostics -- it can be "error" or "warning"
H. Peter Anvin70653092007-10-19 14:42:29 -07001523 * or something else. Finally the line terminates with the actual
Ed Berosetfa771012002-06-09 20:56:40 +00001524 * error message.
H. Peter Anvin70653092007-10-19 14:42:29 -07001525 *
1526 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001527 * @param fmt the printf style format string
1528 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001529static void nasm_verror_gnu(int severity, const char *fmt, va_list ap)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001530{
H. Peter Anvin274cda82016-05-10 02:56:29 -07001531 const char *currentfile = NULL;
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001532 int32_t lineno = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001533
Ed Berosetfa771012002-06-09 20:56:40 +00001534 if (is_suppressed_warning(severity))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001535 return;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001536
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001537 if (!(severity & ERR_NOFILE))
H. Peter Anvin4def1a82016-05-09 13:59:44 -07001538 src_get(&lineno, &currentfile);
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001539
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001540 if (!skip_this_pass(severity)) {
1541 if (currentfile) {
1542 fprintf(error_file, "%s:%"PRId32": ", currentfile, lineno);
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001543 } else {
1544 fputs("nasm: ", error_file);
1545 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001546 }
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001547
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001548 nasm_verror_common(severity, fmt, ap);
Ed Berosetfa771012002-06-09 20:56:40 +00001549}
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001550
Ed Berosetfa771012002-06-09 20:56:40 +00001551/**
1552 * MS style error reporting
1553 * This function prints an error message to error_file in the
H. Peter Anvin70653092007-10-19 14:42:29 -07001554 * style used by Visual C and some other Microsoft tools. An example
Ed Berosetfa771012002-06-09 20:56:40 +00001555 * would be:
Ed Beroset6e61d0d2002-06-11 03:29:36 +00001556 * file.asm(50) : error: blah blah blah
H. Peter Anvin70653092007-10-19 14:42:29 -07001557 * where file.asm is the name of the file, 50 is the line number on
Ed Beroset6e61d0d2002-06-11 03:29:36 +00001558 * which the error occurs (or is detected) and "error:" is one of
1559 * the possible optional diagnostics -- it can be "error" or "warning"
H. Peter Anvin70653092007-10-19 14:42:29 -07001560 * or something else. Finally the line terminates with the actual
Ed Beroset6e61d0d2002-06-11 03:29:36 +00001561 * error message.
H. Peter Anvin70653092007-10-19 14:42:29 -07001562 *
1563 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001564 * @param fmt the printf style format string
1565 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001566static void nasm_verror_vc(int severity, const char *fmt, va_list ap)
Ed Berosetfa771012002-06-09 20:56:40 +00001567{
H. Peter Anvin274cda82016-05-10 02:56:29 -07001568 const char *currentfile = NULL;
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001569 int32_t lineno = 0;
Ed Berosetfa771012002-06-09 20:56:40 +00001570
H. Peter Anvine2c80182005-01-15 22:15:51 +00001571 if (is_suppressed_warning(severity))
1572 return;
Ed Berosetfa771012002-06-09 20:56:40 +00001573
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001574 if (!(severity & ERR_NOFILE))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001575 src_get(&lineno, &currentfile);
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001576
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001577 if (!skip_this_pass(severity)) {
1578 if (currentfile) {
1579 fprintf(error_file, "%s(%"PRId32") : ", currentfile, lineno);
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001580 } else {
1581 fputs("nasm: ", error_file);
1582 }
Ed Berosetfa771012002-06-09 20:56:40 +00001583 }
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001584
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001585 nasm_verror_common(severity, fmt, ap);
Ed Berosetfa771012002-06-09 20:56:40 +00001586}
1587
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001588/*
1589 * check to see if this is a suppressable warning
1590 */
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001591static inline bool is_valid_warning(int severity)
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001592{
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001593 /* Not a warning at all */
1594 if ((severity & ERR_MASK) != ERR_WARNING)
1595 return false;
1596
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001597 return WARN_IDX(severity) < ERR_WARN_ALL;
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001598}
1599
Ed Berosetfa771012002-06-09 20:56:40 +00001600/**
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001601 * check for suppressed warning
H. Peter Anvin70653092007-10-19 14:42:29 -07001602 * checks for suppressed warning or pass one only warning and we're
Ed Berosetfa771012002-06-09 20:56:40 +00001603 * not in pass 1
1604 *
1605 * @param severity the severity of the warning or error
1606 * @return true if we should abort error/warning printing
1607 */
H. Peter Anvin2b046cf2008-01-22 14:08:36 -08001608static bool is_suppressed_warning(int severity)
Ed Berosetfa771012002-06-09 20:56:40 +00001609{
H. Peter Anvin442a05a2012-02-24 21:50:53 -08001610 /* Might be a warning but suppresed explicitly */
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001611 if (is_valid_warning(severity))
1612 return !(warning_state[WARN_IDX(severity)] & WARN_ST_ENABLED);
1613 else
1614 return false;
1615}
1616
1617static bool warning_is_error(int severity)
1618{
1619 if (is_valid_warning(severity))
1620 return !!(warning_state[WARN_IDX(severity)] & WARN_ST_ERROR);
H. Peter Anvin442a05a2012-02-24 21:50:53 -08001621 else
1622 return false;
Ed Berosetfa771012002-06-09 20:56:40 +00001623}
1624
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001625static bool skip_this_pass(int severity)
1626{
H. Peter Anvin8f622462017-04-02 19:02:29 -07001627 /*
1628 * See if it's a pass-specific error or warning which should be skipped.
1629 * We cannot skip errors stronger than ERR_NONFATAL as by definition
1630 * they cannot be resumed from.
1631 */
1632 if ((severity & ERR_MASK) > ERR_NONFATAL)
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001633 return false;
1634
H. Peter Anvin934f0472016-05-09 12:00:19 -07001635 /*
1636 * passn is 1 on the very first pass only.
1637 * pass0 is 2 on the code-generation (final) pass only.
1638 * These are the passes we care about in this case.
1639 */
1640 return (((severity & ERR_PASS1) && passn != 1) ||
1641 ((severity & ERR_PASS2) && pass0 != 2));
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001642}
1643
Ed Berosetfa771012002-06-09 20:56:40 +00001644/**
1645 * common error reporting
1646 * This is the common back end of the error reporting schemes currently
H. Peter Anvin70653092007-10-19 14:42:29 -07001647 * implemented. It prints the nature of the warning and then the
Ed Berosetfa771012002-06-09 20:56:40 +00001648 * specific error message to error_file and may or may not return. It
1649 * doesn't return if the error severity is a "panic" or "debug" type.
H. Peter Anvin70653092007-10-19 14:42:29 -07001650 *
1651 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001652 * @param fmt the printf style format string
1653 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001654static void nasm_verror_common(int severity, const char *fmt, va_list args)
Ed Berosetfa771012002-06-09 20:56:40 +00001655{
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001656 char msg[1024];
1657 const char *pfx;
H. Peter Anvin323fcff2009-07-12 12:04:56 -07001658
H. Peter Anvin7df04172008-06-10 18:27:38 -07001659 switch (severity & (ERR_MASK|ERR_NO_SEVERITY)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001660 case ERR_WARNING:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001661 pfx = "warning: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001662 break;
1663 case ERR_NONFATAL:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001664 pfx = "error: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001665 break;
1666 case ERR_FATAL:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001667 pfx = "fatal: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001668 break;
1669 case ERR_PANIC:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001670 pfx = "panic: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001671 break;
1672 case ERR_DEBUG:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001673 pfx = "debug: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001674 break;
H. Peter Anvin7df04172008-06-10 18:27:38 -07001675 default:
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001676 pfx = "";
1677 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001678 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00001679
H. Peter Anvin934f0472016-05-09 12:00:19 -07001680 vsnprintf(msg, sizeof msg - 64, fmt, args);
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001681 if (is_valid_warning(severity) && WARN_IDX(severity) != ERR_WARN_OTHER) {
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001682 char *p = strchr(msg, '\0');
H. Peter Anvin934f0472016-05-09 12:00:19 -07001683 snprintf(p, 64, " [-w+%s]", warnings[WARN_IDX(severity)].name);
1684 }
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001685
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001686 if (!skip_this_pass(severity))
1687 fprintf(error_file, "%s%s\n", pfx, msg);
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001688
H. Peter Anvin4def1a82016-05-09 13:59:44 -07001689 /* Are we recursing from error_list_macros? */
1690 if (severity & ERR_PP_LISTMACRO)
1691 return;
1692
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001693 /*
1694 * Don't suppress this with skip_this_pass(), or we don't get
H. Peter Anvin934f0472016-05-09 12:00:19 -07001695 * pass1 or preprocessor warnings in the list file
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001696 */
H. Peter Anvin0fcb4882016-07-06 11:55:25 -07001697 lfmt->error(severity, pfx, msg);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001698
H. Peter Anvin8f622462017-04-02 19:02:29 -07001699 if (skip_this_pass(severity))
1700 return;
1701
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001702 if (severity & ERR_USAGE)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001703 want_usage = true;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001704
H. Peter Anvin4def1a82016-05-09 13:59:44 -07001705 preproc->error_list_macros(severity);
1706
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001707 switch (severity & ERR_MASK) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001708 case ERR_DEBUG:
1709 /* no further action, by definition */
1710 break;
H. Peter Anvinb030c922007-11-13 11:31:15 -08001711 case ERR_WARNING:
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001712 /* Treat warnings as errors */
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001713 if (warning_is_error(severity))
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001714 terminate_after_phase = true;
1715 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001716 case ERR_NONFATAL:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001717 terminate_after_phase = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001718 break;
1719 case ERR_FATAL:
1720 if (ofile) {
1721 fclose(ofile);
1722 remove(outname);
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001723 ofile = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001724 }
1725 if (want_usage)
1726 usage();
1727 exit(1); /* instantly die */
1728 break; /* placate silly compilers */
1729 case ERR_PANIC:
1730 fflush(NULL);
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001731 /* abort(); */ /* halt, catch fire, and dump core */
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001732 if (ofile) {
1733 fclose(ofile);
1734 remove(outname);
1735 ofile = NULL;
1736 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001737 exit(3);
1738 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001739 }
1740}
1741
H. Peter Anvin734b1882002-04-30 21:01:08 +00001742static void usage(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001743{
H. Peter Anvin620515a2002-04-30 20:57:38 +00001744 fputs("type `nasm -h' for help\n", error_file);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001745}