From owner-freebsd-questions@freebsd.org Thu Nov 7 05:48:35 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 DE0431A7ECE for ; Thu, 7 Nov 2019 05:48:35 +0000 (UTC) (envelope-from SRS0=4JcV=Y7=mail.sermon-archive.info=doug@sermon-archive.info) Received: from mail.sermon-archive.info (sermon-archive.info [71.177.216.148]) by mx1.freebsd.org (Postfix) with ESMTP id 477sry320yz47tZ for ; Thu, 7 Nov 2019 05:48:34 +0000 (UTC) (envelope-from SRS0=4JcV=Y7=mail.sermon-archive.info=doug@sermon-archive.info) Received: from [10.0.1.251] (mini [10.0.1.251]) by mail.sermon-archive.info (Postfix) with ESMTPSA id 477srw3tQSz2fjVp for ; Wed, 6 Nov 2019 21:48:32 -0800 (PST) From: Doug Hardie Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: quoted-printable Mime-Version: 1.0 (Mac OS X Mail 12.4 \(3445.104.11\)) Subject: Re: Extension to previous posts: Problems with ld, libc, and "struct stat" Date: Wed, 6 Nov 2019 21:48:32 -0800 References: To: FreeBSD Questions In-Reply-To: Message-Id: <5FBE3E7E-D9B2-486B-98F0-DBCF3E72B6E7@mail.sermon-archive.info> X-Mailer: Apple Mail (2.3445.104.11) X-Virus-Scanned: clamav-milter 0.101.4 at mail X-Virus-Status: Clean X-Rspamd-Queue-Id: 477sry320yz47tZ X-Spamd-Bar: - Authentication-Results: mx1.freebsd.org; dkim=none; spf=pass (mx1.freebsd.org: domain of SRS0=4JcV=Y7=mail.sermon-archive.info=doug@sermon-archive.info designates 71.177.216.148 as permitted sender) smtp.mailfrom=SRS0=4JcV=Y7=mail.sermon-archive.info=doug@sermon-archive.info X-Spamd-Result: default: False [-1.44 / 15.00]; ARC_NA(0.00)[]; RCVD_VIA_SMTP_AUTH(0.00)[]; NEURAL_HAM_MEDIUM(-0.99)[-0.988,0]; FROM_HAS_DN(0.00)[]; R_SPF_ALLOW(-0.20)[+ip4:71.177.216.148]; TO_MATCH_ENVRCPT_ALL(0.00)[]; MIME_GOOD(-0.10)[text/plain]; PREVIOUSLY_DELIVERED(0.00)[freebsd-questions@freebsd.org]; NEURAL_HAM_LONG(-0.95)[-0.946,0]; RCPT_COUNT_ONE(0.00)[1]; IP_SCORE(-0.10)[asn: 5650(-0.46), country: US(-0.05)]; TO_DN_ALL(0.00)[]; RCVD_IN_DNSWL_NONE(0.00)[148.216.177.71.list.dnswl.org : 127.0.10.0]; MV_CASE(0.50)[]; FORGED_SENDER(0.30)[bc979@lafn.org,SRS0=4JcV=Y7=mail.sermon-archive.info=doug@sermon-archive.info]; RCVD_NO_TLS_LAST(0.10)[]; R_DKIM_NA(0.00)[]; MIME_TRACE(0.00)[0:+]; ASN(0.00)[asn:5650, ipnet:71.177.216.0/23, country:US]; FROM_NEQ_ENVFROM(0.00)[bc979@lafn.org,SRS0=4JcV=Y7=mail.sermon-archive.info=doug@sermon-archive.info]; RCVD_COUNT_TWO(0.00)[2] 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: Thu, 07 Nov 2019 05:48:35 -0000 We now have 12.1 with an updated compiler and linker. Things have = changed. I added a bit to testprog.c: master# more testprog.c #include extern char id[4]; extern int sock; int unknown; void testfunc(void); int main(int argc, char **argv) { testfunc(); unknown =3D 9; printf ("id (%x) =3D %s\n", (int)&id, id); printf ("sock(%x) =3D %d\n", (int)&sock, sock); printf ("unknown (%x) =3D %d\n", (int)&unknown, unknown); return 0; } running it yields: master# ./testprog Size of testlib.c is 268 bytes. id (20400c) =3D aa sock(204010) =3D 5 unknown (204008) =3D 9 That appears to be correct. Everything worked. However, with lldb, = instead of error messages I get wrong values: master# lldb testprog (lldb) target create "testprog" Current executable set to 'testprog' (x86_64). (lldb) b main Breakpoint 1: where =3D testprog`main + 22 at testprog.c:9:3, address =3D = 0x00000000002012f6 (lldb) r Process 6039 launching Process 6039 launched: '/home/doug/zzz/testprog' (x86_64) Process 6039 stopped * thread #1, name =3D 'testprog', stop reason =3D breakpoint 1.1 frame #0: 0x00000000002012f6 testprog`main(argc=3D1, = argv=3D0x00007fffffffeb38) at testprog.c:9:3 6 =09 7 void testfunc(void); 8 int main(int argc, char **argv) { -> 9 testfunc(); 10 unknown =3D 9; 11 printf ("id (%x) =3D %s\n", (int)&id, id); 12 printf ("sock(%x) =3D %d\n", (int)&sock, sock); (lldb) n Size of testlib.c is 268 bytes. Process 6039 stopped * thread #1, name =3D 'testprog', stop reason =3D step over frame #0: 0x0000000000201307 testprog`main(argc=3D1, = argv=3D0x00007fffffffeb38) at testprog.c:10:11 7 void testfunc(void); 8 int main(int argc, char **argv) { 9 testfunc(); -> 10 unknown =3D 9; 11 printf ("id (%x) =3D %s\n", (int)&id, id); 12 printf ("sock(%x) =3D %d\n", (int)&sock, sock); 13 printf ("unknown (%x) =3D %d\n", (int)&unknown, unknown); (lldb)=20 Process 6039 stopped * thread #1, name =3D 'testprog', stop reason =3D step over frame #0: 0x0000000000201312 testprog`main(argc=3D1, = argv=3D0x00007fffffffeb38) at testprog.c:11:3 8 int main(int argc, char **argv) { 9 testfunc(); 10 unknown =3D 9; -> 11 printf ("id (%x) =3D %s\n", (int)&id, id); 12 printf ("sock(%x) =3D %d\n", (int)&sock, sock); 13 printf ("unknown (%x) =3D %d\n", (int)&unknown, unknown); 14 return 0; (lldb) p id (char [4]) $0 =3D "" (lldb) p &id (char (*)[4]) $1 =3D 0x000000080024d000 (lldb) p unknown (int) $2 =3D 9 (lldb) p &unknown (int *) $3 =3D 0x0000000000204008 (lldb)=20 For the variable unknown lldb displays the correct address and value. = For id and sock, it displays the wrong address and the value at the = wrong address. The correct values are in the proper addresses: x 0x204008 0x00204008: 09 00 00 00 61 61 00 00 05 00 00 00 00 00 00 00 = ....aa.......... -- Doug > On 16 October 2019, at 14:02, Doug Hardie wrote: >=20 > Here is an issue that has plagued me for some time: >=20 > testlib.c: > #include > #include > #include > #include >=20 > char id[4]; > int sock; >=20 > void testfunc() { > struct stat sb; > stat("testlib.c", &sb); > strcpy (id, "aa"); > sock =3D 5; > printf("Size of testlib.c is %i bytes.\n", (int)sb.st_size); > } >=20 >=20 > testprog.c: > #include >=20 > extern char id[4]; > extern int sock; >=20 > void testfunc(void); > int main(int argc, char **argv) { > testfunc(); > printf ("id =3D %s\n", id); > printf ("sock =3D %d\n", sock); > return 0; > } >=20 >=20 > Makefile: > all: clean testprog run >=20 > testprog: > cc -Wall -g -c -fPIC -o testlib.o testlib.c > cc -shared -Wl,-export-dynamic -o testlib.so testlib.o > cc -Wall -g -o testprog ./testlib.so testprog.c >=20 > clean: > rm -f testlib.o testlib.so testprog >=20 > run: > ./testprog >=20 >=20 > Using make: > rm -f testlib.o testlib.so testprog > cc -Wall -g -c -fPIC -o testlib.o testlib.c > cc -shared -Wl,-export-dynamic -o testlib.so testlib.o > cc -Wall -g -o testprog ./testlib.so testprog.c > ./testprog > Size of testlib.c is 268 bytes. > id =3D aa > sock =3D 5 >=20 >=20 > Running lldb: > master# lldb testprog > (lldb) target create "testprog" > Current executable set to 'testprog' (x86_64). > (lldb) b main > Breakpoint 1: where =3D testprog`main + 22 at testprog.c:8, address =3D = 0x0000000000201366 > (lldb) r > Process 34787 launching > Process 34787 launched: '/home/doug/zzz/testprog' (x86_64) > Process 34787 stopped > * thread #1, name =3D 'testprog', stop reason =3D breakpoint 1.1 > frame #0: 0x0000000000201366 testprog`main(argc=3D1, = argv=3D0x00007fffffffeb38) at testprog.c:8 > 5 =09 > 6 void testfunc(void); > 7 int main(int argc, char **argv) { > -> 8 testfunc(); > 9 printf ("id =3D %s\n", id); > 10 printf ("sock =3D %d\n", sock); > 11 return 0; > (lldb) n > Size of testlib.c is 268 bytes. > Process 34787 stopped > * thread #1, name =3D 'testprog', stop reason =3D step over > frame #0: 0x000000000020137f testprog`main(argc=3D1, = argv=3D0x00007fffffffeb38) at testprog.c:9 > 6 void testfunc(void); > 7 int main(int argc, char **argv) { > 8 testfunc(); > -> 9 printf ("id =3D %s\n", id); > 10 printf ("sock =3D %d\n", sock); > 11 return 0; > 12 } > (lldb) p id > error: use of undeclared identifier 'id' > (lldb) p sock > error: Couldn't materialize: couldn't get the value of variable sock: = testlib.so[0x4004] can't be resolved, testlib.so is not currently loaded > error: errored out in DoExecute, couldn't = PrepareToExecuteJITExpression > (lldb) c > id =3D aa > sock =3D 5 > Process 34787 resuming >=20 >=20 > You notice that lldb cannot display values for id or sock. It even = gives quite different messages about them. However the program can = access the values and it prints them out properly. Why can't lldb see = them? How can that be corrected? >=20 > What is even more interesting is that in the real application there = are quite a few of these global variables and lldb can display some of = them, just not all. Possibly it has to do with the specific names as = DATE generally works. sock and id never seem to work. >=20 > -- Doug >=20 > _______________________________________________ > freebsd-questions@freebsd.org mailing list > https://lists.freebsd.org/mailman/listinfo/freebsd-questions > To unsubscribe, send any mail to = "freebsd-questions-unsubscribe@freebsd.org"