Difference between revisions of "Where do your variables stored"
Proj user16 (talk | contribs)  (Created page with "=== BSS and Data Segments ===  <pre> What are text,.bss and data sections in an Embedded C program?  Where do your program recide?Obviously memory,but how is it organized? In...")  | 
				Proj user16 (talk | contribs)   (→BSS and Data Segments)  | 
				||
| Line 1: | Line 1: | ||
=== BSS and Data Segments ===  | === BSS and Data Segments ===  | ||
| − | + | ||
What are text,.bss and data sections in an Embedded C program?  | What are text,.bss and data sections in an Embedded C program?  | ||
Where do your program recide?Obviously memory,but how is it organized?  | Where do your program recide?Obviously memory,but how is it organized?  | ||
| + | |||
In embedded systems ,which are RAM-memory constrained ,memory map is divided into segments  | In embedded systems ,which are RAM-memory constrained ,memory map is divided into segments  | ||
called text,data and bss.  | called text,data and bss.  | ||
| − | Text segment:Contains code and constants of the program.Text section is allocated on flash.  | + | '''Text segment:'''  | 
| + | |||
| + | Contains code and constants of the program.Text section is allocated on flash.  | ||
| + | |||
	Eg1: a.out :executable code    | 	Eg1: a.out :executable code    | ||
| + | |||
	Eg2:const uint8_t i=8 :contant  | 	Eg2:const uint8_t i=8 :contant  | ||
| + | |||
	Eg3:  | 	Eg3:  | ||
| − | + | 	<pre>  | |
| + | |||
	#include <stdio.h>  | 	#include <stdio.h>  | ||
| + | |||
	const int global_var=20;  | 	const int global_var=20;  | ||
| + | |||
	int data_variable = 500;  | 	int data_variable = 500;  | ||
| + | |||
	static int static_var=10;  | 	static int static_var=10;  | ||
| − | 	void foo(){  | + | |
| − | + | 	void foo()  | |
| − | + |         {  | |
| − | + | 	   const int local_constant=100;	  | |
| − | + | ||
| − | + |            int local;  | |
| − | + | ||
| − | + |            local = 3;  | |
| − | + | ||
| + |            int un-initialized;  | ||
| + | |||
| + | 	   int another-initialized=0;  | ||
| + | |||
| + | 	   static int local_static=9;  | ||
| + | |||
| + | 	   local_static++;  | ||
| + | |||
| + | 	   printf("local varaible is %d\n", local);  | ||
	}  | 	}  | ||
