2

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 Answers2

0

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

0___________
  • 60,014
  • 4
  • 34
  • 74
0

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'
Ambroyz
  • 3
  • 4
  • I work in Windows so I can't use that particular grep command, however when using objdump I get similar results in the output: uninitialized static variables have size 0, the others are OK. 2000fed8 l .bss 00000000 Test_ui8_stat 20010754 l .data 00000001 Test_ui8_stat_init 2000feda l .bss 00000000 Test_ui16_stat 20010758 l .data 00000002 Test_ui16_stat_init 2000fedc l .bss 00000000 Test_ui32_stat 20010760 l .data 00000004 Test_ui32_stat_init 00000000 g .bss 00000001 Test_ui8_glob 00000000 g .bss 00000002 Test_ui16_glob – Arjan Oskam Jul 27 '23 at 09:34
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 01 '23 at 02:38