Skip to contentSkip to conversation

lib order fails for xeno-config --posix-ldflags on Blackfin

Xenomai-help Mike McTernan 2 participants 8 messages Feb 5, 2009 – Feb 9, 2009

lore
Share & tools
lore
#0 lore raw

Hi,

Checking xeno-config.in from 2.4.1, it has the following:

XENO_POSIX_LDFLAGS="-L${staging}${libdir} -lpthread_rt -lpthread -lrt @XENO_USER_APP_LDFLAGS@"

On Blackfin uClinux 2008R1.5 linking fails with -lpthread_rt and -lpthread in that order:

bfin-uclinux-gcc -I/usr/xenomai/include -I/usr/xenomai/include/posix -D_GNU_SOURCE -D_REENTRANT -D__XENO__ -o hello hello.o -Wl,@/usr/xenomai/lib/posix.wrappers -L/usr/xenomai/lib -lpthread_rt -lpthread -lrt /opt/uClinux/bfin-uclinux/bfin-uclinux/runtime/usr/lib/libpthread.a(mute x.os): In function `__pthread_once_fork_child':
libpthread/linuxthreads.old/mutex.c:357: undefined reference to ___wrap_pthread_cond_init' /opt/uClinux/bfin-uclinux/bfin-uclinux/runtime/usr/lib/libpthread.a(mute x.os): In function __pthread_once':
libpthread/linuxthreads.old/mutex.c:308: undefined reference to ___wrap_pthread_cond_wait' libpthread/linuxthreads.old/mutex.c:325: undefined reference to ___wrap_pthread_cond_broadcast' /opt/uClinux/bfin-uclinux/bfin-uclinux/runtime/usr/lib/libpthread.a(mute x.os): In function `pthread_once_cancelhandler':
libpthread/linuxthreads.old/mutex.c:281: undefined reference to `___wrap_pthread_cond_broadcast'
collect2: ld returned 1 exit status
make: *** [hello] Error 1

It looks like the uClinux pthread implementation makes use of its own functions and hence needs the library containing the wrappers specified after the pthread lib to enable resolving of references, as per the ld manual:

"Normally, an archive is searched only once in the order that it is specified on the command line. If a symbol in that archive is needed to resolve an undefined symbol referred to by an object in an archive that appears later on the command line, the linker would not be able to resolve that reference."

I'd have thought there are potentially some circular dependencies between the pthread and pthread_rt libs. Would it help portability to surround these options with -Wl,--start-group <libs> -Wl,--end-group? i.e.

XENO_POSIX_LDFLAGS="-L${staging}${libdir} -Wl,--start-group -lpthread_rt -lpthread -Wl,--end-group -lrt @XENO_USER_APP_LDFLAGS@"

I've done this locally and I can build the apps without re-ordering the libraries.

Regards,

Mike

#1 lore raw
Quoted 4 lines · Mike McTernan

Hi,

Checking xeno-config.in from 2.4.1, it has the following:

Please upgrade. The Xenomai/blackfin port is actively maintained, and bugs have been fixed since then.

Quoted 7 lines

XENO_POSIX_LDFLAGS="-L${staging}${libdir} -lpthread_rt -lpthread -lrt @XENO_USER_APP_LDFLAGS@"

On Blackfin uClinux 2008R1.5 linking fails with -lpthread_rt and -lpthread in that order:

bfin-uclinux-gcc -I/usr/xenomai/include -I/usr/xenomai/include/posix

This should rather be bfin-linux-uclibc-gcc to produce fdpic binaries; bfin-uclinux-gcc should be used to build the ELF kernel or FLAT format apps. Xenomai user-space support should be built using bfin-linux-uclibc-gcc.

Quoted 36 lines
(quoted diff) +0 -3
-D_GNU_SOURCE -D_REENTRANT -D__XENO__ -o hello hello.o
-Wl,@/usr/xenomai/lib/posix.wrappers -L/usr/xenomai/lib -lpthread_rt
-lpthread -lrt

/opt/uClinux/bfin-uclinux/bfin-uclinux/runtime/usr/lib/libpthread.a(mute x.os): In function `__pthread_once_fork_child':
libpthread/linuxthreads.old/mutex.c:357: undefined reference to ___wrap_pthread_cond_init' /opt/uClinux/bfin-uclinux/bfin-uclinux/runtime/usr/lib/libpthread.a(mute x.os): In function __pthread_once':
libpthread/linuxthreads.old/mutex.c:308: undefined reference to ___wrap_pthread_cond_wait' libpthread/linuxthreads.old/mutex.c:325: undefined reference to ___wrap_pthread_cond_broadcast' /opt/uClinux/bfin-uclinux/bfin-uclinux/runtime/usr/lib/libpthread.a(mute x.os): In function `pthread_once_cancelhandler':
libpthread/linuxthreads.old/mutex.c:281: undefined reference to `___wrap_pthread_cond_broadcast'
collect2: ld returned 1 exit status
make: *** [hello] Error 1

