ELF文件之九——使用链接脚本-2个函数-data-bss-temp-call-debug信息-struct

main.c

int enable;
int test = 1;

struct aaa
{
    int membera;
    char memberb;
}s_aaa;

int main()
{
    int temp;
    
    add();
    del();
    
    s_aaa.membera = 1;
    s_aaa.memberb = 3;
    
    return 0;
}

int add()
{
    return 0;
}

int del()
{
    return 0;
}
View Code

nm

00000000 a *ABS*
70000000 T main
70000048 T add
7000005c T del
70000070 D test
70000074 B enable
70000078 B s_aaa

asm

main.elf:     file format elf32-sparc

Disassembly of section .text:

70000000 <main>:
70000000:    9d e3 bf 90     save  %sp, -112, %sp
70000004:    40 00 00 11     call  70000048 <add>
70000008:    01 00 00 00     nop 
7000000c:    40 00 00 14     call  7000005c <del>
70000010:    01 00 00 00     nop 
70000014:    11 1c 00 00     sethi  %hi(0x70000000), %o0
70000018:    92 12 20 78     or  %o0, 0x78, %o1    ! 70000078 <s_aaa>
7000001c:    90 10 20 01     mov  1, %o0
70000020:    d0 22 40 00     st  %o0, [ %o1 ]
70000024:    11 1c 00 00     sethi  %hi(0x70000000), %o0
70000028:    92 12 20 78     or  %o0, 0x78, %o1    ! 70000078 <s_aaa>
7000002c:    90 10 20 03     mov  3, %o0
70000030:    d0 2a 60 04     stb  %o0, [ %o1 + 4 ]
70000034:    90 10 20 00     clr  %o0
70000038:    b0 10 00 08     mov  %o0, %i0
7000003c:    01 00 00 00     nop 
70000040:    81 c7 e0 08     ret 
70000044:    81 e8 00 00     restore 

70000048 <add>:
70000048:    9d e3 bf 98     save  %sp, -104, %sp
7000004c:    b0 10 20 00     clr  %i0
70000050:    01 00 00 00     nop 
70000054:    81 c7 e0 08     ret 
70000058:    81 e8 00 00     restore 

7000005c <del>:
7000005c:    9d e3 bf 98     save  %sp, -104, %sp
70000060:    b0 10 20 00     clr  %i0
70000064:    01 00 00 00     nop 
70000068:    81 c7 e0 08     ret 
7000006c:    81 e8 00 00     restore 
Disassembly of section .data:

70000070 <test>:
70000070:    00 00 00 01     unimp  0x1
View Code

s_aaa结构体地址在70000078,sethi和or两步操作将该地址赋值给o1

70000014:    11 1c 00 00     sethi  %hi(0x70000000), %o0
70000018:    92 12 20 78     or  %o0, 0x78, %o1    ! 70000078 <s_aaa>

然后,将o0赋值为1,将o0赋值到第一个结构体成员的内存中

7000001c:    90 10 20 01     mov  1, %o0
70000020:    d0 22 40 00     st  %o0, [ %o1 ]

结构体第二个成员赋值与第一个类似,最后会有4字节偏移,偏移到第二个成员,同时是stb,即存储字节;而第一个是st,存储字,32bit。

70000024:    11 1c 00 00     sethi  %hi(0x70000000), %o0
70000028:    92 12 20 78     or  %o0, 0x78, %o1    ! 70000078 <s_aaa>
7000002c:    90 10 20 03     mov  3, %o0
70000030:    d0 2a 60 04     stb  %o0, [ %o1 + 4 ]

从上面可以看出,高级语言C语言的结构体,int,char等类型在汇编上均没有体现,最后都是体现为ld,st这些赋值上,结构体的成员差别是通过结构体内存起始+内存偏移实现的。

原文地址:https://www.cnblogs.com/yanhc/p/12312204.html