NetBSD-Users archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: link as -lc -lpthread hangs



In article 
<515c64960909102201g6bb7f8cfr2284bb3a1f53933f%mail.gmail.com@localhost>,
Channa  <channa.kad%gmail.com@localhost> wrote:
>Hi,
>
>2009/9/11 Christos Zoulas <christos%astron.com@localhost>:
>> In article 
>> <515c64960909100612h29118e2ftf2af05f513aa103a%mail.gmail.com@localhost>,
>> Channa  <channa.kad%gmail.com@localhost> wrote:
>>>Hi,
>>>I am using NetBSD rtld implementation in my library.
>>>I am facing some issue when the application is built by linking
>>>with libc before libpthread. i.e  '-lc -lpthread' as shown below
>>>
>>>/usr/local/${ARCH}-linux/gcc app.c -o app -lc -lpthread
>>>
>>>When i run the 'app' it hangs.
>>
>> This is not supposed to work. You need to link -lpthread before -lc
>> since pthread overrides some libc functions. In fact the only portable
>> way to compile threaded programs is using gcc -pthread app.c -o app
>>
>> christos
>
>Yes true pthread overrides some of the symbols in libc.
>But there is a way which we can avoid the hang condition.
>When the application is compiled by the method by linking
>'-lc -lpthread' in that case the needed section of the application
>contains the libc.so first and then libpthread.so.
>
>In the rtld implementation the needed libraries are loaded on the same
>order as present in applications needed section, so we can add a fix by
>not loading libc.so if its the first entry in needed objects.
>
>This can be done in the following file
>
>$ cat src/libexec/ld.elf_so/load.c
>:
>:
>+                static int flag = 0;
>               for (needed = obj->needed; needed != NULL;
>                   needed = needed->next) {
>+                       flag++;
>                       const char *name = obj->strtab + needed->name;
>                       /*
>                         * Avoid loading of 'libc.so' if its the first object
>                         * in the needed section
>                         */
>+                       if (flag == 1 &&
>!(strncmp(name,"libc.so",strlen(name))))
>+                               continue;
>               for (needed = obj->needed; needed != NULL;
>                   needed = needed->next) {
>+                       flag++;
>                       const char *name = obj->strtab + needed->name;
>
>:
>:
>
>So by doing this we can avoid loading libc.so first.
>
>This will avoid hang condition if the application is built by
>linking '-lc -lpthread'
>
>Please let me know your views on this.

Why special-case this, when it can easily be avoided by not specifying
-lc in the command line.

christos



Home | Main Index | Thread Index | Old Index