It looks like the uClinux pthread implementation makes use of its own functions and hence needs the library containing the wrappers specified after the pthread lib to enable resolving of references, as per the ld manual:

"Normally, an archive is searched only once in the order that it is specified on the command line. If a symbol in that archive is needed to resolve an undefined symbol referred to by an object in an archive that appears later on the command line, the linker would not be able to resolve that reference."

I'd have thought there are potentially some circular dependencies between the pthread and pthread_rt libs. Would it help portability to surround these options with -Wl,--start-group <libs> -Wl,--end-group? i.e.

Quoted 6 lines

XENO_POSIX_LDFLAGS="-L${staging}${libdir} -Wl,--start-group -lpthread_rt -lpthread -Wl,--end-group -lrt @XENO_USER_APP_LDFLAGS@"

I've done this locally and I can build the apps without re-ordering the libraries.

Which is likely not going to run properly. uClibc does not want to call the Xenomai wrappers from its internal code, but its own code, because it should apply to regular linuxthreads, not Xenomai-enabled RT threads. The only sane way to solve this kind of dependencies properly would be to go for a two-stage linking phase, i.e. first you do a partial link with your app and the Xenomai libraries applying the wrappers (-Wl,@/usr/xenomai/lib/posix.wrappers), then you complete the link without any wrapping against libpthread.so.

Here is a trivial Makefile I have just used to produce an executable against 2008-r1.5-rc3 and 2.4.1, without any problem. Obviously this also depends on the references dragged in by your example code; mine was pulling condvar, mutex and thread support through the common pthread_* calls. If this simple Makefile does not work with a properly built Xenomai userland support, I'd be interested to see your example code. TIA,

X := /usr/xenomai/blackfin
CFLAGS := $(shell $(X)/bin/xeno-config --posix-cflags) LDFLAGS := $(shell $(X)/bin/xeno-config --posix-ldflags) CC := $(shell $(X)/bin/xeno-config --cc)

all: hello

hello: hello.c

	$(CC) -o $@ $< $(CFLAGS) $(LDFLAGS)

clean:

	$(RM) hello
Quoted 9 lines

Regards,

Mike

_______________________________________________
Xenomai-help mailing list
[email protected]
https://mail.gna.org/listinfo/xenomai-help

Signature · 1 line
Philippe.
#2 lore raw
Quoted 8 lines · Philippe Gerum
Quoted 4 lines · Mike McTernan

Hi,

Checking xeno-config.in from 2.4.1, it has the following:

Please upgrade. The Xenomai/blackfin port is actively maintained, and

bugs

Quoted 1 line

have been fixed since then.

Apologies - I mistyped. I checked 2.4.6.1.

Quoted 1 line
Quoted 1 line

XENO_POSIX_LDFLAGS="-L${staging}${libdir} -lpthread_rt -lpthread

