mercredi 5 août 2015

How much DATA section memory is loaded along with Code section in C


I have created a shared library where I have static const data section and code section of 4 functions.

Here are the details of my static const data section,

static const u32 T0[256] = { 256 NON_ZERO value };
static const u32 T1[256] = {256  NON_ZERO value };
static const u32 T2[256] = {256  NON_ZERO value };
static const u32 T3[256] = {256  NON_ZERO value };
static const u32 T4[256] = {256  NON_ZERO value };
static const u32 T5[256] = {256  NON_ZERO value };
static const u32 T6[256] = {256  NON_ZERO value };
static const u32 T7[256] = {256  NON_ZERO value };
static const u32 T8[256] = {256  NON_ZERO value };
static const u32 T9[10] = {10 NON_ZERO value };

Different functions defined just below the static const section

int A();
int B();

// Access different index of T0 - T4 table 
void C();

void D();

As per my understanding the text/code section will contain the executable instruction whereas data section will contain initialized static data ( for simplicity in my case all are static const)

Inside C() function , different index of T0,T1,T2 and T3 are accessed in random order.

Intentionally, inside C(), I have not accessed T0[0].

However, every time I call C() function, it is loading T0[0] irrespective of whether T0[0] is accessed or not inside C() function.

My question is how much adjacent memory of data section is loaded along with code section?

I was thinking may be whole 4KB page loaded, so everytime C() function called, whole 4KB page loaded , therefore T0[0] is also loaded along with it.

but, the experimental result shows THIS CONCEPT IS NOT TRUE/CORRECT.

Let me explain it elaborately as below.

I have calculated the distance between function C() and different static const data as below

Base address of C - Base address of T0[0] = 3221 Bytes
Base address of C - Base address of T1[0] = 4345 Bytes
Base address of C - Base address of T2[0] = 5369 Bytes
Base address of C - Base address of T3[0] = 6393 Bytes

So, whenever C() is invoked , ONLY 64Bytes (i.e. T0[0] ) is loaded . T0[1], T0[2],... the part of T0 array which also belong to same 4KB page with C() NOT LOADED.

In other experiment, when I add another static const of 64Byte ( like static const int DATA=10; ) before static const u32 T0[256] = {.....}

these distances become

Base address of C - Base address of T0[0] =3385 Bytes [ =64 + Base address of C - Base address of T0[0]]
Base address of C - Base address of T1[0] = 4345+64 Bytes =4409 Bytes [=64 + Base address of C - Base address of T0[0]+1024]
Base address of C - Base address of T2[0] = 5369+64 Bytes = 5433 Bytes[=64 + Base address of C - Base address of T0[0]+2*1024]
Base address of C - Base address of T3[0] = 6393 +64 Bytes = 6457 Bytes[=64 + Base address of C - Base address of T0[0]+3*1024]

Now, although T0[0] still present in same 4KB page with C(), it is not loaded whenever C() is invoked.

Note : To calculate whether T0[0] is loaded or not , before invoking C(), I just flushed T0[0] using clflush() instruction and then after C() is invoked I have calculated access time using rdtsc().

Can you help me to explain/understand WHY T0[0] is always accessed/loaded whenever C() is invoked although it is not accessed/used inside C() ?

or any link to understand the memory layout of program and size of memory loaded during execution of program.

I am using Debian OS and gcc compiler.

Thanks in advance



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire