blob: 6ed6a881b5ccb6104e7b35bad189b477f31dee7c [file] [log] [blame]
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07001/* ----------------------------------------------------------------------- *
H. Peter Anvin323fcff2009-07-12 12:04:56 -07002 *
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +01003 * Copyright 1996-2016 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 Anvin6b18bcc2008-02-16 14:54:10 -080046#include <time.h>
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000047
48#include "nasm.h"
49#include "nasmlib.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
Keith Kaniosa6dfa782007-04-13 16:47:53 +000077static int get_bits(char *value);
Cyrill Gorcunov08359152013-11-09 22:16:11 +040078static iflag_t get_cpu(char *cpu_str);
Keith Kaniosa6dfa782007-04-13 16:47:53 +000079static void parse_cmdline(int, char **);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -070080static void assemble_file(char *, StrList **);
H. Peter Anvin130736c2016-02-17 20:27:41 -080081static bool is_suppressed_warning(int severity);
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -080082static bool skip_this_pass(int severity);
H. Peter Anvin9bd15062009-07-18 21:07:17 -040083static void nasm_verror_gnu(int severity, const char *fmt, va_list args);
84static void nasm_verror_vc(int severity, const char *fmt, va_list args);
85static void nasm_verror_common(int severity, const char *fmt, va_list args);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000086static void usage(void);
87
H. Peter Anvin283b3fb2016-03-07 23:18:30 -080088static bool using_debug_info, opt_verbose_info;
89static const char *debug_format;
90
H. Peter Anvin6867acc2007-10-10 14:58:45 -070091bool tasm_compatible_mode = false;
H. Peter Anvin00835fe2008-01-08 23:03:57 -080092int pass0, passn;
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +000093int globalrel = 0;
Jin Kyu Songb287ff02013-12-04 20:05:55 -080094int globalbnd = 0;
H. Peter Anvineba20a72002-04-30 20:53:55 +000095
H. Peter Anvin9bd15062009-07-18 21:07:17 -040096static time_t official_compile_time;
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -080097
Keith Kaniosa6dfa782007-04-13 16:47:53 +000098static char inname[FILENAME_MAX];
99static char outname[FILENAME_MAX];
100static char listname[FILENAME_MAX];
Charles Craynefcce07f2007-09-30 22:15:36 -0700101static char errname[FILENAME_MAX];
H. Peter Anvine2c80182005-01-15 22:15:51 +0000102static int globallineno; /* for forward-reference tracking */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000103/* static int pass = 0; */
H. Peter Anvin338656c2016-02-17 20:59:22 -0800104const struct ofmt *ofmt = &OF_DEFAULT;
105const struct ofmt_alias *ofmt_alias = NULL;
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400106const struct dfmt *dfmt;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000107
H. Peter Anvine2c80182005-01-15 22:15:51 +0000108static FILE *error_file; /* Where to write error messages */
H. Peter Anvin620515a2002-04-30 20:57:38 +0000109
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400110FILE *ofile = NULL;
H. Peter Anvin31387b22010-07-15 18:28:52 -0700111int optimizing = MAX_OPTIMIZE; /* number of optimization passes to take */
112static int sb, cmd_sb = 16; /* by default */
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400113
114static iflag_t cpu;
115static iflag_t cmd_cpu;
116
Charles Craynec1905c22008-09-11 18:54:06 -0700117int64_t global_offset_changed; /* referenced in labels.c */
118int64_t prev_offset_changed;
119int32_t stall_count;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000120
H. Peter Anvincd7893d2016-02-18 01:25:46 -0800121struct location location;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000122int in_abs_seg; /* Flag we are in ABSOLUTE seg */
Keith Kaniosb7a89542007-04-12 02:40:54 +0000123int32_t abs_seg; /* ABSOLUTE segment basis */
124int32_t abs_offset; /* ABSOLUTE offset */
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000125
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000126static struct RAA *offsets;
H. Peter Anvinea838272002-04-30 20:51:53 +0000127
H. Peter Anvine2c80182005-01-15 22:15:51 +0000128static struct SAA *forwrefs; /* keep track of forward references */
H. Peter Anvin9d637df2007-10-04 13:42:56 -0700129static const struct forwrefinfo *forwref;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000130
H. Peter Anvine7469712016-02-18 02:20:59 -0800131static const struct preproc_ops *preproc;
Cyrill Gorcunov86b2ad02011-07-01 10:38:25 +0400132
Cyrill Gorcunove9fc88c2014-06-23 02:22:02 +0400133#define OP_NORMAL (1u << 0)
134#define OP_PREPROCESS (1u << 1)
135#define OP_DEPEND (1u << 2)
136
137static unsigned int operating_mode;
Cyrill Gorcunov3ed32cb2014-06-22 20:53:12 +0400138
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700139/* Dependency flags */
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700140static bool depend_emit_phony = false;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700141static bool depend_missing_ok = false;
142static const char *depend_target = NULL;
143static const char *depend_file = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000144
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000145/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000146 * Which of the suppressible warnings are suppressed. Entry zero
H. Peter Anvinb030c922007-11-13 11:31:15 -0800147 * isn't an actual warning, but it used for -w+error/-Werror.
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000148 */
Victor van den Elzen819703a2008-07-16 15:20:56 +0200149
H. Peter Anvin972079f2008-09-30 17:01:23 -0700150static bool warning_on[ERR_WARN_MAX+1]; /* Current state */
151static bool warning_on_global[ERR_WARN_MAX+1]; /* Command-line state */
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000152
H. Peter Anvin972079f2008-09-30 17:01:23 -0700153static const struct warning {
154 const char *name;
155 const char *help;
156 bool enabled;
157} warnings[ERR_WARN_MAX+1] = {
158 {"error", "treat warnings as errors", false},
159 {"macro-params", "macro calls with wrong parameter count", true},
160 {"macro-selfref", "cyclic macro references", false},
161 {"macro-defaults", "macros with more default than optional parameters", true},
162 {"orphan-labels", "labels alone on lines without trailing `:'", true},
H. Peter Anvin9a65f712008-10-26 08:59:04 -0700163 {"number-overflow", "numeric constant does not fit", true},
H. Peter Anvin972079f2008-09-30 17:01:23 -0700164 {"gnu-elf-extensions", "using 8- or 16-bit relocation in ELF32, a GNU extension", false},
165 {"float-overflow", "floating point overflow", true},
166 {"float-denorm", "floating point denormal", false},
167 {"float-underflow", "floating point underflow", false},
168 {"float-toolong", "too many digits in floating-point number", true},
169 {"user", "%warning directives", true},
H. Peter Anvin5a24fdd2012-02-25 15:10:04 -0800170 {"lock", "lock prefix on unlockable instructions", true},
171 {"hle", "invalid hle prefixes", true},
Jin Kyu Songbb8cf3f2013-11-29 00:38:29 -0800172 {"bnd", "invalid bnd prefixes", true},
H. Peter Anvinecc9e0e2016-02-11 20:29:34 -0800173 {"zext-reloc", "relocation zero-extended to match output format", true},
H. Peter Anvin69550ea2016-05-09 12:05:56 -0700174 {"ptr", "non-NASM keyword used in other assemblers", true},
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000175};
176
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700177static bool want_usage;
178static bool terminate_after_phase;
H. Peter Anvin130736c2016-02-17 20:27:41 -0800179bool user_nolist = false;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000180
H. Peter Anvin55340992012-09-09 17:09:00 -0700181static char *quote_for_make(const char *str);
182
Cyrill Gorcunov190232f2013-02-15 12:35:04 +0400183static int64_t get_curr_offs(void)
184{
185 return in_abs_seg ? abs_offset : raa_read(offsets, location.segment);
186}
187
188static void set_curr_offs(int64_t l_off)
189{
190 if (in_abs_seg)
191 abs_offset = l_off;
192 else
193 offsets = raa_write(offsets, location.segment, l_off);
194}
195
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000196static void nasm_fputs(const char *line, FILE * outfile)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000197{
H. Peter Anvin310b3e12002-05-14 22:38:55 +0000198 if (outfile) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000199 fputs(line, outfile);
H. Peter Anvind1fb15c2007-11-13 09:37:59 -0800200 putc('\n', outfile);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000201 } else
H. Peter Anvine2c80182005-01-15 22:15:51 +0000202 puts(line);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000203}
204
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800205/* Convert a struct tm to a POSIX-style time constant */
H. Peter Anvin724719b2015-01-05 15:17:56 -0800206static int64_t make_posix_time(struct tm *tm)
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800207{
208 int64_t t;
209 int64_t y = tm->tm_year;
210
211 /* See IEEE 1003.1:2004, section 4.14 */
212
213 t = (y-70)*365 + (y-69)/4 - (y-1)/100 + (y+299)/400;
214 t += tm->tm_yday;
215 t *= 24;
216 t += tm->tm_hour;
217 t *= 60;
218 t += tm->tm_min;
219 t *= 60;
220 t += tm->tm_sec;
221
222 return t;
223}
224
225static void define_macros_early(void)
226{
227 char temp[128];
228 struct tm lt, *lt_p, gm, *gm_p;
229 int64_t posix_time;
230
231 lt_p = localtime(&official_compile_time);
232 if (lt_p) {
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400233 lt = *lt_p;
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800234
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400235 strftime(temp, sizeof temp, "__DATE__=\"%Y-%m-%d\"", &lt);
236 preproc->pre_define(temp);
237 strftime(temp, sizeof temp, "__DATE_NUM__=%Y%m%d", &lt);
238 preproc->pre_define(temp);
239 strftime(temp, sizeof temp, "__TIME__=\"%H:%M:%S\"", &lt);
240 preproc->pre_define(temp);
241 strftime(temp, sizeof temp, "__TIME_NUM__=%H%M%S", &lt);
242 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800243 }
244
245 gm_p = gmtime(&official_compile_time);
246 if (gm_p) {
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400247 gm = *gm_p;
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800248
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400249 strftime(temp, sizeof temp, "__UTC_DATE__=\"%Y-%m-%d\"", &gm);
250 preproc->pre_define(temp);
251 strftime(temp, sizeof temp, "__UTC_DATE_NUM__=%Y%m%d", &gm);
252 preproc->pre_define(temp);
253 strftime(temp, sizeof temp, "__UTC_TIME__=\"%H:%M:%S\"", &gm);
254 preproc->pre_define(temp);
255 strftime(temp, sizeof temp, "__UTC_TIME_NUM__=%H%M%S", &gm);
256 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800257 }
H. Peter Anvind85d2502008-05-04 17:53:31 -0700258
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800259 if (gm_p)
H. Peter Anvin724719b2015-01-05 15:17:56 -0800260 posix_time = make_posix_time(&gm);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800261 else if (lt_p)
H. Peter Anvin724719b2015-01-05 15:17:56 -0800262 posix_time = make_posix_time(&lt);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800263 else
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400264 posix_time = 0;
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800265
266 if (posix_time) {
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400267 snprintf(temp, sizeof temp, "__POSIX_TIME__=%"PRId64, posix_time);
268 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800269 }
270}
271
272static void define_macros_late(void)
273{
274 char temp[128];
275
Cyrill Gorcunovc1936da2011-04-06 18:32:15 +0400276 /*
277 * In case if output format is defined by alias
278 * we have to put shortname of the alias itself here
279 * otherwise ABI backward compatibility gets broken.
280 */
Cyrill Gorcunov69ce7502010-07-12 15:19:17 +0400281 snprintf(temp, sizeof(temp), "__OUTPUT_FORMAT__=%s",
Cyrill Gorcunovc1936da2011-04-06 18:32:15 +0400282 ofmt_alias ? ofmt_alias->shortname : ofmt->shortname);
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +0400283 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800284}
285
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700286static void emit_dependencies(StrList *list)
287{
288 FILE *deps;
289 int linepos, len;
290 StrList *l, *nl;
291
292 if (depend_file && strcmp(depend_file, "-")) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -0700293 deps = nasm_open_write(depend_file, NF_TEXT);
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400294 if (!deps) {
295 nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
296 "unable to write dependency file `%s'", depend_file);
297 return;
298 }
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700299 } else {
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400300 deps = stdout;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700301 }
H. Peter Anvin323fcff2009-07-12 12:04:56 -0700302
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700303 linepos = fprintf(deps, "%s:", depend_target);
Cyrill Gorcunovfcd0a742009-07-27 16:26:26 +0400304 list_for_each(l, list) {
H. Peter Anvin55340992012-09-09 17:09:00 -0700305 char *file = quote_for_make(l->str);
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400306 len = strlen(file);
307 if (linepos + len > 62 && linepos > 1) {
308 fprintf(deps, " \\\n ");
309 linepos = 1;
310 }
311 fprintf(deps, " %s", file);
312 linepos += len+1;
H. Peter Anvin55340992012-09-09 17:09:00 -0700313 nasm_free(file);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700314 }
315 fprintf(deps, "\n\n");
H. Peter Anvin323fcff2009-07-12 12:04:56 -0700316
Cyrill Gorcunovfcd0a742009-07-27 16:26:26 +0400317 list_for_each_safe(l, nl, list) {
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400318 if (depend_emit_phony)
319 fprintf(deps, "%s:\n\n", l->str);
320 nasm_free(l);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700321 }
322
323 if (deps != stdout)
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400324 fclose(deps);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700325}
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 Anvin9e1f5282008-05-29 21:38:00 -0700329 StrList *depend_list = NULL, **depend_ptr;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700330
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800331 time(&official_compile_time);
332
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400333 iflag_set(&cpu, IF_PLEVEL);
334 iflag_set(&cmd_cpu, IF_PLEVEL);
335
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 Anvin6b18bcc2008-02-16 14:54:10 -0800351 /* Define some macros dependent on the runtime, but not
352 on the command line. */
353 define_macros_early();
354
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000355 parse_cmdline(argc, argv);
356
H. Peter Anvine2c80182005-01-15 22:15:51 +0000357 if (terminate_after_phase) {
358 if (want_usage)
359 usage();
360 return 1;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000361 }
362
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800363 if (!using_debug_info) {
364 /* No debug info, redirect to the null backend (empty stubs) */
H. Peter Anvina7bc15d2016-02-17 20:55:08 -0800365 dfmt = &null_debug_form;
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800366 } else if (!debug_format) {
367 /* Default debug format for this backend */
H. Peter Anvina7bc15d2016-02-17 20:55:08 -0800368 dfmt = ofmt->default_dfmt;
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800369 } else {
370 dfmt = dfmt_find(ofmt, debug_format);
371 if (!dfmt) {
372 nasm_fatal(ERR_NOFILE | ERR_USAGE,
373 "unrecognized debug format `%s' for"
374 " output format `%s'",
375 debug_format, ofmt->shortname);
376 }
377 }
H. Peter Anvinbb88d012003-09-10 23:34:23 +0000378
H. Peter Anvin76690a12002-04-30 20:52:49 +0000379 if (ofmt->stdmac)
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +0400380 preproc->extra_stdmac(ofmt->stdmac);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000381
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000382 /* define some macros dependent of command-line */
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800383 define_macros_late();
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000384
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400385 depend_ptr = (depend_file || (operating_mode & OP_DEPEND)) ? &depend_list : NULL;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700386 if (!depend_target)
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400387 depend_target = quote_for_make(outname);
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700388
Cyrill Gorcunove9fc88c2014-06-23 02:22:02 +0400389 if (operating_mode & OP_DEPEND) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000390 char *line;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700391
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400392 if (depend_missing_ok)
393 preproc->include_path(NULL); /* "assume generated" */
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700394
H. Peter Anvin130736c2016-02-17 20:27:41 -0800395 preproc->reset(inname, 0, depend_ptr);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000396 if (outname[0] == '\0')
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400397 ofmt->filename(inname, outname);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000398 ofile = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000399 while ((line = preproc->getline()))
400 nasm_free(line);
401 preproc->cleanup(0);
Cyrill Gorcunove9fc88c2014-06-23 02:22:02 +0400402 } else if (operating_mode & OP_PREPROCESS) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000403 char *line;
H. Peter Anvin274cda82016-05-10 02:56:29 -0700404 const char *file_name = NULL;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000405 int32_t prior_linnum = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000406 int lineinc = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000407
H. Peter Anvine2c80182005-01-15 22:15:51 +0000408 if (*outname) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -0700409 ofile = nasm_open_write(outname, NF_TEXT);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000410 if (!ofile)
H. Peter Anvin41087062016-03-03 14:27:34 -0800411 nasm_fatal(ERR_NOFILE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000412 "unable to open output file `%s'",
413 outname);
414 } else
415 ofile = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000416
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700417 location.known = false;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000418
Cyrill Gorcunovb574b072011-12-05 01:56:40 +0400419 /* pass = 1; */
H. Peter Anvin130736c2016-02-17 20:27:41 -0800420 preproc->reset(inname, 3, depend_ptr);
421 memcpy(warning_on, warning_on_global,
422 (ERR_WARN_MAX+1) * sizeof(bool));
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700423
H. Peter Anvine2c80182005-01-15 22:15:51 +0000424 while ((line = preproc->getline())) {
425 /*
426 * We generate %line directives if needed for later programs
427 */
Keith Kaniosb7a89542007-04-12 02:40:54 +0000428 int32_t linnum = prior_linnum += lineinc;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000429 int altline = src_get(&linnum, &file_name);
430 if (altline) {
431 if (altline == 1 && lineinc == 1)
432 nasm_fputs("", ofile);
433 else {
434 lineinc = (altline != -1 || lineinc != 1);
435 fprintf(ofile ? ofile : stdout,
Keith Kanios93f2e9a2007-04-14 00:10:59 +0000436 "%%line %"PRId32"+%d %s\n", linnum, lineinc,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000437 file_name);
438 }
439 prior_linnum = linnum;
440 }
441 nasm_fputs(line, ofile);
442 nasm_free(line);
443 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000444 preproc->cleanup(0);
445 if (ofile)
446 fclose(ofile);
447 if (ofile && terminate_after_phase)
448 remove(outname);
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400449 ofile = NULL;
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400450 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000451
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400452 if (operating_mode & OP_NORMAL) {
453 /*
454 * We must call ofmt->filename _anyway_, even if the user
455 * has specified their own output file, because some
456 * formats (eg OBJ and COFF) use ofmt->filename to find out
457 * the name of the input file and then put that inside the
458 * file.
459 */
460 ofmt->filename(inname, outname);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000461
H. Peter Anvin3e83cec2016-05-25 04:28:46 -0700462 ofile = nasm_open_write(outname, (ofmt->flags & OFMT_TEXT) ? NF_TEXT : NF_BINARY);
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400463 if (!ofile)
H. Peter Anvin41087062016-03-03 14:27:34 -0800464 nasm_fatal(ERR_NOFILE,
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400465 "unable to open output file `%s'", outname);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000466
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400467 /*
468 * We must call init_labels() before ofmt->init() since
469 * some object formats will want to define labels in their
470 * init routines. (eg OS/2 defines the FLAT group)
471 */
472 init_labels();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000473
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400474 ofmt->init();
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400475 dfmt->init();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000476
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400477 assemble_file(inname, depend_ptr);
Victor van den Elzenc82c3722008-06-04 15:24:20 +0200478
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400479 if (!terminate_after_phase) {
H. Peter Anvin477ae442016-03-07 22:53:06 -0800480 ofmt->cleanup();
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400481 cleanup_labels();
482 fflush(ofile);
H. Peter Anvin274cda82016-05-10 02:56:29 -0700483 if (ferror(ofile)) {
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400484 nasm_error(ERR_NONFATAL|ERR_NOFILE,
485 "write error on output file `%s'", outname);
H. Peter Anvin274cda82016-05-10 02:56:29 -0700486 terminate_after_phase = true;
487 }
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400488 }
489
490 if (ofile) {
491 fclose(ofile);
492 if (terminate_after_phase)
493 remove(outname);
494 ofile = NULL;
495 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000496 }
H. Peter Anvin734b1882002-04-30 21:01:08 +0000497
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700498 if (depend_list && !terminate_after_phase)
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400499 emit_dependencies(depend_list);
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700500
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000501 if (want_usage)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000502 usage();
H. Peter Anvin734b1882002-04-30 21:01:08 +0000503
H. Peter Anvine2c80182005-01-15 22:15:51 +0000504 raa_free(offsets);
505 saa_free(forwrefs);
506 eval_cleanup();
H. Peter Anvin74cc5e52007-08-30 22:35:34 +0000507 stdscan_cleanup();
H. Peter Anvin274cda82016-05-10 02:56:29 -0700508 src_free();
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000509
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700510 return terminate_after_phase;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000511}
512
H. Peter Anvineba20a72002-04-30 20:53:55 +0000513/*
514 * Get a parameter for a command line option.
515 * First arg must be in the form of e.g. -f...
516 */
H. Peter Anvin423e3812007-11-15 17:12:29 -0800517static char *get_param(char *p, char *q, bool *advance)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000518{
H. Peter Anvin423e3812007-11-15 17:12:29 -0800519 *advance = false;
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +0400520 if (p[2]) /* the parameter's in the option */
521 return nasm_skip_spaces(p + 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000522 if (q && q[0]) {
H. Peter Anvin423e3812007-11-15 17:12:29 -0800523 *advance = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000524 return q;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000525 }
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400526 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000527 "option `-%c' requires an argument", p[1]);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000528 return NULL;
529}
530
H. Peter Anvindc242712007-11-18 11:55:10 -0800531/*
532 * Copy a filename
533 */
534static void copy_filename(char *dst, const char *src)
535{
536 size_t len = strlen(src);
537
538 if (len >= (size_t)FILENAME_MAX) {
H. Peter Anvin41087062016-03-03 14:27:34 -0800539 nasm_fatal(ERR_NOFILE, "file name too long");
Cyrill Gorcunov45aa1182013-02-15 12:22:59 +0400540 return;
H. Peter Anvindc242712007-11-18 11:55:10 -0800541 }
542 strncpy(dst, src, FILENAME_MAX);
543}
544
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700545/*
546 * Convert a string to Make-safe form
547 */
548static char *quote_for_make(const char *str)
549{
550 const char *p;
551 char *os, *q;
552
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400553 size_t n = 1; /* Terminating zero */
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700554 size_t nbs = 0;
555
556 if (!str)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400557 return NULL;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700558
559 for (p = str; *p; p++) {
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400560 switch (*p) {
561 case ' ':
562 case '\t':
563 /* Convert N backslashes + ws -> 2N+1 backslashes + ws */
564 n += nbs + 2;
565 nbs = 0;
566 break;
567 case '$':
568 case '#':
569 nbs = 0;
570 n += 2;
571 break;
572 case '\\':
573 nbs++;
574 n++;
575 break;
576 default:
577 nbs = 0;
578 n++;
579 break;
580 }
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700581 }
582
583 /* Convert N backslashes at the end of filename to 2N backslashes */
584 if (nbs)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400585 n += nbs;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700586
587 os = q = nasm_malloc(n);
588
589 nbs = 0;
590 for (p = str; *p; p++) {
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400591 switch (*p) {
592 case ' ':
593 case '\t':
594 while (nbs--)
595 *q++ = '\\';
596 *q++ = '\\';
597 *q++ = *p;
598 break;
599 case '$':
600 *q++ = *p;
601 *q++ = *p;
602 nbs = 0;
603 break;
604 case '#':
605 *q++ = '\\';
606 *q++ = *p;
607 nbs = 0;
608 break;
609 case '\\':
610 *q++ = *p;
611 nbs++;
612 break;
613 default:
614 *q++ = *p;
615 nbs = 0;
616 break;
617 }
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700618 }
619 while (nbs--)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400620 *q++ = '\\';
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700621
622 *q = '\0';
623
624 return os;
625}
626
H. Peter Anvine2c80182005-01-15 22:15:51 +0000627struct textargs {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000628 const char *label;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000629 int value;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000630};
631
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100632enum text_options {
633 OPT_PREFIX,
H. Peter Anvin7214d182016-03-01 22:43:51 -0800634 OPT_POSTFIX
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100635};
H. Peter Anvin2530a102016-02-18 02:28:15 -0800636static const struct textargs textopts[] = {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000637 {"prefix", OPT_PREFIX},
638 {"postfix", OPT_POSTFIX},
639 {NULL, 0}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000640};
641
Cyrill Gorcunove7438432014-05-09 22:34:34 +0400642static void show_version(void)
643{
644 printf("NASM version %s compiled on %s%s\n",
645 nasm_version, nasm_date, nasm_compile_options);
646 exit(0);
647}
648
H. Peter Anvin423e3812007-11-15 17:12:29 -0800649static bool stopoptions = false;
650static bool process_arg(char *p, char *q)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000651{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000652 char *param;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800653 int i;
654 bool advance = false;
H. Peter Anvin972079f2008-09-30 17:01:23 -0700655 bool do_warn;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000656
657 if (!p || !p[0])
H. Peter Anvin423e3812007-11-15 17:12:29 -0800658 return false;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000659
H. Peter Anvine2c80182005-01-15 22:15:51 +0000660 if (p[0] == '-' && !stopoptions) {
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400661 if (strchr("oOfpPdDiIlFXuUZwW", p[1])) {
662 /* These parameters take values */
663 if (!(param = get_param(p, q, &advance)))
664 return advance;
665 }
H. Peter Anvin423e3812007-11-15 17:12:29 -0800666
H. Peter Anvine2c80182005-01-15 22:15:51 +0000667 switch (p[1]) {
668 case 's':
669 error_file = stdout;
670 break;
H. Peter Anvin70653092007-10-19 14:42:29 -0700671
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400672 case 'o': /* output file */
673 copy_filename(outname, param);
674 break;
H. Peter Anvinfd7dd112007-10-10 14:06:59 -0700675
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400676 case 'f': /* output format */
677 ofmt = ofmt_find(param, &ofmt_alias);
678 if (!ofmt) {
H. Peter Anvin41087062016-03-03 14:27:34 -0800679 nasm_fatal(ERR_NOFILE | ERR_USAGE,
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400680 "unrecognised output format `%s' - "
681 "use -hf for a list", param);
682 }
683 break;
H. Peter Anvin70653092007-10-19 14:42:29 -0700684
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400685 case 'O': /* Optimization level */
686 {
687 int opt;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700688
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400689 if (!*param) {
690 /* Naked -O == -Ox */
691 optimizing = MAX_OPTIMIZE;
692 } else {
693 while (*param) {
694 switch (*param) {
695 case '0': case '1': case '2': case '3': case '4':
696 case '5': case '6': case '7': case '8': case '9':
697 opt = strtoul(param, &param, 10);
H. Peter Anvind85d2502008-05-04 17:53:31 -0700698
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400699 /* -O0 -> optimizing == -1, 0.98 behaviour */
700 /* -O1 -> optimizing == 0, 0.98.09 behaviour */
701 if (opt < 2)
702 optimizing = opt - 1;
703 else
704 optimizing = opt;
705 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700706
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400707 case 'v':
708 case '+':
709 param++;
710 opt_verbose_info = true;
711 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700712
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400713 case 'x':
714 param++;
715 optimizing = MAX_OPTIMIZE;
716 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700717
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400718 default:
H. Peter Anvin41087062016-03-03 14:27:34 -0800719 nasm_fatal(0,
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400720 "unknown optimization option -O%c\n",
721 *param);
722 break;
723 }
724 }
725 if (optimizing > MAX_OPTIMIZE)
726 optimizing = MAX_OPTIMIZE;
727 }
728 break;
729 }
H. Peter Anvin423e3812007-11-15 17:12:29 -0800730
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400731 case 'p': /* pre-include */
732 case 'P':
733 preproc->pre_include(param);
734 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800735
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400736 case 'd': /* pre-define */
737 case 'D':
738 preproc->pre_define(param);
739 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800740
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400741 case 'u': /* un-define */
742 case 'U':
743 preproc->pre_undefine(param);
744 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800745
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400746 case 'i': /* include search path */
747 case 'I':
748 preproc->include_path(param);
749 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800750
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400751 case 'l': /* listing file */
752 copy_filename(listname, param);
753 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800754
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400755 case 'Z': /* error messages file */
756 copy_filename(errname, param);
757 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800758
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400759 case 'F': /* specify debug format */
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400760 using_debug_info = true;
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800761 debug_format = param;
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400762 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800763
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400764 case 'X': /* specify error reporting format */
765 if (nasm_stricmp("vc", param) == 0)
766 nasm_set_verror(nasm_verror_vc);
767 else if (nasm_stricmp("gnu", param) == 0)
768 nasm_set_verror(nasm_verror_gnu);
769 else
H. Peter Anvin41087062016-03-03 14:27:34 -0800770 nasm_fatal(ERR_NOFILE | ERR_USAGE,
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400771 "unrecognized error reporting format `%s'",
772 param);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000773 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800774
H. Peter Anvine2c80182005-01-15 22:15:51 +0000775 case 'g':
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700776 using_debug_info = true;
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800777 if (p[2])
778 debug_format = nasm_skip_spaces(p + 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000779 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800780
H. Peter Anvine2c80182005-01-15 22:15:51 +0000781 case 'h':
782 printf
783 ("usage: nasm [-@ response file] [-o outfile] [-f format] "
784 "[-l listfile]\n"
785 " [options...] [--] filename\n"
Cyrill Gorcunove7438432014-05-09 22:34:34 +0400786 " or nasm -v (or --v) for version info\n\n"
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800787 " -t assemble in SciTech TASM compatible mode\n");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000788 printf
H. Peter Anvin413fb902007-09-26 15:19:28 -0700789 (" -E (or -e) preprocess only (writes output to stdout by default)\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000790 " -a don't preprocess (assemble only)\n"
H. Peter Anvin37a321f2007-09-24 13:41:58 -0700791 " -M generate Makefile dependencies on stdout\n"
Cyrill Gorcunov984c4db2009-07-31 00:32:17 +0400792 " -MG d:o, missing files assumed generated\n"
793 " -MF <file> set Makefile dependency file\n"
794 " -MD <file> assemble and generate dependencies\n"
795 " -MT <file> dependency target name\n"
796 " -MQ <file> dependency target name (quoted)\n"
797 " -MP emit phony target\n\n"
H. Peter Anvin413fb902007-09-26 15:19:28 -0700798 " -Z<file> redirect error messages to file\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000799 " -s redirect error messages to stdout\n\n"
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800800 " -g generate debugging information\n\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000801 " -F format select a debugging format\n\n"
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800802 " -gformat same as -g -F format\n\n"
Cyrill Gorcunovdeb082d2013-04-20 20:37:17 +0400803 " -o outfile write output to an outfile\n\n"
804 " -f format select an output format\n\n"
805 " -l listfile write listing to a listfile\n\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000806 " -I<path> adds a pathname to the include file path\n");
807 printf
H. Peter Anvinab5bd052010-07-25 12:43:30 -0700808 (" -O<digit> optimize branch offsets\n"
Cyrill Gorcunov5bc6d8e2012-03-11 14:19:17 +0400809 " -O0: No optimization\n"
Cyrill Gorcunov73b87c62009-07-31 10:26:55 +0400810 " -O1: Minimal optimization\n"
Cyrill Gorcunov5bc6d8e2012-03-11 14:19:17 +0400811 " -Ox: Multipass optimization (default)\n\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000812 " -P<file> pre-includes a file\n"
813 " -D<macro>[=<value>] pre-defines a macro\n"
814 " -U<macro> undefines a macro\n"
815 " -X<format> specifies error reporting format (gnu or vc)\n"
H. Peter Anvinb030c922007-11-13 11:31:15 -0800816 " -w+foo enables warning foo (equiv. -Wfoo)\n"
Cyrill Gorcunov984c4db2009-07-31 00:32:17 +0400817 " -w-foo disable warning foo (equiv. -Wno-foo)\n\n"
Cyrill Gorcunovdeb082d2013-04-20 20:37:17 +0400818 " -h show invocation summary and exit\n\n"
Cyrill Gorcunov984c4db2009-07-31 00:32:17 +0400819 "--prefix,--postfix\n"
820 " this options prepend or append the given argument to all\n"
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100821 " extern and global variables\n"
H. Peter Anvinb030c922007-11-13 11:31:15 -0800822 "Warnings:\n");
823 for (i = 0; i <= ERR_WARN_MAX; i++)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000824 printf(" %-23s %s (default %s)\n",
H. Peter Anvin972079f2008-09-30 17:01:23 -0700825 warnings[i].name, warnings[i].help,
826 warnings[i].enabled ? "on" : "off");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000827 printf
828 ("\nresponse files should contain command line parameters"
829 ", one per line.\n");
830 if (p[2] == 'f') {
831 printf("\nvalid output formats for -f are"
832 " (`*' denotes default):\n");
833 ofmt_list(ofmt, stdout);
834 } else {
835 printf("\nFor a list of valid output formats, use -hf.\n");
836 printf("For a list of debug formats, use -f <form> -y.\n");
837 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400838 exit(0); /* never need usage message here */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000839 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800840
H. Peter Anvine2c80182005-01-15 22:15:51 +0000841 case 'y':
842 printf("\nvalid debug formats for '%s' output format are"
843 " ('*' denotes default):\n", ofmt->shortname);
844 dfmt_list(ofmt, stdout);
845 exit(0);
846 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800847
H. Peter Anvine2c80182005-01-15 22:15:51 +0000848 case 't':
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700849 tasm_compatible_mode = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000850 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800851
H. Peter Anvine2c80182005-01-15 22:15:51 +0000852 case 'v':
Cyrill Gorcunove7438432014-05-09 22:34:34 +0400853 show_version();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000854 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800855
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400856 case 'e': /* preprocess only */
857 case 'E':
Cyrill Gorcunov3ed32cb2014-06-22 20:53:12 +0400858 operating_mode = OP_PREPROCESS;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000859 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800860
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400861 case 'a': /* assemble only - don't preprocess */
Cyrill Gorcunovb5e8fec2012-05-07 11:34:27 +0400862 preproc = &preproc_nop;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000863 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800864
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400865 case 'W':
866 if (param[0] == 'n' && param[1] == 'o' && param[2] == '-') {
867 do_warn = false;
868 param += 3;
869 } else {
870 do_warn = true;
871 }
872 goto set_warning;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800873
H. Peter Anvine2c80182005-01-15 22:15:51 +0000874 case 'w':
H. Peter Anvin423e3812007-11-15 17:12:29 -0800875 if (param[0] != '+' && param[0] != '-') {
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400876 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000877 "invalid option to `-w'");
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400878 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000879 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400880 do_warn = (param[0] == '+');
881 param++;
Cyrill Gorcunov66206e72010-04-20 14:53:44 +0400882
883set_warning:
Cyrill Gorcunov3b8c2972011-12-05 01:39:04 +0400884 for (i = 0; i <= ERR_WARN_MAX; i++) {
885 if (!nasm_stricmp(param, warnings[i].name))
886 break;
887 }
888 if (i <= ERR_WARN_MAX) {
889 warning_on_global[i] = do_warn;
890 } else if (!nasm_stricmp(param, "all")) {
891 for (i = 1; i <= ERR_WARN_MAX; i++)
892 warning_on_global[i] = do_warn;
893 } else if (!nasm_stricmp(param, "none")) {
894 for (i = 1; i <= ERR_WARN_MAX; i++)
895 warning_on_global[i] = !do_warn;
896 } else {
H. Peter Anvinb4f734f2016-05-09 12:13:08 -0700897 /* Ignore invalid warning names; forward compatibility */
Cyrill Gorcunov3b8c2972011-12-05 01:39:04 +0400898 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000899 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800900
H. Peter Anvine2c80182005-01-15 22:15:51 +0000901 case 'M':
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400902 switch (p[2]) {
903 case 0:
Cyrill Gorcunov3ed32cb2014-06-22 20:53:12 +0400904 operating_mode = OP_DEPEND;
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400905 break;
906 case 'G':
Cyrill Gorcunov3ed32cb2014-06-22 20:53:12 +0400907 operating_mode = OP_DEPEND;
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400908 depend_missing_ok = true;
909 break;
910 case 'P':
911 depend_emit_phony = true;
912 break;
913 case 'D':
Cyrill Gorcunov0dd37af2014-11-29 21:33:44 +0300914 operating_mode = OP_NORMAL;
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400915 depend_file = q;
916 advance = true;
917 break;
918 case 'F':
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400919 depend_file = q;
920 advance = true;
921 break;
922 case 'T':
923 depend_target = q;
924 advance = true;
925 break;
926 case 'Q':
927 depend_target = quote_for_make(q);
928 advance = true;
929 break;
930 default:
931 nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
932 "unknown dependency option `-M%c'", p[2]);
933 break;
934 }
935 if (advance && (!q || !q[0])) {
936 nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
937 "option `-M%c' requires a parameter", p[2]);
938 break;
939 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000940 break;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000941
H. Peter Anvine2c80182005-01-15 22:15:51 +0000942 case '-':
943 {
944 int s;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000945
H. Peter Anvine2c80182005-01-15 22:15:51 +0000946 if (p[2] == 0) { /* -- => stop processing options */
947 stopoptions = 1;
948 break;
949 }
Cyrill Gorcunove7438432014-05-09 22:34:34 +0400950
951 if (!nasm_stricmp(p, "--v"))
952 show_version();
953
Andy Willis3f546032016-09-13 00:02:21 +0300954 if (!nasm_stricmp(p, "--version"))
955 show_version();
956
H. Peter Anvine2c80182005-01-15 22:15:51 +0000957 for (s = 0; textopts[s].label; s++) {
958 if (!nasm_stricmp(p + 2, textopts[s].label)) {
959 break;
960 }
961 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000962
H. Peter Anvine2c80182005-01-15 22:15:51 +0000963 switch (s) {
964
965 case OPT_PREFIX:
966 case OPT_POSTFIX:
967 {
968 if (!q) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400969 nasm_error(ERR_NONFATAL | ERR_NOFILE |
H. Peter Anvine2c80182005-01-15 22:15:51 +0000970 ERR_USAGE,
971 "option `--%s' requires an argument",
972 p + 2);
973 break;
974 } else {
975 advance = 1, param = q;
976 }
977
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100978 switch (s) {
979 case OPT_PREFIX:
980 strlcpy(lprefix, param, PREFIX_MAX);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000981 break;
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100982 case OPT_POSTFIX:
983 strlcpy(lpostfix, param, POSTFIX_MAX);
984 break;
985 default:
H. Peter Anvin41087062016-03-03 14:27:34 -0800986 nasm_panic(ERR_NOFILE,
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100987 "internal error");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000988 break;
989 }
990 break;
991 }
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800992
H. Peter Anvine2c80182005-01-15 22:15:51 +0000993 default:
994 {
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400995 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000996 "unrecognised option `--%s'", p + 2);
997 break;
998 }
999 }
1000 break;
1001 }
1002
1003 default:
1004 if (!ofmt->setinfo(GI_SWITCH, &p))
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001005 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001006 "unrecognised option `-%c'", p[1]);
1007 break;
1008 }
1009 } else {
1010 if (*inname) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001011 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001012 "more than one input file specified");
H. Peter Anvindc242712007-11-18 11:55:10 -08001013 } else {
1014 copy_filename(inname, p);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001015 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001016 }
1017
1018 return advance;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001019}
1020
H. Peter Anvineba20a72002-04-30 20:53:55 +00001021#define ARG_BUF_DELTA 128
1022
H. Peter Anvine2c80182005-01-15 22:15:51 +00001023static void process_respfile(FILE * rfile)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001024{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001025 char *buffer, *p, *q, *prevarg;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001026 int bufsize, prevargsize;
1027
1028 bufsize = prevargsize = ARG_BUF_DELTA;
1029 buffer = nasm_malloc(ARG_BUF_DELTA);
1030 prevarg = nasm_malloc(ARG_BUF_DELTA);
1031 prevarg[0] = '\0';
1032
H. Peter Anvine2c80182005-01-15 22:15:51 +00001033 while (1) { /* Loop to handle all lines in file */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001034 p = buffer;
1035 while (1) { /* Loop to handle long lines */
1036 q = fgets(p, bufsize - (p - buffer), rfile);
1037 if (!q)
1038 break;
1039 p += strlen(p);
1040 if (p > buffer && p[-1] == '\n')
1041 break;
1042 if (p - buffer > bufsize - 10) {
1043 int offset;
1044 offset = p - buffer;
1045 bufsize += ARG_BUF_DELTA;
1046 buffer = nasm_realloc(buffer, bufsize);
1047 p = buffer + offset;
1048 }
1049 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001050
H. Peter Anvine2c80182005-01-15 22:15:51 +00001051 if (!q && p == buffer) {
1052 if (prevarg[0])
1053 process_arg(prevarg, NULL);
1054 nasm_free(buffer);
1055 nasm_free(prevarg);
1056 return;
1057 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001058
H. Peter Anvine2c80182005-01-15 22:15:51 +00001059 /*
1060 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
1061 * them are present at the end of the line.
1062 */
1063 *(p = &buffer[strcspn(buffer, "\r\n\032")]) = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001064
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001065 while (p > buffer && nasm_isspace(p[-1]))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001066 *--p = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001067
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +04001068 p = nasm_skip_spaces(buffer);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001069
H. Peter Anvine2c80182005-01-15 22:15:51 +00001070 if (process_arg(prevarg, p))
1071 *p = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001072
Charles Crayne192d5b52007-10-18 19:02:42 -07001073 if ((int) strlen(p) > prevargsize - 10) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001074 prevargsize += ARG_BUF_DELTA;
1075 prevarg = nasm_realloc(prevarg, prevargsize);
1076 }
H. Peter Anvindc242712007-11-18 11:55:10 -08001077 strncpy(prevarg, p, prevargsize);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001078 }
1079}
1080
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001081/* Function to process args from a string of args, rather than the
1082 * argv array. Used by the environment variable and response file
1083 * processing.
1084 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001085static void process_args(char *args)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001086{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001087 char *p, *q, *arg, *prevarg;
1088 char separator = ' ';
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001089
1090 p = args;
1091 if (*p && *p != '-')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001092 separator = *p++;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001093 arg = NULL;
1094 while (*p) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001095 q = p;
1096 while (*p && *p != separator)
1097 p++;
1098 while (*p == separator)
1099 *p++ = '\0';
1100 prevarg = arg;
1101 arg = q;
1102 if (process_arg(prevarg, arg))
1103 arg = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001104 }
1105 if (arg)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001106 process_arg(arg, NULL);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001107}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001108
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001109static void process_response_file(const char *file)
1110{
1111 char str[2048];
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001112 FILE *f = nasm_open_read(file, NF_TEXT);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001113 if (!f) {
Cyrill Gorcunovf1964512013-02-15 12:14:06 +04001114 perror(file);
1115 exit(-1);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001116 }
1117 while (fgets(str, sizeof str, f)) {
Cyrill Gorcunovf1964512013-02-15 12:14:06 +04001118 process_args(str);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001119 }
1120 fclose(f);
1121}
1122
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001123static void parse_cmdline(int argc, char **argv)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001124{
1125 FILE *rfile;
Cyrill Gorcunovf4941892011-07-17 13:55:25 +04001126 char *envreal, *envcopy = NULL, *p;
H. Peter Anvin972079f2008-09-30 17:01:23 -07001127 int i;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001128
Charles Craynefcce07f2007-09-30 22:15:36 -07001129 *inname = *outname = *listname = *errname = '\0';
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001130
H. Peter Anvin972079f2008-09-30 17:01:23 -07001131 for (i = 0; i <= ERR_WARN_MAX; i++)
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001132 warning_on_global[i] = warnings[i].enabled;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001133
1134 /*
H. Peter Anvinff7ccc02002-05-06 19:41:57 +00001135 * First, process the NASMENV environment variable.
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001136 */
H. Peter Anvinff7ccc02002-05-06 19:41:57 +00001137 envreal = getenv("NASMENV");
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001138 if (envreal) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001139 envcopy = nasm_strdup(envreal);
1140 process_args(envcopy);
1141 nasm_free(envcopy);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001142 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001143
1144 /*
1145 * Now process the actual command line.
1146 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001147 while (--argc) {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001148 bool advance;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001149 argv++;
1150 if (argv[0][0] == '@') {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001151 /*
1152 * We have a response file, so process this as a set of
H. Peter Anvine2c80182005-01-15 22:15:51 +00001153 * arguments like the environment variable. This allows us
1154 * to have multiple arguments on a single line, which is
1155 * different to the -@resp file processing below for regular
1156 * NASM.
1157 */
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001158 process_response_file(argv[0]+1);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001159 argc--;
1160 argv++;
1161 }
1162 if (!stopoptions && argv[0][0] == '-' && argv[0][1] == '@') {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001163 p = get_param(argv[0], argc > 1 ? argv[1] : NULL, &advance);
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001164 if (p) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001165 rfile = nasm_open_read(p, NF_TEXT);
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001166 if (rfile) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001167 process_respfile(rfile);
1168 fclose(rfile);
1169 } else
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001170 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001171 "unable to open response file `%s'", p);
1172 }
1173 } else
H. Peter Anvin423e3812007-11-15 17:12:29 -08001174 advance = process_arg(argv[0], argc > 1 ? argv[1] : NULL);
1175 argv += advance, argc -= advance;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001176 }
1177
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001178 /*
1179 * Look for basic command line typos. This definitely doesn't
1180 * catch all errors, but it might help cases of fumbled fingers.
1181 */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001182 if (!*inname)
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001183 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001184 "no input file specified");
1185 else if (!strcmp(inname, errname) ||
1186 !strcmp(inname, outname) ||
1187 !strcmp(inname, listname) ||
1188 (depend_file && !strcmp(inname, depend_file)))
H. Peter Anvin41087062016-03-03 14:27:34 -08001189 nasm_fatal(ERR_NOFILE | ERR_USAGE,
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001190 "file `%s' is both input and output file",
1191 inname);
H. Peter Anvin59ddd262007-10-01 11:26:31 -07001192
H. Peter Anvin70653092007-10-19 14:42:29 -07001193 if (*errname) {
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001194 error_file = nasm_open_write(errname, NF_TEXT);
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001195 if (!error_file) {
1196 error_file = stderr; /* Revert to default! */
H. Peter Anvin41087062016-03-03 14:27:34 -08001197 nasm_fatal(ERR_NOFILE | ERR_USAGE,
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001198 "cannot open file `%s' for error messages",
1199 errname);
1200 }
Charles Craynefcce07f2007-09-30 22:15:36 -07001201 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001202}
1203
H. Peter Anvin70055962007-10-11 00:05:31 -07001204static enum directives getkw(char **directive, char **value);
H. Peter Anvin62b24d72007-08-29 16:38:05 +00001205
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001206static void assemble_file(char *fname, StrList **depend_ptr)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001207{
H. Peter Anvin8bec2c72009-08-08 13:49:00 -07001208 char *directive, *value, *p, *q, *special, *line;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001209 insn output_ins;
H. Peter Anvin70055962007-10-11 00:05:31 -07001210 int i, validid;
1211 bool rn_error;
Charles Crayne5fbbc8c2007-11-07 19:03:46 -08001212 int32_t seg;
1213 int64_t offs;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001214 struct tokenval tokval;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001215 expr *e;
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001216 int pass_max;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001217
Cyrill Gorcunov08359152013-11-09 22:16:11 +04001218 if (cmd_sb == 32 && iflag_ffs(&cmd_cpu) < IF_386)
H. Peter Anvin130736c2016-02-17 20:27:41 -08001219 nasm_fatal(0, "command line: 32-bit segment size requires a higher cpu");
H. Peter Anvineba20a72002-04-30 20:53:55 +00001220
Charles Craynec1905c22008-09-11 18:54:06 -07001221 pass_max = prev_offset_changed = (INT_MAX >> 1) + 2; /* Almost unlimited */
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001222 for (passn = 1; pass0 <= 2; passn++) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001223 int pass1, pass2;
1224 ldfunc def_label;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001225
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001226 pass1 = pass0 == 2 ? 2 : 1; /* 1, 1, 1, ..., 1, 2 */
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001227 pass2 = passn > 1 ? 2 : 1; /* 1, 2, 2, ..., 2, 2 */
1228 /* pass0 0, 0, 0, ..., 1, 2 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00001229
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001230 def_label = passn > 1 ? redefine_label : define_label;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001231
Keith Kaniosb7a89542007-04-12 02:40:54 +00001232 globalbits = sb = cmd_sb; /* set 'bits' to command line default */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001233 cpu = cmd_cpu;
1234 if (pass0 == 2) {
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08001235 lfmt->init(listname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001236 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001237 in_abs_seg = false;
Charles Craynec1905c22008-09-11 18:54:06 -07001238 global_offset_changed = 0; /* set by redefine_label */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001239 location.segment = ofmt->section(NULL, pass2, &sb);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001240 globalbits = sb;
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001241 if (passn > 1) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001242 saa_rewind(forwrefs);
1243 forwref = saa_rstruct(forwrefs);
1244 raa_free(offsets);
1245 offsets = raa_init();
1246 }
H. Peter Anvin130736c2016-02-17 20:27:41 -08001247 preproc->reset(fname, pass1, pass1 == 2 ? depend_ptr : NULL);
H. Peter Anvin972079f2008-09-30 17:01:23 -07001248 memcpy(warning_on, warning_on_global, (ERR_WARN_MAX+1) * sizeof(bool));
Victor van den Elzen819703a2008-07-16 15:20:56 +02001249
H. Peter Anvine2c80182005-01-15 22:15:51 +00001250 globallineno = 0;
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001251 if (passn == 1)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001252 location.known = true;
Cyrill Gorcunov190232f2013-02-15 12:35:04 +04001253 location.offset = offs = get_curr_offs();
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001254
H. Peter Anvine2c80182005-01-15 22:15:51 +00001255 while ((line = preproc->getline())) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001256 enum directives d;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001257 globallineno++;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001258
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001259 /*
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001260 * Here we parse our directives; this is not handled by the
1261 * 'real' parser. This really should be a separate function.
1262 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001263 directive = line;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001264 d = getkw(&directive, &value);
H. Peter Anvin70055962007-10-11 00:05:31 -07001265 if (d) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001266 int err = 0;
H. Peter Anvin62b24d72007-08-29 16:38:05 +00001267
H. Peter Anvin70055962007-10-11 00:05:31 -07001268 switch (d) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001269 case D_SEGMENT: /* [SEGMENT n] */
1270 case D_SECTION:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001271 seg = ofmt->section(value, pass2, &sb);
1272 if (seg == NO_SEG) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001273 nasm_error(pass1 == 1 ? ERR_NONFATAL : ERR_PANIC,
Keith Kaniosb7a89542007-04-12 02:40:54 +00001274 "segment name `%s' not recognized",
H. Peter Anvine2c80182005-01-15 22:15:51 +00001275 value);
1276 } else {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001277 in_abs_seg = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001278 location.segment = seg;
1279 }
1280 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001281 case D_SECTALIGN: /* [SECTALIGN n] */
Cyrill Gorcunov9fde3352011-05-23 23:50:03 +04001282 if (*value) {
1283 stdscan_reset();
1284 stdscan_set(value);
1285 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin130736c2016-02-17 20:27:41 -08001286 e = evaluate(stdscan, NULL, &tokval, NULL, pass2, NULL);
Cyrill Gorcunov9fde3352011-05-23 23:50:03 +04001287 if (e) {
1288 unsigned int align = (unsigned int)e->value;
1289 if ((uint64_t)e->value > 0x7fffffff) {
1290 /*
1291 * FIXME: Please make some sane message here
1292 * ofmt should have some 'check' method which
1293 * would report segment alignment bounds.
1294 */
H. Peter Anvin41087062016-03-03 14:27:34 -08001295 nasm_fatal(0,
Cyrill Gorcunov9fde3352011-05-23 23:50:03 +04001296 "incorrect segment alignment `%s'", value);
1297 } else if (!is_power2(align)) {
Cyrill Gorcunovd5f2aef2010-04-20 15:33:45 +04001298 nasm_error(ERR_NONFATAL,
1299 "segment alignment `%s' is not power of two",
1300 value);
1301 }
H. Peter Anvin2c4a4d52016-02-16 17:37:18 -08001302
Cyrill Gorcunov2a587ab2010-04-21 00:51:22 +04001303 /* callee should be able to handle all details */
H. Peter Anvin2c4a4d52016-02-16 17:37:18 -08001304 if (location.segment != NO_SEG)
1305 ofmt->sectalign(location.segment, align);
Cyrill Gorcunovd5f2aef2010-04-20 15:33:45 +04001306 }
1307 }
1308 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001309 case D_EXTERN: /* [EXTERN label:special] */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001310 if (*value == '$')
1311 value++; /* skip initial $ if present */
1312 if (pass0 == 2) {
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001313 q = value;
1314 while (*q && *q != ':')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001315 q++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001316 if (*q == ':') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001317 *q++ = '\0';
1318 ofmt->symdef(value, 0L, 0L, 3, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001319 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001320 } else if (passn == 1) {
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001321 q = value;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001322 validid = true;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001323 if (!isidstart(*q))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001324 validid = false;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001325 while (*q && *q != ':') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001326 if (!isidchar(*q))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001327 validid = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001328 q++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001329 }
1330 if (!validid) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001331 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001332 "identifier expected after EXTERN");
1333 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001334 }
1335 if (*q == ':') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001336 *q++ = '\0';
1337 special = q;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001338 } else
H. Peter Anvine2c80182005-01-15 22:15:51 +00001339 special = NULL;
1340 if (!is_extern(value)) { /* allow re-EXTERN to be ignored */
1341 int temp = pass0;
1342 pass0 = 1; /* fake pass 1 in labels.c */
H. Peter Anvin605f5152009-07-18 18:31:41 -07001343 declare_as_global(value, special);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001344 define_label(value, seg_alloc(), 0L, NULL,
H. Peter Anvin605f5152009-07-18 18:31:41 -07001345 false, true);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001346 pass0 = temp;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001347 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001348 } /* else pass0 == 1 */
1349 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001350 case D_BITS: /* [BITS bits] */
Keith Kaniosb7a89542007-04-12 02:40:54 +00001351 globalbits = sb = get_bits(value);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001352 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001353 case D_GLOBAL: /* [GLOBAL symbol:special] */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001354 if (*value == '$')
1355 value++; /* skip initial $ if present */
1356 if (pass0 == 2) { /* pass 2 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001357 q = value;
1358 while (*q && *q != ':')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001359 q++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001360 if (*q == ':') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001361 *q++ = '\0';
1362 ofmt->symdef(value, 0L, 0L, 3, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001363 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001364 } else if (pass2 == 1) { /* pass == 1 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001365 q = value;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001366 validid = true;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001367 if (!isidstart(*q))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001368 validid = false;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001369 while (*q && *q != ':') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001370 if (!isidchar(*q))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001371 validid = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001372 q++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001373 }
1374 if (!validid) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001375 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001376 "identifier expected after GLOBAL");
1377 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001378 }
1379 if (*q == ':') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001380 *q++ = '\0';
1381 special = q;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001382 } else
H. Peter Anvine2c80182005-01-15 22:15:51 +00001383 special = NULL;
H. Peter Anvin605f5152009-07-18 18:31:41 -07001384 declare_as_global(value, special);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001385 } /* pass == 1 */
1386 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001387 case D_COMMON: /* [COMMON symbol size:special] */
1388 {
1389 int64_t size;
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001390
H. Peter Anvine2c80182005-01-15 22:15:51 +00001391 if (*value == '$')
1392 value++; /* skip initial $ if present */
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001393 p = value;
1394 validid = true;
1395 if (!isidstart(*p))
1396 validid = false;
1397 while (*p && !nasm_isspace(*p)) {
1398 if (!isidchar(*p))
1399 validid = false;
1400 p++;
1401 }
1402 if (!validid) {
1403 nasm_error(ERR_NONFATAL,
1404 "identifier expected after COMMON");
1405 break;
1406 }
1407 if (*p) {
H. Peter Anvinff800412009-10-13 12:03:37 -07001408 p = nasm_zap_spaces_fwd(p);
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001409 q = p;
1410 while (*q && *q != ':')
1411 q++;
1412 if (*q == ':') {
1413 *q++ = '\0';
1414 special = q;
1415 } else {
1416 special = NULL;
1417 }
1418 size = readnum(p, &rn_error);
1419 if (rn_error) {
1420 nasm_error(ERR_NONFATAL,
1421 "invalid size specified"
1422 " in COMMON declaration");
1423 break;
1424 }
1425 } else {
1426 nasm_error(ERR_NONFATAL,
1427 "no size specified in"
1428 " COMMON declaration");
1429 break;
1430 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001431
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001432 if (pass0 < 2) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001433 define_common(value, seg_alloc(), size, special);
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001434 } else if (pass0 == 2) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001435 if (special)
1436 ofmt->symdef(value, 0L, 0L, 3, special);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001437 }
1438 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001439 }
1440 case D_ABSOLUTE: /* [ABSOLUTE address] */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001441 stdscan_reset();
Cyrill Gorcunov917117f2009-10-29 23:09:18 +03001442 stdscan_set(value);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001443 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin130736c2016-02-17 20:27:41 -08001444 e = evaluate(stdscan, NULL, &tokval, NULL, pass2, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001445 if (e) {
1446 if (!is_reloc(e))
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001447 nasm_error(pass0 ==
H. Peter Anvine2c80182005-01-15 22:15:51 +00001448 1 ? ERR_NONFATAL : ERR_PANIC,
1449 "cannot use non-relocatable expression as "
1450 "ABSOLUTE address");
1451 else {
1452 abs_seg = reloc_seg(e);
1453 abs_offset = reloc_value(e);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001454 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001455 } else if (passn == 1)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001456 abs_offset = 0x100; /* don't go near zero in case of / */
1457 else
H. Peter Anvin41087062016-03-03 14:27:34 -08001458 nasm_panic(0, "invalid ABSOLUTE address "
H. Peter Anvine2c80182005-01-15 22:15:51 +00001459 "in pass two");
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001460 in_abs_seg = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001461 location.segment = NO_SEG;
1462 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001463 case D_DEBUG: /* [DEBUG] */
1464 {
1465 char debugid[128];
1466 bool badid, overlong;
H. Peter Anvin8bec2c72009-08-08 13:49:00 -07001467
H. Peter Anvine2c80182005-01-15 22:15:51 +00001468 p = value;
1469 q = debugid;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001470 badid = overlong = false;
H. Peter Anvin8bec2c72009-08-08 13:49:00 -07001471 if (!isidstart(*p)) {
1472 badid = true;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001473 } else {
1474 while (*p && !nasm_isspace(*p)) {
1475 if (q >= debugid + sizeof debugid - 1) {
1476 overlong = true;
1477 break;
1478 }
1479 if (!isidchar(*p))
1480 badid = true;
1481 *q++ = *p++;
1482 }
1483 *q = 0;
1484 }
H. Peter Anvin8bec2c72009-08-08 13:49:00 -07001485 if (badid) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001486 nasm_error(passn == 1 ? ERR_NONFATAL : ERR_PANIC,
1487 "identifier expected after DEBUG");
1488 break;
1489 }
1490 if (overlong) {
1491 nasm_error(passn == 1 ? ERR_NONFATAL : ERR_PANIC,
1492 "DEBUG identifier too long");
1493 break;
1494 }
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +04001495 p = nasm_skip_spaces(p);
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001496 if (pass0 == 2)
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001497 dfmt->debug_directive(debugid, p);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001498 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001499 }
1500 case D_WARNING: /* [WARNING {+|-|*}warn-name] */
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +04001501 value = nasm_skip_spaces(value);
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001502 switch(*value) {
1503 case '-': validid = 0; value++; break;
1504 case '+': validid = 1; value++; break;
1505 case '*': validid = 2; value++; break;
1506 default: validid = 1; break;
1507 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001508
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001509 for (i = 1; i <= ERR_WARN_MAX; i++)
1510 if (!nasm_stricmp(value, warnings[i].name))
1511 break;
1512 if (i <= ERR_WARN_MAX) {
1513 switch(validid) {
1514 case 0:
1515 warning_on[i] = false;
1516 break;
1517 case 1:
1518 warning_on[i] = true;
1519 break;
1520 case 2:
1521 warning_on[i] = warning_on_global[i];
1522 break;
1523 }
H. Peter Anvinb4f734f2016-05-09 12:13:08 -07001524 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001525 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001526 case D_CPU: /* [CPU] */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001527 cpu = get_cpu(value);
1528 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001529 case D_LIST: /* [LIST {+|-}] */
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +04001530 value = nasm_skip_spaces(value);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001531 if (*value == '+') {
1532 user_nolist = 0;
1533 } else {
1534 if (*value == '-') {
1535 user_nolist = 1;
1536 } else {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001537 err = 1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001538 }
1539 }
1540 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001541 case D_DEFAULT: /* [DEFAULT] */
1542 stdscan_reset();
Cyrill Gorcunov917117f2009-10-29 23:09:18 +03001543 stdscan_set(value);
H. Peter Anvin62b24d72007-08-29 16:38:05 +00001544 tokval.t_type = TOKEN_INVALID;
Jin Kyu Songb287ff02013-12-04 20:05:55 -08001545 if (stdscan(NULL, &tokval) != TOKEN_INVALID) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001546 switch ((int)tokval.t_integer) {
1547 case S_REL:
1548 globalrel = 1;
1549 break;
1550 case S_ABS:
1551 globalrel = 0;
1552 break;
Jin Kyu Songb287ff02013-12-04 20:05:55 -08001553 case P_BND:
1554 globalbnd = 1;
1555 break;
1556 case P_NOBND:
1557 globalbnd = 0;
1558 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001559 default:
1560 err = 1;
1561 break;
1562 }
1563 } else {
1564 err = 1;
1565 }
1566 break;
1567 case D_FLOAT:
1568 if (float_option(value)) {
1569 nasm_error(pass1 == 1 ? ERR_NONFATAL : ERR_PANIC,
1570 "unknown 'float' directive: %s",
1571 value);
1572 }
1573 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001574 default:
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001575 if (ofmt->directive(d, value, pass2))
1576 break;
1577 /* else fall through */
1578 case D_unknown:
1579 nasm_error(pass1 == 1 ? ERR_NONFATAL : ERR_PANIC,
1580 "unrecognised directive [%s]",
1581 directive);
1582 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001583 }
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001584 if (err) {
1585 nasm_error(ERR_NONFATAL,
1586 "invalid parameter to [%s] directive",
1587 directive);
1588 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001589 } else { /* it isn't a directive */
H. Peter Anvin00444ae2009-07-18 18:49:55 -07001590 parse_line(pass1, line, &output_ins, def_label);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001591
Charles Crayne2581c862008-09-10 19:21:52 -07001592 if (optimizing > 0) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001593 if (forwref != NULL && globallineno == forwref->lineno) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001594 output_ins.forw_ref = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001595 do {
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001596 output_ins.oprs[forwref->operand].opflags |= OPFLAG_FORWARD;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001597 forwref = saa_rstruct(forwrefs);
1598 } while (forwref != NULL
1599 && forwref->lineno == globallineno);
1600 } else
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001601 output_ins.forw_ref = false;
H. Peter Anvin323fcff2009-07-12 12:04:56 -07001602
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001603 if (output_ins.forw_ref) {
1604 if (passn == 1) {
1605 for (i = 0; i < output_ins.operands; i++) {
1606 if (output_ins.oprs[i].opflags & OPFLAG_FORWARD) {
1607 struct forwrefinfo *fwinf = (struct forwrefinfo *)saa_wstruct(forwrefs);
1608 fwinf->lineno = globallineno;
1609 fwinf->operand = i;
1610 }
1611 }
1612 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001613 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001614 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001615
H. Peter Anvine2c80182005-01-15 22:15:51 +00001616 /* forw_ref */
1617 if (output_ins.opcode == I_EQU) {
1618 if (pass1 == 1) {
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001619 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00001620 * Special `..' EQUs get processed in pass two,
1621 * except `..@' macro-processor EQUs which are done
1622 * in the normal place.
1623 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001624 if (!output_ins.label)
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001625 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001626 "EQU not preceded by label");
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001627
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001628 else if (output_ins.label[0] != '.' ||
1629 output_ins.label[1] != '.' ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00001630 output_ins.label[2] == '@') {
1631 if (output_ins.operands == 1 &&
1632 (output_ins.oprs[0].type & IMMEDIATE) &&
1633 output_ins.oprs[0].wrt == NO_SEG) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001634 bool isext = !!(output_ins.oprs[0].opflags & OPFLAG_EXTERN);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001635 def_label(output_ins.label,
1636 output_ins.oprs[0].segment,
1637 output_ins.oprs[0].offset, NULL,
H. Peter Anvin605f5152009-07-18 18:31:41 -07001638 false, isext);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001639 } else if (output_ins.operands == 2
H. Peter Anvin72191982009-02-26 14:34:48 -08001640 && (output_ins.oprs[0].type & IMMEDIATE)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001641 && (output_ins.oprs[0].type & COLON)
H. Peter Anvin72191982009-02-26 14:34:48 -08001642 && output_ins.oprs[0].segment == NO_SEG
H. Peter Anvine2c80182005-01-15 22:15:51 +00001643 && output_ins.oprs[0].wrt == NO_SEG
H. Peter Anvin72191982009-02-26 14:34:48 -08001644 && (output_ins.oprs[1].type & IMMEDIATE)
1645 && output_ins.oprs[1].segment == NO_SEG
1646 && output_ins.oprs[1].wrt == NO_SEG) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001647 def_label(output_ins.label,
H. Peter Anvin72191982009-02-26 14:34:48 -08001648 output_ins.oprs[0].offset | SEG_ABS,
1649 output_ins.oprs[1].offset,
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001650 NULL, false, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001651 } else
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001652 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001653 "bad syntax for EQU");
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001654 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001655 } else {
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001656 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00001657 * Special `..' EQUs get processed here, except
1658 * `..@' macro processor EQUs which are done above.
1659 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001660 if (output_ins.label[0] == '.' &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00001661 output_ins.label[1] == '.' &&
1662 output_ins.label[2] != '@') {
1663 if (output_ins.operands == 1 &&
1664 (output_ins.oprs[0].type & IMMEDIATE)) {
1665 define_label(output_ins.label,
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001666 output_ins.oprs[0].segment,
1667 output_ins.oprs[0].offset,
H. Peter Anvin605f5152009-07-18 18:31:41 -07001668 NULL, false, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001669 } else if (output_ins.operands == 2
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001670 && (output_ins.oprs[0].type & IMMEDIATE)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001671 && (output_ins.oprs[0].type & COLON)
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001672 && output_ins.oprs[0].segment == NO_SEG
1673 && (output_ins.oprs[1].type & IMMEDIATE)
1674 && output_ins.oprs[1].segment == NO_SEG) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001675 define_label(output_ins.label,
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001676 output_ins.oprs[0].offset | SEG_ABS,
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001677 output_ins.oprs[1].offset,
H. Peter Anvin605f5152009-07-18 18:31:41 -07001678 NULL, false, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001679 } else
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001680 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001681 "bad syntax for EQU");
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001682 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001683 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001684 } else { /* instruction isn't an EQU */
H. Peter Anvineba20a72002-04-30 20:53:55 +00001685
H. Peter Anvine2c80182005-01-15 22:15:51 +00001686 if (pass1 == 1) {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001687
Charles Crayne5fbbc8c2007-11-07 19:03:46 -08001688 int64_t l = insn_size(location.segment, offs, sb, cpu,
H. Peter Anvin130736c2016-02-17 20:27:41 -08001689 &output_ins);
H. Peter Anvina77692b2016-09-20 14:04:33 -07001690 l *= output_ins.times;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001691
H. Peter Anvine2c80182005-01-15 22:15:51 +00001692 /* if (using_debug_info) && output_ins.opcode != -1) */
1693 if (using_debug_info)
1694 { /* fbk 03/25/01 */
1695 /* this is done here so we can do debug type info */
Keith Kaniosb7a89542007-04-12 02:40:54 +00001696 int32_t typeinfo =
H. Peter Anvine2c80182005-01-15 22:15:51 +00001697 TYS_ELEMENTS(output_ins.operands);
1698 switch (output_ins.opcode) {
1699 case I_RESB:
1700 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001701 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_BYTE;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001702 break;
1703 case I_RESW:
1704 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001705 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_WORD;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001706 break;
1707 case I_RESD:
1708 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001709 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_DWORD;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001710 break;
1711 case I_RESQ:
1712 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001713 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_QWORD;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001714 break;
1715 case I_REST:
1716 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001717 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_TBYTE;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001718 break;
H. Peter Anvindfb91802008-05-20 11:43:53 -07001719 case I_RESO:
1720 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001721 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_OWORD;
H. Peter Anvindfb91802008-05-20 11:43:53 -07001722 break;
1723 case I_RESY:
1724 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001725 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_YWORD;
H. Peter Anvindfb91802008-05-20 11:43:53 -07001726 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001727 case I_DB:
1728 typeinfo |= TY_BYTE;
1729 break;
1730 case I_DW:
1731 typeinfo |= TY_WORD;
1732 break;
1733 case I_DD:
1734 if (output_ins.eops_float)
1735 typeinfo |= TY_FLOAT;
1736 else
1737 typeinfo |= TY_DWORD;
1738 break;
1739 case I_DQ:
1740 typeinfo |= TY_QWORD;
1741 break;
1742 case I_DT:
1743 typeinfo |= TY_TBYTE;
1744 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001745 case I_DO:
1746 typeinfo |= TY_OWORD;
1747 break;
1748 case I_DY:
1749 typeinfo |= TY_YWORD;
1750 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001751 default:
1752 typeinfo = TY_LABEL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001753
H. Peter Anvine2c80182005-01-15 22:15:51 +00001754 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001755
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001756 dfmt->debug_typevalue(typeinfo);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001757 }
1758 if (l != -1) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001759 offs += l;
Cyrill Gorcunov190232f2013-02-15 12:35:04 +04001760 set_curr_offs(offs);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001761 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00001762 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00001763 * else l == -1 => invalid instruction, which will be
1764 * flagged as an error on pass 2
1765 */
H. Peter Anvin76690a12002-04-30 20:52:49 +00001766
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001767 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001768 offs += assemble(location.segment, offs, sb, cpu,
H. Peter Anvin130736c2016-02-17 20:27:41 -08001769 &output_ins);
Cyrill Gorcunov190232f2013-02-15 12:35:04 +04001770 set_curr_offs(offs);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001771
H. Peter Anvine2c80182005-01-15 22:15:51 +00001772 }
1773 } /* not an EQU */
1774 cleanup_insn(&output_ins);
1775 }
1776 nasm_free(line);
Cyrill Gorcunov190232f2013-02-15 12:35:04 +04001777 location.offset = offs = get_curr_offs();
H. Peter Anvine2c80182005-01-15 22:15:51 +00001778 } /* end while (line = preproc->getline... */
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001779
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001780 if (pass0 == 2 && global_offset_changed && !terminate_after_phase)
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001781 nasm_error(ERR_NONFATAL,
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001782 "phase error detected at end of assembly.");
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001783
H. Peter Anvine2c80182005-01-15 22:15:51 +00001784 if (pass1 == 1)
1785 preproc->cleanup(1);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001786
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001787 if ((passn > 1 && !global_offset_changed) || pass0 == 2) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001788 pass0++;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001789 } else if (global_offset_changed &&
1790 global_offset_changed < prev_offset_changed) {
Charles Craynec1905c22008-09-11 18:54:06 -07001791 prev_offset_changed = global_offset_changed;
1792 stall_count = 0;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001793 } else {
1794 stall_count++;
1795 }
Victor van den Elzen42528232008-09-11 15:07:05 +02001796
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001797 if (terminate_after_phase)
1798 break;
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001799
1800 if ((stall_count > 997) || (passn >= pass_max)) {
Victor van den Elzen42528232008-09-11 15:07:05 +02001801 /* We get here if the labels don't converge
1802 * Example: FOO equ FOO + 1
1803 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001804 nasm_error(ERR_NONFATAL,
Victor van den Elzen42528232008-09-11 15:07:05 +02001805 "Can't find valid values for all labels "
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001806 "after %d passes, giving up.", passn);
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001807 nasm_error(ERR_NONFATAL,
1808 "Possible causes: recursive EQUs, macro abuse.");
1809 break;
1810 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001811 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001812
H. Peter Anvine2c80182005-01-15 22:15:51 +00001813 preproc->cleanup(0);
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08001814 lfmt->cleanup();
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001815 if (!terminate_after_phase && opt_verbose_info) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001816 /* -On and -Ov switches */
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001817 fprintf(stdout, "info: assembly required 1+%d+1 passes\n", passn-3);
1818 }
1819}
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001820
H. Peter Anvin70055962007-10-11 00:05:31 -07001821static enum directives getkw(char **directive, char **value)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001822{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001823 char *p, *q, *buf;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001824
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001825 buf = nasm_skip_spaces(*directive);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001826
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001827 /* it should be enclosed in [ ] */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001828 if (*buf != '[')
H. Peter Anvin888964a2010-04-06 17:00:12 -07001829 return D_none;
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001830 q = strchr(buf, ']');
1831 if (!q)
H. Peter Anvin888964a2010-04-06 17:00:12 -07001832 return D_none;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001833
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001834 /* stip off the comments */
1835 p = strchr(buf, ';');
1836 if (p) {
1837 if (p < q) /* ouch! somwhere inside */
H. Peter Anvin888964a2010-04-06 17:00:12 -07001838 return D_none;
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001839 *p = '\0';
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001840 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001841
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001842 /* no brace, no trailing spaces */
1843 *q = '\0';
1844 nasm_zap_spaces_rev(--q);
1845
1846 /* directive */
1847 p = nasm_skip_spaces(++buf);
1848 q = nasm_skip_word(p);
1849 if (!q)
H. Peter Anvin888964a2010-04-06 17:00:12 -07001850 return D_none; /* sigh... no value there */
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001851 *q = '\0';
1852 *directive = p;
1853
1854 /* and value finally */
1855 p = nasm_skip_spaces(++q);
1856 *value = p;
H. Peter Anvin62b24d72007-08-29 16:38:05 +00001857
H. Peter Anvin323fcff2009-07-12 12:04:56 -07001858 return find_directive(*directive);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001859}
1860
Ed Berosetfa771012002-06-09 20:56:40 +00001861/**
1862 * gnu style error reporting
1863 * This function prints an error message to error_file in the
1864 * style used by GNU. An example would be:
1865 * file.asm:50: error: blah blah blah
H. Peter Anvin70653092007-10-19 14:42:29 -07001866 * where file.asm is the name of the file, 50 is the line number on
Ed Berosetfa771012002-06-09 20:56:40 +00001867 * which the error occurs (or is detected) and "error:" is one of
1868 * the possible optional diagnostics -- it can be "error" or "warning"
H. Peter Anvin70653092007-10-19 14:42:29 -07001869 * or something else. Finally the line terminates with the actual
Ed Berosetfa771012002-06-09 20:56:40 +00001870 * error message.
H. Peter Anvin70653092007-10-19 14:42:29 -07001871 *
1872 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001873 * @param fmt the printf style format string
1874 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001875static void nasm_verror_gnu(int severity, const char *fmt, va_list ap)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001876{
H. Peter Anvin274cda82016-05-10 02:56:29 -07001877 const char *currentfile = NULL;
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001878 int32_t lineno = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001879
Ed Berosetfa771012002-06-09 20:56:40 +00001880 if (is_suppressed_warning(severity))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001881 return;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001882
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001883 if (!(severity & ERR_NOFILE))
H. Peter Anvin4def1a82016-05-09 13:59:44 -07001884 src_get(&lineno, &currentfile);
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001885
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001886 if (!skip_this_pass(severity)) {
1887 if (currentfile) {
1888 fprintf(error_file, "%s:%"PRId32": ", currentfile, lineno);
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001889 } else {
1890 fputs("nasm: ", error_file);
1891 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001892 }
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001893
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001894 nasm_verror_common(severity, fmt, ap);
Ed Berosetfa771012002-06-09 20:56:40 +00001895}
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001896
Ed Berosetfa771012002-06-09 20:56:40 +00001897/**
1898 * MS style error reporting
1899 * This function prints an error message to error_file in the
H. Peter Anvin70653092007-10-19 14:42:29 -07001900 * style used by Visual C and some other Microsoft tools. An example
Ed Berosetfa771012002-06-09 20:56:40 +00001901 * would be:
Ed Beroset6e61d0d2002-06-11 03:29:36 +00001902 * file.asm(50) : error: blah blah blah
H. Peter Anvin70653092007-10-19 14:42:29 -07001903 * where file.asm is the name of the file, 50 is the line number on
Ed Beroset6e61d0d2002-06-11 03:29:36 +00001904 * which the error occurs (or is detected) and "error:" is one of
1905 * the possible optional diagnostics -- it can be "error" or "warning"
H. Peter Anvin70653092007-10-19 14:42:29 -07001906 * or something else. Finally the line terminates with the actual
Ed Beroset6e61d0d2002-06-11 03:29:36 +00001907 * error message.
H. Peter Anvin70653092007-10-19 14:42:29 -07001908 *
1909 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001910 * @param fmt the printf style format string
1911 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001912static void nasm_verror_vc(int severity, const char *fmt, va_list ap)
Ed Berosetfa771012002-06-09 20:56:40 +00001913{
H. Peter Anvin274cda82016-05-10 02:56:29 -07001914 const char *currentfile = NULL;
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001915 int32_t lineno = 0;
Ed Berosetfa771012002-06-09 20:56:40 +00001916
H. Peter Anvine2c80182005-01-15 22:15:51 +00001917 if (is_suppressed_warning(severity))
1918 return;
Ed Berosetfa771012002-06-09 20:56:40 +00001919
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001920 if (!(severity & ERR_NOFILE))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001921 src_get(&lineno, &currentfile);
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001922
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001923 if (!skip_this_pass(severity)) {
1924 if (currentfile) {
1925 fprintf(error_file, "%s(%"PRId32") : ", currentfile, lineno);
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001926 } else {
1927 fputs("nasm: ", error_file);
1928 }
Ed Berosetfa771012002-06-09 20:56:40 +00001929 }
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001930
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001931 nasm_verror_common(severity, fmt, ap);
Ed Berosetfa771012002-06-09 20:56:40 +00001932}
1933
1934/**
1935 * check for supressed warning
H. Peter Anvin70653092007-10-19 14:42:29 -07001936 * checks for suppressed warning or pass one only warning and we're
Ed Berosetfa771012002-06-09 20:56:40 +00001937 * not in pass 1
1938 *
1939 * @param severity the severity of the warning or error
1940 * @return true if we should abort error/warning printing
1941 */
H. Peter Anvin2b046cf2008-01-22 14:08:36 -08001942static bool is_suppressed_warning(int severity)
Ed Berosetfa771012002-06-09 20:56:40 +00001943{
Cyrill Gorcunovead87722011-12-04 19:24:25 +04001944 /* Not a warning at all */
1945 if ((severity & ERR_MASK) != ERR_WARNING)
1946 return false;
1947
H. Peter Anvin442a05a2012-02-24 21:50:53 -08001948 /* Might be a warning but suppresed explicitly */
1949 if (severity & ERR_WARN_MASK)
1950 return !warning_on[WARN_IDX(severity)];
1951 else
1952 return false;
Ed Berosetfa771012002-06-09 20:56:40 +00001953}
1954
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001955static bool skip_this_pass(int severity)
1956{
H. Peter Anvin934f0472016-05-09 12:00:19 -07001957 /* See if it's a pass-specific warning which should be skipped. */
1958
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001959 if ((severity & ERR_MASK) > ERR_WARNING)
1960 return false;
1961
H. Peter Anvin934f0472016-05-09 12:00:19 -07001962 /*
1963 * passn is 1 on the very first pass only.
1964 * pass0 is 2 on the code-generation (final) pass only.
1965 * These are the passes we care about in this case.
1966 */
1967 return (((severity & ERR_PASS1) && passn != 1) ||
1968 ((severity & ERR_PASS2) && pass0 != 2));
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001969}
1970
Ed Berosetfa771012002-06-09 20:56:40 +00001971/**
1972 * common error reporting
1973 * This is the common back end of the error reporting schemes currently
H. Peter Anvin70653092007-10-19 14:42:29 -07001974 * implemented. It prints the nature of the warning and then the
Ed Berosetfa771012002-06-09 20:56:40 +00001975 * specific error message to error_file and may or may not return. It
1976 * doesn't return if the error severity is a "panic" or "debug" type.
H. Peter Anvin70653092007-10-19 14:42:29 -07001977 *
1978 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001979 * @param fmt the printf style format string
1980 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001981static void nasm_verror_common(int severity, const char *fmt, va_list args)
Ed Berosetfa771012002-06-09 20:56:40 +00001982{
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001983 char msg[1024];
1984 const char *pfx;
H. Peter Anvin323fcff2009-07-12 12:04:56 -07001985
H. Peter Anvin7df04172008-06-10 18:27:38 -07001986 switch (severity & (ERR_MASK|ERR_NO_SEVERITY)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001987 case ERR_WARNING:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001988 pfx = "warning: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001989 break;
1990 case ERR_NONFATAL:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001991 pfx = "error: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001992 break;
1993 case ERR_FATAL:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001994 pfx = "fatal: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001995 break;
1996 case ERR_PANIC:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001997 pfx = "panic: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001998 break;
1999 case ERR_DEBUG:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07002000 pfx = "debug: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00002001 break;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002002 default:
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04002003 pfx = "";
2004 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002005 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002006
H. Peter Anvin934f0472016-05-09 12:00:19 -07002007 vsnprintf(msg, sizeof msg - 64, fmt, args);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07002008 if ((severity & (ERR_WARN_MASK|ERR_PP_LISTMACRO)) == ERR_WARN_MASK) {
H. Peter Anvin934f0472016-05-09 12:00:19 -07002009 char *p = strchr(msg, '\0');
2010 snprintf(p, 64, " [-w+%s]", warnings[WARN_IDX(severity)].name);
2011 }
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07002012
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08002013 if (!skip_this_pass(severity))
2014 fprintf(error_file, "%s%s\n", pfx, msg);
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07002015
H. Peter Anvin4def1a82016-05-09 13:59:44 -07002016 /* Are we recursing from error_list_macros? */
2017 if (severity & ERR_PP_LISTMACRO)
2018 return;
2019
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08002020 /*
2021 * Don't suppress this with skip_this_pass(), or we don't get
H. Peter Anvin934f0472016-05-09 12:00:19 -07002022 * pass1 or preprocessor warnings in the list file
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08002023 */
H. Peter Anvin0fcb4882016-07-06 11:55:25 -07002024 lfmt->error(severity, pfx, msg);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002025
2026 if (severity & ERR_USAGE)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002027 want_usage = true;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002028
H. Peter Anvin4def1a82016-05-09 13:59:44 -07002029 preproc->error_list_macros(severity);
2030
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002031 switch (severity & ERR_MASK) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002032 case ERR_DEBUG:
2033 /* no further action, by definition */
2034 break;
H. Peter Anvinb030c922007-11-13 11:31:15 -08002035 case ERR_WARNING:
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04002036 /* Treat warnings as errors */
2037 if (warning_on[WARN_IDX(ERR_WARN_TERM)])
2038 terminate_after_phase = true;
2039 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002040 case ERR_NONFATAL:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002041 terminate_after_phase = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002042 break;
2043 case ERR_FATAL:
2044 if (ofile) {
2045 fclose(ofile);
2046 remove(outname);
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04002047 ofile = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002048 }
2049 if (want_usage)
2050 usage();
2051 exit(1); /* instantly die */
2052 break; /* placate silly compilers */
2053 case ERR_PANIC:
2054 fflush(NULL);
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04002055 /* abort(); */ /* halt, catch fire, and dump core */
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08002056 if (ofile) {
2057 fclose(ofile);
2058 remove(outname);
2059 ofile = NULL;
2060 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002061 exit(3);
2062 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002063 }
2064}
2065
H. Peter Anvin734b1882002-04-30 21:01:08 +00002066static void usage(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002067{
H. Peter Anvin620515a2002-04-30 20:57:38 +00002068 fputs("type `nasm -h' for help\n", error_file);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002069}
2070
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002071static iflag_t get_cpu(char *value)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002072{
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002073 iflag_t r;
2074
2075 iflag_clear_all(&r);
2076
H. Peter Anvine2c80182005-01-15 22:15:51 +00002077 if (!strcmp(value, "8086"))
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002078 iflag_set(&r, IF_8086);
2079 else if (!strcmp(value, "186"))
2080 iflag_set(&r, IF_186);
2081 else if (!strcmp(value, "286"))
2082 iflag_set(&r, IF_286);
2083 else if (!strcmp(value, "386"))
2084 iflag_set(&r, IF_386);
2085 else if (!strcmp(value, "486"))
2086 iflag_set(&r, IF_486);
2087 else if (!strcmp(value, "586") ||
2088 !nasm_stricmp(value, "pentium"))
2089 iflag_set(&r, IF_PENT);
2090 else if (!strcmp(value, "686") ||
2091 !nasm_stricmp(value, "ppro") ||
2092 !nasm_stricmp(value, "pentiumpro") ||
2093 !nasm_stricmp(value, "p2"))
2094 iflag_set(&r, IF_P6);
2095 else if (!nasm_stricmp(value, "p3") ||
2096 !nasm_stricmp(value, "katmai"))
2097 iflag_set(&r, IF_KATMAI);
2098 else if (!nasm_stricmp(value, "p4") || /* is this right? -- jrc */
2099 !nasm_stricmp(value, "willamette"))
2100 iflag_set(&r, IF_WILLAMETTE);
2101 else if (!nasm_stricmp(value, "prescott"))
2102 iflag_set(&r, IF_PRESCOTT);
2103 else if (!nasm_stricmp(value, "x64") ||
2104 !nasm_stricmp(value, "x86-64"))
2105 iflag_set(&r, IF_X86_64);
2106 else if (!nasm_stricmp(value, "ia64") ||
2107 !nasm_stricmp(value, "ia-64") ||
2108 !nasm_stricmp(value, "itanium")||
2109 !nasm_stricmp(value, "itanic") ||
2110 !nasm_stricmp(value, "merced"))
2111 iflag_set(&r, IF_IA64);
2112 else {
2113 iflag_set(&r, IF_PLEVEL);
2114 nasm_error(pass0 < 2 ? ERR_NONFATAL : ERR_FATAL,
2115 "unknown 'cpu' type");
2116 }
2117 return r;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002118}
2119
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002120static int get_bits(char *value)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002121{
2122 int i;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002123
H. Peter Anvine2c80182005-01-15 22:15:51 +00002124 if ((i = atoi(value)) == 16)
2125 return i; /* set for a 16-bit segment */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002126 else if (i == 32) {
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002127 if (iflag_ffs(&cpu) < IF_386) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04002128 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002129 "cannot specify 32-bit segment on processor below a 386");
2130 i = 16;
2131 }
Keith Kaniosb7a89542007-04-12 02:40:54 +00002132 } else if (i == 64) {
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002133 if (iflag_ffs(&cpu) < IF_X86_64) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04002134 nasm_error(ERR_NONFATAL,
Keith Kaniosb7a89542007-04-12 02:40:54 +00002135 "cannot specify 64-bit segment on processor below an x86-64");
2136 i = 16;
2137 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002138 } else {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04002139 nasm_error(pass0 < 2 ? ERR_NONFATAL : ERR_FATAL,
Keith Kaniosb7a89542007-04-12 02:40:54 +00002140 "`%s' is not a valid segment size; must be 16, 32 or 64",
H. Peter Anvine2c80182005-01-15 22:15:51 +00002141 value);
2142 i = 16;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002143 }
2144 return i;
2145}