Linux»ã±àÓïÑÔ¿ª·¢Ö¸ÄÏ
±à¼£º±ùÌìʹ ×÷Õߣº ³ö´¦£ºChinaUnix 2005-6-3
Ò»¡¢¼ò½é
×÷Ϊ×î»ù±¾µÄ±à³ÌÓïÑÔÖ®Ò»£¬»ã±àÓïÑÔËäȻӦÓõķ¶Î§²»ËãºÜ¹ã£¬µ«ÖØÒªÐÔÈ´ÎðÓ¹ÖÃÒÉ£¬ÒòΪËüÄܹ»Íê³ÉÐí¶àÆäËüÓïÑÔËùÎÞ·¨Íê³ÉµÄ¹¦ÄÜ¡£¾ÍÄà Linux ÄÚºËÀ´½²£¬ËäÈ»¾ø´ó²¿·Ö´úÂëÊÇÓà C ÓïÑÔ±àдµÄ£¬µ«ÈÔÈ»²»¿É±ÜÃâµØÔÚijЩ¹Ø¼üµØ·½Ê¹ÓÃÁË»ã±à´úÂ룬ÆäÖÐÖ÷ÒªÊÇÔÚ Linux µÄÆô¶¯²¿·Ö¡£ÓÉÓÚÕⲿ·Ö´úÂëÓëÓ²¼þµÄ¹Øϵ·Ç³£ÃÜÇУ¬¼´Ê¹ÊÇ C ÓïÑÔÒ²»áÓÐЩÁ¦²»´ÓÐÄ£¬¶ø»ã±àÓïÑÔÔòÄܹ»ºÜºÃÑﳤ±Ü¶Ì£¬×î´óÏ޶ȵط¢»ÓÓ²¼þµÄÐÔÄÜ¡£
´ó¶àÊýÇé¿öÏ Linux ³ÌÐòÔ±²»ÐèҪʹÓûã±àÓïÑÔ£¬ÒòΪ¼´±ãÊÇÓ²¼þÇý¶¯ÕâÑùµÄµ×²ã³ÌÐòÔÚ Linux ²Ù×÷ϵͳÖÐÒ²¿ÉÒÔÓÃÍêÈ«Óà C ÓïÑÔÀ´ÊµÏÖ£¬ÔÙ¼ÓÉÏ GCC ÕâÒ»ÓÅÐãµÄ±àÒëÆ÷Ä¿Ç°ÒѾÄܹ»¶Ô×îÖÕÉú³ÉµÄ´úÂë½øÐкܺõÄÓÅ»¯£¬µÄÈ·ÓÐ×ã¹»µÄÀíÓÉÈÃÎÒÃÇ¿ÉÒÔÔÝʱ½«»ã±àÓïÑÔÅ×ÔÚÒ»±ßÁË¡£µ«ÊµÏÖÇé¿öÊÇ Linux ³ÌÐòÔ±ÓÐʱ»¹ÊÇÐèҪʹÓûã±à£¬»òÕß²»µÃ²»Ê¹Óûã±à£¬ÀíÓɺܼòµ¥£º¾«¼ò¡¢¸ßЧºÍ libc ÎÞ¹ØÐÔ¡£¼ÙÉèÒªÒÆÖ² Linux µ½Ä³Ò»Ìض¨µÄǶÈëʽӲ¼þ»·¾³Ï£¬Ê×ÏȱØÈ»ÃæÁÙÈçºÎ¼õÉÙϵͳ´óС¡¢Ìá¸ßÖ´ÐÐЧÂʵÈÎÊÌ⣬´Ëʱ»òÐíÖ»Óлã±àÓïÑÔÄÜ°ïÉÏæÁË¡£
»ã±àÓïÑÔÖ±½Óͬ¼ÆËã»úµÄµ×²ãÈí¼þÉõÖÁÓ²¼þ½øÐн»»¥£¬Ëü¾ßÓÐÈçÏÂһЩÓŵ㣺
Äܹ»Ö±½Ó·ÃÎÊÓëÓ²¼þÏà¹ØµÄ´æ´¢Æ÷»ò I/O ¶Ë¿Ú£»
Äܹ»²»ÊܱàÒëÆ÷µÄÏÞÖÆ£¬¶ÔÉú³ÉµÄ¶þ½øÖÆ´úÂë½øÐÐÍêÈ«µÄ¿ØÖÆ£»
Äܹ»¶Ô¹Ø¼ü´úÂë½øÐиü׼ȷµÄ¿ØÖÆ£¬±ÜÃâÒòÏ̹߳²Í¬·ÃÎÊ»òÕßÓ²¼þÉ豸¹²ÏíÒýÆðµÄËÀËø£»
Äܹ»¸ù¾ÝÌض¨µÄÓ¦ÓöԴúÂë×ö×î¼ÑµÄÓÅ»¯£¬Ìá¸ßÔËÐÐËٶȣ»
Äܹ»×î´óÏ޶ȵط¢»ÓÓ²¼þµÄ¹¦ÄÜ¡£
ͬʱ»¹Ó¦¸ÃÈÏʶµ½£¬»ã±àÓïÑÔÊÇÒ»ÖÖ²ã´Î·Ç³£µÍµÄÓïÑÔ£¬Ëü½ö½ö¸ßÓÚÖ±½ÓÊÖ¹¤±àд¶þ½øÖƵĻúÆ÷Ö¸ÁîÂ룬Òò´Ë²»¿É±ÜÃâµØ´æÔÚһЩȱµã£º
±àдµÄ´úÂë·Ç³£ÄѶ®£¬²»ºÃά»¤£»
ºÜÈÝÒײúÉú bug£¬ÄÑÓÚµ÷ÊÔ£»
Ö»ÄÜÕë¶ÔÌض¨µÄÌåϵ½á¹¹ºÍ´¦ÀíÆ÷½øÐÐÓÅ»¯£»
¿ª·¢Ð§Âʺܵͣ¬Ê±¼ä³¤ÇÒµ¥µ÷¡£
Linux ÏÂÓûã±àÓïÑÔ±àдµÄ´úÂë¾ßÓÐÁ½ÖÖ²»Í¬µÄÐÎʽ¡£µÚÒ»ÖÖÊÇÍêÈ«µÄ»ã±à´úÂ룬ָµÄÊÇÕû¸ö³ÌÐòÈ«²¿Óûã±àÓïÑÔ±àд¡£¾¡¹ÜÊÇÍêÈ«µÄ»ã±à´úÂ룬Linux ƽ̨ϵĻã±à¹¤¾ßÒ²ÎüÊÕÁË C ÓïÑԵij¤´¦£¬Ê¹µÃ³ÌÐòÔ±¿ÉÒÔʹÓà #include¡¢#ifdef µÈÔ¤´¦ÀíÖ¸Á²¢Äܹ»Í¨¹ýºê¶¨ÒåÀ´¼ò»¯´úÂë¡£µÚ¶þÖÖÊÇÄÚǶµÄ»ã±à´úÂ룬ָµÄÊÇ¿ÉÒÔǶÈëµ½CÓïÑÔ³ÌÐòÖеĻã±à´úÂëƬ¶Î¡£ËäÈ» ANSI µÄ C ÓïÑÔ±ê×¼ÖÐûÓйØÓÚÄÚǶ»ã±à´úÂëµÄÏàÓ¦¹æ¶¨£¬µ«¸÷ÖÖʵ¼ÊʹÓÃµÄ C ±àÒëÆ÷¶¼×öÁËÕâ·½ÃæµÄÀ©³ä£¬ÕâÆäÖе±È»¾Í°üÀ¨ Linux ƽ̨Ï嵀 GCC¡£
¶þ¡¢Linux »ã±àÓï·¨¸ñʽ
¾ø´ó¶àÊý Linux ³ÌÐòÔ±ÒÔÇ°Ö»½Ó´¥¹ýDOS/Windows ϵĻã±àÓïÑÔ£¬ÕâЩ»ã±à´úÂ붼ÊÇ Intel ·ç¸ñµÄ¡£µ«ÔÚ Unix ºÍ Linux ϵͳÖУ¬¸ü¶à²ÉÓõĻ¹ÊÇ AT&T ¸ñʽ£¬Á½ÕßÔÚÓï·¨¸ñʽÉÏÓÐןܴóµÄ²»Í¬£º
ÔÚ AT&T »ã±à¸ñʽÖУ¬¼Ä´æÆ÷ÃûÒª¼ÓÉÏ '%' ×÷Ϊǰ׺£»¶øÔÚ Intel »ã±à¸ñʽÖУ¬¼Ä´æÆ÷Ãû²»ÐèÒª¼Óǰ׺¡£ÀýÈ磺
AT&T ¸ñʽ pushl %eax
Intel ¸ñʽ push eax
ÔÚ AT&T »ã±à¸ñʽÖУ¬Óà '$' ǰ׺±íʾһ¸öÁ¢¼´²Ù×÷Êý£»¶øÔÚ Intel »ã±à¸ñʽÖУ¬Á¢¼´ÊýµÄ±íʾ²»ÓôøÈκÎǰ׺¡£ÀýÈ磺
AT&T ¸ñʽ pushl $1
Intel ¸ñʽ push 1
AT&T ºÍ Intel ¸ñʽÖеÄÔ´²Ù×÷ÊýºÍÄ¿±ê²Ù×÷ÊýµÄλÖÃÕýºÃÏà·´¡£ÔÚ Intel »ã±à¸ñʽÖУ¬Ä¿±ê²Ù×÷ÊýÔÚÔ´²Ù×÷ÊýµÄ×ó±ß£»¶øÔÚ AT&T »ã±à¸ñʽÖУ¬Ä¿±ê²Ù×÷ÊýÔÚÔ´²Ù×÷ÊýµÄÓұߡ£ÀýÈ磺
AT&T ¸ñʽ addl $1, %eax
Intel ¸ñʽ add eax, 1
ÔÚ AT&T »ã±à¸ñʽÖУ¬²Ù×÷ÊýµÄ×Ö³¤ÓɲÙ×÷·ûµÄ×îºóÒ»¸ö×Öĸ¾ö¶¨£¬ºó׺'b'¡¢'w'¡¢'l'·Ö±ð±íʾ²Ù×÷ÊýΪ×Ö½Ú£¨byte£¬8 ±ÈÌØ£©¡¢×Ö£¨word£¬16 ±ÈÌØ£©ºÍ³¤×Ö£¨long£¬32±ÈÌØ£©£»¶øÔÚ Intel »ã±à¸ñʽÖУ¬²Ù×÷ÊýµÄ×Ö³¤ÊÇÓà "byte ptr" ºÍ "word ptr" µÈǰ׺À´±íʾµÄ¡£ÀýÈ磺
AT&T ¸ñʽ
movb val, %al
Intel ¸ñʽ mov al, byte ptr val
ÔÚ AT&T »ã±à¸ñʽÖУ¬¾ø¶ÔתÒƺ͵÷ÓÃÖ¸Ájump/call£©µÄ²Ù×÷ÊýÇ°Òª¼ÓÉÏ'*'×÷Ϊǰ׺£¬¶øÔÚ Intel ¸ñʽÖÐÔò²»ÐèÒª¡£
Ô¶³ÌתÒÆÖ¸ÁîºÍÔ¶³Ì×Óµ÷ÓÃÖ¸ÁîµÄ²Ù×÷Â룬ÔÚ AT&T »ã±à¸ñʽÖÐΪ "ljump" ºÍ "lcall"£¬¶øÔÚ Intel »ã±à¸ñʽÖÐÔòΪ "jmp far" ºÍ "call far"£¬¼´£º
AT&T ¸ñʽ ljump $section, $offset lcall $section, $offset
Intel ¸ñʽ jmp far section:offset call far section:offset
ÓëÖ®ÏàÓ¦µÄÔ¶³Ì·µ»ØÖ¸ÁîÔòΪ£º
AT&T ¸ñʽ lret $stack_adjust
Intel ¸ñʽ ret far stack_adjust
ÔÚ AT&T »ã±à¸ñʽÖУ¬ÄÚ´æ²Ù×÷ÊýµÄÑ°Ö··½Ê½ÊÇ
section:disp(base, index, scale)
¶øÔÚ Intel »ã±à¸ñʽÖУ¬ÄÚ´æ²Ù×÷ÊýµÄÑ°Ö··½Ê½Îª£º
section:[base + index*scale + disp]
ÓÉÓÚ Linux ¹¤×÷ÔÚ±£»¤Ä£Ê½Ï£¬ÓõÄÊÇ 32 λÏßÐÔµØÖ·£¬ËùÒÔÔÚ¼ÆËãµØַʱ²»Óÿ¼ÂǶλùÖ·ºÍÆ«ÒÆÁ¿£¬¶øÊDzÉÓÃÈçϵĵØÖ·¼ÆËã·½·¨£º
disp + base + index * scale
ÏÂÃæÊÇһЩÄÚ´æ²Ù×÷ÊýµÄÀý×Ó£º
AT&T ¸ñʽ Intel ¸ñʽ
movl -4(%ebp), %eax mov eax, [ebp - 4]
movl array(, %eax, 4), %eax mov eax, [eax*4 + array]
movw array(%ebx, %eax, 4), %cx mov cx, [ebx + 4*eax + array]
movb $4, %fs:(%eax) mov fs:eax, 4
Èý¡¢Hello World!
Õæ²»ÖªµÀ´òÆÆÕâ¸ö´«Í³»á´øÀ´Ê²Ã´ÑùµÄºó¹û£¬µ«¼ÈÈ»ËùÓгÌÐòÉè¼ÆÓïÑԵĵÚÒ»¸öÀý×Ó¶¼ÊÇÔÚÆÁÄ»ÉÏ´òÓ¡Ò»¸ö×Ö·û´® "Hello World!"£¬ÄÇÎÒÃÇÒ²ÒÔÕâÖÖ·½Ê½À´¿ªÊ¼½éÉÜ Linux ϵĻã±àÓïÑÔ³ÌÐòÉè¼Æ¡£
ÔÚ Linux ²Ù×÷ϵͳÖУ¬ÄãÓкܶà°ì·¨¿ÉÒÔʵÏÖÔÚÆÁÄ»ÉÏÏÔʾһ¸ö×Ö·û´®£¬µ«×î¼ò½àµÄ·½Ê½ÊÇʹÓà Linux ÄÚºËÌṩµÄϵͳµ÷Óá£Ê¹ÓÃÕâÖÖ·½·¨×î´óµÄºÃ´¦ÊÇ¿ÉÒÔÖ±½ÓºÍ²Ù×÷ϵͳµÄÄں˽øÐÐͨѶ£¬²»ÐèÒªÁ´½ÓÖîÈç libc ÕâÑùµÄº¯Êý¿â£¬Ò²²»ÐèҪʹÓà ELF ½âÊÍÆ÷£¬Òò¶ø´úÂë³ß´çСÇÒÖ´ÐÐËٶȿ졣
Linux ÊÇÒ»¸öÔËÐÐÔÚ±£»¤Ä£Ê½Ï嵀 32 λ²Ù×÷ϵͳ£¬²ÉÓà flat memory ģʽ£¬Ä¿Ç°×î³£Óõ½µÄÊÇ ELF ¸ñʽµÄ¶þ½øÖÆ´úÂë¡£Ò»¸ö ELF ¸ñʽµÄ¿ÉÖ´ÐгÌÐòͨ³£»®·ÖΪÈçϼ¸¸ö²¿·Ö£º.text¡¢.data ºÍ .bss£¬ÆäÖÐ .text ÊÇÖ»¶ÁµÄ´úÂëÇø£¬.data ÊǿɶÁ¿ÉдµÄÊý¾ÝÇø£¬¶ø .bss ÔòÊǿɶÁ¿ÉдÇÒûÓгõʼ»¯µÄÊý¾ÝÇø¡£´úÂëÇøºÍÊý¾ÝÇøÔÚ ELF ÖÐͳ³ÆΪ section£¬¸ù¾Ýʵ¼ÊÐèÒªÄã¿ÉÒÔʹÓÃÆäËü±ê×¼µÄ section£¬Ò²¿ÉÒÔÌí¼Ó×Ô¶¨Òå section£¬µ«Ò»¸ö ELF ¿ÉÖ´ÐгÌÐòÖÁÉÙÓ¦¸ÃÓÐÒ»¸ö .text ²¿·Ö¡£ÏÂÃæ¸ø³öÎÒÃǵĵÚÒ»¸ö»ã±à³ÌÐò£¬ÓõÄÊÇ AT&T »ã±àÓïÑÔ¸ñʽ£º
Àý1. AT&T ¸ñʽ
#hello.s
.data # Êý¾Ý¶ÎÉùÃ÷
msg : .string "Hello, world!\\n" # ÒªÊä³öµÄ×Ö·û´®
len = . - msg # ×Ö´®³¤¶È
.text # ´úÂë¶ÎÉùÃ÷
.global _start # Ö¸¶¨Èë¿Úº¯Êý
_start: # ÔÚÆÁÄ»ÉÏÏÔʾһ¸ö×Ö·û´®
movl $len, %edx # ²ÎÊýÈý£º×Ö·û´®³¤¶È
movl $msg, %ecx # ²ÎÊý¶þ£ºÒªÏÔʾµÄ×Ö·û´®
movl $1, %ebx # ²ÎÊýÒ»£ºÎļþÃèÊö·û(stdout)
movl $4, %eax # ϵͳµ÷ÓúÅ(sys_write)
int $0x80 # µ÷ÓÃÄں˹¦ÄÜ
# Í˳ö³ÌÐò
movl $0,%ebx # ²ÎÊýÒ»£ºÍ˳ö´úÂë
movl $1,%eax # ϵͳµ÷ÓúÅ(sys_exit)
int $0x80 # µ÷ÓÃÄں˹¦ÄÜ
³õ´Î½Ó´¥µ½ AT&T ¸ñʽµÄ»ã±à´úÂëʱ£¬ºÜ¶à³ÌÐòÔ±¶¼ÈÏΪ̫»ÞɬÄѶ®ÁË£¬Ã»ÓйØϵ£¬ÔÚ Linux ƽ̨ÉÏÄãͬÑù¿ÉÒÔʹÓà Intel ¸ñʽÀ´±àд»ã±à³ÌÐò£º
Àý2. Intel ¸ñʽ
; hello.asm
section .data ; Êý¾Ý¶ÎÉùÃ÷
msg db "Hello, world!", 0xA ; ÒªÊä³öµÄ×Ö·û´®
len equ $ - msg ; ×Ö´®³¤¶È
section .text ; ´úÂë¶ÎÉùÃ÷
global _start ; Ö¸¶¨Èë¿Úº¯Êý
_start: ; ÔÚÆÁÄ»ÉÏÏÔʾһ¸ö×Ö·û´®
mov edx, len ; ²ÎÊýÈý£º×Ö·û´®³¤¶È
mov ecx, msg ; ²ÎÊý¶þ£ºÒªÏÔʾµÄ×Ö·û´®
mov ebx, 1 ; ²ÎÊýÒ»£ºÎļþÃèÊö·û(stdout)
mov eax, 4 ; ϵͳµ÷ÓúÅ(sys_write)
int 0x80 ; µ÷ÓÃÄں˹¦ÄÜ
; Í˳ö³ÌÐò
mov ebx, 0 ; ²ÎÊýÒ»£ºÍ˳ö´úÂë
mov eax, 1 ; ϵͳµ÷ÓúÅ(sys_exit)
int 0x80 ; µ÷ÓÃÄں˹¦ÄÜ
ÉÏÃæÁ½¸ö»ã±à³ÌÐò²ÉÓõÄÓï·¨ËäÈ»ÍêÈ«²»Í¬£¬µ«¹¦ÄÜÈ´¶¼Êǵ÷Óà Linux ÄÚºËÌṩµÄ sys_write À´ÏÔʾһ¸ö×Ö·û´®£¬È»ºóÔÙµ÷Óà sys_exit Í˳ö³ÌÐò¡£ÔÚ Linux ÄÚºËÔ´Îļþ include/asm-i386/unistd.h ÖУ¬¿ÉÒÔÕÒµ½ËùÓÐϵͳµ÷ÓõĶ¨Òå¡£
ËÄ¡¢Linux »ã±à¹¤¾ß
Linux ƽ̨ϵĻã±à¹¤¾ßËäÈ»ÖÖÀàºÜ¶à£¬µ«Í¬ DOS/Windows Ò»Ñù£¬×î»ù±¾µÄÈÔÈ»ÊÇ»ã±àÆ÷¡¢Á¬½ÓÆ÷ºÍµ÷ÊÔÆ÷¡£
1.»ã±àÆ÷
»ã±àÆ÷£¨assembler£©µÄ×÷ÓÃÊǽ«Óûã±àÓïÑÔ±àдµÄÔ´³ÌÐòת»»³É¶þ½øÖÆÐÎʽµÄÄ¿±ê´úÂë¡£Linux ƽ̨µÄ±ê×¼»ã±àÆ÷ÊÇ GAS£¬ËüÊÇ GCC ËùÒÀÀµµÄºǫ́»ã±à¹¤¾ß£¬Í¨³£°üº¬ÔÚ binutils Èí¼þ°üÖС£GAS ʹÓñê×¼µÄ AT&T »ã±àÓï·¨£¬¿ÉÒÔÓÃÀ´»ã±àÓà AT&T ¸ñʽ±àдµÄ³ÌÐò£º
[xiaowp@gary code]$ as -o hello.o hello.s
Linux ƽ̨ÉÏÁíÒ»¸ö¾³£Óõ½µÄ»ã±àÆ÷ÊÇ NASM£¬ËüÌṩÁ˺ܺõĺêÖ¸ÁÄÜ£¬²¢Äܹ»Ö§³ÖÏ൱¶àµÄÄ¿±ê´úÂë¸ñʽ£¬°üÀ¨ bin¡¢a.out¡¢coff¡¢elf¡¢rdf µÈ¡£NASM ²ÉÓõÄÊÇÈ˹¤±àдµÄÓï·¨·ÖÎöÆ÷£¬Òò¶øÖ´ÐÐËÙ¶ÈÒª±È GAS ¿ìºÜ¶à£¬¸üÖØÒªµÄÊÇËüʹÓõÄÊÇ Intel »ã±àÓï·¨£¬¿ÉÒÔÓÃÀ´±àÒëÓà Intel Óï·¨¸ñʽ±àдµÄ»ã±à³ÌÐò£º
[xiaowp@gary code]$ nasm -f elf hello.asm
2.Á´½ÓÆ÷
ÓÉ»ã±àÆ÷²úÉúµÄÄ¿±ê´úÂëÊDz»ÄÜÖ±½ÓÔÚ¼ÆËã»úÉÏÔËÐеģ¬Ëü±ØÐë¾¹ýÁ´½ÓÆ÷µÄ´¦Àí²ÅÄÜÉú³É¿ÉÖ´ÐдúÂë¡£Á´½ÓÆ÷ͨ³£ÓÃÀ´½«¶à¸öÄ¿±ê´úÂëÁ¬½Ó³ÉÒ»¸ö¿ÉÖ´ÐдúÂ룬ÕâÑù¿ÉÒÔÏȽ«Õû¸ö³ÌÐò·Ö³É¼¸¸öÄ£¿éÀ´µ¥¶À¿ª·¢£¬È»ºó²Å½«ËüÃÇ×éºÏ(Á´½Ó)³ÉÒ»¸öÓ¦ÓóÌÐò¡£ Linux ʹÓà ld ×÷Ϊ±ê×¼µÄÁ´½Ó³ÌÐò£¬ËüͬÑùÒ²°üº¬ÔÚ binutils Èí¼þ°üÖС£»ã±à³ÌÐòÔڳɹ¦Í¨¹ý GAS »ò NASM µÄ±àÒë²¢Éú³ÉÄ¿±ê´úÂëºó£¬¾Í¿ÉÒÔʹÓà ld ½«ÆäÁ´½Ó³É¿ÉÖ´ÐгÌÐòÁË£º
[xiaowp@gary code]$ ld -s -o hello hello.o
3.µ÷ÊÔÆ÷
ÓÐÈË˵³ÌÐò²»ÊDZà³öÀ´¶øÊǵ÷³öÀ´µÄ£¬×ã¼ûµ÷ÊÔÔÚÈí¼þ¿ª·¢ÖеÄÖØÒª×÷Óã¬ÔÚÓûã±àÓïÑÔ±àд³ÌÐòʱÓÈÆäÈç´Ë¡£Linux ϵ÷ÊÔ»ã±à´úÂë¼È¿ÉÒÔÓà GDB¡¢DDD ÕâÀàͨÓõĵ÷ÊÔÆ÷£¬Ò²¿ÉÒÔʹÓÃרÃÅÓÃÀ´µ÷ÊÔ»ã±à´úÂëµÄ ALD(Assembly Language Debugger)¡£
´Óµ÷ÊԵĽǶÈÀ´¿´£¬Ê¹Óà GAS µÄºÃ´¦ÊÇ¿ÉÒÔÔÚÉú³ÉµÄÄ¿±ê´úÂëÖаüº¬·ûºÅ±í(symbol table)£¬ÕâÑù¾Í¿ÉÒÔʹÓà GDB ºÍ DDD À´½øÐÐÔ´Â뼶µÄµ÷ÊÔÁË¡£ÒªÔÚÉú³ÉµÄ¿ÉÖ´ÐгÌÐòÖаüº¬·ûºÅ±í£¬¿ÉÒÔ²ÉÓÃÏÂÃæµÄ·½Ê½½øÐбàÒëºÍÁ´½Ó£º
[xiaowp@gary code]$ as --gstabs -o hello.o hello.s
[xiaowp@gary code]$ ld -o hello hello.o
Ö´ÐÐ as ÃüÁîʱ´øÉϲÎÊý --gstabs ¿ÉÒÔ¸æËß»ã±àÆ÷ÔÚÉú³ÉµÄÄ¿±ê´úÂëÖмÓÉÏ·ûºÅ±í£¬Í¬Ê±ÐèҪעÒâµÄÊÇ£¬ÔÚÓà ld ÃüÁî½øÐÐÁ´½Óʱ²»Òª¼ÓÉÏ -s ²ÎÊý£¬·ñÔòÄ¿±ê´úÂëÖеķûºÅ±íÔÚÁ´½Óʱ½«±»É¾È¥¡£
ÔÚ GDB ºÍ DDD Öе÷ÊÔ»ã±à´úÂëºÍµ÷ÊÔ C ÓïÑÔ´úÂëÊÇÒ»ÑùµÄ£¬Äã¿ÉÒÔͨ¹ýÉèÖöϵãÀ´ÖжϳÌÐòµÄÔËÐУ¬²é¿´±äÁ¿ºÍ¼Ä´æÆ÷µÄµ±Ç°Öµ£¬²¢¿ÉÒÔ¶Ô´úÂë½øÐе¥²½¸ú×Ù¡£
ͼ1 Óà DDD Öе÷ÊÔ»ã±à³ÌÐò
»ã±à³ÌÐòԱͨ³£Ãæ¶ÔµÄ¶¼ÊÇһЩ±È½Ï¿Á¿ÌµÄÈíÓ²¼þ»·¾³£¬¶ÌС¾«º·µÄALD¿ÉÄܸüÄÜ·ûºÏʵ¼ÊµÄÐèÒª£¬Òò´ËÏÂÃæÖ÷Òª½éÉÜÒ»ÏÂÈçºÎÓÃALDÀ´µ÷ÊÔ»ã±à³ÌÐò¡£Ê×ÏÈÔÚÃüÁîÐз½Ê½ÏÂÖ´ÐÐaldÃüÁîÀ´Æô¶¯µ÷ÊÔÆ÷£¬¸ÃÃüÁîµÄ²ÎÊýÊǽ«Òª±»µ÷ÊԵĿÉÖ´ÐгÌÐò£º
[xiaowp@gary doc]$ ald hello
Assembly Language Debugger 0.1.3
Copyright (C) 2000-2002 Patrick Alken
hello: ELF Intel 80386 (32 bit), LSB, Executable, Version 1 (current)
Loading debugging symbols...(15 symbols loaded)
ald>
µ± ALD µÄÌáʾ·û³öÏÖÖ®ºó£¬Óà disassemble ÃüÁî¶Ô´úÂë¶Î½øÐз´»ã±à£º
ald> disassemble -s .text
Disassembling section .text (0x08048074 - 0x08048096)
08048074 BA0F000000 mov edx, 0xf
08048079 B998900408 mov ecx, 0x8049098
0804807E BB01000000 mov ebx, 0x1
08048083 B804000000 mov eax, 0x4
08048088 CD80 int 0x80
0804808A BB00000000 mov ebx, 0x0
0804808F B801000000 mov eax, 0x1
ÎÒҪͶ¸å ´òÓ¡±¾ÎÄ ¼ÓÈëÊÕ²Ø ·µ»Ø¶¥²¿ ¹Ø±Õ´°¿Ú
wodejjc ÓÚ 2012-03-18 08:45:51·¢±í:
¼ÓÓÍ£¡£¡£¡
shinelong ÓÚ 2012-03-17 16:32:22·¢±í:
¿´ÍêÁË£¬ÍüÍêÁË
honglianqxw123 ÓÚ 2012-03-17 10:24:07·¢±í:
Á˽âÁË£¬Ð»Ð»¡£
EOISFH ÓÚ 2011-12-29 17:50:25·¢±í:
ÓÐľÓеç×ÓÊéÍÛ
artiomgy ÓÚ 2005-09-09 00:33:42·¢±í:
Ö§³Ö