| + |         </pre>  | ||
| − | In the above example, executable code of the program is stored in flash and const variable global_var=20 and local_constant=100 are stored in flash.  | + | In the above example, executable code of the program is stored in flash and const variable global_var=20    | 
| + | and local_constant=100 are stored in flash.  | ||
| − | Data segment:Initialized variables are stored in this section.  | + | '''Data segment:'''  | 
| + | |||
| + | Initialized variables are stored in this section.  | ||
Initially values are stored in flash and copied into RAM during execution  | Initially values are stored in flash and copied into RAM during execution  | ||
through copy down process.  | through copy down process.  | ||
| − | Eg: uint8_t j=10; initialized variable  | + | Eg: uint8_t j=10; --------initialized variable  | 
In the code given above int data_variable = 500,static int static_var=10,local = 3, and local_static=9 are stored in data segment of flash.  | In the code given above int data_variable = 500,static int static_var=10,local = 3, and local_static=9 are stored in data segment of flash.  | ||
| − | .bss segment:Uninitialized variables are stored in bss section.  | + | '''.bss segment:'''  | 
| + | |||
| + | Uninitialized variables are stored in bss section.  | ||
In embedded software, the bss segment is mapped into memory that is initialized to zero by the C run-time system before main() is entered.  | In embedded software, the bss segment is mapped into memory that is initialized to zero by the C run-time system before main() is entered.  | ||
| + | |||
(wiki :https://en.wikipedia.org/wiki/.bss#BSS_in_C)  | (wiki :https://en.wikipedia.org/wiki/.bss#BSS_in_C)  | ||
	Eg:int newvar;  | 	Eg:int newvar;  | ||
| − | In the above code   | + | In the above code un-initialized(after initializing it to zero) and another_initialized are stored in bss section ;  | 
| − | 	Simple example:  | + | 	'''Simple example:'''  | 
| + |         <pre>  | ||
	#include <stdio.h>  | 	#include <stdio.h>  | ||
| + | |||
	int main(void)  | 	int main(void)  | ||
	{  | 	{  | ||
		return 0;  | 		return 0;  | ||
	}  | 	}  | ||
| − | + |        </pre>  | |
| + | |||
| + | Memory occupied by the above program:  | ||
| + |         <pre>  | ||
	$ gcc -o just_main just_main.c  | 	$ gcc -o just_main just_main.c  | ||
| + | |||
	$ size just_main.exe  | 	$ size just_main.exe  | ||
| + | |||
	text    data     bss     dec     hex filename  | 	text    data     bss     dec     hex filename  | ||
	3153    1976     448    5577    15c9 just_main.exe  | 	3153    1976     448    5577    15c9 just_main.exe  | ||
| − | + | ||
| + |         </pre>  | ||
| + | |||
| + | After modifying the above program with one global variable:  | ||
| + |         <pre>  | ||
| + | |||
	#include <stdio.h>  | 	#include <stdio.h>  | ||
| + | |||
	int global_var;  | 	int global_var;  | ||
| + | |||
	int main(void)  | 	int main(void)  | ||
	{  | 	{  | ||
| Line 69: | Line 109: | ||
	}  | 	}  | ||
| + |         </pre>  | ||
| + | |||
| + | Memory map of the above program looks like:  | ||
| + | |||
| + |         <pre>  | ||
| + | |||
	$ gcc -o just_main just_main.c  | 	$ gcc -o just_main just_main.c  | ||
| + | |||
	$ size just_main.exe  | 	$ size just_main.exe  | ||
| + | |||
	text    data     bss     dec     hex filename  | 	text    data     bss     dec     hex filename  | ||
	3153    1976     464    5593    15d9 just_main.exe  | 	3153    1976     464    5593    15d9 just_main.exe  | ||
| − | + | ||
| + |         </pre>  | ||
| + | |||
| + | bss section is increased by 2 bytes;  | ||
| − | + | Modify the above program with global variable initialized to zero;  | |
| + |         <pre>  | ||
| + | |||
	#include <stdio.h>  | 	#include <stdio.h>  | ||
| + | |||
	int global_var=0;  | 	int global_var=0;  | ||
| + | |||
	int main(void)  | 	int main(void)  | ||
	{  | 	{  | ||
| Line 84: | Line 139: | ||
	}  | 	}  | ||
| + |         </pre>  | ||
| + | |||
| + | Data and bss segments occupy the RAM area as below.  | ||
| + | |||
| + |         <pre>  | ||
| + | |||
	$ gcc -o just_main just_main.c  | 	$ gcc -o just_main just_main.c  | ||
| + | |||
	$ size just_main.exe  | 	$ size just_main.exe  | ||
| + | |||
	text    data     bss     dec     hex filename  | 	text    data     bss     dec     hex filename  | ||
	3153    1976     480    5609    15e9 just_main.exe  | 	3153    1976     480    5609    15e9 just_main.exe  | ||
| − | + | ||
| + |         </pre>  | ||
| + | |||
| + | bss segment is increased by 32 bits(4 bytes)  | ||
| − | + | Modify the above program with global variable initialized to non-zero;  | |
| + |         <pre>  | ||
| + | |||
	#include <stdio.h>  | 	#include <stdio.h>  | ||
| + | |||
	int global_var=8;  | 	int global_var=8;  | ||
| + | |||
	int main(void)  | 	int main(void)  | ||
	{  | 	{  | ||
		return 0;  | 		return 0;  | ||
	}  | 	}  | ||
| + | |||
| + |         </pre>  | ||
| + | |||
| + | RAM memory is occupied by bss and data as below  | ||
| + | |||
| + |         <pre>  | ||
| + | |||
	$ gcc -o just_main just_main  | 	$ gcc -o just_main just_main  | ||
| + | |||
	$ size just_main.exe  | 	$ size just_main.exe  | ||
| + | |||
	text    data     bss     dec     hex filename  | 	text    data     bss     dec     hex filename  | ||
	3153    1976     448    5577    15c9 just_main.exe  | 	3153    1976     448    5577    15c9 just_main.exe  | ||
| − | + | ||
| + |         </pre>  | ||
| + | |||
| + | bss section is not modified.  | ||
| Line 112: | Line 194: | ||
What is the amount of RAM being used?  | What is the amount of RAM being used?  | ||
Memory occupied by bss and data segments added together.  | Memory occupied by bss and data segments added together.  | ||
| − | |||
| − | |||
Revision as of 09:24, 17 December 2016
BSS and Data Segments
What are text,.bss and data sections in an Embedded C program?
Where do your program recide?Obviously memory,but how is it organized?
In embedded systems ,which are RAM-memory constrained ,memory map is divided into segments called text,data and bss.
Text segment:
Contains code and constants of the program.Text section is allocated on flash.
Eg1: a.out :executable code
Eg2:const uint8_t i=8 :contant
Eg3:
	#include <stdio.h>
	const int global_var=20;
	int data_variable = 500;
	static int static_var=10;
	void foo()
        {
	   const int local_constant=100;	
           int local;
	 
           local = 3;
           int un-initialized;
	   int another-initialized=0;
	   static int local_static=9;
	   local_static++;
	   printf("local varaible is %d\n", local);
	}
        
In the above example, executable code of the program is stored in flash and const variable global_var=20 and local_constant=100 are stored in flash.
Data segment:
Initialized variables are stored in this section. Initially values are stored in flash and copied into RAM during execution through copy down process.
Eg: uint8_t j=10; --------initialized variable
In the code given above int data_variable = 500,static int static_var=10,local = 3, and local_static=9 are stored in data segment of flash.
.bss segment:
Uninitialized variables are stored in bss section.
In embedded software, the bss segment is mapped into memory that is initialized to zero by the C run-time system before main() is entered.
(wiki :https://en.wikipedia.org/wiki/.bss#BSS_in_C)
Eg:int newvar;
In the above code un-initialized(after initializing it to zero) and another_initialized are stored in bss section ;
Simple example:
	#include <stdio.h>
	int main(void)
	{
		return 0;
	}
       
Memory occupied by the above program:
	$ gcc -o just_main just_main.c
	$ size just_main.exe
	text    data     bss     dec     hex filename
	3153    1976     448    5577    15c9 just_main.exe
        
        
After modifying the above program with one global variable:
	#include <stdio.h>
	int global_var;
	int main(void)
	{
		return 0;
	}
	
        
Memory map of the above program looks like:
	$ gcc -o just_main just_main.c
	$ size just_main.exe
	text    data     bss     dec     hex filename
	3153    1976     464    5593    15d9 just_main.exe
        
        
bss section is increased by 2 bytes;
Modify the above program with global variable initialized to zero;
	#include <stdio.h>
	int global_var=0;
	int main(void)
	{
		return 0;
	}
	
        
Data and bss segments occupy the RAM area as below.
	$ gcc -o just_main just_main.c
	$ size just_main.exe
	text    data     bss     dec     hex filename
	3153    1976     480    5609    15e9 just_main.exe
        
bss segment is increased by 32 bits(4 bytes)
Modify the above program with global variable initialized to non-zero;
	#include <stdio.h>
	int global_var=8;
	int main(void)
	{
		return 0;
	}
         
        
RAM memory is occupied by bss and data as below
        
	$ gcc -o just_main just_main
	$ size just_main.exe
	text    data     bss     dec     hex filename
	3153    1976     448    5577    15c9 just_main.exe
        
bss section is not modified.
	
FAQ:
What is the size of bss segment?
bss does not take any space in the object file and stores the count of variables that can be given initial values.Hence occupy 4 or 8 bytes
depending on the implementation.
What is the amount of RAM being used? Memory occupied by bss and data segments added together.