Commit Graph

2305 Commits (560188711dc12a9d8b3a437069251096b37b1f82)
 

Author SHA1 Message Date
Thomas Preud'homme 2c38800bbe Allow to get sym attr and fail if no entry
Change alloc_sym_attr into get_sym_attr and add a parameter to control
whether to allocate a new symattr structure or return NULL if symbol is
not found;
2016-12-03 17:26:51 +00:00
Thomas Preud'homme a11b0a67e3 Consolidate GOT creation in build_got_entries
Currently GOT/PLT creation happens in two locations depending on whether
the GOT/PLT [entry] is required by the symbol or the relocation:

- bind_exe_dynsym for relocations to undefined symbol
- build_got_entries/put_got_entry for relocations that require a GOT/PLT
  entry

This commit consolidate GOT/PLT creation in build_got_entries by
reducing bind_exe_dynsym's job to create a dynamic symbol for undefined
symbols. build_got_entries then invoke put_got_entry if the symbol being
relocated is undefined or the relocation asks for a PLT or GOT [entry].
put_got_entry is also modified to only export a symbol in the dynamic
symbol table when we are in the case of PLT/GOT [entry] required by the
relocation (since undefined symbol are already exported by
bind_exe_dynsym).
2016-12-03 17:26:51 +00:00
Thomas Preud'homme 1c811a4d1d Make build_got_entries more target independent
Factor most of common logic between targets in build_got_entries by
defining target specific info into structures in the backends.
2016-12-03 17:26:51 +00:00
Thomas Preud'homme 523b55d82d Only create GOT or GOT entry when needed
Currently we always build a GOT when we recognize a relocation in
build_got_entries even if the relocation does not require one. In the
same spirit, when the relocation does require one we always create a GOT
entry even if not entry is necessary. This patch restricts the creation
of a GOT and a GOT entry to relocations that needs it, ie:
- do not create a GOT if relocation is not related to GOT and symbol is
  not UNDEF
- do not create a GOT entry if relocation only relates to beginning of
  GOT
2016-12-03 17:26:51 +00:00
Thomas Preud'homme 6cd23d1d8c Recognize more relocations as needing GOT/PLT entry 2016-12-03 17:26:51 +00:00
Thomas Preud'homme cb273fdad8 Do section relocation in architecture backend 2016-12-03 17:26:51 +00:00
Thomas Preud'homme 60374d01ae Add address of GOT + 8 in PLT + 16 and fix PLT0
On ARM targets, the jump to ld.so resolution routine is done in PLT0 by
loading the offset to the GOT found in PLT+16 and from there loading the
address in GOT+8 and jumping to it.

Currently tcc starts the first regular PLT entry at PLT+16 which thus
does not contain the offset to the GOT. This commit fixes that.

Note that calls via PLT still worked nonetheless because of some missing
dynamic tag which makes ld.so behaves as if RTLD_BIND_NOW was specified
in the environment for all executable created by tcc.
2016-12-03 17:26:51 +00:00
Thomas Preud'homme f924d0ca96 Improve put_got_entry doc and structure 2016-12-03 17:26:51 +00:00
Thomas Preud'homme 64b5ee2dea Rename add_elf_sym to set_elf_sym
add_elf_sym is a confusing name because it is not clear what the
function does compared to put_elf_sym. As a matter of fact, put_elf_sym
also adds a symbol in a symbol table. Besides, "add_elf_sym" fails to
convey that the function can be used to update a symbol (for instance
its value). "set_elf_sym" seems like a more appropriate name: it will
set a symbol to a given set of properties (value, size, etc.) and create
a new one if non exist for that name as one would expect.
2016-12-03 17:26:51 +00:00
Thomas Preud'homme 9e6610b0aa Improve comments for symbol export and binding 2016-12-03 17:26:51 +00:00
Thomas Preud'homme e9769a7249 Do not add symbol if it is already there
Do not create a new symbol in add_elf_sym if a symbol with same properties
(value, size, info, etc.) already exists. This prevents symbols from
being exported twice in the dynamic symbol table.
2016-12-03 17:26:51 +00:00
Thomas Preud'homme bf692af31b Fix error logic for undefined reference in library
Prior to this patch, an error would only be given when a library has an
unresolved undefined symbol if there is no undefined reference for the
same symbol in the executable itself. This patch changes the logic to
check both that the executable has the symbol in its static symbol table
*and* that it is defined to decide if the error path should be followed.
2016-12-03 17:26:51 +00:00
root ccf9ed7d54 Clear SHF_GROUP flag when linking
SHF_GROUP flag set on a section indicates that it is part of a section
group and that if the section is removed, the other sections in the same
group should be removed as well [1]. Since section group are guide for
the linking process, they do not have any meaning after linking has
occured. TCC rightfully [2] discard such sections (by not recognizing the
section type) but keeps the SHF_GROUP flag set on sections that were
part of a section group which confuses binutils (objdump and gdb at
least). Clearing that bit makes objdump and gdb accept binaries created
by TCC.

