Commit graph

214 commits

Author SHA1 Message Date
H. Peter Anvin
1350620bf1 ctype: create our own ctype table
Create our own ctype table where we can do the tests we want to do
cheaply, instead of calling ctype functions and then adding additional
tests all over the code.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2018-11-28 14:55:58 -08:00
H. Peter Anvin
099cc17739 eval: implement the C ? : operator
Add the C ternary conditional ? : operator.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2018-11-28 13:13:16 -08:00
H. Peter Anvin
1722fcf81c preproc.c: tell us which macro definition has too many defaults
We can always be nicer to the user by being more verbose.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2018-11-28 13:05:42 -08:00
H. Peter Anvin
c77f5079e5 Merge remote-tracking branch 'origin/nasm-2.14.xx'
Resolved Conflicts:
	asm/nasm.c
	version

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2018-11-28 12:47:25 -08:00
H. Peter Anvin
3475462ee8 nasm: fix the combination -E -MD, handle -MD without a filename
-E -MD should work and output a dependency file.
-MD can be used without a filename; there is a default filename or
-\c{-MF} can be used.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2018-11-28 12:40:58 -08:00
H. Peter Anvin
6fdf710824 eval: 'i' and 'j' are names normally used for iteration variables
Single letter variables in the sequence i, j, k... are normally used
for integer-valued iterators. Rename the token-type variable 'tt', and
use 'tto' (token type, old) when the value is saved across a scan.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2018-11-28 10:33:16 -08:00
H. Peter Anvin
99fcda0e76 expr: wrap the call to the scanner
*Every* call to the scanner is of the form i = scan(scpriv, tokval).
Wrap that in a static function instead of duplicating the code over
and over.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2018-11-28 10:27:30 -08:00
H. Peter Anvin
ef427b3fa1 eval: drop passing (critical) as an argument
There is no point in passing (critical) as an argument when
we alredy rely on a bunch of static variables.  If eval needs to be
reentrant, we should instead have something like "struct eval_state"
and pass a pointer to that as an argument.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2018-11-28 10:19:50 -08:00
H. Peter Anvin
ca605a3c38 expr: allow any expression to contain relational operators
There is absolutely no reason not to allow relational operators in
arbitrary contexts. and doing so can be quite useful.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2018-11-28 10:13:48 -08:00
H. Peter Anvin
a0ed5b3ffa Merge branch 'master' of ssh://repo.or.cz/nasm 2018-11-28 09:56:15 -08:00
H. Peter Anvin
79a070eea9 BR 3392368: correct handling of exact limb switch
When we have an exact limb switch, we may end up with a case where the
value no longer has any remaining valid bits.  In that case, we end up
relying on the expression *mp |= v << ms shifting the bits on the
subsequent limb all the way to zero, but that is not how real hardware
works when the shift count equals the width of the type. This is
undefined behavior and does, in fact, produce the wrong result.

Instead, change the test for limb shift to (ms < 0), meaning that we
defer the advance to the next limb until we actually need it. At that
point, change the shift into the *old* limb to have a cast to
(fp_2limb) which means the shift right of LIMB_BITS is valid and
produces a zero value as expected.

Reported-by: Brooks Moses <bmoses@google.com>
Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2018-11-26 14:17:40 -08:00
H. Peter Anvin
da79432255 BR 3392529: remove excess printf() argument
The input file is provided by nasm_error(), we should not include it
in the printf list (compiler warning + wrong message.)

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2018-11-26 14:15:46 -08:00
Cyrill Gorcunov
295b795d71 preproc: Use error helpers
Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com>
2018-11-25 13:09:53 +03:00
Cyrill Gorcunov
00526d9845 assemble: Use nasm_ error helpers
Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com>
2018-11-25 01:45:49 +03:00
Cyrill Gorcunov
c3527dd6b2 error: Cover all levels with helpers
Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com>
2018-11-25 01:15:51 +03:00
Cyrill Gorcunov
3351072306 error: Style liftup
Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com>
2018-11-24 18:58:11 +03:00
Cyrill Gorcunov
8c0666b0e6 preproc: Pass include paths as strlist
Instead of copying data just reuse already
allocated paths.

Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com>
2018-11-24 14:33:48 +03:00
Cyrill Gorcunov
b7bb5acdaf strlist: Rework to drop type
Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com>
2018-11-24 13:24:09 +03:00
Cyrill Gorcunov
8e0acaad66 Pass input filename into error message
Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com>
2018-11-24 12:12:15 +03:00
Cyrill Gorcunov
f5a48a6ba2 Fix sigsegv if two outputs specified
outname is only set up by pass two so
earlier access may lead to sigsegv.

Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com>
2018-11-24 12:02:13 +03:00
Cyrill Gorcunov
744100dc14 Merge branch 'nasm-2.14.xx'
* nasm-2.14.xx:
  Fix undefined behavior when shifting left by 32 bits
  BR 3392529: if the default output name is the same as input -> nasm.out
