<?xml version="1.0" encoding="utf-8" ?>
<feed xmlns="http://www.w3.org/2005/Atom">
  <title>Trifling</title>
  <link href="http://blog.fesnel.com/atom.xml" rel="self" />
  <link href="http://blog.fesnel.com" />
  <updated>2011-01-19T00:16:32-05:00</updated>
  <id>http://blog.fesnel.com</id>
  <author>
    <name>Mike Heffner</name>
    <email>mikeh@fesnel.com</email>
  </author>
  <entry>
    <title>Cleanup old EBS snapshots</title>
    <link href="http://blog.fesnel.com/blog/2010/04/16/cleanup-old-ebs-snapshots" />
    <updated>2010-04-16T00:00:00-04:00</updated>
    <id>http://blog.fesnel.com/blog/2010/04/16/cleanup-old-ebs-snapshots</id>
    <content type="html">
      &lt;p&gt;At &lt;a href=&quot;http://www.librato.com/&quot;&gt;Librato&lt;/a&gt; we rely on Amazon EBS as part of our &lt;a href=&quot;http://silverline.librato.com/&quot;&gt;Silverline&lt;/a&gt;
      infrastructure. Accordingly, we periodically snapshot our EBS volumes in case
      of data loss. I wanted to find a script that would cleanup our sprawl of
      snapshots that accumulated over time.&lt;/p&gt;
      
      &lt;p&gt;A quick search found a number of PHP solutions, but we have stuck with mostly
      BASH and Ruby for infrastructure scripting so far so we didn&#8217;t want another
      dependency. Anyways, I stumbled across &lt;a href=&quot;http://www.elastdream.com/2009/04/snapshots.html&quot;&gt;this Ruby script&lt;/a&gt; over at
      ElastDream which did most of what I was looking for. However, I wanted to be
      able to specify a minimum number of snapshots to keep, regardless of time, so
      that you can&#8217;t accidentally delete all snapshots for a particular volume.&lt;/p&gt;
      
      &lt;p&gt;This script supports the following configuration options:&lt;/p&gt;
      
      &lt;pre&gt;&lt;code&gt;Usage: cleanup_snapshots [options] &lt;volume&gt;
      
          -h, --help           Display this screen
              --key KEY        Amazon access key
              --secret KEY     Amazon secret key
              --days DAYS      How many days back to keep (default: 15)
              --min KEEP       Minimum number of snapshots to keep (default: 5)
              --verbose        Enable verbose output
              --uri URI        Use this EC2 URI instead of default (e.g. EU-West)
      &lt;/code&gt;&lt;/pre&gt;
      
      &lt;p&gt;Find the source below, hope this is helpful for others. ;-)&lt;/p&gt;
      
      &lt;script src=&quot;http://gist.github.com/368806.js&quot;&gt;&lt;/script&gt;
    </content>
  </entry>
  <entry>
    <title>Mixing HR timers with itimers</title>
    <link href="http://blog.fesnel.com/blog/2009/09/04/mixing-hr-timers-with-itimers" />
    <updated>2009-09-04T00:00:00-04:00</updated>
    <id>http://blog.fesnel.com/blog/2009/09/04/mixing-hr-timers-with-itimers</id>
    <content type="html">
      &lt;p&gt;While attempting to come up with an example for how it is difficult to
      differentiate the High Resolution (HR) timers from user timers &#8211; for EINTR
      purposes &#8211; I came across a slightly different problem. One newer kernels (at
      least &gt;= 2.6.25 but not 2.6.18), a periodically firing HR timer will appear to
      prevent an itimer from generating an EINTR at all for a blocking system call.&lt;/p&gt;
      
      &lt;p&gt;My test program creates an HR timer on a one second frequency and sets an
      itimer for three seconds. The call immediately blocks on a file lock using the
      flock system call. The itimer should fire and interrupt the flock with an
      EINTR, but most of the time the itimer will fire without interrupting the
      flock. By offsetting the HR timer 0.5 seconds, the itimer will interrupt the
      flock everytime.&lt;/p&gt;
      
      &lt;p&gt;The test program is at &lt;a href=&quot;http://github.com/mheffner/scripts/commits/master/hrtimer_vs_itimer.c&quot;&gt;github&lt;/a&gt;. I&#8217;m currently determining which kernels
      appear to demonstrate this behavior.&lt;/p&gt;
      
      &lt;p&gt;Update: The LKML thread &lt;a href=&quot;http://www.gossamer-threads.com/lists/linux/kernel/1125445&quot;&gt;here&lt;/a&gt; includes followups from Oleg and Roland
      explaining the observed behavior.&lt;/p&gt;
    </content>
  </entry>
  <entry>
    <title>Emulating tcsh&#8217;s %c in bash</title>
    <link href="http://blog.fesnel.com/blog/2009/08/26/emulating-tcshs-_c-in-bash" />
    <updated>2009-08-26T00:00:00-04:00</updated>
    <id>http://blog.fesnel.com/blog/2009/08/26/emulating-tcshs-_c-in-bash</id>
    <content type="html">
      &lt;p&gt;With tcsh you can set your prompt to include the &lt;strong&gt;%c[[0]n]&lt;/strong&gt; escape character.
      The &lt;strong&gt;%c&lt;/strong&gt; will print the current working directory with at most &#8216;n&#8217; trailing
      components. This is similar to &lt;code&gt;PROMPT_DIRTRIM&lt;/code&gt; in recent versions of bash, but
      IMO, better. In tcsh, the %c escape can optionally include a &lt;skipped&gt;
      component to signify how many directories were actually trimmed off the
      beginning of the prompt.&lt;/p&gt;
      
      &lt;p&gt;For example, the escape &lt;strong&gt;%c03&lt;/strong&gt; if you were in a directory
      &lt;code&gt;/etc/sysconfig/networking/profiles/default&lt;/code&gt; would expand to:
      &lt;code&gt;/&lt;2&gt;networking/profiles/default&lt;/code&gt;. It also respects $HOME as a start point, so
      the directory &lt;code&gt;$HOME/work/svn/cdc/scripts&lt;/code&gt; would expand to:
      &lt;code&gt;~/&lt;1&gt;svn/cdc/scripts&lt;/code&gt;.&lt;/p&gt;
      
      &lt;p&gt;Jeremie Le Hen has created an excellent &lt;a href=&quot;http://bsdjlh.blogspot.com/2009/02/bash-prompt-trick-cheap-emulation-of.html&quot;&gt;emulation&lt;/a&gt; of this functionality
      for bash using only builtins. Below is an expanded version of Jeremie&#8217;s that
      should maintain the &#8220;~&#8221; when under $HOME and handle directory names with
      spaces.&lt;/p&gt;
      
      &lt;p&gt;Use it as: &lt;code&gt;PS1=&quot;\$(traildir 3 \&quot;\$PWD\&quot;)&quot;&lt;/code&gt; to get a maximum of three trailing
      directories.&lt;/p&gt;
      
      &lt;script src=&quot;http://gist.github.com/175508.js&quot;&gt;&lt;/script&gt;
    </content>
  </entry>
  <entry>
    <title>Preloading with multiple symbol versions</title>
    <link href="http://blog.fesnel.com/blog/2009/08/25/preloading-with-multiple-symbol-versions" />
    <updated>2009-08-25T00:00:00-04:00</updated>
    <id>http://blog.fesnel.com/blog/2009/08/25/preloading-with-multiple-symbol-versions</id>
    <content type="html">
      &lt;p&gt;This post will describe the process of preloading an application using the
      LD_PRELOAD functionality of the dynamic linker and the difficulties
      encountered when using multiple symbol versions. It will walk-through the
      steps to correctly intercept symbols with multiple versions and map them to
      the correct underlying library versions.&lt;/p&gt;
      
      &lt;h3&gt;What is preloading?&lt;/h3&gt;
      
      &lt;p&gt;Preloading is a method by which a user can force the dynamic linker to load an
      additional dynamic shared object (DSO) when launching a particular executable.
      Preloading a process forces the DSO into the address space of the executing
      process and executes the DSO&#8217;s constructor on startup and its destructor on
      shutdown. Preloading a process can be down with the LD_PRELOAD environment
      variable on a process-by-process basis or can be enabled system-wide with the
      /etc/ld.so.preload configuration file.&lt;/p&gt;
      
      &lt;p&gt;Preloading a process allows new code to be inserted and executed within a
      process without recompiling or relinking the original program. When a DSO is
      preloaded, the dynamic linker places the preloaded DSO before the system
      libraries (libc, libpthread, etc.) in the symbol name search order. This means
      that when an application or one of the application&#8217;s dynamic libraries invokes
      a function within the system libraries, the function is searched for in the
      preloaded DSO first. This means that if the preloaded DSO exports a function
      that is the same name as one in the system libraries, the preloaded DSO will
      be called when the application makes a call to the function. This provides a
      powerful capability to intercept function calls made from the application.&lt;/p&gt;
      
      &lt;h3&gt;Preloading Example&lt;/h3&gt;
      
      &lt;p&gt;Preloading is typically used to interpose new functionality between the
      application and the system. This model could be used for &lt;a href=&quot;http://en.wikipedia.org/wiki/Aspect-oriented_programming&quot;&gt;Aspect-oriented
      Programming&lt;/a&gt; to weave new cross-cutting behavior into existing function
      calls at run-time without modification to the existing source code or binary.
      One example where this is common is in debugging utilities. A debugging DSO
      can be constructed that intercepts system library calls invoked by the
      application and log or track statistics on the frequency and types of calls
      made. The debugging DSO can provide valuable information that could track down
      coding errors or uncover performance bottlenecks.&lt;/p&gt;
      
      &lt;p&gt;A common use case for this facility is tracking dynamic memory operations
      performed by an application during run-time. Tracking application memory
      allocations and deallocations can locate memory leaks and identify poor memory
      allocation patterns. Take the following example of a simple DSO that wraps the
      malloc(3) call:&lt;/p&gt;
      
      &lt;script src=&quot;http://gist.github.com/169723.js&quot;&gt;&lt;/script&gt;
      
      
      &lt;p&gt;Compiling this wrapper and preloading it with the simple &lt;strong&gt;uname&lt;/strong&gt; command
      results in two allocation printouts:&lt;/p&gt;
      
      &lt;script src=&quot;http://gist.github.com/169727.js&quot;&gt;&lt;/script&gt;
      
      
      &lt;p&gt;While this is a very simple example, a more advanced debugging library could
      track both allocations and deallocations and print full reports on exit.&lt;/p&gt;
      
      &lt;h3&gt;Preloading Example #2&lt;/h3&gt;
      
      &lt;p&gt;Let&#8217;s try another preloading example, but this time let&#8217;s override the
      &lt;code&gt;pthread_cond_wait&lt;/code&gt; call from the Pthread&#8217;s library (NPTL). Once again, here is
      our wrapper code:&lt;/p&gt;
      
      &lt;script src=&quot;http://gist.github.com/169799.js&quot;&gt;&lt;/script&gt;
      
      
      &lt;p&gt;This time we&#8217;ll write a simple threaded example program to test with. The test
      program fires off a single thread that will wait on a condition variable for a
      signal. The main thread will sleep for two seconds and then signal the
      condition variable, awaking the blocked thread. The main thread will wait for
      the thread to get the signal and exit, then the main thread will exit the
      entire program.&lt;/p&gt;
      
      &lt;script src=&quot;http://gist.github.com/169798.js&quot;&gt;&lt;/script&gt;
      
      
      &lt;p&gt;Now let&#8217;s compile/link the wrapper and test program and test the normal and
      preloaded cases:&lt;/p&gt;
      
      &lt;script src=&quot;http://gist.github.com/169797.js&quot;&gt;&lt;/script&gt;
      
      
      &lt;p&gt;In the non-preloaded case our test program exited fine after two seconds.
      However, when the test program was preloaded with our wrapper it hung and had
      to be killed with a Control-C from the keyboard. Why did this hang?&lt;/p&gt;
      
      &lt;h3&gt;What happened?&lt;/h3&gt;
      
      &lt;p&gt;Why did the test program work fine when executed without a preload, but hang
      when preloaded with our wrapper? To figure this out, let&#8217;s fire up GDB and
      step through what happens when the test program calls &lt;code&gt;pthread_cond_wait&lt;/code&gt; with
      and without our preload.&lt;/p&gt;
      
      &lt;p&gt;First, let&#8217;s run the program without our preload:&lt;/p&gt;
      
      &lt;script src=&quot;http://gist.github.com/169808.js&quot;&gt;&lt;/script&gt;
      
      
      &lt;p&gt;We setup a breakpoint on the call to &lt;code&gt;pthread_cond_wait&lt;/code&gt; and verify that it
      calls &lt;code&gt;pthread_cond_wait&lt;/code&gt; within NPTL. Now let&#8217;s try the same, but with our
      wrapper preloaded:&lt;/p&gt;
      
      &lt;script src=&quot;http://gist.github.com/169818.js&quot;&gt;&lt;/script&gt;
      
      
      &lt;p&gt;Once again we break on the call to &lt;code&gt;pthread_cond_wait&lt;/code&gt;, but this time when we
      step into it it takes us to our wrapper function. This was expected, however,
      when we then step into the call to &lt;code&gt;pthread_cond_wait&lt;/code&gt; via the condwait_internal
      pointer, it takes us to &lt;code&gt;__pthread_cond_wait_2_0&lt;/code&gt; instead of the
      &lt;code&gt;pthread_cond_wait@@GLIBC_2.3.2&lt;/code&gt; function without our wrapper. What is
      &lt;code&gt;__pthread_cond_wait_2_0&lt;/code&gt; and why did it takes us there? Let&#8217;s look at the
      source for &lt;code&gt;__pthread_cond_wait_2_0&lt;/code&gt;:&lt;/p&gt;
      
      &lt;script src=&quot;http://gist.github.com/169821.js&quot;&gt;&lt;/script&gt;
      
      
      &lt;p&gt;This code is allocating another condition variable within the original
      condition variable structure and passing the new condition variable to the
      actual &lt;code&gt;pthread_cond_wait&lt;/code&gt; function. As you can see from the backtrace, the
      &lt;code&gt;pthread_cond_wait&lt;/code&gt; function is now blocking on the condition variable located
      at address 0x7ffff00008c0 rather than the original one at 0x600d80. However,
      the main thread still thinks the condition variable is located at 0x600d80.&lt;/p&gt;
      
      &lt;h3&gt;Why two different code paths?&lt;/h3&gt;
      
      &lt;p&gt;To understand why the preloaded version invoked a different &lt;code&gt;pthread_cond_wait&lt;/code&gt;
      function than the non-preloaded one, let&#8217;s take a quick look at the output of
      &lt;strong&gt;objdump&lt;/strong&gt; for the NPTL(&lt;a href=&quot;http://en.wikipedia.org/wiki/Nptl&quot;&gt;http://en.wikipedia.org/wiki/Nptl&lt;/a&gt;) library.&lt;/p&gt;
      
      &lt;script src=&quot;http://gist.github.com/169895.js&quot;&gt;&lt;/script&gt;
      
      
      &lt;p&gt;Searching for the pthread_cond_wait symbol shows that there are two different
      versions of pthread_cond_wait in &lt;strong&gt;libpthread.so.0&lt;/strong&gt; &#8211; one located at address
      0x3cf900b8f0 and the other located at 0x3cf900b240. Also, you will notice that
      each symbol is followed by a special &#8220;GLIBC_X.Y.Z&#8221; string. So what do these
      strings mean?&lt;/p&gt;
      
      &lt;p&gt;To understand why there are two different pthread_cond_wait&#8217;s with different
      string suffixes, we first must understand library versioning. In order to
      support changing the ABI definition of a symbol, the standard practice use to
      be that the SONAME of a shared library would be updated each time the ABI
      changed. This practice was referred to as &#8220;bumping the major number&#8221; because
      the major number of the library would be incremented by one. For example, the
      SONAME for a library named &lt;em&gt;libfoobar&lt;/em&gt; may be changed from &lt;em&gt;libfoobar.so.1&lt;/em&gt; to
      &lt;em&gt;libfoobar.so.2&lt;/em&gt; to isolate an ABI change in the .1 version of the library.
      Programs that required the old ABI would link against the old &#8220;.1&#8221; SONAME
      while new programs would get linked against the new &#8220;.2&#8221; SONAME. This method
      was not very practical as it required a new library name each time some small
      ABI change was made, which led to a proliferation of library versions required
      for old executables. Therefore, a new method was developed that versions the
      individual symbols in the library instead of the entire library. This allows
      ABI changes to be made while maintaining the same SONAME and single library
      file. &lt;a href=&quot;http://en.wikipedia.org/wiki/Nptl&quot;&gt;2&lt;/a&gt;&lt;/p&gt;
      
      &lt;p&gt;In this example, there are two different versions of the &lt;code&gt;pthread_cond_wait&lt;/code&gt;
      symbol. The older version, 2.2.5, is from the old &lt;a href=&quot;http://en.wikipedia.org/wiki/Linuxthreads&quot;&gt;LinuxThreads&lt;/a&gt;
      implementation, while the new version, 2.3.2, refers to the NPTL
      implementation. The double at sign (&#8220;@@&#8221;) before the 2.3.2 string indicates
      that it is the default version of the symbol. Programs that are linked against
      libpthread&#8217;s will be linked against the default version. This agrees with the
      output from our NPTL test program:&lt;/p&gt;
      
      &lt;script src=&quot;http://gist.github.com/169893.js&quot;&gt;&lt;/script&gt;
      
      
      &lt;p&gt;It should now be clear that the problem with our wrapper is that while the
      test program is linked against and requesting version 2.3.2 of
      &lt;code&gt;pthread_cond_wait&lt;/code&gt;, our wrapper is mapping that request to version 2.2.5. This
      is because of the use of dlsym in our wrapper code to lookup the real
      &lt;code&gt;pthread_cond_wait&lt;/code&gt; function. So how do we fix this?&lt;/p&gt;
      
      &lt;h3&gt;Versioned symbol lookup&lt;/h3&gt;
      
      &lt;p&gt;The dlsym call makes an &lt;em&gt;unversioned&lt;/em&gt; lookup for the named symbol. By default,
      this unversioned symbol lookup will match the oldest symbol version in the
      DSO. If dlsym mapped to the latest symbol in the DSO, then if a new version of
      the symbol is added in the future, existing programs would map automatically
      to this symbol. However, if the new symbol version changed the behavior of the
      function, then existing programs may misbehave when they are mapped to the
      latest version. It is important to note that even though the &#8220;@@&#8221; symbol
      indicates the &#8220;default symbol&#8221;, this is only true for programs that are
      linking directly with the DSO &#8211; not for lookups performed with dlsym.&lt;/p&gt;
      
      &lt;p&gt;Luckily, we can use the dlvsym function to perform a &lt;em&gt;versioned&lt;/em&gt; lookup for
      the symbol. Dlvsym call is identical to dlsym except that it accepts a third
      argument that defines the version of the symbol to search for. The version
      number is the string right of the &#8220;@&#8221; signs &#8211; so in our example,
      &lt;em&gt;GLIBC_2.3.2&lt;/em&gt; would find the latest &lt;code&gt;pthread_cond_wait&lt;/code&gt; symbol. Let&#8217;s try
      updating our wrapper:&lt;/p&gt;
      
      &lt;script src=&quot;http://gist.github.com/169910.js&quot;&gt;&lt;/script&gt;
      
      
      &lt;p&gt;This time success:&lt;/p&gt;
      
      &lt;script src=&quot;http://gist.github.com/169911.js&quot;&gt;&lt;/script&gt;
      
      
      &lt;p&gt;Our wrapper is now mapping the &lt;code&gt;pthread_cond_wait&lt;/code&gt; call to the correct library
      version, but are we done?&lt;/p&gt;
      
      &lt;h3&gt;Creating versioned overrides&lt;/h3&gt;
      
      &lt;p&gt;We started off by mapping requests for &lt;code&gt;pthread_cond_wait&lt;/code&gt; to version 2.2.5
      which caused our test program to hang when it was looking for 2.3.2. We
      successfully fixed this problem by mapping our internal wrapper to version
      2.3.2 using dlvsym. This will work fine for any executables compiled/linked on
      a recent version of the OS (post-2.2.5), however, what happens when we preload
      our wrapper on an executable linked on a 2.2.5 (LinuxThreads) OS? In this
      scenario we would have the opposite problem from before &#8211; we would take
      requests for version 2.2.5 and map them to version 2.3.2.&lt;/p&gt;
      
      &lt;p&gt;To correctly support both older and newer executables we would like to
      intercept calls to &lt;code&gt;pthread_cond_wait&lt;/code&gt; versions 2.2.5 and 2.3.2 and map them to
      the same libpthread versions, respectively. Obviously we can create two
      individual overrides for &lt;code&gt;pthread_cond_wait&lt;/code&gt; in our wrapper, but how do we
      indicate which symbol they override and prevent symbol name collision? For
      that, we look to the assembler pseudo opcode &#8220;.symver&#8221; &lt;a href=&quot;http://en.wikipedia.org/wiki/Linuxthreads&quot;&gt;3&lt;/a&gt;. With .symver we
      can define two different &lt;code&gt;pthread_cond_wait&lt;/code&gt; override functions &#8211; with
      different names &#8211; and map each of them to a corresponding versioned override
      for the real &lt;code&gt;pthread_cond_wait&lt;/code&gt;. Let&#8217;s update our wrapper code once more using
      &#8220;.symver&#8221;:&lt;/p&gt;
      
      &lt;script src=&quot;http://gist.github.com/169951.js&quot;&gt;&lt;/script&gt;
      
      
      &lt;p&gt;What we&#8217;ve done is added a second &lt;code&gt;pthread_cond_wait&lt;/code&gt; override and suffixed it
      and the existing one with the version number they are overriding. Similarly,
      we&#8217;ve added a second call to dlvsym to lookup the 2.2.5 version of the
      pthread_cond_wait symbol. Finally, we have added two inline assembly calls to
      the &#8220;.symver&#8221; instruction that connects our overrides to the respective symbol
      version name they are overriding. The .symver instructions will redirect any
      2.2.5 or 2.3.2 pthread_cond_wait lookups to the respective overrides. Now
      let&#8217;s compile this and test it out:&lt;/p&gt;
      
      &lt;script src=&quot;http://gist.github.com/169956.js&quot;&gt;&lt;/script&gt;
      
      
      &lt;p&gt;Wait, this now compiles but is failing to link?&lt;/p&gt;
      
      &lt;p&gt;The linker is complaining that it needs to know which symbol versions are
      exported. You specify these mappings to the linker using a &lt;em&gt;version-script&lt;/em&gt;.
      The version script is passed to the linker with the &#8211;version-script option.
      Each .symver opcode must correspond to an entry in the version script. For
      further explanation of the version script, I suggest reading &lt;a href=&quot;http://en.wikipedia.org/wiki/Nptl&quot;&gt;2&lt;/a&gt;.&lt;/p&gt;
      
      &lt;p&gt;For our &lt;code&gt;pthread_cond_wait&lt;/code&gt; wrapper, the corresponding version script looks
      like:&lt;/p&gt;
      
      &lt;script src=&quot;http://gist.github.com/169979.js&quot;&gt;&lt;/script&gt;
      
      
      &lt;p&gt;Now let&#8217;s try building and testing the wrapper again:&lt;/p&gt;
      
      &lt;script src=&quot;http://gist.github.com/169981.js&quot;&gt;&lt;/script&gt;
      
      
      &lt;p&gt;Success! We have produced a &lt;code&gt;pthread_cond_wait&lt;/code&gt; wrapper that overrides both
      versions 2.2.5 and 2.3.2 as illustrated with the objdump output and which
      correctly invokes the underlying &lt;code&gt;pthread_cond_wait&lt;/code&gt; functions from
      libpthread&#8217;s. We should now be able to repeat this pattern for any additional
      symbols we must override.&lt;/p&gt;
      
      &lt;h3&gt;Concluding Remarks&lt;/h3&gt;
      
      &lt;ul&gt;
      &lt;li&gt;&lt;p&gt;I strongly recommend reading Ulrich Drepper&#8217;s guide to writing shared
      libraries &lt;a href=&quot;http://en.wikipedia.org/wiki/Nptl&quot;&gt;2&lt;/a&gt; if you are planning on doing any advanced library work &#8211;
      especially writing DSO preloads.&lt;/p&gt;&lt;/li&gt;
      &lt;li&gt;&lt;p&gt;The examples here were only illustrated for the x86_64 architecture.
      There&#8217;s no guarantee that the available symbol versions for the 32-bit and
      64-bit libraries will be the same. If you are building a library which will be
      compiled for both 32-bit and 64-bit make sure to check the symbol versions for
      each underlying 32/64-bit library. This may also require different version
      script&#8217;s for each build too.&lt;/p&gt;&lt;/li&gt;
      &lt;li&gt;&lt;p&gt;These override functions were clearly very simple. If your overrides are
      more complex than mine (can&#8217;t see how they wouldn&#8217;t be) then you must make
      sure to respect the ABI differences between the multiple versions you are
      overriding. There was obviously a reason for providing multiple symbol
      versions, so make sure you respect that difference or you will break
      unexpecting applications.&lt;/p&gt;&lt;/li&gt;
      &lt;li&gt;&lt;p&gt;If your library requires using a symbol that you are also overriding, make
      sure that you internally use the symbol version marked as the default with the
      &#8221;@@&#8221; string.&lt;/p&gt;&lt;/li&gt;
      &lt;/ul&gt;
      
      
      &lt;h3&gt;References&lt;/h3&gt;
      
      &lt;p&gt;[1] Drepper, Ulrich. &lt;em&gt;ELF Symbol Versioning&lt;/em&gt;.
      &lt;a href=&quot;http://people.redhat.com/drepper/symbol-versioning&quot;&gt;http://people.redhat.com/drepper/symbol-versioning&lt;/a&gt;&lt;/p&gt;
      
      &lt;p&gt;[2] Drepper, Ulrich. &lt;em&gt;How to Write Shared Libraries&lt;/em&gt;.
      &lt;a href=&quot;http://people.redhat.com/drepper/dsohowto.pdf&quot;&gt;http://people.redhat.com/drepper/dsohowto.pdf&lt;/a&gt;&lt;/p&gt;
      
      &lt;p&gt;[3] &lt;em&gt;.symver&lt;/em&gt;. &lt;a href=&quot;http://www.redhat.com/docs/manuals/enterprise/RHEL-3-Manual/gnu-assembler/symver.html&quot;&gt;http://www.redhat.com/docs/manuals/enterprise/RHEL-3-Manual/gnu-assembler/symver.html&lt;/a&gt;&lt;/p&gt;
    </content>
  </entry>
  <entry>
    <title>Hiding what&#8217;s exposed in a shared library</title>
    <link href="http://blog.fesnel.com/blog/2009/08/19/hiding-whats-exposed-in-a-shared-library" />
    <updated>2009-08-19T00:00:00-04:00</updated>
    <id>http://blog.fesnel.com/blog/2009/08/19/hiding-whats-exposed-in-a-shared-library</id>
    <content type="html">
      &lt;p&gt;This post will illustrate how to hide what gets exposed when you distribute a
      shared library. Specifically we will focus on hiding all unnecessary symbol
      names in your distributed shared library.&lt;/p&gt;
      
      &lt;h3&gt;What are we talking about?&lt;/h3&gt;
      
      &lt;p&gt;So what are we talking about when we say we&#8217;d like to hide the internals of a
      library? Let&#8217;s take a very simple example to illustrate what can be exposed
      from a shared library. For our example we have built a very small library that
      we name &#8220;libab&#8221; and which provides a single API call, &lt;code&gt;ab_get_string&lt;/code&gt; as seen in
      the library include file:&lt;/p&gt;
      
      &lt;script src=&quot;http://gist.github.com/170443.js&quot;&gt;&lt;/script&gt;
      
      
      &lt;p&gt;This call will return a single string value. To see what this string actually
      contains we must take a look at the library source. Being the good software
      engineers that we are, we&#8217;ve broken this library into two separate components
      located in files &lt;strong&gt;filea.c&lt;/strong&gt; and &lt;strong&gt;fileb.c&lt;/strong&gt;:&lt;/p&gt;
      
      &lt;script src=&quot;http://gist.github.com/170445.js&quot;&gt;&lt;/script&gt;
      
      
      
      
      &lt;script src=&quot;http://gist.github.com/170449.js&quot;&gt;&lt;/script&gt;
      
      
      &lt;p&gt;As you can see, the &lt;code&gt;ab_get_string&lt;/code&gt; function returns a string containing two
      integer values &#8220;A&#8221; and &#8220;B&#8221;. The value for &#8220;A&#8221; is obtained from an internal
      function in A and the value for &#8220;B&#8221; is returned from the function
      b_getval_super_secret in &lt;strong&gt;fileb.c&lt;/strong&gt; &#8211; which later calls an internal function
      in B. The &#8220;internal&#8221; functions in &lt;strong&gt;filea.c&lt;/strong&gt; and &lt;strong&gt;fileb.c&lt;/strong&gt; are both defined
      as static since their scope is limited to the same file. However,
      b_getval_super_secret can not be declared static as it must be accessible from
      &lt;strong&gt;filea.c&lt;/strong&gt;.&lt;/p&gt;
      
      &lt;p&gt;Let&#8217;s build the library and see what symbols are exposed:&lt;/p&gt;
      
      &lt;script src=&quot;http://gist.github.com/170479.js&quot;&gt;&lt;/script&gt;
      
      
      &lt;p&gt;So at first look all four function names are exposed in the symbol table of
      the library, including the static functions, intra-library functions, and the
      exposed API functions. Before we investigate how to hide some of these
      symbols, let&#8217;s explore why we would want to hide anything.&lt;/p&gt;
      
      &lt;h3&gt;Why Hide?&lt;/h3&gt;
      
      &lt;p&gt;For most shared libraries that are distributed, you won&#8217;t mind if some amount
      of visibility into the library is exposed. The library itself may be openly
      built from free/open source code or there may simply be no pressing desire to
      hide the internals of a library.&lt;/p&gt;
      
      &lt;p&gt;However, there are also many cases when you do want to hide the internals as
      much as possible. For example, if there were significant Intellectual Property
      contained in the library, then internal symbol names may expose some amount of
      that IP. You may also want to hide internal functionality to prevent
      unintended use. For example, if a library exposed a symbol named
      perform_operation_foobar a curious programmer may believe they can directly
      invoke this function to perform operation &#8220;foobar&#8221;, even if the function were
      not exposed in the header file. If the symbol had unintended consequences if
      invoked directly, the enterprising programmer might hassle the company support
      system despite having operated outside of the recommended box. Additionally,
      future versions of the library may remove or change the interfaces the
      programmer was not supposed to be depending on.&lt;/p&gt;
      
      &lt;p&gt;In any case, let&#8217;s see what we can do to limit the unintended exposure.&lt;/p&gt;
      
      &lt;h3&gt;The strip command&lt;/h3&gt;
      
      &lt;p&gt;The strip command will pull out components from an object file, including
      symbols and debugging segments. Let&#8217;s perform a basic strip of our library to
      see how much we can pull out:&lt;/p&gt;
      
      &lt;script src=&quot;http://gist.github.com/170538.js&quot;&gt;&lt;/script&gt;
      
      
      &lt;p&gt;As you can see, the strip command has removed the symbol table from the shared
      library. This removed the exposure of the &#8220;internal&#8221; static functions from the
      file. Clearly, any function symbol that does not need to be exposed outside of
      the file it&#8217;s in should be declared static. However, the &lt;em&gt;dynamic&lt;/em&gt; symbol
      table still exists and contains a reference to b_getval_super_secret contained
      in &lt;strong&gt;fileb.c&lt;/strong&gt;. &lt;em&gt;Wait a minute, there are two symbol tables?&lt;/em&gt;&lt;/p&gt;
      
      &lt;p&gt;&lt;a href=&quot;http://en.wikipedia.org/wiki/Executable_and_Linkable_Format&quot;&gt;ELF&lt;/a&gt; files contain two symbol tables: the .symtab table and the .dynsym
      table (or dynamic symbol table). In simple terms, the symtab table contains
      all local and global symbol names primarily for the purpose of debugging
      capabilities. When you load GDB and inspect a backtrace, it will use this
      symbol table to print the names of the function symbols in the backtrace.
      Otherwise, without this the backtrace would only include hex address offsets
      in the file. The dynsym table contains the list of global symbols that are
      required for run-time dynamic linking and hence can not be removed. The
      dynamic linker will check this section to find symbols required by an
      executable. For a full explanation of the ELF symbol tables, please read &lt;a href=&quot;http://en.wikipedia.org/wiki/Executable_and_Linkable_Format&quot;&gt;1&lt;/a&gt;.&lt;/p&gt;
      
      &lt;p&gt;So we have removed our internal function names from the file, however, can we
      do better?&lt;/p&gt;
      
      &lt;h3&gt;Defining visibility&lt;/h3&gt;
      
      &lt;p&gt;We can&#8217;t define the b_getval_super_secret function &lt;em&gt;static&lt;/em&gt; because we must be
      able to access it from &lt;strong&gt;filea.c&lt;/strong&gt;. Fortunately, we can use the symbol
      &lt;em&gt;visibility&lt;/em&gt; option. The -fvisibility= option allows us to define the
      visibility for all exported symbols from the library. The option supports four
      modes, but we&#8217;ll limit our discussion to the two practical ones default and
      hidden. The &lt;em&gt;default&lt;/em&gt; visibility mode exports all global symbols from the
      library, making them available in the dynamic symbol table. Obviously, if the
      -fvisibility= option is not specified, the default is &lt;em&gt;default&lt;/em&gt;. On the other
      hand, the &lt;em&gt;hidden&lt;/em&gt; option will not export any symbol that is not explicitly
      marked otherwise (more later).&lt;/p&gt;
      
      &lt;p&gt;Let&#8217;s try switching the visibility to &lt;em&gt;hidden&lt;/em&gt; in our build:&lt;/p&gt;
      
      &lt;script src=&quot;http://gist.github.com/170584.js&quot;&gt;&lt;/script&gt;
      
      
      &lt;p&gt;With the -fvisibility=hidden we have been able to remove the
      b_getval_super_secret symbol from the dynamic symbol table. However, this
      removed the ab_get_string symbol from the table as well. Since the exported
      API symbol does not exist in the dynamic symbol table anymore, our test
      program fails to run with an undefined symbol error. How can we hide the
      internal global symbols, but still export the API symbols?&lt;/p&gt;
      
      &lt;h3&gt;Defining per-symbol visibility&lt;/h3&gt;
      
      &lt;p&gt;Luckily, we can also define symbol visibility at the per-symbol granularity.
      GCC supports the &lt;em&gt;visibility&lt;/em&gt; attribute that we can use to annotate our
      functions with their appropriate visibility. Since we want the ab_get_string
      symbol to have global visibility, let&#8217;s define it to have &lt;em&gt;default&lt;/em&gt;
      visibility. We&#8217;ll update &lt;strong&gt;filea.c&lt;/strong&gt; to use the attribute:&lt;/p&gt;
      
      &lt;script src=&quot;http://gist.github.com/170594.js&quot;&gt;&lt;/script&gt;
      
      
      &lt;p&gt;We added a function prototype and suffixed it with the default visibility
      attribute. Let&#8217;s see if this works:&lt;/p&gt;
      
      &lt;script src=&quot;http://gist.github.com/170597.js&quot;&gt;&lt;/script&gt;
      
      
      &lt;p&gt;Success! We now have a library that exports only the single API symbol it
      supports. All other internal symbol names are completely stripped from the
      binary file.&lt;/p&gt;
      
      &lt;h3&gt;Concluding Remarks&lt;/h3&gt;
      
      &lt;ul&gt;
      &lt;li&gt;&lt;p&gt;We could have just as easily annotated the b_getval_super_secret function
      with &lt;em&gt;hidden&lt;/em&gt; visibility and kept the default visibility as &lt;em&gt;default&lt;/em&gt;.
      However, making the default &lt;em&gt;hidden&lt;/em&gt; you guarantee that only functions
      explicitly defined to have global visibility are exported. If tomorrow we
      added a function in &lt;strong&gt;fileb.c&lt;/strong&gt; we would have to make sure to also mark it
      hidden.&lt;/p&gt;&lt;/li&gt;
      &lt;li&gt;&lt;p&gt;You can also use &lt;em&gt;export maps&lt;/em&gt; to define exported symbols in a map file
      that is parsed by the linker. See [2] for more information.&lt;/p&gt;&lt;/li&gt;
      &lt;li&gt;&lt;p&gt;Reducing the number of symbols in the dynamic symbol table can also
      improve performance and runtime. The larger the table the more entries the
      dynamic linker must load and search through.&lt;/p&gt;&lt;/li&gt;
      &lt;li&gt;&lt;p&gt;I suggest reading [2] for a full explanation of symbol visibility and all
      possible options.&lt;/p&gt;&lt;/li&gt;
      &lt;/ul&gt;
      
      
      &lt;h3&gt;Reference&lt;/h3&gt;
      
      &lt;p&gt;[1] Bahrami, Ali. &lt;em&gt;Inside ELF Symbol Tables&lt;/em&gt;.
      &lt;a href=&quot;http://blogs.sun.com/ali/entry/inside_elf_symbol_tables&quot;&gt;http://blogs.sun.com/ali/entry/inside_elf_symbol_tables&lt;/a&gt;&lt;/p&gt;
      
      &lt;p&gt;[2] Drepper, Ulrich. &lt;em&gt;How to Write Shared Libraries&lt;/em&gt;.
      &lt;a href=&quot;http://people.redhat.com/drepper/dsohowto.pdf&quot;&gt;http://people.redhat.com/drepper/dsohowto.pdf&lt;/a&gt;&lt;/p&gt;
    </content>
  </entry>
</feed>
