Commit graph

352 commits

Author SHA1 Message Date
H. Peter Anvin
b13df02490 outmac: allow section alignment to be declared more than once
Allow section alignment to be declared more than once, with different
values.  The strictest alignment value via either a section or
sectalign directive becomes the controlling parameter.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2016-02-12 03:23:25 -08:00
H. Peter Anvin
7dcd1a1549 outmac: default to bits 32 for macho32 output format
Default to 32 bits for macho32; default to 64 bits for macho64.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2016-02-12 03:16:02 -08:00
H. Peter Anvin
09b56b421a outmac: move the format definitions down to the format-specific code
Move the format definitions down the to where the format-specific
initialization code lives.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2016-02-12 03:13:00 -08:00
H. Peter Anvin
c635497870 Merge the macho32 and macho64 (outmac32/64) backends
Merge the two Mach-O backends for cleanliness and maintainability.
This should also make the recent fixes to MachO-64 available in
MachO-32.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2016-02-12 03:04:41 -08:00
H. Peter Anvin
67db8184b1 macho64: remove -64 suffix from static variables
When the macho64 backend was forked, instead of fixing variables which
ought to have been static all along, the porter added a -64 suffix to
prevent namespace conflict.  Fix it by making those variables static.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2016-02-12 01:23:53 -08:00
H. Peter Anvin
797dc4f01e macho: make a bunch of global variables static
These variables should never have been anything but static in the
first place.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2016-02-12 01:21:15 -08:00
H. Peter Anvin
33814133bd Replace global maxbits variable with field in struct ofmt
maxbits is strictly a property of the output format, so get rid of
this ad hoc method of reporting.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2016-02-11 20:40:07 -08:00
H. Peter Anvin
f30d96d1c0 outmac64.c: Fix memory clobber bug, clean up memory free
Fix an array that was way too small resulting in memory overwrite
errors, and free a few more dynamic data structures.

Signed-off-by: H. Peter Anvin <hpa@linux.intel.com>
2016-02-11 20:20:10 -08:00
H. Peter Anvin
97577e82d7 macho64: remove LC_DATA_IN_CODE, change reloc type to 1
Change the relocation type to SIGNED instead of BRANCH, this is
probably more correct.

Signed-off-by: H. Peter Anvin <hpa@linux.intel.com>
2016-02-11 19:25:33 -08:00
H. Peter Anvin
5eebc6bc2b macho64: fix alignment problems, add LC_DATA_IN_CODE
Hopefully actually fix the issues with alignment this time.
Avoid a linear search of segments for each symbol emitted.
Issue an empty LC_DATA_IN_CODE command since that seems to be
expected.

With this, ffmpeg builds but still crashes on startup, which seems
very strange.

Signed-off-by: H. Peter Anvin <hpa@linux.intel.com>
2016-02-11 17:51:37 -08:00
H. Peter Anvin
ae01785d87 LLVM portability fix for abs(size)
size is actually an uint64_t, and LLVM drops the abs() on the
principle that the uint64_t should always be positive.  Make it
explicit that we are converting to a signed integer first, by using
abs((int)size) instead.

Signed-off-by: H. Peter Anvin <hpa@linux.intel.com>
2016-02-05 11:34:07 -08:00
H. Peter Anvin
5482a15e17 codeview.c: remove superfluous pointer test
A pointer to a field in a structure can never be NULL so remove
the test.  There is no reason to test the field for an empty string
either, since we immediately thereafter do a strcmp().

Signed-off-by: H. Peter Anvin <hpa@linux.intel.com>
2016-02-05 11:31:46 -08:00
H. Peter Anvin
def81d5c71 outmac64: Fix the handling of section data alignment
Fix a number of places which failed to account for padding to
alignment of section data.  Also, fix confusion between aligning
section data in the linkable object code and final address alignment;
LLVM/Xcode always align section data to 4 bytes (with a comment saying
that it probably should be 8 bytes for 64-bit output) independent of the
section alignment.

This will need to be ported to the macho32 backend, as well; ideally
the two should be merged as they are really very similar and we don't
want to have these kinds of cross-porting problems.

