title: Extract global variables from a.out file
body: <h2>Edit (updated question)</h2>
  
  <p>I have a simple C program:   </p>
  
  <pre><code>   // it is not important to know what the code does you may skip the code 
  </code></pre>
  
  <p><strong>main.c</strong></p>
  
  <pre><code>#include &lt;bsp.h&gt;
  
  unsigned int   AppCtr;
  unsigned char  AppFlag;
  int SOME_LARGE_VARIABLE;
  
  static  void  AppTest (void);
  
  void  main (void)
  {
      AppCtr  = 0;
      AppFlag = 0;        
      AppTest();
  }
  
  static void Foo(void){
      SOME_LARGE_VARIABLE=15; 
  }
  
  
  static  void  AppTest (void)
  {
      unsigned int  i;
      i = 0;
      while (i &lt; 200000) {
          i++;
      }
  
      BSP_Test();      
      SOME_LARGE_VARIABLE=3;    
      Foo();
  }
  </code></pre>
  
  <p><strong>bsp.c</strong></p>
  
  <pre><code>extern int SOME_LARGE_VARIABLE;
  extern unsigned char  AppFlag;
  
  unsigned int long My_GREAT_COUNTER;
  
  void  BSP_Test (void) {
    SOME_LARGE_VARIABLE = 5;
    My_GREAT_COUNTER = 4;
  }
  </code></pre>
  
  <p>(the program does not do anything useful... <strong>My goal is to extract the variable names their location where they are being declared and their memory address</strong>)</p>
  
  <p>When I compile the program I get the file <code>a.out</code> which is an elf file containing debug information.</p>
  
  <p>Someone on the company wrote a program in .net 5 years ago that will get all this information from the a.out file. This is what the code returns:</p>
  
  <pre><code>   //  Name          Display Name                    Type      Size     Address
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/5jRXM.png" alt="enter image description here"></p>
  
  <p>For this small program it works great and also for other large projects.</p>
  
  <p>That code is 2000 lines long with several bugs and it does not support .NET version 4. That's why I am trying to recreate it. </p>
  
  <hr>
  
  <p><strong>So my question is</strong>, I am lost in the sense that I don't know what approach to take in order to solve this problem. These are the options I have been considering:</p>
  
  <ol>
  <li><p>Organize the buggy code of the program I showed on the first image and try to see what it does and how it parses the a.out file in order to get that information. Once I fully understand it try to figure out why it does not support version 3 and 4.</p></li>
  <li><p>I am ok at creating regex expressions so maybe try to look for the pattern in the a.out file by doing something like: <img src="https://i.stack.imgur.com/fpoK8.png" alt="enter image description here">                                                So far I was able to find the pattern where there is just one file (main.c). But when there are several files it get's more complicated. I haven't tried it yet. Maybe it will be not that complicated and it will be possible to find the pattern.</p></li>
  <li><p>Install <a href="http://www.cygwin.com/install.html" rel="nofollow noreferrer">Cygwin</a> so that I can use linux commands on windows such as <code>objdump</code>, <code>nm</code> or <code>elfread</code>. I have't played enough with the commands when I use those commands such as <code>readelf -w a.out</code> I get way more information that I need. There are some cons why I have not spend that much time with this approach:</p>
  
  <ul>
  <li><p>Cons: It takes a while to install cygwin on windows and when giving this application to our customers we don't want them to have to install it. Maybe there is a way of just installing the commands objdump and elfread without having to install the whole thing</p></li>
  <li><p>Pros: If we find the right command to use we will not be reinventing the wheel and save some time. Maybe it is a matter of parsing the results of a command such as <code>objdump -w a.out</code></p></li>
  </ul></li>
  </ol>
  
  <hr>
  
  <p>In case you want to download the a.out file in order to parse it <a href="http://soundeliverance.com:8080/temp/elf/a.out" rel="nofollow noreferrer">here it is</a>. </p>
  
  <hr>
  
  <h2>Summary</h2>
  
  <p><strong>I will to be able to get the global variables on a.out file. I will like to know what type each variable is (int, char, ..), what memory address they have and I will also like to know on what file the variable is being declared (main.c or someOtherFile.c). I will appreciate if I don't have to use cygwin as that will make it more easy to deploy.</strong> Since this question asks for a lot, I attempted to split it into more:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/11039843/objdump-readelf-get-symbol-types">objdump/readelf get variables information</a></li>
  <li><a href="https://stackoverflow.com/questions/11038731/get-location-of-symbols-in-a-out-file">Get location of symbols in a.out file</a> </li>
  </ul>
  
  <p>perhaps I should delete the other questions. sorry being redundant. </p>