Anonymous

Libc: Difference between revisions

From dankwiki
1,679 bytes added ,  12:22, 5 July 2020
No edit summary
 
(3 intermediate revisions by the same user not shown)
Line 35: Line 35:
               same name contained in libraries that have already been  loaded.
               same name contained in libraries that have already been  loaded.
               This flag is not specified in POSIX.1-2001.</pre>
               This flag is not specified in POSIX.1-2001.</pre>
==Generating a stack trace==
The <tt>backtrace</tt>, <tt>backtrace_symbols</tt> and <tt>backtrace_symbols_fd</tt> glibc calls can be used to generate a stack trace from within a program.
* <tt>backtrace</tt> generates a list of addresses
* <tt>backtrace_symbols</tt>, given the output of <tt>backtrace</tt>, will populate its argv-style return value with any symbols it can look up
In order to extract symbols, appropriate binaries must:
* not be stripped (or linked with -s),
* be linked with -rdynamic/--export-dynamic, and
* provide frame pointers (none for inlined code, omitted with most [[gcc]] optimization levels)
==Using an alternate Glibc==
Acquire the glibc sources, create a build directory, and run <tt>configure</tt> from that build directory. You are advised to run with <tt>--prefix=/usr</tt>, and then install to an alternative directory using e.g. <tt>make install DESTDIR=$HOME/glibc</tt>. Use <tt>--with-headers</tt> to specify kernel headers:
<pre>
[schwarzgerat](0) $ pwd
/home/dank/src/glibc/build
[schwarzgerat](0) $ ../configure --prefix=/usr --with-headers=/usr/src/linux-headers-5.7.7nlb/include/generated/uapi/
</pre>
So you've installed a local build of Glibc to, say, $HOME/glibc, eh?
Trying to run with <tt>LD_LIBRARY_PATH=$HOME/glibc/lib</tt> will result in all dynamically-linked executables segfaulting immediately. That's because they're using a mismatched loader. Try running the correct loader directly to sanity-check your library install, e.g.:
<pre>
[schwarzgerat](0) $ /bin/echo
Segmentation fault (core dumped)
[schwarzgerat](139) $ ~/glibc/lib/ld-linux-x86-64.so.2 /bin/echo this will work
this will work
[schwarzgerat](0) $
</pre>