blob: 2fe2a0bcbabee022bec064062f22b523fa2721e7 [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 Anvin2bc0ab32016-03-08 02:17:36 -080064#include "ver.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000065
H. Peter Anvin31387b22010-07-15 18:28:52 -070066/*
67 * This is the maximum number of optimization passes to do. If we ever
68 * find a case where the optimizer doesn't naturally converge, we might
69 * have to drop this value so the assembler doesn't appear to just hang.
70 */
71#define MAX_OPTIMIZE (INT_MAX >> 1)
72
H. Peter Anvine2c80182005-01-15 22:15:51 +000073struct forwrefinfo { /* info held on forward refs. */
H. Peter Anvineba20a72002-04-30 20:53:55 +000074 int lineno;
75 int operand;
76};
77
Keith Kaniosa6dfa782007-04-13 16:47:53 +000078static int get_bits(char *value);
Cyrill Gorcunov08359152013-11-09 22:16:11 +040079static iflag_t get_cpu(char *cpu_str);
Keith Kaniosa6dfa782007-04-13 16:47:53 +000080static void parse_cmdline(int, char **);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -070081static void assemble_file(char *, StrList **);
H. Peter Anvin130736c2016-02-17 20:27:41 -080082static bool is_suppressed_warning(int severity);
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -080083static bool skip_this_pass(int severity);
H. Peter Anvin9bd15062009-07-18 21:07:17 -040084static void nasm_verror_gnu(int severity, const char *fmt, va_list args);
85static void nasm_verror_vc(int severity, const char *fmt, va_list args);
86static void nasm_verror_common(int severity, const char *fmt, va_list args);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000087static void usage(void);
88
H. Peter Anvin283b3fb2016-03-07 23:18:30 -080089static bool using_debug_info, opt_verbose_info;
90static const char *debug_format;
91
H. Peter Anvin6867acc2007-10-10 14:58:45 -070092bool tasm_compatible_mode = false;
H. Peter Anvin00835fe2008-01-08 23:03:57 -080093int pass0, passn;
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +000094int globalrel = 0;
Jin Kyu Songb287ff02013-12-04 20:05:55 -080095int globalbnd = 0;
H. Peter Anvineba20a72002-04-30 20:53:55 +000096
H. Peter Anvin9bd15062009-07-18 21:07:17 -040097static time_t official_compile_time;
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -080098
Keith Kaniosa6dfa782007-04-13 16:47:53 +000099static char inname[FILENAME_MAX];
100static char outname[FILENAME_MAX];
101static char listname[FILENAME_MAX];
Charles Craynefcce07f2007-09-30 22:15:36 -0700102static char errname[FILENAME_MAX];
H. Peter Anvine2c80182005-01-15 22:15:51 +0000103static int globallineno; /* for forward-reference tracking */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000104/* static int pass = 0; */
H. Peter Anvin338656c2016-02-17 20:59:22 -0800105const struct ofmt *ofmt = &OF_DEFAULT;
106const struct ofmt_alias *ofmt_alias = NULL;
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400107const struct dfmt *dfmt;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000108
H. Peter Anvine2c80182005-01-15 22:15:51 +0000109static FILE *error_file; /* Where to write error messages */
H. Peter Anvin620515a2002-04-30 20:57:38 +0000110
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400111FILE *ofile = NULL;
H. Peter Anvin31387b22010-07-15 18:28:52 -0700112int optimizing = MAX_OPTIMIZE; /* number of optimization passes to take */
113static int sb, cmd_sb = 16; /* by default */
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400114
115static iflag_t cpu;
116static iflag_t cmd_cpu;
117
Charles Craynec1905c22008-09-11 18:54:06 -0700118int64_t global_offset_changed; /* referenced in labels.c */
119int64_t prev_offset_changed;
120int32_t stall_count;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000121
H. Peter Anvincd7893d2016-02-18 01:25:46 -0800122struct location location;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000123int in_abs_seg; /* Flag we are in ABSOLUTE seg */
Keith Kaniosb7a89542007-04-12 02:40:54 +0000124int32_t abs_seg; /* ABSOLUTE segment basis */
125int32_t abs_offset; /* ABSOLUTE offset */
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000126
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000127static struct RAA *offsets;
H. Peter Anvinea838272002-04-30 20:51:53 +0000128
H. Peter Anvine2c80182005-01-15 22:15:51 +0000129static struct SAA *forwrefs; /* keep track of forward references */
H. Peter Anvin9d637df2007-10-04 13:42:56 -0700130static const struct forwrefinfo *forwref;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000131
H. Peter Anvine7469712016-02-18 02:20:59 -0800132static const struct preproc_ops *preproc;
Cyrill Gorcunov86b2ad02011-07-01 10:38:25 +0400133
Cyrill Gorcunove9fc88c2014-06-23 02:22:02 +0400134#define OP_NORMAL (1u << 0)
135#define OP_PREPROCESS (1u << 1)
136#define OP_DEPEND (1u << 2)
137
138static unsigned int operating_mode;
Cyrill Gorcunov3ed32cb2014-06-22 20:53:12 +0400139
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700140/* Dependency flags */
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700141static bool depend_emit_phony = false;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700142static bool depend_missing_ok = false;
143static const char *depend_target = NULL;
144static const char *depend_file = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000145
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000146/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000147 * Which of the suppressible warnings are suppressed. Entry zero
H. Peter Anvinb030c922007-11-13 11:31:15 -0800148 * isn't an actual warning, but it used for -w+error/-Werror.
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000149 */
Victor van den Elzen819703a2008-07-16 15:20:56 +0200150
H. Peter Anvin972079f2008-09-30 17:01:23 -0700151static bool warning_on[ERR_WARN_MAX+1]; /* Current state */
152static bool warning_on_global[ERR_WARN_MAX+1]; /* Command-line state */
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000153
H. Peter Anvin972079f2008-09-30 17:01:23 -0700154static const struct warning {
155 const char *name;
156 const char *help;
157 bool enabled;
158} warnings[ERR_WARN_MAX+1] = {
159 {"error", "treat warnings as errors", false},
160 {"macro-params", "macro calls with wrong parameter count", true},
161 {"macro-selfref", "cyclic macro references", false},
162 {"macro-defaults", "macros with more default than optional parameters", true},
163 {"orphan-labels", "labels alone on lines without trailing `:'", true},
H. Peter Anvin9a65f712008-10-26 08:59:04 -0700164 {"number-overflow", "numeric constant does not fit", true},
H. Peter Anvin972079f2008-09-30 17:01:23 -0700165 {"gnu-elf-extensions", "using 8- or 16-bit relocation in ELF32, a GNU extension", false},
166 {"float-overflow", "floating point overflow", true},
167 {"float-denorm", "floating point denormal", false},
168 {"float-underflow", "floating point underflow", false},
169 {"float-toolong", "too many digits in floating-point number", true},
170 {"user", "%warning directives", true},
H. Peter Anvin5a24fdd2012-02-25 15:10:04 -0800171 {"lock", "lock prefix on unlockable instructions", true},
172 {"hle", "invalid hle prefixes", true},
Jin Kyu Songbb8cf3f2013-11-29 00:38:29 -0800173 {"bnd", "invalid bnd prefixes", true},
H. Peter Anvinecc9e0e2016-02-11 20:29:34 -0800174 {"zext-reloc", "relocation zero-extended to match output format", true},
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000175};
176
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700177static bool want_usage;
178static bool terminate_after_phase;
H. Peter Anvin130736c2016-02-17 20:27:41 -0800179bool user_nolist = false;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000180
H. Peter Anvin55340992012-09-09 17:09:00 -0700181static char *quote_for_make(const char *str);
182
Cyrill Gorcunov190232f2013-02-15 12:35:04 +0400183static int64_t get_curr_offs(void)
184{
185 return in_abs_seg ? abs_offset : raa_read(offsets, location.segment);
186}
187
188static void set_curr_offs(int64_t l_off)
189{
190 if (in_abs_seg)
191 abs_offset = l_off;
192 else
193 offsets = raa_write(offsets, location.segment, l_off);
194}
195
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000196static void nasm_fputs(const char *line, FILE * outfile)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000197{
H. Peter Anvin310b3e12002-05-14 22:38:55 +0000198 if (outfile) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000199 fputs(line, outfile);
H. Peter Anvind1fb15c2007-11-13 09:37:59 -0800200 putc('\n', outfile);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000201 } else
H. Peter Anvine2c80182005-01-15 22:15:51 +0000202 puts(line);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000203}
204
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800205/* Convert a struct tm to a POSIX-style time constant */
H. Peter Anvin724719b2015-01-05 15:17:56 -0800206static int64_t make_posix_time(struct tm *tm)
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800207{
208 int64_t t;
209 int64_t y = tm->tm_year;
210
211 /* See IEEE 1003.1:2004, section 4.14 */
212
213 t = (y-70)*365 + (y-69)/4 - (y-1)/100 + (y+299)/400;
214 t += tm->tm_yday;
215 t *= 24;
216 t += tm->tm_hour;
217 t *= 60;
218 t += tm->tm_min;
219 t *= 60;
220 t += tm->tm_sec;
221
222 return t;
223}
224
225static void define_macros_early(void)
226{
227 char temp[128];
228 struct tm lt, *lt_p, gm, *gm_p;
229 int64_t posix_time;
230
231 lt_p = localtime(&official_compile_time);
232 if (lt_p) {
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400233 lt = *lt_p;
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800234
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400235 strftime(temp, sizeof temp, "__DATE__=\"%Y-%m-%d\"", &lt);
236 preproc->pre_define(temp);
237 strftime(temp, sizeof temp, "__DATE_NUM__=%Y%m%d", &lt);
238 preproc->pre_define(temp);
239 strftime(temp, sizeof temp, "__TIME__=\"%H:%M:%S\"", &lt);
240 preproc->pre_define(temp);
241 strftime(temp, sizeof temp, "__TIME_NUM__=%H%M%S", &lt);
242 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800243 }
244
245 gm_p = gmtime(&official_compile_time);
246 if (gm_p) {
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400247 gm = *gm_p;
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800248
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400249 strftime(temp, sizeof temp, "__UTC_DATE__=\"%Y-%m-%d\"", &gm);
250 preproc->pre_define(temp);
251 strftime(temp, sizeof temp, "__UTC_DATE_NUM__=%Y%m%d", &gm);
252 preproc->pre_define(temp);
253 strftime(temp, sizeof temp, "__UTC_TIME__=\"%H:%M:%S\"", &gm);
254 preproc->pre_define(temp);
255 strftime(temp, sizeof temp, "__UTC_TIME_NUM__=%H%M%S", &gm);
256 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800257 }
H. Peter Anvind85d2502008-05-04 17:53:31 -0700258
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800259 if (gm_p)
H. Peter Anvin724719b2015-01-05 15:17:56 -0800260 posix_time = make_posix_time(&gm);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800261 else if (lt_p)
H. Peter Anvin724719b2015-01-05 15:17:56 -0800262 posix_time = make_posix_time(&lt);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800263 else
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400264 posix_time = 0;
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800265
266 if (posix_time) {
Cyrill Gorcunovcdaae1a2013-02-15 02:16:58 +0400267 snprintf(temp, sizeof temp, "__POSIX_TIME__=%"PRId64, posix_time);
268 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800269 }
270}
271
272static void define_macros_late(void)
273{
274 char temp[128];
275
Cyrill Gorcunovc1936da2011-04-06 18:32:15 +0400276 /*
277 * In case if output format is defined by alias
278 * we have to put shortname of the alias itself here
279 * otherwise ABI backward compatibility gets broken.
280 */
Cyrill Gorcunov69ce7502010-07-12 15:19:17 +0400281 snprintf(temp, sizeof(temp), "__OUTPUT_FORMAT__=%s",
Cyrill Gorcunovc1936da2011-04-06 18:32:15 +0400282 ofmt_alias ? ofmt_alias->shortname : ofmt->shortname);
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +0400283 preproc->pre_define(temp);
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800284}
285
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700286static void emit_dependencies(StrList *list)
287{
288 FILE *deps;
289 int linepos, len;
290 StrList *l, *nl;
291
292 if (depend_file && strcmp(depend_file, "-")) {
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400293 deps = fopen(depend_file, "w");
294 if (!deps) {
295 nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
296 "unable to write dependency file `%s'", depend_file);
297 return;
298 }
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700299 } else {
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400300 deps = stdout;
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700301 }
H. Peter Anvin323fcff2009-07-12 12:04:56 -0700302
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700303 linepos = fprintf(deps, "%s:", depend_target);
Cyrill Gorcunovfcd0a742009-07-27 16:26:26 +0400304 list_for_each(l, list) {
H. Peter Anvin55340992012-09-09 17:09:00 -0700305 char *file = quote_for_make(l->str);
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400306 len = strlen(file);
307 if (linepos + len > 62 && linepos > 1) {
308 fprintf(deps, " \\\n ");
309 linepos = 1;
310 }
311 fprintf(deps, " %s", file);
312 linepos += len+1;
H. Peter Anvin55340992012-09-09 17:09:00 -0700313 nasm_free(file);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700314 }
315 fprintf(deps, "\n\n");
H. Peter Anvin323fcff2009-07-12 12:04:56 -0700316
Cyrill Gorcunovfcd0a742009-07-27 16:26:26 +0400317 list_for_each_safe(l, nl, list) {
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400318 if (depend_emit_phony)
319 fprintf(deps, "%s:\n\n", l->str);
320 nasm_free(l);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700321 }
322
323 if (deps != stdout)
Cyrill Gorcunov52405e32013-02-15 12:25:04 +0400324 fclose(deps);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700325}
326
H. Peter Anvin038d8612007-04-12 16:54:50 +0000327int main(int argc, char **argv)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000328{
H. Peter Anvin9e1f5282008-05-29 21:38:00 -0700329 StrList *depend_list = NULL, **depend_ptr;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700330
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800331 time(&official_compile_time);
332
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400333 iflag_set(&cpu, IF_PLEVEL);
334 iflag_set(&cmd_cpu, IF_PLEVEL);
335
Charles Crayne2581c862008-09-10 19:21:52 -0700336 pass0 = 0;
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700337 want_usage = terminate_after_phase = false;
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400338 nasm_set_verror(nasm_verror_gnu);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000339
H. Peter Anvin97e15752007-09-25 08:47:47 -0700340 error_file = stderr;
341
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -0700342 tolower_init();
343
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000344 offsets = raa_init();
Keith Kaniosb7a89542007-04-12 02:40:54 +0000345 forwrefs = saa_init((int32_t)sizeof(struct forwrefinfo));
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000346
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000347 preproc = &nasmpp;
Cyrill Gorcunov3ed32cb2014-06-22 20:53:12 +0400348 operating_mode = OP_NORMAL;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000349
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800350 /* Define some macros dependent on the runtime, but not
351 on the command line. */
352 define_macros_early();
353
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000354 parse_cmdline(argc, argv);
355
H. Peter Anvine2c80182005-01-15 22:15:51 +0000356 if (terminate_after_phase) {
357 if (want_usage)
358 usage();
359 return 1;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000360 }
361
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800362 if (!using_debug_info) {
363 /* No debug info, redirect to the null backend (empty stubs) */
H. Peter Anvina7bc15d2016-02-17 20:55:08 -0800364 dfmt = &null_debug_form;
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800365 } else if (!debug_format) {
366 /* Default debug format for this backend */
H. Peter Anvina7bc15d2016-02-17 20:55:08 -0800367 dfmt = ofmt->default_dfmt;
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800368 } else {
369 dfmt = dfmt_find(ofmt, debug_format);
370 if (!dfmt) {
371 nasm_fatal(ERR_NOFILE | ERR_USAGE,
372 "unrecognized debug format `%s' for"
373 " output format `%s'",
374 debug_format, ofmt->shortname);
375 }
376 }
H. Peter Anvinbb88d012003-09-10 23:34:23 +0000377
H. Peter Anvin76690a12002-04-30 20:52:49 +0000378 if (ofmt->stdmac)
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +0400379 preproc->extra_stdmac(ofmt->stdmac);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000380
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000381 /* define some macros dependent of command-line */
H. Peter Anvin6b18bcc2008-02-16 14:54:10 -0800382 define_macros_late();
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000383
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400384 depend_ptr = (depend_file || (operating_mode & OP_DEPEND)) ? &depend_list : NULL;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700385 if (!depend_target)
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400386 depend_target = quote_for_make(outname);
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700387
Cyrill Gorcunove9fc88c2014-06-23 02:22:02 +0400388 if (operating_mode & OP_DEPEND) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000389 char *line;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700390
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400391 if (depend_missing_ok)
392 preproc->include_path(NULL); /* "assume generated" */
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700393
H. Peter Anvin130736c2016-02-17 20:27:41 -0800394 preproc->reset(inname, 0, depend_ptr);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000395 if (outname[0] == '\0')
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400396 ofmt->filename(inname, outname);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000397 ofile = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000398 while ((line = preproc->getline()))
399 nasm_free(line);
400 preproc->cleanup(0);
Cyrill Gorcunove9fc88c2014-06-23 02:22:02 +0400401 } else if (operating_mode & OP_PREPROCESS) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000402 char *line;
403 char *file_name = NULL;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000404 int32_t prior_linnum = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000405 int lineinc = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000406
H. Peter Anvine2c80182005-01-15 22:15:51 +0000407 if (*outname) {
408 ofile = fopen(outname, "w");
409 if (!ofile)
H. Peter Anvin41087062016-03-03 14:27:34 -0800410 nasm_fatal(ERR_NOFILE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000411 "unable to open output file `%s'",
412 outname);
413 } else
414 ofile = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000415
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700416 location.known = false;
H. Peter Anvin734b1882002-04-30 21:01:08 +0000417
Cyrill Gorcunovb574b072011-12-05 01:56:40 +0400418 /* pass = 1; */
H. Peter Anvin130736c2016-02-17 20:27:41 -0800419 preproc->reset(inname, 3, depend_ptr);
420 memcpy(warning_on, warning_on_global,
421 (ERR_WARN_MAX+1) * sizeof(bool));
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700422
H. Peter Anvine2c80182005-01-15 22:15:51 +0000423 while ((line = preproc->getline())) {
424 /*
425 * We generate %line directives if needed for later programs
426 */
Keith Kaniosb7a89542007-04-12 02:40:54 +0000427 int32_t linnum = prior_linnum += lineinc;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000428 int altline = src_get(&linnum, &file_name);
429 if (altline) {
430 if (altline == 1 && lineinc == 1)
431 nasm_fputs("", ofile);
432 else {
433 lineinc = (altline != -1 || lineinc != 1);
434 fprintf(ofile ? ofile : stdout,
Keith Kanios93f2e9a2007-04-14 00:10:59 +0000435 "%%line %"PRId32"+%d %s\n", linnum, lineinc,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000436 file_name);
437 }
438 prior_linnum = linnum;
439 }
440 nasm_fputs(line, ofile);
441 nasm_free(line);
442 }
443 nasm_free(file_name);
444 preproc->cleanup(0);
445 if (ofile)
446 fclose(ofile);
447 if (ofile && terminate_after_phase)
448 remove(outname);
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400449 ofile = NULL;
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400450 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000451
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400452 if (operating_mode & OP_NORMAL) {
453 /*
454 * We must call ofmt->filename _anyway_, even if the user
455 * has specified their own output file, because some
456 * formats (eg OBJ and COFF) use ofmt->filename to find out
457 * the name of the input file and then put that inside the
458 * file.
459 */
460 ofmt->filename(inname, outname);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000461
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400462 ofile = fopen(outname, (ofmt->flags & OFMT_TEXT) ? "w" : "wb");
463 if (!ofile)
H. Peter Anvin41087062016-03-03 14:27:34 -0800464 nasm_fatal(ERR_NOFILE,
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400465 "unable to open output file `%s'", outname);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000466
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400467 /*
468 * We must call init_labels() before ofmt->init() since
469 * some object formats will want to define labels in their
470 * init routines. (eg OS/2 defines the FLAT group)
471 */
472 init_labels();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000473
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400474 ofmt->init();
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400475 dfmt->init();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000476
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400477 assemble_file(inname, depend_ptr);
Victor van den Elzenc82c3722008-06-04 15:24:20 +0200478
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400479 if (!terminate_after_phase) {
H. Peter Anvin477ae442016-03-07 22:53:06 -0800480 ofmt->cleanup();
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400481 cleanup_labels();
482 fflush(ofile);
483 if (ferror(ofile))
484 nasm_error(ERR_NONFATAL|ERR_NOFILE,
485 "write error on output file `%s'", outname);
486 }
487
488 if (ofile) {
489 fclose(ofile);
490 if (terminate_after_phase)
491 remove(outname);
492 ofile = NULL;
493 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000494 }
H. Peter Anvin734b1882002-04-30 21:01:08 +0000495
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700496 if (depend_list && !terminate_after_phase)
Cyrill Gorcunov9f563692013-02-15 12:24:11 +0400497 emit_dependencies(depend_list);
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700498
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000499 if (want_usage)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000500 usage();
H. Peter Anvin734b1882002-04-30 21:01:08 +0000501
H. Peter Anvine2c80182005-01-15 22:15:51 +0000502 raa_free(offsets);
503 saa_free(forwrefs);
504 eval_cleanup();
H. Peter Anvin74cc5e52007-08-30 22:35:34 +0000505 stdscan_cleanup();
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000506
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -0700507 return terminate_after_phase;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000508}
509
H. Peter Anvineba20a72002-04-30 20:53:55 +0000510/*
511 * Get a parameter for a command line option.
512 * First arg must be in the form of e.g. -f...
513 */
H. Peter Anvin423e3812007-11-15 17:12:29 -0800514static char *get_param(char *p, char *q, bool *advance)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000515{
H. Peter Anvin423e3812007-11-15 17:12:29 -0800516 *advance = false;
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +0400517 if (p[2]) /* the parameter's in the option */
518 return nasm_skip_spaces(p + 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000519 if (q && q[0]) {
H. Peter Anvin423e3812007-11-15 17:12:29 -0800520 *advance = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000521 return q;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000522 }
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400523 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000524 "option `-%c' requires an argument", p[1]);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000525 return NULL;
526}
527
H. Peter Anvindc242712007-11-18 11:55:10 -0800528/*
529 * Copy a filename
530 */
531static void copy_filename(char *dst, const char *src)
532{
533 size_t len = strlen(src);
534
535 if (len >= (size_t)FILENAME_MAX) {
H. Peter Anvin41087062016-03-03 14:27:34 -0800536 nasm_fatal(ERR_NOFILE, "file name too long");
Cyrill Gorcunov45aa1182013-02-15 12:22:59 +0400537 return;
H. Peter Anvindc242712007-11-18 11:55:10 -0800538 }
539 strncpy(dst, src, FILENAME_MAX);
540}
541
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700542/*
543 * Convert a string to Make-safe form
544 */
545static char *quote_for_make(const char *str)
546{
547 const char *p;
548 char *os, *q;
549
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400550 size_t n = 1; /* Terminating zero */
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700551 size_t nbs = 0;
552
553 if (!str)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400554 return NULL;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700555
556 for (p = str; *p; p++) {
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400557 switch (*p) {
558 case ' ':
559 case '\t':
560 /* Convert N backslashes + ws -> 2N+1 backslashes + ws */
561 n += nbs + 2;
562 nbs = 0;
563 break;
564 case '$':
565 case '#':
566 nbs = 0;
567 n += 2;
568 break;
569 case '\\':
570 nbs++;
571 n++;
572 break;
573 default:
574 nbs = 0;
575 n++;
576 break;
577 }
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700578 }
579
580 /* Convert N backslashes at the end of filename to 2N backslashes */
581 if (nbs)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400582 n += nbs;
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700583
584 os = q = nasm_malloc(n);
585
586 nbs = 0;
587 for (p = str; *p; p++) {
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400588 switch (*p) {
589 case ' ':
590 case '\t':
591 while (nbs--)
592 *q++ = '\\';
593 *q++ = '\\';
594 *q++ = *p;
595 break;
596 case '$':
597 *q++ = *p;
598 *q++ = *p;
599 nbs = 0;
600 break;
601 case '#':
602 *q++ = '\\';
603 *q++ = *p;
604 nbs = 0;
605 break;
606 case '\\':
607 *q++ = *p;
608 nbs++;
609 break;
610 default:
611 *q++ = *p;
612 nbs = 0;
613 break;
614 }
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700615 }
616 while (nbs--)
Cyrill Gorcunovf8316452013-02-15 12:22:27 +0400617 *q++ = '\\';
H. Peter Anvin07b7b9e2008-05-29 19:09:11 -0700618
619 *q = '\0';
620
621 return os;
622}
623
H. Peter Anvine2c80182005-01-15 22:15:51 +0000624struct textargs {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000625 const char *label;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000626 int value;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000627};
628
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100629enum text_options {
630 OPT_PREFIX,
H. Peter Anvin7214d182016-03-01 22:43:51 -0800631 OPT_POSTFIX
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100632};
H. Peter Anvin2530a102016-02-18 02:28:15 -0800633static const struct textargs textopts[] = {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000634 {"prefix", OPT_PREFIX},
635 {"postfix", OPT_POSTFIX},
636 {NULL, 0}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000637};
638
Cyrill Gorcunove7438432014-05-09 22:34:34 +0400639static void show_version(void)
640{
641 printf("NASM version %s compiled on %s%s\n",
642 nasm_version, nasm_date, nasm_compile_options);
643 exit(0);
644}
645
H. Peter Anvin423e3812007-11-15 17:12:29 -0800646static bool stopoptions = false;
647static bool process_arg(char *p, char *q)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000648{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000649 char *param;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800650 int i;
651 bool advance = false;
H. Peter Anvin972079f2008-09-30 17:01:23 -0700652 bool do_warn;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000653
654 if (!p || !p[0])
H. Peter Anvin423e3812007-11-15 17:12:29 -0800655 return false;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000656
H. Peter Anvine2c80182005-01-15 22:15:51 +0000657 if (p[0] == '-' && !stopoptions) {
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400658 if (strchr("oOfpPdDiIlFXuUZwW", p[1])) {
659 /* These parameters take values */
660 if (!(param = get_param(p, q, &advance)))
661 return advance;
662 }
H. Peter Anvin423e3812007-11-15 17:12:29 -0800663
H. Peter Anvine2c80182005-01-15 22:15:51 +0000664 switch (p[1]) {
665 case 's':
666 error_file = stdout;
667 break;
H. Peter Anvin70653092007-10-19 14:42:29 -0700668
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400669 case 'o': /* output file */
670 copy_filename(outname, param);
671 break;
H. Peter Anvinfd7dd112007-10-10 14:06:59 -0700672
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400673 case 'f': /* output format */
674 ofmt = ofmt_find(param, &ofmt_alias);
675 if (!ofmt) {
H. Peter Anvin41087062016-03-03 14:27:34 -0800676 nasm_fatal(ERR_NOFILE | ERR_USAGE,
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400677 "unrecognised output format `%s' - "
678 "use -hf for a list", param);
679 }
680 break;
H. Peter Anvin70653092007-10-19 14:42:29 -0700681
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400682 case 'O': /* Optimization level */
683 {
684 int opt;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700685
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400686 if (!*param) {
687 /* Naked -O == -Ox */
688 optimizing = MAX_OPTIMIZE;
689 } else {
690 while (*param) {
691 switch (*param) {
692 case '0': case '1': case '2': case '3': case '4':
693 case '5': case '6': case '7': case '8': case '9':
694 opt = strtoul(param, &param, 10);
H. Peter Anvind85d2502008-05-04 17:53:31 -0700695
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400696 /* -O0 -> optimizing == -1, 0.98 behaviour */
697 /* -O1 -> optimizing == 0, 0.98.09 behaviour */
698 if (opt < 2)
699 optimizing = opt - 1;
700 else
701 optimizing = opt;
702 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700703
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400704 case 'v':
705 case '+':
706 param++;
707 opt_verbose_info = true;
708 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700709
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400710 case 'x':
711 param++;
712 optimizing = MAX_OPTIMIZE;
713 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700714
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400715 default:
H. Peter Anvin41087062016-03-03 14:27:34 -0800716 nasm_fatal(0,
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400717 "unknown optimization option -O%c\n",
718 *param);
719 break;
720 }
721 }
722 if (optimizing > MAX_OPTIMIZE)
723 optimizing = MAX_OPTIMIZE;
724 }
725 break;
726 }
H. Peter Anvin423e3812007-11-15 17:12:29 -0800727
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400728 case 'p': /* pre-include */
729 case 'P':
730 preproc->pre_include(param);
731 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800732
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400733 case 'd': /* pre-define */
734 case 'D':
735 preproc->pre_define(param);
736 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800737
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400738 case 'u': /* un-define */
739 case 'U':
740 preproc->pre_undefine(param);
741 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800742
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400743 case 'i': /* include search path */
744 case 'I':
745 preproc->include_path(param);
746 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800747
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400748 case 'l': /* listing file */
749 copy_filename(listname, param);
750 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800751
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400752 case 'Z': /* error messages file */
753 copy_filename(errname, param);
754 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800755
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400756 case 'F': /* specify debug format */
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400757 using_debug_info = true;
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800758 debug_format = param;
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400759 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800760
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400761 case 'X': /* specify error reporting format */
762 if (nasm_stricmp("vc", param) == 0)
763 nasm_set_verror(nasm_verror_vc);
764 else if (nasm_stricmp("gnu", param) == 0)
765 nasm_set_verror(nasm_verror_gnu);
766 else
H. Peter Anvin41087062016-03-03 14:27:34 -0800767 nasm_fatal(ERR_NOFILE | ERR_USAGE,
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400768 "unrecognized error reporting format `%s'",
769 param);
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 'g':
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700773 using_debug_info = true;
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800774 if (p[2])
775 debug_format = nasm_skip_spaces(p + 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000776 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800777
H. Peter Anvine2c80182005-01-15 22:15:51 +0000778 case 'h':
779 printf
780 ("usage: nasm [-@ response file] [-o outfile] [-f format] "
781 "[-l listfile]\n"
782 " [options...] [--] filename\n"
Cyrill Gorcunove7438432014-05-09 22:34:34 +0400783 " or nasm -v (or --v) for version info\n\n"
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800784 " -t assemble in SciTech TASM compatible mode\n");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000785 printf
H. Peter Anvin413fb902007-09-26 15:19:28 -0700786 (" -E (or -e) preprocess only (writes output to stdout by default)\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000787 " -a don't preprocess (assemble only)\n"
H. Peter Anvin37a321f2007-09-24 13:41:58 -0700788 " -M generate Makefile dependencies on stdout\n"
Cyrill Gorcunov984c4db2009-07-31 00:32:17 +0400789 " -MG d:o, missing files assumed generated\n"
790 " -MF <file> set Makefile dependency file\n"
791 " -MD <file> assemble and generate dependencies\n"
792 " -MT <file> dependency target name\n"
793 " -MQ <file> dependency target name (quoted)\n"
794 " -MP emit phony target\n\n"
H. Peter Anvin413fb902007-09-26 15:19:28 -0700795 " -Z<file> redirect error messages to file\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000796 " -s redirect error messages to stdout\n\n"
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800797 " -g generate debugging information\n\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000798 " -F format select a debugging format\n\n"
H. Peter Anvin283b3fb2016-03-07 23:18:30 -0800799 " -gformat same as -g -F format\n\n"
Cyrill Gorcunovdeb082d2013-04-20 20:37:17 +0400800 " -o outfile write output to an outfile\n\n"
801 " -f format select an output format\n\n"
802 " -l listfile write listing to a listfile\n\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000803 " -I<path> adds a pathname to the include file path\n");
804 printf
H. Peter Anvinab5bd052010-07-25 12:43:30 -0700805 (" -O<digit> optimize branch offsets\n"
Cyrill Gorcunov5bc6d8e2012-03-11 14:19:17 +0400806 " -O0: No optimization\n"
Cyrill Gorcunov73b87c62009-07-31 10:26:55 +0400807 " -O1: Minimal optimization\n"
Cyrill Gorcunov5bc6d8e2012-03-11 14:19:17 +0400808 " -Ox: Multipass optimization (default)\n\n"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000809 " -P<file> pre-includes a file\n"
810 " -D<macro>[=<value>] pre-defines a macro\n"
811 " -U<macro> undefines a macro\n"
812 " -X<format> specifies error reporting format (gnu or vc)\n"
H. Peter Anvinb030c922007-11-13 11:31:15 -0800813 " -w+foo enables warning foo (equiv. -Wfoo)\n"
Cyrill Gorcunov984c4db2009-07-31 00:32:17 +0400814 " -w-foo disable warning foo (equiv. -Wno-foo)\n\n"
Cyrill Gorcunovdeb082d2013-04-20 20:37:17 +0400815 " -h show invocation summary and exit\n\n"
Cyrill Gorcunov984c4db2009-07-31 00:32:17 +0400816 "--prefix,--postfix\n"
817 " this options prepend or append the given argument to all\n"
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100818 " extern and global variables\n"
H. Peter Anvinb030c922007-11-13 11:31:15 -0800819 "Warnings:\n");
820 for (i = 0; i <= ERR_WARN_MAX; i++)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000821 printf(" %-23s %s (default %s)\n",
H. Peter Anvin972079f2008-09-30 17:01:23 -0700822 warnings[i].name, warnings[i].help,
823 warnings[i].enabled ? "on" : "off");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000824 printf
825 ("\nresponse files should contain command line parameters"
826 ", one per line.\n");
827 if (p[2] == 'f') {
828 printf("\nvalid output formats for -f are"
829 " (`*' denotes default):\n");
830 ofmt_list(ofmt, stdout);
831 } else {
832 printf("\nFor a list of valid output formats, use -hf.\n");
833 printf("For a list of debug formats, use -f <form> -y.\n");
834 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400835 exit(0); /* never need usage message here */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000836 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800837
H. Peter Anvine2c80182005-01-15 22:15:51 +0000838 case 'y':
839 printf("\nvalid debug formats for '%s' output format are"
840 " ('*' denotes default):\n", ofmt->shortname);
841 dfmt_list(ofmt, stdout);
842 exit(0);
843 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800844
H. Peter Anvine2c80182005-01-15 22:15:51 +0000845 case 't':
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700846 tasm_compatible_mode = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000847 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800848
H. Peter Anvine2c80182005-01-15 22:15:51 +0000849 case 'v':
Cyrill Gorcunove7438432014-05-09 22:34:34 +0400850 show_version();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000851 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800852
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400853 case 'e': /* preprocess only */
854 case 'E':
Cyrill Gorcunov3ed32cb2014-06-22 20:53:12 +0400855 operating_mode = OP_PREPROCESS;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000856 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800857
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400858 case 'a': /* assemble only - don't preprocess */
Cyrill Gorcunovb5e8fec2012-05-07 11:34:27 +0400859 preproc = &preproc_nop;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000860 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800861
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400862 case 'W':
863 if (param[0] == 'n' && param[1] == 'o' && param[2] == '-') {
864 do_warn = false;
865 param += 3;
866 } else {
867 do_warn = true;
868 }
869 goto set_warning;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800870
H. Peter Anvine2c80182005-01-15 22:15:51 +0000871 case 'w':
H. Peter Anvin423e3812007-11-15 17:12:29 -0800872 if (param[0] != '+' && param[0] != '-') {
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400873 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000874 "invalid option to `-w'");
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400875 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000876 }
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400877 do_warn = (param[0] == '+');
878 param++;
Cyrill Gorcunov66206e72010-04-20 14:53:44 +0400879
880set_warning:
Cyrill Gorcunov3b8c2972011-12-05 01:39:04 +0400881 for (i = 0; i <= ERR_WARN_MAX; i++) {
882 if (!nasm_stricmp(param, warnings[i].name))
883 break;
884 }
885 if (i <= ERR_WARN_MAX) {
886 warning_on_global[i] = do_warn;
887 } else if (!nasm_stricmp(param, "all")) {
888 for (i = 1; i <= ERR_WARN_MAX; i++)
889 warning_on_global[i] = do_warn;
890 } else if (!nasm_stricmp(param, "none")) {
891 for (i = 1; i <= ERR_WARN_MAX; i++)
892 warning_on_global[i] = !do_warn;
893 } else {
894 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
895 "invalid warning `%s'", param);
896 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000897 break;
H. Peter Anvin423e3812007-11-15 17:12:29 -0800898
H. Peter Anvine2c80182005-01-15 22:15:51 +0000899 case 'M':
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400900 switch (p[2]) {
901 case 0:
Cyrill Gorcunov3ed32cb2014-06-22 20:53:12 +0400902 operating_mode = OP_DEPEND;
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400903 break;
904 case 'G':
Cyrill Gorcunov3ed32cb2014-06-22 20:53:12 +0400905 operating_mode = OP_DEPEND;
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400906 depend_missing_ok = true;
907 break;
908 case 'P':
909 depend_emit_phony = true;
910 break;
911 case 'D':
Cyrill Gorcunov0dd37af2014-11-29 21:33:44 +0300912 operating_mode = OP_NORMAL;
Cyrill Gorcunov599a9822014-06-24 00:55:17 +0400913 depend_file = q;
914 advance = true;
915 break;
916 case 'F':
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +0400917 depend_file = q;
918 advance = true;
919 break;
920 case 'T':
921 depend_target = q;
922 advance = true;
923 break;
924 case 'Q':
925 depend_target = quote_for_make(q);
926 advance = true;
927 break;
928 default:
929 nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
930 "unknown dependency option `-M%c'", p[2]);
931 break;
932 }
933 if (advance && (!q || !q[0])) {
934 nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
935 "option `-M%c' requires a parameter", p[2]);
936 break;
937 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000938 break;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000939
H. Peter Anvine2c80182005-01-15 22:15:51 +0000940 case '-':
941 {
942 int s;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000943
H. Peter Anvine2c80182005-01-15 22:15:51 +0000944 if (p[2] == 0) { /* -- => stop processing options */
945 stopoptions = 1;
946 break;
947 }
Cyrill Gorcunove7438432014-05-09 22:34:34 +0400948
949 if (!nasm_stricmp(p, "--v"))
950 show_version();
951
H. Peter Anvine2c80182005-01-15 22:15:51 +0000952 for (s = 0; textopts[s].label; s++) {
953 if (!nasm_stricmp(p + 2, textopts[s].label)) {
954 break;
955 }
956 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000957
H. Peter Anvine2c80182005-01-15 22:15:51 +0000958 switch (s) {
959
960 case OPT_PREFIX:
961 case OPT_POSTFIX:
962 {
963 if (!q) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400964 nasm_error(ERR_NONFATAL | ERR_NOFILE |
H. Peter Anvine2c80182005-01-15 22:15:51 +0000965 ERR_USAGE,
966 "option `--%s' requires an argument",
967 p + 2);
968 break;
969 } else {
970 advance = 1, param = q;
971 }
972
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100973 switch (s) {
974 case OPT_PREFIX:
975 strlcpy(lprefix, param, PREFIX_MAX);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000976 break;
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100977 case OPT_POSTFIX:
978 strlcpy(lpostfix, param, POSTFIX_MAX);
979 break;
980 default:
H. Peter Anvin41087062016-03-03 14:27:34 -0800981 nasm_panic(ERR_NOFILE,
Knut St. Osmundsen3c72a1b2015-11-10 22:07:20 +0100982 "internal error");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000983 break;
984 }
985 break;
986 }
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800987
H. Peter Anvine2c80182005-01-15 22:15:51 +0000988 default:
989 {
H. Peter Anvin9bd15062009-07-18 21:07:17 -0400990 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000991 "unrecognised option `--%s'", p + 2);
992 break;
993 }
994 }
995 break;
996 }
997
998 default:
999 if (!ofmt->setinfo(GI_SWITCH, &p))
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001000 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001001 "unrecognised option `-%c'", p[1]);
1002 break;
1003 }
1004 } else {
1005 if (*inname) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001006 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001007 "more than one input file specified");
H. Peter Anvindc242712007-11-18 11:55:10 -08001008 } else {
1009 copy_filename(inname, p);
Cyrill Gorcunov331fd7c2013-02-15 12:20:58 +04001010 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001011 }
1012
1013 return advance;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001014}
1015
H. Peter Anvineba20a72002-04-30 20:53:55 +00001016#define ARG_BUF_DELTA 128
1017
H. Peter Anvine2c80182005-01-15 22:15:51 +00001018static void process_respfile(FILE * rfile)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001019{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001020 char *buffer, *p, *q, *prevarg;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001021 int bufsize, prevargsize;
1022
1023 bufsize = prevargsize = ARG_BUF_DELTA;
1024 buffer = nasm_malloc(ARG_BUF_DELTA);
1025 prevarg = nasm_malloc(ARG_BUF_DELTA);
1026 prevarg[0] = '\0';
1027
H. Peter Anvine2c80182005-01-15 22:15:51 +00001028 while (1) { /* Loop to handle all lines in file */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001029 p = buffer;
1030 while (1) { /* Loop to handle long lines */
1031 q = fgets(p, bufsize - (p - buffer), rfile);
1032 if (!q)
1033 break;
1034 p += strlen(p);
1035 if (p > buffer && p[-1] == '\n')
1036 break;
1037 if (p - buffer > bufsize - 10) {
1038 int offset;
1039 offset = p - buffer;
1040 bufsize += ARG_BUF_DELTA;
1041 buffer = nasm_realloc(buffer, bufsize);
1042 p = buffer + offset;
1043 }
1044 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001045
H. Peter Anvine2c80182005-01-15 22:15:51 +00001046 if (!q && p == buffer) {
1047 if (prevarg[0])
1048 process_arg(prevarg, NULL);
1049 nasm_free(buffer);
1050 nasm_free(prevarg);
1051 return;
1052 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001053
H. Peter Anvine2c80182005-01-15 22:15:51 +00001054 /*
1055 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
1056 * them are present at the end of the line.
1057 */
1058 *(p = &buffer[strcspn(buffer, "\r\n\032")]) = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001059
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001060 while (p > buffer && nasm_isspace(p[-1]))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001061 *--p = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001062
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +04001063 p = nasm_skip_spaces(buffer);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001064
H. Peter Anvine2c80182005-01-15 22:15:51 +00001065 if (process_arg(prevarg, p))
1066 *p = '\0';
H. Peter Anvineba20a72002-04-30 20:53:55 +00001067
Charles Crayne192d5b52007-10-18 19:02:42 -07001068 if ((int) strlen(p) > prevargsize - 10) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001069 prevargsize += ARG_BUF_DELTA;
1070 prevarg = nasm_realloc(prevarg, prevargsize);
1071 }
H. Peter Anvindc242712007-11-18 11:55:10 -08001072 strncpy(prevarg, p, prevargsize);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001073 }
1074}
1075
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001076/* Function to process args from a string of args, rather than the
1077 * argv array. Used by the environment variable and response file
1078 * processing.
1079 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001080static void process_args(char *args)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001081{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001082 char *p, *q, *arg, *prevarg;
1083 char separator = ' ';
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001084
1085 p = args;
1086 if (*p && *p != '-')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001087 separator = *p++;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001088 arg = NULL;
1089 while (*p) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001090 q = p;
1091 while (*p && *p != separator)
1092 p++;
1093 while (*p == separator)
1094 *p++ = '\0';
1095 prevarg = arg;
1096 arg = q;
1097 if (process_arg(prevarg, arg))
1098 arg = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001099 }
1100 if (arg)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001101 process_arg(arg, NULL);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001102}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001103
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001104static void process_response_file(const char *file)
1105{
1106 char str[2048];
1107 FILE *f = fopen(file, "r");
1108 if (!f) {
Cyrill Gorcunovf1964512013-02-15 12:14:06 +04001109 perror(file);
1110 exit(-1);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001111 }
1112 while (fgets(str, sizeof str, f)) {
Cyrill Gorcunovf1964512013-02-15 12:14:06 +04001113 process_args(str);
H. Peter Anvinbe2678c2008-01-21 16:23:59 -08001114 }
1115 fclose(f);
1116}
1117
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001118static void parse_cmdline(int argc, char **argv)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001119{
1120 FILE *rfile;
Cyrill Gorcunovf4941892011-07-17 13:55:25 +04001121 char *envreal, *envcopy = NULL, *p;
H. Peter Anvin972079f2008-09-30 17:01:23 -07001122 int i;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001123
Charles Craynefcce07f2007-09-30 22:15:36 -07001124 *inname = *outname = *listname = *errname = '\0';
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001125
H. Peter Anvin972079f2008-09-30 17:01:23 -07001126 for (i = 0; i <= ERR_WARN_MAX; i++)
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001127 warning_on_global[i] = warnings[i].enabled;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001128
1129 /*
H. Peter Anvinff7ccc02002-05-06 19:41:57 +00001130 * First, process the NASMENV environment variable.
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001131 */
H. Peter Anvinff7ccc02002-05-06 19:41:57 +00001132 envreal = getenv("NASMENV");
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001133 if (envreal) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001134 envcopy = nasm_strdup(envreal);
1135 process_args(envcopy);
1136 nasm_free(envcopy);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001137 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001138
1139 /*
1140 * Now process the actual command line.
1141 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001142 while (--argc) {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001143 bool advance;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001144 argv++;
1145 if (argv[0][0] == '@') {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001146 /*
1147 * We have a response file, so process this as a set of
H. Peter Anvine2c80182005-01-15 22:15:51 +00001148 * arguments like the environment variable. This allows us
1149 * to have multiple arguments on a single line, which is
1150 * different to the -@resp file processing below for regular
1151 * NASM.
1152 */
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001153 process_response_file(argv[0]+1);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001154 argc--;
1155 argv++;
1156 }
1157 if (!stopoptions && argv[0][0] == '-' && argv[0][1] == '@') {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001158 p = get_param(argv[0], argc > 1 ? argv[1] : NULL, &advance);
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001159 if (p) {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001160 rfile = fopen(p, "r");
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001161 if (rfile) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001162 process_respfile(rfile);
1163 fclose(rfile);
1164 } else
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001165 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001166 "unable to open response file `%s'", p);
1167 }
1168 } else
H. Peter Anvin423e3812007-11-15 17:12:29 -08001169 advance = process_arg(argv[0], argc > 1 ? argv[1] : NULL);
1170 argv += advance, argc -= advance;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001171 }
1172
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001173 /*
1174 * Look for basic command line typos. This definitely doesn't
1175 * catch all errors, but it might help cases of fumbled fingers.
1176 */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001177 if (!*inname)
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001178 nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001179 "no input file specified");
1180 else if (!strcmp(inname, errname) ||
1181 !strcmp(inname, outname) ||
1182 !strcmp(inname, listname) ||
1183 (depend_file && !strcmp(inname, depend_file)))
H. Peter Anvin41087062016-03-03 14:27:34 -08001184 nasm_fatal(ERR_NOFILE | ERR_USAGE,
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001185 "file `%s' is both input and output file",
1186 inname);
H. Peter Anvin59ddd262007-10-01 11:26:31 -07001187
H. Peter Anvin70653092007-10-19 14:42:29 -07001188 if (*errname) {
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001189 error_file = fopen(errname, "w");
1190 if (!error_file) {
1191 error_file = stderr; /* Revert to default! */
H. Peter Anvin41087062016-03-03 14:27:34 -08001192 nasm_fatal(ERR_NOFILE | ERR_USAGE,
Cyrill Gorcunovd64b8092011-12-05 01:44:43 +04001193 "cannot open file `%s' for error messages",
1194 errname);
1195 }
Charles Craynefcce07f2007-09-30 22:15:36 -07001196 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001197}
1198
H. Peter Anvin70055962007-10-11 00:05:31 -07001199static enum directives getkw(char **directive, char **value);
H. Peter Anvin62b24d72007-08-29 16:38:05 +00001200
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001201static void assemble_file(char *fname, StrList **depend_ptr)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001202{
H. Peter Anvin8bec2c72009-08-08 13:49:00 -07001203 char *directive, *value, *p, *q, *special, *line;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001204 insn output_ins;
H. Peter Anvin70055962007-10-11 00:05:31 -07001205 int i, validid;
1206 bool rn_error;
Charles Crayne5fbbc8c2007-11-07 19:03:46 -08001207 int32_t seg;
1208 int64_t offs;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001209 struct tokenval tokval;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001210 expr *e;
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001211 int pass_max;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001212
Cyrill Gorcunov08359152013-11-09 22:16:11 +04001213 if (cmd_sb == 32 && iflag_ffs(&cmd_cpu) < IF_386)
H. Peter Anvin130736c2016-02-17 20:27:41 -08001214 nasm_fatal(0, "command line: 32-bit segment size requires a higher cpu");
H. Peter Anvineba20a72002-04-30 20:53:55 +00001215
Charles Craynec1905c22008-09-11 18:54:06 -07001216 pass_max = prev_offset_changed = (INT_MAX >> 1) + 2; /* Almost unlimited */
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001217 for (passn = 1; pass0 <= 2; passn++) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001218 int pass1, pass2;
1219 ldfunc def_label;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001220
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001221 pass1 = pass0 == 2 ? 2 : 1; /* 1, 1, 1, ..., 1, 2 */
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001222 pass2 = passn > 1 ? 2 : 1; /* 1, 2, 2, ..., 2, 2 */
1223 /* pass0 0, 0, 0, ..., 1, 2 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00001224
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001225 def_label = passn > 1 ? redefine_label : define_label;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001226
Keith Kaniosb7a89542007-04-12 02:40:54 +00001227 globalbits = sb = cmd_sb; /* set 'bits' to command line default */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001228 cpu = cmd_cpu;
1229 if (pass0 == 2) {
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08001230 lfmt->init(listname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001231 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001232 in_abs_seg = false;
Charles Craynec1905c22008-09-11 18:54:06 -07001233 global_offset_changed = 0; /* set by redefine_label */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001234 location.segment = ofmt->section(NULL, pass2, &sb);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001235 globalbits = sb;
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001236 if (passn > 1) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001237 saa_rewind(forwrefs);
1238 forwref = saa_rstruct(forwrefs);
1239 raa_free(offsets);
1240 offsets = raa_init();
1241 }
H. Peter Anvin130736c2016-02-17 20:27:41 -08001242 preproc->reset(fname, pass1, pass1 == 2 ? depend_ptr : NULL);
H. Peter Anvin972079f2008-09-30 17:01:23 -07001243 memcpy(warning_on, warning_on_global, (ERR_WARN_MAX+1) * sizeof(bool));
Victor van den Elzen819703a2008-07-16 15:20:56 +02001244
H. Peter Anvine2c80182005-01-15 22:15:51 +00001245 globallineno = 0;
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001246 if (passn == 1)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001247 location.known = true;
Cyrill Gorcunov190232f2013-02-15 12:35:04 +04001248 location.offset = offs = get_curr_offs();
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001249
H. Peter Anvine2c80182005-01-15 22:15:51 +00001250 while ((line = preproc->getline())) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001251 enum directives d;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001252 globallineno++;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001253
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001254 /*
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001255 * Here we parse our directives; this is not handled by the
1256 * 'real' parser. This really should be a separate function.
1257 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001258 directive = line;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001259 d = getkw(&directive, &value);
H. Peter Anvin70055962007-10-11 00:05:31 -07001260 if (d) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001261 int err = 0;
H. Peter Anvin62b24d72007-08-29 16:38:05 +00001262
H. Peter Anvin70055962007-10-11 00:05:31 -07001263 switch (d) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001264 case D_SEGMENT: /* [SEGMENT n] */
1265 case D_SECTION:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001266 seg = ofmt->section(value, pass2, &sb);
1267 if (seg == NO_SEG) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001268 nasm_error(pass1 == 1 ? ERR_NONFATAL : ERR_PANIC,
Keith Kaniosb7a89542007-04-12 02:40:54 +00001269 "segment name `%s' not recognized",
H. Peter Anvine2c80182005-01-15 22:15:51 +00001270 value);
1271 } else {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001272 in_abs_seg = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001273 location.segment = seg;
1274 }
1275 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001276 case D_SECTALIGN: /* [SECTALIGN n] */
Cyrill Gorcunov9fde3352011-05-23 23:50:03 +04001277 if (*value) {
1278 stdscan_reset();
1279 stdscan_set(value);
1280 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin130736c2016-02-17 20:27:41 -08001281 e = evaluate(stdscan, NULL, &tokval, NULL, pass2, NULL);
Cyrill Gorcunov9fde3352011-05-23 23:50:03 +04001282 if (e) {
1283 unsigned int align = (unsigned int)e->value;
1284 if ((uint64_t)e->value > 0x7fffffff) {
1285 /*
1286 * FIXME: Please make some sane message here
1287 * ofmt should have some 'check' method which
1288 * would report segment alignment bounds.
1289 */
H. Peter Anvin41087062016-03-03 14:27:34 -08001290 nasm_fatal(0,
Cyrill Gorcunov9fde3352011-05-23 23:50:03 +04001291 "incorrect segment alignment `%s'", value);
1292 } else if (!is_power2(align)) {
Cyrill Gorcunovd5f2aef2010-04-20 15:33:45 +04001293 nasm_error(ERR_NONFATAL,
1294 "segment alignment `%s' is not power of two",
1295 value);
1296 }
H. Peter Anvin2c4a4d52016-02-16 17:37:18 -08001297
Cyrill Gorcunov2a587ab2010-04-21 00:51:22 +04001298 /* callee should be able to handle all details */
H. Peter Anvin2c4a4d52016-02-16 17:37:18 -08001299 if (location.segment != NO_SEG)
1300 ofmt->sectalign(location.segment, align);
Cyrill Gorcunovd5f2aef2010-04-20 15:33:45 +04001301 }
1302 }
1303 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001304 case D_EXTERN: /* [EXTERN label:special] */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001305 if (*value == '$')
1306 value++; /* skip initial $ if present */
1307 if (pass0 == 2) {
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001308 q = value;
1309 while (*q && *q != ':')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001310 q++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001311 if (*q == ':') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001312 *q++ = '\0';
1313 ofmt->symdef(value, 0L, 0L, 3, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001314 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001315 } else if (passn == 1) {
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001316 q = value;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001317 validid = true;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001318 if (!isidstart(*q))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001319 validid = false;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001320 while (*q && *q != ':') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001321 if (!isidchar(*q))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001322 validid = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001323 q++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001324 }
1325 if (!validid) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001326 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001327 "identifier expected after EXTERN");
1328 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001329 }
1330 if (*q == ':') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001331 *q++ = '\0';
1332 special = q;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001333 } else
H. Peter Anvine2c80182005-01-15 22:15:51 +00001334 special = NULL;
1335 if (!is_extern(value)) { /* allow re-EXTERN to be ignored */
1336 int temp = pass0;
1337 pass0 = 1; /* fake pass 1 in labels.c */
H. Peter Anvin605f5152009-07-18 18:31:41 -07001338 declare_as_global(value, special);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001339 define_label(value, seg_alloc(), 0L, NULL,
H. Peter Anvin605f5152009-07-18 18:31:41 -07001340 false, true);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001341 pass0 = temp;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001342 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001343 } /* else pass0 == 1 */
1344 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001345 case D_BITS: /* [BITS bits] */
Keith Kaniosb7a89542007-04-12 02:40:54 +00001346 globalbits = sb = get_bits(value);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001347 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001348 case D_GLOBAL: /* [GLOBAL symbol:special] */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001349 if (*value == '$')
1350 value++; /* skip initial $ if present */
1351 if (pass0 == 2) { /* pass 2 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001352 q = value;
1353 while (*q && *q != ':')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001354 q++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001355 if (*q == ':') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001356 *q++ = '\0';
1357 ofmt->symdef(value, 0L, 0L, 3, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001358 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001359 } else if (pass2 == 1) { /* pass == 1 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001360 q = value;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001361 validid = true;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001362 if (!isidstart(*q))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001363 validid = false;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001364 while (*q && *q != ':') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001365 if (!isidchar(*q))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001366 validid = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001367 q++;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001368 }
1369 if (!validid) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001370 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001371 "identifier expected after GLOBAL");
1372 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001373 }
1374 if (*q == ':') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001375 *q++ = '\0';
1376 special = q;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001377 } else
H. Peter Anvine2c80182005-01-15 22:15:51 +00001378 special = NULL;
H. Peter Anvin605f5152009-07-18 18:31:41 -07001379 declare_as_global(value, special);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001380 } /* pass == 1 */
1381 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001382 case D_COMMON: /* [COMMON symbol size:special] */
1383 {
1384 int64_t size;
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001385
H. Peter Anvine2c80182005-01-15 22:15:51 +00001386 if (*value == '$')
1387 value++; /* skip initial $ if present */
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001388 p = value;
1389 validid = true;
1390 if (!isidstart(*p))
1391 validid = false;
1392 while (*p && !nasm_isspace(*p)) {
1393 if (!isidchar(*p))
1394 validid = false;
1395 p++;
1396 }
1397 if (!validid) {
1398 nasm_error(ERR_NONFATAL,
1399 "identifier expected after COMMON");
1400 break;
1401 }
1402 if (*p) {
H. Peter Anvinff800412009-10-13 12:03:37 -07001403 p = nasm_zap_spaces_fwd(p);
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001404 q = p;
1405 while (*q && *q != ':')
1406 q++;
1407 if (*q == ':') {
1408 *q++ = '\0';
1409 special = q;
1410 } else {
1411 special = NULL;
1412 }
1413 size = readnum(p, &rn_error);
1414 if (rn_error) {
1415 nasm_error(ERR_NONFATAL,
1416 "invalid size specified"
1417 " in COMMON declaration");
1418 break;
1419 }
1420 } else {
1421 nasm_error(ERR_NONFATAL,
1422 "no size specified in"
1423 " COMMON declaration");
1424 break;
1425 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001426
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001427 if (pass0 < 2) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001428 define_common(value, seg_alloc(), size, special);
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001429 } else if (pass0 == 2) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001430 if (special)
1431 ofmt->symdef(value, 0L, 0L, 3, special);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001432 }
1433 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001434 }
1435 case D_ABSOLUTE: /* [ABSOLUTE address] */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001436 stdscan_reset();
Cyrill Gorcunov917117f2009-10-29 23:09:18 +03001437 stdscan_set(value);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001438 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin130736c2016-02-17 20:27:41 -08001439 e = evaluate(stdscan, NULL, &tokval, NULL, pass2, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001440 if (e) {
1441 if (!is_reloc(e))
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001442 nasm_error(pass0 ==
H. Peter Anvine2c80182005-01-15 22:15:51 +00001443 1 ? ERR_NONFATAL : ERR_PANIC,
1444 "cannot use non-relocatable expression as "
1445 "ABSOLUTE address");
1446 else {
1447 abs_seg = reloc_seg(e);
1448 abs_offset = reloc_value(e);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001449 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001450 } else if (passn == 1)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001451 abs_offset = 0x100; /* don't go near zero in case of / */
1452 else
H. Peter Anvin41087062016-03-03 14:27:34 -08001453 nasm_panic(0, "invalid ABSOLUTE address "
H. Peter Anvine2c80182005-01-15 22:15:51 +00001454 "in pass two");
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001455 in_abs_seg = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001456 location.segment = NO_SEG;
1457 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001458 case D_DEBUG: /* [DEBUG] */
1459 {
1460 char debugid[128];
1461 bool badid, overlong;
H. Peter Anvin8bec2c72009-08-08 13:49:00 -07001462
H. Peter Anvine2c80182005-01-15 22:15:51 +00001463 p = value;
1464 q = debugid;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001465 badid = overlong = false;
H. Peter Anvin8bec2c72009-08-08 13:49:00 -07001466 if (!isidstart(*p)) {
1467 badid = true;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001468 } else {
1469 while (*p && !nasm_isspace(*p)) {
1470 if (q >= debugid + sizeof debugid - 1) {
1471 overlong = true;
1472 break;
1473 }
1474 if (!isidchar(*p))
1475 badid = true;
1476 *q++ = *p++;
1477 }
1478 *q = 0;
1479 }
H. Peter Anvin8bec2c72009-08-08 13:49:00 -07001480 if (badid) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001481 nasm_error(passn == 1 ? ERR_NONFATAL : ERR_PANIC,
1482 "identifier expected after DEBUG");
1483 break;
1484 }
1485 if (overlong) {
1486 nasm_error(passn == 1 ? ERR_NONFATAL : ERR_PANIC,
1487 "DEBUG identifier too long");
1488 break;
1489 }
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +04001490 p = nasm_skip_spaces(p);
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001491 if (pass0 == 2)
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001492 dfmt->debug_directive(debugid, p);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001493 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001494 }
1495 case D_WARNING: /* [WARNING {+|-|*}warn-name] */
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +04001496 value = nasm_skip_spaces(value);
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001497 switch(*value) {
1498 case '-': validid = 0; value++; break;
1499 case '+': validid = 1; value++; break;
1500 case '*': validid = 2; value++; break;
1501 default: validid = 1; break;
1502 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001503
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001504 for (i = 1; i <= ERR_WARN_MAX; i++)
1505 if (!nasm_stricmp(value, warnings[i].name))
1506 break;
1507 if (i <= ERR_WARN_MAX) {
1508 switch(validid) {
1509 case 0:
1510 warning_on[i] = false;
1511 break;
1512 case 1:
1513 warning_on[i] = true;
1514 break;
1515 case 2:
1516 warning_on[i] = warning_on_global[i];
1517 break;
1518 }
1519 } else
1520 nasm_error(ERR_NONFATAL,
1521 "invalid warning id in WARNING directive");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001522 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001523 case D_CPU: /* [CPU] */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001524 cpu = get_cpu(value);
1525 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001526 case D_LIST: /* [LIST {+|-}] */
Cyrill Gorcunovd61debf2009-10-13 19:38:52 +04001527 value = nasm_skip_spaces(value);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001528 if (*value == '+') {
1529 user_nolist = 0;
1530 } else {
1531 if (*value == '-') {
1532 user_nolist = 1;
1533 } else {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001534 err = 1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001535 }
1536 }
1537 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001538 case D_DEFAULT: /* [DEFAULT] */
1539 stdscan_reset();
Cyrill Gorcunov917117f2009-10-29 23:09:18 +03001540 stdscan_set(value);
H. Peter Anvin62b24d72007-08-29 16:38:05 +00001541 tokval.t_type = TOKEN_INVALID;
Jin Kyu Songb287ff02013-12-04 20:05:55 -08001542 if (stdscan(NULL, &tokval) != TOKEN_INVALID) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001543 switch ((int)tokval.t_integer) {
1544 case S_REL:
1545 globalrel = 1;
1546 break;
1547 case S_ABS:
1548 globalrel = 0;
1549 break;
Jin Kyu Songb287ff02013-12-04 20:05:55 -08001550 case P_BND:
1551 globalbnd = 1;
1552 break;
1553 case P_NOBND:
1554 globalbnd = 0;
1555 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001556 default:
1557 err = 1;
1558 break;
1559 }
1560 } else {
1561 err = 1;
1562 }
1563 break;
1564 case D_FLOAT:
1565 if (float_option(value)) {
1566 nasm_error(pass1 == 1 ? ERR_NONFATAL : ERR_PANIC,
1567 "unknown 'float' directive: %s",
1568 value);
1569 }
1570 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001571 default:
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001572 if (ofmt->directive(d, value, pass2))
1573 break;
1574 /* else fall through */
1575 case D_unknown:
1576 nasm_error(pass1 == 1 ? ERR_NONFATAL : ERR_PANIC,
1577 "unrecognised directive [%s]",
1578 directive);
1579 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001580 }
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001581 if (err) {
1582 nasm_error(ERR_NONFATAL,
1583 "invalid parameter to [%s] directive",
1584 directive);
1585 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001586 } else { /* it isn't a directive */
H. Peter Anvin00444ae2009-07-18 18:49:55 -07001587 parse_line(pass1, line, &output_ins, def_label);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001588
Charles Crayne2581c862008-09-10 19:21:52 -07001589 if (optimizing > 0) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001590 if (forwref != NULL && globallineno == forwref->lineno) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001591 output_ins.forw_ref = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001592 do {
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001593 output_ins.oprs[forwref->operand].opflags |= OPFLAG_FORWARD;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001594 forwref = saa_rstruct(forwrefs);
1595 } while (forwref != NULL
1596 && forwref->lineno == globallineno);
1597 } else
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001598 output_ins.forw_ref = false;
H. Peter Anvin323fcff2009-07-12 12:04:56 -07001599
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001600 if (output_ins.forw_ref) {
1601 if (passn == 1) {
1602 for (i = 0; i < output_ins.operands; i++) {
1603 if (output_ins.oprs[i].opflags & OPFLAG_FORWARD) {
1604 struct forwrefinfo *fwinf = (struct forwrefinfo *)saa_wstruct(forwrefs);
1605 fwinf->lineno = globallineno;
1606 fwinf->operand = i;
1607 }
1608 }
1609 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001610 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001611 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001612
H. Peter Anvine2c80182005-01-15 22:15:51 +00001613 /* forw_ref */
1614 if (output_ins.opcode == I_EQU) {
1615 if (pass1 == 1) {
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001616 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00001617 * Special `..' EQUs get processed in pass two,
1618 * except `..@' macro-processor EQUs which are done
1619 * in the normal place.
1620 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001621 if (!output_ins.label)
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001622 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001623 "EQU not preceded by label");
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001624
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001625 else if (output_ins.label[0] != '.' ||
1626 output_ins.label[1] != '.' ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00001627 output_ins.label[2] == '@') {
1628 if (output_ins.operands == 1 &&
1629 (output_ins.oprs[0].type & IMMEDIATE) &&
1630 output_ins.oprs[0].wrt == NO_SEG) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001631 bool isext = !!(output_ins.oprs[0].opflags & OPFLAG_EXTERN);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001632 def_label(output_ins.label,
1633 output_ins.oprs[0].segment,
1634 output_ins.oprs[0].offset, NULL,
H. Peter Anvin605f5152009-07-18 18:31:41 -07001635 false, isext);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001636 } else if (output_ins.operands == 2
H. Peter Anvin72191982009-02-26 14:34:48 -08001637 && (output_ins.oprs[0].type & IMMEDIATE)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001638 && (output_ins.oprs[0].type & COLON)
H. Peter Anvin72191982009-02-26 14:34:48 -08001639 && output_ins.oprs[0].segment == NO_SEG
H. Peter Anvine2c80182005-01-15 22:15:51 +00001640 && output_ins.oprs[0].wrt == NO_SEG
H. Peter Anvin72191982009-02-26 14:34:48 -08001641 && (output_ins.oprs[1].type & IMMEDIATE)
1642 && output_ins.oprs[1].segment == NO_SEG
1643 && output_ins.oprs[1].wrt == NO_SEG) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001644 def_label(output_ins.label,
H. Peter Anvin72191982009-02-26 14:34:48 -08001645 output_ins.oprs[0].offset | SEG_ABS,
1646 output_ins.oprs[1].offset,
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001647 NULL, false, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001648 } else
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001649 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001650 "bad syntax for EQU");
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001651 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001652 } else {
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001653 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00001654 * Special `..' EQUs get processed here, except
1655 * `..@' macro processor EQUs which are done above.
1656 */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001657 if (output_ins.label[0] == '.' &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00001658 output_ins.label[1] == '.' &&
1659 output_ins.label[2] != '@') {
1660 if (output_ins.operands == 1 &&
1661 (output_ins.oprs[0].type & IMMEDIATE)) {
1662 define_label(output_ins.label,
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001663 output_ins.oprs[0].segment,
1664 output_ins.oprs[0].offset,
H. Peter Anvin605f5152009-07-18 18:31:41 -07001665 NULL, false, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001666 } else if (output_ins.operands == 2
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001667 && (output_ins.oprs[0].type & IMMEDIATE)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001668 && (output_ins.oprs[0].type & COLON)
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001669 && output_ins.oprs[0].segment == NO_SEG
1670 && (output_ins.oprs[1].type & IMMEDIATE)
1671 && output_ins.oprs[1].segment == NO_SEG) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001672 define_label(output_ins.label,
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001673 output_ins.oprs[0].offset | SEG_ABS,
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001674 output_ins.oprs[1].offset,
H. Peter Anvin605f5152009-07-18 18:31:41 -07001675 NULL, false, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001676 } else
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001677 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001678 "bad syntax for EQU");
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001679 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001680 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001681 } else { /* instruction isn't an EQU */
H. Peter Anvineba20a72002-04-30 20:53:55 +00001682
H. Peter Anvine2c80182005-01-15 22:15:51 +00001683 if (pass1 == 1) {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001684
Charles Crayne5fbbc8c2007-11-07 19:03:46 -08001685 int64_t l = insn_size(location.segment, offs, sb, cpu,
H. Peter Anvin130736c2016-02-17 20:27:41 -08001686 &output_ins);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001687
H. Peter Anvine2c80182005-01-15 22:15:51 +00001688 /* if (using_debug_info) && output_ins.opcode != -1) */
1689 if (using_debug_info)
1690 { /* fbk 03/25/01 */
1691 /* this is done here so we can do debug type info */
Keith Kaniosb7a89542007-04-12 02:40:54 +00001692 int32_t typeinfo =
H. Peter Anvine2c80182005-01-15 22:15:51 +00001693 TYS_ELEMENTS(output_ins.operands);
1694 switch (output_ins.opcode) {
1695 case I_RESB:
1696 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001697 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_BYTE;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001698 break;
1699 case I_RESW:
1700 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001701 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_WORD;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001702 break;
1703 case I_RESD:
1704 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001705 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_DWORD;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001706 break;
1707 case I_RESQ:
1708 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001709 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_QWORD;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001710 break;
1711 case I_REST:
1712 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001713 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_TBYTE;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001714 break;
H. Peter Anvindfb91802008-05-20 11:43:53 -07001715 case I_RESO:
1716 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001717 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_OWORD;
H. Peter Anvindfb91802008-05-20 11:43:53 -07001718 break;
1719 case I_RESY:
1720 typeinfo =
Cyrill Gorcunovd8799002010-01-09 16:40:03 +03001721 TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_YWORD;
H. Peter Anvindfb91802008-05-20 11:43:53 -07001722 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001723 case I_DB:
1724 typeinfo |= TY_BYTE;
1725 break;
1726 case I_DW:
1727 typeinfo |= TY_WORD;
1728 break;
1729 case I_DD:
1730 if (output_ins.eops_float)
1731 typeinfo |= TY_FLOAT;
1732 else
1733 typeinfo |= TY_DWORD;
1734 break;
1735 case I_DQ:
1736 typeinfo |= TY_QWORD;
1737 break;
1738 case I_DT:
1739 typeinfo |= TY_TBYTE;
1740 break;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001741 case I_DO:
1742 typeinfo |= TY_OWORD;
1743 break;
1744 case I_DY:
1745 typeinfo |= TY_YWORD;
1746 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001747 default:
1748 typeinfo = TY_LABEL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001749
H. Peter Anvine2c80182005-01-15 22:15:51 +00001750 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00001751
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001752 dfmt->debug_typevalue(typeinfo);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001753 }
1754 if (l != -1) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001755 offs += l;
Cyrill Gorcunov190232f2013-02-15 12:35:04 +04001756 set_curr_offs(offs);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001757 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00001758 /*
H. Peter Anvine2c80182005-01-15 22:15:51 +00001759 * else l == -1 => invalid instruction, which will be
1760 * flagged as an error on pass 2
1761 */
H. Peter Anvin76690a12002-04-30 20:52:49 +00001762
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001763 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001764 offs += assemble(location.segment, offs, sb, cpu,
H. Peter Anvin130736c2016-02-17 20:27:41 -08001765 &output_ins);
Cyrill Gorcunov190232f2013-02-15 12:35:04 +04001766 set_curr_offs(offs);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001767
H. Peter Anvine2c80182005-01-15 22:15:51 +00001768 }
1769 } /* not an EQU */
1770 cleanup_insn(&output_ins);
1771 }
1772 nasm_free(line);
Cyrill Gorcunov190232f2013-02-15 12:35:04 +04001773 location.offset = offs = get_curr_offs();
H. Peter Anvine2c80182005-01-15 22:15:51 +00001774 } /* end while (line = preproc->getline... */
H. Peter Anvinaeb0e0e2009-06-27 16:30:00 -07001775
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001776 if (pass0 == 2 && global_offset_changed && !terminate_after_phase)
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001777 nasm_error(ERR_NONFATAL,
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001778 "phase error detected at end of assembly.");
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001779
H. Peter Anvine2c80182005-01-15 22:15:51 +00001780 if (pass1 == 1)
1781 preproc->cleanup(1);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001782
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001783 if ((passn > 1 && !global_offset_changed) || pass0 == 2) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001784 pass0++;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001785 } else if (global_offset_changed &&
1786 global_offset_changed < prev_offset_changed) {
Charles Craynec1905c22008-09-11 18:54:06 -07001787 prev_offset_changed = global_offset_changed;
1788 stall_count = 0;
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001789 } else {
1790 stall_count++;
1791 }
Victor van den Elzen42528232008-09-11 15:07:05 +02001792
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001793 if (terminate_after_phase)
1794 break;
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001795
1796 if ((stall_count > 997) || (passn >= pass_max)) {
Victor van den Elzen42528232008-09-11 15:07:05 +02001797 /* We get here if the labels don't converge
1798 * Example: FOO equ FOO + 1
1799 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001800 nasm_error(ERR_NONFATAL,
Victor van den Elzen42528232008-09-11 15:07:05 +02001801 "Can't find valid values for all labels "
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001802 "after %d passes, giving up.", passn);
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001803 nasm_error(ERR_NONFATAL,
1804 "Possible causes: recursive EQUs, macro abuse.");
1805 break;
1806 }
H. Peter Anvin00835fe2008-01-08 23:03:57 -08001807 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001808
H. Peter Anvine2c80182005-01-15 22:15:51 +00001809 preproc->cleanup(0);
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08001810 lfmt->cleanup();
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001811 if (!terminate_after_phase && opt_verbose_info) {
Cyrill Gorcunov14763192013-02-15 12:13:09 +04001812 /* -On and -Ov switches */
H. Peter Anvinf7a9eca2009-06-27 15:34:32 -07001813 fprintf(stdout, "info: assembly required 1+%d+1 passes\n", passn-3);
1814 }
1815}
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001816
H. Peter Anvin70055962007-10-11 00:05:31 -07001817static enum directives getkw(char **directive, char **value)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001818{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001819 char *p, *q, *buf;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001820
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001821 buf = nasm_skip_spaces(*directive);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001822
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001823 /* it should be enclosed in [ ] */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001824 if (*buf != '[')
H. Peter Anvin888964a2010-04-06 17:00:12 -07001825 return D_none;
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001826 q = strchr(buf, ']');
1827 if (!q)
H. Peter Anvin888964a2010-04-06 17:00:12 -07001828 return D_none;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001829
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001830 /* stip off the comments */
1831 p = strchr(buf, ';');
1832 if (p) {
1833 if (p < q) /* ouch! somwhere inside */
H. Peter Anvin888964a2010-04-06 17:00:12 -07001834 return D_none;
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001835 *p = '\0';
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001836 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001837
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001838 /* no brace, no trailing spaces */
1839 *q = '\0';
1840 nasm_zap_spaces_rev(--q);
1841
1842 /* directive */
1843 p = nasm_skip_spaces(++buf);
1844 q = nasm_skip_word(p);
1845 if (!q)
H. Peter Anvin888964a2010-04-06 17:00:12 -07001846 return D_none; /* sigh... no value there */
Cyrill Gorcunovbd416c62009-09-18 19:23:53 +04001847 *q = '\0';
1848 *directive = p;
1849
1850 /* and value finally */
1851 p = nasm_skip_spaces(++q);
1852 *value = p;
H. Peter Anvin62b24d72007-08-29 16:38:05 +00001853
H. Peter Anvin323fcff2009-07-12 12:04:56 -07001854 return find_directive(*directive);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001855}
1856
Ed Berosetfa771012002-06-09 20:56:40 +00001857/**
1858 * gnu style error reporting
1859 * This function prints an error message to error_file in the
1860 * style used by GNU. An example would be:
1861 * file.asm:50: error: blah blah blah
H. Peter Anvin70653092007-10-19 14:42:29 -07001862 * where file.asm is the name of the file, 50 is the line number on
Ed Berosetfa771012002-06-09 20:56:40 +00001863 * which the error occurs (or is detected) and "error:" is one of
1864 * the possible optional diagnostics -- it can be "error" or "warning"
H. Peter Anvin70653092007-10-19 14:42:29 -07001865 * or something else. Finally the line terminates with the actual
Ed Berosetfa771012002-06-09 20:56:40 +00001866 * error message.
H. Peter Anvin70653092007-10-19 14:42:29 -07001867 *
1868 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001869 * @param fmt the printf style format string
1870 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001871static void nasm_verror_gnu(int severity, const char *fmt, va_list ap)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001872{
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001873 char *currentfile = NULL;
1874 int32_t lineno = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001875
Ed Berosetfa771012002-06-09 20:56:40 +00001876 if (is_suppressed_warning(severity))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001877 return;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001878
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001879 if (!(severity & ERR_NOFILE))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001880 src_get(&lineno, &currentfile);
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001881
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001882 if (!skip_this_pass(severity)) {
1883 if (currentfile) {
1884 fprintf(error_file, "%s:%"PRId32": ", currentfile, lineno);
1885 nasm_free(currentfile);
1886 } else {
1887 fputs("nasm: ", error_file);
1888 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001889 }
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001890
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001891 nasm_verror_common(severity, fmt, ap);
Ed Berosetfa771012002-06-09 20:56:40 +00001892}
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001893
Ed Berosetfa771012002-06-09 20:56:40 +00001894/**
1895 * MS style error reporting
1896 * This function prints an error message to error_file in the
H. Peter Anvin70653092007-10-19 14:42:29 -07001897 * style used by Visual C and some other Microsoft tools. An example
Ed Berosetfa771012002-06-09 20:56:40 +00001898 * would be:
Ed Beroset6e61d0d2002-06-11 03:29:36 +00001899 * file.asm(50) : error: blah blah blah
H. Peter Anvin70653092007-10-19 14:42:29 -07001900 * where file.asm is the name of the file, 50 is the line number on
Ed Beroset6e61d0d2002-06-11 03:29:36 +00001901 * which the error occurs (or is detected) and "error:" is one of
1902 * the possible optional diagnostics -- it can be "error" or "warning"
H. Peter Anvin70653092007-10-19 14:42:29 -07001903 * or something else. Finally the line terminates with the actual
Ed Beroset6e61d0d2002-06-11 03:29:36 +00001904 * error message.
H. Peter Anvin70653092007-10-19 14:42:29 -07001905 *
1906 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001907 * @param fmt the printf style format string
1908 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001909static void nasm_verror_vc(int severity, const char *fmt, va_list ap)
Ed Berosetfa771012002-06-09 20:56:40 +00001910{
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001911 char *currentfile = NULL;
1912 int32_t lineno = 0;
Ed Berosetfa771012002-06-09 20:56:40 +00001913
H. Peter Anvine2c80182005-01-15 22:15:51 +00001914 if (is_suppressed_warning(severity))
1915 return;
Ed Berosetfa771012002-06-09 20:56:40 +00001916
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001917 if (!(severity & ERR_NOFILE))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001918 src_get(&lineno, &currentfile);
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001919
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001920 if (!skip_this_pass(severity)) {
1921 if (currentfile) {
1922 fprintf(error_file, "%s(%"PRId32") : ", currentfile, lineno);
1923 nasm_free(currentfile);
1924 } else {
1925 fputs("nasm: ", error_file);
1926 }
Ed Berosetfa771012002-06-09 20:56:40 +00001927 }
H. Peter Anvin48ef4192009-07-01 22:12:59 -07001928
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001929 nasm_verror_common(severity, fmt, ap);
Ed Berosetfa771012002-06-09 20:56:40 +00001930}
1931
1932/**
1933 * check for supressed warning
H. Peter Anvin70653092007-10-19 14:42:29 -07001934 * checks for suppressed warning or pass one only warning and we're
Ed Berosetfa771012002-06-09 20:56:40 +00001935 * not in pass 1
1936 *
1937 * @param severity the severity of the warning or error
1938 * @return true if we should abort error/warning printing
1939 */
H. Peter Anvin2b046cf2008-01-22 14:08:36 -08001940static bool is_suppressed_warning(int severity)
Ed Berosetfa771012002-06-09 20:56:40 +00001941{
Cyrill Gorcunovead87722011-12-04 19:24:25 +04001942 /* Not a warning at all */
1943 if ((severity & ERR_MASK) != ERR_WARNING)
1944 return false;
1945
H. Peter Anvin442a05a2012-02-24 21:50:53 -08001946 /* Might be a warning but suppresed explicitly */
1947 if (severity & ERR_WARN_MASK)
1948 return !warning_on[WARN_IDX(severity)];
1949 else
1950 return false;
Ed Berosetfa771012002-06-09 20:56:40 +00001951}
1952
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08001953static bool skip_this_pass(int severity)
1954{
1955 /* See if it's a pass-one only warning and we're not in pass one. */
1956 if ((severity & ERR_MASK) > ERR_WARNING)
1957 return false;
1958
1959 if (((severity & ERR_PASS1) && pass0 != 1) ||
1960 ((severity & ERR_PASS2) && pass0 != 2))
1961 return true;
1962
1963 return false;
1964}
1965
Ed Berosetfa771012002-06-09 20:56:40 +00001966/**
1967 * common error reporting
1968 * This is the common back end of the error reporting schemes currently
H. Peter Anvin70653092007-10-19 14:42:29 -07001969 * implemented. It prints the nature of the warning and then the
Ed Berosetfa771012002-06-09 20:56:40 +00001970 * specific error message to error_file and may or may not return. It
1971 * doesn't return if the error severity is a "panic" or "debug" type.
H. Peter Anvin70653092007-10-19 14:42:29 -07001972 *
1973 * @param severity the severity of the warning or error
Ed Berosetfa771012002-06-09 20:56:40 +00001974 * @param fmt the printf style format string
1975 */
H. Peter Anvin9bd15062009-07-18 21:07:17 -04001976static void nasm_verror_common(int severity, const char *fmt, va_list args)
Ed Berosetfa771012002-06-09 20:56:40 +00001977{
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001978 char msg[1024];
1979 const char *pfx;
H. Peter Anvin323fcff2009-07-12 12:04:56 -07001980
H. Peter Anvin7df04172008-06-10 18:27:38 -07001981 switch (severity & (ERR_MASK|ERR_NO_SEVERITY)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001982 case ERR_WARNING:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001983 pfx = "warning: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001984 break;
1985 case ERR_NONFATAL:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001986 pfx = "error: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001987 break;
1988 case ERR_FATAL:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001989 pfx = "fatal: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001990 break;
1991 case ERR_PANIC:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001992 pfx = "panic: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001993 break;
1994 case ERR_DEBUG:
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07001995 pfx = "debug: ";
H. Peter Anvine2c80182005-01-15 22:15:51 +00001996 break;
H. Peter Anvin7df04172008-06-10 18:27:38 -07001997 default:
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04001998 pfx = "";
1999 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002000 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002001
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07002002 vsnprintf(msg, sizeof msg, fmt, args);
2003
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08002004 if (!skip_this_pass(severity))
2005 fprintf(error_file, "%s%s\n", pfx, msg);
H. Peter Anvina23aa4a2009-07-07 12:04:12 -07002006
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08002007 /*
2008 * Don't suppress this with skip_this_pass(), or we don't get
2009 * preprocessor warnings in the list file
2010 */
2011 if ((severity & ERR_MASK) >= ERR_WARNING)
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08002012 lfmt->error(severity, pfx, msg);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002013
2014 if (severity & ERR_USAGE)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002015 want_usage = true;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002016
2017 switch (severity & ERR_MASK) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002018 case ERR_DEBUG:
2019 /* no further action, by definition */
2020 break;
H. Peter Anvinb030c922007-11-13 11:31:15 -08002021 case ERR_WARNING:
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04002022 /* Treat warnings as errors */
2023 if (warning_on[WARN_IDX(ERR_WARN_TERM)])
2024 terminate_after_phase = true;
2025 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002026 case ERR_NONFATAL:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002027 terminate_after_phase = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002028 break;
2029 case ERR_FATAL:
2030 if (ofile) {
2031 fclose(ofile);
2032 remove(outname);
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04002033 ofile = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002034 }
2035 if (want_usage)
2036 usage();
2037 exit(1); /* instantly die */
2038 break; /* placate silly compilers */
2039 case ERR_PANIC:
2040 fflush(NULL);
Cyrill Gorcunov22ad9042013-02-15 02:20:26 +04002041 /* abort(); */ /* halt, catch fire, and dump core */
H. Peter Anvin4a8d10c2016-02-17 20:47:01 -08002042 if (ofile) {
2043 fclose(ofile);
2044 remove(outname);
2045 ofile = NULL;
2046 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002047 exit(3);
2048 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002049 }
2050}
2051
H. Peter Anvin734b1882002-04-30 21:01:08 +00002052static void usage(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002053{
H. Peter Anvin620515a2002-04-30 20:57:38 +00002054 fputs("type `nasm -h' for help\n", error_file);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002055}
2056
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002057static iflag_t get_cpu(char *value)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002058{
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002059 iflag_t r;
2060
2061 iflag_clear_all(&r);
2062
H. Peter Anvine2c80182005-01-15 22:15:51 +00002063 if (!strcmp(value, "8086"))
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002064 iflag_set(&r, IF_8086);
2065 else if (!strcmp(value, "186"))
2066 iflag_set(&r, IF_186);
2067 else if (!strcmp(value, "286"))
2068 iflag_set(&r, IF_286);
2069 else if (!strcmp(value, "386"))
2070 iflag_set(&r, IF_386);
2071 else if (!strcmp(value, "486"))
2072 iflag_set(&r, IF_486);
2073 else if (!strcmp(value, "586") ||
2074 !nasm_stricmp(value, "pentium"))
2075 iflag_set(&r, IF_PENT);
2076 else if (!strcmp(value, "686") ||
2077 !nasm_stricmp(value, "ppro") ||
2078 !nasm_stricmp(value, "pentiumpro") ||
2079 !nasm_stricmp(value, "p2"))
2080 iflag_set(&r, IF_P6);
2081 else if (!nasm_stricmp(value, "p3") ||
2082 !nasm_stricmp(value, "katmai"))
2083 iflag_set(&r, IF_KATMAI);
2084 else if (!nasm_stricmp(value, "p4") || /* is this right? -- jrc */
2085 !nasm_stricmp(value, "willamette"))
2086 iflag_set(&r, IF_WILLAMETTE);
2087 else if (!nasm_stricmp(value, "prescott"))
2088 iflag_set(&r, IF_PRESCOTT);
2089 else if (!nasm_stricmp(value, "x64") ||
2090 !nasm_stricmp(value, "x86-64"))
2091 iflag_set(&r, IF_X86_64);
2092 else if (!nasm_stricmp(value, "ia64") ||
2093 !nasm_stricmp(value, "ia-64") ||
2094 !nasm_stricmp(value, "itanium")||
2095 !nasm_stricmp(value, "itanic") ||
2096 !nasm_stricmp(value, "merced"))
2097 iflag_set(&r, IF_IA64);
2098 else {
2099 iflag_set(&r, IF_PLEVEL);
2100 nasm_error(pass0 < 2 ? ERR_NONFATAL : ERR_FATAL,
2101 "unknown 'cpu' type");
2102 }
2103 return r;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002104}
2105
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002106static int get_bits(char *value)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002107{
2108 int i;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002109
H. Peter Anvine2c80182005-01-15 22:15:51 +00002110 if ((i = atoi(value)) == 16)
2111 return i; /* set for a 16-bit segment */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002112 else if (i == 32) {
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002113 if (iflag_ffs(&cpu) < IF_386) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04002114 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +00002115 "cannot specify 32-bit segment on processor below a 386");
2116 i = 16;
2117 }
Keith Kaniosb7a89542007-04-12 02:40:54 +00002118 } else if (i == 64) {
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002119 if (iflag_ffs(&cpu) < IF_X86_64) {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04002120 nasm_error(ERR_NONFATAL,
Keith Kaniosb7a89542007-04-12 02:40:54 +00002121 "cannot specify 64-bit segment on processor below an x86-64");
2122 i = 16;
2123 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002124 } else {
H. Peter Anvin9bd15062009-07-18 21:07:17 -04002125 nasm_error(pass0 < 2 ? ERR_NONFATAL : ERR_FATAL,
Keith Kaniosb7a89542007-04-12 02:40:54 +00002126 "`%s' is not a valid segment size; must be 16, 32 or 64",
H. Peter Anvine2c80182005-01-15 22:15:51 +00002127 value);
2128 i = 16;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002129 }
2130 return i;
2131}