Relationships in code for functions, variables, constants/BSS segments, data segments, text segments/SCT files,. map files [instance Analysis arm Code (MDK)]

Source: Internet
Author: User

Function code://DEMO.C

#include <stdio.h>#include<stdlib.h>intGlobal1 =0, Global2 =0, Global3 =0;voidfunctionvoid){        intLocal4 =0, LOCAL5 =0, local6 =0; Static intSTATIC4 =0, static5 =0, static6 =0; int*P2 = (int*) malloc (sizeof(int)); printf ("child function local variable: \ n"); printf ("local4:%p \ n", &Local4); printf ("LOCAL5:%p \ n", &LOCAL5); printf ("local6:%p \ n", &local6); printf ("child function pointer variable: \ n"); printf ("P2:%p \ n", p2); printf ("global variable: \ n"); printf ("global1:%p \ n", &global1); printf ("global2:%p \ n", &global2); printf ("global3:%p \ n", &global3); printf ("child function static variable: \ n"); printf ("static4:%p \ n", &static4); printf ("static5:%p \ n", &static5); printf ("static6:%p \ n", &static6); printf ("child function Address: \ n"); printf ("function:%p \ n", function);}intDemo_main (intargcChar**argv) {        intLocal1 =0, Local2 =0, Local3 =0; Static intStatic1 =0, Static2 =0, static3 =0; int*P1 = (int*) malloc (sizeof(int)); Const intConst1 =0; Char*char_p ="Char"; printf ("main function local variable: \ n"); printf ("local1:%p \ n", &local1); printf ("local2:%p \ n", &Local2); printf ("local3:%p \ n", &local3); printf ("const1:%p \ n", &Const1); printf ("main function pointer variable: \ n"); printf ("p1:%p \ n", p1); printf ("global variable: \ n"); printf ("global1:%p \ n", &global1); printf ("global2:%p \ n", &global2); printf ("global3:%p \ n", &global3); printf ("main function static variable: \ n"); printf ("static1:%p \ n", &Static1); printf ("static2:%p \ n", &static2); printf ("static3:%p \ n", &static3); printf ("string constants: \ n"); printf ("char_p:%p \ n", char_p); printf ("main function Address: \ n"); printf ("main:%p \ n", Demo_main); printf ("= = = = = = = = = = = = = = = = \ n");        function (); return 0;}

function printing, combined with SCT file, INIT.S analysis:

;; Init.s; The location of Stacksuserstack EQU (_stack_baseaddress-0x3800)    ;0x33ff4800~ 0x33ff5800svcstack EQU (_stack_baseaddress-0x2800)    ;0x33ff5800~undefstack EQU (_stack_baseaddress-0x2400)    ;0x33ff5c00~abortstack EQU (_stack_baseaddress-0x2000)    ;0x33ff6000~irqstack EQU (_stack_baseaddress-0x1000)    ;0x33ff7000~fiqstack EQU (_stack_baseaddress-0x0)    ;0x33ff8000~

Main function Local Variables://at runtime, the map file is not in the Userstack stack.local1:33ff56e4 local2:33ff56e0 local3:33ff56dc const1:33ff56d8 main function malloc pointer variable://runtime, in the heap, there is no map file insideP1:37000020//_init_alloc (0x37000000,0x38000000-8); Global variables://data demo.o (. data) [RW]global1:322000a8 global2:322000ac global3:322000b0 main function static variable://data demo.o (. data) [RW]static1:322000c0 static2:322000c4 Static3:322000c8 string constants://Code demo.o (. Text) [RO]char_p:32013b28 main function Address://ARM Code demo.o (. Text) [RO]demo_main:32013a10= = = = = = = = = = = = = = =Sub-function local variables://at runtime, the map file does not containlocal4:33ff56c8 local5:33ff56c4 local6:33ff56c0 child function pointer variable://at runtime, the map file does not containP2:37000030Global Variables://data demo.o (. data) [RW]global1:322000a8 global2:322000ac global3:322000b0 child function static variable://data demo.o (. data) [RW]static4:322000b4 static5:322000b8 STATIC6:322000BC child function Address://ARM Code demo.o (. Text) [RO]function:3201381c

SCT, Map file analysis:

; *************************************************************; Scatter-loading Description File generated by uvision * * *; *************************************************************lr_rom10x32000000 0x00200000{; Load region size_region er_rom10x32000000 0x00200000{; Load address =Execution Address*.O (RESET,+First  )* (inroot$ $Sections). Any (    + RO) } ;ConstData[ro] and code; Symbol Name Ov Type Size Object (section); RESET Section532INIT.O (RESET); Isrirq ARM Code0INIT.O (RESET);StaticU16 readstatus ARM Code nand.o (. text);Static voidWriteRawRCBySPI1 ARM Code nand.o (. text);Static ConstU8 exchang_right Data des.o (. constdata); __entry ARM Code0INIT.O (RESET);voidDelayms ARM Code +common.o (. Text) rw_ram10x32200000 0x04000000{  ; RW data0x3220002c . Any (+RW + ZI)//variable that can be changed } ; RW Data+BSS;Static Chariccardsnrfound[7] Data execmd.o (. data);StaticU8 s_buf[ -][2048] Data nfblack.o (. BSS);intgrdebugcom Data common.o (. data); U8 downloadbuf[192*2048] Data fireupdate.o (. BSS);structusb_device_descriptor Descdev Data usbsetup.o (. BSS) rw_iram10x40000000 0x00001000  {   . Any (+RW +ZI)}}

BSS segment, code snippet and data segment differences [1]:
 BSS and data: The global uninitialized variable exists in the. BSS segment, which is represented as a placeholder; the global initialized variable is stored in the. Data segment;. BSS is not consuming. exe files    Space, whose contents are initialized by the operating system (0), while. Data needs to be occupied, and its contents are initialized by the program. If you define a global variable like this:  int  g_bval[9  ] ;    It is in the. BSS segment, where the placeholder space is occupied. If you define a global variable like this:  int  g_aval[9 ] ={< Span style= "color: #800080;" >1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9     }; It is in the. Data segment, and the program takes up space of the entire size of the array.    BSS and data Links: both in the RW area; The BSS segment takes up the same amount of space and data after it is run as a process. 
Code Snippets: Code segment/text segment usually refers to a piece of memory area that is used to store program execution code. The size of this area is determined before the program runs, and the memory area is usually read-only (some schemas also allow code snippets to be writable, which allows the program to be modified). in a code snippet, it is also possible to include some read-only constant variables, such as String constants. The code snippet is the data that holds the program code, and if several processes in the machine run the same program, they can use the same code snippet. 

Reference:

1. BSS segment, code segment and data segment difference

http://blog.163.com/starjj_embeded/blog/static/2045000512012213113440344/

Relationships in code for functions, variables, constants/BSS segments, data segments, text segments/SCT files,. map files [instance Analysis arm Code (MDK)]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.