blob: ecab73be124da8a4e0fbb498b6e3cc024154aa84 [file] [log] [blame]
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07001/* ----------------------------------------------------------------------- *
H. Peter Anvin323fcff2009-07-12 12:04:56 -07002 *
H. Peter Anvin3366e312018-02-07 14:14:36 -08003 * Copyright 1996-2018 The NASM Authors - All Rights Reserved
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07004 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00006 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07007 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
H. Peter Anvin323fcff2009-07-12 12:04:56 -070017 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -070018 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 * ----------------------------------------------------------------------- */
33
H. Peter Anvin323fcff2009-07-12 12:04:56 -070034/*
H. Peter Anvin9e6747c2009-06-28 17:13:04 -070035 * The Netwide Assembler main program module
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000036 */
37
H. Peter Anvinfe501952007-10-02 21:53:51 -070038#include "compiler.h"
39
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000040#include <stdio.h>
41#include <stdarg.h>
42#include <stdlib.h>
43#include <string.h>
44#include <ctype.h>
H. Peter Anvinfd7dd112007-10-10 14:06:59 -070045#include <limits.h>
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000046
47#include "nasm.h"
48#include "nasmlib.h"
H. Peter Anvinb20bc732017-03-07 19:23:03 -080049#include "error.h"
H. Peter Anvin1803ded2008-06-09 17:32:43 -070050#include "saa.h"
H. Peter Anvinfcb89092008-06-09 17:40:16 -070051#include "raa.h"
H. Peter Anvinf6c9e652007-10-16 14:40:27 -070052#include "float.h"
H. Peter Anvin74cc5e52007-08-30 22:35:34 +000053#include "stdscan.h"
H. Peter Anvinaf535c12002-04-30 20:59:21 +000054#include "insns.h"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000055#include "preproc.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000056#include "parser.h"
H. Peter Anvin76690a12002-04-30 20:52:49 +000057#include "eval.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000058#include "assemble.h"
59#include "labels.h"
H. Peter Anvine1f985c2016-05-25 12:06:29 -070060#include "outform.h"
H. Peter Anvin6768eb72002-04-30 20:52:26 +000061#include "listing.h"
Cyrill Gorcunov08359152013-11-09 22:16:11 +040062#include "iflag.h"
H. Peter Anvin2bc0ab32016-03-08 02:17:36 -080063#include "ver.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000064
H. Peter Anvin31387b22010-07-15 18:28:52 -070065/*
66 * This is the maximum number of optimization passes to do. If we ever
67 * find a case where the optimizer doesn't naturally converge, we might
68 * have to drop this value so the assembler doesn't appear to just hang.
69 */
70#define MAX_OPTIMIZE (INT_MAX >> 1)
71
H. Peter Anvine2c80182005-01-15 22:15:51 +000072struct forwrefinfo { /* info held on forward refs. */
H. Peter Anvineba20a72002-04-30 20:53:55 +000073 int lineno;
74 int operand;
75};
76
H. Peter Anvin55568c12016-10-03 19:46:49 -070077static void parse_cmdline(int, char **, int);
H. Peter Anvin81b62b92017-12-20 13:33:49 -080078static void assemble_file(const char *, StrList **);
H. Peter Anvin130736c2016-02-17 20:27:41 -080079static bool is_suppressed_warning(int severity);
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -080080static bool skip_this_pass(int severity);
H. Peter Anvin9bd15062009-07-18 21:07:17 -040081static void nasm_verror_gnu(int severity, const char *fmt, va_list args);
82static void nasm_verror_vc(int severity, const char *fmt, va_list args);
83static void nasm_verror_common(int severity, const char *fmt, va_list args);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000084static void usage(void);
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 Anvin3366e312018-02-07 14:14:36 -080089#ifndef ABORT_ON_PANIC
90# define ABORT_ON_PANIC 0
91#endif
92static bool abort_on_panic = ABORT_ON_PANIC;
93
H. Peter Anvin6867acc2007-10-10 14:58:45 -070094bool tasm_compatible_mode = false;
H. Peter Anvin00835fe2008-01-08 23:03:57 -080095int pass0, passn;
H. Peter Anvinc7131682017-03-07 17:45:01 -080096static int pass1, pass2; /* XXX: Get rid of these, they are redundant */
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +000097int globalrel = 0;
Jin Kyu Songb287ff02013-12-04 20:05:55 -080098int globalbnd = 0;
H. Peter Anvineba20a72002-04-30 20:53:55 +000099
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700100struct compile_time official_compile_time;
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800101
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800102const char *inname;
103const char *outname;
104static const char *listname;
105static const char *errname;
106
H. Peter Anvine2c80182005-01-15 22:15:51 +0000107static int globallineno; /* for forward-reference tracking */
Chang S. Baef0ceb1e2018-05-02 08:07:53 -0700108#define GLOBALLINENO_MAX INT32_MAX
109
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000110/* static int pass = 0; */
H. Peter Anvin338656c2016-02-17 20:59:22 -0800111const struct ofmt *ofmt = &OF_DEFAULT;
112const struct ofmt_alias *ofmt_alias = NULL;
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400113const struct dfmt *dfmt;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000114
H. Peter Anvine2c80182005-01-15 22:15:51 +0000115static FILE *error_file; /* Where to write error messages */
H. Peter Anvin620515a2002-04-30 20:57:38 +0000116
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400117FILE *ofile = NULL;
H. Peter Anvin31387b22010-07-15 18:28:52 -0700118int optimizing = MAX_OPTIMIZE; /* number of optimization passes to take */
Martin Lindhe8cc93f52016-11-16 16:48:13 +0100119static int cmd_sb = 16; /* by default */
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400120
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800121iflag_t cpu;
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400122static iflag_t cmd_cpu;
123
H. Peter Anvincd7893d2016-02-18 01:25:46 -0800124struct location location;
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800125bool in_absolute; /* Flag we are in ABSOLUTE seg */
126struct location absolute; /* Segment/offset inside ABSOLUTE */
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000127
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000128static struct RAA *offsets;
H. Peter Anvinea838272002-04-30 20:51:53 +0000129
H. Peter Anvine2c80182005-01-15 22:15:51 +0000130static struct SAA *forwrefs; /* keep track of forward references */
H. Peter Anvin9d637df2007-10-04 13:42:56 -0700131static const struct forwrefinfo *forwref;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000132
H. Peter Anvine7469712016-02-18 02:20:59 -0800133static const struct preproc_ops *preproc;
Cyrill Gorcunov86b2ad02011-07-01 10:38:25 +0400134
Cyrill Gorcunove9fc88c2014-06-23 02:22:02 +0400135#define OP_NORMAL (1u << 0)
136#define OP_PREPROCESS (1u << 1)
137#define OP_DEPEND (1u << 2)
138
139static unsigned int operating_mode;
Cyrill Gorcunov3ed32cb2014-06-22 20:53:12 +0400140
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700141/* Dependency flags */
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700142static bool depend_emit_phony = false;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700143static bool depend_missing_ok = false;
144static const char *depend_target = NULL;
145static const char *depend_file = NULL;
H. Peter Anvina771be82017-08-16 14:53:18 -0700146StrList *depend_list;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000147
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700148static bool want_usage;
149static bool terminate_after_phase;
H. Peter Anvin130736c2016-02-17 20:27:41 -0800150bool user_nolist = false;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000151
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700152static char *quote_for_pmake(const char *str);
153static char *quote_for_wmake(const char *str);
154static char *(*quote_for_make)(const char *) = quote_for_pmake;
H. Peter Anvin55340992012-09-09 17:09:00 -0700155
Cyrill Gorcunov190232f2013-02-15 12:35:04 +0400156static int64_t get_curr_offs(void)
157{
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800158 return in_absolute ? absolute.offset : raa_read(offsets, location.segment);
Cyrill Gorcunov190232f2013-02-15 12:35:04 +0400159}
160
161static void set_curr_offs(int64_t l_off)
162{
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800163 if (in_absolute)
164 absolute.offset = l_off;
Cyrill Gorcunov190232f2013-02-15 12:35:04 +0400165 else
166 offsets = raa_write(offsets, location.segment, l_off);
167}
168
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000169static void nasm_fputs(const char *line, FILE * outfile)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000170{
H. Peter Anvin310b3e12002-05-14 22:38:55 +0000171 if (outfile) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000172 fputs(line, outfile);
H. Peter Anvind1fb15c2007-11-13 09:37:59 -0800173 putc('\n', outfile);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000174 } else
H. Peter Anvine2c80182005-01-15 22:15:51 +0000175 puts(line);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000176}
177
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800178static void define_macros_early(void)
179{
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700180 const struct compile_time * const oct = &official_compile_time;
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800181 char temp[128];
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800182
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700183 if (oct->have_local) {
184 strftime(temp, sizeof temp, "__DATE__=\"%Y-%m-%d\"", &oct->local);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400185 preproc->pre_define(temp);
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700186 strftime(temp, sizeof temp, "__DATE_NUM__=%Y%m%d", &oct->local);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400187 preproc->pre_define(temp);
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700188 strftime(temp, sizeof temp, "__TIME__=\"%H:%M:%S\"", &oct->local);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400189 preproc->pre_define(temp);
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700190 strftime(temp, sizeof temp, "__TIME_NUM__=%H%M%S", &oct->local);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400191 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800192 }
193
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700194 if (oct->have_gm) {
195 strftime(temp, sizeof temp, "__UTC_DATE__=\"%Y-%m-%d\"", &oct->gm);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400196 preproc->pre_define(temp);
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700197 strftime(temp, sizeof temp, "__UTC_DATE_NUM__=%Y%m%d", &oct->gm);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400198 preproc->pre_define(temp);
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700199 strftime(temp, sizeof temp, "__UTC_TIME__=\"%H:%M:%S\"", &oct->gm);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400200 preproc->pre_define(temp);
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700201 strftime(temp, sizeof temp, "__UTC_TIME_NUM__=%H%M%S", &oct->gm);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400202 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800203 }
H. Peter Anvind85d2502008-05-04 17:53:31 -0700204
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700205 if (oct->have_posix) {
206 snprintf(temp, sizeof temp, "__POSIX_TIME__=%"PRId64, oct->posix);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400207 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800208 }
209}
210
211static void define_macros_late(void)
212{
213 char temp[128];
214
Cyrill Gorcunovc1936da2011-04-06 18:32:15 +0400215 /*
216 * In case if output format is defined by alias
217 * we have to put shortname of the alias itself here
218 * otherwise ABI backward compatibility gets broken.
219 */
Cyrill Gorcunov69ce7502010-07-12 15:19:17 +0400220 snprintf(temp, sizeof(temp), "__OUTPUT_FORMAT__=%s",
Cyrill Gorcunovc1936da2011-04-06 18:32:15 +0400221 ofmt_alias ? ofmt_alias->shortname : ofmt->shortname);
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +0400222 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800223}
224
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700225static void emit_dependencies(StrList *list)
226{
227 FILE *deps;
228 int linepos, len;
229 StrList *l, *nl;
H. Peter Anvinf05034f2017-08-16 22:08:36 -0700230 bool wmake = (quote_for_make == quote_for_wmake);
231 const char *wrapstr, *nulltarget;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700232
H. Peter Anvinf05034f2017-08-16 22:08:36 -0700233 wrapstr = wmake ? " &\n " : " \\\n ";
234 nulltarget = wmake ? "\t%null\n" : "";
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700235
236 if (depend_file && strcmp(depend_file, "-")) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -0700237 deps = nasm_open_write(depend_file, NF_TEXT);
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400238 if (!deps) {
239 nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
240 "unable to write dependency file `%s'", depend_file);
241 return;
242 }
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700243 } else {
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400244 deps = stdout;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700245 }
H. Peter Anvin323fcff2009-07-12 12:04:56 -0700246
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700247 linepos = fprintf(deps, "%s :", depend_target);
Cyrill Gorcunovfcd0a742009-07-27 16:26:26 +0400248 list_for_each(l, list) {
H. Peter Anvin55340992012-09-09 17:09:00 -0700249 char *file = quote_for_make(l->str);
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400250 len = strlen(file);
251 if (linepos + len > 62 && linepos > 1) {
H. Peter Anvinf05034f2017-08-16 22:08:36 -0700252 fputs(wrapstr, deps);
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400253 linepos = 1;
254 }
255 fprintf(deps, " %s", file);
256 linepos += len+1;
H. Peter Anvin55340992012-09-09 17:09:00 -0700257 nasm_free(file);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700258 }
259 fprintf(deps, "\n\n");
H. Peter Anvin323fcff2009-07-12 12:04:56 -0700260
Cyrill Gorcunovfcd0a742009-07-27 16:26:26 +0400261 list_for_each_safe(l, nl, list) {
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700262 if (depend_emit_phony) {
263 char *file = quote_for_make(l->str);
H. Peter Anvinf05034f2017-08-16 22:08:36 -0700264 fprintf(deps, "%s :\n%s\n", file, nulltarget);
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700265 nasm_free(file);
266 }
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400267 nasm_free(l);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700268 }
269
270 if (deps != stdout)
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400271 fclose(deps);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700272}
273
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700274/* Convert a struct tm to a POSIX-style time constant */
275static int64_t make_posix_time(const struct tm *tm)
276{
277 int64_t t;
278 int64_t y = tm->tm_year;
279
280 /* See IEEE 1003.1:2004, section 4.14 */
281
282 t = (y-70)*365 + (y-69)/4 - (y-1)/100 + (y+299)/400;
283 t += tm->tm_yday;
284 t *= 24;
285 t += tm->tm_hour;
286 t *= 60;
287 t += tm->tm_min;
288 t *= 60;
289 t += tm->tm_sec;
290
291 return t;
292}
293
294static void timestamp(void)
295{
296 struct compile_time * const oct = &official_compile_time;
297 const struct tm *tp, *best_gm;
298
299 time(&oct->t);
300
301 best_gm = NULL;
302
303 tp = localtime(&oct->t);
304 if (tp) {
305 oct->local = *tp;
306 best_gm = &oct->local;
307 oct->have_local = true;
308 }
309
310 tp = gmtime(&oct->t);
311 if (tp) {
312 oct->gm = *tp;
313 best_gm = &oct->gm;
314 oct->have_gm = true;
315 if (!oct->have_local)
316 oct->local = oct->gm;
317 } else {
318 oct->gm = oct->local;
319 }
320
321 if (best_gm) {
322 oct->posix = make_posix_time(best_gm);
323 oct->have_posix = true;
324 }
325}
326
H. Peter Anvin038d8612007-04-12 16:54:50 +0000327int main(int argc, char **argv)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000328{
H. Peter Anvina771be82017-08-16 14:53:18 -0700329 StrList **depend_ptr;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700330
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700331 timestamp();
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800332
H. Peter Anvina7ecf262018-02-06 14:43:07 -0800333 iflag_set_default_cpu(&cpu);
334 iflag_set_default_cpu(&cmd_cpu);
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400335
Charles Crayne2581c862008-09-10 19:21:52 -0700336 pass0 = 0;
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700337 want_usage = terminate_after_phase = false;
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400338 nasm_set_verror(nasm_verror_gnu);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000339
H. Peter Anvin97e15752007-09-25 08:47:47 -0700340 error_file = stderr;
341
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -0700342 tolower_init();
H. Peter Anvin274cda82016-05-10 02:56:29 -0700343 src_init();
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -0700344
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000345 offsets = raa_init();
Keith Kaniosb7a89542007-04-12 02:40:54 +0000346 forwrefs = saa_init((int32_t)sizeof(struct forwrefinfo));
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000347
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000348 preproc = &nasmpp;
Cyrill Gorcunov3ed32cb2014-06-22 20:53:12 +0400349 operating_mode = OP_NORMAL;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000350
H. Peter Anvin55568c12016-10-03 19:46:49 -0700351 parse_cmdline(argc, argv, 1);
352 if (terminate_after_phase) {
353 if (want_usage)
354 usage();
355 return 1;
356 }
357
H. Peter Anvin169ac7c2016-09-25 17:08:05 -0700358 /*
359 * Define some macros dependent on the runtime, but not
H. Peter Anvin55568c12016-10-03 19:46:49 -0700360 * on the command line (as those are scanned in cmdline pass 2.)
H. Peter Anvin169ac7c2016-09-25 17:08:05 -0700361 */
362 preproc->init();
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800363 define_macros_early();
364
H. Peter Anvin55568c12016-10-03 19:46:49 -0700365 parse_cmdline(argc, argv, 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000366 if (terminate_after_phase) {
367 if (want_usage)
368 usage();
369 return 1;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000370 }
371
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800372 /* Save away the default state of warnings */
373 memcpy(warning_state_init, warning_state, sizeof warning_state);
374
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800375 if (!using_debug_info) {
376 /* No debug info, redirect to the null backend (empty stubs) */
H. Peter Anvina7bc15d2016-02-17 20:55:08 -0800377 dfmt = &null_debug_form;
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800378 } else if (!debug_format) {
379 /* Default debug format for this backend */
H. Peter Anvina7bc15d2016-02-17 20:55:08 -0800380 dfmt = ofmt->default_dfmt;
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800381 } else {
382 dfmt = dfmt_find(ofmt, debug_format);
383 if (!dfmt) {
384 nasm_fatal(ERR_NOFILE | ERR_USAGE,
385 "unrecognized debug format `%s' for"
386 " output format `%s'",
387 debug_format, ofmt->shortname);
388 }
389 }
H. Peter Anvinbb88d012003-09-10 23:34:23 +0000390
H. Peter Anvin76690a12002-04-30 20:52:49 +0000391 if (ofmt->stdmac)
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +0400392 preproc->extra_stdmac(ofmt->stdmac);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000393
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800394 /* no output file name? */
395 if (!outname)
396 outname = filename_set_extension(inname, ofmt->extension);
397
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000398 /* define some macros dependent of command-line */
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800399 define_macros_late();
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000400
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400401 depend_ptr = (depend_file || (operating_mode & OP_DEPEND)) ? &depend_list : NULL;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700402
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700403 if (!depend_target)
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400404 depend_target = quote_for_make(outname);
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700405
Cyrill Gorcunove9fc88c2014-06-23 02:22:02 +0400406 if (operating_mode & OP_DEPEND) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000407 char *line;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700408
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400409 if (depend_missing_ok)
410 preproc->include_path(NULL); /* "assume generated" */
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700411
H. Peter Anvin130736c2016-02-17 20:27:41 -0800412 preproc->reset(inname, 0, depend_ptr);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000413 ofile = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000414 while ((line = preproc->getline()))
415 nasm_free(line);
416 preproc->cleanup(0);
Cyrill Gorcunove9fc88c2014-06-23 02:22:02 +0400417 } else if (operating_mode & OP_PREPROCESS) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000418 char *line;
H. Peter Anvin274cda82016-05-10 02:56:29 -0700419 const char *file_name = NULL;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000420 int32_t prior_linnum = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000421 int lineinc = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000422
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800423 if (outname) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -0700424 ofile = nasm_open_write(outname, NF_TEXT);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000425 if (!ofile)
H. Peter Anvin41087062016-03-03 14:27:34 -0800426 nasm_fatal(ERR_NOFILE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000427 "unable to open output file `%s'",
428 outname);
429 } else
430 ofile = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000431
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700432 location.known = false;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000433
Cyrill Gorcunovb574b072011-12-05 01:56:40 +0400434 /* pass = 1; */
H. Peter Anvin130736c2016-02-17 20:27:41 -0800435 preproc->reset(inname, 3, depend_ptr);
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800436
437 /* Revert all warnings to the default state */
438 memcpy(warning_state, warning_state_init, sizeof warning_state);
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700439
H. Peter Anvine2c80182005-01-15 22:15:51 +0000440 while ((line = preproc->getline())) {
441 /*
442 * We generate %line directives if needed for later programs
443 */
Keith Kaniosb7a89542007-04-12 02:40:54 +0000444 int32_t linnum = prior_linnum += lineinc;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000445 int altline = src_get(&linnum, &file_name);
446 if (altline) {
447 if (altline == 1 && lineinc == 1)
448 nasm_fputs("", ofile);
449 else {
450 lineinc = (altline != -1 || lineinc != 1);
451 fprintf(ofile ? ofile : stdout,
Keith Kanios93f2e9a2007-04-14 00:10:59 +0000452 "%%line %"PRId32"+%d %s\n", linnum, lineinc,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000453 file_name);
454 }
455 prior_linnum = linnum;
456 }
457 nasm_fputs(line, ofile);
458 nasm_free(line);
459 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000460 preproc->cleanup(0);
461 if (ofile)
462 fclose(ofile);
463 if (ofile && terminate_after_phase)
464 remove(outname);
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400465 ofile = NULL;
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400466 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000467
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400468 if (operating_mode & OP_NORMAL) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -0700469 ofile = nasm_open_write(outname, (ofmt->flags & OFMT_TEXT) ? NF_TEXT : NF_BINARY);
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400470 if (!ofile)
H. Peter Anvin41087062016-03-03 14:27:34 -0800471 nasm_fatal(ERR_NOFILE,
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400472 "unable to open output file `%s'", outname);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000473
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400474 /*
475 * We must call init_labels() before ofmt->init() since
476 * some object formats will want to define labels in their
477 * init routines. (eg OS/2 defines the FLAT group)
478 */
479 init_labels();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000480
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400481 ofmt->init();
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400482 dfmt->init();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000483
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400484 assemble_file(inname, depend_ptr);
Victor van den Elzenc82c3722008-06-04 15:24:20 +0200485
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400486 if (!terminate_after_phase) {
H. Peter Anvin477ae442016-03-07 22:53:06 -0800487 ofmt->cleanup();
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400488 cleanup_labels();
489 fflush(ofile);
H. Peter Anvin274cda82016-05-10 02:56:29 -0700490 if (ferror(ofile)) {
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400491 nasm_error(ERR_NONFATAL|ERR_NOFILE,
492 "write error on output file `%s'", outname);
H. Peter Anvin274cda82016-05-10 02:56:29 -0700493 terminate_after_phase = true;
494 }
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400495 }
496
497 if (ofile) {
498 fclose(ofile);
499 if (terminate_after_phase)
500 remove(outname);
501 ofile = NULL;
502 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000503 }
H. Peter Anvin734b1882002-04-30 21:01:08 +0000504
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700505 if (depend_list && !terminate_after_phase)
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400506 emit_dependencies(depend_list);
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700507
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000508 if (want_usage)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000509 usage();
H. Peter Anvin734b1882002-04-30 21:01:08 +0000510
H. Peter Anvine2c80182005-01-15 22:15:51 +0000511 raa_free(offsets);
512 saa_free(forwrefs);
513 eval_cleanup();
H. Peter Anvin74cc5e52007-08-30 22:35:34 +0000514 stdscan_cleanup();
H. Peter Anvin274cda82016-05-10 02:56:29 -0700515 src_free();
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000516
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700517 return terminate_after_phase;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000518}
519
H. Peter Anvineba20a72002-04-30 20:53:55 +0000520/*
521 * Get a parameter for a command line option.
522 * First arg must be in the form of e.g. -f...
523 */
H. Peter Anvin423e3812007-11-15 17:12:29 -0800524static char *get_param(char *p, char *q, bool *advance)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000525{
H. Peter Anvin423e3812007-11-15 17:12:29 -0800526 *advance = false;
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +0400527 if (p[2]) /* the parameter's in the option */
528 return nasm_skip_spaces(p + 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000529 if (q && q[0]) {
H. Peter Anvin423e3812007-11-15 17:12:29 -0800530 *advance = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000531 return q;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000532 }
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400533 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000534 "option `-%c' requires an argument", p[1]);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000535 return NULL;
536}
537
H. Peter Anvindc242712007-11-18 11:55:10 -0800538/*
539 * Copy a filename
540 */
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800541static void copy_filename(const char **dst, const char *src, const char *what)
H. Peter Anvindc242712007-11-18 11:55:10 -0800542{
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800543 if (*dst)
544 nasm_fatal(0, "more than one %s file specified: %s\n", what, src);
H. Peter Anvindc242712007-11-18 11:55:10 -0800545
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800546 *dst = nasm_strdup(src);
H. Peter Anvindc242712007-11-18 11:55:10 -0800547}
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;
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700638 bool quote = false;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700639
640 size_t n = 1; /* Terminating zero */
641
642 if (!str)
643 return NULL;
644
645 for (p = str; *p; p++) {
646 switch (*p) {
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700647 case ' ':
648 case '\t':
H. Peter Anvin3e30c322017-08-16 22:20:36 -0700649 case '&':
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700650 quote = true;
651 n++;
652 break;
653 case '\"':
654 quote = true;
655 n += 2;
656 break;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700657 case '$':
658 case '#':
659 n += 2;
660 break;
661 default:
662 n++;
663 break;
664 }
665 }
666
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700667 if (quote)
668 n += 2;
669
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700670 os = q = nasm_malloc(n);
671
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700672 if (quote)
673 *q++ = '\"';
674
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700675 for (p = str; *p; p++) {
676 switch (*p) {
677 case '$':
678 case '#':
679 *q++ = '$';
680 *q++ = *p;
681 break;
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700682 case '\"':
683 *q++ = *p;
684 *q++ = *p;
685 break;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700686 default:
687 *q++ = *p;
688 break;
689 }
690 }
691
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700692 if (quote)
693 *q++ = '\"';
694
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700695 *q = '\0';
696
697 return os;
698}
699
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100700enum text_options {
H. Peter Anvin3366e312018-02-07 14:14:36 -0800701 OPT_BOGUS,
702 OPT_VERSION,
703 OPT_ABORT_ON_PANIC,
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100704 OPT_PREFIX,
H. Peter Anvin7214d182016-03-01 22:43:51 -0800705 OPT_POSTFIX
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100706};
H. Peter Anvin3366e312018-02-07 14:14:36 -0800707struct textargs {
708 const char *label;
709 enum text_options opt;
710 bool need_arg;
711};
H. Peter Anvin2530a102016-02-18 02:28:15 -0800712static const struct textargs textopts[] = {
H. Peter Anvin3366e312018-02-07 14:14:36 -0800713 {"v", OPT_VERSION, false},
714 {"version", OPT_VERSION, false},
715 {"abort-on-panic", OPT_ABORT_ON_PANIC, false},
716 {"prefix", OPT_PREFIX, true},
717 {"postfix", OPT_POSTFIX, true},
718 {NULL, OPT_BOGUS, false}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000719};
720
Cyrill Gorcunove7438432014-05-09 22:34:34 +0400721static void show_version(void)
722{
723 printf("NASM version %s compiled on %s%s\n",
724 nasm_version, nasm_date, nasm_compile_options);
725 exit(0);
726}
727
H. Peter Anvin423e3812007-11-15 17:12:29 -0800728static bool stopoptions = false;
H. Peter Anvin55568c12016-10-03 19:46:49 -0700729static bool process_arg(char *p, char *q, int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000730{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000731 char *param;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800732 int i;
733 bool advance = false;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000734
735 if (!p || !p[0])
H. Peter Anvin423e3812007-11-15 17:12:29 -0800736 return false;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000737
H. Peter Anvine2c80182005-01-15 22:15:51 +0000738 if (p[0] == '-' && !stopoptions) {
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400739 if (strchr("oOfpPdDiIlFXuUZwW", p[1])) {
740 /* These parameters take values */
741 if (!(param = get_param(p, q, &advance)))
742 return advance;
743 }
H. Peter Anvin423e3812007-11-15 17:12:29 -0800744
H. Peter Anvine2c80182005-01-15 22:15:51 +0000745 switch (p[1]) {
746 case 's':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700747 if (pass == 1)
748 error_file = stdout;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000749 break;
H. Peter Anvin70653092007-10-19 14:42:29 -0700750
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400751 case 'o': /* output file */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700752 if (pass == 2)
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800753 copy_filename(&outname, param, "output");
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400754 break;
H. Peter Anvinfd7dd112007-10-10 14:06:59 -0700755
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400756 case 'f': /* output format */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700757 if (pass == 1) {
758 ofmt = ofmt_find(param, &ofmt_alias);
759 if (!ofmt) {
760 nasm_fatal(ERR_NOFILE | ERR_USAGE,
761 "unrecognised output format `%s' - "
762 "use -hf for a list", param);
763 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400764 }
765 break;
H. Peter Anvin70653092007-10-19 14:42:29 -0700766
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400767 case 'O': /* Optimization level */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700768 if (pass == 2) {
769 int opt;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700770
H. Peter Anvin55568c12016-10-03 19:46:49 -0700771 if (!*param) {
772 /* Naked -O == -Ox */
773 optimizing = MAX_OPTIMIZE;
774 } else {
775 while (*param) {
776 switch (*param) {
777 case '0': case '1': case '2': case '3': case '4':
778 case '5': case '6': case '7': case '8': case '9':
779 opt = strtoul(param, &param, 10);
H. Peter Anvind85d2502008-05-04 17:53:31 -0700780
H. Peter Anvin55568c12016-10-03 19:46:49 -0700781 /* -O0 -> optimizing == -1, 0.98 behaviour */
782 /* -O1 -> optimizing == 0, 0.98.09 behaviour */
783 if (opt < 2)
784 optimizing = opt - 1;
785 else
786 optimizing = opt;
787 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700788
H. Peter Anvin55568c12016-10-03 19:46:49 -0700789 case 'v':
790 case '+':
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400791 param++;
792 opt_verbose_info = true;
793 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700794
H. Peter Anvin55568c12016-10-03 19:46:49 -0700795 case 'x':
796 param++;
797 optimizing = MAX_OPTIMIZE;
798 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700799
H. Peter Anvin55568c12016-10-03 19:46:49 -0700800 default:
801 nasm_fatal(0,
802 "unknown optimization option -O%c\n",
803 *param);
804 break;
805 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400806 }
H. Peter Anvin55568c12016-10-03 19:46:49 -0700807 if (optimizing > MAX_OPTIMIZE)
808 optimizing = MAX_OPTIMIZE;
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400809 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400810 }
811 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800812
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400813 case 'p': /* pre-include */
814 case 'P':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700815 if (pass == 2)
816 preproc->pre_include(param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400817 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800818
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400819 case 'd': /* pre-define */
820 case 'D':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700821 if (pass == 2)
822 preproc->pre_define(param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400823 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800824
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400825 case 'u': /* un-define */
826 case 'U':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700827 if (pass == 2)
828 preproc->pre_undefine(param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400829 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800830
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400831 case 'i': /* include search path */
832 case 'I':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700833 if (pass == 2)
834 preproc->include_path(param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400835 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800836
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400837 case 'l': /* listing file */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700838 if (pass == 2)
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800839 copy_filename(&listname, param, "listing");
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400840 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800841
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400842 case 'Z': /* error messages file */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700843 if (pass == 1)
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800844 copy_filename(&errname, param, "error");
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400845 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800846
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400847 case 'F': /* specify debug format */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700848 if (pass == 2) {
849 using_debug_info = true;
850 debug_format = param;
851 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400852 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800853
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400854 case 'X': /* specify error reporting format */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700855 if (pass == 1) {
856 if (nasm_stricmp("vc", param) == 0)
857 nasm_set_verror(nasm_verror_vc);
858 else if (nasm_stricmp("gnu", param) == 0)
859 nasm_set_verror(nasm_verror_gnu);
860 else
861 nasm_fatal(ERR_NOFILE | ERR_USAGE,
862 "unrecognized error reporting format `%s'",
863 param);
864 }
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 'g':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700868 if (pass == 2) {
869 using_debug_info = true;
870 if (p[2])
871 debug_format = nasm_skip_spaces(p + 2);
872 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000873 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800874
H. Peter Anvine2c80182005-01-15 22:15:51 +0000875 case 'h':
876 printf
877 ("usage: nasm [-@ response file] [-o outfile] [-f format] "
878 "[-l listfile]\n"
879 " [options...] [--] filename\n"
Cyrill Gorcunove7438432014-05-09 22:34:34 +0400880 " or nasm -v (or --v) for version info\n\n"
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800881 " -t assemble in SciTech TASM compatible mode\n");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000882 printf
H. Peter Anvin413fb902007-09-26 15:19:28 -0700883 (" -E (or -e) preprocess only (writes output to stdout by default)\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000884 " -a don't preprocess (assemble only)\n"
H. Peter Anvin37a321f2007-09-24 13:41:58 -0700885 " -M generate Makefile dependencies on stdout\n"
Cyrill Gorcunov984c4db2009-07-31 00:32:17 +0400886 " -MG d:o, missing files assumed generated\n"
887 " -MF <file> set Makefile dependency file\n"
888 " -MD <file> assemble and generate dependencies\n"
889 " -MT <file> dependency target name\n"
890 " -MQ <file> dependency target name (quoted)\n"
891 " -MP emit phony target\n\n"
H. Peter Anvin413fb902007-09-26 15:19:28 -0700892 " -Z<file> redirect error messages to file\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000893 " -s redirect error messages to stdout\n\n"
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800894 " -g generate debugging information\n\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000895 " -F format select a debugging format\n\n"
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800896 " -gformat same as -g -F format\n\n"
Cyrill Gorcunovdeb082d2013-04-20 20:37:17 +0400897 " -o outfile write output to an outfile\n\n"
898 " -f format select an output format\n\n"
899 " -l listfile write listing to a listfile\n\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000900 " -I<path> adds a pathname to the include file path\n");
901 printf
H. Peter Anvinab5bd052010-07-25 12:43:30 -0700902 (" -O<digit> optimize branch offsets\n"
Cyrill Gorcunov5bc6d8e2012-03-11 14:19:17 +0400903 " -O0: No optimization\n"
Cyrill Gorcunov73b87c62009-07-31 10:26:55 +0400904 " -O1: Minimal optimization\n"
Cyrill Gorcunov5bc6d8e2012-03-11 14:19:17 +0400905 " -Ox: Multipass optimization (default)\n\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000906 " -P<file> pre-includes a file\n"
907 " -D<macro>[=<value>] pre-defines a macro\n"
908 " -U<macro> undefines a macro\n"
909 " -X<format> specifies error reporting format (gnu or vc)\n"
H. Peter Anvinb030c922007-11-13 11:31:15 -0800910 " -w+foo enables warning foo (equiv. -Wfoo)\n"
Cyrill Gorcunov984c4db2009-07-31 00:32:17 +0400911 " -w-foo disable warning foo (equiv. -Wno-foo)\n\n"
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800912 " -w[+-]error[=foo] can be used to promote warnings to errors\n"
913 " -h show invocation summary and exit\n\n"
Cyrill Gorcunov984c4db2009-07-31 00:32:17 +0400914 "--prefix,--postfix\n"
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800915 " these options prepend or append the given string\n"
916 " to all extern and global variables\n"
917 "\n"
918 "Response files should contain command line parameters,\n"
919 "one per line.\n"
920 "\n"
921 "Warnings for the -W/-w options:\n");
922 for (i = 0; i <= ERR_WARN_ALL; i++)
923 printf(" %-23s %s%s\n",
H. Peter Anvin972079f2008-09-30 17:01:23 -0700924 warnings[i].name, warnings[i].help,
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800925 i == ERR_WARN_ALL ? "\n" :
926 warnings[i].enabled ? " (default on)" :
927 " (default off)");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000928 if (p[2] == 'f') {
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800929 printf("valid output formats for -f are"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000930 " (`*' denotes default):\n");
931 ofmt_list(ofmt, stdout);
932 } else {
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800933 printf("For a list of valid output formats, use -hf.\n");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000934 printf("For a list of debug formats, use -f <form> -y.\n");
935 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400936 exit(0); /* never need usage message here */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000937 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800938
H. Peter Anvine2c80182005-01-15 22:15:51 +0000939 case 'y':
940 printf("\nvalid debug formats for '%s' output format are"
941 " ('*' denotes default):\n", ofmt->shortname);
942 dfmt_list(ofmt, stdout);
943 exit(0);
944 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800945
H. Peter Anvine2c80182005-01-15 22:15:51 +0000946 case 't':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700947 if (pass == 2)
948 tasm_compatible_mode = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000949 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800950
H. Peter Anvine2c80182005-01-15 22:15:51 +0000951 case 'v':
Cyrill Gorcunove7438432014-05-09 22:34:34 +0400952 show_version();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000953 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800954
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400955 case 'e': /* preprocess only */
956 case 'E':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700957 if (pass == 1)
958 operating_mode = OP_PREPROCESS;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000959 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800960
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400961 case 'a': /* assemble only - don't preprocess */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700962 if (pass == 1)
963 preproc = &preproc_nop;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000964 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800965
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800966 case 'w':
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400967 case 'W':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700968 if (pass == 2) {
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800969 if (!set_warning_status(param)) {
970 nasm_error(ERR_WARNING|ERR_NOFILE|ERR_WARN_UNK_WARNING,
971 "unknown warning option: %s", param);
H. Peter Anvin55568c12016-10-03 19:46:49 -0700972 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400973 }
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800974 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800975
H. Peter Anvine2c80182005-01-15 22:15:51 +0000976 case 'M':
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700977 if (pass == 1) {
978 switch (p[2]) {
979 case 'W':
980 quote_for_make = quote_for_wmake;
981 break;
982 case 'D':
983 case 'F':
984 case 'T':
985 case 'Q':
986 advance = true;
987 break;
988 default:
989 break;
990 }
991 } else {
H. Peter Anvin55568c12016-10-03 19:46:49 -0700992 switch (p[2]) {
993 case 0:
994 operating_mode = OP_DEPEND;
995 break;
996 case 'G':
997 operating_mode = OP_DEPEND;
998 depend_missing_ok = true;
999 break;
1000 case 'P':
1001 depend_emit_phony = true;
1002 break;
1003 case 'D':
1004 operating_mode = OP_NORMAL;
1005 depend_file = q;
1006 advance = true;
1007 break;
1008 case 'F':
1009 depend_file = q;
1010 advance = true;
1011 break;
1012 case 'T':
1013 depend_target = q;
1014 advance = true;
1015 break;
1016 case 'Q':
1017 depend_target = quote_for_make(q);
1018 advance = true;
1019 break;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -07001020 case 'W':
1021 /* handled in pass 1 */
1022 break;
H. Peter Anvin55568c12016-10-03 19:46:49 -07001023 default:
1024 nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
1025 "unknown dependency option `-M%c'", p[2]);
1026 break;
1027 }
H. Peter Anvin77c9bf62017-08-16 21:14:33 -07001028 }
1029 if (advance && (!q || !q[0])) {
1030 nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
1031 "option `-M%c' requires a parameter", p[2]);
1032 break;
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001033 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001034 break;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001035
H. Peter Anvine2c80182005-01-15 22:15:51 +00001036 case '-':
1037 {
H. Peter Anvin3366e312018-02-07 14:14:36 -08001038 const struct textargs *tx;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001039
H. Peter Anvine2c80182005-01-15 22:15:51 +00001040 if (p[2] == 0) { /* -- => stop processing options */
H. Peter Anvin3366e312018-02-07 14:14:36 -08001041 stopoptions = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001042 break;
1043 }
Cyrill Gorcunove7438432014-05-09 22:34:34 +04001044
H. Peter Anvin3366e312018-02-07 14:14:36 -08001045 for (tx = textopts; tx->label; tx++) {
1046 if (!nasm_stricmp(p + 2, tx->label))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001047 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001048 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001049
H. Peter Anvin3366e312018-02-07 14:14:36 -08001050 if (tx->need_arg) {
1051 if (!q) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001052 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvin3366e312018-02-07 14:14:36 -08001053 "option `--%s' requires an argument",
1054 p + 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001055 break;
1056 }
H. Peter Anvin3366e312018-02-07 14:14:36 -08001057 advance = true;
1058 }
1059
1060 switch (tx->opt) {
1061 case OPT_VERSION:
1062 show_version();
1063 break;
1064 case OPT_ABORT_ON_PANIC:
1065 abort_on_panic = true;
1066 break;
1067 case OPT_PREFIX:
1068 if (pass == 2)
1069 strlcpy(lprefix, q, PREFIX_MAX);
1070 break;
1071 case OPT_POSTFIX:
1072 if (pass == 2)
1073 strlcpy(lpostfix, q, POSTFIX_MAX);
1074 break;
1075 case OPT_BOGUS:
1076 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
1077 "unrecognized option `--%s'", p + 2);
1078 break;
1079 default:
1080 panic();
H. Peter Anvine2c80182005-01-15 22:15:51 +00001081 }
1082 break;
1083 }
1084
1085 default:
H. Peter Anvinac061332017-03-31 11:41:16 -07001086 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
1087 "unrecognised option `-%c'", p[1]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001088 break;
1089 }
H. Peter Anvin55568c12016-10-03 19:46:49 -07001090 } else if (pass == 2) {
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001091 /* In theory we could allow multiple input files... */
1092 copy_filename(&inname, p, "input");
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001093 }
1094
1095 return advance;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001096}
1097
H. Peter Anvineba20a72002-04-30 20:53:55 +00001098#define ARG_BUF_DELTA 128
1099
H. Peter Anvin55568c12016-10-03 19:46:49 -07001100static void process_respfile(FILE * rfile, int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001101{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001102 char *buffer, *p, *q, *prevarg;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001103 int bufsize, prevargsize;
1104
1105 bufsize = prevargsize = ARG_BUF_DELTA;
1106 buffer = nasm_malloc(ARG_BUF_DELTA);
1107 prevarg = nasm_malloc(ARG_BUF_DELTA);
1108 prevarg[0] = '\0';
1109
H. Peter Anvine2c80182005-01-15 22:15:51 +00001110 while (1) { /* Loop to handle all lines in file */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001111 p = buffer;
1112 while (1) { /* Loop to handle long lines */
1113 q = fgets(p, bufsize - (p - buffer), rfile);
1114 if (!q)
1115 break;
1116 p += strlen(p);
1117 if (p > buffer && p[-1] == '\n')
1118 break;
1119 if (p - buffer > bufsize - 10) {
1120 int offset;
1121 offset = p - buffer;
1122 bufsize += ARG_BUF_DELTA;
1123 buffer = nasm_realloc(buffer, bufsize);
1124 p = buffer + offset;
1125 }
1126 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001127
H. Peter Anvine2c80182005-01-15 22:15:51 +00001128 if (!q && p == buffer) {
1129 if (prevarg[0])
H. Peter Anvin55568c12016-10-03 19:46:49 -07001130 process_arg(prevarg, NULL, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001131 nasm_free(buffer);
1132 nasm_free(prevarg);
1133 return;
1134 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001135
H. Peter Anvine2c80182005-01-15 22:15:51 +00001136 /*
1137 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
1138 * them are present at the end of the line.
1139 */
1140 *(p = &buffer[strcspn(buffer, "\r\n\032")]) = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001141
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001142 while (p > buffer && nasm_isspace(p[-1]))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001143 *--p = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001144
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +04001145 p = nasm_skip_spaces(buffer);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001146
H. Peter Anvin55568c12016-10-03 19:46:49 -07001147 if (process_arg(prevarg, p, pass))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001148 *p = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001149
Charles Crayne192d5b52007-10-18 19:02:42 -07001150 if ((int) strlen(p) > prevargsize - 10) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001151 prevargsize += ARG_BUF_DELTA;
1152 prevarg = nasm_realloc(prevarg, prevargsize);
1153 }
H. Peter Anvindc242712007-11-18 11:55:10 -08001154 strncpy(prevarg, p, prevargsize);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001155 }
1156}
1157
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001158/* Function to process args from a string of args, rather than the
1159 * argv array. Used by the environment variable and response file
1160 * processing.
1161 */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001162static void process_args(char *args, int pass)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001163{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001164 char *p, *q, *arg, *prevarg;
1165 char separator = ' ';
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001166
1167 p = args;
1168 if (*p && *p != '-')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001169 separator = *p++;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001170 arg = NULL;
1171 while (*p) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001172 q = p;
1173 while (*p && *p != separator)
1174 p++;
1175 while (*p == separator)
1176 *p++ = '\0';
1177 prevarg = arg;
1178 arg = q;
H. Peter Anvin55568c12016-10-03 19:46:49 -07001179 if (process_arg(prevarg, arg, pass))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001180 arg = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001181 }
1182 if (arg)
H. Peter Anvin55568c12016-10-03 19:46:49 -07001183 process_arg(arg, NULL, pass);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001184}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001185
H. Peter Anvin55568c12016-10-03 19:46:49 -07001186static void process_response_file(const char *file, int pass)
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001187{
1188 char str[2048];
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001189 FILE *f = nasm_open_read(file, NF_TEXT);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001190 if (!f) {
Cyrill Gorcunovf1964512013-02-15 12:14:06 +04001191 perror(file);
1192 exit(-1);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001193 }
1194 while (fgets(str, sizeof str, f)) {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001195 process_args(str, pass);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001196 }
1197 fclose(f);
1198}
1199
H. Peter Anvin55568c12016-10-03 19:46:49 -07001200static void parse_cmdline(int argc, char **argv, int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001201{
1202 FILE *rfile;
Cyrill Gorcunovf4941892011-07-17 13:55:25 +04001203 char *envreal, *envcopy = NULL, *p;
H. Peter Anvin972079f2008-09-30 17:01:23 -07001204 int i;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001205
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001206 /* Initialize all the warnings to their default state */
1207 for (i = 0; i < ERR_WARN_ALL; i++) {
1208 warning_state_init[i] = warning_state[i] =
1209 warnings[i].enabled ? WARN_ST_ENABLED : 0;
1210 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001211
1212 /*
H. Peter Anvinff7ccc02002-05-06 19:41:57 +00001213 * First, process the NASMENV environment variable.
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001214 */
H. Peter Anvinff7ccc02002-05-06 19:41:57 +00001215 envreal = getenv("NASMENV");
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001216 if (envreal) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001217 envcopy = nasm_strdup(envreal);
H. Peter Anvin55568c12016-10-03 19:46:49 -07001218 process_args(envcopy, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001219 nasm_free(envcopy);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001220 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001221
1222 /*
1223 * Now process the actual command line.
1224 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001225 while (--argc) {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001226 bool advance;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001227 argv++;
1228 if (argv[0][0] == '@') {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001229 /*
1230 * We have a response file, so process this as a set of
H. Peter Anvine2c80182005-01-15 22:15:51 +00001231 * arguments like the environment variable. This allows us
1232 * to have multiple arguments on a single line, which is
1233 * different to the -@resp file processing below for regular
1234 * NASM.
1235 */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001236 process_response_file(argv[0]+1, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001237 argc--;
1238 argv++;
1239 }
1240 if (!stopoptions && argv[0][0] == '-' && argv[0][1] == '@') {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001241 p = get_param(argv[0], argc > 1 ? argv[1] : NULL, &advance);
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001242 if (p) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001243 rfile = nasm_open_read(p, NF_TEXT);
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001244 if (rfile) {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001245 process_respfile(rfile, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001246 fclose(rfile);
1247 } else
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001248 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001249 "unable to open response file `%s'", p);
1250 }
1251 } else
H. Peter Anvin55568c12016-10-03 19:46:49 -07001252 advance = process_arg(argv[0], argc > 1 ? argv[1] : NULL, pass);
H. Peter Anvin423e3812007-11-15 17:12:29 -08001253 argv += advance, argc -= advance;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001254 }
1255
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001256 /*
1257 * Look for basic command line typos. This definitely doesn't
1258 * catch all errors, but it might help cases of fumbled fingers.
1259 */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001260 if (pass != 2)
1261 return;
1262
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001263 if (!inname)
1264 nasm_fatal(ERR_NOFILE | ERR_USAGE, "no input file specified");
H. Peter Anvin59ddd262007-10-01 11:26:31 -07001265
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001266 else if ((errname && !strcmp(inname, errname)) ||
1267 (outname && !strcmp(inname, outname)) ||
1268 (listname && !strcmp(inname, listname)) ||
1269 (depend_file && !strcmp(inname, depend_file)))
1270 nasm_fatal(ERR_USAGE, "will not overwrite input file");
1271
1272 if (errname) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001273 error_file = nasm_open_write(errname, NF_TEXT);
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001274 if (!error_file) {
1275 error_file = stderr; /* Revert to default! */
H. Peter Anvin41087062016-03-03 14:27:34 -08001276 nasm_fatal(ERR_NOFILE | ERR_USAGE,
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001277 "cannot open file `%s' for error messages",
1278 errname);
1279 }
Charles Craynefcce07f2007-09-30 22:15:36 -07001280 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001281}
1282
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001283static void assemble_file(const char *fname, StrList **depend_ptr)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001284{
H. Peter Anvinc7131682017-03-07 17:45:01 -08001285 char *line;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001286 insn output_ins;
H. Peter Anvinc7131682017-03-07 17:45:01 -08001287 int i;
Charles Crayne5fbbc8c2007-11-07 19:03:46 -08001288 int64_t offs;
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001289 int pass_max;
H. Peter Anvin9c595b62017-03-07 19:44:21 -08001290 uint64_t prev_offset_changed;
1291 unsigned int stall_count = 0; /* Make sure we make forward progress... */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001292
H. Peter Anvina7ecf262018-02-06 14:43:07 -08001293 switch (cmd_sb) {
1294 case 16:
1295 break;
1296 case 32:
1297 if (!iflag_cpu_level_ok(&cmd_cpu, IF_386))
1298 nasm_fatal(0, "command line: 32-bit segment size requires a higher cpu");
1299 break;
1300 case 64:
1301 if (!iflag_cpu_level_ok(&cmd_cpu, IF_X86_64))
1302 nasm_fatal(0, "command line: 64-bit segment size requires a higher cpu");
1303 break;
1304 default:
1305 panic();
1306 break;
1307 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001308
Charles Craynec1905c22008-09-11 18:54:06 -07001309 pass_max = prev_offset_changed = (INT_MAX >> 1) + 2; /* Almost unlimited */
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001310 for (passn = 1; pass0 <= 2; passn++) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001311 ldfunc def_label;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001312
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001313 pass1 = pass0 == 2 ? 2 : 1; /* 1, 1, 1, ..., 1, 2 */
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001314 pass2 = passn > 1 ? 2 : 1; /* 1, 2, 2, ..., 2, 2 */
1315 /* pass0 0, 0, 0, ..., 1, 2 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00001316
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001317 def_label = passn > 1 ? redefine_label : define_label;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001318
H. Peter Anvincac0b192017-03-28 16:12:30 -07001319 globalbits = cmd_sb; /* set 'bits' to command line default */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001320 cpu = cmd_cpu;
1321 if (pass0 == 2) {
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08001322 lfmt->init(listname);
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001323 } else if (passn == 1 && listname) {
H. Peter Anvinaac01ff2017-04-02 19:10:26 -07001324 /* Remove the list file in case we die before the output pass */
1325 remove(listname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001326 }
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001327 in_absolute = false;
Charles Craynec1905c22008-09-11 18:54:06 -07001328 global_offset_changed = 0; /* set by redefine_label */
H. Peter Anvincac0b192017-03-28 16:12:30 -07001329 location.segment = ofmt->section(NULL, pass2, &globalbits);
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001330 if (passn > 1) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001331 saa_rewind(forwrefs);
1332 forwref = saa_rstruct(forwrefs);
1333 raa_free(offsets);
1334 offsets = raa_init();
1335 }
H. Peter Anvin130736c2016-02-17 20:27:41 -08001336 preproc->reset(fname, pass1, pass1 == 2 ? depend_ptr : NULL);
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001337
1338 /* Revert all warnings to the default state */
1339 memcpy(warning_state, warning_state_init, sizeof warning_state);
Victor van den Elzen819703a2008-07-16 15:20:56 +02001340
H. Peter Anvine2c80182005-01-15 22:15:51 +00001341 globallineno = 0;
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001342 if (passn == 1)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001343 location.known = true;
Cyrill Gorcunov190232f2013-02-15 12:35:04 +04001344 location.offset = offs = get_curr_offs();
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001345
H. Peter Anvine2c80182005-01-15 22:15:51 +00001346 while ((line = preproc->getline())) {
Chang S. Baef0ceb1e2018-05-02 08:07:53 -07001347 if (globallineno++ == GLOBALLINENO_MAX)
1348 nasm_error(ERR_FATAL,
1349 "overall line number reaches the maximum %d\n",
1350 GLOBALLINENO_MAX);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001351
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001352 /*
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001353 * Here we parse our directives; this is not handled by the
H. Peter Anvinc7131682017-03-07 17:45:01 -08001354 * main parser.
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001355 */
H. Peter Anvina6e26d92017-03-07 21:32:37 -08001356 if (process_directives(line))
H. Peter Anvinc7131682017-03-07 17:45:01 -08001357 goto end_of_line; /* Just do final cleanup */
H. Peter Anvin62b24d72007-08-29 16:38:05 +00001358
H. Peter Anvinc7131682017-03-07 17:45:01 -08001359 /* Not a directive, or even something that starts with [ */
1360
1361 parse_line(pass1, line, &output_ins, def_label);
1362
1363 if (optimizing > 0) {
1364 if (forwref != NULL && globallineno == forwref->lineno) {
1365 output_ins.forw_ref = true;
1366 do {
1367 output_ins.oprs[forwref->operand].opflags |= OPFLAG_FORWARD;
1368 forwref = saa_rstruct(forwrefs);
1369 } while (forwref != NULL
1370 && forwref->lineno == globallineno);
1371 } else
1372 output_ins.forw_ref = false;
1373
1374 if (output_ins.forw_ref) {
1375 if (passn == 1) {
1376 for (i = 0; i < output_ins.operands; i++) {
1377 if (output_ins.oprs[i].opflags & OPFLAG_FORWARD) {
1378 struct forwrefinfo *fwinf = (struct forwrefinfo *)saa_wstruct(forwrefs);
1379 fwinf->lineno = globallineno;
1380 fwinf->operand = i;
Cyrill Gorcunovd5f2aef2010-04-20 15:33:45 +04001381 }
1382 }
1383 }
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001384 }
H. Peter Anvinc7131682017-03-07 17:45:01 -08001385 }
H. Peter Anvin8bec2c72009-08-08 13:49:00 -07001386
H. Peter Anvinc7131682017-03-07 17:45:01 -08001387 /* forw_ref */
1388 if (output_ins.opcode == I_EQU) {
1389 if (pass1 == 1) {
1390 /*
1391 * Special `..' EQUs get processed in pass two,
1392 * except `..@' macro-processor EQUs which are done
1393 * in the normal place.
1394 */
1395 if (!output_ins.label)
1396 nasm_error(ERR_NONFATAL,
1397 "EQU not preceded by label");
1398
1399 else if (output_ins.label[0] != '.' ||
1400 output_ins.label[1] != '.' ||
1401 output_ins.label[2] == '@') {
1402 if (output_ins.operands == 1 &&
1403 (output_ins.oprs[0].type & IMMEDIATE) &&
1404 output_ins.oprs[0].wrt == NO_SEG) {
1405 bool isext = !!(output_ins.oprs[0].opflags & OPFLAG_EXTERN);
1406 def_label(output_ins.label,
1407 output_ins.oprs[0].segment,
1408 output_ins.oprs[0].offset, NULL,
1409 false, isext);
1410 } else if (output_ins.operands == 2
1411 && (output_ins.oprs[0].type & IMMEDIATE)
1412 && (output_ins.oprs[0].type & COLON)
1413 && output_ins.oprs[0].segment == NO_SEG
1414 && output_ins.oprs[0].wrt == NO_SEG
1415 && (output_ins.oprs[1].type & IMMEDIATE)
1416 && output_ins.oprs[1].segment == NO_SEG
1417 && output_ins.oprs[1].wrt == NO_SEG) {
1418 def_label(output_ins.label,
1419 output_ins.oprs[0].offset | SEG_ABS,
1420 output_ins.oprs[1].offset,
1421 NULL, false, false);
1422 } else
1423 nasm_error(ERR_NONFATAL,
1424 "bad syntax for EQU");
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001425 }
H. Peter Anvinc7131682017-03-07 17:45:01 -08001426 } else {
1427 /*
1428 * Special `..' EQUs get processed here, except
1429 * `..@' macro processor EQUs which are done above.
1430 */
1431 if (output_ins.label[0] == '.' &&
1432 output_ins.label[1] == '.' &&
1433 output_ins.label[2] != '@') {
1434 if (output_ins.operands == 1 &&
1435 (output_ins.oprs[0].type & IMMEDIATE)) {
1436 define_label(output_ins.label,
1437 output_ins.oprs[0].segment,
1438 output_ins.oprs[0].offset,
1439 NULL, false, false);
1440 } else if (output_ins.operands == 2
1441 && (output_ins.oprs[0].type & IMMEDIATE)
1442 && (output_ins.oprs[0].type & COLON)
1443 && output_ins.oprs[0].segment == NO_SEG
1444 && (output_ins.oprs[1].type & IMMEDIATE)
1445 && output_ins.oprs[1].segment == NO_SEG) {
1446 define_label(output_ins.label,
1447 output_ins.oprs[0].offset | SEG_ABS,
1448 output_ins.oprs[1].offset,
1449 NULL, false, false);
1450 } else
1451 nasm_error(ERR_NONFATAL,
1452 "bad syntax for EQU");
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001453 }
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001454 }
H. Peter Anvinc7131682017-03-07 17:45:01 -08001455 } else { /* instruction isn't an EQU */
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001456 int32_t n;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001457
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001458 nasm_assert(output_ins.times >= 0);
H. Peter Anvinc7131682017-03-07 17:45:01 -08001459
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001460 for (n = 1; n <= output_ins.times; n++) {
1461 if (pass1 == 1) {
1462 int64_t l = insn_size(location.segment, offs,
1463 globalbits, &output_ins);
1464
1465 /* if (using_debug_info) && output_ins.opcode != -1) */
1466 if (using_debug_info)
1467 { /* fbk 03/25/01 */
H. Peter Anvinc7131682017-03-07 17:45:01 -08001468 /* this is done here so we can do debug type info */
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001469 int32_t typeinfo =
1470 TYS_ELEMENTS(output_ins.operands);
1471 switch (output_ins.opcode) {
1472 case I_RESB:
1473 typeinfo =
1474 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_BYTE;
1475 break;
1476 case I_RESW:
1477 typeinfo =
1478 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_WORD;
1479 break;
1480 case I_RESD:
1481 typeinfo =
1482 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_DWORD;
1483 break;
1484 case I_RESQ:
1485 typeinfo =
1486 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_QWORD;
1487 break;
1488 case I_REST:
1489 typeinfo =
1490 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_TBYTE;
1491 break;
1492 case I_RESO:
1493 typeinfo =
1494 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_OWORD;
1495 break;
1496 case I_RESY:
1497 typeinfo =
1498 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_YWORD;
1499 break;
1500 case I_RESZ:
1501 typeinfo =
1502 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_ZWORD;
1503 break;
1504 case I_DB:
1505 typeinfo |= TY_BYTE;
1506 break;
1507 case I_DW:
1508 typeinfo |= TY_WORD;
1509 break;
1510 case I_DD:
1511 if (output_ins.eops_float)
1512 typeinfo |= TY_FLOAT;
1513 else
1514 typeinfo |= TY_DWORD;
1515 break;
1516 case I_DQ:
1517 typeinfo |= TY_QWORD;
1518 break;
1519 case I_DT:
1520 typeinfo |= TY_TBYTE;
1521 break;
1522 case I_DO:
1523 typeinfo |= TY_OWORD;
1524 break;
1525 case I_DY:
1526 typeinfo |= TY_YWORD;
1527 break;
1528 case I_DZ:
1529 typeinfo |= TY_ZWORD;
1530 break;
1531 default:
1532 typeinfo = TY_LABEL;
1533 break;
1534 }
H. Peter Anvinc7131682017-03-07 17:45:01 -08001535
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001536 dfmt->debug_typevalue(typeinfo);
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001537 }
H. Peter Anvinc7131682017-03-07 17:45:01 -08001538
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001539 /*
1540 * For INCBIN, let the code in assemble
1541 * handle TIMES, so we don't have to read the
1542 * input file over and over.
1543 */
1544 if (l != -1) {
1545 offs += l;
1546 set_curr_offs(offs);
1547 }
1548 /*
1549 * else l == -1 => invalid instruction, which will be
1550 * flagged as an error on pass 2
1551 */
1552 } else {
1553 if (n == 2)
1554 lfmt->uplevel(LIST_TIMES);
1555 offs += assemble(location.segment, offs,
1556 globalbits, &output_ins);
Cyrill Gorcunov190232f2013-02-15 12:35:04 +04001557 set_curr_offs(offs);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001558 }
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001559 } /* not an EQU */
1560 }
1561 if (output_ins.times > 1)
1562 lfmt->downlevel(LIST_TIMES);
H. Peter Anvinc7131682017-03-07 17:45:01 -08001563
H. Peter Anvinc7131682017-03-07 17:45:01 -08001564 cleanup_insn(&output_ins);
1565
1566 end_of_line:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001567 nasm_free(line);
Cyrill Gorcunov190232f2013-02-15 12:35:04 +04001568 location.offset = offs = get_curr_offs();
H. Peter Anvine2c80182005-01-15 22:15:51 +00001569 } /* end while (line = preproc->getline... */
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001570
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001571 if (pass0 == 2 && global_offset_changed && !terminate_after_phase)
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001572 nasm_error(ERR_NONFATAL,
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001573 "phase error detected at end of assembly.");
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001574
H. Peter Anvine2c80182005-01-15 22:15:51 +00001575 if (pass1 == 1)
1576 preproc->cleanup(1);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001577
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001578 if ((passn > 1 && !global_offset_changed) || pass0 == 2) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001579 pass0++;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001580 } else if (global_offset_changed &&
1581 global_offset_changed < prev_offset_changed) {
Charles Craynec1905c22008-09-11 18:54:06 -07001582 prev_offset_changed = global_offset_changed;
1583 stall_count = 0;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001584 } else {
1585 stall_count++;
1586 }
Victor van den Elzen42528232008-09-11 15:07:05 +02001587
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001588 if (terminate_after_phase)
1589 break;
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001590
H. Peter Anvin9c595b62017-03-07 19:44:21 -08001591 if ((stall_count > 997U) || (passn >= pass_max)) {
Victor van den Elzen42528232008-09-11 15:07:05 +02001592 /* We get here if the labels don't converge
1593 * Example: FOO equ FOO + 1
1594 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001595 nasm_error(ERR_NONFATAL,
Victor van den Elzen42528232008-09-11 15:07:05 +02001596 "Can't find valid values for all labels "
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001597 "after %d passes, giving up.", passn);
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001598 nasm_error(ERR_NONFATAL,
1599 "Possible causes: recursive EQUs, macro abuse.");
1600 break;
1601 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001602 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001603
H. Peter Anvine2c80182005-01-15 22:15:51 +00001604 preproc->cleanup(0);
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08001605 lfmt->cleanup();
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001606 if (!terminate_after_phase && opt_verbose_info) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001607 /* -On and -Ov switches */
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001608 fprintf(stdout, "info: assembly required 1+%d+1 passes\n", passn-3);
1609 }
1610}
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001611
Ed Berosetfa771012002-06-09 20:56:40 +00001612/**
1613 * gnu style error reporting
1614 * This function prints an error message to error_file in the
1615 * style used by GNU. An example would be:
1616 * file.asm:50: error: blah blah blah
H. Peter Anvin70653092007-10-19 14:42:29 -07001617 * where file.asm is the name of the file, 50 is the line number on
Ed Berosetfa771012002-06-09 20:56:40 +00001618 * which the error occurs (or is detected) and "error:" is one of
1619 * the possible optional diagnostics -- it can be "error" or "warning"
H. Peter Anvin70653092007-10-19 14:42:29 -07001620 * or something else. Finally the line terminates with the actual
Ed Berosetfa771012002-06-09 20:56:40 +00001621 * error message.
H. Peter Anvin70653092007-10-19 14:42:29 -07001622 *
1623 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001624 * @param fmt the printf style format string
1625 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001626static void nasm_verror_gnu(int severity, const char *fmt, va_list ap)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001627{
H. Peter Anvin274cda82016-05-10 02:56:29 -07001628 const char *currentfile = NULL;
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001629 int32_t lineno = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001630
Ed Berosetfa771012002-06-09 20:56:40 +00001631 if (is_suppressed_warning(severity))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001632 return;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001633
H. Peter Anvin7a6bf742017-12-20 11:54:31 -08001634 if (!(severity & ERR_NOFILE)) {
H. Peter Anvin4def1a82016-05-09 13:59:44 -07001635 src_get(&lineno, &currentfile);
H. Peter Anvin7a6bf742017-12-20 11:54:31 -08001636 if (!currentfile || (severity & ERR_TOPFILE)) {
1637 currentfile = inname[0] ? inname : outname[0] ? outname : NULL;
1638 lineno = 0;
1639 }
1640 }
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001641
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001642 if (!skip_this_pass(severity)) {
H. Peter Anvin7a6bf742017-12-20 11:54:31 -08001643 if (!lineno)
1644 fprintf(error_file, "%s:", currentfile ? currentfile : "nasm");
H. Peter Anvin883985d2017-12-20 11:32:39 -08001645 else
1646 fprintf(error_file, "%s:%"PRId32": ", currentfile, lineno);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001647 }
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001648
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001649 nasm_verror_common(severity, fmt, ap);
Ed Berosetfa771012002-06-09 20:56:40 +00001650}
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001651
Ed Berosetfa771012002-06-09 20:56:40 +00001652/**
1653 * MS style error reporting
1654 * This function prints an error message to error_file in the
H. Peter Anvin70653092007-10-19 14:42:29 -07001655 * style used by Visual C and some other Microsoft tools. An example
Ed Berosetfa771012002-06-09 20:56:40 +00001656 * would be:
Ed Beroset6e61d0d2002-06-11 03:29:36 +00001657 * file.asm(50) : error: blah blah blah
H. Peter Anvin70653092007-10-19 14:42:29 -07001658 * where file.asm is the name of the file, 50 is the line number on
Ed Beroset6e61d0d2002-06-11 03:29:36 +00001659 * which the error occurs (or is detected) and "error:" is one of
1660 * the possible optional diagnostics -- it can be "error" or "warning"
H. Peter Anvin70653092007-10-19 14:42:29 -07001661 * or something else. Finally the line terminates with the actual
Ed Beroset6e61d0d2002-06-11 03:29:36 +00001662 * error message.
H. Peter Anvin70653092007-10-19 14:42:29 -07001663 *
1664 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001665 * @param fmt the printf style format string
1666 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001667static void nasm_verror_vc(int severity, const char *fmt, va_list ap)
Ed Berosetfa771012002-06-09 20:56:40 +00001668{
H. Peter Anvin274cda82016-05-10 02:56:29 -07001669 const char *currentfile = NULL;
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001670 int32_t lineno = 0;
Ed Berosetfa771012002-06-09 20:56:40 +00001671
H. Peter Anvine2c80182005-01-15 22:15:51 +00001672 if (is_suppressed_warning(severity))
1673 return;
Ed Berosetfa771012002-06-09 20:56:40 +00001674
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001675 if (!(severity & ERR_NOFILE))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001676 src_get(&lineno, &currentfile);
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001677
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001678 if (!skip_this_pass(severity)) {
1679 if (currentfile) {
1680 fprintf(error_file, "%s(%"PRId32") : ", currentfile, lineno);
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001681 } else {
1682 fputs("nasm: ", error_file);
1683 }
Ed Berosetfa771012002-06-09 20:56:40 +00001684 }
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001685
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001686 nasm_verror_common(severity, fmt, ap);
Ed Berosetfa771012002-06-09 20:56:40 +00001687}
1688
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001689/*
1690 * check to see if this is a suppressable warning
1691 */
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001692static inline bool is_valid_warning(int severity)
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001693{
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001694 /* Not a warning at all */
1695 if ((severity & ERR_MASK) != ERR_WARNING)
1696 return false;
1697
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001698 return WARN_IDX(severity) < ERR_WARN_ALL;
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001699}
1700
Ed Berosetfa771012002-06-09 20:56:40 +00001701/**
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001702 * check for suppressed warning
H. Peter Anvin70653092007-10-19 14:42:29 -07001703 * checks for suppressed warning or pass one only warning and we're
Ed Berosetfa771012002-06-09 20:56:40 +00001704 * not in pass 1
1705 *
1706 * @param severity the severity of the warning or error
1707 * @return true if we should abort error/warning printing
1708 */
H. Peter Anvin2b046cf2008-01-22 14:08:36 -08001709static bool is_suppressed_warning(int severity)
Ed Berosetfa771012002-06-09 20:56:40 +00001710{
H. Peter Anvin442a05a2012-02-24 21:50:53 -08001711 /* Might be a warning but suppresed explicitly */
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001712 if (is_valid_warning(severity))
1713 return !(warning_state[WARN_IDX(severity)] & WARN_ST_ENABLED);
1714 else
1715 return false;
1716}
1717
1718static bool warning_is_error(int severity)
1719{
1720 if (is_valid_warning(severity))
1721 return !!(warning_state[WARN_IDX(severity)] & WARN_ST_ERROR);
H. Peter Anvin442a05a2012-02-24 21:50:53 -08001722 else
1723 return false;
Ed Berosetfa771012002-06-09 20:56:40 +00001724}
1725
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001726static bool skip_this_pass(int severity)
1727{
H. Peter Anvin8f622462017-04-02 19:02:29 -07001728 /*
1729 * See if it's a pass-specific error or warning which should be skipped.
1730 * We cannot skip errors stronger than ERR_NONFATAL as by definition
1731 * they cannot be resumed from.
1732 */
1733 if ((severity & ERR_MASK) > ERR_NONFATAL)
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001734 return false;
1735
H. Peter Anvin934f0472016-05-09 12:00:19 -07001736 /*
1737 * passn is 1 on the very first pass only.
1738 * pass0 is 2 on the code-generation (final) pass only.
1739 * These are the passes we care about in this case.
1740 */
1741 return (((severity & ERR_PASS1) && passn != 1) ||
1742 ((severity & ERR_PASS2) && pass0 != 2));
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001743}
1744
Ed Berosetfa771012002-06-09 20:56:40 +00001745/**
1746 * common error reporting
1747 * This is the common back end of the error reporting schemes currently
H. Peter Anvin70653092007-10-19 14:42:29 -07001748 * implemented. It prints the nature of the warning and then the
Ed Berosetfa771012002-06-09 20:56:40 +00001749 * specific error message to error_file and may or may not return. It
1750 * doesn't return if the error severity is a "panic" or "debug" type.
H. Peter Anvin70653092007-10-19 14:42:29 -07001751 *
1752 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001753 * @param fmt the printf style format string
1754 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001755static void nasm_verror_common(int severity, const char *fmt, va_list args)
Ed Berosetfa771012002-06-09 20:56:40 +00001756{
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001757 char msg[1024];
1758 const char *pfx;
H. Peter Anvin323fcff2009-07-12 12:04:56 -07001759
H. Peter Anvin7df04172008-06-10 18:27:38 -07001760 switch (severity & (ERR_MASK|ERR_NO_SEVERITY)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001761 case ERR_WARNING:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001762 pfx = "warning: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001763 break;
1764 case ERR_NONFATAL:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001765 pfx = "error: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001766 break;
1767 case ERR_FATAL:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001768 pfx = "fatal: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001769 break;
1770 case ERR_PANIC:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001771 pfx = "panic: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001772 break;
1773 case ERR_DEBUG:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001774 pfx = "debug: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001775 break;
H. Peter Anvin7df04172008-06-10 18:27:38 -07001776 default:
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001777 pfx = "";
1778 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001779 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00001780
H. Peter Anvin934f0472016-05-09 12:00:19 -07001781 vsnprintf(msg, sizeof msg - 64, fmt, args);
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001782 if (is_valid_warning(severity) && WARN_IDX(severity) != ERR_WARN_OTHER) {
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001783 char *p = strchr(msg, '\0');
H. Peter Anvin934f0472016-05-09 12:00:19 -07001784 snprintf(p, 64, " [-w+%s]", warnings[WARN_IDX(severity)].name);
1785 }
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001786
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001787 if (!skip_this_pass(severity))
1788 fprintf(error_file, "%s%s\n", pfx, msg);
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001789
H. Peter Anvin4def1a82016-05-09 13:59:44 -07001790 /* Are we recursing from error_list_macros? */
1791 if (severity & ERR_PP_LISTMACRO)
1792 return;
1793
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001794 /*
1795 * Don't suppress this with skip_this_pass(), or we don't get
H. Peter Anvin934f0472016-05-09 12:00:19 -07001796 * pass1 or preprocessor warnings in the list file
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001797 */
H. Peter Anvin0fcb4882016-07-06 11:55:25 -07001798 lfmt->error(severity, pfx, msg);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001799
H. Peter Anvin8f622462017-04-02 19:02:29 -07001800 if (skip_this_pass(severity))
1801 return;
1802
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001803 if (severity & ERR_USAGE)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001804 want_usage = true;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001805
H. Peter Anvin4def1a82016-05-09 13:59:44 -07001806 preproc->error_list_macros(severity);
1807
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001808 switch (severity & ERR_MASK) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001809 case ERR_DEBUG:
1810 /* no further action, by definition */
1811 break;
H. Peter Anvinb030c922007-11-13 11:31:15 -08001812 case ERR_WARNING:
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001813 /* Treat warnings as errors */
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001814 if (warning_is_error(severity))
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001815 terminate_after_phase = true;
1816 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001817 case ERR_NONFATAL:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001818 terminate_after_phase = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001819 break;
1820 case ERR_FATAL:
1821 if (ofile) {
1822 fclose(ofile);
1823 remove(outname);
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001824 ofile = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001825 }
1826 if (want_usage)
1827 usage();
1828 exit(1); /* instantly die */
1829 break; /* placate silly compilers */
1830 case ERR_PANIC:
1831 fflush(NULL);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001832
1833 if (abort_on_panic)
1834 abort(); /* halt, catch fire, dump core/stop debugger */
1835
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001836 if (ofile) {
1837 fclose(ofile);
1838 remove(outname);
1839 ofile = NULL;
1840 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001841 exit(3);
1842 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001843 }
1844}
1845
H. Peter Anvin734b1882002-04-30 21:01:08 +00001846static void usage(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001847{
H. Peter Anvin620515a2002-04-30 20:57:38 +00001848 fputs("type `nasm -h' for help\n", error_file);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001849}