簡體   English   中英

如何判斷在Linux上是否使用幀指針編譯二進制文件?

[英]How can I tell whether a binary is compiled with frame pointers or not on Linux?

我在Linux中有一個二進制文件/庫。 如何確定使用幀指針編譯它?

贊/黑翼:

使用/不使用framepointer優化編譯一些簡單的東西並在反匯編輸出上使用diff -u提供了一些線索:

$ diff -u with*
--- with-fp 2011-03-23 09:49:29.366277002 +0000
+++ without-fp  2011-03-23 09:49:35.046277002 +0000
@@ -5,14 +5,12 @@
 Disassembly of section .text:

 00000000 <func>:
-   0:  55                      push   %ebp
+   0:  53                      push   %ebx
    1:  31 c0                   xor    %eax,%eax
-   3:  89 e5                   mov    %esp,%ebp
-   5:  53                      push   %ebx
-   6:  81 ec 00 04 00 00       sub    $0x400,%esp
-   c:  8b 4d 08                mov    0x8(%ebp),%ecx
-   f:  8d 9d fc fb ff ff       lea    -0x404(%ebp),%ebx
-  15:  8d 76 00                lea    0x0(%esi),%esi
+   3:  81 ec 00 04 00 00       sub    $0x400,%esp
+   9:  8b 8c 24 08 04 00 00    mov    0x408(%esp),%ecx
+  10:  89 e3                   mov    %esp,%ebx
+  12:  8d b6 00 00 00 00       lea    0x0(%esi),%esi
   18:  8b 14 81                mov    (%ecx,%eax,4),%edx
   1b:  89 14 83                mov    %edx,(%ebx,%eax,4)
   1e:  83 c0 01                add    $0x1,%eax
@@ -28,5 +26,4 @@
   3e:  75 f0                   jne    30 <func+0x30>
   40:  81 c4 00 04 00 00       add    $0x400,%esp
   46:  5b                      pop    %ebx
-  47:  5d                      pop    %ebp
-  48:  c3                      ret    
+  47:  c3                      ret    

您會看到多種更改:

  1. 與framepointers代碼總是包含的兩個指令 push %ebp mov %esp, %ebp
    無幀無代碼可能 (在所示的情況下,因為它沒有使用%ebp寄存器來執行任何操作)具有push %ebp具有mov %esp, %ebp one,因為沒有必要初始化framepointer 。
  2. 帶有framepointers的代碼訪問堆棧相對於framepointer的參數 ,如所示情況下的mov 0x8(%ebp), %ecx
    無幀代碼的代碼相對於堆棧指針是這樣做的,附加偏移量是函數堆棧幀的大小,如mov 0x408(%esp), %ecx
    對於局部變量也是如此,在顯示的代碼中, lea -0x404(%ebp), %ebx用於幀指針使用代碼與mov %esp, %ebx (可能是lea 0x0(%esp), %ebx )代碼沒有framepointers。
  3. 兩者之間的寄存器分配可能會有一些變化,特別是如果代碼變得足夠復雜以使用 %ebp寄存器作為局部變量(顯示的樣本沒有顯示)

編譯器優化級別對生成的代碼實際上看起來有多大影響但是這些特定的項目( mov %esp, %ebp和使用%ebp -relative尋址用於參數/局部變量) 只能在使用的代碼中找到framepointers,如果用-fomit-frame-pointer編譯則丟失。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM