从 a.out 文件中提取全局变量 [英] Extract global variables from a.out file

查看:46
本文介绍了从 a.out 文件中提取全局变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的 C 程序:

I have a simple C program:

   // it is not important to know what the code does you may skip the code 

ma​​in.c

#include <bsp.h>

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 < 200000) {
        i++;
    }

    BSP_Test();      
    SOME_LARGE_VARIABLE=3;    
    Foo();
}

bsp.c

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;
}

(该程序没有做任何有用的事情......我的目标是提取变量名,它们被声明的位置和它们的内存地址)

(the program does not do anything useful... My goal is to extract the variable names their location where they are being declared and their memory address)

当我编译程序时,我得到文件 a.out 这是一个包含调试信息的 elf 文件.

When I compile the program I get the file a.out which is an elf file containing debug information.

公司有人在 5 年前在 .net 中编写了一个程序,该程序将从 a.out 文件中获取所有这些信息.这是代码返回的内容:

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:

   //  Name          Display Name                    Type      Size     Address

对于这个小程序来说效果很好,也适用于其他大型项目.

For this small program it works great and also for other large projects.

该代码有 2000 行长,有几个错误,它不支持 .NET 版本 4.这就是我尝试重新创建它的原因.

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.

所以我的问题是,我很迷茫,我不知道要采取什么方法来解决这个问题.这些是我一直在考虑的选项:

So my question is, 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:

  1. 整理我在第一张图片上展示的程序的错误代码,并尝试查看它的作用以及它如何解析 a.out 文件以获取该信息.一旦我完全理解它,试着找出它为什么不支持版本 3 和 4.

  1. 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.

我可以创建正则表达式,因此可以尝试通过执行以下操作在 a.out 文件中查找模式: 到目前为止,我能够找到只有一个文件 (main.c) 的模式.但是当有多个文件时,它会变得更加复杂.我还没试过.或许它不会那么复杂,并且可以找到模式.

I am ok at creating regex expressions so maybe try to look for the pattern in the a.out file by doing something like: 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.

安装Cygwin 这样我就可以在windows上使用linux命令了,比如<代码>objdump、nmelfread.当我使用诸如 readelf -w a.out 之类的命令时,我还没有玩够这些命令,我​​得到了我需要的更多信息.我没有花太多时间使用这种方法有一些缺点:

Install Cygwin so that I can use linux commands on windows such as objdump, nm or elfread. I have't played enough with the commands when I use those commands such as readelf -w a.out I get way more information that I need. There are some cons why I have not spend that much time with this approach:

  • 缺点:在 Windows 上安装 cygwin 需要一些时间,并且在将此应用程序提供给我们的客户时,我们不希望他们必须安装它.也许有一种方法可以只安装命令 objdump 和 elfread 而不必安装整个东西

  • 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

优点:如果我们找到正确的命令来使用,我们将不会重新发明轮子并节省一些时间.也许是解析命令结果的问题,例如 objdump -w a.out

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 objdump -w a.out

<小时>

如果您想下载 a.out 文件以解析它这里它是.

我将能够获取 a.out 文件中的全局变量.我想知道每个变量是什么类型(int、char、..),它们有什么内存地址,我还想知道在哪个文件上声明了变量(main.c 或 someOtherFile.c).如果我不必使用 cygwin,我将不胜感激,因为这将使部署更容易.由于这个问题要求很多,我试图将其拆分为更多:

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. Since this question asks for a lot, I attempted to split it into more:

也许我应该删除其他问题.很抱歉是多余的.

perhaps I should delete the other questions. sorry being redundant.

推荐答案

这就是我要做的.为什么要重新发明轮子!

Here is what I will do. Why reinvent the wheel!

  1. 这里.

在bin目录下应该有:readelf.exe

on the bin directory there should be: readelf.exe

请注意,我们不需要 Cygwin 或任何程序,因此部署很简单!

Note we will not need Cygwin or any program so deploying will be simple!

一旦我们在 cmd 中执行该文件:

Once we have that file execute in cmd:

// cd "path where readelf.exe is"
readelf.exe -s a.out

这是即将发布的列表:

因此,如果您看一下,我们有兴趣获取所有大小大于 0 的 OBJECT 类型的变量.

so if you take a look we are interested in getting all the variables that are of type OBJECT with size greater than 0.

获得变量后,我们可以使用 readelf.exe -w a.out 命令查看树,它看起来像: 让我们开始寻找我们在步骤 2 中找到的变量之一 (SOME_GREAT_COUNTER) 请注意,在顶部我们知道变量正在被声明,我们得到了更多信息,例如声明它的行和内存地址

Once we got the variables we can use the readelf.exe -w a.out command to take a look at the tree and it looks like: let's start looking for one of the variable we found on step 2 (SOME_GREAT_COUNTER) Note that at the top we know the location where the variable is being declared, we got more information such as the line where it was declared and the memory address

我们最不想做的就是获取类型.如果您看一下,我们会看到类型是 = <0x522>.这意味着我们必须前往树的 522 以获取有关该时间的更多信息.如果我们去那部分,我们会得到: 通过查看树我们知道SOME_LARGE_VARIABLE 是 unsigned long 类型

The last thing we are missing to do is to get the type. if you take a look we see that the type is = <0x522>. What that means is that we have to go to 522 of the tree to get more info about that time. If we go to that part this is what we get: From looking at the tree we know that SOME_LARGE_VARIABLE is of type unsigned long

这篇关于从 a.out 文件中提取全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