From owner-freebsd-questions@freebsd.org Wed Oct 16 11:45:37 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 E3D321629FB for ; Wed, 16 Oct 2019 11:45:37 +0000 (UTC) (envelope-from mafsys1234@gmail.com) Received: from mail-pf1-x444.google.com (mail-pf1-x444.google.com [IPv6:2607:f8b0:4864:20::444]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) server-signature RSA-PSS (4096 bits) client-signature RSA-PSS (2048 bits) client-digest SHA256) (Client CN "smtp.gmail.com", Issuer "GTS CA 1O1" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 46tVq445Tmz4cfF for ; Wed, 16 Oct 2019 11:45:36 +0000 (UTC) (envelope-from mafsys1234@gmail.com) Received: by mail-pf1-x444.google.com with SMTP id x127so14561861pfb.7 for ; Wed, 16 Oct 2019 04:45:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-transfer-encoding:content-language; bh=39/Xv66Sb0/wgUk2thj5jI8reRslzMPpNUodX8VP1f0=; b=f0zSluksYGtr1+5+gR7LrmgdRwzWAaa77OlIOVmKHkWRFQJZLX0k7pfXBZWgyV6+Kh yK2gsidUyLuYhatjN7hTh+Jgxm8oAsi0ZwDYev9aeORQhtCLf4Xs/nbdI7LQfBfmGpbi Gd54410CtU3ikgkbE8fE6qtfl3hrc96I+3S3AjI2OAwCx+ESZV6YVyISl81DhTBsETC6 xYqhnW5JRRYnPuwFPlUALJX/AEX4wzlyj1e137X7WFseRA4Y8OTT2MSuKHZJHTjx9AkU nFyStjxGyJhHNxK1wqAs8qFAIgVEdLyTYLdiMsAjmO+eLs6C3rtP9AuvsLI7LqX2m4N1 Uxlw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-transfer-encoding :content-language; bh=39/Xv66Sb0/wgUk2thj5jI8reRslzMPpNUodX8VP1f0=; b=O4Qtg+0CLe09oRIMJNCnmXquca3op6vyMgUI/pnwdfT8ufs6vbLxZj0Q+tdnnIt1nm ajW5Db23z1u55cyJkrbvB2XAbEQqBsI8tfEM+6fpd8rKd5Xqy2EOMLZxsvpxgNE4J2sr tvqH2uPLuXt9LeyTSQPXsVRDpCeWycx2ZqDR6URTx9/lHZX3Sjg1DkHshuS6UW45QbhH CXXNic1Ce89QqoNO6QWMxpHSDC18G5NMHGx1tzWtxN4b9gPszGS9KXZVrnVFKa2bnf9A VOItBmpcb89s959rgywLe3OBrFc3LFTQIHL9ByqcwTFua2Tfa+Dv4ZUQcc0tcyT4D7pb xjiA== X-Gm-Message-State: APjAAAXxTWmY3K9Jj0zJYsu9u5EPid2MsfjI/r1HHxvIAOL5Yp5aF3w+ bfOeEaNqB5blTYxvZCGzEj2ZLjkk X-Google-Smtp-Source: APXvYqyghQhqpiwjt+UBCq6OY4IDczjUUZSuj+ldDvtPDrcu12C9SNN2GiEX0zKaJtM6y3lQ/HDU0g== X-Received: by 2002:a17:90a:6302:: with SMTP id e2mr4457326pjj.20.1571226334674; Wed, 16 Oct 2019 04:45:34 -0700 (PDT) Received: from [192.168.1.10] (C-59-101-161-221.mel.connect.net.au. [59.101.161.221]) by smtp.gmail.com with ESMTPSA id d22sm26112256pfq.168.2019.10.16.04.45.32 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 16 Oct 2019 04:45:34 -0700 (PDT) Subject: Re: Problems with ld, libc, and "struct stat" To: Jan Behrens , freebsd-questions@freebsd.org References: <20191015204400.e33c8f62af711e829288ddae@magnetkern.de> <47c27361-4e74-05d1-3343-e39526730d85@malikania.fr> <20191016131552.6fda34292987e22ae78072cc@magnetkern.de> From: MJ Message-ID: <71e9fd21-506f-bbae-df27-74ea0b32a660@gmail.com> Date: Wed, 16 Oct 2019 22:45:10 +1100 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:60.0) Gecko/20100101 Thunderbird/60.9.0 MIME-Version: 1.0 In-Reply-To: <20191016131552.6fda34292987e22ae78072cc@magnetkern.de> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Content-Language: en-US X-Rspamd-Queue-Id: 46tVq445Tmz4cfF X-Spamd-Bar: -- Authentication-Results: mx1.freebsd.org; dkim=pass header.d=gmail.com header.s=20161025 header.b=f0zSluks; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (mx1.freebsd.org: domain of mafsys1234@gmail.com designates 2607:f8b0:4864:20::444 as permitted sender) smtp.mailfrom=mafsys1234@gmail.com X-Spamd-Result: default: False [-3.00 / 15.00]; RCVD_VIA_SMTP_AUTH(0.00)[]; TO_DN_SOME(0.00)[]; R_SPF_ALLOW(-0.20)[+ip6:2607:f8b0:4000::/36:c]; FREEMAIL_FROM(0.00)[gmail.com]; RCVD_COUNT_THREE(0.00)[3]; DKIM_TRACE(0.00)[gmail.com:+]; RCPT_COUNT_TWO(0.00)[2]; DMARC_POLICY_ALLOW(-0.50)[gmail.com,none]; RECEIVED_SPAMHAUS_PBL(0.00)[221.161.101.59.khpj7ygk5idzvmvt5x4ziurxhy.zen.dq.spamhaus.net : 127.0.0.10]; FROM_EQ_ENVFROM(0.00)[]; MIME_TRACE(0.00)[0:+]; FREEMAIL_ENVFROM(0.00)[gmail.com]; ASN(0.00)[asn:15169, ipnet:2607:f8b0::/32, country:US]; MID_RHS_MATCH_FROM(0.00)[]; DWL_DNSWL_NONE(0.00)[gmail.com.dwl.dnswl.org : 127.0.5.0]; ARC_NA(0.00)[]; NEURAL_HAM_MEDIUM(-1.00)[-1.000,0]; R_DKIM_ALLOW(-0.20)[gmail.com:s=20161025]; FROM_HAS_DN(0.00)[]; NEURAL_HAM_LONG(-1.00)[-1.000,0]; MIME_GOOD(-0.10)[text/plain]; PREVIOUSLY_DELIVERED(0.00)[freebsd-questions@freebsd.org]; IP_SCORE_FREEMAIL(0.00)[]; TO_MATCH_ENVRCPT_SOME(0.00)[]; RCVD_IN_DNSWL_NONE(0.00)[4.4.4.0.0.0.0.0.0.0.0.0.0.0.0.0.0.2.0.0.4.6.8.4.0.b.8.f.7.0.6.2.list.dnswl.org : 127.0.5.0]; IP_SCORE(0.00)[ip: (-0.50), ipnet: 2607:f8b0::/32(-2.48), asn: 15169(-2.10), country: US(-0.05)]; RCVD_TLS_ALL(0.00)[] X-Mailman-Approved-At: Wed, 16 Oct 2019 15:47:12 +0000 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: Wed, 16 Oct 2019 11:45:37 -0000 On 16/10/2019 10:15 pm, Jan Behrens wrote: > On Wed, 16 Oct 2019 09:18:52 +0200 > David Demelier wrote: > >> Le 15/10/2019 à 20:44, Jan Behrens a écrit : >>> 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. >>> >>> [...] >>> >>> stat("testlib.c", &sb); >> Please test the result of stat otherwise sb is left untouched (so all >> member undefined). > You are right, of course (this was just a quick and dirty demonstration). Initialize sb - it's also quick and dirty to do and prevents accessing memory with garbage in it. It's always a good habit to get into, regardless. >>> But when I make a shared library like this, I get a different result: >>> >>> % ld -shared -o testlib.so testlib.o >> Hmm, we usually never call the linker itself when creating shared libraries. >> >> Try instead: cc -shared -o testlib.so testlib.o >> >> HTH >> -- >> David > Thank you very much; I tried that, and it works properly: > > % cc -Wall -c -fPIC -o testlib.o testlib.c > % cc -shared -o testlib.so testlib.o > % cc -Wall -o testprog `pwd`/testlib.so testprog.c > % ./testprog > Size of testlib.c is 168 bytes. > > I will from now on use cc instead of ld to create shared libraries. > > I still wonder though if there is any documentation on this behavior > (and where to find it), whether it's FreeBSD related or LLVM related. > It feels a bit scary that using "ld" to make a shared library can > result in weird runtime behavior without even raising a warning. ld is doing what you told it, which is not much other than it's a shared library it's linking to. If you're using ld then you need to specify -L or -rpath. How else is it to know where it's shared libraries are? If you examined the runtime with gdb (or lldb) you would immediately answer your question. It's your best tool to answer lots of these types of questions. > Do you know any link where I find a more detailed explanation about why > I need to use "cc" instead of "ld" to create shared libraries? I assume > that "cc" adds the necessary options to "ld" that are otherwise > missing. But I don't see where this is documented. This explains it succinctly: https://www.cprogramming.com/tutorial/shared-libraries-linux-gcc.html (apart from the linux-isms) Also this, in more detail: http://tldp.org/HOWTO/Program-Library-HOWTO/shared-libraries.html Cheers Mark