-lrt

Quoted 11 lines
Quoted 6 lines

@XENO_USER_APP_LDFLAGS@"

On Blackfin uClinux 2008R1.5 linking fails with -lpthread_rt and -lpthread in that order:

bfin-uclinux-gcc -I/usr/xenomai/include -I/usr/xenomai/include/posix

This should rather be bfin-linux-uclibc-gcc to produce fdpic binaries; bfin-uclinux-gcc should be used to build the ELF kernel or FLAT format apps.
Xenomai user-space support should be built using

bfin-linux-uclibc-gcc.

For various reasons we build all our apps as FLAT or ZFLAT, hence use bfin-uclinux-gcc -Wl,-elf2flt to link, which I think is common for embedded.

I presumed the binary format doesn't matter to Xenomai - is FLAT supported?

Quoted 4 lines

Review

Quoted 4 lines
(quoted diff) +0 -3
-D_GNU_SOURCE -D_REENTRANT -D__XENO__ -o hello hello.o
-Wl,@/usr/xenomai/lib/posix.wrappers -L/usr/xenomai/lib -lpthread_rt
-lpthread -lrt

/opt/uClinux/bfin-uclinux/bfin-uclinux/runtime/usr/lib/libpthread.a(mute

Quoted 4 lines
Quoted 4 lines

x.os): In function `__pthread_once_fork_child':
libpthread/linuxthreads.old/mutex.c:357: undefined reference to `___wrap_pthread_cond_init'

/opt/uClinux/bfin-uclinux/bfin-uclinux/runtime/usr/lib/libpthread.a(mute

Quoted 3 lines
Quoted 3 lines

x.os): In function `__pthread_once':
libpthread/linuxthreads.old/mutex.c:308: undefined reference to `___wrap_pthread_cond_wait'

<snip>

Quoted 2 lines
Quoted 2 lines

It looks like the uClinux pthread implementation makes use of its

own

Quoted 1 line
Quoted 1 line

functions and hence needs the library containing the wrappers

specified

Quoted 1 line
Quoted 1 line

after the pthread lib to enable resolving of references, as per the

ld

Quoted 4 lines
Quoted 4 lines

manual:

"Normally, an archive is searched only once in the order that it is specified on the command line. If a symbol in that archive is needed

to

Quoted 1 line
Quoted 1 line

resolve an undefined symbol referred to by an object in an archive

that

Quoted 5 lines
Quoted 5 lines

appears later on the command line, the linker would not be able to resolve that reference."

I'd have thought there are potentially some circular dependencies between the pthread and pthread_rt libs. Would it help portability

to

Quoted 1 line
Quoted 1 line

surround these options with -Wl,--start-group <libs>

-Wl,--end-group?

Quoted 3 lines
Quoted 3 lines

i.e.

XENO_POSIX_LDFLAGS="-L${staging}${libdir} -Wl,--start-group

-lpthread_rt

Quoted 3 lines
Quoted 3 lines

-lpthread -Wl,--end-group -lrt @XENO_USER_APP_LDFLAGS@"

I've done this locally and I can build the apps without re-ordering

the

Quoted 4 lines
Quoted 2 lines

libraries.

Which is likely not going to run properly. uClibc does not want to

call

Quoted 4 lines

the
Xenomai wrappers from its internal code, but its own code, because it should
apply to regular linuxthreads, not Xenomai-enabled RT threads. The

only

Quoted 2 lines

sane way
to solve this kind of dependencies properly would be to go for a

two-stage

Quoted 3 lines

linking phase, i.e. first you do a partial link with your app and the Xenomai
libraries applying the wrappers

(-Wl,@/usr/xenomai/lib/posix.wrappers),

Quoted 2 lines

then you
complete the link without any wrapping against libpthread.so.

I think you are using ELF, so maybe you don't see the problem - the dynamic linker maybe patching up the internal pthread references? I'm using FLAT, which is essentially a static link, so everything has to be fully resolved.

