blob: 3c4a1b12ce7fb19d86bb18170d6be9eb232ba146 [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>
Keith Kaniosb7a89542007-04-12 02:40:54 +000045#include <inttypes.h>
H. Peter Anvinfd7dd112007-10-10 14:06:59 -070046#include <limits.h>
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -080047#include <time.h>
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000048
49#include "nasm.h"
50#include "nasmlib.h"
H. Peter Anvin1803ded2008-06-09 17:32:43 -070051#include "saa.h"
H. Peter Anvinfcb89092008-06-09 17:40:16 -070052#include "raa.h"
H. Peter Anvinf6c9e652007-10-16 14:40:27 -070053#include "float.h"
H. Peter Anvin74cc5e52007-08-30 22:35:34 +000054#include "stdscan.h"
H. Peter Anvinaf535c12002-04-30 20:59:21 +000055#include "insns.h"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000056#include "preproc.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000057#include "parser.h"
H. Peter Anvin76690a12002-04-30 20:52:49 +000058#include "eval.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000059#include "assemble.h"
60#include "labels.h"
H. Peter Anvin31b707b2009-06-27 22:07:33 -070061#include "output/outform.h"
H. Peter Anvin6768eb72002-04-30 20:52:26 +000062#include "listing.h"
Cyrill Gorcunov08359152013-11-09 22:16:11 +040063#include "iflag.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 Anvin215186f2016-02-17 20:27:41 -080081static bool is_suppressed_warning(int severity);
H. Peter Anvinbd464742016-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
John Coffman0efaec92002-05-26 19:20:08 +000088static int using_debug_info, opt_verbose_info;
H. Peter Anvin6867acc2007-10-10 14:58:45 -070089bool tasm_compatible_mode = false;
H. Peter Anvin00835fe2008-01-08 23:03:57 -080090int pass0, passn;
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +000091int globalrel = 0;
Jin Kyu Songb287ff02013-12-04 20:05:55 -080092int globalbnd = 0;
H. Peter Anvineba20a72002-04-30 20:53:55 +000093
H. Peter Anvin9bd15062009-07-18 21:07:17 -040094static time_t official_compile_time;
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -080095
Keith Kaniosa6dfa782007-04-13 16:47:53 +000096static char inname[FILENAME_MAX];
97static char outname[FILENAME_MAX];
98static char listname[FILENAME_MAX];
Charles Craynefcce07f2007-09-30 22:15:36 -070099static char errname[FILENAME_MAX];
H. Peter Anvine2c80182005-01-15 22:15:51 +0000100static int globallineno; /* for forward-reference tracking */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000101/* static int pass = 0; */
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400102struct ofmt *ofmt = &OF_DEFAULT;
Cyrill Gorcunovc1936da2011-04-06 18:32:15 +0400103struct ofmt_alias *ofmt_alias = NULL;
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400104const struct dfmt *dfmt;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000105
H. Peter Anvine2c80182005-01-15 22:15:51 +0000106static FILE *error_file; /* Where to write error messages */
H. Peter Anvin620515a2002-04-30 20:57:38 +0000107
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400108FILE *ofile = NULL;
H. Peter Anvin31387b22010-07-15 18:28:52 -0700109int optimizing = MAX_OPTIMIZE; /* number of optimization passes to take */
110static int sb, cmd_sb = 16; /* by default */
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400111
112static iflag_t cpu;
113static iflag_t cmd_cpu;
114
Charles Craynec1905c22008-09-11 18:54:06 -0700115int64_t global_offset_changed; /* referenced in labels.c */
116int64_t prev_offset_changed;
117int32_t stall_count;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000118
H. Peter Anvin12e46512007-10-03 21:30:57 -0700119static struct location location;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000120int in_abs_seg; /* Flag we are in ABSOLUTE seg */
Keith Kaniosb7a89542007-04-12 02:40:54 +0000121int32_t abs_seg; /* ABSOLUTE segment basis */
122int32_t abs_offset; /* ABSOLUTE offset */
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000123
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000124static struct RAA *offsets;
H. Peter Anvinea838272002-04-30 20:51:53 +0000125
H. Peter Anvine2c80182005-01-15 22:15:51 +0000126static struct SAA *forwrefs; /* keep track of forward references */
H. Peter Anvin9d637df2007-10-04 13:42:56 -0700127static const struct forwrefinfo *forwref;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000128
Cyrill Gorcunov86b2ad02011-07-01 10:38:25 +0400129static struct preproc_ops *preproc;
130
Cyrill Gorcunove9fc88c2014-06-23 02:22:02 +0400131#define OP_NORMAL (1u << 0)
132#define OP_PREPROCESS (1u << 1)
133#define OP_DEPEND (1u << 2)
134
135static unsigned int operating_mode;
Cyrill Gorcunov3ed32cb2014-06-22 20:53:12 +0400136
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700137/* Dependency flags */
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700138static bool depend_emit_phony = false;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700139static bool depend_missing_ok = false;
140static const char *depend_target = NULL;
141static const char *depend_file = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000142
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000143/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000144 * Which of the suppressible warnings are suppressed. Entry zero
H. Peter Anvinb030c922007-11-13 11:31:15 -0800145 * isn't an actual warning, but it used for -w+error/-Werror.
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000146 */
Victor van den Elzen819703a2008-07-16 15:20:56 +0200147
H. Peter Anvin972079f2008-09-30 17:01:23 -0700148static bool warning_on[ERR_WARN_MAX+1]; /* Current state */
149static bool warning_on_global[ERR_WARN_MAX+1]; /* Command-line state */
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000150
H. Peter Anvin972079f2008-09-30 17:01:23 -0700151static const struct warning {
152 const char *name;
153 const char *help;
154 bool enabled;
155} warnings[ERR_WARN_MAX+1] = {
156 {"error", "treat warnings as errors", false},
157 {"macro-params", "macro calls with wrong parameter count", true},
158 {"macro-selfref", "cyclic macro references", false},
159 {"macro-defaults", "macros with more default than optional parameters", true},
160 {"orphan-labels", "labels alone on lines without trailing `:'", true},
H. Peter Anvin9a65f712008-10-26 08:59:04 -0700161 {"number-overflow", "numeric constant does not fit", true},
H. Peter Anvin972079f2008-09-30 17:01:23 -0700162 {"gnu-elf-extensions", "using 8- or 16-bit relocation in ELF32, a GNU extension", false},
163 {"float-overflow", "floating point overflow", true},
164 {"float-denorm", "floating point denormal", false},
165 {"float-underflow", "floating point underflow", false},
166 {"float-toolong", "too many digits in floating-point number", true},
167 {"user", "%warning directives", true},
H. Peter Anvin5a24fdd2012-02-25 15:10:04 -0800168 {"lock", "lock prefix on unlockable instructions", true},
169 {"hle", "invalid hle prefixes", true},
Jin Kyu Songbb8cf3f2013-11-29 00:38:29 -0800170 {"bnd", "invalid bnd prefixes", true},
H. Peter Anvinecc9e0e2016-02-11 20:29:34 -0800171 {"zext-reloc", "relocation zero-extended to match output format", true},
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000172};
173
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700174static bool want_usage;
175static bool terminate_after_phase;
H. Peter Anvin215186f2016-02-17 20:27:41 -0800176bool user_nolist = false;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000177
H. Peter Anvin55340992012-09-09 17:09:00 -0700178static char *quote_for_make(const char *str);
179
Cyrill Gorcunov190232f2013-02-15 12:35:04 +0400180static int64_t get_curr_offs(void)
181{
182 return in_abs_seg ? abs_offset : raa_read(offsets, location.segment);
183}
184
185static void set_curr_offs(int64_t l_off)
186{
187 if (in_abs_seg)
188 abs_offset = l_off;
189 else
190 offsets = raa_write(offsets, location.segment, l_off);
191}
192
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000193static void nasm_fputs(const char *line, FILE * outfile)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000194{
H. Peter Anvin310b3e12002-05-14 22:38:55 +0000195 if (outfile) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000196 fputs(line, outfile);
H. Peter Anvind1fb15c2007-11-13 09:37:59 -0800197 putc('\n', outfile);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000198 } else
H. Peter Anvine2c80182005-01-15 22:15:51 +0000199 puts(line);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000200}
201
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800202/* Convert a struct tm to a POSIX-style time constant */
H. Peter Anvin724719b2015-01-05 15:17:56 -0800203static int64_t make_posix_time(struct tm *tm)
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800204{
205 int64_t t;
206 int64_t y = tm->tm_year;
207
208 /* See IEEE 1003.1:2004, section 4.14 */
209
210 t = (y-70)*365 + (y-69)/4 - (y-1)/100 + (y+299)/400;
211 t += tm->tm_yday;
212 t *= 24;
213 t += tm->tm_hour;
214 t *= 60;
215 t += tm->tm_min;
216 t *= 60;
217 t += tm->tm_sec;
218
219 return t;
220}
221
222static void define_macros_early(void)
223{
224 char temp[128];
225 struct tm lt, *lt_p, gm, *gm_p;
226 int64_t posix_time;
227
228 lt_p = localtime(&official_compile_time);
229 if (lt_p) {
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400230 lt = *lt_p;
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800231
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400232 strftime(temp, sizeof temp, "__DATE__=\"%Y-%m-%d\"", &lt);
233 preproc->pre_define(temp);
234 strftime(temp, sizeof temp, "__DATE_NUM__=%Y%m%d", &lt);
235 preproc->pre_define(temp);
236 strftime(temp, sizeof temp, "__TIME__=\"%H:%M:%S\"", &lt);
237 preproc->pre_define(temp);
238 strftime(temp, sizeof temp, "__TIME_NUM__=%H%M%S", &lt);
239 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800240 }
241
242 gm_p = gmtime(&official_compile_time);
243 if (gm_p) {
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400244 gm = *gm_p;
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800245
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400246 strftime(temp, sizeof temp, "__UTC_DATE__=\"%Y-%m-%d\"", &gm);
247 preproc->pre_define(temp);
248 strftime(temp, sizeof temp, "__UTC_DATE_NUM__=%Y%m%d", &gm);
249 preproc->pre_define(temp);
250 strftime(temp, sizeof temp, "__UTC_TIME__=\"%H:%M:%S\"", &gm);
251 preproc->pre_define(temp);
252 strftime(temp, sizeof temp, "__UTC_TIME_NUM__=%H%M%S", &gm);
253 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800254 }
H. Peter Anvind85d2502008-05-04 17:53:31 -0700255
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800256 if (gm_p)
H. Peter Anvin724719b2015-01-05 15:17:56 -0800257 posix_time = make_posix_time(&gm);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800258 else if (lt_p)
H. Peter Anvin724719b2015-01-05 15:17:56 -0800259 posix_time = make_posix_time(&lt);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800260 else
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400261 posix_time = 0;
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800262
263 if (posix_time) {
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400264 snprintf(temp, sizeof temp, "__POSIX_TIME__=%"PRId64, posix_time);
265 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800266 }
267}
268
269static void define_macros_late(void)
270{
271 char temp[128];
272
Cyrill Gorcunovc1936da2011-04-06 18:32:15 +0400273 /*
274 * In case if output format is defined by alias
275 * we have to put shortname of the alias itself here
276 * otherwise ABI backward compatibility gets broken.
277 */
Cyrill Gorcunov69ce7502010-07-12 15:19:17 +0400278 snprintf(temp, sizeof(temp), "__OUTPUT_FORMAT__=%s",
Cyrill Gorcunovc1936da2011-04-06 18:32:15 +0400279 ofmt_alias ? ofmt_alias->shortname : ofmt->shortname);
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +0400280 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800281}
282
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700283static void emit_dependencies(StrList *list)
284{
285 FILE *deps;
286 int linepos, len;
287 StrList *l, *nl;
288
289 if (depend_file && strcmp(depend_file, "-")) {
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400290 deps = fopen(depend_file, "w");
291 if (!deps) {
292 nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
293 "unable to write dependency file `%s'", depend_file);
294 return;
295 }
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700296 } else {
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400297 deps = stdout;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700298 }
H. Peter Anvin323fcff2009-07-12 12:04:56 -0700299
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700300 linepos = fprintf(deps, "%s:", depend_target);
Cyrill Gorcunovfcd0a742009-07-27 16:26:26 +0400301 list_for_each(l, list) {
H. Peter Anvin55340992012-09-09 17:09:00 -0700302 char *file = quote_for_make(l->str);
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400303 len = strlen(file);
304 if (linepos + len > 62 && linepos > 1) {
305 fprintf(deps, " \\\n ");
306 linepos = 1;
307 }
308 fprintf(deps, " %s", file);
309 linepos += len+1;
H. Peter Anvin55340992012-09-09 17:09:00 -0700310 nasm_free(file);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700311 }
312 fprintf(deps, "\n\n");
H. Peter Anvin323fcff2009-07-12 12:04:56 -0700313
Cyrill Gorcunovfcd0a742009-07-27 16:26:26 +0400314 list_for_each_safe(l, nl, list) {
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400315 if (depend_emit_phony)
316 fprintf(deps, "%s:\n\n", l->str);
317 nasm_free(l);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700318 }
319
320 if (deps != stdout)
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400321 fclose(deps);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700322}
323
H. Peter Anvin038d8612007-04-12 16:54:50 +0000324int main(int argc, char **argv)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000325{
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700326 StrList *depend_list = NULL, **depend_ptr;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700327
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800328 time(&official_compile_time);
329
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400330 iflag_set(&cpu, IF_PLEVEL);
331 iflag_set(&cmd_cpu, IF_PLEVEL);
332
Charles Crayne2581c862008-09-10 19:21:52 -0700333 pass0 = 0;
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700334 want_usage = terminate_after_phase = false;
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400335 nasm_set_verror(nasm_verror_gnu);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000336
H. Peter Anvin97e15752007-09-25 08:47:47 -0700337 error_file = stderr;
338
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -0700339 tolower_init();
340
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000341 offsets = raa_init();
Keith Kaniosb7a89542007-04-12 02:40:54 +0000342 forwrefs = saa_init((int32_t)sizeof(struct forwrefinfo));
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000343
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000344 preproc = &nasmpp;
Cyrill Gorcunov3ed32cb2014-06-22 20:53:12 +0400345 operating_mode = OP_NORMAL;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000346
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800347 /* Define some macros dependent on the runtime, but not
348 on the command line. */
349 define_macros_early();
350
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000351 parse_cmdline(argc, argv);
352
H. Peter Anvine2c80182005-01-15 22:15:51 +0000353 if (terminate_after_phase) {
354 if (want_usage)
355 usage();
356 return 1;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000357 }
358
H. Peter Anvinbb88d012003-09-10 23:34:23 +0000359 /* If debugging info is disabled, suppress any debug calls */
360 if (!using_debug_info)
H. Peter Anvin335c4852016-02-17 20:55:08 -0800361 dfmt = &null_debug_form;
362 else if (!dfmt)
363 dfmt = ofmt->default_dfmt;
H. Peter Anvinbb88d012003-09-10 23:34:23 +0000364
H. Peter Anvin76690a12002-04-30 20:52:49 +0000365 if (ofmt->stdmac)
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +0400366 preproc->extra_stdmac(ofmt->stdmac);
H. Peter Anvin605f5152009-07-18 18:31:41 -0700367 parser_global_info(&location);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000368 eval_global_info(ofmt, lookup_label, &location);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000369
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000370 /* define some macros dependent of command-line */
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800371 define_macros_late();
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000372
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400373 depend_ptr = (depend_file || (operating_mode & OP_DEPEND)) ? &depend_list : NULL;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700374 if (!depend_target)
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400375 depend_target = quote_for_make(outname);
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700376
Cyrill Gorcunove9fc88c2014-06-23 02:22:02 +0400377 if (operating_mode & OP_DEPEND) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000378 char *line;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700379
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400380 if (depend_missing_ok)
381 preproc->include_path(NULL); /* "assume generated" */
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700382
H. Peter Anvin215186f2016-02-17 20:27:41 -0800383 preproc->reset(inname, 0, depend_ptr);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000384 if (outname[0] == '\0')
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400385 ofmt->filename(inname, outname);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000386 ofile = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000387 while ((line = preproc->getline()))
388 nasm_free(line);
389 preproc->cleanup(0);
Cyrill Gorcunove9fc88c2014-06-23 02:22:02 +0400390 } else if (operating_mode & OP_PREPROCESS) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000391 char *line;
392 char *file_name = NULL;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000393 int32_t prior_linnum = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000394 int lineinc = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000395
H. Peter Anvine2c80182005-01-15 22:15:51 +0000396 if (*outname) {
397 ofile = fopen(outname, "w");
398 if (!ofile)
H. Peter Anvin41087062016-03-03 14:27:34 -0800399 nasm_fatal(ERR_NOFILE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000400 "unable to open output file `%s'",
401 outname);
402 } else
403 ofile = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000404
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700405 location.known = false;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000406
Cyrill Gorcunovb574b072011-12-05 01:56:40 +0400407 /* pass = 1; */
H. Peter Anvin215186f2016-02-17 20:27:41 -0800408 preproc->reset(inname, 3, depend_ptr);
409 memcpy(warning_on, warning_on_global,
410 (ERR_WARN_MAX+1) * sizeof(bool));
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700411
H. Peter Anvine2c80182005-01-15 22:15:51 +0000412 while ((line = preproc->getline())) {
413 /*
414 * We generate %line directives if needed for later programs
415 */
Keith Kaniosb7a89542007-04-12 02:40:54 +0000416 int32_t linnum = prior_linnum += lineinc;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000417 int altline = src_get(&linnum, &file_name);
418 if (altline) {
419 if (altline == 1 && lineinc == 1)
420 nasm_fputs("", ofile);
421 else {
422 lineinc = (altline != -1 || lineinc != 1);
423 fprintf(ofile ? ofile : stdout,
Keith Kanios93f2e9a2007-04-14 00:10:59 +0000424 "%%line %"PRId32"+%d %s\n", linnum, lineinc,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000425 file_name);
426 }
427 prior_linnum = linnum;
428 }
429 nasm_fputs(line, ofile);
430 nasm_free(line);
431 }
432 nasm_free(file_name);
433 preproc->cleanup(0);
434 if (ofile)
435 fclose(ofile);
436 if (ofile && terminate_after_phase)
437 remove(outname);
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400438 ofile = NULL;
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400439 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000440
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400441 if (operating_mode & OP_NORMAL) {
442 /*
443 * We must call ofmt->filename _anyway_, even if the user
444 * has specified their own output file, because some
445 * formats (eg OBJ and COFF) use ofmt->filename to find out
446 * the name of the input file and then put that inside the
447 * file.
448 */
449 ofmt->filename(inname, outname);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000450
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400451 ofile = fopen(outname, (ofmt->flags & OFMT_TEXT) ? "w" : "wb");
452 if (!ofile)
H. Peter Anvin41087062016-03-03 14:27:34 -0800453 nasm_fatal(ERR_NOFILE,
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400454 "unable to open output file `%s'", outname);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000455
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400456 /*
457 * We must call init_labels() before ofmt->init() since
458 * some object formats will want to define labels in their
459 * init routines. (eg OS/2 defines the FLAT group)
460 */
461 init_labels();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000462
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400463 ofmt->init();
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400464 dfmt->init();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000465
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400466 assemble_file(inname, depend_ptr);
Victor van den Elzenc82c3722008-06-04 15:24:20 +0200467
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400468 if (!terminate_after_phase) {
469 ofmt->cleanup(using_debug_info);
470 cleanup_labels();
471 fflush(ofile);
472 if (ferror(ofile))
473 nasm_error(ERR_NONFATAL|ERR_NOFILE,
474 "write error on output file `%s'", outname);
475 }
476
477 if (ofile) {
478 fclose(ofile);
479 if (terminate_after_phase)
480 remove(outname);
481 ofile = NULL;
482 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000483 }
H. Peter Anvin734b1882002-04-30 21:01:08 +0000484
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700485 if (depend_list && !terminate_after_phase)
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400486 emit_dependencies(depend_list);
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700487
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000488 if (want_usage)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000489 usage();
H. Peter Anvin734b1882002-04-30 21:01:08 +0000490
H. Peter Anvine2c80182005-01-15 22:15:51 +0000491 raa_free(offsets);
492 saa_free(forwrefs);
493 eval_cleanup();
H. Peter Anvin74cc5e52007-08-30 22:35:34 +0000494 stdscan_cleanup();
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000495
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700496 return terminate_after_phase;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000497}
498
H. Peter Anvineba20a72002-04-30 20:53:55 +0000499/*
500 * Get a parameter for a command line option.
501 * First arg must be in the form of e.g. -f...
502 */
H. Peter Anvin423e3812007-11-15 17:12:29 -0800503static char *get_param(char *p, char *q, bool *advance)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000504{
H. Peter Anvin423e3812007-11-15 17:12:29 -0800505 *advance = false;
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +0400506 if (p[2]) /* the parameter's in the option */
507 return nasm_skip_spaces(p + 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000508 if (q && q[0]) {
H. Peter Anvin423e3812007-11-15 17:12:29 -0800509 *advance = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000510 return q;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000511 }
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400512 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000513 "option `-%c' requires an argument", p[1]);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000514 return NULL;
515}
516
H. Peter Anvindc242712007-11-18 11:55:10 -0800517/*
518 * Copy a filename
519 */
520static void copy_filename(char *dst, const char *src)
521{
522 size_t len = strlen(src);
523
524 if (len >= (size_t)FILENAME_MAX) {
H. Peter Anvin41087062016-03-03 14:27:34 -0800525 nasm_fatal(ERR_NOFILE, "file name too long");
Cyrill Gorcunov45aa1182013-02-15 12:22:59 +0400526 return;
H. Peter Anvindc242712007-11-18 11:55:10 -0800527 }
528 strncpy(dst, src, FILENAME_MAX);
529}
530
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700531/*
532 * Convert a string to Make-safe form
533 */
534static char *quote_for_make(const char *str)
535{
536 const char *p;
537 char *os, *q;
538
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400539 size_t n = 1; /* Terminating zero */
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700540 size_t nbs = 0;
541
542 if (!str)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400543 return NULL;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700544
545 for (p = str; *p; p++) {
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400546 switch (*p) {
547 case ' ':
548 case '\t':
549 /* Convert N backslashes + ws -> 2N+1 backslashes + ws */
550 n += nbs + 2;
551 nbs = 0;
552 break;
553 case '$':
554 case '#':
555 nbs = 0;
556 n += 2;
557 break;
558 case '\\':
559 nbs++;
560 n++;
561 break;
562 default:
563 nbs = 0;
564 n++;
565 break;
566 }
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700567 }
568
569 /* Convert N backslashes at the end of filename to 2N backslashes */
570 if (nbs)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400571 n += nbs;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700572
573 os = q = nasm_malloc(n);
574
575 nbs = 0;
576 for (p = str; *p; p++) {
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400577 switch (*p) {
578 case ' ':
579 case '\t':
580 while (nbs--)
581 *q++ = '\\';
582 *q++ = '\\';
583 *q++ = *p;
584 break;
585 case '$':
586 *q++ = *p;
587 *q++ = *p;
588 nbs = 0;
589 break;
590 case '#':
591 *q++ = '\\';
592 *q++ = *p;
593 nbs = 0;
594 break;
595 case '\\':
596 *q++ = *p;
597 nbs++;
598 break;
599 default:
600 *q++ = *p;
601 nbs = 0;
602 break;
603 }
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700604 }
605 while (nbs--)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400606 *q++ = '\\';
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700607
608 *q = '\0';
609
610 return os;
611}
612
H. Peter Anvine2c80182005-01-15 22:15:51 +0000613struct textargs {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000614 const char *label;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000615 int value;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000616};
617
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100618enum text_options {
619 OPT_PREFIX,
H. Peter Anvin53f15592016-03-01 22:43:51 -0800620 OPT_POSTFIX
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100621};
H. Peter Anvine2c80182005-01-15 22:15:51 +0000622struct textargs textopts[] = {
623 {"prefix", OPT_PREFIX},
624 {"postfix", OPT_POSTFIX},
625 {NULL, 0}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000626};
627
Cyrill Gorcunove7438432014-05-09 22:34:34 +0400628static void show_version(void)
629{
630 printf("NASM version %s compiled on %s%s\n",
631 nasm_version, nasm_date, nasm_compile_options);
632 exit(0);
633}
634
H. Peter Anvin423e3812007-11-15 17:12:29 -0800635static bool stopoptions = false;
636static bool process_arg(char *p, char *q)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000637{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000638 char *param;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800639 int i;
640 bool advance = false;
H. Peter Anvin972079f2008-09-30 17:01:23 -0700641 bool do_warn;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000642
643 if (!p || !p[0])
H. Peter Anvin423e3812007-11-15 17:12:29 -0800644 return false;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000645
H. Peter Anvine2c80182005-01-15 22:15:51 +0000646 if (p[0] == '-' && !stopoptions) {
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400647 if (strchr("oOfpPdDiIlFXuUZwW", p[1])) {
648 /* These parameters take values */
649 if (!(param = get_param(p, q, &advance)))
650 return advance;
651 }
H. Peter Anvin423e3812007-11-15 17:12:29 -0800652
H. Peter Anvine2c80182005-01-15 22:15:51 +0000653 switch (p[1]) {
654 case 's':
655 error_file = stdout;
656 break;
H. Peter Anvin70653092007-10-19 14:42:29 -0700657
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400658 case 'o': /* output file */
659 copy_filename(outname, param);
660 break;
H. Peter Anvinfd7dd112007-10-10 14:06:59 -0700661
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400662 case 'f': /* output format */
663 ofmt = ofmt_find(param, &ofmt_alias);
664 if (!ofmt) {
H. Peter Anvin41087062016-03-03 14:27:34 -0800665 nasm_fatal(ERR_NOFILE | ERR_USAGE,
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400666 "unrecognised output format `%s' - "
667 "use -hf for a list", param);
668 }
H. Peter Anvin335c4852016-02-17 20:55:08 -0800669 dfmt = NULL;
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400670 break;
H. Peter Anvin70653092007-10-19 14:42:29 -0700671
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400672 case 'O': /* Optimization level */
673 {
674 int opt;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700675
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400676 if (!*param) {
677 /* Naked -O == -Ox */
678 optimizing = MAX_OPTIMIZE;
679 } else {
680 while (*param) {
681 switch (*param) {
682 case '0': case '1': case '2': case '3': case '4':
683 case '5': case '6': case '7': case '8': case '9':
684 opt = strtoul(param, &param, 10);
H. Peter Anvind85d2502008-05-04 17:53:31 -0700685
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400686 /* -O0 -> optimizing == -1, 0.98 behaviour */
687 /* -O1 -> optimizing == 0, 0.98.09 behaviour */
688 if (opt < 2)
689 optimizing = opt - 1;
690 else
691 optimizing = opt;
692 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700693
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400694 case 'v':
695 case '+':
696 param++;
697 opt_verbose_info = true;
698 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700699
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400700 case 'x':
701 param++;
702 optimizing = MAX_OPTIMIZE;
703 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700704
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400705 default:
H. Peter Anvin41087062016-03-03 14:27:34 -0800706 nasm_fatal(0,
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400707 "unknown optimization option -O%c\n",
708 *param);
709 break;
710 }
711 }
712 if (optimizing > MAX_OPTIMIZE)
713 optimizing = MAX_OPTIMIZE;
714 }
715 break;
716 }
H. Peter Anvin423e3812007-11-15 17:12:29 -0800717
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400718 case 'p': /* pre-include */
719 case 'P':
720 preproc->pre_include(param);
721 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800722
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400723 case 'd': /* pre-define */
724 case 'D':
725 preproc->pre_define(param);
726 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800727
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400728 case 'u': /* un-define */
729 case 'U':
730 preproc->pre_undefine(param);
731 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800732
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400733 case 'i': /* include search path */
734 case 'I':
735 preproc->include_path(param);
736 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800737
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400738 case 'l': /* listing file */
739 copy_filename(listname, param);
740 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800741
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400742 case 'Z': /* error messages file */
743 copy_filename(errname, param);
744 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800745
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400746 case 'F': /* specify debug format */
H. Peter Anvin335c4852016-02-17 20:55:08 -0800747 dfmt = dfmt_find(ofmt, param);
748 if (!dfmt) {
H. Peter Anvin41087062016-03-03 14:27:34 -0800749 nasm_fatal(ERR_NOFILE | ERR_USAGE,
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400750 "unrecognized debug format `%s' for"
751 " output format `%s'",
752 param, ofmt->shortname);
753 }
754 using_debug_info = true;
755 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800756
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400757 case 'X': /* specify error reporting format */
758 if (nasm_stricmp("vc", param) == 0)
759 nasm_set_verror(nasm_verror_vc);
760 else if (nasm_stricmp("gnu", param) == 0)
761 nasm_set_verror(nasm_verror_gnu);
762 else
H. Peter Anvin41087062016-03-03 14:27:34 -0800763 nasm_fatal(ERR_NOFILE | ERR_USAGE,
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400764 "unrecognized error reporting format `%s'",
765 param);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000766 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800767
H. Peter Anvine2c80182005-01-15 22:15:51 +0000768 case 'g':
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700769 using_debug_info = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000770 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800771
H. Peter Anvine2c80182005-01-15 22:15:51 +0000772 case 'h':
773 printf
774 ("usage: nasm [-@ response file] [-o outfile] [-f format] "
775 "[-l listfile]\n"
776 " [options...] [--] filename\n"
Cyrill Gorcunove7438432014-05-09 22:34:34 +0400777 " or nasm -v (or --v) for version info\n\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000778 " -t assemble in SciTech TASM compatible mode\n"
Cyrill Gorcunov984c4db2009-07-31 00:32:17 +0400779 " -g generate debug information in selected format\n");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000780 printf
H. Peter Anvin413fb902007-09-26 15:19:28 -0700781 (" -E (or -e) preprocess only (writes output to stdout by default)\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000782 " -a don't preprocess (assemble only)\n"
H. Peter Anvin37a321f2007-09-24 13:41:58 -0700783 " -M generate Makefile dependencies on stdout\n"
Cyrill Gorcunov984c4db2009-07-31 00:32:17 +0400784 " -MG d:o, missing files assumed generated\n"
785 " -MF <file> set Makefile dependency file\n"
786 " -MD <file> assemble and generate dependencies\n"
787 " -MT <file> dependency target name\n"
788 " -MQ <file> dependency target name (quoted)\n"
789 " -MP emit phony target\n\n"
H. Peter Anvin413fb902007-09-26 15:19:28 -0700790 " -Z<file> redirect error messages to file\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000791 " -s redirect error messages to stdout\n\n"
792 " -F format select a debugging format\n\n"
Cyrill Gorcunovdeb082d2013-04-20 20:37:17 +0400793 " -o outfile write output to an outfile\n\n"
794 " -f format select an output format\n\n"
795 " -l listfile write listing to a listfile\n\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000796 " -I<path> adds a pathname to the include file path\n");
797 printf
H. Peter Anvinab5bd052010-07-25 12:43:30 -0700798 (" -O<digit> optimize branch offsets\n"
Cyrill Gorcunov5bc6d8e2012-03-11 14:19:17 +0400799 " -O0: No optimization\n"
Cyrill Gorcunov73b87c62009-07-31 10:26:55 +0400800 " -O1: Minimal optimization\n"
Cyrill Gorcunov5bc6d8e2012-03-11 14:19:17 +0400801 " -Ox: Multipass optimization (default)\n\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000802 " -P<file> pre-includes a file\n"
803 " -D<macro>[=<value>] pre-defines a macro\n"
804 " -U<macro> undefines a macro\n"
805 " -X<format> specifies error reporting format (gnu or vc)\n"
H. Peter Anvinb030c922007-11-13 11:31:15 -0800806 " -w+foo enables warning foo (equiv. -Wfoo)\n"
Cyrill Gorcunov984c4db2009-07-31 00:32:17 +0400807 " -w-foo disable warning foo (equiv. -Wno-foo)\n\n"
Cyrill Gorcunovdeb082d2013-04-20 20:37:17 +0400808 " -h show invocation summary and exit\n\n"
Cyrill Gorcunov984c4db2009-07-31 00:32:17 +0400809 "--prefix,--postfix\n"
810 " this options prepend or append the given argument to all\n"
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100811 " extern and global variables\n"
H. Peter Anvinb030c922007-11-13 11:31:15 -0800812 "Warnings:\n");
813 for (i = 0; i <= ERR_WARN_MAX; i++)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000814 printf(" %-23s %s (default %s)\n",
H. Peter Anvin972079f2008-09-30 17:01:23 -0700815 warnings[i].name, warnings[i].help,
816 warnings[i].enabled ? "on" : "off");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000817 printf
818 ("\nresponse files should contain command line parameters"
819 ", one per line.\n");
820 if (p[2] == 'f') {
821 printf("\nvalid output formats for -f are"
822 " (`*' denotes default):\n");
823 ofmt_list(ofmt, stdout);
824 } else {
825 printf("\nFor a list of valid output formats, use -hf.\n");
826 printf("For a list of debug formats, use -f <form> -y.\n");
827 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400828 exit(0); /* never need usage message here */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000829 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800830
H. Peter Anvine2c80182005-01-15 22:15:51 +0000831 case 'y':
832 printf("\nvalid debug formats for '%s' output format are"
833 " ('*' denotes default):\n", ofmt->shortname);
834 dfmt_list(ofmt, stdout);
835 exit(0);
836 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800837
H. Peter Anvine2c80182005-01-15 22:15:51 +0000838 case 't':
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700839 tasm_compatible_mode = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000840 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800841
H. Peter Anvine2c80182005-01-15 22:15:51 +0000842 case 'v':
Cyrill Gorcunove7438432014-05-09 22:34:34 +0400843 show_version();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000844 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800845
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400846 case 'e': /* preprocess only */
847 case 'E':
Cyrill Gorcunov3ed32cb2014-06-22 20:53:12 +0400848 operating_mode = OP_PREPROCESS;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000849 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800850
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400851 case 'a': /* assemble only - don't preprocess */
Cyrill Gorcunovb5e8fec2012-05-07 11:34:27 +0400852 preproc = &preproc_nop;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000853 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800854
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400855 case 'W':
856 if (param[0] == 'n' && param[1] == 'o' && param[2] == '-') {
857 do_warn = false;
858 param += 3;
859 } else {
860 do_warn = true;
861 }
862 goto set_warning;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800863
H. Peter Anvine2c80182005-01-15 22:15:51 +0000864 case 'w':
H. Peter Anvin423e3812007-11-15 17:12:29 -0800865 if (param[0] != '+' && param[0] != '-') {
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400866 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000867 "invalid option to `-w'");
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400868 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000869 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400870 do_warn = (param[0] == '+');
871 param++;
Cyrill Gorcunov66206e72010-04-20 14:53:44 +0400872
873set_warning:
Cyrill Gorcunov3b8c2972011-12-05 01:39:04 +0400874 for (i = 0; i <= ERR_WARN_MAX; i++) {
875 if (!nasm_stricmp(param, warnings[i].name))
876 break;
877 }
878 if (i <= ERR_WARN_MAX) {
879 warning_on_global[i] = do_warn;
880 } else if (!nasm_stricmp(param, "all")) {
881 for (i = 1; i <= ERR_WARN_MAX; i++)
882 warning_on_global[i] = do_warn;
883 } else if (!nasm_stricmp(param, "none")) {
884 for (i = 1; i <= ERR_WARN_MAX; i++)
885 warning_on_global[i] = !do_warn;
886 } else {
887 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
888 "invalid warning `%s'", param);
889 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000890 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800891
H. Peter Anvine2c80182005-01-15 22:15:51 +0000892 case 'M':
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400893 switch (p[2]) {
894 case 0:
Cyrill Gorcunov3ed32cb2014-06-22 20:53:12 +0400895 operating_mode = OP_DEPEND;
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400896 break;
897 case 'G':
Cyrill Gorcunov3ed32cb2014-06-22 20:53:12 +0400898 operating_mode = OP_DEPEND;
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400899 depend_missing_ok = true;
900 break;
901 case 'P':
902 depend_emit_phony = true;
903 break;
904 case 'D':
Cyrill Gorcunov0dd37af2014-11-29 21:33:44 +0300905 operating_mode = OP_NORMAL;
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400906 depend_file = q;
907 advance = true;
908 break;
909 case 'F':
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400910 depend_file = q;
911 advance = true;
912 break;
913 case 'T':
914 depend_target = q;
915 advance = true;
916 break;
917 case 'Q':
918 depend_target = quote_for_make(q);
919 advance = true;
920 break;
921 default:
922 nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
923 "unknown dependency option `-M%c'", p[2]);
924 break;
925 }
926 if (advance && (!q || !q[0])) {
927 nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
928 "option `-M%c' requires a parameter", p[2]);
929 break;
930 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000931 break;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000932
H. Peter Anvine2c80182005-01-15 22:15:51 +0000933 case '-':
934 {
935 int s;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000936
H. Peter Anvine2c80182005-01-15 22:15:51 +0000937 if (p[2] == 0) { /* -- => stop processing options */
938 stopoptions = 1;
939 break;
940 }
Cyrill Gorcunove7438432014-05-09 22:34:34 +0400941
942 if (!nasm_stricmp(p, "--v"))
943 show_version();
944
H. Peter Anvine2c80182005-01-15 22:15:51 +0000945 for (s = 0; textopts[s].label; s++) {
946 if (!nasm_stricmp(p + 2, textopts[s].label)) {
947 break;
948 }
949 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000950
H. Peter Anvine2c80182005-01-15 22:15:51 +0000951 switch (s) {
952
953 case OPT_PREFIX:
954 case OPT_POSTFIX:
955 {
956 if (!q) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400957 nasm_error(ERR_NONFATAL | ERR_NOFILE |
H. Peter Anvine2c80182005-01-15 22:15:51 +0000958 ERR_USAGE,
959 "option `--%s' requires an argument",
960 p + 2);
961 break;
962 } else {
963 advance = 1, param = q;
964 }
965
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100966 switch (s) {
967 case OPT_PREFIX:
968 strlcpy(lprefix, param, PREFIX_MAX);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000969 break;
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100970 case OPT_POSTFIX:
971 strlcpy(lpostfix, param, POSTFIX_MAX);
972 break;
973 default:
H. Peter Anvin41087062016-03-03 14:27:34 -0800974 nasm_panic(ERR_NOFILE,
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100975 "internal error");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000976 break;
977 }
978 break;
979 }
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800980
H. Peter Anvine2c80182005-01-15 22:15:51 +0000981 default:
982 {
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400983 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000984 "unrecognised option `--%s'", p + 2);
985 break;
986 }
987 }
988 break;
989 }
990
991 default:
992 if (!ofmt->setinfo(GI_SWITCH, &p))
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400993 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000994 "unrecognised option `-%c'", p[1]);
995 break;
996 }
997 } else {
998 if (*inname) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400999 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001000 "more than one input file specified");
H. Peter Anvindc242712007-11-18 11:55:10 -08001001 } else {
1002 copy_filename(inname, p);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001003 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001004 }
1005
1006 return advance;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001007}
1008
H. Peter Anvineba20a72002-04-30 20:53:55 +00001009#define ARG_BUF_DELTA 128
1010
H. Peter Anvine2c80182005-01-15 22:15:51 +00001011static void process_respfile(FILE * rfile)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001012{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001013 char *buffer, *p, *q, *prevarg;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001014 int bufsize, prevargsize;
1015
1016 bufsize = prevargsize = ARG_BUF_DELTA;
1017 buffer = nasm_malloc(ARG_BUF_DELTA);
1018 prevarg = nasm_malloc(ARG_BUF_DELTA);
1019 prevarg[0] = '\0';
1020
H. Peter Anvine2c80182005-01-15 22:15:51 +00001021 while (1) { /* Loop to handle all lines in file */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001022 p = buffer;
1023 while (1) { /* Loop to handle long lines */
1024 q = fgets(p, bufsize - (p - buffer), rfile);
1025 if (!q)
1026 break;
1027 p += strlen(p);
1028 if (p > buffer && p[-1] == '\n')
1029 break;
1030 if (p - buffer > bufsize - 10) {
1031 int offset;
1032 offset = p - buffer;
1033 bufsize += ARG_BUF_DELTA;
1034 buffer = nasm_realloc(buffer, bufsize);
1035 p = buffer + offset;
1036 }
1037 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001038
H. Peter Anvine2c80182005-01-15 22:15:51 +00001039 if (!q && p == buffer) {
1040 if (prevarg[0])
1041 process_arg(prevarg, NULL);
1042 nasm_free(buffer);
1043 nasm_free(prevarg);
1044 return;
1045 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001046
H. Peter Anvine2c80182005-01-15 22:15:51 +00001047 /*
1048 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
1049 * them are present at the end of the line.
1050 */
1051 *(p = &buffer[strcspn(buffer, "\r\n\032")]) = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001052
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001053 while (p > buffer && nasm_isspace(p[-1]))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001054 *--p = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001055
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +04001056 p = nasm_skip_spaces(buffer);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001057
H. Peter Anvine2c80182005-01-15 22:15:51 +00001058 if (process_arg(prevarg, p))
1059 *p = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001060
Charles Crayne192d5b52007-10-18 19:02:42 -07001061 if ((int) strlen(p) > prevargsize - 10) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001062 prevargsize += ARG_BUF_DELTA;
1063 prevarg = nasm_realloc(prevarg, prevargsize);
1064 }
H. Peter Anvindc242712007-11-18 11:55:10 -08001065 strncpy(prevarg, p, prevargsize);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001066 }
1067}
1068
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001069/* Function to process args from a string of args, rather than the
1070 * argv array. Used by the environment variable and response file
1071 * processing.
1072 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001073static void process_args(char *args)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001074{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001075 char *p, *q, *arg, *prevarg;
1076 char separator = ' ';
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001077
1078 p = args;
1079 if (*p && *p != '-')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001080 separator = *p++;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001081 arg = NULL;
1082 while (*p) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001083 q = p;
1084 while (*p && *p != separator)
1085 p++;
1086 while (*p == separator)
1087 *p++ = '\0';
1088 prevarg = arg;
1089 arg = q;
1090 if (process_arg(prevarg, arg))
1091 arg = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001092 }
1093 if (arg)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001094 process_arg(arg, NULL);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001095}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001096
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001097static void process_response_file(const char *file)
1098{
1099 char str[2048];
1100 FILE *f = fopen(file, "r");
1101 if (!f) {
Cyrill Gorcunovf1964512013-02-15 12:14:06 +04001102 perror(file);
1103 exit(-1);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001104 }
1105 while (fgets(str, sizeof str, f)) {
Cyrill Gorcunovf1964512013-02-15 12:14:06 +04001106 process_args(str);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001107 }
1108 fclose(f);
1109}
1110
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001111static void parse_cmdline(int argc, char **argv)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001112{
1113 FILE *rfile;
Cyrill Gorcunovf4941892011-07-17 13:55:25 +04001114 char *envreal, *envcopy = NULL, *p;
H. Peter Anvin972079f2008-09-30 17:01:23 -07001115 int i;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001116
Charles Craynefcce07f2007-09-30 22:15:36 -07001117 *inname = *outname = *listname = *errname = '\0';
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001118
H. Peter Anvin972079f2008-09-30 17:01:23 -07001119 for (i = 0; i <= ERR_WARN_MAX; i++)
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001120 warning_on_global[i] = warnings[i].enabled;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001121
1122 /*
H. Peter Anvinff7ccc02002-05-06 19:41:57 +00001123 * First, process the NASMENV environment variable.
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001124 */
H. Peter Anvinff7ccc02002-05-06 19:41:57 +00001125 envreal = getenv("NASMENV");
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001126 if (envreal) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001127 envcopy = nasm_strdup(envreal);
1128 process_args(envcopy);
1129 nasm_free(envcopy);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001130 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001131
1132 /*
1133 * Now process the actual command line.
1134 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001135 while (--argc) {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001136 bool advance;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001137 argv++;
1138 if (argv[0][0] == '@') {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001139 /*
1140 * We have a response file, so process this as a set of
H. Peter Anvine2c80182005-01-15 22:15:51 +00001141 * arguments like the environment variable. This allows us
1142 * to have multiple arguments on a single line, which is
1143 * different to the -@resp file processing below for regular
1144 * NASM.
1145 */
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001146 process_response_file(argv[0]+1);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001147 argc--;
1148 argv++;
1149 }
1150 if (!stopoptions && argv[0][0] == '-' && argv[0][1] == '@') {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001151 p = get_param(argv[0], argc > 1 ? argv[1] : NULL, &advance);
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001152 if (p) {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001153 rfile = fopen(p, "r");
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001154 if (rfile) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001155 process_respfile(rfile);
1156 fclose(rfile);
1157 } else
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001158 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001159 "unable to open response file `%s'", p);
1160 }
1161 } else
H. Peter Anvin423e3812007-11-15 17:12:29 -08001162 advance = process_arg(argv[0], argc > 1 ? argv[1] : NULL);
1163 argv += advance, argc -= advance;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001164 }
1165
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001166 /*
1167 * Look for basic command line typos. This definitely doesn't
1168 * catch all errors, but it might help cases of fumbled fingers.
1169 */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001170 if (!*inname)
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001171 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001172 "no input file specified");
1173 else if (!strcmp(inname, errname) ||
1174 !strcmp(inname, outname) ||
1175 !strcmp(inname, listname) ||
1176 (depend_file && !strcmp(inname, depend_file)))
H. Peter Anvin41087062016-03-03 14:27:34 -08001177 nasm_fatal(ERR_NOFILE | ERR_USAGE,
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001178 "file `%s' is both input and output file",
1179 inname);
H. Peter Anvin59ddd262007-10-01 11:26:31 -07001180
H. Peter Anvin70653092007-10-19 14:42:29 -07001181 if (*errname) {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001182 error_file = fopen(errname, "w");
1183 if (!error_file) {
1184 error_file = stderr; /* Revert to default! */
H. Peter Anvin41087062016-03-03 14:27:34 -08001185 nasm_fatal(ERR_NOFILE | ERR_USAGE,
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001186 "cannot open file `%s' for error messages",
1187 errname);
1188 }
Charles Craynefcce07f2007-09-30 22:15:36 -07001189 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001190}
1191
H. Peter Anvin70055962007-10-11 00:05:31 -07001192static enum directives getkw(char **directive, char **value);
H. Peter Anvin62b24d72007-08-29 16:38:05 +00001193
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001194static void assemble_file(char *fname, StrList **depend_ptr)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001195{
H. Peter Anvin8bec2c72009-08-08 13:49:00 -07001196 char *directive, *value, *p, *q, *special, *line;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001197 insn output_ins;
H. Peter Anvin70055962007-10-11 00:05:31 -07001198 int i, validid;
1199 bool rn_error;
Charles Crayne5fbbc8c2007-11-07 19:03:46 -08001200 int32_t seg;
1201 int64_t offs;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001202 struct tokenval tokval;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001203 expr *e;
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001204 int pass_max;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001205
Cyrill Gorcunov08359152013-11-09 22:16:11 +04001206 if (cmd_sb == 32 && iflag_ffs(&cmd_cpu) < IF_386)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001207 nasm_fatal(0, "command line: 32-bit segment size requires a higher cpu");
H. Peter Anvineba20a72002-04-30 20:53:55 +00001208
Charles Craynec1905c22008-09-11 18:54:06 -07001209 pass_max = prev_offset_changed = (INT_MAX >> 1) + 2; /* Almost unlimited */
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001210 for (passn = 1; pass0 <= 2; passn++) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001211 int pass1, pass2;
1212 ldfunc def_label;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001213
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001214 pass1 = pass0 == 2 ? 2 : 1; /* 1, 1, 1, ..., 1, 2 */
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001215 pass2 = passn > 1 ? 2 : 1; /* 1, 2, 2, ..., 2, 2 */
1216 /* pass0 0, 0, 0, ..., 1, 2 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00001217
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001218 def_label = passn > 1 ? redefine_label : define_label;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001219
Keith Kaniosb7a89542007-04-12 02:40:54 +00001220 globalbits = sb = cmd_sb; /* set 'bits' to command line default */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001221 cpu = cmd_cpu;
1222 if (pass0 == 2) {
H. Peter Anvin172b8402016-02-18 01:16:18 -08001223 lfmt->init(listname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001224 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001225 in_abs_seg = false;
Charles Craynec1905c22008-09-11 18:54:06 -07001226 global_offset_changed = 0; /* set by redefine_label */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001227 location.segment = ofmt->section(NULL, pass2, &sb);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001228 globalbits = sb;
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001229 if (passn > 1) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001230 saa_rewind(forwrefs);
1231 forwref = saa_rstruct(forwrefs);
1232 raa_free(offsets);
1233 offsets = raa_init();
1234 }
H. Peter Anvin215186f2016-02-17 20:27:41 -08001235 preproc->reset(fname, pass1, pass1 == 2 ? depend_ptr : NULL);
H. Peter Anvin972079f2008-09-30 17:01:23 -07001236 memcpy(warning_on, warning_on_global, (ERR_WARN_MAX+1) * sizeof(bool));
Victor van den Elzen819703a2008-07-16 15:20:56 +02001237
H. Peter Anvine2c80182005-01-15 22:15:51 +00001238 globallineno = 0;
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001239 if (passn == 1)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001240 location.known = true;
Cyrill Gorcunov190232f2013-02-15 12:35:04 +04001241 location.offset = offs = get_curr_offs();
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001242
H. Peter Anvine2c80182005-01-15 22:15:51 +00001243 while ((line = preproc->getline())) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001244 enum directives d;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001245 globallineno++;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001246
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001247 /*
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001248 * Here we parse our directives; this is not handled by the
1249 * 'real' parser. This really should be a separate function.
1250 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001251 directive = line;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001252 d = getkw(&directive, &value);
H. Peter Anvin70055962007-10-11 00:05:31 -07001253 if (d) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001254 int err = 0;
H. Peter Anvin62b24d72007-08-29 16:38:05 +00001255
H. Peter Anvin70055962007-10-11 00:05:31 -07001256 switch (d) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001257 case D_SEGMENT: /* [SEGMENT n] */
1258 case D_SECTION:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001259 seg = ofmt->section(value, pass2, &sb);
1260 if (seg == NO_SEG) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001261 nasm_error(pass1 == 1 ? ERR_NONFATAL : ERR_PANIC,
Keith Kaniosb7a89542007-04-12 02:40:54 +00001262 "segment name `%s' not recognized",
H. Peter Anvine2c80182005-01-15 22:15:51 +00001263 value);
1264 } else {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001265 in_abs_seg = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001266 location.segment = seg;
1267 }
1268 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001269 case D_SECTALIGN: /* [SECTALIGN n] */
Cyrill Gorcunov9fde3352011-05-23 23:50:03 +04001270 if (*value) {
1271 stdscan_reset();
1272 stdscan_set(value);
1273 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin215186f2016-02-17 20:27:41 -08001274 e = evaluate(stdscan, NULL, &tokval, NULL, pass2, NULL);
Cyrill Gorcunov9fde3352011-05-23 23:50:03 +04001275 if (e) {
1276 unsigned int align = (unsigned int)e->value;
1277 if ((uint64_t)e->value > 0x7fffffff) {
1278 /*
1279 * FIXME: Please make some sane message here
1280 * ofmt should have some 'check' method which
1281 * would report segment alignment bounds.
1282 */
H. Peter Anvin41087062016-03-03 14:27:34 -08001283 nasm_fatal(0,
Cyrill Gorcunov9fde3352011-05-23 23:50:03 +04001284 "incorrect segment alignment `%s'", value);
1285 } else if (!is_power2(align)) {
Cyrill Gorcunovd5f2aef2010-04-20 15:33:45 +04001286 nasm_error(ERR_NONFATAL,
1287 "segment alignment `%s' is not power of two",
1288 value);
1289 }
H. Peter Anvin2c4a4d52016-02-16 17:37:18 -08001290
Cyrill Gorcunov2a587ab2010-04-21 00:51:22 +04001291 /* callee should be able to handle all details */
H. Peter Anvin2c4a4d52016-02-16 17:37:18 -08001292 if (location.segment != NO_SEG)
1293 ofmt->sectalign(location.segment, align);
Cyrill Gorcunovd5f2aef2010-04-20 15:33:45 +04001294 }
1295 }
1296 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001297 case D_EXTERN: /* [EXTERN label:special] */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001298 if (*value == '$')
1299 value++; /* skip initial $ if present */
1300 if (pass0 == 2) {
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001301 q = value;
1302 while (*q && *q != ':')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001303 q++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001304 if (*q == ':') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001305 *q++ = '\0';
1306 ofmt->symdef(value, 0L, 0L, 3, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001307 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001308 } else if (passn == 1) {
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001309 q = value;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001310 validid = true;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001311 if (!isidstart(*q))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001312 validid = false;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001313 while (*q && *q != ':') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001314 if (!isidchar(*q))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001315 validid = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001316 q++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001317 }
1318 if (!validid) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001319 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001320 "identifier expected after EXTERN");
1321 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001322 }
1323 if (*q == ':') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001324 *q++ = '\0';
1325 special = q;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001326 } else
H. Peter Anvine2c80182005-01-15 22:15:51 +00001327 special = NULL;
1328 if (!is_extern(value)) { /* allow re-EXTERN to be ignored */
1329 int temp = pass0;
1330 pass0 = 1; /* fake pass 1 in labels.c */
H. Peter Anvin605f5152009-07-18 18:31:41 -07001331 declare_as_global(value, special);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001332 define_label(value, seg_alloc(), 0L, NULL,
H. Peter Anvin605f5152009-07-18 18:31:41 -07001333 false, true);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001334 pass0 = temp;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001335 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001336 } /* else pass0 == 1 */
1337 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001338 case D_BITS: /* [BITS bits] */
Keith Kaniosb7a89542007-04-12 02:40:54 +00001339 globalbits = sb = get_bits(value);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001340 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001341 case D_GLOBAL: /* [GLOBAL symbol:special] */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001342 if (*value == '$')
1343 value++; /* skip initial $ if present */
1344 if (pass0 == 2) { /* pass 2 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001345 q = value;
1346 while (*q && *q != ':')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001347 q++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001348 if (*q == ':') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001349 *q++ = '\0';
1350 ofmt->symdef(value, 0L, 0L, 3, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001351 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001352 } else if (pass2 == 1) { /* pass == 1 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001353 q = value;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001354 validid = true;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001355 if (!isidstart(*q))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001356 validid = false;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001357 while (*q && *q != ':') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001358 if (!isidchar(*q))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001359 validid = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001360 q++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001361 }
1362 if (!validid) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001363 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001364 "identifier expected after GLOBAL");
1365 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001366 }
1367 if (*q == ':') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001368 *q++ = '\0';
1369 special = q;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001370 } else
H. Peter Anvine2c80182005-01-15 22:15:51 +00001371 special = NULL;
H. Peter Anvin605f5152009-07-18 18:31:41 -07001372 declare_as_global(value, special);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001373 } /* pass == 1 */
1374 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001375 case D_COMMON: /* [COMMON symbol size:special] */
1376 {
1377 int64_t size;
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001378
H. Peter Anvine2c80182005-01-15 22:15:51 +00001379 if (*value == '$')
1380 value++; /* skip initial $ if present */
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001381 p = value;
1382 validid = true;
1383 if (!isidstart(*p))
1384 validid = false;
1385 while (*p && !nasm_isspace(*p)) {
1386 if (!isidchar(*p))
1387 validid = false;
1388 p++;
1389 }
1390 if (!validid) {
1391 nasm_error(ERR_NONFATAL,
1392 "identifier expected after COMMON");
1393 break;
1394 }
1395 if (*p) {
H. Peter Anvinff800412009-10-13 12:03:37 -07001396 p = nasm_zap_spaces_fwd(p);
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001397 q = p;
1398 while (*q && *q != ':')
1399 q++;
1400 if (*q == ':') {
1401 *q++ = '\0';
1402 special = q;
1403 } else {
1404 special = NULL;
1405 }
1406 size = readnum(p, &rn_error);
1407 if (rn_error) {
1408 nasm_error(ERR_NONFATAL,
1409 "invalid size specified"
1410 " in COMMON declaration");
1411 break;
1412 }
1413 } else {
1414 nasm_error(ERR_NONFATAL,
1415 "no size specified in"
1416 " COMMON declaration");
1417 break;
1418 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001419
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001420 if (pass0 < 2) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001421 define_common(value, seg_alloc(), size, special);
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001422 } else if (pass0 == 2) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001423 if (special)
1424 ofmt->symdef(value, 0L, 0L, 3, special);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001425 }
1426 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001427 }
1428 case D_ABSOLUTE: /* [ABSOLUTE address] */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001429 stdscan_reset();
Cyrill Gorcunov917117f2009-10-29 23:09:18 +03001430 stdscan_set(value);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001431 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin215186f2016-02-17 20:27:41 -08001432 e = evaluate(stdscan, NULL, &tokval, NULL, pass2, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001433 if (e) {
1434 if (!is_reloc(e))
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001435 nasm_error(pass0 ==
H. Peter Anvine2c80182005-01-15 22:15:51 +00001436 1 ? ERR_NONFATAL : ERR_PANIC,
1437 "cannot use non-relocatable expression as "
1438 "ABSOLUTE address");
1439 else {
1440 abs_seg = reloc_seg(e);
1441 abs_offset = reloc_value(e);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001442 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001443 } else if (passn == 1)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001444 abs_offset = 0x100; /* don't go near zero in case of / */
1445 else
H. Peter Anvin41087062016-03-03 14:27:34 -08001446 nasm_panic(0, "invalid ABSOLUTE address "
H. Peter Anvine2c80182005-01-15 22:15:51 +00001447 "in pass two");
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001448 in_abs_seg = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001449 location.segment = NO_SEG;
1450 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001451 case D_DEBUG: /* [DEBUG] */
1452 {
1453 char debugid[128];
1454 bool badid, overlong;
H. Peter Anvin8bec2c72009-08-08 13:49:00 -07001455
H. Peter Anvine2c80182005-01-15 22:15:51 +00001456 p = value;
1457 q = debugid;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001458 badid = overlong = false;
H. Peter Anvin8bec2c72009-08-08 13:49:00 -07001459 if (!isidstart(*p)) {
1460 badid = true;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001461 } else {
1462 while (*p && !nasm_isspace(*p)) {
1463 if (q >= debugid + sizeof debugid - 1) {
1464 overlong = true;
1465 break;
1466 }
1467 if (!isidchar(*p))
1468 badid = true;
1469 *q++ = *p++;
1470 }
1471 *q = 0;
1472 }
H. Peter Anvin8bec2c72009-08-08 13:49:00 -07001473 if (badid) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001474 nasm_error(passn == 1 ? ERR_NONFATAL : ERR_PANIC,
1475 "identifier expected after DEBUG");
1476 break;
1477 }
1478 if (overlong) {
1479 nasm_error(passn == 1 ? ERR_NONFATAL : ERR_PANIC,
1480 "DEBUG identifier too long");
1481 break;
1482 }
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +04001483 p = nasm_skip_spaces(p);
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001484 if (pass0 == 2)
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001485 dfmt->debug_directive(debugid, p);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001486 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001487 }
1488 case D_WARNING: /* [WARNING {+|-|*}warn-name] */
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +04001489 value = nasm_skip_spaces(value);
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001490 switch(*value) {
1491 case '-': validid = 0; value++; break;
1492 case '+': validid = 1; value++; break;
1493 case '*': validid = 2; value++; break;
1494 default: validid = 1; break;
1495 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001496
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001497 for (i = 1; i <= ERR_WARN_MAX; i++)
1498 if (!nasm_stricmp(value, warnings[i].name))
1499 break;
1500 if (i <= ERR_WARN_MAX) {
1501 switch(validid) {
1502 case 0:
1503 warning_on[i] = false;
1504 break;
1505 case 1:
1506 warning_on[i] = true;
1507 break;
1508 case 2:
1509 warning_on[i] = warning_on_global[i];
1510 break;
1511 }
1512 } else
1513 nasm_error(ERR_NONFATAL,
1514 "invalid warning id in WARNING directive");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001515 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001516 case D_CPU: /* [CPU] */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001517 cpu = get_cpu(value);
1518 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001519 case D_LIST: /* [LIST {+|-}] */
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +04001520 value = nasm_skip_spaces(value);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001521 if (*value == '+') {
1522 user_nolist = 0;
1523 } else {
1524 if (*value == '-') {
1525 user_nolist = 1;
1526 } else {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001527 err = 1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001528 }
1529 }
1530 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001531 case D_DEFAULT: /* [DEFAULT] */
1532 stdscan_reset();
Cyrill Gorcunov917117f2009-10-29 23:09:18 +03001533 stdscan_set(value);
H. Peter Anvin62b24d72007-08-29 16:38:05 +00001534 tokval.t_type = TOKEN_INVALID;
Jin Kyu Songb287ff02013-12-04 20:05:55 -08001535 if (stdscan(NULL, &tokval) != TOKEN_INVALID) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001536 switch ((int)tokval.t_integer) {
1537 case S_REL:
1538 globalrel = 1;
1539 break;
1540 case S_ABS:
1541 globalrel = 0;
1542 break;
Jin Kyu Songb287ff02013-12-04 20:05:55 -08001543 case P_BND:
1544 globalbnd = 1;
1545 break;
1546 case P_NOBND:
1547 globalbnd = 0;
1548 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001549 default:
1550 err = 1;
1551 break;
1552 }
1553 } else {
1554 err = 1;
1555 }
1556 break;
1557 case D_FLOAT:
1558 if (float_option(value)) {
1559 nasm_error(pass1 == 1 ? ERR_NONFATAL : ERR_PANIC,
1560 "unknown 'float' directive: %s",
1561 value);
1562 }
1563 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001564 default:
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001565 if (ofmt->directive(d, value, pass2))
1566 break;
1567 /* else fall through */
1568 case D_unknown:
1569 nasm_error(pass1 == 1 ? ERR_NONFATAL : ERR_PANIC,
1570 "unrecognised directive [%s]",
1571 directive);
1572 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001573 }
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001574 if (err) {
1575 nasm_error(ERR_NONFATAL,
1576 "invalid parameter to [%s] directive",
1577 directive);
1578 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001579 } else { /* it isn't a directive */
H. Peter Anvin00444ae2009-07-18 18:49:55 -07001580 parse_line(pass1, line, &output_ins, def_label);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001581
Charles Crayne2581c862008-09-10 19:21:52 -07001582 if (optimizing > 0) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001583 if (forwref != NULL && globallineno == forwref->lineno) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001584 output_ins.forw_ref = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001585 do {
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001586 output_ins.oprs[forwref->operand].opflags |= OPFLAG_FORWARD;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001587 forwref = saa_rstruct(forwrefs);
1588 } while (forwref != NULL
1589 && forwref->lineno == globallineno);
1590 } else
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001591 output_ins.forw_ref = false;
H. Peter Anvin323fcff2009-07-12 12:04:56 -07001592
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001593 if (output_ins.forw_ref) {
1594 if (passn == 1) {
1595 for (i = 0; i < output_ins.operands; i++) {
1596 if (output_ins.oprs[i].opflags & OPFLAG_FORWARD) {
1597 struct forwrefinfo *fwinf = (struct forwrefinfo *)saa_wstruct(forwrefs);
1598 fwinf->lineno = globallineno;
1599 fwinf->operand = i;
1600 }
1601 }
1602 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001603 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001604 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001605
H. Peter Anvine2c80182005-01-15 22:15:51 +00001606 /* forw_ref */
1607 if (output_ins.opcode == I_EQU) {
1608 if (pass1 == 1) {
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001609 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00001610 * Special `..' EQUs get processed in pass two,
1611 * except `..@' macro-processor EQUs which are done
1612 * in the normal place.
1613 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001614 if (!output_ins.label)
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001615 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001616 "EQU not preceded by label");
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001617
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001618 else if (output_ins.label[0] != '.' ||
1619 output_ins.label[1] != '.' ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00001620 output_ins.label[2] == '@') {
1621 if (output_ins.operands == 1 &&
1622 (output_ins.oprs[0].type & IMMEDIATE) &&
1623 output_ins.oprs[0].wrt == NO_SEG) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001624 bool isext = !!(output_ins.oprs[0].opflags & OPFLAG_EXTERN);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001625 def_label(output_ins.label,
1626 output_ins.oprs[0].segment,
1627 output_ins.oprs[0].offset, NULL,
H. Peter Anvin605f5152009-07-18 18:31:41 -07001628 false, isext);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001629 } else if (output_ins.operands == 2
H. Peter Anvin72191982009-02-26 14:34:48 -08001630 && (output_ins.oprs[0].type & IMMEDIATE)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001631 && (output_ins.oprs[0].type & COLON)
H. Peter Anvin72191982009-02-26 14:34:48 -08001632 && output_ins.oprs[0].segment == NO_SEG
H. Peter Anvine2c80182005-01-15 22:15:51 +00001633 && output_ins.oprs[0].wrt == NO_SEG
H. Peter Anvin72191982009-02-26 14:34:48 -08001634 && (output_ins.oprs[1].type & IMMEDIATE)
1635 && output_ins.oprs[1].segment == NO_SEG
1636 && output_ins.oprs[1].wrt == NO_SEG) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001637 def_label(output_ins.label,
H. Peter Anvin72191982009-02-26 14:34:48 -08001638 output_ins.oprs[0].offset | SEG_ABS,
1639 output_ins.oprs[1].offset,
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001640 NULL, false, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001641 } else
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001642 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001643 "bad syntax for EQU");
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001644 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001645 } else {
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001646 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00001647 * Special `..' EQUs get processed here, except
1648 * `..@' macro processor EQUs which are done above.
1649 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001650 if (output_ins.label[0] == '.' &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00001651 output_ins.label[1] == '.' &&
1652 output_ins.label[2] != '@') {
1653 if (output_ins.operands == 1 &&
1654 (output_ins.oprs[0].type & IMMEDIATE)) {
1655 define_label(output_ins.label,
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001656 output_ins.oprs[0].segment,
1657 output_ins.oprs[0].offset,
H. Peter Anvin605f5152009-07-18 18:31:41 -07001658 NULL, false, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001659 } else if (output_ins.operands == 2
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001660 && (output_ins.oprs[0].type & IMMEDIATE)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001661 && (output_ins.oprs[0].type & COLON)
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001662 && output_ins.oprs[0].segment == NO_SEG
1663 && (output_ins.oprs[1].type & IMMEDIATE)
1664 && output_ins.oprs[1].segment == NO_SEG) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001665 define_label(output_ins.label,
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001666 output_ins.oprs[0].offset | SEG_ABS,
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001667 output_ins.oprs[1].offset,
H. Peter Anvin605f5152009-07-18 18:31:41 -07001668 NULL, false, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001669 } else
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001670 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001671 "bad syntax for EQU");
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001672 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001673 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001674 } else { /* instruction isn't an EQU */
H. Peter Anvineba20a72002-04-30 20:53:55 +00001675
H. Peter Anvine2c80182005-01-15 22:15:51 +00001676 if (pass1 == 1) {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001677
Charles Crayne5fbbc8c2007-11-07 19:03:46 -08001678 int64_t l = insn_size(location.segment, offs, sb, cpu,
H. Peter Anvin215186f2016-02-17 20:27:41 -08001679 &output_ins);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001680
H. Peter Anvine2c80182005-01-15 22:15:51 +00001681 /* if (using_debug_info) && output_ins.opcode != -1) */
1682 if (using_debug_info)
1683 { /* fbk 03/25/01 */
1684 /* this is done here so we can do debug type info */
Keith Kaniosb7a89542007-04-12 02:40:54 +00001685 int32_t typeinfo =
H. Peter Anvine2c80182005-01-15 22:15:51 +00001686 TYS_ELEMENTS(output_ins.operands);
1687 switch (output_ins.opcode) {
1688 case I_RESB:
1689 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001690 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_BYTE;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001691 break;
1692 case I_RESW:
1693 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001694 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_WORD;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001695 break;
1696 case I_RESD:
1697 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001698 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_DWORD;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001699 break;
1700 case I_RESQ:
1701 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001702 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_QWORD;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001703 break;
1704 case I_REST:
1705 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001706 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_TBYTE;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001707 break;
H. Peter Anvindfb91802008-05-20 11:43:53 -07001708 case I_RESO:
1709 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001710 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_OWORD;
H. Peter Anvindfb91802008-05-20 11:43:53 -07001711 break;
1712 case I_RESY:
1713 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001714 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_YWORD;
H. Peter Anvindfb91802008-05-20 11:43:53 -07001715 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001716 case I_DB:
1717 typeinfo |= TY_BYTE;
1718 break;
1719 case I_DW:
1720 typeinfo |= TY_WORD;
1721 break;
1722 case I_DD:
1723 if (output_ins.eops_float)
1724 typeinfo |= TY_FLOAT;
1725 else
1726 typeinfo |= TY_DWORD;
1727 break;
1728 case I_DQ:
1729 typeinfo |= TY_QWORD;
1730 break;
1731 case I_DT:
1732 typeinfo |= TY_TBYTE;
1733 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001734 case I_DO:
1735 typeinfo |= TY_OWORD;
1736 break;
1737 case I_DY:
1738 typeinfo |= TY_YWORD;
1739 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001740 default:
1741 typeinfo = TY_LABEL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001742
H. Peter Anvine2c80182005-01-15 22:15:51 +00001743 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001744
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001745 dfmt->debug_typevalue(typeinfo);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001746 }
1747 if (l != -1) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001748 offs += l;
Cyrill Gorcunov190232f2013-02-15 12:35:04 +04001749 set_curr_offs(offs);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001750 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00001751 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00001752 * else l == -1 => invalid instruction, which will be
1753 * flagged as an error on pass 2
1754 */
H. Peter Anvin76690a12002-04-30 20:52:49 +00001755
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001756 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001757 offs += assemble(location.segment, offs, sb, cpu,
H. Peter Anvin215186f2016-02-17 20:27:41 -08001758 &output_ins);
Cyrill Gorcunov190232f2013-02-15 12:35:04 +04001759 set_curr_offs(offs);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001760
H. Peter Anvine2c80182005-01-15 22:15:51 +00001761 }
1762 } /* not an EQU */
1763 cleanup_insn(&output_ins);
1764 }
1765 nasm_free(line);
Cyrill Gorcunov190232f2013-02-15 12:35:04 +04001766 location.offset = offs = get_curr_offs();
H. Peter Anvine2c80182005-01-15 22:15:51 +00001767 } /* end while (line = preproc->getline... */
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001768
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001769 if (pass0 == 2 && global_offset_changed && !terminate_after_phase)
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001770 nasm_error(ERR_NONFATAL,
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001771 "phase error detected at end of assembly.");
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001772
H. Peter Anvine2c80182005-01-15 22:15:51 +00001773 if (pass1 == 1)
1774 preproc->cleanup(1);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001775
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001776 if ((passn > 1 && !global_offset_changed) || pass0 == 2) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001777 pass0++;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001778 } else if (global_offset_changed &&
1779 global_offset_changed < prev_offset_changed) {
Charles Craynec1905c22008-09-11 18:54:06 -07001780 prev_offset_changed = global_offset_changed;
1781 stall_count = 0;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001782 } else {
1783 stall_count++;
1784 }
Victor van den Elzen42528232008-09-11 15:07:05 +02001785
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001786 if (terminate_after_phase)
1787 break;
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001788
1789 if ((stall_count > 997) || (passn >= pass_max)) {
Victor van den Elzen42528232008-09-11 15:07:05 +02001790 /* We get here if the labels don't converge
1791 * Example: FOO equ FOO + 1
1792 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001793 nasm_error(ERR_NONFATAL,
Victor van den Elzen42528232008-09-11 15:07:05 +02001794 "Can't find valid values for all labels "
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001795 "after %d passes, giving up.", passn);
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001796 nasm_error(ERR_NONFATAL,
1797 "Possible causes: recursive EQUs, macro abuse.");
1798 break;
1799 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001800 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001801
H. Peter Anvine2c80182005-01-15 22:15:51 +00001802 preproc->cleanup(0);
H. Peter Anvin172b8402016-02-18 01:16:18 -08001803 lfmt->cleanup();
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001804 if (!terminate_after_phase && opt_verbose_info) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001805 /* -On and -Ov switches */
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001806 fprintf(stdout, "info: assembly required 1+%d+1 passes\n", passn-3);
1807 }
1808}
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001809
H. Peter Anvin70055962007-10-11 00:05:31 -07001810static enum directives getkw(char **directive, char **value)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001811{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001812 char *p, *q, *buf;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001813
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001814 buf = nasm_skip_spaces(*directive);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001815
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001816 /* it should be enclosed in [ ] */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001817 if (*buf != '[')
H. Peter Anvin888964a2010-04-06 17:00:12 -07001818 return D_none;
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001819 q = strchr(buf, ']');
1820 if (!q)
H. Peter Anvin888964a2010-04-06 17:00:12 -07001821 return D_none;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001822
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001823 /* stip off the comments */
1824 p = strchr(buf, ';');
1825 if (p) {
1826 if (p < q) /* ouch! somwhere inside */
H. Peter Anvin888964a2010-04-06 17:00:12 -07001827 return D_none;
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001828 *p = '\0';
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001829 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001830
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001831 /* no brace, no trailing spaces */
1832 *q = '\0';
1833 nasm_zap_spaces_rev(--q);
1834
1835 /* directive */
1836 p = nasm_skip_spaces(++buf);
1837 q = nasm_skip_word(p);
1838 if (!q)
H. Peter Anvin888964a2010-04-06 17:00:12 -07001839 return D_none; /* sigh... no value there */
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001840 *q = '\0';
1841 *directive = p;
1842
1843 /* and value finally */
1844 p = nasm_skip_spaces(++q);
1845 *value = p;
H. Peter Anvin62b24d72007-08-29 16:38:05 +00001846
H. Peter Anvin323fcff2009-07-12 12:04:56 -07001847 return find_directive(*directive);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001848}
1849
Ed Berosetfa771012002-06-09 20:56:40 +00001850/**
1851 * gnu style error reporting
1852 * This function prints an error message to error_file in the
1853 * style used by GNU. An example would be:
1854 * file.asm:50: error: blah blah blah
H. Peter Anvin70653092007-10-19 14:42:29 -07001855 * where file.asm is the name of the file, 50 is the line number on
Ed Berosetfa771012002-06-09 20:56:40 +00001856 * which the error occurs (or is detected) and "error:" is one of
1857 * the possible optional diagnostics -- it can be "error" or "warning"
H. Peter Anvin70653092007-10-19 14:42:29 -07001858 * or something else. Finally the line terminates with the actual
Ed Berosetfa771012002-06-09 20:56:40 +00001859 * error message.
H. Peter Anvin70653092007-10-19 14:42:29 -07001860 *
1861 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001862 * @param fmt the printf style format string
1863 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001864static void nasm_verror_gnu(int severity, const char *fmt, va_list ap)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001865{
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001866 char *currentfile = NULL;
1867 int32_t lineno = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001868
Ed Berosetfa771012002-06-09 20:56:40 +00001869 if (is_suppressed_warning(severity))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001870 return;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001871
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001872 if (!(severity & ERR_NOFILE))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001873 src_get(&lineno, &currentfile);
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001874
H. Peter Anvinbd464742016-02-17 20:47:01 -08001875 if (!skip_this_pass(severity)) {
1876 if (currentfile) {
1877 fprintf(error_file, "%s:%"PRId32": ", currentfile, lineno);
1878 nasm_free(currentfile);
1879 } else {
1880 fputs("nasm: ", error_file);
1881 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001882 }
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001883
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001884 nasm_verror_common(severity, fmt, ap);
Ed Berosetfa771012002-06-09 20:56:40 +00001885}
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001886
Ed Berosetfa771012002-06-09 20:56:40 +00001887/**
1888 * MS style error reporting
1889 * This function prints an error message to error_file in the
H. Peter Anvin70653092007-10-19 14:42:29 -07001890 * style used by Visual C and some other Microsoft tools. An example
Ed Berosetfa771012002-06-09 20:56:40 +00001891 * would be:
Ed Beroset6e61d0d2002-06-11 03:29:36 +00001892 * file.asm(50) : error: blah blah blah
H. Peter Anvin70653092007-10-19 14:42:29 -07001893 * where file.asm is the name of the file, 50 is the line number on
Ed Beroset6e61d0d2002-06-11 03:29:36 +00001894 * which the error occurs (or is detected) and "error:" is one of
1895 * the possible optional diagnostics -- it can be "error" or "warning"
H. Peter Anvin70653092007-10-19 14:42:29 -07001896 * or something else. Finally the line terminates with the actual
Ed Beroset6e61d0d2002-06-11 03:29:36 +00001897 * error message.
H. Peter Anvin70653092007-10-19 14:42:29 -07001898 *
1899 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001900 * @param fmt the printf style format string
1901 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001902static void nasm_verror_vc(int severity, const char *fmt, va_list ap)
Ed Berosetfa771012002-06-09 20:56:40 +00001903{
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001904 char *currentfile = NULL;
1905 int32_t lineno = 0;
Ed Berosetfa771012002-06-09 20:56:40 +00001906
H. Peter Anvine2c80182005-01-15 22:15:51 +00001907 if (is_suppressed_warning(severity))
1908 return;
Ed Berosetfa771012002-06-09 20:56:40 +00001909
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001910 if (!(severity & ERR_NOFILE))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001911 src_get(&lineno, &currentfile);
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001912
H. Peter Anvinbd464742016-02-17 20:47:01 -08001913 if (!skip_this_pass(severity)) {
1914 if (currentfile) {
1915 fprintf(error_file, "%s(%"PRId32") : ", currentfile, lineno);
1916 nasm_free(currentfile);
1917 } else {
1918 fputs("nasm: ", error_file);
1919 }
Ed Berosetfa771012002-06-09 20:56:40 +00001920 }
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001921
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001922 nasm_verror_common(severity, fmt, ap);
Ed Berosetfa771012002-06-09 20:56:40 +00001923}
1924
1925/**
1926 * check for supressed warning
H. Peter Anvin70653092007-10-19 14:42:29 -07001927 * checks for suppressed warning or pass one only warning and we're
Ed Berosetfa771012002-06-09 20:56:40 +00001928 * not in pass 1
1929 *
1930 * @param severity the severity of the warning or error
1931 * @return true if we should abort error/warning printing
1932 */
H. Peter Anvin2b046cf2008-01-22 14:08:36 -08001933static bool is_suppressed_warning(int severity)
Ed Berosetfa771012002-06-09 20:56:40 +00001934{
Cyrill Gorcunovead87722011-12-04 19:24:25 +04001935 /* Not a warning at all */
1936 if ((severity & ERR_MASK) != ERR_WARNING)
1937 return false;
1938
H. Peter Anvin442a05a2012-02-24 21:50:53 -08001939 /* Might be a warning but suppresed explicitly */
1940 if (severity & ERR_WARN_MASK)
1941 return !warning_on[WARN_IDX(severity)];
1942 else
1943 return false;
Ed Berosetfa771012002-06-09 20:56:40 +00001944}
1945
H. Peter Anvinbd464742016-02-17 20:47:01 -08001946static bool skip_this_pass(int severity)
1947{
1948 /* See if it's a pass-one only warning and we're not in pass one. */
1949 if ((severity & ERR_MASK) > ERR_WARNING)
1950 return false;
1951
1952 if (((severity & ERR_PASS1) && pass0 != 1) ||
1953 ((severity & ERR_PASS2) && pass0 != 2))
1954 return true;
1955
1956 return false;
1957}
1958
Ed Berosetfa771012002-06-09 20:56:40 +00001959/**
1960 * common error reporting
1961 * This is the common back end of the error reporting schemes currently
H. Peter Anvin70653092007-10-19 14:42:29 -07001962 * implemented. It prints the nature of the warning and then the
Ed Berosetfa771012002-06-09 20:56:40 +00001963 * specific error message to error_file and may or may not return. It
1964 * doesn't return if the error severity is a "panic" or "debug" type.
H. Peter Anvin70653092007-10-19 14:42:29 -07001965 *
1966 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001967 * @param fmt the printf style format string
1968 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001969static void nasm_verror_common(int severity, const char *fmt, va_list args)
Ed Berosetfa771012002-06-09 20:56:40 +00001970{
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001971 char msg[1024];
1972 const char *pfx;
H. Peter Anvin323fcff2009-07-12 12:04:56 -07001973
H. Peter Anvin7df04172008-06-10 18:27:38 -07001974 switch (severity & (ERR_MASK|ERR_NO_SEVERITY)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001975 case ERR_WARNING:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001976 pfx = "warning: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001977 break;
1978 case ERR_NONFATAL:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001979 pfx = "error: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001980 break;
1981 case ERR_FATAL:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001982 pfx = "fatal: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001983 break;
1984 case ERR_PANIC:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001985 pfx = "panic: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001986 break;
1987 case ERR_DEBUG:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001988 pfx = "debug: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001989 break;
H. Peter Anvin7df04172008-06-10 18:27:38 -07001990 default:
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001991 pfx = "";
1992 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001993 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00001994
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001995 vsnprintf(msg, sizeof msg, fmt, args);
1996
H. Peter Anvinbd464742016-02-17 20:47:01 -08001997 if (!skip_this_pass(severity))
1998 fprintf(error_file, "%s%s\n", pfx, msg);
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001999
H. Peter Anvinbd464742016-02-17 20:47:01 -08002000 /*
2001 * Don't suppress this with skip_this_pass(), or we don't get
2002 * preprocessor warnings in the list file
2003 */
2004 if ((severity & ERR_MASK) >= ERR_WARNING)
H. Peter Anvin172b8402016-02-18 01:16:18 -08002005 lfmt->error(severity, pfx, msg);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002006
2007 if (severity & ERR_USAGE)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002008 want_usage = true;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002009
2010 switch (severity & ERR_MASK) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002011 case ERR_DEBUG:
2012 /* no further action, by definition */
2013 break;
H. Peter Anvinb030c922007-11-13 11:31:15 -08002014 case ERR_WARNING:
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04002015 /* Treat warnings as errors */
2016 if (warning_on[WARN_IDX(ERR_WARN_TERM)])
2017 terminate_after_phase = true;
2018 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002019 case ERR_NONFATAL:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002020 terminate_after_phase = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002021 break;
2022 case ERR_FATAL:
2023 if (ofile) {
2024 fclose(ofile);
2025 remove(outname);
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04002026 ofile = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002027 }
2028 if (want_usage)
2029 usage();
2030 exit(1); /* instantly die */
2031 break; /* placate silly compilers */
2032 case ERR_PANIC:
2033 fflush(NULL);
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04002034 /* abort(); */ /* halt, catch fire, and dump core */
H. Peter Anvinbd464742016-02-17 20:47:01 -08002035 if (ofile) {
2036 fclose(ofile);
2037 remove(outname);
2038 ofile = NULL;
2039 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002040 exit(3);
2041 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002042 }
2043}
2044
H. Peter Anvin734b1882002-04-30 21:01:08 +00002045static void usage(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002046{
H. Peter Anvin620515a2002-04-30 20:57:38 +00002047 fputs("type `nasm -h' for help\n", error_file);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002048}
2049
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002050static iflag_t get_cpu(char *value)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002051{
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002052 iflag_t r;
2053
2054 iflag_clear_all(&r);
2055
H. Peter Anvine2c80182005-01-15 22:15:51 +00002056 if (!strcmp(value, "8086"))
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002057 iflag_set(&r, IF_8086);
2058 else if (!strcmp(value, "186"))
2059 iflag_set(&r, IF_186);
2060 else if (!strcmp(value, "286"))
2061 iflag_set(&r, IF_286);
2062 else if (!strcmp(value, "386"))
2063 iflag_set(&r, IF_386);
2064 else if (!strcmp(value, "486"))
2065 iflag_set(&r, IF_486);
2066 else if (!strcmp(value, "586") ||
2067 !nasm_stricmp(value, "pentium"))
2068 iflag_set(&r, IF_PENT);
2069 else if (!strcmp(value, "686") ||
2070 !nasm_stricmp(value, "ppro") ||
2071 !nasm_stricmp(value, "pentiumpro") ||
2072 !nasm_stricmp(value, "p2"))
2073 iflag_set(&r, IF_P6);
2074 else if (!nasm_stricmp(value, "p3") ||
2075 !nasm_stricmp(value, "katmai"))
2076 iflag_set(&r, IF_KATMAI);
2077 else if (!nasm_stricmp(value, "p4") || /* is this right? -- jrc */
2078 !nasm_stricmp(value, "willamette"))
2079 iflag_set(&r, IF_WILLAMETTE);
2080 else if (!nasm_stricmp(value, "prescott"))
2081 iflag_set(&r, IF_PRESCOTT);
2082 else if (!nasm_stricmp(value, "x64") ||
2083 !nasm_stricmp(value, "x86-64"))
2084 iflag_set(&r, IF_X86_64);
2085 else if (!nasm_stricmp(value, "ia64") ||
2086 !nasm_stricmp(value, "ia-64") ||
2087 !nasm_stricmp(value, "itanium")||
2088 !nasm_stricmp(value, "itanic") ||
2089 !nasm_stricmp(value, "merced"))
2090 iflag_set(&r, IF_IA64);
2091 else {
2092 iflag_set(&r, IF_PLEVEL);
2093 nasm_error(pass0 < 2 ? ERR_NONFATAL : ERR_FATAL,
2094 "unknown 'cpu' type");
2095 }
2096 return r;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002097}
2098
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002099static int get_bits(char *value)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002100{
2101 int i;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002102
H. Peter Anvine2c80182005-01-15 22:15:51 +00002103 if ((i = atoi(value)) == 16)
2104 return i; /* set for a 16-bit segment */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002105 else if (i == 32) {
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002106 if (iflag_ffs(&cpu) < IF_386) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04002107 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002108 "cannot specify 32-bit segment on processor below a 386");
2109 i = 16;
2110 }
Keith Kaniosb7a89542007-04-12 02:40:54 +00002111 } else if (i == 64) {
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002112 if (iflag_ffs(&cpu) < IF_X86_64) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04002113 nasm_error(ERR_NONFATAL,
Keith Kaniosb7a89542007-04-12 02:40:54 +00002114 "cannot specify 64-bit segment on processor below an x86-64");
2115 i = 16;
2116 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002117 } else {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04002118 nasm_error(pass0 < 2 ? ERR_NONFATAL : ERR_FATAL,
Keith Kaniosb7a89542007-04-12 02:40:54 +00002119 "`%s' is not a valid segment size; must be 16, 32 or 64",
H. Peter Anvine2c80182005-01-15 22:15:51 +00002120 value);
2121 i = 16;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002122 }
2123 return i;
2124}