Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot link shared library #178

Closed
moritzbuhl opened this issue Sep 1, 2020 · 5 comments
Closed

Cannot link shared library #178

moritzbuhl opened this issue Sep 1, 2020 · 5 comments

Comments

@moritzbuhl
Copy link

On OpenBSD I cannot build v5.9:

/usr/bin/libtool --mode=link cc -O2 -pipe -g -DNETSNMP_ENABLE_IPV6 -fno-strict-aliasing -DNETSNMP_REMOVE_U64 -O2 -pipe -g -Uopenbsd6 -Dopen
-checks -pipe -fstack-protector-strong -I/usr/local/include  -I/usr/libdata/perl5/amd64-openbsd/CORE   -Wall -Wextra -Wstrict-prototypes -W
no-type-limits -rpath /usr/local/lib -o libnetsnmp.la snmp_client.lo mib.lo parse.lo snmp_api.lo snmp.lo         snmp_auth.lo asn1.lo md5.l
tools.lo  snmp_logging.lo        text_utils.lo   large_fd_set.lo cert_util.lo snmp_openssl.lo            snmpv3.lo lcd_time.lo keytools.lo 
.lo fd_event_manager.lo          check_varbind.lo                                        mt_support.lo snmp_enum.lo snmp-tc.lo snmp_service
snmpIPv6BaseDomain.lo transports/snmpIPBaseDomain.lo transports/snmpUDPBaseDomain.lo transports/snmpUDPIPv4BaseDomain.lo transports/snmpTCP
IPv6Domain.lo transports/snmpTCPIPv6Domain.lo transports/snmpUDPDomain.lo transports/snmpTCPDomain.lo transports/snmpAliasDomain.lo transpo
 snmp_version.lo      container.lo container_binary_array.lo                     ucd_compat.lo                                            d
iner_iterator.lo  -Wl,-no-undefined  -lm   -lcrypto                                                                                        
libtool: link: cc -shared -fPIC -DPIC -o .libs/libnetsnmp.so.17.1 -O2 -pipe -g -DNETSNMP_ENABLE_IPV6 -fno-strict-aliasing -DNETSNMP_REMOVE_
aliasing -fno-delete-null-pointer-checks -pipe -fstack-protector-strong -I/usr/local/include -I/usr/libdata/perl5/amd64-openbsd/CORE -Wall 
mpare -Wno-unused-parameter -Wno-type-limits -Wl,-no-undefined .libs/snmp_client.o .libs/mib.o .libs/parse.o .libs/snmp_api.o .libs/snmp.o 
/int64.o .libs/read_config.o .libs/pkcs.o .libs/snmp_debug.o .libs/tools.o .libs/snmp_logging.o .libs/text_utils.o .libs/large_fd_set.o .li
libs/callback.o .libs/default_store.o .libs/snmp_alarm.o .libs/data_list.o .libs/oid_stash.o .libs/fd_event_manager.o .libs/check_varbind.o
printf.o .libs/snmp_transport.o transports/.libs/snmpIPv6BaseDomain.o transports/.libs/snmpIPBaseDomain.o transports/.libs/snmpUDPBaseDomai
nmpSocketBaseDomain.o transports/.libs/snmpIPv4BaseDomain.o transports/.libs/snmpUDPIPv6Domain.o transports/.libs/snmpTCPIPv6Domain.o trans
sports/.libs/snmpUnixDomain.o transports/.libs/snmpCallbackDomain.o .libs/snmp_secmod.o .libs/snmpusm.o .libs/snmp_version.o .libs/containe
tainer_null.o .libs/container_list_ssll.o .libs/container_iterator.o -L.libs -lm -lcrypto -Wl,-soname,libnetsnmp.so.17.1                   
ld: error: undefined symbol: calloc                                  
>>> referenced by snmp_client.c:130                                                                                                        
>>>               .libs/snmp_client.o:(snmp_pdu_create)              
>>> referenced by snmp_client.c:130                                                                                                        
>>>               .libs/snmp_client.o:(_query)                                                                                             
>>> referenced by mib.c:3179                                                                                                               
>>>               .libs/mib.o:(netsnmp_sprint_realloc_objid_tree)                                                                          
>>> referenced by mib.c:2871                                                                                                               
>>>               .libs/mib.o:(netsnmp_init_mib)                     
>>> referenced by mib.c:3108                                                                                                               
>>>               .libs/mib.o:(netsnmp_sprint_realloc_objid)                                                                               
>>> referenced by mib.c:3347                                    

Bisecting between 5.8.1.pre2 and 5.8.1.rc1 showed that commit cb9b87d is at fault.
After some testing I am convinced that -lc is missing:

Here is a small POC:

scanny$ cat Makefile hello.c  

LDFLAGS =       -Wl,-no-undefined
CFLAGS =        -shared

#include <stdio.h>
int
main(void){
        printf("hello world\n");
        return 0;
}
scanny$ make hello
cc -shared  -Wl,-no-undefined -o hello hello.c 
ld: error: undefined symbol: printf
>>> referenced by hello.c
>>>               /tmp/hello-9a4a5e.o:(main)
cc: error: linker command failed with exit code 1 (use -v to see invocation)
*** Error 1 in /tmp/tmp.JZ1T2Ayofw (<sys.mk>:85 'hello')

After addding -lc:

scanny$ cat Makefile hello.c                                                                                                              

LDFLAGS =       -lc -Wl,-no-undefined
CFLAGS =        -shared

#include <stdio.h>
int
main(void){
        printf("hello world\n");
        return 0;
}
scanny$ make hello
cc -shared  -lc -Wl,-no-undefined -o hello hello.c 

So the solution should be adding -lc somewhere in the linker flags.

@bvanassche
Copy link
Contributor

In the libtool source code I found the following:

	  *-*-openbsd* | *-*-freebsd* | *-*-dragonfly*)
	    # Do not include libc due to us having libc/libc_r.
	    ;;

How to decide whether to link libnetsnmp against libc or libc_r?

@sthen
Copy link
Contributor

sthen commented Sep 4, 2020

I think it would be better to avoid -Wl,-no-undefined on OpenBSD. I'm not sure of the original reason but shared libraries are explicitly not recording a dependency on libc and I think that's what trips up the -no-undefined check.

bvanassche added a commit that referenced this issue Sep 4, 2020
This patch fixes a linker error on OpenBSD.

See also #178 .
@bvanassche
Copy link
Contributor

Please take a look at commit ee7c66c.

@sthen
Copy link
Contributor

sthen commented Sep 7, 2020

@bvanassche Sorry for the slow reply, confirmed your commit is working on OpenBSD.

@bvanassche
Copy link
Contributor

Thanks for confirming!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants