ãã®ãããã¯ã«é¢ããæ å ±ã¯ããããã¯ãŒã¯ããã®ä»ã®ã©ãã«ããããŸãããå©çšå¯èœãªãªãœãŒã¹ã®äžã§æãéèŠãªã®ã¯å ¬åŒã®x64 ABIã§ããããããããŠã³ããŒãã§ããŸãïŒä»¥äžããABIããšåŒã³ãŸãïŒãæ å ±ã®äžéšã¯ãäžã§èŠã€ããããšãã§ããŸã
man
-pagesgcc
... ãã®èšäºã®ç®çã¯ããããã¯ã«é¢ããã¢ã¯ã»ã·ãã«ãªæšå¥šäºé
ãæäŸããé¢é£ããåé¡ã«ã€ããŠè°è«ããäœæ¥ã§äœ¿çšãããé©åãªã³ãŒããéããŠããã€ãã®æŠå¿µãå®èšŒããããšã§ãã
éèŠãªæ³šæïŒãã®èšäºã¯åå¿è åãã®ãã¥ãŒããªã¢ã«ã§ã¯ãããŸãããç¥ãåã«ãCãšã¢ã»ã³ãã©ã®åŒ·åãªã³ãã³ããããã³x64ã¢ãŒããã¯ãã£ã®åºæ¬çãªç¥èãæã£ãŠããããšããå§ãããŸãã
é¢é£ãããã¯ã«é¢ãã以åã®æçš¿ãåç §ããŠãã ããïŒx86_x64ãã¡ã¢ãªãã¢ãã¬ã¹æå®ããæ¹æ³
ã³ãŒãã¢ãã«ãããæ°ãèµ·ããããéšå
x64ã¢ãŒããã¯ãã£ã§ã¯ãã³ãŒããšããŒã¿ã®äž¡æ¹ããã³ãã³ãçžå¯ŸïŒãŸãã¯x64å°éçšèªã䜿çšããŠRIPçžå¯ŸïŒã¢ãã¬ãã·ã³ã°ã¢ãã«ãéããŠåç §ãããŸãããããã®ã³ãã³ãã§ã¯ãRIPããã®ã·ããã¯32ãããã«å¶éãããŠããŸãããã¡ã¢ãªãŸãã¯ããŒã¿ã®äžéšãã¢ãã¬ã¹æå®ããããšãããšãã«ãããšãã°2ã®ã¬ãã€ããè¶ ããããã°ã©ã ã䜿çšããŠããå Žåãªã©ãããŒã ãåã«32ãããã·ãããè¡ããªãå ŽåããããŸãã
ãã®åé¡ã解決ãã1ã€ã®æ¹æ³ã¯ãRIPçžå¯Ÿã¢ãã¬ãã·ã³ã°ã¢ãŒããå®å šã«ç Žæ£ããŠããã¹ãŠã®ããŒã¿ããã³ã³ãŒãåç §ã«å¯ŸããŠå®å šãª64ãããã·ãããåªå ããããšã§ãããã ãããã®ã¹ãããã¯éåžžã«è²»çšãããããŸããéåžžã«å€§ããªããã°ã©ã ãšã©ã€ãã©ãªã®ïŒããªããŸããªïŒã±ãŒã¹ãã«ããŒããã«ã¯ãã³ãŒãå šäœã§æãåçŽãªæäœã§ãããéåžžããå€ãã®ã³ãã³ããå¿ èŠã«ãªããŸãã
ãããã£ãŠãã³ãŒãã¢ãã«ã¯ãã¬ãŒããªãã«ãªããŸãã[1]ã³ãŒãââã¢ãã«ã¯ãããã°ã©ããŒãšã³ã³ãã€ã©ãŒã®éã®æ£åŒãªåæã§ãããããã°ã©ããŒã¯ãçŸåšã³ã³ãã€ã«ãããŠãããªããžã§ã¯ãã¢ãžã¥ãŒã«ã該åœããäºå®ã®ããã°ã©ã ïŒè€æ°ã®å ŽåãããïŒã®ãµã€ãºã«é¢ããæå³ãæå®ããŸãã[2]ã³ãŒãã¢ãã«ãå¿ èŠãªã®ã¯ãããã°ã©ããã³ã³ãã€ã©ã«ããã®ãªããžã§ã¯ãã¢ãžã¥ãŒã«ã¯å°ããªããã°ã©ã ã«å ¥ãã ããªã®ã§ãé«éãªRIPçžå¯Ÿã¢ãã¬ãã·ã³ã°ã¢ãŒãã䜿çšã§ãããããå¿é ããå¿ èŠããªãããšããããšã§ããäžæ¹ãã³ã³ãã€ã©ãŒã«æ¬¡ã®ããã«äŒããå¯èœæ§ããããŸããããã®ã¢ãžã¥ãŒã«ã倧ããªããã°ã©ã ã«ãªã³ã¯ããã®ã§ãå®å šãª64ãããã·ããã§ãã£ãããšå®å šãªçµ¶å¯Ÿã¢ãã¬ãã·ã³ã°ã¢ãŒãã䜿çšããŠãã ãããã
ãã®èšäºãäŒããããš
äžèšã®2ã€ã®ã·ããªãªãå°ããªã³ãŒãã¢ãã«ãšå€§ããªã³ãŒãã¢ãã«ã«ã€ããŠèª¬æããŸããæåã®ã¢ãã«ã¯ããªããžã§ã¯ããŠãããå ã®ãã¹ãŠã®ã³ãŒããšããŒã¿åç §ã«32ãããã®çžå¯Ÿãªãã»ããã§ååã§ããããšãã³ã³ãã€ã©ãŒã«äŒããŸãã2çªç®ã¯ãã³ã³ãã€ã©ãŒã絶察64ãããã»ã¢ãã¬ãã·ã³ã°ã»ã¢ãŒãã䜿çšããããšãèŠæ±ããŠããŸããããã«ãäžéããŒãžã§ã³ãããããããã«ã³ãŒãã¢ãã«ããããŸãã
ãããã®åã³ãŒãã¢ãã«ã¯ãç¬ç«ããPICãšéPICã®ããªãšãŒã·ã§ã³ã§æ瀺ããã6ã€ããããã«ã€ããŠèª¬æããŸãã
å ã®Cã®äŸ
ãã®èšäºã§èª¬æããæŠå¿µã瀺ãããã«ã以äžã®Cããã°ã©ã ã䜿çšããŠãããŸããŸãªã³ãŒãã¢ãã«ã§ã³ã³ãã€ã«ããŸããã芧ã®ãšããããã®é¢æ°
main
ã¯4ã€ã®ç°ãªãã°ããŒãã«é
åãš1ã€ã®ã°ããŒãã«é¢æ°ã«ã¢ã¯ã»ã¹ããŸããé
åã¯ããµã€ãºãšå¯èŠæ§ã®2ã€ã®ãã©ã¡ãŒã¿ãŒãç°ãªããŸãããµã€ãºã¯å¹³åçãªã³ãŒãã¢ãã«ã説æããããã«éèŠã§ããã倧å°ã®ã¢ãã«ãæ±ãå Žåã«ã¯å¿
èŠãããŸãããå¯èŠæ§ã¯PICã³ãŒãã¢ãã«ã®æäœã«ãšã£ãŠéèŠã§ãããéçïŒãœãŒã¹ãã¡ã€ã«ã§ã®ã¿è¡šç€ºïŒãŸãã¯ã°ããŒãã«ïŒããã°ã©ã ã«ãªã³ã¯ãããŠãããã¹ãŠã®ãªããžã§ã¯ãã«å¯Ÿããå¯èŠæ§ïŒã®ããããã§ãã
int global_arr[100] = {2, 3};
static int static_arr[100] = {9, 7};
int global_arr_big[50000] = {5, 6};
static int static_arr_big[50000] = {10, 20};
int global_func(int param)
{
return param * 10;
}
int main(int argc, const char* argv[])
{
int t = global_func(argc);
t += global_arr[7];
t += static_arr[7];
t += global_arr_big[7];
t += static_arr_big[7];
return t;
}
gcc
ãªãã·ã§ã³å€ãšããŠã³ãŒãã¢ãã«ã䜿çšããŸã-mcmodel
ãããã«ã-fpic
PICã³ã³ãã€ã«ã¯ãã©ã°ã§èšå®ã§ããŸãã
PICã䜿çšãã倧èŠæš¡ãªã³ãŒãã¢ãã«ã«ãããªããžã§ã¯ãã¢ãžã¥ãŒã«ãžã®ã³ã³ãã€ã«ã®äŸïŒ
> gcc -g -O0 -c codemodel1.c -fpic -mcmodel=large -o codemodel1_large_pic.o
å°ããªã³ãŒãã¢ãã«
å°ããªã³ãŒãã¢ãã«ã§ã®man gccããã®åŒçšã®ç¿»èš³ïŒ
-mcmodel = small
å°ããªã¢ãã«ã®ã³ãŒãã®çæïŒããã°ã©ã ãšãã®ã·ã³ãã«ã¯ãã¢ãã¬ã¹ç©ºéã®äžäœ2ã®ã¬ãã€ãã«é 眮ããå¿ èŠããããŸãããã€ã³ã¿ã®ãµã€ãºã¯64ãããã§ããããã°ã©ã ã¯éçã«ãåçã«ãæ§ç¯ã§ããŸããããã¯åºæ¬çãªã³ãŒãã¢ãã«ã§ãã
èšãæãããšãã³ã³ãã€ã©ã¯ãã³ãŒãå ã®ã³ãã³ãããã®32ãããRIPçžå¯Ÿãªãã»ãããä»ããŠã³ãŒããšããŒã¿ã«ã¢ã¯ã»ã¹ã§ãããšå®å šã«æ³å®ã§ããŸããPIC以å€ã®å°ããªã³ãŒãã¢ãã«ã§ã³ã³ãã€ã«ããCããã°ã©ã ã®éã¢ã»ã³ãã«ãããäŸãèŠãŠã¿ãŸãããã
> objdump -dS codemodel1_small.o
[...]
int main(int argc, const char* argv[])
{
15: 55 push %rbp
16: 48 89 e5 mov %rsp,%rbp
19: 48 83 ec 20 sub $0x20,%rsp
1d: 89 7d ec mov %edi,-0x14(%rbp)
20: 48 89 75 e0 mov %rsi,-0x20(%rbp)
int t = global_func(argc);
24: 8b 45 ec mov -0x14(%rbp),%eax
27: 89 c7 mov %eax,%edi
29: b8 00 00 00 00 mov $0x0,%eax
2e: e8 00 00 00 00 callq 33 <main+0x1e>
33: 89 45 fc mov %eax,-0x4(%rbp)
t += global_arr[7];
36: 8b 05 00 00 00 00 mov 0x0(%rip),%eax
3c: 01 45 fc add %eax,-0x4(%rbp)
t += static_arr[7];
3f: 8b 05 00 00 00 00 mov 0x0(%rip),%eax
45: 01 45 fc add %eax,-0x4(%rbp)
t += global_arr_big[7];
48: 8b 05 00 00 00 00 mov 0x0(%rip),%eax
4e: 01 45 fc add %eax,-0x4(%rbp)
t += static_arr_big[7];
51: 8b 05 00 00 00 00 mov 0x0(%rip),%eax
57: 01 45 fc add %eax,-0x4(%rbp)
return t;
5a: 8b 45 fc mov -0x4(%rbp),%eax
}
5d: c9 leaveq
5e: c3 retq
ã芧ã®ãšããããã¹ãŠã®ã¢ã¬ã€ãžã®ã¢ã¯ã»ã¹ã¯ãRIPçžå¯Ÿã·ããã䜿çšããŠåãæ¹æ³ã§ç·šæãããŠããŸãããã ããã³ãŒãã®ã·ããã¯0ã§ããããã¯ãã³ã³ãã€ã©ãããŒã¿ã»ã°ã¡ã³ãã®é çœ®å Žæãç¥ããªãããããã®ãããªã¢ã¯ã»ã¹ããšã«åé 眮ãäœæãããããã§ãã
> readelf -r codemodel1_small.o
Relocation section '.rela.text' at offset 0x62bd8 contains 5 entries:
Offset Info Type Sym. Value Sym. Name + Addend
00000000002f 001500000002 R_X86_64_PC32 0000000000000000 global_func - 4
000000000038 001100000002 R_X86_64_PC32 0000000000000000 global_arr + 18
000000000041 000300000002 R_X86_64_PC32 0000000000000000 .data + 1b8
00000000004a 001200000002 R_X86_64_PC32 0000000000000340 global_arr_big + 18
000000000053 000300000002 R_X86_64_PC32 0000000000000000 .data + 31098
ãžã®ã¢ã¯ã»ã¹ãå®å šã«ãã³ãŒãããŠã¿ãŸããã
global_arr
ãç§ãã¡ãé¢å¿ã®ããéã¢ã»ã³ãã«ãããã»ã°ã¡ã³ãã¯æ¬¡ã®ãšããã§ãã
t += global_arr[7];
36: 8b 05 00 00 00 00 mov 0x0(%rip),%eax
3c: 01 45 fc add %eax,-0x4(%rbp)
RIPçžå¯Ÿã¢ãã¬ãã·ã³ã°ã¯æ¬¡ã®ã³ãã³ãã«é¢é£ããŠãããããã·ããã¯
mov
0x3sã«å¯Ÿå¿ããããã«ã³ãã³ãã«ãããããå¿
èŠããããŸãã 2çªç®ã®åé
眮ã«é¢å¿ããããŸããããã¯ã¢ãã¬ã¹R_X86_64_PC32
ã®ãªãã©ã³ãmov
ãæã0x38
ã次ã®ããšãæå³ããŸããã·ã³ãã«ã®å€ãååŸããé
ãè¿œå ããŠãåé
眮ã«ãã£ãŠç€ºãããã·ãããå·®ãåŒããŸãããã¹ãŠãæ£ããèšç®ãããšãçµæã次ã®ã³ãã³ããšglobal_arr
ãããã³ã®éã«çžå¯Ÿã·ãããã©ã®ããã«é
眮ããããããããŸã01
ã01
ãé
åã®7çªç®ã®intããæå³ããããïŒx64ã¢ãŒããã¯ãã£ã§ã¯ãããããã®ãµã€ãºint
ã¯4ãã€ãã§ãïŒããã®çžå¯Ÿçãªã·ãããå¿
èŠã§ãããããã£ãŠãRIPçžå¯Ÿã¢ãã¬ãã·ã³ã°ã䜿çšãããšãã³ãã³ãã¯ãæ£ããåç
§ãglobal_arr[7]
ãŸãã
次ã®ç¹ã«ã泚ç®ããŠãã ããã
static_arr
ããã§ã®ã¢ã¯ã»ã¹ã³ãã³ãã¯äŒŒãŠããŸããããã®ãªãã€ã¬ã¯ãã§ã¯å¥ã®ã·ã³ãã«ã䜿çšããŠãããããç¹å®ã®ã·ã³ãã«ã§ã¯ãªãã»ã¯ã·ã§ã³ããã€ã³ãããŠããŸã.data
ãããã¯ãªã³ã«ã®ã¢ã¯ã·ã§ã³ã«ãããã®ã§ãéçé
åãã»ã¯ã·ã§ã³å
ã®æ¢ç¥ã®å Žæã«é
眮ãããããé
åãä»ã®å
±æã©ã€ãã©ãªãšå
±æããããšã¯ã§ããŸããããã®çµæããªã³ã«ã¯ãã®åé
眮ã§ç¶æ³ã解決ããŸããäžæ¹ãglobal_arr
å¥ã®å
±æã©ã€ãã©ãªã§äœ¿çšïŒãŸãã¯äžæžãïŒã§ããããããã§ã«ãã€ãããã¯ããŒããŒã¯ãžã®ãªã³ã¯ãåŠçããå¿
èŠãããglobal_arr
ãŸãã [3]
æåŸã«ããžã®åç §ãèŠãŠã¿ãŸããã
global_func
ïŒ
int t = global_func(argc);
24: 8b 45 ec mov -0x14(%rbp),%eax
27: 89 c7 mov %eax,%edi
29: b8 00 00 00 00 mov $0x0,%eax
2e: e8 00 00 00 00 callq 33 <main+0x1e>
33: 89 45 fc mov %eax,-0x4(%rbp)
ãªãã©ã³ã
callq
ãRIPçžå¯Ÿã§ãããããR_X86_64_PC32
ããã§ã®åé
眮ã¯ãglobal_funcãžã®å®éã®çžå¯Ÿã·ããããªãã©ã³ãã«é
眮ããã®ãšåãããã«æ©èœããŸãã
çµè«ãšããŠãã³ãŒãã¢ãã«ãå°ãããããã³ã³ãã€ã©ã¯å°æ¥ã®ããã°ã©ã ã®ãã¹ãŠã®ããŒã¿ãšã³ãŒãã32ãããã·ãããéããŠå©çšã§ãããšèªèããããããçš®é¡ã®ãªããžã§ã¯ãã«ã¢ã¯ã»ã¹ããããã®ã·ã³ãã«ã§å¹ççãªã³ãŒããäœæããŸãã
倧ããªã³ãŒãã¢ãã«
man
gcc
倧ããªã³ãŒãã¢ãã«
ããã®åŒçšã®ç¿»èš³ïŒ
-mcmodel = large倧
èŠæš¡ã¢ãã«çšã®ã³ãŒãã®çæïŒãã®ã¢ãã«ã¯ãã¢ãã¬ã¹ãšã»ã¯ã·ã§ã³ãµã€ãºã«é¢ããä»®å®ãè¡ããŸããã
main
PIC以å€ã®å€§ããªã¢ãã«ã䜿çšããŠã³ã³ãã€ã«ã
ããéã¢ã»ã³ãã«ãããã³ãŒãã®äŸïŒ
int main(int argc, const char* argv[])
{
15: 55 push %rbp
16: 48 89 e5 mov %rsp,%rbp
19: 48 83 ec 20 sub $0x20,%rsp
1d: 89 7d ec mov %edi,-0x14(%rbp)
20: 48 89 75 e0 mov %rsi,-0x20(%rbp)
int t = global_func(argc);
24: 8b 45 ec mov -0x14(%rbp),%eax
27: 89 c7 mov %eax,%edi
29: b8 00 00 00 00 mov $0x0,%eax
2e: 48 ba 00 00 00 00 00 movabs $0x0,%rdx
35: 00 00 00
38: ff d2 callq *%rdx
3a: 89 45 fc mov %eax,-0x4(%rbp)
t += global_arr[7];
3d: 48 b8 00 00 00 00 00 movabs $0x0,%rax
44: 00 00 00
47: 8b 40 1c mov 0x1c(%rax),%eax
4a: 01 45 fc add %eax,-0x4(%rbp)
t += static_arr[7];
4d: 48 b8 00 00 00 00 00 movabs $0x0,%rax
54: 00 00 00
57: 8b 40 1c mov 0x1c(%rax),%eax
5a: 01 45 fc add %eax,-0x4(%rbp)
t += global_arr_big[7];
5d: 48 b8 00 00 00 00 00 movabs $0x0,%rax
64: 00 00 00
67: 8b 40 1c mov 0x1c(%rax),%eax
6a: 01 45 fc add %eax,-0x4(%rbp)
t += static_arr_big[7];
6d: 48 b8 00 00 00 00 00 movabs $0x0,%rax
74: 00 00 00
77: 8b 40 1c mov 0x1c(%rax),%eax
7a: 01 45 fc add %eax,-0x4(%rbp)
return t;
7d: 8b 45 fc mov -0x4(%rbp),%eax
}
80: c9 leaveq
81: c3 retq
ç¹°ãè¿ãã«ãªããŸãããåé 眮ã確èªãããšäŸ¿å©ã§ãã
Relocation section '.rela.text' at offset 0x62c18 contains 5 entries:
Offset Info Type Sym. Value Sym. Name + Addend
000000000030 001500000001 R_X86_64_64 0000000000000000 global_func + 0
00000000003f 001100000001 R_X86_64_64 0000000000000000 global_arr + 0
00000000004f 000300000001 R_X86_64_64 0000000000000000 .data + 1a0
00000000005f 001200000001 R_X86_64_64 0000000000000340 global_arr_big + 0
00000000006f 000300000001 R_X86_64_64 0000000000000000 .data + 31080
ã³ãŒããšããŒã¿ã»ã¯ã·ã§ã³ã®ãµã€ãºãæ³å®ããå¿ èŠããªãããã倧èŠæš¡ãªã³ãŒãã¢ãã«ã¯ããªãåäžã§ããããã¹ãŠã®ããŒã¿ãžã®ã¢ã¯ã»ã¹ãåãæ¹æ³ã§å®çŸ©ããŸããããäžåºŠèŠãŠã¿ãŸããã
global_arr
ïŒ
t += global_arr[7];
3d: 48 b8 00 00 00 00 00 movabs $0x0,%rax
44: 00 00 00
47: 8b 40 1c mov 0x1c(%rax),%eax
4a: 01 45 fc add %eax,-0x4(%rbp)
2ã€ã®ããŒã ãã¢ã¬ã€ããç®çã®å€ãååŸããå¿ èŠããããŸããæåã®ã³ãã³ãã¯çµ¶å¯Ÿ64ãããã¢ãã¬ã¹ãã«é 眮ããŸã
rax
ãããã¯ãåŸã§èª¬æããããã«ãã¢ãã¬ã¹ã«ãªãããšãããããŸããglobal_arr
ã2çªç®ã®ã³ãã³ãã¯ã¯ãŒãã(rax) + 01
ã«ããŒããeax
ãŸãã
ããã§ã¯ã§ããŒã ã«çŠç¹ãåœãŠãŠã¿ãŸããã
0x3d
ãmovabs
絶察64ãããçmov
x64ã¢ãŒããã¯ãã£ã€ã³ãå®å
šãª64ãããå®æ°ãçŽæ¥ã¬ãžã¹ã¿ãŒã«ã¹ããŒã§ããŸããéã¢ã»ã³ãã«ãããã³ãŒãã§ã¯ããã®å®æ°ã®å€ã¯ãŒãã«çããã®ã§ãçããæ±ããã«ã¯åé
眮ããŒãã«ã䜿çšããå¿
èŠããããŸãããã®äžã§ã次ã®å€ã䜿çšR_X86_64_64
ããŠãã¢ãã¬ã¹0x3f
ã§ã®ãªãã©ã³ãã®çµ¶å¯Ÿåé
眮ãèŠã€ãããŸããã·ã³ãã«ã®å€ãšå ç®å€ãã·ããã«æ»ããŸããèšãæãããšãrax
絶察ã¢ãã¬ã¹ãå«ãŸããŸãglobal_arr
ã
åŒã³åºãæ©èœã¯ã©ãã§ããïŒ
int t = global_func(argc);
24: 8b 45 ec mov -0x14(%rbp),%eax
27: 89 c7 mov %eax,%edi
29: b8 00 00 00 00 mov $0x0,%eax
2e: 48 ba 00 00 00 00 00 movabs $0x0,%rdx
35: 00 00 00
38: ff d2 callq *%rdx
3a: 89 45 fc mov %eax,-0x4(%rbp)
atã§é¢æ°ãåŒã³åºã
movabs
ã³ãã³ãã«ç¶ã
ãã®ã¯ãã§ã«ç¥ã£ãŠããŸãã察å¿ããåé
眮ãèŠãŠãããŒã¿ãžã®ã¢ã¯ã»ã¹ãšã©ã®çšåºŠé¡äŒŒããŠãããã確èªããŠãã ããã
ã芧ã®ãšããã倧èŠæš¡ãªã³ãŒãã¢ãã«ã§ã¯ãã³ãŒããšããŒã¿ã»ã¯ã·ã§ã³ã®ãµã€ãºãããã³æåã®æçµçãªé
眮ã«ã€ããŠã®ä»®å®ã¯ãããŸãããããã¯ããã»ãŒããã©ãã¯ãã®äžçš®ã§ããã絶察64ãããã¹ãããã«ããæåãåã«åç
§ããŠããŸãããã ããå°ããªã³ãŒãã¢ãã«ãšæ¯èŒããŠã倧ããªã¢ãã«ã§ã¯åæåã«å¯ŸããŠè¿œå ã®ã³ãã³ãã䜿çšããå¿
èŠãããããšã«æ³šæããŠãã ãããããã¯ã»ãã¥ãªãã£ã®ä»£åã§ããcall
rdx
ãããã£ãŠã2ã€ã®å®å šã«å察ã®ã¢ãã«ã«åºäŒããŸããïŒå°ããªã³ãŒãã¢ãã«ã¯ãã¹ãŠãäžã®2ã®ã¬ãã€ãã®ã¡ã¢ãªã«åãŸããšæ³å®ããŠããŸããã倧ããªã¢ãã«ã¯äœãäžå¯èœã§ã¯ãªãããã¹ãŠã®æåãã©ãã«ã§ããããšæ³å®ããŠããŸã64ãããã¢ãã¬ã¹ç©ºéã2ã€ã®éã®ãã¬ãŒããªãã¯ãäžéã³ãŒãã¢ãã«ã§ãã
äžåã³ãŒãã¢ãã«
åãšåãããã«ãããã®åŒçšã®ç¿»èš³ãèŠãŠã¿ãŸããã
man
gcc
ïŒ
-mcmodel=medium
: . . , -mlarge-data-threshold, bss . , .
å°ããªã³ãŒãã¢ãã«ãšåæ§ã«ãäžéã®ã¢ãã«ã§ã¯ãã³ãŒãå šäœã2ã®ã¬ãã€ã以äžã«é 眮ãããŠãããšæ³å®ããŠããŸããããã«ãããããããããŒã¿ã¯ããããããã®ã¬ãã€ããã®äžäœ2ã®ã¬ãã€ãã«é 眮ãããã¡ã¢ãªã¹ããŒã¹ãç¡å¶éãã«ç¡å¶éã«åå²ãããŸããããŒã¿ã¯ãå®çŸ©ã«ãã64ãããã€ãã«çããå¶éãè¶ ãããšã倧ããªã«ããŽãªã«åé¡ãããŸãã
ã»ã¯ã·ã§ã³ããé¡æšããŠãããã°ããŒã¿ã®å¹³åã³ãŒãã¢ãã«ãæ±ããšãã«æ³šæããããšãéèŠã§ãã
.data
ãš.bss
ïŒãç¹å¥ãªã»ã¯ã·ã§ã³ãäœæãããŠãã.ldata
ãš.lbss
ãããã¯ãçŸåšã®èšäºã®ãããã¯ã®ããªãºã ã§ã¯ããã»ã©éèŠã§ã¯ãããŸããããå°ãããããéžè±ããŸãããã®åé¡ã®è©³çŽ°ã«ã€ããŠã¯ãABIãåç
§ããŠãã ããã
ãããã®é åãäŸã«è¡šç€ºãããçç±ãæããã«ãªããŸããã
_big
ïŒãããã¯ãããããã200ãããã€ãã®ãµã€ãºã§ãããããã°ããŒã¿ãã解éããããã®äžéã¢ãã«ã§å¿
èŠã§ãã以äžã«ãå解ã®çµæã瀺ããŸãã
int main(int argc, const char* argv[])
{
15: 55 push %rbp
16: 48 89 e5 mov %rsp,%rbp
19: 48 83 ec 20 sub $0x20,%rsp
1d: 89 7d ec mov %edi,-0x14(%rbp)
20: 48 89 75 e0 mov %rsi,-0x20(%rbp)
int t = global_func(argc);
24: 8b 45 ec mov -0x14(%rbp),%eax
27: 89 c7 mov %eax,%edi
29: b8 00 00 00 00 mov $0x0,%eax
2e: e8 00 00 00 00 callq 33 <main+0x1e>
33: 89 45 fc mov %eax,-0x4(%rbp)
t += global_arr[7];
36: 8b 05 00 00 00 00 mov 0x0(%rip),%eax
3c: 01 45 fc add %eax,-0x4(%rbp)
t += static_arr[7];
3f: 8b 05 00 00 00 00 mov 0x0(%rip),%eax
45: 01 45 fc add %eax,-0x4(%rbp)
t += global_arr_big[7];
48: 48 b8 00 00 00 00 00 movabs $0x0,%rax
4f: 00 00 00
52: 8b 40 1c mov 0x1c(%rax),%eax
55: 01 45 fc add %eax,-0x4(%rbp)
t += static_arr_big[7];
58: 48 b8 00 00 00 00 00 movabs $0x0,%rax
5f: 00 00 00
62: 8b 40 1c mov 0x1c(%rax),%eax
65: 01 45 fc add %eax,-0x4(%rbp)
return t;
68: 8b 45 fc mov -0x4(%rbp),%eax
}
6b: c9 leaveq
6c: c3 retq
é åãžã®ã¢ã¯ã»ã¹ãã©ã®ããã«è¡ããããã«æ³šæããŠãã ãããé åãžã®ã¢ã¯ã»ã¹ã¯
_big
倧ããªã³ãŒãã¢ãã«ã®ã¡ãœãããçµç±ããä»ã®é
åãžã®ã¢ã¯ã»ã¹ã¯å°ããªã¢ãã«ã®ã¡ãœãããçµç±ããŸããé¢æ°ãå°ããªã³ãŒãã¢ãã«ã¡ãœããã䜿çšããŠåŒã³åºãããŸããåé
眮ã¯åã®äŸãšéåžžã«äŒŒãŠãããããããã§ã¯èª¬æããŸããã
äžéã³ãŒãã¢ãã«ã¯ã倧èŠæš¡ã¢ãã«ãšå°èŠæš¡ã¢ãã«ã®éã®å·§ã¿ãªåŠ¥åæ¡ã§ããããã°ã©ã ã³ãŒãã倧ãããªããããå¯èœæ§ã¯äœã[4]ãéçã«ãªã³ã¯ãããããŒã¿ã®å€§ããªãã£ã³ã¯ã®ã¿ããããããããçš®ã®èšå€§ãªããŒãã«æ€çŽ¢ã®äžéšãšããŠã2ã®ã¬ãã€ãã®å¶éãè¶ ããŠç§»åã§ããŸããäžéã³ãŒãã¢ãã«ã¯ããã®ãããªå€§ããªããŒã¿ãã£ã³ã¯ããã£ã«ã¿ãªã³ã°ããŠç¹å¥ãªæ¹æ³ã§åŠçãããããé¢æ°ãšå°ããªã·ã³ãã«ã®ã³ãŒãã«ããåŒã³åºãã¯ãå°ããªã³ãŒãã¢ãã«ãšåããããå¹ççã§ããã©ãŒãžã¢ãã«ãšåæ§ã«ãã©ãŒãžã·ã³ãã«ãžã®ã¢ã¯ã»ã¹ã®ã¿ããã©ãŒãžã¢ãã«ã®å®å šãª64ãããã¡ãœããã䜿çšããã³ãŒããå¿ èŠãšããŸãã
å°ããªPICã³ãŒãã¢ãã«
次ã«ãã³ãŒãã¢ãã«ã®PICããªã¢ã³ããèŠãŠã¿ãŸããããåãšåãããã«ãå°ããªã¢ãã«ããå§ããŸãã[5]以äžã¯ãå°ããªPICã¢ãã«ã§ã³ã³ãã€ã«ãããã³ãŒãã®äŸã§ãã
int main(int argc, const char* argv[])
{
15: 55 push %rbp
16: 48 89 e5 mov %rsp,%rbp
19: 48 83 ec 20 sub $0x20,%rsp
1d: 89 7d ec mov %edi,-0x14(%rbp)
20: 48 89 75 e0 mov %rsi,-0x20(%rbp)
int t = global_func(argc);
24: 8b 45 ec mov -0x14(%rbp),%eax
27: 89 c7 mov %eax,%edi
29: b8 00 00 00 00 mov $0x0,%eax
2e: e8 00 00 00 00 callq 33 <main+0x1e>
33: 89 45 fc mov %eax,-0x4(%rbp)
t += global_arr[7];
36: 48 8b 05 00 00 00 00 mov 0x0(%rip),%rax
3d: 8b 40 1c mov 0x1c(%rax),%eax
40: 01 45 fc add %eax,-0x4(%rbp)
t += static_arr[7];
43: 8b 05 00 00 00 00 mov 0x0(%rip),%eax
49: 01 45 fc add %eax,-0x4(%rbp)
t += global_arr_big[7];
4c: 48 8b 05 00 00 00 00 mov 0x0(%rip),%rax
53: 8b 40 1c mov 0x1c(%rax),%eax
56: 01 45 fc add %eax,-0x4(%rbp)
t += static_arr_big[7];
59: 8b 05 00 00 00 00 mov 0x0(%rip),%eax
5f: 01 45 fc add %eax,-0x4(%rbp)
return t;
62: 8b 45 fc mov -0x4(%rbp),%eax
}
65: c9 leaveq
66: c3 retq
移転ïŒ
Relocation section '.rela.text' at offset 0x62ce8 contains 5 entries:
Offset Info Type Sym. Value Sym. Name + Addend
00000000002f 001600000004 R_X86_64_PLT32 0000000000000000 global_func - 4
000000000039 001100000009 R_X86_64_GOTPCREL 0000000000000000 global_arr - 4
000000000045 000300000002 R_X86_64_PC32 0000000000000000 .data + 1b8
00000000004f 001200000009 R_X86_64_GOTPCREL 0000000000000340 global_arr_big - 4
00000000005b 000300000002 R_X86_64_PC32 0000000000000000 .data + 31098
倧ããªããŒã¿ãšå°ããªããŒã¿ã®éãã¯å°ããªã³ãŒãã¢ãã«ã§ã¯äœã®åœ¹å²ãæãããªããããPICã䜿çšããŠã³ãŒããçæãããšãã®éèŠãªãã€ã³ãã§ããããŒã«ã«ïŒéçïŒã·ã³ãã«ãšã°ããŒãã«ã·ã³ãã«ã®éãã«çŠç¹ãåœãŠãŸãã
ã芧ã®ãšãããéçé åçšã«çæãããã³ãŒããšPIC以å€ã®å Žåã®ã³ãŒãã«éãã¯ãããŸãããããã¯ãx64ã¢ãŒããã¯ãã£ã®å©ç¹ã®1ã€ã§ããããŒã¿ãžã®IPçžå¯Ÿã¢ã¯ã»ã¹ã®ãããã§ãå°ãªããšãã·ã³ãã«ãžã®å€éšã¢ã¯ã»ã¹ãå¿ èŠã«ãªããŸã§ãããŒãã¹ãšããŠPICãååŸã§ããŸãããã¹ãŠã®ã³ãã³ããšåé 眮ã¯åããŸãŸãªã®ã§ãããããå床åŠçããå¿ èŠã¯ãããŸããã
ã°ããŒãã«é åã«æ³šæãæãã®ã¯èå³æ·±ãããšã§ããPICã§ã¯ãã°ããŒãã«ããŒã¿ã¯GOTãééããå¿ èŠãããããšãæãåºããŠãã ããã以äžã«ã¢ã¯ã»ã¹ããã³ãŒãã瀺ããŸã
global_arr
ã
t += global_arr[7];
36: 48 8b 05 00 00 00 00 mov 0x0(%rip),%rax
3d: 8b 40 1c mov 0x1c(%rax),%eax
40: 01 45 fc add %eax,-0x4(%rbp)
察象ãšãªãåé 眮ã¯
R_X86_64_GOTPCREL
ãGOTå
ã®ã·ã³ãã«ã®å
¥åã®äœçœ®ã«é
ãå ãããã®ãããåé
眮ãé©çšããããã®ã·ãããå·®ãåŒãããã®ã§ããã€ãŸããRIPïŒæ¬¡ã®åœä»€ïŒãšglobal_arr
GOTã§äºçŽãããŠããã¹ãããã®éã®çžå¯Ÿçãªã·ãããã³ãã³ãã«ããããããŸãããããã£ãŠãå®éã®ã¢ãã¬ã¹ã¯rax
ã³ãã³ãã®0x36
addressã«é
眮ãããŸãglobal_arr
ããã®ã¹ãããã®åŸã«ãã¢ãã¬ã¹ãžã®åç
§ã®ãªã»ãããšãã®global_arr
7çªç®ã®èŠçŽ ãžã®ãªãã»ãããç¶ãeax
ãŸãã
次ã«ãé¢æ°åŒã³åºããèŠãŠã¿ãŸãããã
int t = global_func(argc);
24: 8b 45 ec mov -0x14(%rbp),%eax
27: 89 c7 mov %eax,%edi
29: b8 00 00 00 00 mov $0x0,%eax
2e: e8 00 00 00 00 callq 33 <main+0x1e>
33: 89 45 fc mov %eax,-0x4(%rbp)
ã·ã³ãã« ã®ãªãã©ã³ã
callq
ã¢ãã¬ã¹0x2e
ãR_X86_64_PLT32
ïŒPLTãšã³ããªã¢ãã¬ã¹ã®åé
眮ãšãåé
眮ã®é©çšã®ããã®é
ã®è² ã®ã·ãããã€ãŸããcallq
ã®PLTã¹ããªã³ã°ããŒããæ£ããåŒã³åºãå¿
èŠãããglobal_func
ãŸãã
ã³ã³ãã€ã©ãæé»çã«æ³å®ããŠããããšã«æ³šæããŠãã ãããGOTãšPLTã¯RIPçžå¯Ÿã¢ãã¬ã¹æå®ãä»ããŠã¢ã¯ã»ã¹ã§ãããšããããšã§ããããã¯ããã®ã¢ãã«ãä»ã®PICã³ãŒãã¢ãã«ããªã¢ã³ããšæ¯èŒãããšãã«éèŠã«ãªããŸãã
倧èŠæš¡ãªPICã³ãŒãã¢ãã«
å解ïŒ
int main(int argc, const char* argv[])
{
15: 55 push %rbp
16: 48 89 e5 mov %rsp,%rbp
19: 53 push %rbx
1a: 48 83 ec 28 sub $0x28,%rsp
1e: 48 8d 1d f9 ff ff ff lea -0x7(%rip),%rbx
25: 49 bb 00 00 00 00 00 movabs $0x0,%r11
2c: 00 00 00
2f: 4c 01 db add %r11,%rbx
32: 89 7d dc mov %edi,-0x24(%rbp)
35: 48 89 75 d0 mov %rsi,-0x30(%rbp)
int t = global_func(argc);
39: 8b 45 dc mov -0x24(%rbp),%eax
3c: 89 c7 mov %eax,%edi
3e: b8 00 00 00 00 mov $0x0,%eax
43: 48 ba 00 00 00 00 00 movabs $0x0,%rdx
4a: 00 00 00
4d: 48 01 da add %rbx,%rdx
50: ff d2 callq *%rdx
52: 89 45 ec mov %eax,-0x14(%rbp)
t += global_arr[7];
55: 48 b8 00 00 00 00 00 movabs $0x0,%rax
5c: 00 00 00
5f: 48 8b 04 03 mov (%rbx,%rax,1),%rax
63: 8b 40 1c mov 0x1c(%rax),%eax
66: 01 45 ec add %eax,-0x14(%rbp)
t += static_arr[7];
69: 48 b8 00 00 00 00 00 movabs $0x0,%rax
70: 00 00 00
73: 8b 44 03 1c mov 0x1c(%rbx,%rax,1),%eax
77: 01 45 ec add %eax,-0x14(%rbp)
t += global_arr_big[7];
7a: 48 b8 00 00 00 00 00 movabs $0x0,%rax
81: 00 00 00
84: 48 8b 04 03 mov (%rbx,%rax,1),%rax
88: 8b 40 1c mov 0x1c(%rax),%eax
8b: 01 45 ec add %eax,-0x14(%rbp)
t += static_arr_big[7];
8e: 48 b8 00 00 00 00 00 movabs $0x0,%rax
95: 00 00 00
98: 8b 44 03 1c mov 0x1c(%rbx,%rax,1),%eax
9c: 01 45 ec add %eax,-0x14(%rbp)
return t;
9f: 8b 45 ec mov -0x14(%rbp),%eax
}
a2: 48 83 c4 28 add $0x28,%rsp
a6: 5b pop %rbx
a7: c9 leaveq
a8: c3 retq
åé çœ®ïŒ ä»åã¯ã倧ããªããŒã¿ãšå°ããªããŒã¿ã®éãã¯ãŸã åé¡ã§ã¯ãªãã®ã§ããšã«çŠç¹ãåœãŠãŸããããããæåã«ãã®ã³ãŒãã®ããããŒã°ã«æ³šæãæãå¿ èŠããããŸãã以åã¯ããã«ééããŠããŸããã§ããã
Relocation section '.rela.text' at offset 0x62c70 contains 6 entries:
Offset Info Type Sym. Value Sym. Name + Addend
000000000027 00150000001d R_X86_64_GOTPC64 0000000000000000 _GLOBAL_OFFSET_TABLE_ + 9
000000000045 00160000001f R_X86_64_PLTOFF64 0000000000000000 global_func + 0
000000000057 00110000001b R_X86_64_GOT64 0000000000000000 global_arr + 0
00000000006b 000800000019 R_X86_64_GOTOFF64 00000000000001a0 static_arr + 0
00000000007c 00120000001b R_X86_64_GOT64 0000000000000340 global_arr_big + 0
000000000090 000900000019 R_X86_64_GOTOFF64 0000000000031080 static_arr_big + 0
static_arr
global_arr
1e: 48 8d 1d f9 ff ff ff lea -0x7(%rip),%rbx
25: 49 bb 00 00 00 00 00 movabs $0x0,%r11
2c: 00 00 00
2f: 4c 01 db add %r11,%rbx
以äžã«ãABIããã®é¢é£ããåŒçšã®ç¿»èš³ã瀺ããŸãã
( GOT) AMD64 IP- . GOT . GOT , AMD64 ISA 32 .
äžèšã®ããããŒã°ãGOTã¢ãã¬ã¹ãèšç®ããæ¹æ³ãèŠãŠã¿ãŸããããæåã«ãã¢ãã¬ã¹ã®ã³ãã³ã
0x1e
ãèªèº«ã®ã¢ãã¬ã¹ãã«ããŒãããŸãrbx
ã次ã«ãåé
眮ãšãšãã«R_X86_64_GOTPC64
ã絶察64ãããã¹ããããå®è¡ããr11
ãŸãããã®åé
眮ã¯ã次ã®ããšãæå³ããŸããGOTã¢ãã¬ã¹ãååŸããã·ãããããã·ãããæžç®ããŠãé
ãè¿œå ããŸããæåŸã«ãäœæã®ããŒã 0x2f
ãäž¡æ¹ã®çµæãåèšããŸããçµæã¯GOTã®çµ¶å¯Ÿã¢ãã¬ã¹rbx
ã§ãã [7]
ãªãGOTã¢ãã¬ã¹ãèšç®ããå¿ èŠãããã®ã§ããïŒãŸããåŒçšã§è¿°ã¹ãããã«ã倧èŠæš¡ãªã³ãŒãã¢ãã«ã§ã¯ã32ãããã®RIPçžå¯Ÿã·ããã§GOTã¢ãã¬ãã·ã³ã°ãååã§ãããšæ³å®ã§ããªããããå®å šãª64ãããã¢ãã¬ã¹ãå¿ èŠã§ãã次ã«ãPICããªãšãŒã·ã§ã³ãåŒãç¶ã䜿çšãããããåçŽã«çµ¶å¯Ÿã¢ãã¬ã¹ãã¬ãžã¹ã¿ãŒã«å ¥ããããšã¯ã§ããŸãããããããã¢ãã¬ã¹èªäœã¯RIPã«å¯ŸããŠçžå¯Ÿçã«èšç®ããå¿ èŠããããŸãããããããããŒã°ã®ç®çã§ãã64ãããã®RIPçžå¯Ÿèšç®ãå®è¡ããŸãã
ãšã«ããã
rbx
GOTã¢ãã¬ã¹ãååŸããã®ã§ãã¢ã¯ã»ã¹æ¹æ³ãèŠãŠã¿ãŸãããstatic_arr
ã
t += static_arr[7];
69: 48 b8 00 00 00 00 00 movabs $0x0,%rax
70: 00 00 00
73: 8b 44 03 1c mov 0x1c(%rbx,%rax,1),%eax
77: 01 45 ec add %eax,-0x14(%rbp)
æåã®ã³ãã³ãã®åé 眮ã¯ã
R_X86_64_GOTOFF64
ã·ã³ãã«ãšãã€ãã¹GOTé
ã§ãããã®å Žåãããã¯ã¢ãã¬ã¹static_arr
ãšGOTã¢ãã¬ã¹éã®çžå¯Ÿãªãã»ããã§ãã次ã®åœä»€ã¯ãçµæãrbx
ïŒçµ¶å¯ŸGOTã¢ãã¬ã¹ïŒã«è¿œå ããåç
§ã«ãã£ãŠãªãã»ããããªã»ããããŸã0x1c
ããã®ãããªèšç®ãç°¡åã«èŠèŠåããããã«ãç䌌Cã®äŸã以äžã«ç€ºããŸãã
// char* static_arr
// char* GOT
rax = static_arr + 0 - GOT; // rax now contains an offset
eax = *(rbx + rax + 0x1c); // rbx == GOT, so eax now contains
// *(GOT + static_arr - GOT + 0x1c) or
// *(static_arr + 0x1c)
èå³æ·±ãç¹ã«æ³šæããŠãã ãããGOTã¢ãã¬ã¹ã¯ãžã®ãã€ã³ãã£ã³ã°ãšããŠäœ¿çšãã
static_arr
ãŸããéåžžãGOTã«ã¯ã·ã³ãã«ã®ã¢ãã¬ã¹ãå«ãŸããŠããŸããããŸããGOTã¯static_arr
å€éšã·ã³ãã«ã§ã¯ãªããããGOTå
ã«æ ŒçŽããå¿
èŠã¯ãããŸããããã ãããã®å ŽåãGOTã¯ããŒã¿ã»ã¯ã·ã§ã³ã®çžå¯Ÿã·ã³ãã«ã¢ãã¬ã¹ãžã®ãã€ã³ãã£ã³ã°ãšããŠäœ¿çšãããŸãããã®ã¢ãã¬ã¹ã¯ãç¹ã«å Žæã«äŸåããªããããå®å
šãª64ãããã·ããã§èŠã€ããããšãã§ããŸãããªã³ã«ã¯ãã®åé
眮ãåŠçã§ãããããèµ·åæã«ã³ãŒãã»ã¯ã·ã§ã³ãå€æŽããå¿
èŠã¯ãããŸããã
ããããã©ã
global_arr
ã§ããïŒ
t += global_arr[7];
55: 48 b8 00 00 00 00 00 movabs $0x0,%rax
5c: 00 00 00
5f: 48 8b 04 03 mov (%rbx,%rax,1),%rax
63: 8b 40 1c mov 0x1c(%rax),%eax
66: 01 45 ec add %eax,-0x14(%rbp)
ãã®ã³ãŒãã¯å°ãé·ããåé 眮ã¯éåžžã®ãã®ãšã¯ç°ãªããŸããå®éã«ã¯ãGOTã¯ãããäŒçµ±çãªæ¹æ³ã§äœ¿çšãããŠããïŒç§»è»¢
R_X86_64_GOT64
ã®ããmovabs
ã ãã§ã¯GOTã®ã·ããé
眮ããæ©èœåããrax
ã¢ãã¬ã¹ãé
眮ãããŠãããglobal_arr
ãã¢ãã¬ã¹ã®ã³ãã³ãã¯ãGOTãã0x5f
ã¢ãã¬ã¹global_arr
ãååŸãããããã«é
眮ãrax
ãŸãã次ã®ã³ãã³ãã¯ããžã®åç
§ããªã»ããããå€ãglobal_arr[7]
ã«å
¥ããŸãeax
ã
ã§ã¯ãã®ã³ãŒããªã³ã¯ãèŠãŠã¿ãŸããã
global_func
ã倧èŠæš¡ãªã³ãŒãã¢ãã«ã§ã¯ãã³ãŒãã»ã¯ã·ã§ã³ã®ãµã€ãºã«ã€ããŠæ³å®ã§ããªããããPLTã«ã¢ã¯ã»ã¹ããå Žåã§ãã絶察64ãããã¢ãã¬ã¹ãå¿
èŠã§ãããšæ³å®ããå¿
èŠããããŸãã
int t = global_func(argc);
39: 8b 45 dc mov -0x24(%rbp),%eax
3c: 89 c7 mov %eax,%edi
3e: b8 00 00 00 00 mov $0x0,%eax
43: 48 ba 00 00 00 00 00 movabs $0x0,%rdx
4a: 00 00 00
4d: 48 01 da add %rbx,%rdx
50: ff d2 callq *%rdx
52: 89 45 ec mov %eax,-0x14(%rbp)
察象ãšãªãåé 眮ã¯
R_X86_64_PLTOFF64
ãPLTå
¥åglobal_func
ã¢ãã¬ã¹ããGOTã¢ãã¬ã¹ãåŒãããã®ã§ããçµæã¯ã«é
眮ããrdx
ãããã§é
眮ãããŸãrbx
ïŒGOTã®çµ¶å¯Ÿã¢ãã¬ã¹ïŒããã®çµæãæã
ã¯ã®ããã®PLTãšã³ããªã®ã¢ãã¬ã¹ãååŸããŸãã
ããã§ãGOTãã¢ã³ã«ãŒãšããŠäœ¿çšãããŠããããšã«æ³šæããŠãã ãããä»åã¯ãPLTå
¥åã®ãªãã»ãããžã®ã¢ãã¬ã¹ã«äŸåããªãåç
§ãæäŸããŸããglobal_func
rdx
å¹³åPICã³ãŒãã¢ãã«
æåŸã«ãå¹³åçãªPICã¢ãã«ã®çæã³ãŒããå解ããŸãã
int main(int argc, const char* argv[])
{
15: 55 push %rbp
16: 48 89 e5 mov %rsp,%rbp
19: 53 push %rbx
1a: 48 83 ec 28 sub $0x28,%rsp
1e: 48 8d 1d 00 00 00 00 lea 0x0(%rip),%rbx
25: 89 7d dc mov %edi,-0x24(%rbp)
28: 48 89 75 d0 mov %rsi,-0x30(%rbp)
int t = global_func(argc);
2c: 8b 45 dc mov -0x24(%rbp),%eax
2f: 89 c7 mov %eax,%edi
31: b8 00 00 00 00 mov $0x0,%eax
36: e8 00 00 00 00 callq 3b <main+0x26>
3b: 89 45 ec mov %eax,-0x14(%rbp)
t += global_arr[7];
3e: 48 8b 05 00 00 00 00 mov 0x0(%rip),%rax
45: 8b 40 1c mov 0x1c(%rax),%eax
48: 01 45 ec add %eax,-0x14(%rbp)
t += static_arr[7];
4b: 8b 05 00 00 00 00 mov 0x0(%rip),%eax
51: 01 45 ec add %eax,-0x14(%rbp)
t += global_arr_big[7];
54: 48 8b 05 00 00 00 00 mov 0x0(%rip),%rax
5b: 8b 40 1c mov 0x1c(%rax),%eax
5e: 01 45 ec add %eax,-0x14(%rbp)
t += static_arr_big[7];
61: 48 b8 00 00 00 00 00 movabs $0x0,%rax
68: 00 00 00
6b: 8b 44 03 1c mov 0x1c(%rbx,%rax,1),%eax
6f: 01 45 ec add %eax,-0x14(%rbp)
return t;
72: 8b 45 ec mov -0x14(%rbp),%eax
}
75: 48 83 c4 28 add $0x28,%rsp
79: 5b pop %rbx
7a: c9 leaveq
7b: c3 retq
移転ïŒ
Relocation section '.rela.text' at offset 0x62d60 contains 6 entries:
Offset Info Type Sym. Value Sym. Name + Addend
000000000021 00160000001a R_X86_64_GOTPC32 0000000000000000 _GLOBAL_OFFSET_TABLE_ - 4
000000000037 001700000004 R_X86_64_PLT32 0000000000000000 global_func - 4
000000000041 001200000009 R_X86_64_GOTPCREL 0000000000000000 global_arr - 4
00000000004d 000300000002 R_X86_64_PC32 0000000000000000 .data + 1b8
000000000057 001300000009 R_X86_64_GOTPCREL 0000000000000000 global_arr_big - 4
000000000063 000a00000019 R_X86_64_GOTOFF64 0000000000030d40 static_arr_big + 0
ãŸããé¢æ°åŒã³åºããåé€ããŸããããå°èŠæš¡ã¢ãã«ãšåæ§ã«ãäžéã¢ãã«ã§ã¯ãã³ãŒãåç §ã32ãããRIPã·ããã®å¶éãè¶ ããªãããšãåæãšããŠãããããåŒã³åºãã®
global_func
ã³ãŒãã¯ãå°èŠæš¡PICã¢ãã«ã®åãã³ãŒããstatic_arr
ããã³å°èŠæš¡ããŒã¿é
åãšã«å®å
šã«é¡äŒŒããŠããŸãglobal_arr
ããããã£ãŠãããã§ã¯ããã°ããŒã¿é
åã«çŠç¹ãåœãŠãŸãããæåã«ããããŒã°ã«ã€ããŠè©±ããŸããããããã§ã¯ã倧ããªããŒã¿ã¢ãã«ã®ããããŒã°ãšã¯ç°ãªããŸãã
1e: 48 8d 1d 00 00 00 00 lea 0x0(%rip),%rbx
ããã¯å šäœã®ããããŒã°ã§ããåé 眮ã䜿çšã
R_X86_64_GOTPC32
ãŠGOTã¢ãã¬ã¹ãã«é
眮ããããã«å¿
èŠãªrbx
ããŒã ã¯1ã€ã ãã§ãïŒå€§èŠæš¡ã¢ãã«ã§ã¯3ã€ïŒãéãã¯ãªãã§ããïŒçãäžã®ã¢ãã«ã§ã¯GOTããããã°ããŒã¿ã»ã¯ã·ã§ã³ãã®äžéšã§ã¯ãªãããã32ãããã·ããå
ã§äœ¿çšã§ãããšæ³å®ããŠããŸãã倧èŠæš¡ãªã¢ãã«ã§ã¯ããã®ãããªä»®å®ãè¡ãããšãã§ãããå®å
šãª64ãããã·ããã䜿çšããããåŸãŸããã§ããã
èå³æ·±ãã®ã¯ãã¢ã¯ã»ã¹ããããã®
global_arr_big
ã³ãŒããå°ããªPICã¢ãã«ã®åãã³ãŒãã«äŒŒãŠããããšã§ããããã¯ãããã«ã¢ãã«ã®ããããŒã°ãã©ãŒãžã¢ãã«ã®ããããŒã°ãããçãã®ãšåãçç±ã§ãGOTã¯32ãããRIPçžå¯Ÿã¢ãã¬ãã·ã³ã°å
ã§äœ¿çšã§ãããšæ³å®ããŠããŸãã確ãã«ãéåžžã«global_arr_big
ãã®ãããªã¢ã¯ã»ã¹ã¯ã§ããŸããããå®églobal_arr_big
ã«ã¯GOT å
ã«å®å
šãª64ãããã¢ãã¬ã¹ã®åœ¢åŒã§é
眮ãããŠããããããã®ã±ãŒã¹ã¯GOTãã«ããŒããŠããŸãã
ãã ããç¶æ³ã¯æ¬¡ã®ããã«ç°ãªã
static_arr_big
ãŸãã
t += static_arr_big[7];
61: 48 b8 00 00 00 00 00 movabs $0x0,%rax
68: 00 00 00
6b: 8b 44 03 1c mov 0x1c(%rbx,%rax,1),%eax
6f: 01 45 ec add %eax,-0x14(%rbp)
ãã®ã±ãŒã¹ã¯å€§èŠæš¡ãªPICã³ãŒãã¢ãã«ã«äŒŒãŠããŸããããã§ã¯ãGOTèªäœã«ã¯ãªãã·ã³ãã«ã®çµ¶å¯Ÿã¢ãã¬ã¹ãååŸããŠããããã§ããããã¯å€§ããªã·ã³ãã«ã§ãããäžäœ2ã®ã¬ãã€ãã«ãããšã¯æ³å®ã§ããªãããã倧ããªã¢ãã«ãšåæ§ã«ã64ãããã®PICã·ãããå¿ èŠã§ãã
ããŒãïŒ
[1]ã³ãŒãââã¢ãã«ãš64ãããããŒã¿ã¢ãã«ããã³Intelã¡ã¢ãªã¢ãã«ãæ··åããªãã§ãã ããããããã¯ãã¹ãŠç°ãªããããã¯ã§ãã
[2]èŠããŠããããšãéèŠã§ããå®éã®ã³ãã³ãã¯ã³ã³ãã€ã©ã«ãã£ãŠäœæãããã¢ãã¬ãã·ã³ã°ã¢ãŒãã¯ãã®ã¹ãããã§æ£ç¢ºã«ä¿®æ£ãããŸããã³ã³ãã€ã©ã¯ããªããžã§ã¯ãã¢ãžã¥ãŒã«ãã©ã®ããã°ã©ã ãŸãã¯å ±æã©ã€ãã©ãªã«åé¡ãããããèªèã§ããŸãããå°ãããã®ãšå€§ãããã®ããããŸãããªã³ã«ã¯æçµçãªããã°ã©ã ã®ãµã€ãºãç¥ã£ãŠããŸãããæé ãã§ãããªã³ã«ã¯ã³ãã³ãã®ã·ããã«åé 眮ã§ã®ã¿ããããåœãŠãããšãã§ããã³ãã³ãèªäœãå€æŽããããšã¯ã§ããŸããããããã£ãŠãã³ãŒãã¢ãã«ã®ãèŠçŽãã¯ãã³ã³ãã€ã«æã«ããã°ã©ããŒãã眲åãããå¿ èŠããããŸãã
[3]äžæãªç¹ãããå Žåã¯ã次ã®èšäºãã芧ãã ããã
[4]ãã ããããªã¥ãŒã ã¯åŸã ã«å¢å ããŠããŸããååãDebug + Assertsã®Clangãã«ãã確èªãããšãããã»ãŒ1ã®ã¬ãã€ãã«éããŸãããããã¯ãèªåçæãããã³ãŒãã®ãããã§ãã
[5] PICã®åäœããŸã ããããªãå ŽåïŒäžè¬ã«ãç¹ã«x64ã¢ãŒããã¯ãã£ãŒã®äž¡æ¹ã§ïŒããã®ãããã¯ã«é¢ãã次ã®èšäºãç解ããŠãã ããïŒ1åãš2åã
[6]ãããã£ãŠããªã³ã«ã¯ãªã³ã¯ãåå¥ã«è§£æ±ºããããšãã§ãããGOTåŠçããã€ãããã¯ããŒããŒã«ã·ããããå¿ èŠããããŸãã
[7] 0x25-0x7 + GOT-0x27 + 0x9 = GOT