Symbol table from readelf shows static variable size 0

99 views Asked by At

I'm looking into the possibilities to get a list of all global and static variables that are in my application, with their name, size, and address.

My toolchain (TI CCS 12.3) produces an ELF output file, so I thought I would give the readelf tool a try. I tested with these variables in my application code:

typedef struct
{
    uint32_t ui32;
    uint16_t ui16;
    uint8_t  ui8;
} T_TEST_STRUCT;

uint8_t Test_ui8_glob;
uint8_t Test_ui8_glob_init = 0;
static uint8_t Test_ui8_stat;
static uint8_t Test_ui8_stat_init = 0;

uint16_t Test_ui16_glob;
uint16_t Test_ui16_glob_init = 0;
static uint16_t Test_ui16_stat;
static uint16_t Test_ui16_stat_init = 0;

uint32_t Test_ui32_glob;
uint32_t Test_ui32_glob_init = 0;
static uint32_t Test_ui32_stat;
static uint32_t Test_ui32_stat_init = 0;

T_TEST_STRUCT Test_struct_glob;
T_TEST_STRUCT Test_struct_glob_init = { 0, 0, 0 };
static T_TEST_STRUCT Test_struct_stat;
static T_TEST_STRUCT Test_struct_stat_init = { 0, 0, 0 };

Then after running 'readelf -s mytest.out' I got these results for those variables:

   Num:    Value  Size Type    Bind   Vis      Ndx Name
...
    77: 2000fed8     0 OBJECT  LOCAL  HIDDEN    14 Test_ui8_stat
    78: 20010745     1 OBJECT  LOCAL  HIDDEN    13 Test_ui8_stat_init
    79: 2000feda     0 OBJECT  LOCAL  HIDDEN    14 Test_ui16_stat
    80: 20010748     2 OBJECT  LOCAL  HIDDEN    13 Test_ui16_stat_init
    81: 2000fedc     0 OBJECT  LOCAL  HIDDEN    14 Test_ui32_stat
    82: 20010750     4 OBJECT  LOCAL  HIDDEN    13 Test_ui32_stat_init
    83: 2001008c     8 OBJECT  LOCAL  HIDDEN    14 Test_struct_stat
    84: 20010838     8 OBJECT  LOCAL  HIDDEN    13 Test_struct_stat_init
...
 25329: 20010123     1 COMMON  GLOBAL HIDDEN    14 Test_ui8_glob
 25330: 2001011e     2 COMMON  GLOBAL HIDDEN    14 Test_ui16_glob
 25331: 200100cc     4 COMMON  GLOBAL HIDDEN    14 Test_ui32_glob
 25332: 200100ac     8 COMMON  GLOBAL HIDDEN    14 Test_struct_glob
...
 25334: 20010830     8 OBJECT  GLOBAL HIDDEN    13 Test_struct_glob_init
...
 25339: 20010744     1 OBJECT  GLOBAL HIDDEN    13 Test_ui8_glob_init
...
 25341: 20010746     2 OBJECT  GLOBAL HIDDEN    13 Test_ui16_glob_init
 25342: 2001074c     4 OBJECT  GLOBAL HIDDEN    13 Test_ui32_glob_init

Most of this is OK for me, except when a variable is defined as static and not initialized. Then the size appears as 0. In all other cases the correct size is shown, even for an uninitialized static struct.

What is the cause of this behavior, and is there a way to get the size of these uninitialized static variables?

Thanks, Arjan

2

There are 2 answers

2
0___________ On

Variables not initialized and having static storage duration are placed in the .bss segment. Look there, but it is very likely that you will see only the total size of this segment

2
Ambroyz On

Try to use enumeration of symbols with type filtration. For example,

objdump -t mytest.out | grep -P '   \b(a|b|c|d|e|f|O)\b'