2018-11-23 23:52:11 +03:00
Michael Bradshaw
fd14310469 Fix undefined behavior when shifting left by 32 bits
See https://bugzilla.nasm.us/show_bug.cgi?id=3392368

Signed-off-by: Michael Bradshaw <mjbshaw@google.com>
Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com>
2018-11-23 23:51:16 +03:00
H. Peter Anvin (Intel)
7b6371b9d3 BR 3392529: if the default output name is the same as input -> nasm.out
If no output filename is specified, then a default filename is used
based on the input filename. If that ends up the *same* as the input
filename, change the output filename to "nasm.out".

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2018-11-20 10:56:57 -08:00
Cyrill Gorcunov
3079f7966d preproc: Fix malformed parameter count
readnum returns 64bit number which may become
a negative integer upon conversion which in
turn lead to out of bound array access.

Fix it by explicit conversion with bounds check

 | POC6:2: error: parameter count `2222222222' is out of bounds [0; 2147483647]

https://bugzilla.nasm.us/show_bug.cgi?id=3392528

Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com>
2018-11-14 10:26:26 +03:00
Cyrill Gorcunov
feabd742a3 preproc_init: Just clean include path
It is more natural to keep include path valid
during the whole lifetime.

Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com>
2018-11-13 01:23:47 +03:00
Cyrill Gorcunov
e358851526 Merge branch 'nasm-2.14.xx'
* nasm-2.14.xx:
  preproc: command-line preproc directive after system-generated

gorcunov@: Had to fix include_path StrList conversion,
it is a bit ugly by now, will rework.

Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com>
2018-11-13 01:09:27 +03:00
Cyrill Gorcunov
0135a8147e Merge nasm-2.14
* commit '9a1216a1efa0ccb48e5df97acc763ea3de71e0ce':
  NASM 2.14
  nasmdoc.src: fix compound word
  doc: Add a description for a useful case of mangling symbols
  preproc: Don't access out of bound data on malformed input
  rdstrnum: Make sure we dont shift out of bound
  preproc: Fix out of bound access on malformed input
  doc: Clarify %include search directory semantics

Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com>
2018-11-12 23:21:43 +03:00
H. Peter Anvin
bf6230baa9 preproc: command-line preproc directive after system-generated
BR 3392527: make sure that all command-line specified preprocessing
directives are processed after the system-generated ones. In
particular __OUTPUT_FORMAT__ was generated after command line pass 2,
at which point -p, -d, -u, --pragma and --before had already been
processed.

There is no reason to split up defined_macros() anymore: the right
place to execute it is simply between command line passes 1 and 2. We
can also set dfmt here, which lets us define a __DEBUG_FORMAT__ macro
as well.

Finally move some options that have no business being processed in
pass 2 to pass 1.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2018-11-11 13:36:13 -08:00
Cyrill Gorcunov
4b5b737d49 preproc: Don't access out of bound data on malformed input
There are a number of places still where we test text
data which is potentially may be an empty string. This
is known to happen on fuzzer input but usually doesn't
take place in regular valid programs. Surely we need
to revisit preprocessor code for this kind of errors.

https://bugzilla.nasm.us/show_bug.cgi?id=3392525

Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com>
2018-10-29 22:54:34 +03:00
Cyrill Gorcunov
b756372b06 rdstrnum: Make sure we dont shift out of bound
Otherwise we may hit underfined behavior.

https://bugzilla.nasm.us/show_bug.cgi?id=3392526

Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com>
2018-10-29 22:25:16 +03:00
Cyrill Gorcunov
661f723d39 preproc: Fix out of bound access on malformed input
A fuzzer revealed a problem in preproc code.

https://bugzilla.nasm.us/show_bug.cgi?id=3392521

Reported-by: ganshuitao <ganshuitao@gmail.com>
Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com>
2018-10-28 20:39:34 +03:00
H. Peter Anvin (Intel)
f7106d06e4 strlist: use a hash table
Use a hash table to enforce uniqueness in a string list. It is still
an ordered list, however, and can be walked in insertion order.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2018-10-25 12:33:58 -07:00
Cyrill Gorcunov
eb82f99367 Merge branch 'nasm-2.14.xx'
* nasm-2.14.xx:
  preproc: Make the preprocessor use nasm_catfile for include
  path: nasm_catfile -- Fix incorrect return of filename only
2018-10-21 15:06:36 +03:00
night199uk
fdb1a1b151 preproc: Make the preprocessor use nasm_catfile for include
All include paths to nasm must already have a trailing separator
prefix which is uncommon among tools. Change to using nasm_catfile
which gives a more normal behaviour.

https://bugzilla.nasm.us/show_bug.cgi?id=3392205

Signed-off-by: night199uk <night199uk@hermitcrabslab.com>
Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com>
2018-10-19 01:00:11 +03:00
Cyrill Gorcunov
f7b44f6092 Merge branch 'nasm-2.14.xx'
* nasm-2.14.xx: (83 commits)
  NASM 2.14rc16
  doc: Update changes
  preproc: expand_smacro -- Fix nil dereference on error path
  eval: Eliminate division by zero
  doc: Update changes
  opflags: Convert is_class and is_reg_class to helpers
  preproc: Fix out of range access in expand mmacro
  doc: Update changes
  parser: Fix sigsegv on certain equ instruction parsing
  labels: Make sure nil label is never passed
  labels: Don't nil dereference if no label provided
  macho: Add warning message in macho_output()
  macho/reloc: Fix addr size sensitive conditions
  macho/reloc: Fix macho_output() to get the offset adjustments by add_reloc()
  macho/reloc: Fixed offset adjustment in add_reloc()
  macho/reloc: Allow absolute relocation when forcing a symbol reference
  macho/reloc: Adjust SUB relocation information
  macho/reloc: Fixed in handling GOT/GOTLOAD/TLV relocations
  macho/reloc: Simplified relocation for REL/BRANCH
  macho/sym: Record initial symbol number always
  ...

Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com>
2018-10-15 22:58:13 +03:00
Cyrill Gorcunov
980dd658b5 preproc: expand_smacro -- Fix nil dereference on error path
When error happened earlier we might have a.mac already
handled and set to nil.

https://bugzilla.nasm.us/show_bug.cgi?id=3392508

Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com>
2018-10-14 19:25:32 +03:00
Cyrill Gorcunov
ceec0d8187 eval: Eliminate division by zero
When doing division we should detect if the value we're
divided by is not zero. Instead of is_unknown() helper
we should use is_just_unknown().

https://bugzilla.nasm.us/show_bug.cgi?id=3392515
https://bugzilla.nasm.us/show_bug.cgi?id=3392473

Reported-by: Jun <jxx13@psu.edu>
Reported-by: stuartly <situlingyun@gmail.com>
Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com>
2018-10-14 01:49:43 +03:00
Cyrill Gorcunov
67f2ca2b3f preproc: Fix out of range access in expand mmacro
On specially crafetd malformed input file the params
might be zapped (say due to invalid syntax) so we might
access out of bound having nil dereference in best case.

Note the later code in this helper uses tok_isnt_ helper
which already has similar check.

https://bugzilla.nasm.us/show_bug.cgi?id=3392518

Reported-by: Jordan Zebor <j.zebor@f5.com>
Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com>
2018-10-13 19:41:01 +03:00
Cyrill Gorcunov
a28c40d546 parser: Fix sigsegv on certain equ instruction parsing
We should check for bounds when accessing nasm_reg_flags.
Seems this bug was for long time already.

https://bugzilla.nasm.us/show_bug.cgi?id=3392516

Reported-by: Jordan Zebor <j.zebor@f5.com>
Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com>
2018-10-13 18:10:30 +03:00
Cyrill Gorcunov
8e740c6773 labels: Make sure nil label is never passed
We already catched a case where we've missed
test for non nil label and in result got sigsegv,
lets rather panic next time.

Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com>
2018-10-13 17:18:05 +03:00
Cyrill Gorcunov
e996d28c70 labels: Don't nil dereference if no label provided
An equ without label may cause nil dereference

 |	equ 0x100

Fixes 98578071b9

Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com>
2018-10-13 16:18:16 +03:00
Cyrill Gorcunov
c713b5f994 preproc: parse_size -- Check for string provided
In case if the string is nil we will have sigsegv.

https://bugzilla.nasm.us/show_bug.cgi?id=3392507

Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com>
2018-09-29 14:31:26 +03:00
Cyrill Gorcunov
da3780dc22 Fix small typo in comment
Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com>
2018-09-22 14:10:36 +03:00
Cyrill Gorcunov
69bb052e00 asm: Restore showing output for preproc mode in stdout
Before the commit 81b62b9f54
we've been always putting -E,-e results into stdout if no
output file provded. So bring this backward compatibility
back.

Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com>
2018-09-22 13:46:45 +03:00
Chang S. Bae
a578634b61 optimization: Introduce new flag to turn-off selectively
While configuring optimization in a level is conventional,
a certain optimization tends to conflict with some pragma.

For example, jump match conflicts with Mach-O's
"subsections-via-symbols" macro.

This configurability will workaround such conflicts.

Signed-off-by: Chang S. Bae <chang.seok.bae@intel.com>
2018-08-16 00:01:31 +03:00
H. Peter Anvin
af59af466a float: fix bogus pass flags for errors and warnings
Formatting errors -- syntax errors -- are errors, no matter which pass
they end up in.  ERR_PASS1 is just plain crazy: if we end up with a
formatting error on the code-generation pass, we are in a world of
hurt.

Defer warnings to the code-generation pass; that's the pass which
matters value-wise, and that way we get the warnings in the list file,
too.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2018-07-06 03:11:52 -07:00
Adam Majer
c7c28357c8 asm/float.c: fix buffer underflow in float parsing
When we suffer an underflow that cross limb boundaries, it is possible
to end up with a stack underflow.  Put in an explicit check for this
case (the mantissa will be zero in this case.)

   https://bugzilla.nasm.us/show_bug.cgi?id=3392445

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
diff --git a/asm/float.c b/asm/float.c
index dcf69fea..2965d3db 100644
--- a/asm/float.c
+++ b/asm/float.c
@@ -608,6 +608,8 @@ static void ieee_shr(fp_limb *mant, int i)
         if (offs)
             for (j = MANT_LIMBS-1; j >= offs; j--)
                 mant[j] = mant[j-offs];
+    } else if (MANT_LIMBS-1-offs < 0) {
+        j = MANT_LIMBS-1;
     } else {
         n = mant[MANT_LIMBS-1-offs] >> sr;
         for (j = MANT_LIMBS-1; j > offs; j--) {
2018-07-06 03:08:15 -07:00
Cyrill Gorcunov
70d429676b labels: Add missing backend type for extern symbols
Typo in 98578071b9

https://bugzilla.nasm.us/show_bug.cgi?id=3392494

Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com>
2018-07-01 02:00:12 +03:00
Cyrill Gorcunov
a8e3d6a836 labels: Shrink declare_label
No need to pass unused 'created', find_label can
handle nil here.

Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com>
2018-06-30 20:02:24 +03:00
H. Peter Anvin (Intel)
e8ceba5979 assemble: defer MERR_OPSIZEMISSING
In order for the machinery that deduces memory operand sizes when they
are not provided to work correctly, we need to make sure that
MERR_OPSIZEMISSING is only issued by matches() as the last resort;
that way all other error conditions will have been filtered out and we
know at the very end if we have exactly one option left.

This is a partial revert of cd26fccab4,
but does not affect the functionality introduced by that patch.

Reported-by: Cyrill Gorcunov <gorcunov@gmail.com>
Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2018-06-28 02:22:55 -07:00