Извлечение глобальных переменных из файла .out
У меня есть простая программа на C:
// it is not important to know what the code does you may skip the code
main.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;
}
(программа не делает ничего полезного ...My goal is to extract the variable names their location where they are being declared and their memory address)
Когда я компилирую программу, я получаю файлa.out
Это файл эльфа, содержащий отладочную информацию.
Кто-то в компании написал программу в .net 5 лет назад, которая получит всю эту информацию из файла a.out. Вот что возвращает код:
// Name Display Name Type Size Address
Для этой маленькой программы она отлично работает, а также для других крупных проектов.
Этот код имеет длину 2000 строк и содержит несколько ошибок, и он не поддерживает .NET версии 4. Вот почему я пытаюсь воссоздать его.
So my question isЯ теряюсь в том смысле, что не знаю, какой подход выбрать для решения этой проблемы. Вот варианты, которые я рассматривал:
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.
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.
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:
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
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, чтобы разобрать еговот.
SummaryI 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. Поскольку этот вопрос требует много, я попытался разделить его на несколько:
objdump/readelf get variables information Get location of symbols in a.out fileвозможно, я должен удалить другие вопросы. извините, что излишним.