Signed-off-by: H. Peter Anvin <hpa@linux.intel.com>
2016-02-05 11:19:29 -08:00
Jim Kukunas
3115e789d6 coff: Add support for the Codeview 8 debug format
Codeview is a debug format for win32/win64 PE/COFF files. It adds two sections,
.debug$S (symbols) and .debug$T (types), to the generated object file. These
sections are then used by the linker to generate a PDB file which can be used
by various debuggers (WinDbg, Visual Studio, etc).

Signed-off-by: Jim Kukunas <james.t.kukunas@linux.intel.com>
Acked-by: H. Peter Anvin <hpa@zytor.com>
Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com>
2016-01-24 14:14:39 +03:00
Cyrill Gorcunov
8aa9c2eb91 output: macho64 -- Fix OUT_REL4ADR on bigendians
We're converting address value into bigendian
(on BE machine) and then continue doing arithmetics
on top, which is of course incorrect.

Instead do all operations first then convert
to BE and write it into image.

Reported-by: "H. Peter Anvin" <hpa@zytor.com>
Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com>
2015-11-05 00:43:29 +03:00
Mark Scott
be8a5c8f2d out: elf,stabs -- Fix @n_value attribute for HdrSym entry
Each stabs entry is 12 bytes in size, for some reason we've
been pasing wrong attribute here in @n_value.

Signed-off-by: Mark Scott <nasm@mscott.cx>
Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com>
2015-11-03 01:14:14 +03:00
Cyrill Gorcunov
94b4a2c819 output: aout -- Use nasm_x_space helpers
Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com>
2015-07-26 16:42:27 +03:00
Martin Storsjö
561242bd43 out: maco64 -- Properly update the full 64 bit value when adding relocations
Previously only the first byte was updated (since @mydata
is a an uint8_t[]).

Signed-off-by: Martin Storsjö <martin@martin.st>
Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com>
2015-07-24 00:45:59 +03:00
Cyrill Gorcunov
6f05b8008c output: outmacho32 -- Fix stack overwrite
The size of address migh be up to 8 bytes here
so allocate enough stack space.

http://bugzilla.nasm.us/show_bug.cgi?id=3392317

Reported-by: Kyle Brodie <kylecbrodie@gmail.com>
Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com>
2015-07-21 22:51:34 +03:00
Cyrill Gorcunov
bd628dedea output: outmach64 -- Use back WRITELONG for OUT_REL4ADR
Because of 74a08cc3f we no longer need to write all
8 bytes here, revert it back as it were before
5b730a197

Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com>
2015-07-19 14:24:32 +03:00
Cyrill Gorcunov
2e6297ff1b out: Zeroify temp buffer before use
This is a a buffer on stack big enough to hold
bigger object we might need (address, number and
etc) but it's defined as an array of bytes and
we treat it as different types depending on context,
which may lead to situation where data from stack
been treated as meaningful.

In particular in commit 5b730a197 we've fixed such
problem simply using a "big" write to zeroify stack
data before use.

Lets simply zeroify this buffer explicitly to escape
such problems in future.

Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com>
2015-07-19 14:24:26 +03:00
Delan Azabani
5b730a197a out: maco64 -- Fix erroneously small write for OUT_REL4ADR
Ensure that the int64_t offset value, which ultimately comes from an
int64_t value in gencode() (assemble.c:1906), is completely written to
the temporary buffer, instead of merely its least significant 32 bits.

Prior to this change, WRITELONG was used instead of WRITEDLONG, which
resulted in add_reloc being passed an int64_t "reloff" whose least
significant 32 bits were those from the aforementioned offset value,
and whose most significant 32 bits were stack garbage from "mydata".

This led to get_closest_section_symbol_by_offset() attempting to search
for extremely large values of "offset" among the symbols in "syms",
which meant that the last symbol with a matching section number would
always win the symbol search.

In effect, this clobbered the resultant relocation information, such
that all entries would be resolved with the same symbol.

Test output can be found here

https://www.azabani.com/patch/2/output.txt

This patch fixes

http://bugzilla.nasm.us/show_bug.cgi?id=3392306

Signed-off-by: Delan Azabani <delan@azabani.com>
Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com>
2015-06-02 13:22:32 +03:00
Cyrill Gorcunov
4920a03243 output: outmac64 -- Fix the case when first hit matches the symbol
In case if we're looking up for a symbol and it's first
one in symbol table we might endup with error because of
using GE here (78f477b35f) ending cycle with @nearest = NULL.

