1 ate: Mon, 15 Dec 2008 15:23:31 -0500
2 From: Stephen McCamant <smcc@CSAIL.MIT.EDU>
3 To: Igor Shaul <mindmaze@gmail.com>
4 Cc: valgrind-developers@lists.sourceforge.net
5 Subject: Re: [Valgrind-developers] Using standard C library in valgrind tool
7 RW> On Dec 12, 2008, at 6:33 PM, "Igor Shaul" <mindmaze@gmail.com> wrote:
10 IS> I would like to write a valgrind tool that uses the standard c
11 IS> library (actually, I must link my tool to another library, which
12 IS> happens to use stdlib). I noticed that all the tools link with -
13 IS> nodefaultlibs flag, and if said flag is removed, then naturally no
14 IS> main() is found (stdlib requires a main). So, is there a natural
15 IS> way to use stdlib in my valgrind tool?
17 >>>>> "RW" == Robert Walsh <rjwalsh@durables.org> writes:
19 RW> Sadly, no. Valgrind shares the address space of the guest process,
20 RW> which would mean libc would get linked into the address space
21 RW> twice. There's no telling how libc would react to that.
23 Though I agree that the short answer is "sorry, that's not supported",
24 I thought you might find a few more technical details helpful in
25 considering what to do.
27 Valgrind tools and the guest processes do run in the same address
28 space in terms of memory management, but in current versions they
29 don't share any dynamic linker context, and in fact Valgrind tools
30 don't link with libc at all, so there wouldn't be a double-linking
33 However, there are some incompatibilities between Valgrind and glibc
34 that are the reason Valgrind tools don't link with the standard
35 libraries. The most fundamental one is that Valgrind and glibc are
36 both designed with the assumption that they alone will be talking to
37 the kernel on behalf of their process, but obviously this can't be
38 true for both at once.
40 As of a few years ago, it was still possible (though unsupported) to
41 just link your tool directly with /usr/lib/libc.a, and it worked for
42 at least a reasonable subset of programs and glibc functionality. I
43 research tool I was working on did that for a while. However, we gave
44 that up because of a further issue that affects glibcs configured with
45 thread-local storage (which I think is standard these days). Glibc now
46 uses a segment pointed to by %gs to keep TLS, other thread data,
47 -fstack-protector canary values, and who knows what else. It relies on
48 its startup code to initialize this correctly, so if you call many
49 glibc functions before initializing this, it crashes. That's the point
52 In theory, I don't think any of these Valgrind/glibc incompatibilities
53 are fundamental, and there would be ways of hacking around them. But
54 the glibc and Valgrind developers don't consider them bugs, and so
55 probably aren't interesting it expending much effort to fix them.
57 So if you need C standard library functionality that isn't covered by
58 the Valgrind core's somewhat non-standard subset, you'll have to get
59 it from somewhere else. What we found to be the easiest approach in
60 our Fjalar tool was to cut and paste the particular functions we need
61 from dietlibc (a nice lightweight implementation) or glibc itself. The
62 code is GPLed if you want to reuse it.
64 http://groups.csail.mit.edu/pag/fjalar/