blob: 6db5cce29300caa2d8edb02444d0fe506a7f0101 [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
H. Peter Anvin892c4812018-05-30 14:43:46 -0700156int64_t switch_segment(int32_t segment)
Cyrill Gorcunov190232f2013-02-15 12:35:04 +0400157{
H. Peter Anvin892c4812018-05-30 14:43:46 -0700158 location.segment = segment;
159 if (segment == NO_SEG) {
160 location.offset = absolute.offset;
161 in_absolute = true;
162 } else {
163 location.offset = raa_read(offsets, segment);
164 in_absolute = false;
165 }
166 return location.offset;
Cyrill Gorcunov190232f2013-02-15 12:35:04 +0400167}
168
169static void set_curr_offs(int64_t l_off)
170{
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800171 if (in_absolute)
172 absolute.offset = l_off;
Cyrill Gorcunov190232f2013-02-15 12:35:04 +0400173 else
174 offsets = raa_write(offsets, location.segment, l_off);
175}
176
H. Peter Anvin892c4812018-05-30 14:43:46 -0700177static void increment_offset(int64_t delta)
178{
179 if (unlikely(delta == 0))
180 return;
181
182 location.offset += delta;
183 set_curr_offs(location.offset);
184}
185
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000186static void nasm_fputs(const char *line, FILE * outfile)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000187{
H. Peter Anvin310b3e12002-05-14 22:38:55 +0000188 if (outfile) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000189 fputs(line, outfile);
H. Peter Anvind1fb15c2007-11-13 09:37:59 -0800190 putc('\n', outfile);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000191 } else
H. Peter Anvine2c80182005-01-15 22:15:51 +0000192 puts(line);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000193}
194
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800195static void define_macros_early(void)
196{
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700197 const struct compile_time * const oct = &official_compile_time;
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800198 char temp[128];
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800199
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700200 if (oct->have_local) {
201 strftime(temp, sizeof temp, "__DATE__=\"%Y-%m-%d\"", &oct->local);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400202 preproc->pre_define(temp);
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700203 strftime(temp, sizeof temp, "__DATE_NUM__=%Y%m%d", &oct->local);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400204 preproc->pre_define(temp);
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700205 strftime(temp, sizeof temp, "__TIME__=\"%H:%M:%S\"", &oct->local);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400206 preproc->pre_define(temp);
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700207 strftime(temp, sizeof temp, "__TIME_NUM__=%H%M%S", &oct->local);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400208 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800209 }
210
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700211 if (oct->have_gm) {
212 strftime(temp, sizeof temp, "__UTC_DATE__=\"%Y-%m-%d\"", &oct->gm);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400213 preproc->pre_define(temp);
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700214 strftime(temp, sizeof temp, "__UTC_DATE_NUM__=%Y%m%d", &oct->gm);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400215 preproc->pre_define(temp);
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700216 strftime(temp, sizeof temp, "__UTC_TIME__=\"%H:%M:%S\"", &oct->gm);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400217 preproc->pre_define(temp);
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700218 strftime(temp, sizeof temp, "__UTC_TIME_NUM__=%H%M%S", &oct->gm);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400219 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800220 }
H. Peter Anvind85d2502008-05-04 17:53:31 -0700221
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700222 if (oct->have_posix) {
223 snprintf(temp, sizeof temp, "__POSIX_TIME__=%"PRId64, oct->posix);
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400224 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800225 }
226}
227
228static void define_macros_late(void)
229{
230 char temp[128];
231
Cyrill Gorcunovc1936da2011-04-06 18:32:15 +0400232 /*
233 * In case if output format is defined by alias
234 * we have to put shortname of the alias itself here
235 * otherwise ABI backward compatibility gets broken.
236 */
Cyrill Gorcunov69ce7502010-07-12 15:19:17 +0400237 snprintf(temp, sizeof(temp), "__OUTPUT_FORMAT__=%s",
Cyrill Gorcunovc1936da2011-04-06 18:32:15 +0400238 ofmt_alias ? ofmt_alias->shortname : ofmt->shortname);
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +0400239 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800240}
241
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700242static void emit_dependencies(StrList *list)
243{
244 FILE *deps;
245 int linepos, len;
246 StrList *l, *nl;
H. Peter Anvinf05034f2017-08-16 22:08:36 -0700247 bool wmake = (quote_for_make == quote_for_wmake);
248 const char *wrapstr, *nulltarget;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700249
H. Peter Anvinf05034f2017-08-16 22:08:36 -0700250 wrapstr = wmake ? " &\n " : " \\\n ";
251 nulltarget = wmake ? "\t%null\n" : "";
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700252
253 if (depend_file && strcmp(depend_file, "-")) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -0700254 deps = nasm_open_write(depend_file, NF_TEXT);
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400255 if (!deps) {
256 nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
257 "unable to write dependency file `%s'", depend_file);
258 return;
259 }
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700260 } else {
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400261 deps = stdout;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700262 }
H. Peter Anvin323fcff2009-07-12 12:04:56 -0700263
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700264 linepos = fprintf(deps, "%s :", depend_target);
Cyrill Gorcunovfcd0a742009-07-27 16:26:26 +0400265 list_for_each(l, list) {
H. Peter Anvin55340992012-09-09 17:09:00 -0700266 char *file = quote_for_make(l->str);
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400267 len = strlen(file);
268 if (linepos + len > 62 && linepos > 1) {
H. Peter Anvinf05034f2017-08-16 22:08:36 -0700269 fputs(wrapstr, deps);
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400270 linepos = 1;
271 }
272 fprintf(deps, " %s", file);
273 linepos += len+1;
H. Peter Anvin55340992012-09-09 17:09:00 -0700274 nasm_free(file);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700275 }
276 fprintf(deps, "\n\n");
H. Peter Anvin323fcff2009-07-12 12:04:56 -0700277
Cyrill Gorcunovfcd0a742009-07-27 16:26:26 +0400278 list_for_each_safe(l, nl, list) {
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700279 if (depend_emit_phony) {
280 char *file = quote_for_make(l->str);
H. Peter Anvinf05034f2017-08-16 22:08:36 -0700281 fprintf(deps, "%s :\n%s\n", file, nulltarget);
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700282 nasm_free(file);
283 }
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400284 nasm_free(l);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700285 }
286
287 if (deps != stdout)
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400288 fclose(deps);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700289}
290
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700291/* Convert a struct tm to a POSIX-style time constant */
292static int64_t make_posix_time(const struct tm *tm)
293{
294 int64_t t;
295 int64_t y = tm->tm_year;
296
297 /* See IEEE 1003.1:2004, section 4.14 */
298
299 t = (y-70)*365 + (y-69)/4 - (y-1)/100 + (y+299)/400;
300 t += tm->tm_yday;
301 t *= 24;
302 t += tm->tm_hour;
303 t *= 60;
304 t += tm->tm_min;
305 t *= 60;
306 t += tm->tm_sec;
307
308 return t;
309}
310
311static void timestamp(void)
312{
313 struct compile_time * const oct = &official_compile_time;
314 const struct tm *tp, *best_gm;
315
316 time(&oct->t);
317
318 best_gm = NULL;
319
320 tp = localtime(&oct->t);
321 if (tp) {
322 oct->local = *tp;
323 best_gm = &oct->local;
324 oct->have_local = true;
325 }
326
327 tp = gmtime(&oct->t);
328 if (tp) {
329 oct->gm = *tp;
330 best_gm = &oct->gm;
331 oct->have_gm = true;
332 if (!oct->have_local)
333 oct->local = oct->gm;
334 } else {
335 oct->gm = oct->local;
336 }
337
338 if (best_gm) {
339 oct->posix = make_posix_time(best_gm);
340 oct->have_posix = true;
341 }
342}
343
H. Peter Anvin038d8612007-04-12 16:54:50 +0000344int main(int argc, char **argv)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000345{
H. Peter Anvina771be82017-08-16 14:53:18 -0700346 StrList **depend_ptr;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700347
H. Peter Anvin24f7b5c2017-08-02 18:37:54 -0700348 timestamp();
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800349
H. Peter Anvina7ecf262018-02-06 14:43:07 -0800350 iflag_set_default_cpu(&cpu);
351 iflag_set_default_cpu(&cmd_cpu);
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400352
Charles Crayne2581c862008-09-10 19:21:52 -0700353 pass0 = 0;
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700354 want_usage = terminate_after_phase = false;
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400355 nasm_set_verror(nasm_verror_gnu);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000356
H. Peter Anvin97e15752007-09-25 08:47:47 -0700357 error_file = stderr;
358
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -0700359 tolower_init();
H. Peter Anvin274cda82016-05-10 02:56:29 -0700360 src_init();
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -0700361
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000362 offsets = raa_init();
Keith Kaniosb7a89542007-04-12 02:40:54 +0000363 forwrefs = saa_init((int32_t)sizeof(struct forwrefinfo));
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000364
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000365 preproc = &nasmpp;
Cyrill Gorcunov3ed32cb2014-06-22 20:53:12 +0400366 operating_mode = OP_NORMAL;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000367
H. Peter Anvin55568c12016-10-03 19:46:49 -0700368 parse_cmdline(argc, argv, 1);
369 if (terminate_after_phase) {
370 if (want_usage)
371 usage();
372 return 1;
373 }
374
H. Peter Anvin169ac7c2016-09-25 17:08:05 -0700375 /*
376 * Define some macros dependent on the runtime, but not
H. Peter Anvin55568c12016-10-03 19:46:49 -0700377 * on the command line (as those are scanned in cmdline pass 2.)
H. Peter Anvin169ac7c2016-09-25 17:08:05 -0700378 */
379 preproc->init();
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800380 define_macros_early();
381
H. Peter Anvin55568c12016-10-03 19:46:49 -0700382 parse_cmdline(argc, argv, 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000383 if (terminate_after_phase) {
384 if (want_usage)
385 usage();
386 return 1;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000387 }
388
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800389 /* Save away the default state of warnings */
390 memcpy(warning_state_init, warning_state, sizeof warning_state);
391
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800392 if (!using_debug_info) {
393 /* No debug info, redirect to the null backend (empty stubs) */
H. Peter Anvina7bc15d2016-02-17 20:55:08 -0800394 dfmt = &null_debug_form;
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800395 } else if (!debug_format) {
396 /* Default debug format for this backend */
H. Peter Anvina7bc15d2016-02-17 20:55:08 -0800397 dfmt = ofmt->default_dfmt;
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800398 } else {
399 dfmt = dfmt_find(ofmt, debug_format);
400 if (!dfmt) {
401 nasm_fatal(ERR_NOFILE | ERR_USAGE,
402 "unrecognized debug format `%s' for"
403 " output format `%s'",
404 debug_format, ofmt->shortname);
405 }
406 }
H. Peter Anvinbb88d012003-09-10 23:34:23 +0000407
H. Peter Anvin76690a12002-04-30 20:52:49 +0000408 if (ofmt->stdmac)
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +0400409 preproc->extra_stdmac(ofmt->stdmac);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000410
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800411 /* no output file name? */
412 if (!outname)
413 outname = filename_set_extension(inname, ofmt->extension);
414
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000415 /* define some macros dependent of command-line */
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800416 define_macros_late();
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000417
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400418 depend_ptr = (depend_file || (operating_mode & OP_DEPEND)) ? &depend_list : NULL;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700419
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700420 if (!depend_target)
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400421 depend_target = quote_for_make(outname);
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700422
Cyrill Gorcunove9fc88c2014-06-23 02:22:02 +0400423 if (operating_mode & OP_DEPEND) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000424 char *line;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700425
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400426 if (depend_missing_ok)
427 preproc->include_path(NULL); /* "assume generated" */
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700428
H. Peter Anvin130736c2016-02-17 20:27:41 -0800429 preproc->reset(inname, 0, depend_ptr);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000430 ofile = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000431 while ((line = preproc->getline()))
432 nasm_free(line);
433 preproc->cleanup(0);
Cyrill Gorcunove9fc88c2014-06-23 02:22:02 +0400434 } else if (operating_mode & OP_PREPROCESS) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000435 char *line;
H. Peter Anvin274cda82016-05-10 02:56:29 -0700436 const char *file_name = NULL;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000437 int32_t prior_linnum = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000438 int lineinc = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000439
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800440 if (outname) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -0700441 ofile = nasm_open_write(outname, NF_TEXT);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000442 if (!ofile)
H. Peter Anvin41087062016-03-03 14:27:34 -0800443 nasm_fatal(ERR_NOFILE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000444 "unable to open output file `%s'",
445 outname);
446 } else
447 ofile = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000448
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700449 location.known = false;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000450
Cyrill Gorcunovb574b072011-12-05 01:56:40 +0400451 /* pass = 1; */
H. Peter Anvin130736c2016-02-17 20:27:41 -0800452 preproc->reset(inname, 3, depend_ptr);
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800453
454 /* Revert all warnings to the default state */
455 memcpy(warning_state, warning_state_init, sizeof warning_state);
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700456
H. Peter Anvine2c80182005-01-15 22:15:51 +0000457 while ((line = preproc->getline())) {
458 /*
459 * We generate %line directives if needed for later programs
460 */
Keith Kaniosb7a89542007-04-12 02:40:54 +0000461 int32_t linnum = prior_linnum += lineinc;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000462 int altline = src_get(&linnum, &file_name);
463 if (altline) {
464 if (altline == 1 && lineinc == 1)
465 nasm_fputs("", ofile);
466 else {
467 lineinc = (altline != -1 || lineinc != 1);
468 fprintf(ofile ? ofile : stdout,
Keith Kanios93f2e9a2007-04-14 00:10:59 +0000469 "%%line %"PRId32"+%d %s\n", linnum, lineinc,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000470 file_name);
471 }
472 prior_linnum = linnum;
473 }
474 nasm_fputs(line, ofile);
475 nasm_free(line);
476 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000477 preproc->cleanup(0);
478 if (ofile)
479 fclose(ofile);
480 if (ofile && terminate_after_phase)
481 remove(outname);
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400482 ofile = NULL;
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400483 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000484
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400485 if (operating_mode & OP_NORMAL) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -0700486 ofile = nasm_open_write(outname, (ofmt->flags & OFMT_TEXT) ? NF_TEXT : NF_BINARY);
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400487 if (!ofile)
H. Peter Anvin41087062016-03-03 14:27:34 -0800488 nasm_fatal(ERR_NOFILE,
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400489 "unable to open output file `%s'", outname);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000490
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400491 /*
492 * We must call init_labels() before ofmt->init() since
493 * some object formats will want to define labels in their
494 * init routines. (eg OS/2 defines the FLAT group)
495 */
496 init_labels();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000497
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400498 ofmt->init();
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400499 dfmt->init();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000500
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400501 assemble_file(inname, depend_ptr);
Victor van den Elzenc82c3722008-06-04 15:24:20 +0200502
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400503 if (!terminate_after_phase) {
H. Peter Anvin477ae442016-03-07 22:53:06 -0800504 ofmt->cleanup();
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400505 cleanup_labels();
506 fflush(ofile);
H. Peter Anvin274cda82016-05-10 02:56:29 -0700507 if (ferror(ofile)) {
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400508 nasm_error(ERR_NONFATAL|ERR_NOFILE,
509 "write error on output file `%s'", outname);
H. Peter Anvin274cda82016-05-10 02:56:29 -0700510 terminate_after_phase = true;
511 }
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400512 }
513
514 if (ofile) {
515 fclose(ofile);
516 if (terminate_after_phase)
517 remove(outname);
518 ofile = NULL;
519 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000520 }
H. Peter Anvin734b1882002-04-30 21:01:08 +0000521
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700522 if (depend_list && !terminate_after_phase)
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400523 emit_dependencies(depend_list);
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700524
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000525 if (want_usage)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000526 usage();
H. Peter Anvin734b1882002-04-30 21:01:08 +0000527
H. Peter Anvine2c80182005-01-15 22:15:51 +0000528 raa_free(offsets);
529 saa_free(forwrefs);
530 eval_cleanup();
H. Peter Anvin74cc5e52007-08-30 22:35:34 +0000531 stdscan_cleanup();
H. Peter Anvin274cda82016-05-10 02:56:29 -0700532 src_free();
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000533
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700534 return terminate_after_phase;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000535}
536
H. Peter Anvineba20a72002-04-30 20:53:55 +0000537/*
538 * Get a parameter for a command line option.
539 * First arg must be in the form of e.g. -f...
540 */
H. Peter Anvin423e3812007-11-15 17:12:29 -0800541static char *get_param(char *p, char *q, bool *advance)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000542{
H. Peter Anvin423e3812007-11-15 17:12:29 -0800543 *advance = false;
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +0400544 if (p[2]) /* the parameter's in the option */
545 return nasm_skip_spaces(p + 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000546 if (q && q[0]) {
H. Peter Anvin423e3812007-11-15 17:12:29 -0800547 *advance = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000548 return q;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000549 }
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400550 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000551 "option `-%c' requires an argument", p[1]);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000552 return NULL;
553}
554
H. Peter Anvindc242712007-11-18 11:55:10 -0800555/*
556 * Copy a filename
557 */
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800558static void copy_filename(const char **dst, const char *src, const char *what)
H. Peter Anvindc242712007-11-18 11:55:10 -0800559{
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800560 if (*dst)
561 nasm_fatal(0, "more than one %s file specified: %s\n", what, src);
H. Peter Anvindc242712007-11-18 11:55:10 -0800562
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800563 *dst = nasm_strdup(src);
H. Peter Anvindc242712007-11-18 11:55:10 -0800564}
565
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700566/*
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700567 * Convert a string to a POSIX make-safe form
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700568 */
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700569static char *quote_for_pmake(const char *str)
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700570{
571 const char *p;
572 char *os, *q;
573
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400574 size_t n = 1; /* Terminating zero */
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700575 size_t nbs = 0;
576
577 if (!str)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400578 return NULL;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700579
580 for (p = str; *p; p++) {
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400581 switch (*p) {
582 case ' ':
583 case '\t':
584 /* Convert N backslashes + ws -> 2N+1 backslashes + ws */
585 n += nbs + 2;
586 nbs = 0;
587 break;
588 case '$':
589 case '#':
590 nbs = 0;
591 n += 2;
592 break;
593 case '\\':
594 nbs++;
595 n++;
596 break;
597 default:
598 nbs = 0;
599 n++;
600 break;
601 }
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700602 }
603
604 /* Convert N backslashes at the end of filename to 2N backslashes */
605 if (nbs)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400606 n += nbs;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700607
608 os = q = nasm_malloc(n);
609
610 nbs = 0;
611 for (p = str; *p; p++) {
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400612 switch (*p) {
613 case ' ':
614 case '\t':
615 while (nbs--)
616 *q++ = '\\';
617 *q++ = '\\';
618 *q++ = *p;
619 break;
620 case '$':
621 *q++ = *p;
622 *q++ = *p;
623 nbs = 0;
624 break;
625 case '#':
626 *q++ = '\\';
627 *q++ = *p;
628 nbs = 0;
629 break;
630 case '\\':
631 *q++ = *p;
632 nbs++;
633 break;
634 default:
635 *q++ = *p;
636 nbs = 0;
637 break;
638 }
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700639 }
640 while (nbs--)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400641 *q++ = '\\';
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700642
643 *q = '\0';
644
645 return os;
646}
647
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700648/*
649 * Convert a string to a Watcom make-safe form
650 */
651static char *quote_for_wmake(const char *str)
652{
653 const char *p;
654 char *os, *q;
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700655 bool quote = false;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700656
657 size_t n = 1; /* Terminating zero */
658
659 if (!str)
660 return NULL;
661
662 for (p = str; *p; p++) {
663 switch (*p) {
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700664 case ' ':
665 case '\t':
H. Peter Anvin3e30c322017-08-16 22:20:36 -0700666 case '&':
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700667 quote = true;
668 n++;
669 break;
670 case '\"':
671 quote = true;
672 n += 2;
673 break;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700674 case '$':
675 case '#':
676 n += 2;
677 break;
678 default:
679 n++;
680 break;
681 }
682 }
683
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700684 if (quote)
685 n += 2;
686
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700687 os = q = nasm_malloc(n);
688
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700689 if (quote)
690 *q++ = '\"';
691
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700692 for (p = str; *p; p++) {
693 switch (*p) {
694 case '$':
695 case '#':
696 *q++ = '$';
697 *q++ = *p;
698 break;
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700699 case '\"':
700 *q++ = *p;
701 *q++ = *p;
702 break;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700703 default:
704 *q++ = *p;
705 break;
706 }
707 }
708
H. Peter Anvin427b9ca2017-08-16 22:15:39 -0700709 if (quote)
710 *q++ = '\"';
711
H. Peter Anvin77c9bf62017-08-16 21:14:33 -0700712 *q = '\0';
713
714 return os;
715}
716
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100717enum text_options {
H. Peter Anvin3366e312018-02-07 14:14:36 -0800718 OPT_BOGUS,
719 OPT_VERSION,
720 OPT_ABORT_ON_PANIC,
H. Peter Anvin05990342018-06-11 13:32:42 -0700721 OPT_MANGLE,
722 OPT_INCLUDE,
723 OPT_PRAGMA,
724 OPT_BEFORE
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100725};
H. Peter Anvin3366e312018-02-07 14:14:36 -0800726struct textargs {
727 const char *label;
728 enum text_options opt;
729 bool need_arg;
H. Peter Anvin98578072018-06-01 18:02:54 -0700730 int pvt;
H. Peter Anvin3366e312018-02-07 14:14:36 -0800731};
H. Peter Anvin2530a102016-02-18 02:28:15 -0800732static const struct textargs textopts[] = {
H. Peter Anvin98578072018-06-01 18:02:54 -0700733 {"v", OPT_VERSION, false, 0},
734 {"version", OPT_VERSION, false, 0},
735 {"abort-on-panic", OPT_ABORT_ON_PANIC, false, 0},
736 {"prefix", OPT_MANGLE, true, LM_GPREFIX},
737 {"postfix", OPT_MANGLE, true, LM_GSUFFIX},
738 {"gprefix", OPT_MANGLE, true, LM_GPREFIX},
739 {"gpostfix", OPT_MANGLE, true, LM_GSUFFIX},
740 {"lprefix", OPT_MANGLE, true, LM_LPREFIX},
741 {"lpostfix", OPT_MANGLE, true, LM_LSUFFIX},
H. Peter Anvin05990342018-06-11 13:32:42 -0700742 {"include", OPT_INCLUDE, true, 0},
743 {"pragma", OPT_PRAGMA, true, 0},
744 {"before", OPT_BEFORE, true, 0},
H. Peter Anvin98578072018-06-01 18:02:54 -0700745 {NULL, OPT_BOGUS, false, 0}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000746};
747
Cyrill Gorcunove7438432014-05-09 22:34:34 +0400748static void show_version(void)
749{
750 printf("NASM version %s compiled on %s%s\n",
751 nasm_version, nasm_date, nasm_compile_options);
752 exit(0);
753}
754
H. Peter Anvin423e3812007-11-15 17:12:29 -0800755static bool stopoptions = false;
H. Peter Anvin55568c12016-10-03 19:46:49 -0700756static bool process_arg(char *p, char *q, int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000757{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000758 char *param;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800759 int i;
760 bool advance = false;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000761
762 if (!p || !p[0])
H. Peter Anvin423e3812007-11-15 17:12:29 -0800763 return false;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000764
H. Peter Anvine2c80182005-01-15 22:15:51 +0000765 if (p[0] == '-' && !stopoptions) {
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400766 if (strchr("oOfpPdDiIlFXuUZwW", p[1])) {
767 /* These parameters take values */
768 if (!(param = get_param(p, q, &advance)))
769 return advance;
770 }
H. Peter Anvin423e3812007-11-15 17:12:29 -0800771
H. Peter Anvine2c80182005-01-15 22:15:51 +0000772 switch (p[1]) {
773 case 's':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700774 if (pass == 1)
775 error_file = stdout;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000776 break;
H. Peter Anvin70653092007-10-19 14:42:29 -0700777
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400778 case 'o': /* output file */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700779 if (pass == 2)
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800780 copy_filename(&outname, param, "output");
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400781 break;
H. Peter Anvinfd7dd112007-10-10 14:06:59 -0700782
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400783 case 'f': /* output format */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700784 if (pass == 1) {
785 ofmt = ofmt_find(param, &ofmt_alias);
786 if (!ofmt) {
787 nasm_fatal(ERR_NOFILE | ERR_USAGE,
788 "unrecognised output format `%s' - "
789 "use -hf for a list", param);
790 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400791 }
792 break;
H. Peter Anvin70653092007-10-19 14:42:29 -0700793
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400794 case 'O': /* Optimization level */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700795 if (pass == 2) {
796 int opt;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700797
H. Peter Anvin55568c12016-10-03 19:46:49 -0700798 if (!*param) {
799 /* Naked -O == -Ox */
800 optimizing = MAX_OPTIMIZE;
801 } else {
802 while (*param) {
803 switch (*param) {
804 case '0': case '1': case '2': case '3': case '4':
805 case '5': case '6': case '7': case '8': case '9':
806 opt = strtoul(param, &param, 10);
H. Peter Anvind85d2502008-05-04 17:53:31 -0700807
H. Peter Anvin55568c12016-10-03 19:46:49 -0700808 /* -O0 -> optimizing == -1, 0.98 behaviour */
809 /* -O1 -> optimizing == 0, 0.98.09 behaviour */
810 if (opt < 2)
811 optimizing = opt - 1;
812 else
813 optimizing = opt;
814 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700815
H. Peter Anvin55568c12016-10-03 19:46:49 -0700816 case 'v':
817 case '+':
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400818 param++;
819 opt_verbose_info = true;
820 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700821
H. Peter Anvin55568c12016-10-03 19:46:49 -0700822 case 'x':
823 param++;
824 optimizing = MAX_OPTIMIZE;
825 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700826
H. Peter Anvin55568c12016-10-03 19:46:49 -0700827 default:
828 nasm_fatal(0,
829 "unknown optimization option -O%c\n",
830 *param);
831 break;
832 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400833 }
H. Peter Anvin55568c12016-10-03 19:46:49 -0700834 if (optimizing > MAX_OPTIMIZE)
835 optimizing = MAX_OPTIMIZE;
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400836 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400837 }
838 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800839
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400840 case 'p': /* pre-include */
841 case 'P':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700842 if (pass == 2)
843 preproc->pre_include(param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400844 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800845
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400846 case 'd': /* pre-define */
847 case 'D':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700848 if (pass == 2)
849 preproc->pre_define(param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400850 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800851
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400852 case 'u': /* un-define */
853 case 'U':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700854 if (pass == 2)
855 preproc->pre_undefine(param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400856 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800857
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400858 case 'i': /* include search path */
859 case 'I':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700860 if (pass == 2)
861 preproc->include_path(param);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400862 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800863
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400864 case 'l': /* listing file */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700865 if (pass == 2)
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800866 copy_filename(&listname, param, "listing");
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400867 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800868
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400869 case 'Z': /* error messages file */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700870 if (pass == 1)
H. Peter Anvin81b62b92017-12-20 13:33:49 -0800871 copy_filename(&errname, param, "error");
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400872 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800873
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400874 case 'F': /* specify debug format */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700875 if (pass == 2) {
876 using_debug_info = true;
877 debug_format = param;
878 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400879 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800880
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400881 case 'X': /* specify error reporting format */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700882 if (pass == 1) {
883 if (nasm_stricmp("vc", param) == 0)
884 nasm_set_verror(nasm_verror_vc);
885 else if (nasm_stricmp("gnu", param) == 0)
886 nasm_set_verror(nasm_verror_gnu);
887 else
888 nasm_fatal(ERR_NOFILE | ERR_USAGE,
889 "unrecognized error reporting format `%s'",
890 param);
891 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000892 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800893
H. Peter Anvine2c80182005-01-15 22:15:51 +0000894 case 'g':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700895 if (pass == 2) {
896 using_debug_info = true;
897 if (p[2])
898 debug_format = nasm_skip_spaces(p + 2);
899 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000900 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800901
H. Peter Anvine2c80182005-01-15 22:15:51 +0000902 case 'h':
903 printf
904 ("usage: nasm [-@ response file] [-o outfile] [-f format] "
905 "[-l listfile]\n"
906 " [options...] [--] filename\n"
Cyrill Gorcunove7438432014-05-09 22:34:34 +0400907 " or nasm -v (or --v) for version info\n\n"
H. Peter Anvin05990342018-06-11 13:32:42 -0700908 " -t assemble in SciTech TASM compatible mode\n");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000909 printf
H. Peter Anvin05990342018-06-11 13:32:42 -0700910 (" -E (or -e) preprocess only (writes output to stdout by default)\n"
911 " -a don't preprocess (assemble only)\n"
912 " -M generate Makefile dependencies on stdout\n"
913 " -MG d:o, missing files assumed generated\n"
914 " -MF file set Makefile dependency file\n"
915 " -MD file assemble and generate dependencies\n"
916 " -MT file dependency target name\n"
917 " -MQ file dependency target name (quoted)\n"
918 " -MP emit phony target\n\n"
919 " -Zfile redirect error messages to file\n"
920 " -s redirect error messages to stdout\n\n"
921 " -g generate debugging information\n\n"
922 " -F format select a debugging format\n\n"
923 " -gformat same as -g -F format\n\n"
924 " -o outfile write output to an outfile\n\n"
925 " -f format select an output format\n\n"
926 " -l listfile write listing to a listfile\n\n"
927 " -Ipath add a pathname to the include file path\n");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000928 printf
H. Peter Anvin05990342018-06-11 13:32:42 -0700929 (" -Olevel optimize opcodes, immediates and branch offsets\n"
930 " -O0 no optimization\n"
931 " -O1 minimal optimization\n"
932 " -Ox multipass optimization (default)\n"
933 " -Pfile pre-include a file (also --include)\n"
934 " -Dmacro[=str] pre-define a macro\n"
935 " -Umacro undefine a macro\n"
936 " -Xformat specifiy error reporting format (gnu or vc)\n"
937 " -w+foo enable warning foo (equiv. -Wfoo)\n"
938 " -w-foo disable warning foo (equiv. -Wno-foo)\n"
939 " -w[+-]error[=foo]\n"
940 " promote [specific] warnings to errors\n"
941 " -h show invocation summary and exit\n\n"
942 " --pragma str pre-executes a specific %%pragma\n"
943 " --before str add line (usually a preprocessor statement) before the input\n"
944 " --prefix str prepend the given string to all the given string\n"
945 " to all extern, common and global symbols\n"
946 " --suffix str append the given string to all the given string\n"
947 " to all extern, common and global symbols\n"
948 " --lprefix str prepend the given string to all other symbols\n"
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800949 "\n"
950 "Response files should contain command line parameters,\n"
951 "one per line.\n"
952 "\n"
953 "Warnings for the -W/-w options:\n");
954 for (i = 0; i <= ERR_WARN_ALL; i++)
955 printf(" %-23s %s%s\n",
H. Peter Anvin972079f2008-09-30 17:01:23 -0700956 warnings[i].name, warnings[i].help,
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800957 i == ERR_WARN_ALL ? "\n" :
958 warnings[i].enabled ? " (default on)" :
959 " (default off)");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000960 if (p[2] == 'f') {
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800961 printf("valid output formats for -f are"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000962 " (`*' denotes default):\n");
963 ofmt_list(ofmt, stdout);
964 } else {
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800965 printf("For a list of valid output formats, use -hf.\n");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000966 printf("For a list of debug formats, use -f <form> -y.\n");
967 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400968 exit(0); /* never need usage message here */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000969 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800970
H. Peter Anvine2c80182005-01-15 22:15:51 +0000971 case 'y':
972 printf("\nvalid debug formats for '%s' output format are"
973 " ('*' denotes default):\n", ofmt->shortname);
974 dfmt_list(ofmt, stdout);
975 exit(0);
976 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800977
H. Peter Anvine2c80182005-01-15 22:15:51 +0000978 case 't':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700979 if (pass == 2)
980 tasm_compatible_mode = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000981 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800982
H. Peter Anvine2c80182005-01-15 22:15:51 +0000983 case 'v':
Cyrill Gorcunove7438432014-05-09 22:34:34 +0400984 show_version();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000985 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800986
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400987 case 'e': /* preprocess only */
988 case 'E':
H. Peter Anvin55568c12016-10-03 19:46:49 -0700989 if (pass == 1)
990 operating_mode = OP_PREPROCESS;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000991 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800992
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400993 case 'a': /* assemble only - don't preprocess */
H. Peter Anvin55568c12016-10-03 19:46:49 -0700994 if (pass == 1)
995 preproc = &preproc_nop;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000996 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800997
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800998 case 'w':
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400999 case 'W':
H. Peter Anvin55568c12016-10-03 19:46:49 -07001000 if (pass == 2) {
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001001 if (!set_warning_status(param)) {
1002 nasm_error(ERR_WARNING|ERR_NOFILE|ERR_WARN_UNK_WARNING,
1003 "unknown warning option: %s", param);
H. Peter Anvin55568c12016-10-03 19:46:49 -07001004 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001005 }
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001006 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -08001007
H. Peter Anvine2c80182005-01-15 22:15:51 +00001008 case 'M':
H. Peter Anvin77c9bf62017-08-16 21:14:33 -07001009 if (pass == 1) {
1010 switch (p[2]) {
1011 case 'W':
1012 quote_for_make = quote_for_wmake;
1013 break;
1014 case 'D':
1015 case 'F':
1016 case 'T':
1017 case 'Q':
1018 advance = true;
1019 break;
1020 default:
1021 break;
1022 }
1023 } else {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001024 switch (p[2]) {
1025 case 0:
1026 operating_mode = OP_DEPEND;
1027 break;
1028 case 'G':
1029 operating_mode = OP_DEPEND;
1030 depend_missing_ok = true;
1031 break;
1032 case 'P':
1033 depend_emit_phony = true;
1034 break;
1035 case 'D':
1036 operating_mode = OP_NORMAL;
1037 depend_file = q;
1038 advance = true;
1039 break;
1040 case 'F':
1041 depend_file = q;
1042 advance = true;
1043 break;
1044 case 'T':
1045 depend_target = q;
1046 advance = true;
1047 break;
1048 case 'Q':
1049 depend_target = quote_for_make(q);
1050 advance = true;
1051 break;
H. Peter Anvin77c9bf62017-08-16 21:14:33 -07001052 case 'W':
1053 /* handled in pass 1 */
1054 break;
H. Peter Anvin55568c12016-10-03 19:46:49 -07001055 default:
1056 nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
1057 "unknown dependency option `-M%c'", p[2]);
1058 break;
1059 }
H. Peter Anvin77c9bf62017-08-16 21:14:33 -07001060 }
1061 if (advance && (!q || !q[0])) {
1062 nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
1063 "option `-M%c' requires a parameter", p[2]);
1064 break;
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001065 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001066 break;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001067
H. Peter Anvine2c80182005-01-15 22:15:51 +00001068 case '-':
1069 {
H. Peter Anvin3366e312018-02-07 14:14:36 -08001070 const struct textargs *tx;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001071
H. Peter Anvine2c80182005-01-15 22:15:51 +00001072 if (p[2] == 0) { /* -- => stop processing options */
H. Peter Anvin3366e312018-02-07 14:14:36 -08001073 stopoptions = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001074 break;
1075 }
Cyrill Gorcunove7438432014-05-09 22:34:34 +04001076
H. Peter Anvin3366e312018-02-07 14:14:36 -08001077 for (tx = textopts; tx->label; tx++) {
1078 if (!nasm_stricmp(p + 2, tx->label))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001079 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001080 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001081
H. Peter Anvin3366e312018-02-07 14:14:36 -08001082 if (tx->need_arg) {
1083 if (!q) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001084 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvin3366e312018-02-07 14:14:36 -08001085 "option `--%s' requires an argument",
1086 p + 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001087 break;
1088 }
H. Peter Anvin3366e312018-02-07 14:14:36 -08001089 advance = true;
1090 }
1091
1092 switch (tx->opt) {
1093 case OPT_VERSION:
1094 show_version();
1095 break;
1096 case OPT_ABORT_ON_PANIC:
1097 abort_on_panic = true;
1098 break;
H. Peter Anvin98578072018-06-01 18:02:54 -07001099 case OPT_MANGLE:
H. Peter Anvin3366e312018-02-07 14:14:36 -08001100 if (pass == 2)
H. Peter Anvin98578072018-06-01 18:02:54 -07001101 set_label_mangle(tx->pvt, q);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001102 break;
H. Peter Anvin05990342018-06-11 13:32:42 -07001103 case OPT_INCLUDE:
1104 if (pass == 2)
1105 preproc->pre_include(q);
1106 break;
1107 case OPT_PRAGMA:
1108 if (pass == 2)
1109 preproc->pre_command("pragma", q);
1110 break;
1111 case OPT_BEFORE:
1112 if (pass == 2)
1113 preproc->pre_command(NULL, q);
1114 break;
H. Peter Anvin3366e312018-02-07 14:14:36 -08001115 case OPT_BOGUS:
1116 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
1117 "unrecognized option `--%s'", p + 2);
1118 break;
1119 default:
1120 panic();
H. Peter Anvine2c80182005-01-15 22:15:51 +00001121 }
1122 break;
1123 }
1124
1125 default:
H. Peter Anvinac061332017-03-31 11:41:16 -07001126 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
1127 "unrecognised option `-%c'", p[1]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001128 break;
1129 }
H. Peter Anvin55568c12016-10-03 19:46:49 -07001130 } else if (pass == 2) {
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001131 /* In theory we could allow multiple input files... */
1132 copy_filename(&inname, p, "input");
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001133 }
1134
1135 return advance;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001136}
1137
H. Peter Anvineba20a72002-04-30 20:53:55 +00001138#define ARG_BUF_DELTA 128
1139
H. Peter Anvin55568c12016-10-03 19:46:49 -07001140static void process_respfile(FILE * rfile, int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001141{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001142 char *buffer, *p, *q, *prevarg;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001143 int bufsize, prevargsize;
1144
1145 bufsize = prevargsize = ARG_BUF_DELTA;
1146 buffer = nasm_malloc(ARG_BUF_DELTA);
1147 prevarg = nasm_malloc(ARG_BUF_DELTA);
1148 prevarg[0] = '\0';
1149
H. Peter Anvine2c80182005-01-15 22:15:51 +00001150 while (1) { /* Loop to handle all lines in file */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001151 p = buffer;
1152 while (1) { /* Loop to handle long lines */
1153 q = fgets(p, bufsize - (p - buffer), rfile);
1154 if (!q)
1155 break;
1156 p += strlen(p);
1157 if (p > buffer && p[-1] == '\n')
1158 break;
1159 if (p - buffer > bufsize - 10) {
1160 int offset;
1161 offset = p - buffer;
1162 bufsize += ARG_BUF_DELTA;
1163 buffer = nasm_realloc(buffer, bufsize);
1164 p = buffer + offset;
1165 }
1166 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001167
H. Peter Anvine2c80182005-01-15 22:15:51 +00001168 if (!q && p == buffer) {
1169 if (prevarg[0])
H. Peter Anvin55568c12016-10-03 19:46:49 -07001170 process_arg(prevarg, NULL, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001171 nasm_free(buffer);
1172 nasm_free(prevarg);
1173 return;
1174 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001175
H. Peter Anvine2c80182005-01-15 22:15:51 +00001176 /*
1177 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
1178 * them are present at the end of the line.
1179 */
1180 *(p = &buffer[strcspn(buffer, "\r\n\032")]) = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001181
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001182 while (p > buffer && nasm_isspace(p[-1]))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001183 *--p = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001184
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +04001185 p = nasm_skip_spaces(buffer);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001186
H. Peter Anvin55568c12016-10-03 19:46:49 -07001187 if (process_arg(prevarg, p, pass))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001188 *p = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001189
Charles Crayne192d5b52007-10-18 19:02:42 -07001190 if ((int) strlen(p) > prevargsize - 10) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001191 prevargsize += ARG_BUF_DELTA;
1192 prevarg = nasm_realloc(prevarg, prevargsize);
1193 }
H. Peter Anvindc242712007-11-18 11:55:10 -08001194 strncpy(prevarg, p, prevargsize);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001195 }
1196}
1197
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001198/* Function to process args from a string of args, rather than the
1199 * argv array. Used by the environment variable and response file
1200 * processing.
1201 */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001202static void process_args(char *args, int pass)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001203{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001204 char *p, *q, *arg, *prevarg;
1205 char separator = ' ';
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001206
1207 p = args;
1208 if (*p && *p != '-')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001209 separator = *p++;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001210 arg = NULL;
1211 while (*p) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001212 q = p;
1213 while (*p && *p != separator)
1214 p++;
1215 while (*p == separator)
1216 *p++ = '\0';
1217 prevarg = arg;
1218 arg = q;
H. Peter Anvin55568c12016-10-03 19:46:49 -07001219 if (process_arg(prevarg, arg, pass))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001220 arg = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001221 }
1222 if (arg)
H. Peter Anvin55568c12016-10-03 19:46:49 -07001223 process_arg(arg, NULL, pass);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001224}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001225
H. Peter Anvin55568c12016-10-03 19:46:49 -07001226static void process_response_file(const char *file, int pass)
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001227{
1228 char str[2048];
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001229 FILE *f = nasm_open_read(file, NF_TEXT);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001230 if (!f) {
Cyrill Gorcunovf1964512013-02-15 12:14:06 +04001231 perror(file);
1232 exit(-1);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001233 }
1234 while (fgets(str, sizeof str, f)) {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001235 process_args(str, pass);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001236 }
1237 fclose(f);
1238}
1239
H. Peter Anvin55568c12016-10-03 19:46:49 -07001240static void parse_cmdline(int argc, char **argv, int pass)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001241{
1242 FILE *rfile;
Cyrill Gorcunovf4941892011-07-17 13:55:25 +04001243 char *envreal, *envcopy = NULL, *p;
H. Peter Anvin972079f2008-09-30 17:01:23 -07001244 int i;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001245
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001246 /* Initialize all the warnings to their default state */
1247 for (i = 0; i < ERR_WARN_ALL; i++) {
1248 warning_state_init[i] = warning_state[i] =
1249 warnings[i].enabled ? WARN_ST_ENABLED : 0;
1250 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001251
1252 /*
H. Peter Anvinff7ccc02002-05-06 19:41:57 +00001253 * First, process the NASMENV environment variable.
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001254 */
H. Peter Anvinff7ccc02002-05-06 19:41:57 +00001255 envreal = getenv("NASMENV");
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001256 if (envreal) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001257 envcopy = nasm_strdup(envreal);
H. Peter Anvin55568c12016-10-03 19:46:49 -07001258 process_args(envcopy, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001259 nasm_free(envcopy);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001260 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001261
1262 /*
1263 * Now process the actual command line.
1264 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001265 while (--argc) {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001266 bool advance;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001267 argv++;
1268 if (argv[0][0] == '@') {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001269 /*
1270 * We have a response file, so process this as a set of
H. Peter Anvine2c80182005-01-15 22:15:51 +00001271 * arguments like the environment variable. This allows us
1272 * to have multiple arguments on a single line, which is
1273 * different to the -@resp file processing below for regular
1274 * NASM.
1275 */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001276 process_response_file(argv[0]+1, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001277 argc--;
1278 argv++;
1279 }
1280 if (!stopoptions && argv[0][0] == '-' && argv[0][1] == '@') {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001281 p = get_param(argv[0], argc > 1 ? argv[1] : NULL, &advance);
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001282 if (p) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001283 rfile = nasm_open_read(p, NF_TEXT);
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001284 if (rfile) {
H. Peter Anvin55568c12016-10-03 19:46:49 -07001285 process_respfile(rfile, pass);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001286 fclose(rfile);
1287 } else
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001288 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001289 "unable to open response file `%s'", p);
1290 }
1291 } else
H. Peter Anvin55568c12016-10-03 19:46:49 -07001292 advance = process_arg(argv[0], argc > 1 ? argv[1] : NULL, pass);
H. Peter Anvin423e3812007-11-15 17:12:29 -08001293 argv += advance, argc -= advance;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001294 }
1295
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001296 /*
1297 * Look for basic command line typos. This definitely doesn't
1298 * catch all errors, but it might help cases of fumbled fingers.
1299 */
H. Peter Anvin55568c12016-10-03 19:46:49 -07001300 if (pass != 2)
1301 return;
1302
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001303 if (!inname)
1304 nasm_fatal(ERR_NOFILE | ERR_USAGE, "no input file specified");
H. Peter Anvin59ddd262007-10-01 11:26:31 -07001305
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001306 else if ((errname && !strcmp(inname, errname)) ||
1307 (outname && !strcmp(inname, outname)) ||
1308 (listname && !strcmp(inname, listname)) ||
1309 (depend_file && !strcmp(inname, depend_file)))
1310 nasm_fatal(ERR_USAGE, "will not overwrite input file");
1311
1312 if (errname) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001313 error_file = nasm_open_write(errname, NF_TEXT);
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001314 if (!error_file) {
1315 error_file = stderr; /* Revert to default! */
H. Peter Anvin41087062016-03-03 14:27:34 -08001316 nasm_fatal(ERR_NOFILE | ERR_USAGE,
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001317 "cannot open file `%s' for error messages",
1318 errname);
1319 }
Charles Craynefcce07f2007-09-30 22:15:36 -07001320 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001321}
1322
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001323static void assemble_file(const char *fname, StrList **depend_ptr)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001324{
H. Peter Anvinc7131682017-03-07 17:45:01 -08001325 char *line;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001326 insn output_ins;
H. Peter Anvinc7131682017-03-07 17:45:01 -08001327 int i;
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001328 int pass_max;
H. Peter Anvin9c595b62017-03-07 19:44:21 -08001329 uint64_t prev_offset_changed;
1330 unsigned int stall_count = 0; /* Make sure we make forward progress... */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001331
H. Peter Anvina7ecf262018-02-06 14:43:07 -08001332 switch (cmd_sb) {
1333 case 16:
1334 break;
1335 case 32:
1336 if (!iflag_cpu_level_ok(&cmd_cpu, IF_386))
1337 nasm_fatal(0, "command line: 32-bit segment size requires a higher cpu");
1338 break;
1339 case 64:
1340 if (!iflag_cpu_level_ok(&cmd_cpu, IF_X86_64))
1341 nasm_fatal(0, "command line: 64-bit segment size requires a higher cpu");
1342 break;
1343 default:
1344 panic();
1345 break;
1346 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001347
H. Peter Anvin73482482018-06-11 14:54:14 -07001348 /* Any segment numbers allocated before this point are permanent */
1349 seg_alloc_setup_done();
1350
Charles Craynec1905c22008-09-11 18:54:06 -07001351 pass_max = prev_offset_changed = (INT_MAX >> 1) + 2; /* Almost unlimited */
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001352 for (passn = 1; pass0 <= 2; passn++) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001353 pass1 = pass0 == 2 ? 2 : 1; /* 1, 1, 1, ..., 1, 2 */
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001354 pass2 = passn > 1 ? 2 : 1; /* 1, 2, 2, ..., 2, 2 */
1355 /* pass0 0, 0, 0, ..., 1, 2 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00001356
H. Peter Anvincac0b192017-03-28 16:12:30 -07001357 globalbits = cmd_sb; /* set 'bits' to command line default */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001358 cpu = cmd_cpu;
1359 if (pass0 == 2) {
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08001360 lfmt->init(listname);
H. Peter Anvin81b62b92017-12-20 13:33:49 -08001361 } else if (passn == 1 && listname) {
H. Peter Anvinaac01ff2017-04-02 19:10:26 -07001362 /* Remove the list file in case we die before the output pass */
1363 remove(listname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001364 }
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001365 in_absolute = false;
Charles Craynec1905c22008-09-11 18:54:06 -07001366 global_offset_changed = 0; /* set by redefine_label */
H. Peter Anvin892c4812018-05-30 14:43:46 -07001367 seg_alloc_reset();
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001368 if (passn > 1) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001369 saa_rewind(forwrefs);
1370 forwref = saa_rstruct(forwrefs);
1371 raa_free(offsets);
1372 offsets = raa_init();
1373 }
H. Peter Anvin892c4812018-05-30 14:43:46 -07001374 location.segment = NO_SEG;
1375 location.offset = 0;
1376 if (passn == 1)
1377 location.known = true;
1378 ofmt->reset();
1379 switch_segment(ofmt->section(NULL, pass2, &globalbits));
H. Peter Anvin130736c2016-02-17 20:27:41 -08001380 preproc->reset(fname, pass1, pass1 == 2 ? depend_ptr : NULL);
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001381
1382 /* Revert all warnings to the default state */
1383 memcpy(warning_state, warning_state_init, sizeof warning_state);
Victor van den Elzen819703a2008-07-16 15:20:56 +02001384
H. Peter Anvine2c80182005-01-15 22:15:51 +00001385 globallineno = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001386
H. Peter Anvine2c80182005-01-15 22:15:51 +00001387 while ((line = preproc->getline())) {
Chang S. Baef0ceb1e2018-05-02 08:07:53 -07001388 if (globallineno++ == GLOBALLINENO_MAX)
1389 nasm_error(ERR_FATAL,
1390 "overall line number reaches the maximum %d\n",
1391 GLOBALLINENO_MAX);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001392
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001393 /*
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001394 * Here we parse our directives; this is not handled by the
H. Peter Anvinc7131682017-03-07 17:45:01 -08001395 * main parser.
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001396 */
H. Peter Anvina6e26d92017-03-07 21:32:37 -08001397 if (process_directives(line))
H. Peter Anvinc7131682017-03-07 17:45:01 -08001398 goto end_of_line; /* Just do final cleanup */
H. Peter Anvin62b24d72007-08-29 16:38:05 +00001399
H. Peter Anvinc7131682017-03-07 17:45:01 -08001400 /* Not a directive, or even something that starts with [ */
H. Peter Anvin98578072018-06-01 18:02:54 -07001401 parse_line(pass1, line, &output_ins);
H. Peter Anvinc7131682017-03-07 17:45:01 -08001402
1403 if (optimizing > 0) {
1404 if (forwref != NULL && globallineno == forwref->lineno) {
1405 output_ins.forw_ref = true;
1406 do {
1407 output_ins.oprs[forwref->operand].opflags |= OPFLAG_FORWARD;
1408 forwref = saa_rstruct(forwrefs);
1409 } while (forwref != NULL
1410 && forwref->lineno == globallineno);
1411 } else
1412 output_ins.forw_ref = false;
1413
1414 if (output_ins.forw_ref) {
1415 if (passn == 1) {
1416 for (i = 0; i < output_ins.operands; i++) {
1417 if (output_ins.oprs[i].opflags & OPFLAG_FORWARD) {
1418 struct forwrefinfo *fwinf = (struct forwrefinfo *)saa_wstruct(forwrefs);
1419 fwinf->lineno = globallineno;
1420 fwinf->operand = i;
Cyrill Gorcunovd5f2aef2010-04-20 15:33:45 +04001421 }
1422 }
1423 }
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001424 }
H. Peter Anvinc7131682017-03-07 17:45:01 -08001425 }
H. Peter Anvin8bec2c72009-08-08 13:49:00 -07001426
H. Peter Anvinc7131682017-03-07 17:45:01 -08001427 /* forw_ref */
1428 if (output_ins.opcode == I_EQU) {
H. Peter Anvin98578072018-06-01 18:02:54 -07001429 if (!output_ins.label)
1430 nasm_error(ERR_NONFATAL,
1431 "EQU not preceded by label");
H. Peter Anvin73482482018-06-11 14:54:14 -07001432
H. Peter Anvin98578072018-06-01 18:02:54 -07001433 if (output_ins.operands == 1 &&
1434 (output_ins.oprs[0].type & IMMEDIATE) &&
1435 output_ins.oprs[0].wrt == NO_SEG) {
1436 define_label(output_ins.label,
1437 output_ins.oprs[0].segment,
1438 output_ins.oprs[0].offset, false);
1439 } else if (output_ins.operands == 2
1440 && (output_ins.oprs[0].type & IMMEDIATE)
1441 && (output_ins.oprs[0].type & COLON)
1442 && output_ins.oprs[0].segment == NO_SEG
1443 && output_ins.oprs[0].wrt == NO_SEG
1444 && (output_ins.oprs[1].type & IMMEDIATE)
1445 && output_ins.oprs[1].segment == NO_SEG
1446 && output_ins.oprs[1].wrt == NO_SEG) {
1447 define_label(output_ins.label,
1448 output_ins.oprs[0].offset | SEG_ABS,
1449 output_ins.oprs[1].offset, false);
H. Peter Anvinc7131682017-03-07 17:45:01 -08001450 } else {
H. Peter Anvin98578072018-06-01 18:02:54 -07001451 nasm_error(ERR_NONFATAL, "bad syntax for EQU");
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001452 }
H. Peter Anvinc7131682017-03-07 17:45:01 -08001453 } else { /* instruction isn't an EQU */
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001454 int32_t n;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001455
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001456 nasm_assert(output_ins.times >= 0);
H. Peter Anvinc7131682017-03-07 17:45:01 -08001457
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001458 for (n = 1; n <= output_ins.times; n++) {
1459 if (pass1 == 1) {
H. Peter Anvin892c4812018-05-30 14:43:46 -07001460 int64_t l = insn_size(location.segment,
1461 location.offset,
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001462 globalbits, &output_ins);
1463
1464 /* if (using_debug_info) && output_ins.opcode != -1) */
1465 if (using_debug_info)
1466 { /* fbk 03/25/01 */
H. Peter Anvinc7131682017-03-07 17:45:01 -08001467 /* this is done here so we can do debug type info */
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001468 int32_t typeinfo =
1469 TYS_ELEMENTS(output_ins.operands);
1470 switch (output_ins.opcode) {
1471 case I_RESB:
1472 typeinfo =
1473 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_BYTE;
1474 break;
1475 case I_RESW:
1476 typeinfo =
1477 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_WORD;
1478 break;
1479 case I_RESD:
1480 typeinfo =
1481 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_DWORD;
1482 break;
1483 case I_RESQ:
1484 typeinfo =
1485 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_QWORD;
1486 break;
1487 case I_REST:
1488 typeinfo =
1489 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_TBYTE;
1490 break;
1491 case I_RESO:
1492 typeinfo =
1493 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_OWORD;
1494 break;
1495 case I_RESY:
1496 typeinfo =
1497 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_YWORD;
1498 break;
1499 case I_RESZ:
1500 typeinfo =
1501 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_ZWORD;
1502 break;
1503 case I_DB:
1504 typeinfo |= TY_BYTE;
1505 break;
1506 case I_DW:
1507 typeinfo |= TY_WORD;
1508 break;
1509 case I_DD:
1510 if (output_ins.eops_float)
1511 typeinfo |= TY_FLOAT;
1512 else
1513 typeinfo |= TY_DWORD;
1514 break;
1515 case I_DQ:
1516 typeinfo |= TY_QWORD;
1517 break;
1518 case I_DT:
1519 typeinfo |= TY_TBYTE;
1520 break;
1521 case I_DO:
1522 typeinfo |= TY_OWORD;
1523 break;
1524 case I_DY:
1525 typeinfo |= TY_YWORD;
1526 break;
1527 case I_DZ:
1528 typeinfo |= TY_ZWORD;
1529 break;
1530 default:
1531 typeinfo = TY_LABEL;
1532 break;
1533 }
H. Peter Anvinc7131682017-03-07 17:45:01 -08001534
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001535 dfmt->debug_typevalue(typeinfo);
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001536 }
H. Peter Anvinc7131682017-03-07 17:45:01 -08001537
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001538 /*
1539 * For INCBIN, let the code in assemble
1540 * handle TIMES, so we don't have to read the
1541 * input file over and over.
1542 */
1543 if (l != -1) {
H. Peter Anvin892c4812018-05-30 14:43:46 -07001544 increment_offset(l);
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001545 }
1546 /*
1547 * else l == -1 => invalid instruction, which will be
1548 * flagged as an error on pass 2
1549 */
1550 } else {
1551 if (n == 2)
1552 lfmt->uplevel(LIST_TIMES);
H. Peter Anvin892c4812018-05-30 14:43:46 -07001553 increment_offset(assemble(location.segment,
1554 location.offset,
1555 globalbits, &output_ins));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001556 }
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001557 } /* not an EQU */
1558 }
1559 if (output_ins.times > 1)
1560 lfmt->downlevel(LIST_TIMES);
H. Peter Anvinc7131682017-03-07 17:45:01 -08001561
H. Peter Anvinc7131682017-03-07 17:45:01 -08001562 cleanup_insn(&output_ins);
1563
1564 end_of_line:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001565 nasm_free(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001566 } /* end while (line = preproc->getline... */
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001567
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001568 if (pass0 == 2 && global_offset_changed && !terminate_after_phase)
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001569 nasm_error(ERR_NONFATAL,
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001570 "phase error detected at end of assembly.");
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001571
H. Peter Anvine2c80182005-01-15 22:15:51 +00001572 if (pass1 == 1)
1573 preproc->cleanup(1);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001574
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001575 if ((passn > 1 && !global_offset_changed) || pass0 == 2) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001576 pass0++;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001577 } else if (global_offset_changed &&
1578 global_offset_changed < prev_offset_changed) {
Charles Craynec1905c22008-09-11 18:54:06 -07001579 prev_offset_changed = global_offset_changed;
1580 stall_count = 0;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001581 } else {
1582 stall_count++;
1583 }
Victor van den Elzen42528232008-09-11 15:07:05 +02001584
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001585 if (terminate_after_phase)
1586 break;
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001587
H. Peter Anvin9c595b62017-03-07 19:44:21 -08001588 if ((stall_count > 997U) || (passn >= pass_max)) {
Victor van den Elzen42528232008-09-11 15:07:05 +02001589 /* We get here if the labels don't converge
1590 * Example: FOO equ FOO + 1
1591 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001592 nasm_error(ERR_NONFATAL,
Victor van den Elzen42528232008-09-11 15:07:05 +02001593 "Can't find valid values for all labels "
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001594 "after %d passes, giving up.", passn);
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001595 nasm_error(ERR_NONFATAL,
1596 "Possible causes: recursive EQUs, macro abuse.");
1597 break;
1598 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001599 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001600
H. Peter Anvine2c80182005-01-15 22:15:51 +00001601 preproc->cleanup(0);
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08001602 lfmt->cleanup();
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001603 if (!terminate_after_phase && opt_verbose_info) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001604 /* -On and -Ov switches */
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001605 fprintf(stdout, "info: assembly required 1+%d+1 passes\n", passn-3);
1606 }
1607}
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001608
Ed Berosetfa771012002-06-09 20:56:40 +00001609/**
1610 * gnu style error reporting
1611 * This function prints an error message to error_file in the
1612 * style used by GNU. An example would be:
1613 * file.asm:50: error: blah blah blah
H. Peter Anvin70653092007-10-19 14:42:29 -07001614 * where file.asm is the name of the file, 50 is the line number on
Ed Berosetfa771012002-06-09 20:56:40 +00001615 * which the error occurs (or is detected) and "error:" is one of
1616 * the possible optional diagnostics -- it can be "error" or "warning"
H. Peter Anvin70653092007-10-19 14:42:29 -07001617 * or something else. Finally the line terminates with the actual
Ed Berosetfa771012002-06-09 20:56:40 +00001618 * error message.
H. Peter Anvin70653092007-10-19 14:42:29 -07001619 *
1620 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001621 * @param fmt the printf style format string
1622 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001623static void nasm_verror_gnu(int severity, const char *fmt, va_list ap)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001624{
H. Peter Anvin274cda82016-05-10 02:56:29 -07001625 const char *currentfile = NULL;
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001626 int32_t lineno = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001627
Ed Berosetfa771012002-06-09 20:56:40 +00001628 if (is_suppressed_warning(severity))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001629 return;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001630
H. Peter Anvin7a6bf742017-12-20 11:54:31 -08001631 if (!(severity & ERR_NOFILE)) {
H. Peter Anvin4def1a82016-05-09 13:59:44 -07001632 src_get(&lineno, &currentfile);
H. Peter Anvin7a6bf742017-12-20 11:54:31 -08001633 if (!currentfile || (severity & ERR_TOPFILE)) {
1634 currentfile = inname[0] ? inname : outname[0] ? outname : NULL;
1635 lineno = 0;
1636 }
1637 }
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001638
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001639 if (!skip_this_pass(severity)) {
H. Peter Anvin7a6bf742017-12-20 11:54:31 -08001640 if (!lineno)
1641 fprintf(error_file, "%s:", currentfile ? currentfile : "nasm");
H. Peter Anvin883985d2017-12-20 11:32:39 -08001642 else
1643 fprintf(error_file, "%s:%"PRId32": ", currentfile, lineno);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001644 }
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001645
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001646 nasm_verror_common(severity, fmt, ap);
Ed Berosetfa771012002-06-09 20:56:40 +00001647}
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001648
Ed Berosetfa771012002-06-09 20:56:40 +00001649/**
1650 * MS style error reporting
1651 * This function prints an error message to error_file in the
H. Peter Anvin70653092007-10-19 14:42:29 -07001652 * style used by Visual C and some other Microsoft tools. An example
Ed Berosetfa771012002-06-09 20:56:40 +00001653 * would be:
Ed Beroset6e61d0d2002-06-11 03:29:36 +00001654 * file.asm(50) : error: blah blah blah
H. Peter Anvin70653092007-10-19 14:42:29 -07001655 * where file.asm is the name of the file, 50 is the line number on
Ed Beroset6e61d0d2002-06-11 03:29:36 +00001656 * which the error occurs (or is detected) and "error:" is one of
1657 * the possible optional diagnostics -- it can be "error" or "warning"
H. Peter Anvin70653092007-10-19 14:42:29 -07001658 * or something else. Finally the line terminates with the actual
Ed Beroset6e61d0d2002-06-11 03:29:36 +00001659 * error message.
H. Peter Anvin70653092007-10-19 14:42:29 -07001660 *
1661 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001662 * @param fmt the printf style format string
1663 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001664static void nasm_verror_vc(int severity, const char *fmt, va_list ap)
Ed Berosetfa771012002-06-09 20:56:40 +00001665{
H. Peter Anvin274cda82016-05-10 02:56:29 -07001666 const char *currentfile = NULL;
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001667 int32_t lineno = 0;
Ed Berosetfa771012002-06-09 20:56:40 +00001668
H. Peter Anvine2c80182005-01-15 22:15:51 +00001669 if (is_suppressed_warning(severity))
1670 return;
Ed Berosetfa771012002-06-09 20:56:40 +00001671
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001672 if (!(severity & ERR_NOFILE))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001673 src_get(&lineno, &currentfile);
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001674
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001675 if (!skip_this_pass(severity)) {
1676 if (currentfile) {
1677 fprintf(error_file, "%s(%"PRId32") : ", currentfile, lineno);
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001678 } else {
1679 fputs("nasm: ", error_file);
1680 }
Ed Berosetfa771012002-06-09 20:56:40 +00001681 }
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001682
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001683 nasm_verror_common(severity, fmt, ap);
Ed Berosetfa771012002-06-09 20:56:40 +00001684}
1685
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001686/*
1687 * check to see if this is a suppressable warning
1688 */
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001689static inline bool is_valid_warning(int severity)
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001690{
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001691 /* Not a warning at all */
1692 if ((severity & ERR_MASK) != ERR_WARNING)
1693 return false;
1694
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001695 return WARN_IDX(severity) < ERR_WARN_ALL;
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001696}
1697
Ed Berosetfa771012002-06-09 20:56:40 +00001698/**
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001699 * check for suppressed warning
H. Peter Anvin70653092007-10-19 14:42:29 -07001700 * checks for suppressed warning or pass one only warning and we're
Ed Berosetfa771012002-06-09 20:56:40 +00001701 * not in pass 1
1702 *
1703 * @param severity the severity of the warning or error
1704 * @return true if we should abort error/warning printing
1705 */
H. Peter Anvin2b046cf2008-01-22 14:08:36 -08001706static bool is_suppressed_warning(int severity)
Ed Berosetfa771012002-06-09 20:56:40 +00001707{
H. Peter Anvin442a05a2012-02-24 21:50:53 -08001708 /* Might be a warning but suppresed explicitly */
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001709 if (is_valid_warning(severity))
1710 return !(warning_state[WARN_IDX(severity)] & WARN_ST_ENABLED);
1711 else
1712 return false;
1713}
1714
1715static bool warning_is_error(int severity)
1716{
1717 if (is_valid_warning(severity))
1718 return !!(warning_state[WARN_IDX(severity)] & WARN_ST_ERROR);
H. Peter Anvin442a05a2012-02-24 21:50:53 -08001719 else
1720 return false;
Ed Berosetfa771012002-06-09 20:56:40 +00001721}
1722
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001723static bool skip_this_pass(int severity)
1724{
H. Peter Anvin8f622462017-04-02 19:02:29 -07001725 /*
1726 * See if it's a pass-specific error or warning which should be skipped.
1727 * We cannot skip errors stronger than ERR_NONFATAL as by definition
1728 * they cannot be resumed from.
1729 */
1730 if ((severity & ERR_MASK) > ERR_NONFATAL)
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001731 return false;
1732
H. Peter Anvin934f0472016-05-09 12:00:19 -07001733 /*
1734 * passn is 1 on the very first pass only.
1735 * pass0 is 2 on the code-generation (final) pass only.
1736 * These are the passes we care about in this case.
1737 */
1738 return (((severity & ERR_PASS1) && passn != 1) ||
1739 ((severity & ERR_PASS2) && pass0 != 2));
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001740}
1741
Ed Berosetfa771012002-06-09 20:56:40 +00001742/**
1743 * common error reporting
1744 * This is the common back end of the error reporting schemes currently
H. Peter Anvin70653092007-10-19 14:42:29 -07001745 * implemented. It prints the nature of the warning and then the
Ed Berosetfa771012002-06-09 20:56:40 +00001746 * specific error message to error_file and may or may not return. It
1747 * doesn't return if the error severity is a "panic" or "debug" type.
H. Peter Anvin70653092007-10-19 14:42:29 -07001748 *
1749 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001750 * @param fmt the printf style format string
1751 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001752static void nasm_verror_common(int severity, const char *fmt, va_list args)
Ed Berosetfa771012002-06-09 20:56:40 +00001753{
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001754 char msg[1024];
1755 const char *pfx;
H. Peter Anvin323fcff2009-07-12 12:04:56 -07001756
H. Peter Anvin7df04172008-06-10 18:27:38 -07001757 switch (severity & (ERR_MASK|ERR_NO_SEVERITY)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001758 case ERR_WARNING:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001759 pfx = "warning: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001760 break;
1761 case ERR_NONFATAL:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001762 pfx = "error: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001763 break;
1764 case ERR_FATAL:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001765 pfx = "fatal: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001766 break;
1767 case ERR_PANIC:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001768 pfx = "panic: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001769 break;
1770 case ERR_DEBUG:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001771 pfx = "debug: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001772 break;
H. Peter Anvin7df04172008-06-10 18:27:38 -07001773 default:
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001774 pfx = "";
1775 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001776 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00001777
H. Peter Anvin934f0472016-05-09 12:00:19 -07001778 vsnprintf(msg, sizeof msg - 64, fmt, args);
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001779 if (is_valid_warning(severity) && WARN_IDX(severity) != ERR_WARN_OTHER) {
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001780 char *p = strchr(msg, '\0');
H. Peter Anvin934f0472016-05-09 12:00:19 -07001781 snprintf(p, 64, " [-w+%s]", warnings[WARN_IDX(severity)].name);
1782 }
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001783
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001784 if (!skip_this_pass(severity))
1785 fprintf(error_file, "%s%s\n", pfx, msg);
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001786
H. Peter Anvin4def1a82016-05-09 13:59:44 -07001787 /* Are we recursing from error_list_macros? */
1788 if (severity & ERR_PP_LISTMACRO)
1789 return;
1790
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001791 /*
1792 * Don't suppress this with skip_this_pass(), or we don't get
H. Peter Anvin934f0472016-05-09 12:00:19 -07001793 * pass1 or preprocessor warnings in the list file
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001794 */
H. Peter Anvin0fcb4882016-07-06 11:55:25 -07001795 lfmt->error(severity, pfx, msg);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001796
H. Peter Anvin8f622462017-04-02 19:02:29 -07001797 if (skip_this_pass(severity))
1798 return;
1799
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001800 if (severity & ERR_USAGE)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001801 want_usage = true;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001802
H. Peter Anvin4def1a82016-05-09 13:59:44 -07001803 preproc->error_list_macros(severity);
1804
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001805 switch (severity & ERR_MASK) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001806 case ERR_DEBUG:
1807 /* no further action, by definition */
1808 break;
H. Peter Anvinb030c922007-11-13 11:31:15 -08001809 case ERR_WARNING:
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001810 /* Treat warnings as errors */
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08001811 if (warning_is_error(severity))
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001812 terminate_after_phase = true;
1813 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001814 case ERR_NONFATAL:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001815 terminate_after_phase = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001816 break;
1817 case ERR_FATAL:
1818 if (ofile) {
1819 fclose(ofile);
1820 remove(outname);
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001821 ofile = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001822 }
1823 if (want_usage)
1824 usage();
1825 exit(1); /* instantly die */
1826 break; /* placate silly compilers */
1827 case ERR_PANIC:
1828 fflush(NULL);
H. Peter Anvin3366e312018-02-07 14:14:36 -08001829
1830 if (abort_on_panic)
1831 abort(); /* halt, catch fire, dump core/stop debugger */
1832
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001833 if (ofile) {
1834 fclose(ofile);
1835 remove(outname);
1836 ofile = NULL;
1837 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001838 exit(3);
1839 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001840 }
1841}
1842
H. Peter Anvin734b1882002-04-30 21:01:08 +00001843static void usage(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001844{
H. Peter Anvin620515a2002-04-30 20:57:38 +00001845 fputs("type `nasm -h' for help\n", error_file);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001846}