http://bugzilla.nasm.us/show_bug.cgi?id=3392306

Reprted-by: Benjamin Randazzo <benjamin@linuxcrashing.org>
Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com>
2015-05-09 18:07:47 +03:00
Ed Maste
1f0cb0f2c1 output: elf64 -- increase .symtab and .rel* alignment to 8
Discovered while working on ELF Tool Chain elfcopy (strip),
which originally crashed on an assert while processing
a nasm-generated ELF object.

The .symtab and .rela.text sections report 4 byte alignment,
but require 8.

As an aside, see https://sourceforge.net/p/elftoolchain/tickets/485/ for a
discussion of the ELF Tool Chain issue that this bug exposed.

With my WIP elfcopy change and nasm-assembled jccolss2-64.o from libjpeg-turbo:

% strip -o /dev/null --strip-debug jccolss2-64.o
strip: section .symtab alignment 4 increased to 8
strip: section .rela.text alignment 4 increased to 8

http://bugzilla.nasm.us/show_bug.cgi?id=3392307

Signed-off-by: Ed Maste <emaste@freebsd.org>
Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com>
2015-04-16 01:02:55 +03:00
Cyrill Gorcunov
775153b1bf output: dbg,aout,elf32 -- Fix out for signed relocations
@size might be negative for signed relocations but its length
is abs value. This is rather a fix for future use because at
moment we can't hit this problems but better be on a safe side.

Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com>
2015-02-21 21:14:28 +03:00
Cyrill Gorcunov
d72bec12bc output: bin -- Fix section length miscalc for OUT_ADDRESS
While we using proper @asize variable for relocation itself
we miss the fact that @size variable (which might be negative
for signed relocations since fd52c277dd) is used to calculate
section size increment.

http://bugzilla.nasm.us/show_bug.cgi?id=3392299

Reported-by: Ben de Waal <ben@dewaals.com>
Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com>
2015-02-15 18:18:57 +03:00
Cyrill Gorcunov
d4923fc6a2 output: maco 64 -- Fix data conversion
Looks like were a typo in first place

http://bugzilla.nasm.us/show_bug.cgi?id=3392298

Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com>
2015-01-11 17:21:46 +03:00
Cyrill Gorcunov
78f477b35f output: maco 64 -- Fix get_closest_section_symbol_by_offset
- fixup comparision it should be GE
 - make sure we never return nil here

Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com>
2015-01-11 17:19:34 +03:00
H. Peter Anvin
d4184a0e0f Add missing static declarations in output/outobj.c
Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2014-11-25 12:14:52 -08:00
H. Peter Anvin
895ac7e53f Add missing static declaration in output/outdbg.c
Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2014-11-25 12:14:52 -08:00
H. Peter Anvin
443b8e9b5f Add missing static declaration in output/outieee.c
Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2014-11-25 12:14:52 -08:00
H. Peter Anvin
aa433d738c Add missing static declaration in output/outcoff.c
Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2014-11-25 12:14:52 -08:00
H. Peter Anvin
c0c67c67c8 Add missing header file in output/nulldbg.c
Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2014-11-25 12:14:52 -08:00
H. Peter Anvin
e76a626055 Replace unchecked fwrite() calls
Instead of having unchecked fwrite() calls, introduce nasm_write()
which does error checking (and fatal errors if the write fails).

Signed-off-by: H. Peter Anvin <hpa@linux.intel.com>
2014-10-21 12:50:47 -07:00
Cyrill Gorcunov
8a076f4260 output: elf -- Use common elf_ structures
Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com>
2014-09-21 13:05:03 +04:00
Cyrill Gorcunov
c24568b0b0 output: elf -- Move common structures into outelf.h header
All Elf formats we're supporting at the moment have
are using same structures, move them into a header
and name then with elf_ prefix.

