title: Separating out symbols and stripping unneeded symbols at the same time
body: <p>I am interested in creating an external symbol file which I reference from my ELF file using <code>objcopy --add-gnu-debuglink=...</code>.</p>
  
  <p>Now I know how to extract <em>only</em> the debug symbols (<code>objcopy --only-keep-debug</code>) and how to <em>only</em> strip the debug symbols (<code>objcopy --strip-debug</code>).</p>
  
  <p>However, I'd like to strip <strong>all unneeded symbols</strong>. I know this can be done via <code>objcopy --strip-unneeded</code>. The fact that debug and unneeded symbols are two different categories and two different command line switches suggests that there could be non-overlapping symbols.</p>
  
  <p>Since unneeded symbols seem to encompass <em>more</em> than just the debug symbols, I'm afraid to lose some symbolic information when doing this (in GNU make):</p>
  
  <pre><code>stripped/%: %
      objcopy --only-keep-debug $@ $@.dbg
      objcopy --strip-unneeded $@
      objcopy --add-gnu-debuglink=$@.dbg $@
  </code></pre>
  
  <p>Can anyone explain what kind of symbols may be removed via <code>--strip-unneeded</code> that may not be removed with <code>--strip-debug</code>? Or put differently: which symbols not kept in the external symbol file by <code>--only-keep-debug</code> may get lost when running <code>--strip-unneeded</code> on the original binary? Is there a way to retain these symbols in my external symbol file or is this a non-issue?</p>
  
  <p>Goal is to be able to debug issues using the external debug symbols and not ending up missing some symbolic information and find out too late. So I don't want to lose symbolic information, I just want to remove it entirely from the program binary.</p>
  
  <p>PS: I am aware of <a href="https://stackoverflow.com/q/866721/476371">How to generate gcc debug symbol outside the build target?</a> But that question and its answers don't touch the detail I am asking about.</p>