Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 16 Oct 2019 16:19:52 -0700
From:      Doug Hardie <bc979@lafn.org>
To:        MJ <mafsys1234@gmail.com>
Cc:        Doug Hardie <bc979@lafn.org>, FreeBSD Questions <freebsd-questions@freebsd.org>
Subject:   Re: Extension to previous posts: Problems with ld, libc, and "struct stat"
Message-ID:  <FD1D1CB6-3B4F-4298-B100-DEDC4A368C67@mail.sermon-archive.info>
In-Reply-To: <ccb4b6bc-df7e-eab5-bf41-085f979ad53e@gmail.com>
References:  <E92C7F26-912F-4443-A37B-E7AF9E025CD8@mail.sermon-archive.info> <ccb4b6bc-df7e-eab5-bf41-085f979ad53e@gmail.com>

next in thread | previous in thread | raw e-mail | index | archive | help
> On 16 October 2019, at 16:09, MJ <mafsys1234@gmail.com> wrote:
>=20
>=20
> On 17/10/2019 8:02 am, Doug Hardie wrote:
>> Here is an issue that has plagued me for some time:
>>=20
>> testlib.c:
>> #include <sys/stat.h>
>> #include <stdio.h>
>> #include <string.h>
>> #include <strings.h>
>>=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 <stdio.h>
>>=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
> Well it's obviously wrong. It's a bug in lldb. Unless you have to =
specifically load the shared library in? (process load testlib?)
>=20
> I tested this with gdb, it works as expected. That's probably why I =
still use gdb...
>=20

I tried gdb on 12.0 and got the following:

master# /usr/libexec/gdb testprog
GNU gdb 6.1.1 [FreeBSD]
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you =
are
welcome to change it and/or distribute copies of it under certain =
conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for =
details.
This GDB was configured as "amd64-marcel-freebsd"...
(gdb) b main
Breakpoint 1 at 0x201366: file testprog.c, line 8.
(gdb) r
Starting program: /usr/home/doug/zzz/testprog=20

Breakpoint 1, main (argc=3D1, argv=3D0x7fffffffeb30) at testprog.c:8
8	  testfunc();
Current language:  auto; currently minimal
(gdb) n
Size of testlib.c is 268 bytes.
9	  printf ("id =3D %s\n", id);
(gdb) p id
$1 =3D 0x80024b000 ""
(gdb) p sock
$2 =3D 0
(gdb) n
id =3D aa
10	  printf ("sock =3D %d\n", sock);
(gdb)=20
sock =3D 5
11	  return 0;
(gdb) ^DThe program is running.  Exit anyway? (y or n) y


It doesn't throw an error, but it shows incorrect values.

-- Doug




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?FD1D1CB6-3B4F-4298-B100-DEDC4A368C67>