[1] https://docs.oracle.com/cd/E19683-01/816-1386/chapter7-26/index.html
[2] GNU ld does the same
2016-12-03 17:26:50 +00:00
Christian Jullien ed99f3608d Remove warning when __builtin_frame_address is used with gcc >= 6. 2016-11-30 06:18:48 +01:00
David Mertens d2e2f42382 Implement gcc bitfield algorithm; add -mms-bitfields 2016-11-28 09:01:12 -05:00
David Mertens 3c68a8c6c0 Minor grammar fixes to docs 2016-11-28 08:59:53 -05:00
grischka 0d14e7e698 configure: prefer lib64 on 64-bit platforms
use lib64 if
- "/usr/lib/multi-arch-triplet" does not work and
- we are on a 64-bit platform and
- lib64 exists and does contain crti.o
2016-11-28 14:48:54 +01:00
grischka a52a39179a tccelf: introduce add32/64le() 2016-11-20 14:52:56 +01:00
grischka 4a3741bf02 x86_64-asm: =m operand fixes
The problem was with tcctest.c:

    unsigned set;
    __asm__("btsl %1,%0" : "=m"(set) : "Ir"(20) : "cc");

when with tcc compiled with the HAVE_SELINUX option, run with
tcc -run, it would use large addresses far beyond the 32bits
range when tcc did not use the pc-relative mode for accessing
'set' in global data memory.  In fact the assembler did not
know about %rip at all.

Changes:
- memory operands use (%rax) not (%eax)
- conversion from VT_LLOCAL: use type VT_PTR
- support 'k' modifier
- support %rip register
- support X(%rip) pc-relative addresses

The test in tcctest.c is from Michael Matz.
2016-11-20 14:50:56 +01:00
Thomas Stalder 47fd807f9b arm: Fix relocate_section with TCC_OUTPUT_MEMORY 2016-11-13 11:52:28 +01:00
Thomas Stalder 4af25aed92 remove warnings 2016-11-13 11:45:55 +01:00
grischka 59216d3db0 tccgen: fix inline_functions double free fix 2016-11-11 20:25:13 +01:00
grischka 7c28c9b13f tccgen: inline_functions double free fix
Fix double free of the inline function token_string which
could happen when an error/longjmp occurred while compiling
the inline function.
2016-11-11 18:29:45 +01:00
Pavlas, Zdenek 7e7f2e5d1b bcheck: access fields of local structs w/o bcheck
Revert previous commit, this is probably a better fix.
2016-11-10 05:15:07 -08:00
Pavlas, Zdenek 550e861bf7 bcheck: add structs to local regions
int test()
{
  struct { int i; } s = { 42 };
  return s.i; // bound checked
}
2016-11-09 04:11:40 -08:00
Pavlas, Zdenek cdf715a0b5 i386 + bcheck: fix __bound_local_new
With -b, this produces garbage. Code to call __bound_local_new
is put at wrong place, overwriting the regparam setup code.
Fix copied from x86_64-gen.c.

void __attribute__((regparm(3)))
fun(int unused)
{
  char local[1];
}
2016-11-09 01:04:45 -08:00
grischka 3054a76249 i386-gen: use EBX as 4th register
May be enabled/disabled by changing this line:
    #define USE_EBX 1
2016-10-19 19:22:15 +02:00
grischka 02642bc94c lib/libtcc1.c: cleanup
- remove #include dependencies from libtcc1.c
  for easier cross compilation
- clear_cache only on ARM
- error-message for mprotect failure
2016-10-19 19:21:36 +02:00
grischka bfd1c08d6c tccrun/win64: cleanup runtime function table
- call RtlDeleteFunctionTable
  (important for multiple compilations)

- the RUNTIME_FUNCTION* is now at the beginning of the
  runtime memory.  Therefor when tcc_relocate is called
  with user memory, this should be done manually before
  it is free'd:
      RtlDeleteFunctionTable(*(void**)user_mem);
      [ free(user_mem); ]

- x86_64-gen.c: expand char/short return values to int
2016-10-19 19:21:27 +02:00
grischka 02919cd275 configure: --triplet= option, Makefile: cleanup 2016-10-17 23:24:10 +02:00
grischka 0be098929a tccpp_new/delete and other cleanups 2016-10-17 23:24:01 +02:00
Michael Matz 6fbcc14a5d system-hacks: define __GNUC__ for FreeBSD
FreeBSDs system headers contain unconditional usage of
macros like __aligned(x), which are only conditionally defined
in sys/cdefs.h (conditional on __GNUC__ or __INTEL_COMPILER).

