简体   繁体   中英

Why is my data and bss segment memory usage zero?

I have this code which I compiled for MSP430 platform using msp430-gcc.

#include<stdio.h>
#include<stdint.h>

// Preprocessor definitions
#define ROR(x,r) ((x>>r) | (x << (64-r)))
#define ROL(x,r) ((x << r) | (x >> (64-r)))
#define R(x,y,k) (x = ROR(x,8), x+=y, x^=k, y = ROL(y,3), y^=x)

void encrypt(uint64_t* , uint64_t*, uint64_t*);

void main(){
    uint64_t key[2] ={0x0706050403020100, 0x0f0e0d0c0b0a0908}  ;    // key
    uint64_t plain_text[2] = {0x7469206564616d20, 0x6c61766975716520};
    uint64_t cipher_text[2] = {0,0};      //initializing cipher text to 0s
    uint64_t* pt = plain_text;
    uint64_t* ct = cipher_text;   // Cipher Text
    uint64_t* k = key;    // 64 b
    encrypt(pt, ct, k);
}

 /*
 * Ecnryption Method
 */
void encrypt(uint64_t* pt, uint64_t* ct, uint64_t* k){

    uint64_t i;
    uint64_t B = k[1];
    uint64_t A = k[0];
    // Encryption rounds
    for(i=0; i<32; i++){
        R(ct[1], ct[0], A);
        R(B,A, i);        
        }
    }

I wanted the memory segment usage statistics for this program and I did that by generating the object file and using the $ size command. The result I got out of that is as below:

text       data     bss     dec     hex filename
    278       0       0     278     116 encrypt.o

I dont understand why the data segment, which tells me about my RAM usage is zero. I am assuming that my bss segment is zero as I don't have any uninitialized variables.

I would be really grateful if somebody can explain me what is happening here. Thank you.

The data section contains initialized data, ie, global or static variables with a non-zero value:

int x = 5;
void f() {
    static int y = 42;
}

The bss section contains uninitialized data, ie, global or static variables with the default (zero) value:

int x;
void f() {
    static int y;
}

Your program contains no such variables, so these sections are empty.

All your variables are automatic , ie, on the stack. The stack is allocated dynamically, and there is no easy way to detect the maximum stack size by just looking at the object file.

What you are declaring are local variables. They will be stored in the stack. In the general case, the RAM size occupied by the stack cannot be determined at the compile time. However, for simple applications such a yours it's possible to estimate the upper bound of stack usage, either by using objdump and counting the bytes manually, or using some of existing tools for static analysis.

To store the variables in .data and .bss segments, use the static type qualifier:

void main() {
     static uint64_t key[2] ={0x0706050403020100, 0x0f0e0d0c0b0a0908}; 

This is a better practice; dynamic allocation such as allocation in the stack is not something you want to do in microcontroller applications.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM