1

Since it's my first entry on SO: hello to everyone :)

Now to the good part... I'm struggling to find out a type of a global variable from an ELF file. My source code before compiling looks like this:

#include "stdint.h"

uint8_t variable_global1;
uint8_t variable_global2 = 1;
uint8_t variable_global4;
uint16_t variable_global16b = 1;
int16_t variable_global16b8;
uint16_t variable_global16b9;

int main(void)
{
    variable_global1 = 2;
    static uint8_t variable_global3;
    static uint8_t variable_global5 = 8;
    static uint8_t variable_global6;
    variable_global6 = 7;
    variable_global16b9 = 500;
}

When I try my luck with readelf or objectdump this is the furthest I get. Result of readelf -s LEKCJA2.elf is:

Symbol table '.symtab' contains 72 entries:
Num:    Value  Size Type    Bind   Vis      Ndx Name
 0: 00000000     0 NOTYPE  LOCAL  DEFAULT  UND 
 1: 00000000     0 SECTION LOCAL  DEFAULT    1 
 2: 00800060     0 SECTION LOCAL  DEFAULT    2 
 3: 00800064     0 SECTION LOCAL  DEFAULT    3 
 4: 00000000     0 SECTION LOCAL  DEFAULT    4 
 5: 00000000     0 SECTION LOCAL  DEFAULT    5 
 6: 00000042     0 NOTYPE  LOCAL  DEFAULT    1 .do_copy_data_start
 7: 0000003e     0 NOTYPE  LOCAL  DEFAULT    1 .do_copy_data_loop
 8: 00000052     0 NOTYPE  LOCAL  DEFAULT    1 .do_clear_bss_start
 9: 00000050     0 NOTYPE  LOCAL  DEFAULT    1 .do_clear_bss_loop
10: 00000000     0 FILE    LOCAL  DEFAULT  ABS main.c
11: 0000003f     0 NOTYPE  LOCAL  DEFAULT  ABS __SREG__
12: 0000003e     0 NOTYPE  LOCAL  DEFAULT  ABS __SP_H__
13: 0000003d     0 NOTYPE  LOCAL  DEFAULT  ABS __SP_L__
14: 00000034     0 NOTYPE  LOCAL  DEFAULT  ABS __CCP__
15: 00000000     0 NOTYPE  LOCAL  DEFAULT  ABS __tmp_reg__
16: 00000001     0 NOTYPE  LOCAL  DEFAULT  ABS __zero_reg__
17: 00800064     1 OBJECT  LOCAL  DEFAULT    3 variable_global6.1217
18: 00800063     1 OBJECT  LOCAL  DEFAULT    2 variable_global5.1216
19: 00800065     1 OBJECT  LOCAL  DEFAULT    3 variable_global3.1215
20: 0000008a     0 NOTYPE  LOCAL  DEFAULT    1 __stop_program
21: 0000005c     0 NOTYPE  WEAK   DEFAULT    1 __vector_1
22: 00000026     0 NOTYPE  GLOBAL DEFAULT    1 __trampolines_start
23: 0000008c     0 NOTYPE  GLOBAL DEFAULT    1 _etext
24: 0000005c     0 NOTYPE  WEAK   DEFAULT    1 __vector_12
25: 0000005c     0 NOTYPE  GLOBAL DEFAULT    1 __bad_interrupt
26: 00000090     0 NOTYPE  GLOBAL DEFAULT  ABS __data_load_end
27: 0000005c     0 NOTYPE  WEAK   DEFAULT    1 __vector_6
28: 00000026     0 NOTYPE  GLOBAL DEFAULT    1 __trampolines_end
29: 0000005c     0 NOTYPE  WEAK   DEFAULT    1 __vector_3
30: 00800066     1 OBJECT  GLOBAL DEFAULT    3 variable_global1
31: 0000008c     0 NOTYPE  GLOBAL DEFAULT  ABS __data_load_start
32: 00000026     0 NOTYPE  GLOBAL DEFAULT    1 __dtors_end
33: 0080006c     0 NOTYPE  GLOBAL DEFAULT    3 __bss_end
34: 00800067     2 OBJECT  GLOBAL DEFAULT    3 variable_global16b9
35: 0000005c     0 NOTYPE  WEAK   DEFAULT    1 __vector_11
36: 00000026     0 NOTYPE  WEAK   DEFAULT    1 __init
37: 0000005c     0 NOTYPE  WEAK   DEFAULT    1 __vector_13
38: 0000005c     0 NOTYPE  WEAK   DEFAULT    1 __vector_17
39: 0000005c     0 NOTYPE  WEAK   DEFAULT    1 __vector_7
40: 00000048     0 NOTYPE  GLOBAL DEFAULT    1 __do_clear_bss
41: 00810000     0 NOTYPE  GLOBAL DEFAULT    4 __eeprom_end
42: 00000000     0 NOTYPE  GLOBAL DEFAULT    1 __vectors
43: 00800064     0 NOTYPE  GLOBAL DEFAULT    2 __data_end
44: 00000000     0 NOTYPE  WEAK   DEFAULT    1 __vector_default
45: 0000005c     0 NOTYPE  WEAK   DEFAULT    1 __vector_5
46: 00000026     0 NOTYPE  GLOBAL DEFAULT    1 __ctors_start
47: 00000032     0 NOTYPE  GLOBAL DEFAULT    1 __do_copy_data
48: 00800064     0 NOTYPE  GLOBAL DEFAULT    3 __bss_start
49: 0000005e    42 FUNC    GLOBAL DEFAULT    1 main
50: 0000005c     0 NOTYPE  WEAK   DEFAULT    1 __vector_4
51: 00000000     0 NOTYPE  WEAK   DEFAULT  ABS __heap_end
52: 00800060     1 OBJECT  GLOBAL DEFAULT    2 variable_global2
53: 0000005c     0 NOTYPE  WEAK   DEFAULT    1 __vector_9
54: 0000005c     0 NOTYPE  WEAK   DEFAULT    1 __vector_2
55: 00800061     2 OBJECT  GLOBAL DEFAULT    2 variable_global16b
56: 0000005c     0 NOTYPE  WEAK   DEFAULT    1 __vector_15
57: 00800069     1 OBJECT  GLOBAL DEFAULT    3 variable_global4
58: 00000026     0 NOTYPE  GLOBAL DEFAULT    1 __dtors_start
59: 00000026     0 NOTYPE  GLOBAL DEFAULT    1 __ctors_end
60: 0000045f     0 NOTYPE  WEAK   DEFAULT  ABS __stack
61: 00800064     0 NOTYPE  GLOBAL DEFAULT    2 _edata
62: 0080006c     0 NOTYPE  GLOBAL DEFAULT    4 _end
63: 0000005c     0 NOTYPE  WEAK   DEFAULT    1 __vector_8
64: 0080006a     2 OBJECT  GLOBAL DEFAULT    3 variable_global16b8
65: 00000088     0 NOTYPE  WEAK   DEFAULT    1 exit
66: 00000088     0 NOTYPE  GLOBAL DEFAULT    1 _exit
67: 0000005c     0 NOTYPE  WEAK   DEFAULT    1 __vector_14
68: 0000005c     0 NOTYPE  WEAK   DEFAULT    1 __vector_10
69: 0000005c     0 NOTYPE  WEAK   DEFAULT    1 __vector_16
70: 00800060     0 NOTYPE  GLOBAL DEFAULT    2 __data_start
71: 0000005c     0 NOTYPE  WEAK   DEFAULT    1 __vector_18

As you can see, I can list my global variables (for instance variable_global1), I also know its size in the memory, but unfortunately not the type - whether it's unsigned int, signed int, UBYTE and so on.

Finally my question: Is it even possible to figure out a type of a global variable (uninitialized or initialized) from an ELF file and if so how could I do that? For now I'm playing with different CMD tools and Python libraries for parsing ELF files, but I can't get any closer for what I'm trying to do.

I appreciate all you help :)

tomly
  • 11
  • 3

1 Answers1

3

Is it even possible to figure out a type of a global variable

In general: no.

The type of the variable is useful for the compiler so it can generate correct code, but once the code is generated, the type is not needed for anything else, and therefore not stored in the ELF file.

The type may be needed for debugging, and IF you compile with debug info, THEN the debug info will contain the type.

You can examine debug info (if present) with objdump -g or readelf -w commands.

Employed Russian
  • 199,314
  • 34
  • 295
  • 362
  • Thanks. I figured that out and ended up parsing the dump from DWARF. It's still tricky and time-consuming due to the project requirements, but it works. – tomly Dec 14 '15 at 10:38