blob: 60cd6ea58b859e30a87f00bc3160ad4ef33ff18e [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 Anvin77c9bf62017-08-16 21:14:33 -0700222 char wrapstr[] = " \\\n ";
223
224 wrapstr[1] = (quote_for_make == quote_for_wmake) ? '&' : '\\';
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700225
226 if (depend_file && strcmp(depend_file, "-")) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -0700227 deps = nasm_open_write(depend_file, NF_TEXT);
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400228 if (!deps) {
229 nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
230 "unable to write dependency file `%s'", depend_file);
231 return;
232 }
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700233 } else {
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400234 deps = stdout;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700235 }
H. Peter Anvin323fcff2009-07-12 12:04:56 -0700236
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700237 linepos = fprintf(deps, "%s :", depend_target);
Cyrill Gorcunovfcd0a742009-07-27 16:26:26 +0400238 list_for_each(l, list) {
H. Peter Anvin55340992012-09-09 17:09:00 -0700239 char *file = quote_for_make(l->str);
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400240 len = strlen(file);
241 if (linepos + len > 62 && linepos > 1) {
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700242 fwrite(wrapstr, 1, sizeof wrapstr-1, deps);
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400243 linepos = 1;
244 }
245 fprintf(deps, " %s", file);
246 linepos += len+1;
H. Peter Anvin55340992012-09-09 17:09:00 -0700247 nasm_free(file);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700248 }
249 fprintf(deps, "\n\n");
H. Peter Anvin323fcff2009-07-12 12:04:56 -0700250
Cyrill Gorcunovfcd0a742009-07-27 16:26:26 +0400251 list_for_each_safe(l, nl, list) {
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700252 if (depend_emit_phony) {
253 char *file = quote_for_make(l->str);
254 fprintf(deps, "%s :\n\n", file);
255 nasm_free(file);
256 }
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400257 nasm_free(l);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700258 }
259
260 if (deps != stdout)
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400261 fclose(deps);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700262}
263
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700264/* Convert a struct tm to a POSIX-style time constant */
265static int64_t make_posix_time(const struct tm *tm)
266{
267 int64_t t;
268 int64_t y = tm->tm_year;
269
270 /* See IEEE 1003.1:2004, section 4.14 */
271
272 t = (y-70)*365 + (y-69)/4 - (y-1)/100 + (y+299)/400;
273 t += tm->tm_yday;
274 t *= 24;
275 t += tm->tm_hour;
276 t *= 60;
277 t += tm->tm_min;
278 t *= 60;
279 t += tm->tm_sec;
280
281 return t;
282}
283
284static void timestamp(void)
285{
286 struct compile_time * const oct = &official_compile_time;
287 const struct tm *tp, *best_gm;
288
289 time(&oct->t);
290
291 best_gm = NULL;
292
293 tp = localtime(&oct->t);
294 if (tp) {
295 oct->local = *tp;
296 best_gm = &oct->local;
297 oct->have_local = true;
298 }
299
300 tp = gmtime(&oct->t);
301 if (tp) {
302 oct->gm = *tp;
303 best_gm = &oct->gm;
304 oct->have_gm = true;
305 if (!oct->have_local)
306 oct->local = oct->gm;
307 } else {
308 oct->gm = oct->local;
309 }
310
311 if (best_gm) {
312 oct->posix = make_posix_time(best_gm);
313 oct->have_posix = true;
314 }
315}
316
H. Peter Anvin038d8612007-04-12 16:54:50 +0000317int main(int argc, char **argv)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000318{
H. Peter Anvina771be82017-08-16 14:53:18 -0700319 StrList **depend_ptr;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700320
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700321 timestamp();
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800322
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400323 iflag_set(&cpu, IF_PLEVEL);
324 iflag_set(&cmd_cpu, IF_PLEVEL);
325
Charles Crayne2581c862008-09-10 19:21:52 -0700326 pass0 = 0;
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700327 want_usage = terminate_after_phase = false;
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400328 nasm_set_verror(nasm_verror_gnu);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000329
H. Peter Anvin97e15752007-09-25 08:47:47 -0700330 error_file = stderr;
331
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -0700332 tolower_init();
H. Peter Anvin274cda82016-05-10 02:56:29 -0700333 src_init();
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -0700334
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000335 offsets = raa_init();
Keith Kaniosb7a89542007-04-12 02:40:54 +0000336 forwrefs = saa_init((int32_t)sizeof(struct forwrefinfo));
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000337
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000338 preproc = &nasmpp;
Cyrill Gorcunov3ed32cb2014-06-22 20:53:12 +0400339 operating_mode = OP_NORMAL;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000340
H. Peter Anvin55568c12016-10-03 19:46:49 -0700341 parse_cmdline(argc, argv, 1);
342 if (terminate_after_phase) {
343 if (want_usage)
344 usage();
345 return 1;
346 }
347
H. Peter Anvin169ac7c2016-09-25 17:08:05 -0700348 /*
349 * Define some macros dependent on the runtime, but not
H. Peter Anvin55568c12016-10-03 19:46:49 -0700350 * on the command line (as those are scanned in cmdline pass 2.)
H. Peter Anvin169ac7c2016-09-25 17:08:05 -0700351 */
352 preproc->init();
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800353 define_macros_early();
354
H. Peter Anvin55568c12016-10-03 19:46:49 -0700355 parse_cmdline(argc, argv, 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000356 if (terminate_after_phase) {
357 if (want_usage)
358 usage();
359 return 1;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000360 }
361
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800362 /* Save away the default state of warnings */
363 memcpy(warning_state_init, warning_state, sizeof warning_state);
364
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800365 if (!using_debug_info) {
366 /* No debug info, redirect to the null backend (empty stubs) */
H. Peter Anvina7bc15d2016-02-17 20:55:08 -0800367 dfmt = &null_debug_form;
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800368 } else if (!debug_format) {
369 /* Default debug format for this backend */
H. Peter Anvina7bc15d2016-02-17 20:55:08 -0800370 dfmt = ofmt->default_dfmt;
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800371 } else {
372 dfmt = dfmt_find(ofmt, debug_format);
373 if (!dfmt) {
374 nasm_fatal(ERR_NOFILE | ERR_USAGE,
375 "unrecognized debug format `%s' for"
376 " output format `%s'",
377 debug_format, ofmt->shortname);
378 }
379 }
H. Peter Anvinbb88d012003-09-10 23:34:23 +0000380
H. Peter Anvin76690a12002-04-30 20:52:49 +0000381 if (ofmt->stdmac)
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +0400382 preproc->extra_stdmac(ofmt->stdmac);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000383
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000384 /* define some macros dependent of command-line */
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800385 define_macros_late();
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000386
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400387 depend_ptr = (depend_file || (operating_mode & OP_DEPEND)) ? &depend_list : NULL;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700388
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700389 if (!depend_target)
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400390 depend_target = quote_for_make(outname);
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700391
Cyrill Gorcunove9fc88c2014-06-23 02:22:02 +0400392 if (operating_mode & OP_DEPEND) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000393 char *line;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700394
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400395 if (depend_missing_ok)
396 preproc->include_path(NULL); /* "assume generated" */
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700397
H. Peter Anvin130736c2016-02-17 20:27:41 -0800398 preproc->reset(inname, 0, depend_ptr);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000399 if (outname[0] == '\0')
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400400 ofmt->filename(inname, outname);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000401 ofile = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000402 while ((line = preproc->getline()))
403 nasm_free(line);
404 preproc->cleanup(0);
Cyrill Gorcunove9fc88c2014-06-23 02:22:02 +0400405 } else if (operating_mode & OP_PREPROCESS) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000406 char *line;
H. Peter Anvin274cda82016-05-10 02:56:29 -0700407 const char *file_name = NULL;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000408 int32_t prior_linnum = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000409 int lineinc = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000410
H. Peter Anvine2c80182005-01-15 22:15:51 +0000411 if (*outname) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -0700412 ofile = nasm_open_write(outname, NF_TEXT);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000413 if (!ofile)
H. Peter Anvin41087062016-03-03 14:27:34 -0800414 nasm_fatal(ERR_NOFILE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000415 "unable to open output file `%s'",
416 outname);
417 } else
418 ofile = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000419
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700420 location.known = false;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000421
Cyrill Gorcunovb574b072011-12-05 01:56:40 +0400422 /* pass = 1; */
H. Peter Anvin130736c2016-02-17 20:27:41 -0800423 preproc->reset(inname, 3, depend_ptr);
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800424
425 /* Revert all warnings to the default state */
426 memcpy(warning_state, warning_state_init, sizeof warning_state);
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700427
H. Peter Anvine2c80182005-01-15 22:15:51 +0000428 while ((line = preproc->getline())) {
429 /*
430 * We generate %line directives if needed for later programs
431 */
Keith Kaniosb7a89542007-04-12 02:40:54 +0000432 int32_t linnum = prior_linnum += lineinc;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000433 int altline = src_get(&linnum, &file_name);
434 if (altline) {
435 if (altline == 1 && lineinc == 1)
436 nasm_fputs("", ofile);
437 else {
438 lineinc = (altline != -1 || lineinc != 1);
439 fprintf(ofile ? ofile : stdout,
Keith Kanios93f2e9a2007-04-14 00:10:59 +0000440 "%%line %"PRId32"+%d %s\n", linnum, lineinc,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000441 file_name);
442 }
443 prior_linnum = linnum;
444 }
445 nasm_fputs(line, ofile);
446 nasm_free(line);
447 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000448 preproc->cleanup(0);
449 if (ofile)
450 fclose(ofile);
451 if (ofile && terminate_after_phase)
452 remove(outname);
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400453 ofile = NULL;
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400454 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000455
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400456 if (operating_mode & OP_NORMAL) {
457 /*
458 * We must call ofmt->filename _anyway_, even if the user
459 * has specified their own output file, because some
460 * formats (eg OBJ and COFF) use ofmt->filename to find out
461 * the name of the input file and then put that inside the
462 * file.
463 */
464 ofmt->filename(inname, outname);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000465
H. Peter Anvin3e83cec2016-05-25 04:28:46 -0700466 ofile = nasm_open_write(outname, (ofmt->flags & OFMT_TEXT) ? NF_TEXT : NF_BINARY);
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400467 if (!ofile)
H. Peter Anvin41087062016-03-03 14:27:34 -0800468 nasm_fatal(ERR_NOFILE,
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400469 "unable to open output file `%s'", outname);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000470
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400471 /*
472 * We must call init_labels() before ofmt->init() since
473 * some object formats will want to define labels in their
474 * init routines. (eg OS/2 defines the FLAT group)
475 */
476 init_labels();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000477
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400478 ofmt->init();
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400479 dfmt->init();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000480
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400481 assemble_file(inname, depend_ptr);
Victor van den Elzenc82c3722008-06-04 15:24:20 +0200482
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400483 if (!terminate_after_phase) {
H. Peter Anvin477ae442016-03-07 22:53:06 -0800484 ofmt->cleanup();
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400485 cleanup_labels();
486 fflush(ofile);
H. Peter Anvin274cda82016-05-10 02:56:29 -0700487 if (ferror(ofile)) {
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400488 nasm_error(ERR_NONFATAL|ERR_NOFILE,
489 "write error on output file `%s'", outname);
H. Peter Anvin274cda82016-05-10 02:56:29 -0700490 terminate_after_phase = true;
491 }
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400492 }
493
494 if (ofile) {
495 fclose(ofile);
496 if (terminate_after_phase)
497 remove(outname);
498 ofile = NULL;
499 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000500 }
H. Peter Anvin734b1882002-04-30 21:01:08 +0000501
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700502 if (depend_list && !terminate_after_phase)
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400503 emit_dependencies(depend_list);
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700504
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000505 if (want_usage)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000506 usage();
H. Peter Anvin734b1882002-04-30 21:01:08 +0000507
H. Peter Anvine2c80182005-01-15 22:15:51 +0000508 raa_free(offsets);
509 saa_free(forwrefs);
510 eval_cleanup();
H. Peter Anvin74cc5e52007-08-30 22:35:34 +0000511 stdscan_cleanup();
H. Peter Anvin274cda82016-05-10 02:56:29 -0700512 src_free();
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000513
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700514 return terminate_after_phase;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000515}
516
H. Peter Anvineba20a72002-04-30 20:53:55 +0000517/*
518 * Get a parameter for a command line option.
519 * First arg must be in the form of e.g. -f...
520 */
H. Peter Anvin423e3812007-11-15 17:12:29 -0800521static char *get_param(char *p, char *q, bool *advance)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000522{
H. Peter Anvin423e3812007-11-15 17:12:29 -0800523 *advance = false;
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +0400524 if (p[2]) /* the parameter's in the option */
525 return nasm_skip_spaces(p + 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000526 if (q && q[0]) {
H. Peter Anvin423e3812007-11-15 17:12:29 -0800527 *advance = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000528 return q;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000529 }
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400530 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000531 "option `-%c' requires an argument", p[1]);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000532 return NULL;
533}
534
H. Peter Anvindc242712007-11-18 11:55:10 -0800535/*
536 * Copy a filename
537 */
538static void copy_filename(char *dst, const char *src)
539{
540 size_t len = strlen(src);
541
542 if (len >= (size_t)FILENAME_MAX) {
H. Peter Anvin41087062016-03-03 14:27:34 -0800543 nasm_fatal(ERR_NOFILE, "file name too long");
Cyrill Gorcunov45aa1182013-02-15 12:22:59 +0400544 return;
H. Peter Anvindc242712007-11-18 11:55:10 -0800545 }
546 strncpy(dst, src, FILENAME_MAX);
547}
548
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700549/*
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700550 * Convert a string to a POSIX make-safe form
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700551 */
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700552static char *quote_for_pmake(const char *str)
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700553{
554 const char *p;
555 char *os, *q;
556
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400557 size_t n = 1; /* Terminating zero */
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700558 size_t nbs = 0;
559
560 if (!str)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400561 return NULL;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700562
563 for (p = str; *p; p++) {
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400564 switch (*p) {
565 case ' ':
566 case '\t':
567 /* Convert N backslashes + ws -> 2N+1 backslashes + ws */
568 n += nbs + 2;
569 nbs = 0;
570 break;
571 case '$':
572 case '#':
573 nbs = 0;
574 n += 2;
575 break;
576 case '\\':
577 nbs++;
578 n++;
579 break;
580 default:
581 nbs = 0;
582 n++;
583 break;
584 }
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700585 }
586
587 /* Convert N backslashes at the end of filename to 2N backslashes */
588 if (nbs)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400589 n += nbs;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700590
591 os = q = nasm_malloc(n);
592
593 nbs = 0;
594 for (p = str; *p; p++) {
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400595 switch (*p) {
596 case ' ':
597 case '\t':
598 while (nbs--)
599 *q++ = '\\';
600 *q++ = '\\';
601 *q++ = *p;
602 break;
603 case '$':
604 *q++ = *p;
605 *q++ = *p;
606 nbs = 0;
607 break;
608 case '#':
609 *q++ = '\\';
610 *q++ = *p;
611 nbs = 0;
612 break;
613 case '\\':
614 *q++ = *p;
615 nbs++;
616 break;
617 default:
618 *q++ = *p;
619 nbs = 0;
620 break;
621 }
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700622 }
623 while (nbs--)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400624 *q++ = '\\';
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700625
626 *q = '\0';
627
628 return os;
629}
630
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700631/*
632 * Convert a string to a Watcom make-safe form
633 */
634static char *quote_for_wmake(const char *str)
635{
636 const char *p;
637 char *os, *q;
638
639 size_t n = 1; /* Terminating zero */
640
641 if (!str)
642 return NULL;
643
644 for (p = str; *p; p++) {
645 switch (*p) {
646 case '$':
647 case '#':
648 n += 2;
649 break;
650 default:
651 n++;
652 break;
653 }
654 }
655
656 os = q = nasm_malloc(n);
657
658 for (p = str; *p; p++) {
659 switch (*p) {
660 case '$':
661 case '#':
662 *q++ = '$';
663 *q++ = *p;
664 break;
665 default:
666 *q++ = *p;
667 break;
668 }
669 }
670
671 *q = '\0';
672
673 return os;
674}
675
H. Peter Anvine2c80182005-01-15 22:15:51 +0000676struct textargs {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000677 const char *label;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000678 int value;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000679};
680
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100681enum text_options {
682 OPT_PREFIX,
H. Peter Anvin7214d182016-03-01 22:43:51 -0800683 OPT_POSTFIX
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100684};
H. Peter Anvin2530a102016-02-18 02:28:15 -0800685static const struct textargs textopts[] = {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000686 {"prefix", OPT_PREFIX},
687 {"postfix", OPT_POSTFIX},
688 {NULL, 0}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000689};
690
Cyrill Gorcunove7438432014-05-09 22:34:34 +0400691static void show_version(void)
692{
693 printf("NASM version %s compiled on %s%s\n",
694 nasm_version, nasm_date, nasm_compile_options);
695 exit(0);
696}
697
H. Peter Anvin423e3812007-11-15 17:12:29 -0800698static bool stopoptions = false;
H. Peter Anvin55568c12016-10-03 19:46:49 -0700699static bool process_arg(char *p, char *q, int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000700{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000701 char *param;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800702 int i;
703 bool advance = false;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000704
705 if (!p || !p[0])
H. Peter Anvin423e3812007-11-15 17:12:29 -0800706 return false;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000707
H. Peter Anvine2c80182005-01-15 22:15:51 +0000708 if (p[0] == '-' && !stopoptions) {
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400709 if (strchr("oOfpPdDiIlFXuUZwW", p[1])) {
710 /* These parameters take values */
711 if (!(param = get_param(p, q, &advance)))
712 return advance;
713 }
H. Peter Anvin423e3812007-11-15 17:12:29 -0800714
H. Peter Anvine2c80182005-01-15 22:15:51 +0000715 switch (p[1]) {
716 case 's':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700717 if (pass == 1)
718 error_file = stdout;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000719 break;
H. Peter Anvin70653092007-10-19 14:42:29 -0700720
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400721 case 'o': /* output file */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700722 if (pass == 2)
723 copy_filename(outname, param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400724 break;
H. Peter Anvinfd7dd112007-10-10 14:06:59 -0700725
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400726 case 'f': /* output format */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700727 if (pass == 1) {
728 ofmt = ofmt_find(param, &ofmt_alias);
729 if (!ofmt) {
730 nasm_fatal(ERR_NOFILE | ERR_USAGE,
731 "unrecognised output format `%s' - "
732 "use -hf for a list", param);
733 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400734 }
735 break;
H. Peter Anvin70653092007-10-19 14:42:29 -0700736
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400737 case 'O': /* Optimization level */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700738 if (pass == 2) {
739 int opt;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700740
H. Peter Anvin55568c12016-10-03 19:46:49 -0700741 if (!*param) {
742 /* Naked -O == -Ox */
743 optimizing = MAX_OPTIMIZE;
744 } else {
745 while (*param) {
746 switch (*param) {
747 case '0': case '1': case '2': case '3': case '4':
748 case '5': case '6': case '7': case '8': case '9':
749 opt = strtoul(param, &param, 10);
H. Peter Anvind85d2502008-05-04 17:53:31 -0700750
H. Peter Anvin55568c12016-10-03 19:46:49 -0700751 /* -O0 -> optimizing == -1, 0.98 behaviour */
752 /* -O1 -> optimizing == 0, 0.98.09 behaviour */
753 if (opt < 2)
754 optimizing = opt - 1;
755 else
756 optimizing = opt;
757 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700758
H. Peter Anvin55568c12016-10-03 19:46:49 -0700759 case 'v':
760 case '+':
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400761 param++;
762 opt_verbose_info = true;
763 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700764
H. Peter Anvin55568c12016-10-03 19:46:49 -0700765 case 'x':
766 param++;
767 optimizing = MAX_OPTIMIZE;
768 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700769
H. Peter Anvin55568c12016-10-03 19:46:49 -0700770 default:
771 nasm_fatal(0,
772 "unknown optimization option -O%c\n",
773 *param);
774 break;
775 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400776 }
H. Peter Anvin55568c12016-10-03 19:46:49 -0700777 if (optimizing > MAX_OPTIMIZE)
778 optimizing = MAX_OPTIMIZE;
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400779 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400780 }
781 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800782
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400783 case 'p': /* pre-include */
784 case 'P':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700785 if (pass == 2)
786 preproc->pre_include(param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400787 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800788
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400789 case 'd': /* pre-define */
790 case 'D':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700791 if (pass == 2)
792 preproc->pre_define(param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400793 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800794
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400795 case 'u': /* un-define */
796 case 'U':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700797 if (pass == 2)
798 preproc->pre_undefine(param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400799 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800800
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400801 case 'i': /* include search path */
802 case 'I':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700803 if (pass == 2)
804 preproc->include_path(param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400805 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800806
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400807 case 'l': /* listing file */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700808 if (pass == 2)
809 copy_filename(listname, param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400810 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800811
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400812 case 'Z': /* error messages file */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700813 if (pass == 1)
814 copy_filename(errname, param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400815 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800816
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400817 case 'F': /* specify debug format */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700818 if (pass == 2) {
819 using_debug_info = true;
820 debug_format = param;
821 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400822 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800823
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400824 case 'X': /* specify error reporting format */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700825 if (pass == 1) {
826 if (nasm_stricmp("vc", param) == 0)
827 nasm_set_verror(nasm_verror_vc);
828 else if (nasm_stricmp("gnu", param) == 0)
829 nasm_set_verror(nasm_verror_gnu);
830 else
831 nasm_fatal(ERR_NOFILE | ERR_USAGE,
832 "unrecognized error reporting format `%s'",
833 param);
834 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000835 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800836
H. Peter Anvine2c80182005-01-15 22:15:51 +0000837 case 'g':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700838 if (pass == 2) {
839 using_debug_info = true;
840 if (p[2])
841 debug_format = nasm_skip_spaces(p + 2);
842 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000843 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800844
H. Peter Anvine2c80182005-01-15 22:15:51 +0000845 case 'h':
846 printf
847 ("usage: nasm [-@ response file] [-o outfile] [-f format] "
848 "[-l listfile]\n"
849 " [options...] [--] filename\n"
Cyrill Gorcunove7438432014-05-09 22:34:34 +0400850 " or nasm -v (or --v) for version info\n\n"
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800851 " -t assemble in SciTech TASM compatible mode\n");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000852 printf
H. Peter Anvin413fb902007-09-26 15:19:28 -0700853 (" -E (or -e) preprocess only (writes output to stdout by default)\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000854 " -a don't preprocess (assemble only)\n"
H. Peter Anvin37a321f2007-09-24 13:41:58 -0700855 " -M generate Makefile dependencies on stdout\n"
Cyrill Gorcunov984c4db2009-07-31 00:32:17 +0400856 " -MG d:o, missing files assumed generated\n"
857 " -MF <file> set Makefile dependency file\n"
858 " -MD <file> assemble and generate dependencies\n"
859 " -MT <file> dependency target name\n"
860 " -MQ <file> dependency target name (quoted)\n"
861 " -MP emit phony target\n\n"
H. Peter Anvin413fb902007-09-26 15:19:28 -0700862 " -Z<file> redirect error messages to file\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000863 " -s redirect error messages to stdout\n\n"
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800864 " -g generate debugging information\n\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000865 " -F format select a debugging format\n\n"
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800866 " -gformat same as -g -F format\n\n"
Cyrill Gorcunovdeb082d2013-04-20 20:37:17 +0400867 " -o outfile write output to an outfile\n\n"
868 " -f format select an output format\n\n"
869 " -l listfile write listing to a listfile\n\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000870 " -I<path> adds a pathname to the include file path\n");
871 printf
H. Peter Anvinab5bd052010-07-25 12:43:30 -0700872 (" -O<digit> optimize branch offsets\n"
Cyrill Gorcunov5bc6d8e2012-03-11 14:19:17 +0400873 " -O0: No optimization\n"
Cyrill Gorcunov73b87c62009-07-31 10:26:55 +0400874 " -O1: Minimal optimization\n"
Cyrill Gorcunov5bc6d8e2012-03-11 14:19:17 +0400875 " -Ox: Multipass optimization (default)\n\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000876 " -P<file> pre-includes a file\n"
877 " -D<macro>[=<value>] pre-defines a macro\n"
878 " -U<macro> undefines a macro\n"
879 " -X<format> specifies error reporting format (gnu or vc)\n"
H. Peter Anvinb030c922007-11-13 11:31:15 -0800880 " -w+foo enables warning foo (equiv. -Wfoo)\n"
Cyrill Gorcunov984c4db2009-07-31 00:32:17 +0400881 " -w-foo disable warning foo (equiv. -Wno-foo)\n\n"
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800882 " -w[+-]error[=foo] can be used to promote warnings to errors\n"
883 " -h show invocation summary and exit\n\n"
Cyrill Gorcunov984c4db2009-07-31 00:32:17 +0400884 "--prefix,--postfix\n"
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800885 " these options prepend or append the given string\n"
886 " to all extern and global variables\n"
887 "\n"
888 "Response files should contain command line parameters,\n"
889 "one per line.\n"
890 "\n"
891 "Warnings for the -W/-w options:\n");
892 for (i = 0; i <= ERR_WARN_ALL; i++)
893 printf(" %-23s %s%s\n",
H. Peter Anvin972079f2008-09-30 17:01:23 -0700894 warnings[i].name, warnings[i].help,
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800895 i == ERR_WARN_ALL ? "\n" :
896 warnings[i].enabled ? " (default on)" :
897 " (default off)");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000898 if (p[2] == 'f') {
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800899 printf("valid output formats for -f are"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000900 " (`*' denotes default):\n");
901 ofmt_list(ofmt, stdout);
902 } else {
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800903 printf("For a list of valid output formats, use -hf.\n");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000904 printf("For a list of debug formats, use -f <form> -y.\n");
905 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400906 exit(0); /* never need usage message here */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000907 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800908
H. Peter Anvine2c80182005-01-15 22:15:51 +0000909 case 'y':
910 printf("\nvalid debug formats for '%s' output format are"
911 " ('*' denotes default):\n", ofmt->shortname);
912 dfmt_list(ofmt, stdout);
913 exit(0);
914 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800915
H. Peter Anvine2c80182005-01-15 22:15:51 +0000916 case 't':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700917 if (pass == 2)
918 tasm_compatible_mode = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000919 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800920
H. Peter Anvine2c80182005-01-15 22:15:51 +0000921 case 'v':
Cyrill Gorcunove7438432014-05-09 22:34:34 +0400922 show_version();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000923 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800924
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400925 case 'e': /* preprocess only */
926 case 'E':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700927 if (pass == 1)
928 operating_mode = OP_PREPROCESS;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000929 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800930
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400931 case 'a': /* assemble only - don't preprocess */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700932 if (pass == 1)
933 preproc = &preproc_nop;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000934 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800935
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800936 case 'w':
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400937 case 'W':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700938 if (pass == 2) {
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800939 if (!set_warning_status(param)) {
940 nasm_error(ERR_WARNING|ERR_NOFILE|ERR_WARN_UNK_WARNING,
941 "unknown warning option: %s", param);
H. Peter Anvin55568c12016-10-03 19:46:49 -0700942 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400943 }
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800944 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800945
H. Peter Anvine2c80182005-01-15 22:15:51 +0000946 case 'M':
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700947 if (pass == 1) {
948 switch (p[2]) {
949 case 'W':
950 quote_for_make = quote_for_wmake;
951 break;
952 case 'D':
953 case 'F':
954 case 'T':
955 case 'Q':
956 advance = true;
957 break;
958 default:
959 break;
960 }
961 } else {
H. Peter Anvin55568c12016-10-03 19:46:49 -0700962 switch (p[2]) {
963 case 0:
964 operating_mode = OP_DEPEND;
965 break;
966 case 'G':
967 operating_mode = OP_DEPEND;
968 depend_missing_ok = true;
969 break;
970 case 'P':
971 depend_emit_phony = true;
972 break;
973 case 'D':
974 operating_mode = OP_NORMAL;
975 depend_file = q;
976 advance = true;
977 break;
978 case 'F':
979 depend_file = q;
980 advance = true;
981 break;
982 case 'T':
983 depend_target = q;
984 advance = true;
985 break;
986 case 'Q':
987 depend_target = quote_for_make(q);
988 advance = true;
989 break;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700990 case 'W':
991 /* handled in pass 1 */
992 break;
H. Peter Anvin55568c12016-10-03 19:46:49 -0700993 default:
994 nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
995 "unknown dependency option `-M%c'", p[2]);
996 break;
997 }
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700998 }
999 if (advance && (!q || !q[0])) {
1000 nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
1001 "option `-M%c' requires a parameter", p[2]);
1002 break;
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001003 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001004 break;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001005
H. Peter Anvine2c80182005-01-15 22:15:51 +00001006 case '-':
1007 {
1008 int s;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001009
H. Peter Anvine2c80182005-01-15 22:15:51 +00001010 if (p[2] == 0) { /* -- => stop processing options */
1011 stopoptions = 1;
1012 break;
1013 }
Cyrill Gorcunove7438432014-05-09 22:34:34 +04001014
1015 if (!nasm_stricmp(p, "--v"))
1016 show_version();
1017
Andy Willis3f546032016-09-13 00:02:21 +03001018 if (!nasm_stricmp(p, "--version"))
1019 show_version();
1020
H. Peter Anvine2c80182005-01-15 22:15:51 +00001021 for (s = 0; textopts[s].label; s++) {
1022 if (!nasm_stricmp(p + 2, textopts[s].label)) {
1023 break;
1024 }
1025 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001026
H. Peter Anvine2c80182005-01-15 22:15:51 +00001027 switch (s) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001028 case OPT_PREFIX:
1029 case OPT_POSTFIX:
1030 {
1031 if (!q) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001032 nasm_error(ERR_NONFATAL | ERR_NOFILE |
H. Peter Anvine2c80182005-01-15 22:15:51 +00001033 ERR_USAGE,
1034 "option `--%s' requires an argument",
1035 p + 2);
1036 break;
1037 } else {
1038 advance = 1, param = q;
1039 }
1040
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +01001041 switch (s) {
1042 case OPT_PREFIX:
H. Peter Anvin55568c12016-10-03 19:46:49 -07001043 if (pass == 2)
1044 strlcpy(lprefix, param, PREFIX_MAX);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001045 break;
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +01001046 case OPT_POSTFIX:
H. Peter Anvin55568c12016-10-03 19:46:49 -07001047 if (pass == 2)
1048 strlcpy(lpostfix, param, POSTFIX_MAX);
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +01001049 break;
1050 default:
H. Peter Anvinac061332017-03-31 11:41:16 -07001051 panic();
H. Peter Anvine2c80182005-01-15 22:15:51 +00001052 break;
1053 }
1054 break;
1055 }
H. Peter Anvind24dd5f2016-02-08 10:32:13 -08001056
H. Peter Anvine2c80182005-01-15 22:15:51 +00001057 default:
1058 {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001059 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001060 "unrecognised option `--%s'", p + 2);
1061 break;
1062 }
1063 }
1064 break;
1065 }
1066
1067 default:
H. Peter Anvinac061332017-03-31 11:41:16 -07001068 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
1069 "unrecognised option `-%c'", p[1]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001070 break;
1071 }
H. Peter Anvin55568c12016-10-03 19:46:49 -07001072 } else if (pass == 2) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001073 if (*inname) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001074 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001075 "more than one input file specified");
H. Peter Anvindc242712007-11-18 11:55:10 -08001076 } else {
1077 copy_filename(inname, p);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001078 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001079 }
1080
1081 return advance;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001082}
1083
H. Peter Anvineba20a72002-04-30 20:53:55 +00001084#define ARG_BUF_DELTA 128
1085
H. Peter Anvin55568c12016-10-03 19:46:49 -07001086static void process_respfile(FILE * rfile, int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001087{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001088 char *buffer, *p, *q, *prevarg;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001089 int bufsize, prevargsize;
1090
1091 bufsize = prevargsize = ARG_BUF_DELTA;
1092 buffer = nasm_malloc(ARG_BUF_DELTA);
1093 prevarg = nasm_malloc(ARG_BUF_DELTA);
1094 prevarg[0] = '\0';
1095
H. Peter Anvine2c80182005-01-15 22:15:51 +00001096 while (1) { /* Loop to handle all lines in file */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001097 p = buffer;
1098 while (1) { /* Loop to handle long lines */
1099 q = fgets(p, bufsize - (p - buffer), rfile);
1100 if (!q)
1101 break;
1102 p += strlen(p);
1103 if (p > buffer && p[-1] == '\n')
1104 break;
1105 if (p - buffer > bufsize - 10) {
1106 int offset;
1107 offset = p - buffer;
1108 bufsize += ARG_BUF_DELTA;
1109 buffer = nasm_realloc(buffer, bufsize);
1110 p = buffer + offset;
1111 }
1112 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001113
H. Peter Anvine2c80182005-01-15 22:15:51 +00001114 if (!q && p == buffer) {
1115 if (prevarg[0])
H. Peter Anvin55568c12016-10-03 19:46:49 -07001116 process_arg(prevarg, NULL, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001117 nasm_free(buffer);
1118 nasm_free(prevarg);
1119 return;
1120 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001121
H. Peter Anvine2c80182005-01-15 22:15:51 +00001122 /*
1123 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
1124 * them are present at the end of the line.
1125 */
1126 *(p = &buffer[strcspn(buffer, "\r\n\032")]) = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001127
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001128 while (p > buffer && nasm_isspace(p[-1]))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001129 *--p = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001130
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +04001131 p = nasm_skip_spaces(buffer);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001132
H. Peter Anvin55568c12016-10-03 19:46:49 -07001133 if (process_arg(prevarg, p, pass))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001134 *p = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001135
Charles Crayne192d5b52007-10-18 19:02:42 -07001136 if ((int) strlen(p) > prevargsize - 10) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001137 prevargsize += ARG_BUF_DELTA;
1138 prevarg = nasm_realloc(prevarg, prevargsize);
1139 }
H. Peter Anvindc242712007-11-18 11:55:10 -08001140 strncpy(prevarg, p, prevargsize);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001141 }
1142}
1143
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001144/* Function to process args from a string of args, rather than the
1145 * argv array. Used by the environment variable and response file
1146 * processing.
1147 */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001148static void process_args(char *args, int pass)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001149{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001150 char *p, *q, *arg, *prevarg;
1151 char separator = ' ';
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001152
1153 p = args;
1154 if (*p && *p != '-')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001155 separator = *p++;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001156 arg = NULL;
1157 while (*p) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001158 q = p;
1159 while (*p && *p != separator)
1160 p++;
1161 while (*p == separator)
1162 *p++ = '\0';
1163 prevarg = arg;
1164 arg = q;
H. Peter Anvin55568c12016-10-03 19:46:49 -07001165 if (process_arg(prevarg, arg, pass))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001166 arg = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001167 }
1168 if (arg)
H. Peter Anvin55568c12016-10-03 19:46:49 -07001169 process_arg(arg, NULL, pass);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001170}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001171
H. Peter Anvin55568c12016-10-03 19:46:49 -07001172static void process_response_file(const char *file, int pass)
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001173{
1174 char str[2048];
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001175 FILE *f = nasm_open_read(file, NF_TEXT);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001176 if (!f) {
Cyrill Gorcunovf1964512013-02-15 12:14:06 +04001177 perror(file);
1178 exit(-1);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001179 }
1180 while (fgets(str, sizeof str, f)) {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001181 process_args(str, pass);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001182 }
1183 fclose(f);
1184}
1185
H. Peter Anvin55568c12016-10-03 19:46:49 -07001186static void parse_cmdline(int argc, char **argv, int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001187{
1188 FILE *rfile;
Cyrill Gorcunovf4941892011-07-17 13:55:25 +04001189 char *envreal, *envcopy = NULL, *p;
H. Peter Anvin972079f2008-09-30 17:01:23 -07001190 int i;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001191
Charles Craynefcce07f2007-09-30 22:15:36 -07001192 *inname = *outname = *listname = *errname = '\0';
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001193
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001194 /* Initialize all the warnings to their default state */
1195 for (i = 0; i < ERR_WARN_ALL; i++) {
1196 warning_state_init[i] = warning_state[i] =
1197 warnings[i].enabled ? WARN_ST_ENABLED : 0;
1198 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001199
1200 /*
H. Peter Anvinff7ccc02002-05-06 19:41:57 +00001201 * First, process the NASMENV environment variable.
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001202 */
H. Peter Anvinff7ccc02002-05-06 19:41:57 +00001203 envreal = getenv("NASMENV");
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001204 if (envreal) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001205 envcopy = nasm_strdup(envreal);
H. Peter Anvin55568c12016-10-03 19:46:49 -07001206 process_args(envcopy, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001207 nasm_free(envcopy);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001208 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001209
1210 /*
1211 * Now process the actual command line.
1212 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001213 while (--argc) {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001214 bool advance;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001215 argv++;
1216 if (argv[0][0] == '@') {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001217 /*
1218 * We have a response file, so process this as a set of
H. Peter Anvine2c80182005-01-15 22:15:51 +00001219 * arguments like the environment variable. This allows us
1220 * to have multiple arguments on a single line, which is
1221 * different to the -@resp file processing below for regular
1222 * NASM.
1223 */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001224 process_response_file(argv[0]+1, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001225 argc--;
1226 argv++;
1227 }
1228 if (!stopoptions && argv[0][0] == '-' && argv[0][1] == '@') {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001229 p = get_param(argv[0], argc > 1 ? argv[1] : NULL, &advance);
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001230 if (p) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001231 rfile = nasm_open_read(p, NF_TEXT);
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001232 if (rfile) {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001233 process_respfile(rfile, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001234 fclose(rfile);
1235 } else
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001236 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001237 "unable to open response file `%s'", p);
1238 }
1239 } else
H. Peter Anvin55568c12016-10-03 19:46:49 -07001240 advance = process_arg(argv[0], argc > 1 ? argv[1] : NULL, pass);
H. Peter Anvin423e3812007-11-15 17:12:29 -08001241 argv += advance, argc -= advance;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001242 }
1243
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001244 /*
1245 * Look for basic command line typos. This definitely doesn't
1246 * catch all errors, but it might help cases of fumbled fingers.
1247 */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001248 if (pass != 2)
1249 return;
1250
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001251 if (!*inname)
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001252 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001253 "no input file specified");
1254 else if (!strcmp(inname, errname) ||
1255 !strcmp(inname, outname) ||
1256 !strcmp(inname, listname) ||
1257 (depend_file && !strcmp(inname, depend_file)))
H. Peter Anvin41087062016-03-03 14:27:34 -08001258 nasm_fatal(ERR_NOFILE | ERR_USAGE,
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001259 "file `%s' is both input and output file",
1260 inname);
H. Peter Anvin59ddd262007-10-01 11:26:31 -07001261
H. Peter Anvin70653092007-10-19 14:42:29 -07001262 if (*errname) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001263 error_file = nasm_open_write(errname, NF_TEXT);
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001264 if (!error_file) {
1265 error_file = stderr; /* Revert to default! */
H. Peter Anvin41087062016-03-03 14:27:34 -08001266 nasm_fatal(ERR_NOFILE | ERR_USAGE,
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001267 "cannot open file `%s' for error messages",
1268 errname);
1269 }
Charles Craynefcce07f2007-09-30 22:15:36 -07001270 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001271}
1272
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001273static void assemble_file(char *fname, StrList **depend_ptr)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001274{
H. Peter Anvinc7131682017-03-07 17:45:01 -08001275 char *line;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001276 insn output_ins;
H. Peter Anvinc7131682017-03-07 17:45:01 -08001277 int i;
Charles Crayne5fbbc8c2007-11-07 19:03:46 -08001278 int64_t offs;
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001279 int pass_max;
H. Peter Anvin9c595b62017-03-07 19:44:21 -08001280 uint64_t prev_offset_changed;
1281 unsigned int stall_count = 0; /* Make sure we make forward progress... */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001282
Cyrill Gorcunov08359152013-11-09 22:16:11 +04001283 if (cmd_sb == 32 && iflag_ffs(&cmd_cpu) < IF_386)
H. Peter Anvin130736c2016-02-17 20:27:41 -08001284 nasm_fatal(0, "command line: 32-bit segment size requires a higher cpu");
H. Peter Anvineba20a72002-04-30 20:53:55 +00001285
Charles Craynec1905c22008-09-11 18:54:06 -07001286 pass_max = prev_offset_changed = (INT_MAX >> 1) + 2; /* Almost unlimited */
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001287 for (passn = 1; pass0 <= 2; passn++) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001288 ldfunc def_label;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001289
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001290 pass1 = pass0 == 2 ? 2 : 1; /* 1, 1, 1, ..., 1, 2 */
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001291 pass2 = passn > 1 ? 2 : 1; /* 1, 2, 2, ..., 2, 2 */
1292 /* pass0 0, 0, 0, ..., 1, 2 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00001293
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001294 def_label = passn > 1 ? redefine_label : define_label;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001295
H. Peter Anvincac0b192017-03-28 16:12:30 -07001296 globalbits = cmd_sb; /* set 'bits' to command line default */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001297 cpu = cmd_cpu;
1298 if (pass0 == 2) {
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08001299 lfmt->init(listname);
H. Peter Anvinaac01ff2017-04-02 19:10:26 -07001300 } else if (passn == 1 && *listname) {
1301 /* Remove the list file in case we die before the output pass */
1302 remove(listname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001303 }
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001304 in_absolute = false;
Charles Craynec1905c22008-09-11 18:54:06 -07001305 global_offset_changed = 0; /* set by redefine_label */
H. Peter Anvincac0b192017-03-28 16:12:30 -07001306 location.segment = ofmt->section(NULL, pass2, &globalbits);
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001307 if (passn > 1) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001308 saa_rewind(forwrefs);
1309 forwref = saa_rstruct(forwrefs);
1310 raa_free(offsets);
1311 offsets = raa_init();
1312 }
H. Peter Anvin130736c2016-02-17 20:27:41 -08001313 preproc->reset(fname, pass1, pass1 == 2 ? depend_ptr : NULL);
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001314
1315 /* Revert all warnings to the default state */
1316 memcpy(warning_state, warning_state_init, sizeof warning_state);
Victor van den Elzen819703a2008-07-16 15:20:56 +02001317
H. Peter Anvine2c80182005-01-15 22:15:51 +00001318 globallineno = 0;
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001319 if (passn == 1)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001320 location.known = true;
Cyrill Gorcunov190232f2013-02-15 12:35:04 +04001321 location.offset = offs = get_curr_offs();
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001322
H. Peter Anvine2c80182005-01-15 22:15:51 +00001323 while ((line = preproc->getline())) {
1324 globallineno++;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001325
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001326 /*
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001327 * Here we parse our directives; this is not handled by the
H. Peter Anvinc7131682017-03-07 17:45:01 -08001328 * main parser.
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001329 */
H. Peter Anvina6e26d92017-03-07 21:32:37 -08001330 if (process_directives(line))
H. Peter Anvinc7131682017-03-07 17:45:01 -08001331 goto end_of_line; /* Just do final cleanup */
H. Peter Anvin62b24d72007-08-29 16:38:05 +00001332
H. Peter Anvinc7131682017-03-07 17:45:01 -08001333 /* Not a directive, or even something that starts with [ */
1334
1335 parse_line(pass1, line, &output_ins, def_label);
1336
1337 if (optimizing > 0) {
1338 if (forwref != NULL && globallineno == forwref->lineno) {
1339 output_ins.forw_ref = true;
1340 do {
1341 output_ins.oprs[forwref->operand].opflags |= OPFLAG_FORWARD;
1342 forwref = saa_rstruct(forwrefs);
1343 } while (forwref != NULL
1344 && forwref->lineno == globallineno);
1345 } else
1346 output_ins.forw_ref = false;
1347
1348 if (output_ins.forw_ref) {
1349 if (passn == 1) {
1350 for (i = 0; i < output_ins.operands; i++) {
1351 if (output_ins.oprs[i].opflags & OPFLAG_FORWARD) {
1352 struct forwrefinfo *fwinf = (struct forwrefinfo *)saa_wstruct(forwrefs);
1353 fwinf->lineno = globallineno;
1354 fwinf->operand = i;
Cyrill Gorcunovd5f2aef2010-04-20 15:33:45 +04001355 }
1356 }
1357 }
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001358 }
H. Peter Anvinc7131682017-03-07 17:45:01 -08001359 }
H. Peter Anvin8bec2c72009-08-08 13:49:00 -07001360
H. Peter Anvinc7131682017-03-07 17:45:01 -08001361 /* forw_ref */
1362 if (output_ins.opcode == I_EQU) {
1363 if (pass1 == 1) {
1364 /*
1365 * Special `..' EQUs get processed in pass two,
1366 * except `..@' macro-processor EQUs which are done
1367 * in the normal place.
1368 */
1369 if (!output_ins.label)
1370 nasm_error(ERR_NONFATAL,
1371 "EQU not preceded by label");
1372
1373 else if (output_ins.label[0] != '.' ||
1374 output_ins.label[1] != '.' ||
1375 output_ins.label[2] == '@') {
1376 if (output_ins.operands == 1 &&
1377 (output_ins.oprs[0].type & IMMEDIATE) &&
1378 output_ins.oprs[0].wrt == NO_SEG) {
1379 bool isext = !!(output_ins.oprs[0].opflags & OPFLAG_EXTERN);
1380 def_label(output_ins.label,
1381 output_ins.oprs[0].segment,
1382 output_ins.oprs[0].offset, NULL,
1383 false, isext);
1384 } else if (output_ins.operands == 2
1385 && (output_ins.oprs[0].type & IMMEDIATE)
1386 && (output_ins.oprs[0].type & COLON)
1387 && output_ins.oprs[0].segment == NO_SEG
1388 && output_ins.oprs[0].wrt == NO_SEG
1389 && (output_ins.oprs[1].type & IMMEDIATE)
1390 && output_ins.oprs[1].segment == NO_SEG
1391 && output_ins.oprs[1].wrt == NO_SEG) {
1392 def_label(output_ins.label,
1393 output_ins.oprs[0].offset | SEG_ABS,
1394 output_ins.oprs[1].offset,
1395 NULL, false, false);
1396 } else
1397 nasm_error(ERR_NONFATAL,
1398 "bad syntax for EQU");
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001399 }
H. Peter Anvinc7131682017-03-07 17:45:01 -08001400 } else {
1401 /*
1402 * Special `..' EQUs get processed here, except
1403 * `..@' macro processor EQUs which are done above.
1404 */
1405 if (output_ins.label[0] == '.' &&
1406 output_ins.label[1] == '.' &&
1407 output_ins.label[2] != '@') {
1408 if (output_ins.operands == 1 &&
1409 (output_ins.oprs[0].type & IMMEDIATE)) {
1410 define_label(output_ins.label,
1411 output_ins.oprs[0].segment,
1412 output_ins.oprs[0].offset,
1413 NULL, false, false);
1414 } else if (output_ins.operands == 2
1415 && (output_ins.oprs[0].type & IMMEDIATE)
1416 && (output_ins.oprs[0].type & COLON)
1417 && output_ins.oprs[0].segment == NO_SEG
1418 && (output_ins.oprs[1].type & IMMEDIATE)
1419 && output_ins.oprs[1].segment == NO_SEG) {
1420 define_label(output_ins.label,
1421 output_ins.oprs[0].offset | SEG_ABS,
1422 output_ins.oprs[1].offset,
1423 NULL, false, false);
1424 } else
1425 nasm_error(ERR_NONFATAL,
1426 "bad syntax for EQU");
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001427 }
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001428 }
H. Peter Anvinc7131682017-03-07 17:45:01 -08001429 } else { /* instruction isn't an EQU */
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001430 int32_t n;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001431
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001432 nasm_assert(output_ins.times >= 0);
H. Peter Anvinc7131682017-03-07 17:45:01 -08001433
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001434 for (n = 1; n <= output_ins.times; n++) {
1435 if (pass1 == 1) {
1436 int64_t l = insn_size(location.segment, offs,
1437 globalbits, &output_ins);
1438
1439 /* if (using_debug_info) && output_ins.opcode != -1) */
1440 if (using_debug_info)
1441 { /* fbk 03/25/01 */
H. Peter Anvinc7131682017-03-07 17:45:01 -08001442 /* this is done here so we can do debug type info */
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001443 int32_t typeinfo =
1444 TYS_ELEMENTS(output_ins.operands);
1445 switch (output_ins.opcode) {
1446 case I_RESB:
1447 typeinfo =
1448 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_BYTE;
1449 break;
1450 case I_RESW:
1451 typeinfo =
1452 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_WORD;
1453 break;
1454 case I_RESD:
1455 typeinfo =
1456 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_DWORD;
1457 break;
1458 case I_RESQ:
1459 typeinfo =
1460 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_QWORD;
1461 break;
1462 case I_REST:
1463 typeinfo =
1464 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_TBYTE;
1465 break;
1466 case I_RESO:
1467 typeinfo =
1468 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_OWORD;
1469 break;
1470 case I_RESY:
1471 typeinfo =
1472 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_YWORD;
1473 break;
1474 case I_RESZ:
1475 typeinfo =
1476 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_ZWORD;
1477 break;
1478 case I_DB:
1479 typeinfo |= TY_BYTE;
1480 break;
1481 case I_DW:
1482 typeinfo |= TY_WORD;
1483 break;
1484 case I_DD:
1485 if (output_ins.eops_float)
1486 typeinfo |= TY_FLOAT;
1487 else
1488 typeinfo |= TY_DWORD;
1489 break;
1490 case I_DQ:
1491 typeinfo |= TY_QWORD;
1492 break;
1493 case I_DT:
1494 typeinfo |= TY_TBYTE;
1495 break;
1496 case I_DO:
1497 typeinfo |= TY_OWORD;
1498 break;
1499 case I_DY:
1500 typeinfo |= TY_YWORD;
1501 break;
1502 case I_DZ:
1503 typeinfo |= TY_ZWORD;
1504 break;
1505 default:
1506 typeinfo = TY_LABEL;
1507 break;
1508 }
H. Peter Anvinc7131682017-03-07 17:45:01 -08001509
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001510 dfmt->debug_typevalue(typeinfo);
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001511 }
H. Peter Anvinc7131682017-03-07 17:45:01 -08001512
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001513 /*
1514 * For INCBIN, let the code in assemble
1515 * handle TIMES, so we don't have to read the
1516 * input file over and over.
1517 */
1518 if (l != -1) {
1519 offs += l;
1520 set_curr_offs(offs);
1521 }
1522 /*
1523 * else l == -1 => invalid instruction, which will be
1524 * flagged as an error on pass 2
1525 */
1526 } else {
1527 if (n == 2)
1528 lfmt->uplevel(LIST_TIMES);
1529 offs += assemble(location.segment, offs,
1530 globalbits, &output_ins);
Cyrill Gorcunov190232f2013-02-15 12:35:04 +04001531 set_curr_offs(offs);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001532 }
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001533 } /* not an EQU */
1534 }
1535 if (output_ins.times > 1)
1536 lfmt->downlevel(LIST_TIMES);
H. Peter Anvinc7131682017-03-07 17:45:01 -08001537
H. Peter Anvinc7131682017-03-07 17:45:01 -08001538 cleanup_insn(&output_ins);
1539
1540 end_of_line:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001541 nasm_free(line);
Cyrill Gorcunov190232f2013-02-15 12:35:04 +04001542 location.offset = offs = get_curr_offs();
H. Peter Anvine2c80182005-01-15 22:15:51 +00001543 } /* end while (line = preproc->getline... */
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001544
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001545 if (pass0 == 2 && global_offset_changed && !terminate_after_phase)
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001546 nasm_error(ERR_NONFATAL,
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001547 "phase error detected at end of assembly.");
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001548
H. Peter Anvine2c80182005-01-15 22:15:51 +00001549 if (pass1 == 1)
1550 preproc->cleanup(1);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001551
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001552 if ((passn > 1 && !global_offset_changed) || pass0 == 2) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001553 pass0++;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001554 } else if (global_offset_changed &&
1555 global_offset_changed < prev_offset_changed) {
Charles Craynec1905c22008-09-11 18:54:06 -07001556 prev_offset_changed = global_offset_changed;
1557 stall_count = 0;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001558 } else {
1559 stall_count++;
1560 }
Victor van den Elzen42528232008-09-11 15:07:05 +02001561
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001562 if (terminate_after_phase)
1563 break;
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001564
H. Peter Anvin9c595b62017-03-07 19:44:21 -08001565 if ((stall_count > 997U) || (passn >= pass_max)) {
Victor van den Elzen42528232008-09-11 15:07:05 +02001566 /* We get here if the labels don't converge
1567 * Example: FOO equ FOO + 1
1568 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001569 nasm_error(ERR_NONFATAL,
Victor van den Elzen42528232008-09-11 15:07:05 +02001570 "Can't find valid values for all labels "
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001571 "after %d passes, giving up.", passn);
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001572 nasm_error(ERR_NONFATAL,
1573 "Possible causes: recursive EQUs, macro abuse.");
1574 break;
1575 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001576 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001577
H. Peter Anvine2c80182005-01-15 22:15:51 +00001578 preproc->cleanup(0);
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08001579 lfmt->cleanup();
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001580 if (!terminate_after_phase && opt_verbose_info) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001581 /* -On and -Ov switches */
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001582 fprintf(stdout, "info: assembly required 1+%d+1 passes\n", passn-3);
1583 }
1584}
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001585
Ed Berosetfa771012002-06-09 20:56:40 +00001586/**
1587 * gnu style error reporting
1588 * This function prints an error message to error_file in the
1589 * style used by GNU. An example would be:
1590 * file.asm:50: error: blah blah blah
H. Peter Anvin70653092007-10-19 14:42:29 -07001591 * where file.asm is the name of the file, 50 is the line number on
Ed Berosetfa771012002-06-09 20:56:40 +00001592 * which the error occurs (or is detected) and "error:" is one of
1593 * the possible optional diagnostics -- it can be "error" or "warning"
H. Peter Anvin70653092007-10-19 14:42:29 -07001594 * or something else. Finally the line terminates with the actual
Ed Berosetfa771012002-06-09 20:56:40 +00001595 * error message.
H. Peter Anvin70653092007-10-19 14:42:29 -07001596 *
1597 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001598 * @param fmt the printf style format string
1599 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001600static void nasm_verror_gnu(int severity, const char *fmt, va_list ap)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001601{
H. Peter Anvin274cda82016-05-10 02:56:29 -07001602 const char *currentfile = NULL;
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001603 int32_t lineno = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001604
Ed Berosetfa771012002-06-09 20:56:40 +00001605 if (is_suppressed_warning(severity))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001606 return;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001607
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001608 if (!(severity & ERR_NOFILE))
H. Peter Anvin4def1a82016-05-09 13:59:44 -07001609 src_get(&lineno, &currentfile);
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001610
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001611 if (!skip_this_pass(severity)) {
1612 if (currentfile) {
1613 fprintf(error_file, "%s:%"PRId32": ", currentfile, lineno);
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001614 } else {
1615 fputs("nasm: ", error_file);
1616 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001617 }
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001618
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001619 nasm_verror_common(severity, fmt, ap);
Ed Berosetfa771012002-06-09 20:56:40 +00001620}
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001621
Ed Berosetfa771012002-06-09 20:56:40 +00001622/**
1623 * MS style error reporting
1624 * This function prints an error message to error_file in the
H. Peter Anvin70653092007-10-19 14:42:29 -07001625 * style used by Visual C and some other Microsoft tools. An example
Ed Berosetfa771012002-06-09 20:56:40 +00001626 * would be:
Ed Beroset6e61d0d2002-06-11 03:29:36 +00001627 * file.asm(50) : error: blah blah blah
H. Peter Anvin70653092007-10-19 14:42:29 -07001628 * where file.asm is the name of the file, 50 is the line number on
Ed Beroset6e61d0d2002-06-11 03:29:36 +00001629 * which the error occurs (or is detected) and "error:" is one of
1630 * the possible optional diagnostics -- it can be "error" or "warning"
H. Peter Anvin70653092007-10-19 14:42:29 -07001631 * or something else. Finally the line terminates with the actual
Ed Beroset6e61d0d2002-06-11 03:29:36 +00001632 * error message.
H. Peter Anvin70653092007-10-19 14:42:29 -07001633 *
1634 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001635 * @param fmt the printf style format string
1636 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001637static void nasm_verror_vc(int severity, const char *fmt, va_list ap)
Ed Berosetfa771012002-06-09 20:56:40 +00001638{
H. Peter Anvin274cda82016-05-10 02:56:29 -07001639 const char *currentfile = NULL;
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001640 int32_t lineno = 0;
Ed Berosetfa771012002-06-09 20:56:40 +00001641
H. Peter Anvine2c80182005-01-15 22:15:51 +00001642 if (is_suppressed_warning(severity))
1643 return;
Ed Berosetfa771012002-06-09 20:56:40 +00001644
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001645 if (!(severity & ERR_NOFILE))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001646 src_get(&lineno, &currentfile);
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001647
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001648 if (!skip_this_pass(severity)) {
1649 if (currentfile) {
1650 fprintf(error_file, "%s(%"PRId32") : ", currentfile, lineno);
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001651 } else {
1652 fputs("nasm: ", error_file);
1653 }
Ed Berosetfa771012002-06-09 20:56:40 +00001654 }
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001655
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001656 nasm_verror_common(severity, fmt, ap);
Ed Berosetfa771012002-06-09 20:56:40 +00001657}
1658
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001659/*
1660 * check to see if this is a suppressable warning
1661 */
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001662static inline bool is_valid_warning(int severity)
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001663{
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001664 /* Not a warning at all */
1665 if ((severity & ERR_MASK) != ERR_WARNING)
1666 return false;
1667
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001668 return WARN_IDX(severity) < ERR_WARN_ALL;
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001669}
1670
Ed Berosetfa771012002-06-09 20:56:40 +00001671/**
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001672 * check for suppressed warning
H. Peter Anvin70653092007-10-19 14:42:29 -07001673 * checks for suppressed warning or pass one only warning and we're
Ed Berosetfa771012002-06-09 20:56:40 +00001674 * not in pass 1
1675 *
1676 * @param severity the severity of the warning or error
1677 * @return true if we should abort error/warning printing
1678 */
H. Peter Anvin2b046cf2008-01-22 14:08:36 -08001679static bool is_suppressed_warning(int severity)
Ed Berosetfa771012002-06-09 20:56:40 +00001680{
H. Peter Anvin442a05a2012-02-24 21:50:53 -08001681 /* Might be a warning but suppresed explicitly */
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001682 if (is_valid_warning(severity))
1683 return !(warning_state[WARN_IDX(severity)] & WARN_ST_ENABLED);
1684 else
1685 return false;
1686}
1687
1688static bool warning_is_error(int severity)
1689{
1690 if (is_valid_warning(severity))
1691 return !!(warning_state[WARN_IDX(severity)] & WARN_ST_ERROR);
H. Peter Anvin442a05a2012-02-24 21:50:53 -08001692 else
1693 return false;
Ed Berosetfa771012002-06-09 20:56:40 +00001694}
1695
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001696static bool skip_this_pass(int severity)
1697{
H. Peter Anvin8f622462017-04-02 19:02:29 -07001698 /*
1699 * See if it's a pass-specific error or warning which should be skipped.
1700 * We cannot skip errors stronger than ERR_NONFATAL as by definition
1701 * they cannot be resumed from.
1702 */
1703 if ((severity & ERR_MASK) > ERR_NONFATAL)
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001704 return false;
1705
H. Peter Anvin934f0472016-05-09 12:00:19 -07001706 /*
1707 * passn is 1 on the very first pass only.
1708 * pass0 is 2 on the code-generation (final) pass only.
1709 * These are the passes we care about in this case.
1710 */
1711 return (((severity & ERR_PASS1) && passn != 1) ||
1712 ((severity & ERR_PASS2) && pass0 != 2));
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001713}
1714
Ed Berosetfa771012002-06-09 20:56:40 +00001715/**
1716 * common error reporting
1717 * This is the common back end of the error reporting schemes currently
H. Peter Anvin70653092007-10-19 14:42:29 -07001718 * implemented. It prints the nature of the warning and then the
Ed Berosetfa771012002-06-09 20:56:40 +00001719 * specific error message to error_file and may or may not return. It
1720 * doesn't return if the error severity is a "panic" or "debug" type.
H. Peter Anvin70653092007-10-19 14:42:29 -07001721 *
1722 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001723 * @param fmt the printf style format string
1724 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001725static void nasm_verror_common(int severity, const char *fmt, va_list args)
Ed Berosetfa771012002-06-09 20:56:40 +00001726{
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001727 char msg[1024];
1728 const char *pfx;
H. Peter Anvin323fcff2009-07-12 12:04:56 -07001729
H. Peter Anvin7df04172008-06-10 18:27:38 -07001730 switch (severity & (ERR_MASK|ERR_NO_SEVERITY)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001731 case ERR_WARNING:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001732 pfx = "warning: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001733 break;
1734 case ERR_NONFATAL:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001735 pfx = "error: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001736 break;
1737 case ERR_FATAL:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001738 pfx = "fatal: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001739 break;
1740 case ERR_PANIC:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001741 pfx = "panic: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001742 break;
1743 case ERR_DEBUG:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001744 pfx = "debug: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001745 break;
H. Peter Anvin7df04172008-06-10 18:27:38 -07001746 default:
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001747 pfx = "";
1748 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001749 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00001750
H. Peter Anvin934f0472016-05-09 12:00:19 -07001751 vsnprintf(msg, sizeof msg - 64, fmt, args);
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001752 if (is_valid_warning(severity) && WARN_IDX(severity) != ERR_WARN_OTHER) {
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001753 char *p = strchr(msg, '\0');
H. Peter Anvin934f0472016-05-09 12:00:19 -07001754 snprintf(p, 64, " [-w+%s]", warnings[WARN_IDX(severity)].name);
1755 }
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001756
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001757 if (!skip_this_pass(severity))
1758 fprintf(error_file, "%s%s\n", pfx, msg);
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001759
H. Peter Anvin4def1a82016-05-09 13:59:44 -07001760 /* Are we recursing from error_list_macros? */
1761 if (severity & ERR_PP_LISTMACRO)
1762 return;
1763
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001764 /*
1765 * Don't suppress this with skip_this_pass(), or we don't get
H. Peter Anvin934f0472016-05-09 12:00:19 -07001766 * pass1 or preprocessor warnings in the list file
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001767 */
H. Peter Anvin0fcb4882016-07-06 11:55:25 -07001768 lfmt->error(severity, pfx, msg);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001769
H. Peter Anvin8f622462017-04-02 19:02:29 -07001770 if (skip_this_pass(severity))
1771 return;
1772
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001773 if (severity & ERR_USAGE)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001774 want_usage = true;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001775
H. Peter Anvin4def1a82016-05-09 13:59:44 -07001776 preproc->error_list_macros(severity);
1777
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001778 switch (severity & ERR_MASK) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001779 case ERR_DEBUG:
1780 /* no further action, by definition */
1781 break;
H. Peter Anvinb030c922007-11-13 11:31:15 -08001782 case ERR_WARNING:
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001783 /* Treat warnings as errors */
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001784 if (warning_is_error(severity))
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001785 terminate_after_phase = true;
1786 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001787 case ERR_NONFATAL:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001788 terminate_after_phase = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001789 break;
1790 case ERR_FATAL:
1791 if (ofile) {
1792 fclose(ofile);
1793 remove(outname);
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001794 ofile = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001795 }
1796 if (want_usage)
1797 usage();
1798 exit(1); /* instantly die */
1799 break; /* placate silly compilers */
1800 case ERR_PANIC:
1801 fflush(NULL);
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001802 /* abort(); */ /* halt, catch fire, and dump core */
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001803 if (ofile) {
1804 fclose(ofile);
1805 remove(outname);
1806 ofile = NULL;
1807 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001808 exit(3);
1809 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001810 }
1811}
1812
H. Peter Anvin734b1882002-04-30 21:01:08 +00001813static void usage(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001814{
H. Peter Anvin620515a2002-04-30 20:57:38 +00001815 fputs("type `nasm -h' for help\n", error_file);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001816}