While I'm not familiar with the internals of Xenomai, I'd have thought that intercepting the internal call would be inefficient, but not harmful. Presumably we end up in the normal pthread lib because the calling thread has no Xenomai context. When the wrapper is called from pthread internally, it should always find there is no Xenomai-thread context for the caller and hence go back into the normal pthread lib. FWIW the compiled test app runs and produces the expected behaviour, although is too trivial to be a thorough test.

Quoted 14 lines

Here is a trivial Makefile I have just used to produce an executable against 2008-r1.5-rc3 and 2.4.1, without any problem. Obviously this also depends on the references dragged in by your example code; mine was pulling condvar, mutex and thread support through the common pthread_* calls. If this simple Makefile does not work with a properly

built Xenomai userland support, I'd be interested to see your example code. TIA,

X := /usr/xenomai/blackfin
CFLAGS := $(shell $(X)/bin/xeno-config --posix-cflags) LDFLAGS := $(shell $(X)/bin/xeno-config --posix-ldflags) CC := $(shell $(X)/bin/xeno-config --cc)

Quoted 8 lines

all: hello

hello: hello.c

	$(CC) -o $@ $< $(CFLAGS) $(LDFLAGS)

clean:

	$(RM) hello

Unfortunately this does not link either (note I added -Wl,-elf2flt). Here's the program I am trying to compile, the compile output, and the xeno-config output:

#include <stdio.h>
#include <sys/mman.h>
#include <pthread.h>

void *demo(void *arg)
{
printf("Hello world\n");
}

int main(int argc, char* argv[])

