Greg Ungerer [Fri, 19 Aug 2016 13:49:51 +0000 (23:49 +1000)]
elf2flt: fix relocation support for R_ARM_TARGET types
R_ARM_TARGET1 (and I think R_ARM_TARGET2) relocation types should be
treated in the same way as R_ARM_ABS32. Fix them to write out the addend
to the flat binary in network byte order.
In output_relocs, we do "sprintf(&addstr[0], "+0x%lx", ...)", with addrstr
being a 16 bytes array.
On 64bits hosts, in the unlikely case the value overflows 32bits, the buffer
may overflow.
Indeed, the maximum theorical size is 20 bytes (16 bytes for the value + 3
bytes for "+0x" + the end of string marker).
The reason the value overflows 32bits is yet to be understood, as the ARMV7-M
is 32bits architecture, but this patch first ensure the sprintf call is robust
enough.
Signed-off-by: Maxime Coquelin <mcoquelin.stm32@gmail.com>
Greg Ungerer [Thu, 4 Oct 2012 02:32:38 +0000 (02:32 +0000)]
From: Larry Baker <baker@usgs.gov>
The _stack_start symbol needs to be in the same flatmem memory region
as text/data/bss, otherwise it will not end up with the correct address.
Direct the section into the flatmem region.
The GNU linker uses -v as a shortcut to --version, not --verbose. So atm,
if you run `ld -v` to get the linker version, ld-elf2flt throws out a lot
of verbose debugging information. So drop the -v checking in ld-elf2flt
to keep from breaking systems that parse the linker version.
David McCullough [Thu, 16 Dec 2010 01:37:41 +0000 (01:37 +0000)]
The .note.ABI-tag section exists to indicate to other projects (like gdb
or library loaders) information about the target OS. It doesn't actually
contain anything that is used at runtime. So while the current linker
script gathers this into the .data section, the final FLAT doesn't include
anything from it. But tools expect to find a dedicated section in ELFs
which the current section merge prevents.^M
So give .note.ABI-tag its own output section so gdb can locate and use it.
This shouldn't change the FLAT files produced in any way.
David McCullough [Tue, 17 Aug 2010 04:25:26 +0000 (04:25 +0000)]
When we converted ld-elf2flt from the shell script to C, one small nuance
was missed: argv[0] contains the full path only when invoked with the full
path. This is not the same behavior for shell scripts as $0 is always the
full path to the script in question. Most of the time this isn't an issue
as gcc will invoke all of its tools (like the linker) with a full relative
path to itself. However, if we attempt to invoke the linker directly, we
can see misbehavior such as:
bfin-uclinux-ld.real: cannot open linker script file ./../lib/elf2flt.ld:
No such file or directory
So, to fix this, we lean on more libiberty functions. Specifically, the
make_relative_prefix() function. This function locates a full argv[0] by
scanning $PATH to see where it was invoked. This might sound a little
dodgy, but this is fundamental to how gcc and binutils implement support
for their runtime relocation, so it can't break ld-elf2flt without first
breaking every one else ;).
In the fall out of this fix, we can cull a bunch of local code that does
custom path parsing. So not only do we get to fix an annoying bug, we get
to shrink code in the process.
Signed-off-by: Steve Kilbane <steve@whitecrow.demon.co.uk> Signed-off-by: Mike Frysinger <vapier@gentoo.org>
David McCullough [Tue, 22 Jun 2010 06:12:47 +0000 (06:12 +0000)]
The current code misses checking a few args in order to determine the
default "print" mode (ktrace/l1stack/...). Rather than update a list
that people easily forget, rework the code to generically detect that
no arguments have been specified.
The bflt loader in the kernel will, however, add a small extra data table
just before .data's content (cf. handling of MAX_SHARED_LIBS in
binfmt_flat.c:load_flat_file).
Now, if .text and .data are in the same segment, directly following each
other in the binary file, but have that extra data table added in the
run-time memory layout, GDB will get very confused when trying to access
items in the now-moved .data section. Without any kernel (loader) / GDB
changes, the solution is to tell the linker to always put .text and .data
into separate segments, which GDB will handle gracefully then.
Poor getopt() implementations as found in many BSD/Darwin systems will
stop processing options after a non-option is encountered. That means
ld-elf2flt has to be careful to not stick options after non-options when
executing sub children. In a default setup, it will invoke `elf2flt` with
the output followed by the -a option which subsequently fails:
elf2flt: Can't open '-a': No such file or directory
David McCullough [Tue, 14 Jul 2009 23:00:33 +0000 (23:00 +0000)]
the "all" target should not be depending on "ld-elf2flt"
anymore as this is handled through the PROG vars. it isnt a problem
for Linux systems, but when EXEEXT is set, things go boom.
David McCullough [Sun, 12 Jul 2009 23:28:58 +0000 (23:28 +0000)]
Due to shell portability issues (which is to say shell scripts are not
portable -- think Windows), convert elf2flt to C code.
I've updated this code base to the latest elf2flt tree and actually done
some basic tests -- building the three Blackfin tuples (ELF, FLAT, FDPIC)
and running programs on my Blackfin boards. This process found errors in
the original implementation as well as some of the cleanups I did.
Signed-off-by: Nathan Sidwell <nathan@codesourcery.com> Signed-off-by: Mike Frysinger <vapier@gentoo.org>
Unify the duplicated windows and other system fallback logic in stubs.h
and add some fatal() helper functions to standardize the error output when
falling over. This way we don't end up with obscure error messages with
no idea what util they are coming from.
This cleans up the Makefile handling of the different compiler flags such
that it uses standard names across the board as well as unifies the link
method.
Rather than putting the `rm` at the end of the script before the normal
exit point, create a trap to automatically delete the script when exiting.
This way the linker script gets cleaned up whenever there is an error as
well. Otherwise every link invocation that ends in a failure could leave
behind crap. On my system, i found almost 2 million of these suckers in
my /tmp dir.
David McCullough [Sun, 24 May 2009 23:33:48 +0000 (23:33 +0000)]
When the relocs are larger than 16bits, incorrect values are written when
the .H/.L loading are reversed. Normally this wouldn't happen because the
gcc compiler always outputs in the same order (first hi, then lo).
Signed-off-by: Jie Zhang <jie.zhang@analog.com> Signed-off-by: Mike Frysinger <vapier@gentoo.org>
David McCullough [Sun, 17 May 2009 23:14:54 +0000 (23:14 +0000)]
The header logic for pulling in ELF defines mixes common ELF headers and
target specific headers. In the Blackfin case, we always want to pull in
the bfin.h since most of the time, the common ELF headers do not have our
relocation defines. This fixes building for mingw targets.
The FLAT structure is all built on 32bit types, so make sure the elf2flt
code uses 32bit types rather than long's. This way we get correct
behavior when the host sizeof(long) is not 32bit as is on all 64bit
systems nowadays.
Signed-off-by: Bernd Schmidt <bernds_cb1@t-online.de> Signed-off-by: Mike Frysinger <vapier@gentoo.org>
GCC properly provides CTOR/DTOR END/LIST symbols already via the normal
crtbegin and crtend objects. Since it's been doing this for a while, add
a configure option to control the manual list found in the elf2flt linker
script and default it to off now.
Often build systems will use -shared when creating shared libraries, so
don't barf on the flag when we see it. This makes porting efforts easier.
As a sanity check, make sure we see the -shared-lib-id flag when creating
a shared library.
Signed-off-by: Bernd Schmidt <bernds_cb1@t-online.de> Signed-off-by: Mike Frysinger <vapier@gentoo.org>
Some arches (like the Blackfin architecture) have an ABI where all symbols
get a symbol prefix. In our case, it's an underscore ("_"). So we set the
symbol prefix in the configure script and output it in the linker script and
all relevant files.
David McCullough [Thu, 27 Nov 2008 12:20:48 +0000 (12:20 +0000)]
When compiled with CVS binutils, busybox fails to execute properly.
That's because it's using --gc-sections, and the elf2flt linker script
allows the linker to remove parts of the .init and .fini sections. This
patch fixes it.
David McCullough [Thu, 27 Nov 2008 01:08:04 +0000 (01:08 +0000)]
Kazu Hirata wrote:
> Attached is a patch to fix elf2flt.ld so that C++ programs will link
> successfully.
>
> Nathan Sidwell says:
> nathan@codesourcery.com
>
> "The problem is the .eh_frame_hdr optimization. firstly that input
> section was not mentioned in the linker script and secondly, the
> linker requires .eh_frame_hdr and .eh_frame section in the output,
> so it can optimize them. I move gcc_except_table out of .data, for
> consistency."
I've been trying to get CVS binutils up and running on the Blackfin, and
after debugging a C++ link failure I eventually tracked it down to the
elf2flt linker script, and google pointed me at this thread which I'd
completely forgotten in the meantime. The patch is needed, please apply
to elf2flt.
David McCullough [Fri, 21 Nov 2008 11:35:07 +0000 (11:35 +0000)]
It is somewhat common for people to use the -s (strip) option to the
compiler when linking. Currently this breaks FLAT generation. There's no
real reason that I can see that we would bother keeping this, so just filter
it out to make compatibility / ease of use nicer.
We should link against -lc before any of the binutils libs (libiberty/bfd)
as those libs provide replacements for some common functions we don't really
care about (like getopt). Otherwise, if the C library does something wacky
in its API (like renaming symbols), we get a desync where some symbols are
provided by the binutils libs while others are provided by the C library.
For a concrete example, try building elf2flt on OS X 10.5. Their stupid
headers do this in unistd.h:
when debugging a flat loader problem involving relocs, i found it very
useful to compare what the loader was trying to do with what relocs
were actually in the binary
to that end, i add a '-P' flag to flthdr that will dump all the reloc
information found in a flat binary.
David McCullough [Wed, 29 Oct 2008 11:38:34 +0000 (11:38 +0000)]
Reserve a flag for the blackfin target (FLAT_FLAG_L1STK), no other platform
will use it, but for now we have plenty of bits left and we can always
re-use it later if needed.
David McCullough [Thu, 13 Mar 2008 04:38:28 +0000 (04:38 +0000)]
I'm lazy and having to set 4 configure options when I could just set 1 is a
pain. So --with-binutils-build-dir allows you to set the base tree of the
compiled binutils directory and will setup reasonable defaults for the flags
(if they haven't already been set):
David McCullough [Wed, 27 Feb 2008 11:41:32 +0000 (11:41 +0000)]
This patch allows elf2flt/flthdr's compression options to work in a wider
variety of environments (e.g. under MinGW/Win32), by linking with zlib
rather than by using external gzip/gunzip executables. The cp binary
isn't used any more either, and libiberty's make_temp_file() is used
instead of mkstemp() as a more portable way of creating a temporary file.
Also the compression logic is made somewhat clearer, IMO.
Signed-off-by: Julian Brown <julian@codesourcery.com> Signed-off-by: Mike Frysinger <vapier@gentoo.org>
David McCullough [Sun, 24 Feb 2008 23:17:51 +0000 (23:17 +0000)]
When moving the xmalloc changes from the Blackfin elf2flt to the upstream
elf2flt repo, I accidentally dropped the libiberty.h include. Not a fatal
error, but having proper prototypes is always a good thing.
David McCullough [Thu, 21 Feb 2008 01:09:05 +0000 (01:09 +0000)]
I hit a random failure in elf2flt (not elf2flt's fault, I was screwing with
ELFs and LMAs/VMAs), but the resulting error message was useless:
malloc: Cannot allocate memory
Since I was executing `...-gcc` at the time, where exactly this error message
was coming from was hard to say. So instead of using malloc() and then doing
a simple 'perror("malloc"); exit(1);' in the elf2flt.c code, it'd be better
for everyone if we used the xmalloc() funcs from libiberty. We're already
linking elf2flt against libiberty, so there's no extra headers/libs to link
against.
Now the crash looks like:
bfin-uclinux-elf2flt: out of memory allocating 4221960244 bytes after a total of
135168 bytes
So much nicer! :)
This patch fixes two issues which are triggered when compiling for nios2:
* Typo in a goto label
* Misplaced switch statement which probably is left from an older revision.
David McCullough [Tue, 27 Nov 2007 22:51:30 +0000 (22:51 +0000)]
This patch is required to successfully link against libstdc++ on the Nios2
platform.
Signed-off-by: Atle Nissestad <atle@nissestad.no>
Also remove the duplicated data1 section and add the eh_frame_hdr section.
Source of these changes lost (sorry :-)
David McCullough [Thu, 11 Oct 2007 03:50:51 +0000 (03:50 +0000)]
the intl subdir in binutils may redirect some funcs with libintl_* prefixes
(like building for win32), so the attached patch adds a stub like the
existing one for dcgettect() ... this fixes building of elf2flt for me for
mingw targets
Greg Ungerer [Wed, 1 Aug 2007 03:40:12 +0000 (03:40 +0000)]
lf2flt crashes on Linux/amd64:
(gdb) run -a -o links -p links.gdb links.gdb
Starting program: /home/stsp/dslinux/toolchain/prefix/bin/arm-linux-elf-elf2flt -a -o links -p links.gdb links.gdb
Program received signal SIGSEGV, Segmentation fault.
_bfd_elf_canonicalize_reloc (abfd=<value optimized out>, section=0x5f6900,
relptr=0xffffffffa6360010, symbols=<value optimized out>)
at /home/stsp/dslinux/toolchain/src/binutils-2.17/bfd/elf.c:6367
6367 *relptr++ = tblptr++;
(gdb) bt
#0 _bfd_elf_canonicalize_reloc (abfd=<value optimized out>, section=0x5f6900,
relptr=0xffffffffa6360010, symbols=<value optimized out>)
at /home/stsp/dslinux/toolchain/src/binutils-2.17/bfd/elf.c:6367
#1 0x00000000004006dd in output_relocs (abs_bfd=0x5f5570,
symbols=0x2b30a5e99010, number_of_symbols=16585, n_relocs=0x7fff04c0fe58,
text=0x2b30a6102010 "", text_len=<value optimized out>, text_vma=0,
data=0x2b30a627b010 "", data_len=934480, data_vma=1541824,
rel_bfd=0x5f4400)
at /home/stsp/dslinux/toolchain/src/elf2flt-20051225/elf2flt.c:587
#2 0x0000000000401180 in main (argc=<value optimized out>,
argv=<value optimized out>)
at /home/stsp/dslinux/toolchain/src/elf2flt-20051225/elf2flt.c:1949
The problem seems to be that the one and only call to xmalloc()
in elf2flt.c does not return a valid pointer for some reason.
I'm wondering why xmalloc() is used exactly once in elf2flt.c.
All other heap allocations in elf2flt are done with plain malloc().
The attached patch fixes the segfault by replacing the call to xmalloc()
with a call to malloc(). It also makes elf2flt check for return
values of malloc() calls, providing the equivalent behaviour of
using xmalloc().
Patch submitted by Stefan Sperling <stsp@stsp.name>
David McCullough [Thu, 23 Nov 2006 22:42:07 +0000 (22:42 +0000)]
the install program is run with the '-s' option which makes `install` run the
host `strip` on the installed binary ... this certainly wont work when
cross-compiling and really, the standard is to leave stripping up to the
people doing the actual install
so attached patch simply drops the -s argument to install