This makes a few fields to carry 64 bit integers while
in former Elf32|x formats they can be 32 bit wide, but
I think it's acceptable tradeoff.

Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com>
2014-09-21 13:03:26 +04:00
Cyrill Gorcunov
500dd5461c output: elf -- Move elf_directive into single instance
For all Elf32/32x/64 this routine is the same. So lets
merge it into one instance.

Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com>
2014-08-29 20:30:57 +04:00
Cyrill Gorcunov
9b76c802ae output: elf -- Rename section_attrib to elf_section_attrib
Global section_attrib name is too general.

Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com>
2014-08-29 20:24:24 +04:00
Cyrill Gorcunov
6ebe3bcd2e output: outbin -- Fix relocation size
In commit fd52c277dd
we've started handling negative address relocations
but support Elf format only and got a typo in bin
format which leaded to endless cycle of applying
relocation due to negative argument (make test
hangs).

Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com>
2014-06-28 22:31:06 +04:00
H. Peter Anvin
49de44e56d Merge remote-tracking branch 'origin/signrel' 2014-05-09 15:09:43 -07:00
H. Peter Anvin
0ace62cb6a outelf: Error out on "section align" without value
If someone specifies "section align" without =value, error out.

Reported-by: Ilya Albrekht <ilya.albrekht@gmail.com>
Signed-off-by: H. Peter Anvin <hpa@linux.intel.com>
2014-02-18 13:30:44 -08:00
Cyrill Gorcunov
d0293d3392 BR3392274: output: Elf -- Don't crash on erronious syntax
Elf align section attribute requires syntax "align=value",
but in case if '=' is missed we pass nil pointer into
atoi function which cause libc to crash.

Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com>
2014-02-15 18:40:12 +04:00
H. Peter Anvin
fd52c277dd output: Allow OUT_ADDRESS with a negative size to mean signed relocation
This only matters for ELF64/ELFx32, at least for now.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2013-11-26 18:12:39 -08:00
Marat Dukhan
29227125f0 coff: Better handling of section redefinition
Currently, if we try to define an already defined section and specify
section flags, NASM will output "warning: section attributes ignored
on redeclaration of section %SECTIONNAME%".

The patch modifies this behaviour:

1. If the previous section definition differs only in alignment flags,
   no warning is generated

2. If the new definition implies larger alignment, it overrides the
   previous section alignment

3. If the new definition specifies any section alignment, the content of
   the section will be aligned on the new boundary (i.e. the effect is the
   same as if there was ALIGN macro)

Signed-off-by: Marat Dukhan <maratek@gmail.com>
Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com>
2013-10-03 16:55:50 +04:00
Marat Dukhan
91c43d789a coff: Support for section names longer than 8 bytes
http://bugzilla.nasm.us/show_bug.cgi?id=3392233

Signed-off-by: Marat Dukhan <maratek@gmail.com>
Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com>
2013-09-21 13:09:03 +04:00
Philipp Kloke
91a2f99346 Fixed wrong format specifier in format string
Signed-off-by: Philipp Kloke <philipp.kloke@web.de>
Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com>
2013-04-01 02:11:51 +04:00
Philipp Kloke
c51a224ceb Remove unnecessary calls to memset
The C standard guarantees that strncpy pads
the string with zeros if source string is smaller
than destination buffer.

Signed-off-by: Philipp Kloke <philipp.kloke@web.de>
Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com>
2013-04-01 02:11:01 +04:00
Andrew Nayenko
57162eb084 Fix warnings generated by clang 3.0
Fix warnings like this:

output/outelf32.c:2120:33: warning: equality comparison with extraneous
      parentheses [-Wparentheses-equality]
            if ((match->section == index)) {
                 ~~~~~~~~~~~~~~~^~~~~~~~
output/outelf32.c:2120:33: note: remove extraneous parentheses around the
      comparison to silence this warning
            if ((match->section == index)) {
                ~               ^       ~
output/outelf32.c:2120:33: note: use '=' to turn this equality comparison into
      an assignment
            if ((match->section == index)) {
                                ^~
                                =
1 warning generated.

Signed-off-by: Andrew Nayenko <resver@gmail.com>
Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com>
2013-02-25 01:08:00 +04:00
Keith Kanios
0863bc386b BR3392232: Fix relocations in MachO64
Signed-off-by: Keith Kanios <keith@kanios.net>
Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com>
2012-11-28 20:05:20 +04:00
Cyrill Gorcunov
5fa17e8398 output: Add more Elf unification
One day the elf output routines would be abstracted
enough to be merged in one file. This patch simply
removes some differences from elf32/64 code.

Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com>
2012-11-16 22:57:07 +04:00