From owner-freebsd-questions@freebsd.org Tue Oct 15 18:44:09 2019 Return-Path: Delivered-To: freebsd-questions@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id 578591511FD for ; Tue, 15 Oct 2019 18:44:09 +0000 (UTC) (envelope-from jbe-mlist@magnetkern.de) Received: from sapphire.magnetkern.de (sapphire.magnetkern.de [185.228.139.199]) by mx1.freebsd.org (Postfix) with ESMTP id 46t48S2dPyz4fqR for ; Tue, 15 Oct 2019 18:44:07 +0000 (UTC) (envelope-from jbe-mlist@magnetkern.de) Received: from titanium (p57A35420.dip0.t-ipconnect.de [87.163.84.32]) by sapphire.magnetkern.de (Postfix) with ESMTPSA id D76F99B7D for ; Tue, 15 Oct 2019 18:44:00 +0000 (UTC) Date: Tue, 15 Oct 2019 20:44:00 +0200 From: Jan Behrens To: freebsd-questions@freebsd.org Subject: Problems with ld, libc, and "struct stat" Message-Id: <20191015204400.e33c8f62af711e829288ddae@magnetkern.de> X-Mailer: Sylpheed 3.7.0 (GTK+ 2.24.32; amd64-portbld-freebsd12.0) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Rspamd-Queue-Id: 46t48S2dPyz4fqR X-Spamd-Bar: - Authentication-Results: mx1.freebsd.org; dkim=none; dmarc=none; spf=pass (mx1.freebsd.org: domain of jbe-mlist@magnetkern.de designates 185.228.139.199 as permitted sender) smtp.mailfrom=jbe-mlist@magnetkern.de X-Spamd-Result: default: False [-1.65 / 15.00]; ARC_NA(0.00)[]; RCVD_VIA_SMTP_AUTH(0.00)[]; RCVD_COUNT_TWO(0.00)[2]; NEURAL_HAM_MEDIUM(-0.83)[-0.829,0]; FROM_HAS_DN(0.00)[]; R_SPF_ALLOW(-0.20)[+mx]; TO_MATCH_ENVRCPT_ALL(0.00)[]; MIME_GOOD(-0.10)[text/plain]; PREVIOUSLY_DELIVERED(0.00)[freebsd-questions@freebsd.org]; TO_DN_NONE(0.00)[]; RCPT_COUNT_ONE(0.00)[1]; NEURAL_HAM_LONG(-0.99)[-0.991,0]; DMARC_NA(0.00)[magnetkern.de]; MV_CASE(0.50)[]; IP_SCORE(-0.13)[asn: 197540(-0.63), country: DE(-0.01)]; RCVD_NO_TLS_LAST(0.10)[]; RECEIVED_SPAMHAUS_PBL(0.00)[32.84.163.87.khpj7ygk5idzvmvt5x4ziurxhy.zen.dq.spamhaus.net : 127.0.0.10]; R_DKIM_NA(0.00)[]; MIME_TRACE(0.00)[0:+]; ASN(0.00)[asn:197540, ipnet:185.228.136.0/22, country:DE]; MID_RHS_MATCH_FROM(0.00)[]; FROM_EQ_ENVFROM(0.00)[] X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 15 Oct 2019 18:44:09 -0000 Hello, I stumbled across a weird problem related stat() that (according to my research) seems to be related to an update of the "struct stat" C-structure in recent Kernel versions. Consider the following two files. testlib.c: #include #include void testfunc() { struct stat sb; stat("testlib.c", &sb); printf("Size of testlib.c is %i bytes.\n", (int)sb.st_size); } testprog.c: extern void testfunc(void); int main(int argc, char **argv) { testfunc(); return 0; } Now I run: % cc -Wall -c -fPIC -o testlib.o testlib.c % cc -Wall -o testprog testlib.o testprog.c % ./testprog Size of testlib.c is 168 bytes. But when I make a shared library like this, I get a different result: % ld -shared -o testlib.so testlib.o % cc -Wall -o testprog `pwd`/testlib.so testprog.c % ./testprog Size of testlib.c is 394655000 bytes. This result is obviously wrong. Note that I did not get any compiler or linker warnings or errors; yet there happens a bad error during runtime! With several attempts of trial and error, I figured out that the problem does not appear when appending "-L/usr/lib -lc" to the ld call when linking the shared library: % ld -shared -o testlib.so testlib.o -L/usr/lib -lc % cc -Wall -o testprog `pwd`/testlib.so testprog.c % ./testprog Size of testlib.c is 168 bytes. Can anyone tell me why is that and if I am supposed to always add "-L/usr/lib -lc" when creating an *.so file? Is this maybe a bug in FreeBSD or ld? Or am I using the linker in a wrong way? Kind regards, Jan Behrens