Using disassembly in the ARM platform to analyze C memory Distribution: arm: Use the arm-linux-objdump command to disassemble the Elf file after the compilation is completed. Then redirect to the Tmp.s file. The first step is the following C file. Vim tmp.c#include<stdio.h> #define VAR 0xFFint a = 0; static int b = 0;int c = 10;static int d = 20; const int finalone = 10;const int final;int main (void) {char *hell = "hhhhhhh"; const int finaltwo =; static int f = 0;st atic int k = 10;int AA; int bb=10;printf ("VAR =%d\n, Finalone =%d, Finaltwo =%d", var,finalone,finaltwo);} The second step: write the makefile file as follows makefile file as follows: Vim makefilecc=arm-linux-gcccflags + =-march=armv7-a Step three: Compile and build the executable file. Then use the make command to compile to the file. Make TMP generates the TMP elf format file. Fourth step: The following file Tmp.s files are obtained by using arm-linux-objdump-d tmp > tmp.s//.: File format Elf32-littlearm below is Pick out the relevant inside as follows://The following is the corresponding. Data segment related initialized variables.//variable c,d,k are accessed again within the region. The following are the conclusions: the variables that need to meet the requirements are placed in the. Data segment, which is the initialization data segment.//All Global | | static keyword decoration) && uninitialized variable sassembly of section. data:00011020 <__data_start>:11020:00000000andeqr0, R0, r000011024 <__dso_handle>:11024:00000000andeqr0, R0, r000011028 <c>:11028:0000000aandeqr0, r0, SL 0001102c <d>:1102c:00000014andeqr0, R0, R4, LSL r000011030 <k.1728>:11030:0000000aandeqr0, R0, sl// The following is the corresponding. BSS segment. The variable a,b,f is stored in this area.//The zone stores variables that are not initialized or initialized to 0. These variables should be met as follows, and the condition will be placed in the given zone: (Global | Modified by the STATIC keyword) && (for initialization | | Variable initialized to 0) disassembly of section. bss:00011034 <completed.5796>:11034:00000000andeqr0, R0, r000011038 <a> : 11038:00000000andeqr0, R0, r00001103c <b>:1103c:00000000andeqr0, R0, r000011040 <f.1727> : 11040:00000000andeqr0, R0, r000011044 <final>:11044:00000000andeqr0, R0, r0//This area holds some string constants. "Hhhhhhh" in the program above C The corresponding 686868.....//also has global initialization constants that use the const modifier. The const int Finalone variable as above. It only corresponds to the 00000000a.sassembly of section of 848c. rodata:0 0008488 <_io_stdin_used>:8488:00020001andeqr0, R2, r10000848c <finalone>:848c:0000000aandeqr0, R0, sl8490:68686868stmdavsr8!, {R3, R5, R6, FP, SP, lr}^8494:68686868stmdavsr8!, {R3, R5, R6, FP, SP, lr}^8498:00000068andeqr0 , R0, R8, RRX849C:20524156SUBSCSR4, R2, R6, ASR R184A0:6425203DSTRTVSR2, [R5], #-61; 0x3d84a4:66202c0asTRTVSR2, [R0],-SL, LSL #2484a8:6c616e69stclvs14, CR6, [R1], #-420; 0XFFFFFE5C84AC:20656E6FRSBCSR6, R5, PC, Ror #2884b0:6425203DSTRTVSR2, [R5], #-61; 0x3d84b4:6966202cstmdbvsr6!, {R2, R3, R5, SP}^84B8:746C616ESTRBTVCR6, [IP], #-366; 0X16E84BC:3D206F77STCCC15, CR6, [R0, #-476]!; 0XFFFFFE2484C0:2C642520CFSTR64CSMVDX2, [R4], #-128; 0XFFFFFF8084C4:203D2068EORSCSR2, SP, R8, RRX84C8:00732520RSBSEQR2, R3, R0, LSR #10}//also use # Define declares a macro. Where is it stored? We can take a look. The assembly in main is as follows://First step to find out. The local variable declared in Main. Char *hell = "hhhhhhh"//This is the declaration of the Hell variable, 83c0: E3083490MOVWR3, #33936; 0x8490const int finaltwo = 50; 83CC:E3A03032MOVR3, #50; 0x32//It will be saved in the stack. static int f = 0;static int k = 10;int AA; The AA variable is optimized by default and does not exist. Because it is not being used, nor is it using the volatile keyword modifier,//compiling the default optimization level under the current arm platform is O2, then it will be assembled in the middle step. int bb=10; 83D4:E3A0300AMOVR3, #10 This is bb=10//. This assembly code also contains a #255, that is, we use the # define VAR 255 constant,//It is an immediate number. Indicates that it occupies only the. Text text segment, which is what we often call code snippets .//The following is a detailed explanation of the paragraph: description of the const, and the difference between the # define constants. 000083B4 <MAIN>:83B4:E92D4800PUSH{FP, Lr}83b8: E28DB004ADDFP, SP, #483bc: e24dd018subsp, SP, #2483c0: E3083490MOVWR3, #33936; 0X849083C4:E3403000MOVTR3, #083c8: E50B3008STRR3, [FP, #-8]83cc:e3a03032movr3, #50; 0X3283D0:E50B300CSTRR3, [FP, #-12]83d4:e3a0300amovr3, #1083d8: E50B3010STRR3, [FP, #-16]83dc:e308349cmovwr3, #33948; 0X849C83E0:E3403000MOVTR3, #083e4: E308248CMOVWR2, #33932; 0X848C83E8:E3402000MOVTR2, #083ec: E5922000LDRR2, [R2]83F0:E51B1008LDRR1, [FP, #-8]83f4:e58d1000strr1, [SP]83F8: E1a00003movr0, R383FC:E3A010FFMOVR1, #255; 0XFF8400:E51B300CLDRR3, [FP, #-12]8404:ebffffbcbl82fc <_init+0x44>8408:e1a00003movr0, r3840c:e24bd004subsp, FP, #48410: e8bd8800pop{fp, pc}//resolves the differences between define and Const. const-defined read-only variable from the assembly point of view just give the corresponding memory address instead of the immediate number as given by define. The const-defined read-only variable has only one copy during the program's run (because it is a global read-only variable in the static area) and the macro variable defined by define has several copies in memory define the macro is replaced during the precompilation phase and the const-modified read-only variable is determined at compile time Define macros do not have a type and a const-modified read-only variable has a specific type.
Using assembler to analyze the memory distribution of C code