{
    pthread_t tid;

    mlockall(MCL_CURRENT | MCL_FUTURE);
    pthread_create(&tid, NULL, demo, NULL);
    sleep(1);
    return 0;
}
$ make
bfin-uclinux-gcc -o hello hello.c -I/usr/xenomai/include
-I/usr/xenomai/include/posix -D_GNU_SOURCE -D_REENTRANT -D__XENO__
-Wl,-elf2flt -Wl,@/usr/xenomai/lib/posix.wrappers -L/usr/xenomai/lib
-lpthread_rt -lpthread -lrt
/opt/uClinux/bfin-uclinux/bfin-uclinux/runtime/usr/lib/libpthread.a(mute
x.os): In function `__pthread_once_fork_child':
libpthread/linuxthreads.old/mutex.c:357: undefined reference to
`___wrap_pthread_cond_init'
/opt/uClinux/bfin-uclinux/bfin-uclinux/runtime/usr/lib/libpthread.a(mute
x.os): In function `__pthread_once':
libpthread/linuxthreads.old/mutex.c:308: undefined reference to
`___wrap_pthread_cond_wait'
libpthread/linuxthreads.old/mutex.c:325: undefined reference to
`___wrap_pthread_cond_broadcast'
/opt/uClinux/bfin-uclinux/bfin-uclinux/runtime/usr/lib/libpthread.a(mute
x.os): In function `pthread_once_cancelhandler':
libpthread/linuxthreads.old/mutex.c:281: undefined reference to
`___wrap_pthread_cond_broadcast'
collect2: ld returned 1 exit status
make: *** [hello] Error 1 
$ /usr/xenomai/bin/xeno-config
xeno-config --verbose
    --version="2.4.0"
    --cc="bfin-uclinux-gcc"
    --arch="blackfin"
    --prefix="/usr/xenomai"
    --xeno-cflags="-I/usr/xenomai/include -D_GNU_SOURCE -D_REENTRANT
-D__XENO__"
    --xeno-ldflags="-L/usr/xenomai/lib -lpthread "
    --posix-cflags="-I/usr/xenomai/include -I/usr/xenomai/include/posix
-D_GNU_SOURCE -D_REENTRANT -D__XENO__"
    --posix-ldflags="-L/usr/xenomai/lib -lpthread_rt -lpthread -lrt"
    --library-dir="/usr/xenomai/lib"
Usage xeno-config OPTIONS
Options :
        --help
        --v,--verbose
        --version
        --cc
        --arch
        --prefix
        --xeno-cflags
        --xeno-ldflags
        --posix-cflags
        --posix-ldflags
        --lib*-dir,--libdir,--user-libdir

Regards,

Mike

#3 lore raw
Quoted 11 lines · Mike McTernan
Quoted 3 lines · Philippe Gerum
Quoted 2 lines · Mike McTernan

Hi,

I think you are using ELF, so maybe you don't see the problem - the dynamic linker maybe patching up the internal pthread references? I'm using FLAT, which is essentially a static link, so everything has to be fully resolved.

Yes, this is where FLAT is biting you. All refs have to be resolved, so the linker tries to pick whatever it needs at link time.

Quoted 10 lines

While I'm not familiar with the internals of Xenomai, I'd have thought that intercepting the internal call would be inefficient, but not harmful. Presumably we end up in the normal pthread lib because the calling thread has no Xenomai context. When the wrapper is called from pthread internally, it should always find there is no Xenomai-thread context for the caller and hence go back into the normal pthread lib. FWIW the compiled test app runs and produces the expected behaviour, although is too trivial to be a thorough test.

An internal uclibc call to grab e.g. a regular mutex from a Xenomai thread would likely be an issue though.

Quoted 19 lines
Quoted 19 lines

Here is a trivial Makefile I have just used to produce an executable against 2008-r1.5-rc3 and 2.4.1, without any problem. Obviously this also depends on the references dragged in by your example code; mine was pulling condvar, mutex and thread support through the common pthread_* calls. If this simple Makefile does not work with a properly

built Xenomai userland support, I'd be interested to see your example code. TIA,

X := /usr/xenomai/blackfin
CFLAGS := $(shell $(X)/bin/xeno-config --posix-cflags) LDFLAGS := $(shell $(X)/bin/xeno-config --posix-ldflags) CC := $(shell $(X)/bin/xeno-config --cc)

all: hello

hello: hello.c

	$(CC) -o $@ $< $(CFLAGS) $(LDFLAGS)
Quoted 8 lines
Quoted 3 lines

clean:

	$(RM) hello

Unfortunately this does not link either (note I added -Wl,-elf2flt). Here's the program I am trying to compile, the compile output, and the xeno-config output:

This is all due to the FLAT mode. So I guess your next option is two-stage linking in you don't want to rely on shared fdpic libs.

Quoted 4 lines

#include <stdio.h>
#include <sys/mman.h>
#include <pthread.h>

Signature · 1 line
Philippe.
#4 lore raw
Quoted 7 lines · Philippe Gerum
Quoted 6 lines · Mike McTernan
Quoted 3 lines · Philippe Gerum
Quoted 2 lines · Mike McTernan

Hi,

I think you are using ELF, so maybe you don't see the problem - the dynamic linker maybe patching up the internal pthread references?

I'm

Quoted 1 line
Quoted 1 line

using FLAT, which is essentially a static link, so everything has to

be

Quoted 3 lines
Quoted 1 line

fully resolved.

Yes, this is where FLAT is biting you. All refs have to be resolved,

so

Quoted 1 line

the linker tries to pick whatever it needs at link time.

<snip>

Quoted 1 line

This is all due to the FLAT mode. So I guess your next option is

two-stage

Quoted 1 line

linking in you don't want to rely on shared fdpic libs.

Unfortunately moving away from ZFLAT isn't trivial for us and is an unlikely step.

I think Xenomai should support FLAT though, it's just this link problem, right?

Unless you have some hints as to how the two-stage link is done, I'm going to go and try to get this working and report back later.

Regards,

Mike

#5 lore raw
Quoted 17 lines · Mike McTernan
Quoted 7 lines · Philippe Gerum
Quoted 6 lines · Mike McTernan
Quoted 3 lines · Philippe Gerum
Quoted 2 lines · Mike McTernan

Hi,

I think you are using ELF, so maybe you don't see the problem - the dynamic linker maybe patching up the internal pthread references?

I'm

Quoted 1 line
Quoted 1 line

using FLAT, which is essentially a static link, so everything has to

be

Quoted 2 lines
Quoted 1 line

fully resolved.

Yes, this is where FLAT is biting you. All refs have to be resolved,

so

Quoted 1 line

the linker tries to pick whatever it needs at link time.

<snip>

Quoted 1 line

This is all due to the FLAT mode. So I guess your next option is

Quoted 9 lines

two-stage

Quoted 1 line

linking in you don't want to rely on shared fdpic libs.

Unfortunately moving away from ZFLAT isn't trivial for us and is an unlikely step.

I think Xenomai should support FLAT though, it's just this link problem, right?

Yes, Xenomai does not care about the underlying binary format. It is just a convenience for us to follow the default one uClinux uses on the Blackfin.

Quoted 3 lines

Unless you have some hints as to how the two-stage link is done, I'm going to go and try to get this working and report back later.

I was thinking about something along these lines (you probably need the flat directive to be passed to the linker as well); totally untested, but the idea is illustrated:

X=/usr/xenomai
CFLAGS= $(shell $(X)/bin/xeno-config --posix-cflags) -g WRAPPERS := $(X)/lib/posix.wrappers
LDFLAGS_1 = -Bstatic -L$(X)/lib @$(WRAPPERS) LDFLAGS_2 = -static -L$(X)/lib -lpthread -lrt CC = $(shell $(X)/bin/xeno-config --cc)
LD = $(shell $(CC) --print-prog-name=ld)

all: hello

hello: hello-stage1.o

	$(CC) -o $@ $< $(LDFLAGS_2)

hello-stage1.o: hello.o $(X)/lib/libpthread_rt.a

	$(LD) -r -o $@ $^ $(LDFLAGS_1)

hello.o: hello.c

	$(CC) -c $< $(CFLAGS)

clean:
rm -f *.o hello

Quoted 4 lines

Regards,

Mike

Signature · 1 line
Philippe.
#6 lore raw
Quoted 2 lines · Philippe Gerum
Quoted 1 line · Mike McTernan

I think Xenomai should support FLAT though, it's just this link

problem,

Quoted 3 lines
Quoted 1 line

right?

Yes, Xenomai does not care about the underlying binary format. It is

just

Quoted 2 lines

a convenience for us to follow the default one uClinux uses on the Blackfin.

I think FLAT is possibly more popular for embedded apps which Blackfin goes into. From our experience ZFLAT are much smaller and load quickly.

Quoted 1 line

I was thinking about something along these lines

You were thinking very well. That does indeed produce a linked binary which runs.

Any chance this can be added to future Xenomai releases? Would it be okay for me to add a page to your wiki describing how to static link?

Regards,

Mike

#7 lore raw
Quoted 13 lines · Mike McTernan
Quoted 2 lines · Philippe Gerum
Quoted 1 line · Mike McTernan

I think Xenomai should support FLAT though, it's just this link

problem,

Quoted 2 lines
Quoted 1 line

right?

Yes, Xenomai does not care about the underlying binary format. It is

just

Quoted 2 lines

a convenience for us to follow the default one uClinux uses on the Blackfin.

I think FLAT is possibly more popular for embedded apps which Blackfin goes into. From our experience ZFLAT are much smaller and load quickly.

Quoted 8 lines
Quoted 1 line

I was thinking about something along these lines

You were thinking very well. That does indeed produce a linked binary which runs.

Any chance this can be added to future Xenomai releases? Would it be okay for me to add a page to your wiki describing how to static link?

Sure.

Quoted 4 lines

Regards,

Mike

Signature · 1 line
Philippe.