Bug in FreeBSD, but as work-around we can define __GNUC__ which
picks up these defs.

[This also moves back the glibc defines we had before into the
non-BSD ifdef branch]
2016-10-17 01:08:29 +02:00
Michael Matz 68a7af632c x86-64: Fix long long bug
With the last improvements to lexpand it's now harmful
to use on native 64bit platforms when not necessary.  For gv_dup
it's not necessary there.  It can still be used with really
transforming a 64bit value into two 32bit ones.
2016-10-17 00:57:16 +02:00
grischka d9b7f018ce i386: do not 'lexpand' into registers necessarily
Previously, long longs were 'lexpand'ed into two registers
always.

Now, it expands
- constants into two constants (lo-part, hi-part)
- variables into two lvalues with offset+4 for the hi-part.

This makes long long operations look a bit nicer.

Also: don't apply i386 'inc/dec' optimization if carry
generation is wanted.
2016-10-16 19:04:40 +02:00
grischka 6245db9fca tccgen/32bits: fix unsigned long long -> int cast
gen_cast() failed to truncate long long's if they
were unsigned, which was causing mess on the vstack.

There was a similar bug here
    tccgen: 32bits: fix PTR +/- long long
    ed15cddacd

Both were not visible until this patch
    tccgen: arm/i386: save_reg_upstack
    b691585785

I'd still assume that this patch is correct per se.

Also:
- remove 2x !nocode_wanted (we are already under a general
  "else if (!nocode_wanted)" clause above).
2016-10-16 11:03:57 +02:00
Christian Jullien 4ac0e1971e Actual complete name for DragonFly is 'DragonFly BSD'. 2016-10-16 06:12:55 +02:00
Christian Jullien 06db384f8a No 'Thread Storage Local' on FreeBSD with tcc. 2016-10-15 18:47:31 +02:00
Christian Jullien f7bfa0970e Backslash was missing after reindentation to let code be < 80 col. 2016-10-15 18:13:01 +02:00
grischka f3c1ea6c2d #define __GNUC__ = 2.1
__GNUC__ nowadays as macro seems to mean the "GNU C dialect"
rather than the compiler itself.  See also

  http://gcc.gnu.org/ml/gcc/2008-07/msg00026.html

This patch will probably cause problems of various kinds but
maybe we should try nonetheless.
2016-10-15 16:01:16 +02:00
grischka 4d247c00a3 tccgen/tccelf: move code from libtcc.c 2016-10-15 15:55:31 +02:00
Christian Jullien 3e32479594 Add more OpenBSD support. 2016-10-15 15:08:44 +02:00
Christian Jullien 70dec93f2b OpenBSD does not support -v option in rm command. 2016-10-15 14:59:52 +02:00
Christian Jullien 36759ddacf Start support of OpenBSD, tcc compiler can be produced from source tree 2016-10-15 14:50:17 +02:00
Christian Jullien 18a5d8188a World is not reduced to either Windows or Linux 2016-10-15 09:47:08 +02:00
Michael Matz 383f568a64 Fix misleading indentation 2016-10-14 17:46:04 +02:00
Michael Matz 682ecc1745 arm64: Fix -run
With -run the call instruction and a defined function can be
far away, if the function is defined in the executable itself,
not in the to be compiled code.  So we always need PLT slots
for -run, not just for undefined symbols.
2016-10-14 17:32:10 +02:00
Michael Matz 7600b03f35 arm64: Support PREL32 relocation
A PC-relative 32bit value is stored.
2016-10-14 16:47:43 +02:00
Michael Matz c232af7ddb Support archives with 64 bit indices
Some systems started using SYM64 .a libraries, so start
supporting them.
2016-10-14 16:47:43 +02:00
grischka ed15cddacd tccgen: 32bits: fix PTR +/- long long
Previously in order to perform a ll+ll operation tcc
was trying to 'lexpand' PTR in gen_opl which did
not work well.  The case:


    int printf(const char *, ...);
    char t[] = "012345678";

    int main(void)
    {
        char *data = t;
        unsigned long long r = 4;
        unsigned a = 5;
        unsigned long long b = 12;

        *(unsigned*)(data + r) += a - b;

        printf("data %s\n", data);
        return 0;
    }
2016-10-13 19:21:43 +02:00
Christian Jullien 8986bc8af4 Use ISO C string functions instead of obsolete BSD ones that used to be in strings.h. It allows more systems -- i.e. Windows -- to use those tests 2016-10-12 06:18:38 +02:00