Hi,
I recently tried to build a few debian packages with tinycc, during this
i came across a shared library which is normally compiled as follows:
gcc -Wl,-soname,libfoobar.so.1 -o libfoobar.so.1.0.1 *.o
So i tried to implement an -soname option for tcc, the result is
attached. As can be seen below the soname seems to be correctly stored
in the ELF file.
$> cat foo.c
int foo(int a,int b){
return a + b;
}
$> cat bar.c
int bar(int a, int b){
return a * b;
}
$> tcc -fPIC -c foo.c
$> tcc -fPIC -c bar.c
$> tcc -shared -soname libfoobar.so.1 -o libfoobar.so.1.0.1 foo.o bar.o
$> objdump -p libfoobar.so.1.0.1 | grep SONAME
SONAME libfoobar.so.1
However when i try to link something with my shared library, it
segfaults.
$> cat main.c
int foo(int a,int b);
int bar(int a,int b);
int main(){
printf("%d\n",foo(5,5));
printf("%d\n",bar(5,5));
}
$> ln -s libfoobar.so.1.0.1 libfoobar.so.1
$> ln -s libfoobar.so.1 libfoobar.so
$> tcc -L. -lfoobar main.c -o test
$> /lib/ld-linux.so.2 --library-path . ./test
Segmentation fault.
If i create the shared library with gcc as shown in the beginning of my
mail it works as expected. This issue is unrelated to my patch, it also
segfaults with current mercurial tip. Is this because tcc doesn't
currently generate position independent code? But the documentation
states that tcc is able to generate dynamic librarys, so what i am doing
wrong?
Marc
--
Marc Andre Tanner >< http://www.brain-dump.org/ >< GPG key: CF7D56C0
diff -r 218d28dbcf0b tcc.c
--- a/tcc.c Fri Nov 16 12:16:14 2007 +0800
+++ b/tcc.c Sat Nov 24 00:02:41 2007 +0100
@@ -9339,8 +9339,8 @@ void help(void)
{
printf("tcc version " TCC_VERSION " - Tiny C Compiler - Copyright (C) 2001-2006 Fabrice Bellard\n"
"usage: tcc [-v] [-c] [-o outfile] [-Bdir] [-bench] [-Idir] [-Dsym[=val]] [-Usym]\n"
- " [-Wwarn] [-g] [-b] [-bt N] [-Ldir] [-llib] [-shared] [-static]\n"
- " [infile1 infile2...] [-run infile args...]\n"
+ " [-Wwarn] [-g] [-b] [-bt N] [-Ldir] [-llib] [-shared] [-soname name]\n"
+ " [-static] [infile1 infile2...] [-run infile args...]\n"
"\n"
"General options:\n"
" -v display current version\n"
@@ -9361,6 +9361,7 @@ void help(void)
" -Ldir add library path 'dir'\n"
" -llib link with dynamic or static library 'lib'\n"
" -shared generate a shared library\n"
+ " -soname set the inernal DT_SONAME field to the specified name\n"
" -static static linking\n"
" -rdynamic export all global symbols to dynamic linker\n"
" -r output relocatable .o file\n"
@@ -9398,6 +9399,7 @@ enum {
TCC_OPTION_c,
TCC_OPTION_static,
TCC_OPTION_shared,
+ TCC_OPTION_soname,
TCC_OPTION_o,
TCC_OPTION_r,
TCC_OPTION_Wl,
@@ -9434,6 +9436,7 @@ static const TCCOption tcc_options[] = {
{ "c", TCC_OPTION_c, 0 },
{ "static", TCC_OPTION_static, 0 },
{ "shared", TCC_OPTION_shared, 0 },
+ { "soname", TCC_OPTION_soname, TCC_OPTION_HAS_ARG },
{ "o", TCC_OPTION_o, TCC_OPTION_HAS_ARG },
{ "run", TCC_OPTION_run, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
{ "rdynamic", TCC_OPTION_rdynamic, 0 },
@@ -9607,6 +9610,9 @@ int parse_args(TCCState *s, int argc, ch
case TCC_OPTION_shared:
output_type = TCC_OUTPUT_DLL;
break;
+ case TCC_OPTION_soname:
+ s->soname = optarg;
+ break;
case TCC_OPTION_o:
multiple_files = 1;
outfile = optarg;
diff -r 218d28dbcf0b tcc.h
--- a/tcc.h Fri Nov 16 12:16:14 2007 +0800
+++ b/tcc.h Sat Nov 24 00:02:41 2007 +0100
@@ -367,6 +367,9 @@ struct TCCState {
/* if true, static linking is performed */
int static_link;
+
+ /* soname as specified on the command line (-soname) */
+ char *soname;
/* if true, all symbols are exported */
int rdynamic;
diff -r 218d28dbcf0b tccelf.c
--- a/tccelf.c Fri Nov 16 12:16:14 2007 +0800
+++ b/tccelf.c Sat Nov 24 00:02:41 2007 +0100
@@ -1312,8 +1312,11 @@ int tcc_output_file(TCCState *s1, const
}
/* XXX: currently, since we do not handle PIC code, we
must relocate the readonly segments */
- if (file_type == TCC_OUTPUT_DLL)
+ if (file_type == TCC_OUTPUT_DLL) {
+ if (s1->soname)
+ put_dt(dynamic, DT_SONAME, put_elf_str(dynstr, s1->soname));
put_dt(dynamic, DT_TEXTREL, 0);
+ }
/* add necessary space for other entries */
saved_dynamic_data_offset = dynamic->data_offset;
_______________________________________________
Tinycc-devel mailing list
[email protected]
http://lists.nongnu.org/mailman/listinfo/tinycc-devel