Linux??????????????
???????????? ???????[ 2014/12/16 14:47:17 ] ????????Linux ??????
???????п????????????????? .txt ?ε???? 0 ????????е???7????????????? a ???????? %eax ?У?????? a ?????????? %eax ?е??????? g_share ??????????? g_share ???????0(%rip). ?????????????????????????????? g_share ??????????????????????????????????????????????????棬???????????????????????????????????????????????????????????????Щ??????????????????????????????????????????б????????????Щ???????????λ?????????????Щ???????“???λ??”(relocatioin table)??
????-bash-3.00$ objdump -r a.o
????a.o: file format elf64-x86-64
????RELOCATION RECORDS FOR [.text]:
????OFFSET TYPE VALUE
????000000000000000c R_X86_64_PC32 g_share+0xfffffffffffffffc
???????????У???????????????????????У?????? g_share ??????????????? offset ??????????????λ???????е?????TYPE ?????????????????? cpu ???????????????????????????????????????????? type ??????????????? value ??????? type ???????÷????????
?????????????????????????????????????????????????????????ε?????????????????????Щ??????λ?????????????? ????????????????????????У??? g_share(0x40496??)?? g_func(0x4047a??)????????????????????????
-bash-3.00$ gcc -o am a.o main.o
-bash-3.00$ objdump -S am
// skip some of the ouput
extern int g_func(int a);
int main()
{
400468: 55 push %rbp
400469: 48 89 e5 mov %rsp??%rbp
40046c: 48 83 ec 10 sub $0x10??%rsp
int a = 42;
400470: c7 45 fc 2a 00 00 00 movl $0x2a??0xfffffffffffffffc(%rbp)
a = g_func(a);
400477: 8b 7d fc mov 0xfffffffffffffffc(%rbp)??%edi
40047a: e8 0d 00 00 00 callq 40048c <g_func>
40047f: 89 45 fc mov %eax??0xfffffffffffffffc(%rbp)
return 0;
400482: b8 00 00 00 00 mov $0x0??%eax
}
400487: c9 leaveq
400488: c3 retq
400489: 90 nop
40048a: 90 nop
40048b: 90 nop
000000000040048c <g_func>:
int g_share = 1;
int g_func(int a)
{
40048c: 55 push %rbp
40048d: 48 89 e5 mov %rsp??%rbp
400490: 89 7d fc mov %edi??0xfffffffffffffffc(%rbp)
g_share += a;
400493: 8b 45 fc mov 0xfffffffffffffffc(%rbp)??%eax
400496: 01 05 dc 03 10 00 add %eax??1049564(%rip) # 500878 <g_share>
return a * 2;
40049c: 8b 45 fc mov 0xfffffffffffffffc(%rbp)??%eax
40049f: 01 c0 add %eax??%eax
}
4004a1: c9 leaveq
4004a2: c3 retq
// skip some of the ouput
????????????λ??????????巽?????浽???????????£??????????
????????????????λ
???????????????????????????????????λ???????????????????????????????????????Щ???????λ????????????????????????Linux ?? ELF ??????????????????????????λ????????????????????????????????????????λ???????????????????????λ????μ?????????λ????????????????????????????????????????С?
int g_share = 1;
int g_func(int a)
{
g_share += a;
return a * 2;
}
int g_func2()
{
int a = 2;
int b = g_func(3);
return a + b;
}
// compile on 32bit linux OS
-bash-3.00$ gcc -c a.c main.c
-bash-3.00$ gcc -shared -o liba.so a.o
-bash-3.00$ gcc -o am main.o -L. -la
-bash-3.00$ objdump -S liba.so
// skip some of the output
000004f4 <g_func>:
int g_share = 1;
int g_func(int a)
{
4f4: 55 push %ebp
4f5: 89 e5 mov %esp??%ebp
g_share += a;
4f7: 8b 45 08 mov 0x8(%ebp)??%eax
4fa: 01 05 00 00 00 00 add %eax??0x0
return a * 2;
500: 8b 45 08 mov 0x8(%ebp)??%eax
503: d1 e0 shl %eax
}
505: c9 leave
506: c3 ret
00000507 <g_func2>:
int g_func2()
{
507: 55 push %ebp
508: 89 e5 mov %esp??%ebp
50a: 83 ec 08 sub $0x8??%esp
int a = 2;
50d: c7 45 fc 02 00 00 00 movl $0x2??0xfffffffc(%ebp)
int b = g_func(3);
514: 6a 03 push $0x3
516: e8 fc ff ff ff call 517 <g_func2+0x10>
51b: 83 c4 04 add $0x4??%esp
51e: 89 45 f8 mov %eax??0xfffffff8(%ebp)
return a + b;
521: 8b 45 f8 mov 0xfffffff8(%ebp)??%eax
524: 03 45 fc add 0xfffffffc(%ebp)??%eax
}
527: c9 leave
// skip some of the output
??????????е?? 4fa ?? 516 ????????????????? g_share ?? g_func ???????????????????????????????????Щ?????????????????????????????????λ??????????????????????????????????????λ???????????????????м???????????????
??????????????????????????ν??м???????????λ???????????????????????????е???????????????????????????????λ??????????????????????????????????????????????????????????????????????????????????????????÷??????????????????????????????????????????ж?÷???????y??????λ?????÷??????????????????????????????????
????ELF ??????????е???????ò???????ν??"????”(lazy binding)?? ??????е???????????ε??÷????????????????????????????????????????????????????????????????????????Щ??????????滻?????????????
??????????????????????????λ????????????????????????????????????????????????????????????????????????????????????????????????λ?????????????????????????????????????????????????????????????? copy ??????????????λ??????????????????в????????????????????????????????????
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11