Common GCC options

Source: Internet
Author: User
Document directory
  • ★Preface
  • ★Demo program
  • ★General
  • ★-O compilation options
  • ★-O2 compilation options
  • ★-Fomit-frame-pointer compilation Option
  • ★-Fomit-frame-pointer &-O2
  • ★-FPIC compilation options
  • ★-Static compilation options
Influence of common GCC compilation options on code

Creation Time:
Article attributes: original
Source: http://xfocus.org/
Article submission: alert7 (sztcww_at_sina.com)

Test Environment RedHat 6.2

★Preface

This article discusses the impact of some common GCC compilation options on the code. Of course the Code has changed,
The memory layout changes, and the exploit changes accordingly.
There are too many GCC compilation options. This article checks several of the most common options.

★Demo program

[Alert7 @ redhat62 alert7] $ cat> test. c
# Include <stdio. h>
Void HI (void)
{
Printf ("hi ");
}

Int main (INT argc, char * argv [])
{
Hi ();
Return 0;
}

★General

[Alert7 @ redhat62 alert7] $ gcc-O test. c
[Alert7 @ redhat62 alert7] $ WC-C Test
11773 Test
[Alert7 @ redhat62 alert7] $ GDB-q test
(GDB) disass main
Dump of worker er code for function main:
0x80483e4 <main>: Push % EBP
0x80483e5 <main + 1>: mov % ESP, % EBP
0x80483e7 <main + 3>: Call 0x80483d0 <HI>
0x80483ec <main + 8>: XOR % eax, % eax
0x80483ee <main + 10>: JMP 0x80483f0 <main + 12>
0x80483f0 <main + 12>: Leave
0x80483f1 <main + 13>: Ret
....
End of worker er dump.
(GDB) disass hi
Dump of worker er code for function hi:
0x80483d0 <HI>: Push % EBP
0x80483d1 0x80483d3 0x80483d8 0x80483dd 0x80483e0 0x80483e1 0x80483e2 End of worker er dump.

Let's take a look at some memory images.
(Memory high address)
+ -------- +
| Bffffbc4 | Address of argv (that is, address of argv [0)
0xbffffb84 + -------- +
| 00000001 | argc Value
0xbffffb80 + -------- +
| 400309cb | return address of main
0xbffffb7c + -------- + <-- ESP before calling the main function
| Bffffb98 | EBP before calling the main function
0xbffffb78 + -------- + <-- EBP of the Main Function
| 080483ec | return address of HI ()
0xbffffb74 + -------- +
| Bffffb78 | ESP before calling HI ()
0xbffffb70 + -------- +
| 08048450 | Address of "hi"
0xbffffb6c + -------- +
| ...... |
(Low memory address)

The operation performed by the Leave command is equivalent to mov ESP, EBP, and then pop EBP.
The operation performed by the RET command is equivalent to pop EIP.

★-O compilation options

With '-O', the compiler tries to reduce code size and execution time.
When you specify '-O', the two options'-fthread-jumps' and
'-Fdefer-pop' are turned on
Optimized to reduce code size and execution time

[Alert7 @ redhat62 alert7] $ gcc-o Test test. c
[Alert7 @ redhat62 alert7] $ WC-C Test
11757 Test
[Alert7 @ redhat62 alert7] $ GDB-q test
(GDB) disass main
Dump of worker er code for function main:
0x80483d8 <main>: Push % EBP
0x80483d9 <main + 1>: mov % ESP, % EBP
0x8048db <main + 3>: Call 0x80483c8 <HI>
0x80483e0 <main + 8>: XOR % eax, % eax
0x80483e2 <main + 10>: Leave
0x80483e3 <main + 11>: Ret
0x80483e4 <main + 12>: NOP
...
End of worker er dump.
(GDB) disass hi
Dump of worker er code for function hi:
0x80483c8 <HI>: Push % EBP
0x80483c9 0x80483cb 0x80483d0 0x80483d5 0x80483d6 0x80483d7 End of worker er dump.

In Main (), a JMP command is optimized. Obviously, this command is not needed.
In hi (), add $0x4, % ESP is optimized. Will this cause an imbalance in stack?
Let's take a look at some memory images.
(Memory high address)
+ -------- +
| Bffffbc4 | Address of argv (that is, address of argv [0)
0xbffffb84 + -------- +
| 00000001 | argc Value
0xbffffb80 + -------- +
| 400309cb | return address of main
0xbffffb7c + -------- + <-- ESP before calling the main function
| Bffffb98 | EBP before calling the main function
0xbffffb78 + -------- + <-- EBP of the Main Function
| 080483e0 | return address of HI ()
0xbffffb74 + -------- +
| Bffffb78 | ESP before calling HI ()
0xbffffb70 + -------- +
| 08048440 | Address of "hi"
0xbffffb6c + -------- +
| ...... |
(Low memory address)

The operations performed by the Leave command are equivalent to mov ESP, EBP, and pop EBP.
If you see that the leave command has not been operated, first put EBP --> ESP, then pop EBP, so that even
During the process, the ESP and EBP of the stack are unbalanced, but as long as the Leave command is returned
So there is no problem in optimizing add $0x4 and % ESP.

★-O2 compilation options

-O2 optimize even more. Nearly all supported optimizations that do
Not involve a space-speed tradeoff are running med. loop unrolling
And function inlining are not done, for example. As compared to-o,
This option increases both Compilation Time and the performance
The generated code.

[Alert7 @ redhat62 alert7] $ gcc-O2-O test. c
[Alert7 @ redhat62 alert7] $ WC-C Test
11757 Test
[Alert7 @ redhat62 alert7] $ GDB-q test
(GDB) disass main
Dump of worker er code for function main:
0x80483d8 <main>: Push % EBP
0x80483d9 <main + 1>: mov % ESP, % EBP
0x8048db <main + 3>: Call 0x80483c8 <HI>
0x80483e0 <main + 8>: XOR % eax, % eax
0x80483e2 <main + 10>: Leave
0x80483e3 <main + 11>: Ret
...
0x80483ef <main + 23>: NOP
End of worker er dump.
(GDB) disass hi
Dump of worker er code for function hi:
0x80483c8 <HI>: Push % EBP
0x80483c9 0x80483cb 0x80483d0 0x80483d5 0x80483d6 0x80483d7 End of worker er dump.

Because the program is relatively simple and there is no better optimization for optimization, it is the same as that of-o.

★-Fomit-frame-pointer compilation Option

-Fomit-frame-pointer
Don't keep the frame pointer in a register for Functions
That don't need one. This avoids the instructions to save,
Set up and restore frame pointers; it also makes an extra
Register available in your functions. It also makes
Debugging impossible on most machines.

Ignore the frame pointer. In this way, you do not need to save, install, and restore EBP in the program. In this way, EBP is also
Free register is ready for use in functions.

[Alert7 @ redhat62 alert7] $ gcc-fomit-frame-pointer-O test. c
[Alert7 @ redhat62 alert7] $ WC-C Test
11773 Test
[Alert7 @ redhat62 alert7] $ GDB-q test
(GDB) disass main
Dump of worker er code for function main:
0x80483e0 <main>: Call 0x80483d0 <HI>
0x80483e5 <main + 5>: XOR % eax, % eax
0x80483e7 <main + 7>: JMP 0x80483f0 <main + 16>
0x80483e9 <main + 9>: Lea 0x0 (% ESI, 1), % ESI
0x80483f0 <main + 16>: Ret
....
End of worker er dump.
(GDB) disass hi
Dump of worker er code for function hi:
0x80483d0 <HI>: Push $0x8048450
0x80483d5 0x80483da 0x80483dd 0x80483de End of worker er dump.

The following commands are removed in main () and HI ().
Push % EBP
MoV % ESP, % EBP // these two commands are installed
Leave // restore this command
Let's take a look at some memory images.
(Memory high address)
+ -------- +
| Bffffbc4 | Address of argv (that is, address of argv [0)
0xbffffb84 + -------- +
| 00000001 | argc Value
0xbffffb80 + -------- +
| 400309cb | return address of main
0xbffffb7c + -------- +
| 080483e5 | return address of HI ()
0xbffffb78 + -------- +
| 08048450 | Address of the "hi" String
0xbffffb74 + -------- +
| ...... |
(Low memory address)
No EBP is saved for the upper-layer execution environment.

★-Fomit-frame-pointer &-O2

-The fomit-frame-pointer compilation option is removed.
Push % EBP
MoV % ESP, % EBP // these two commands are installed
Leave // restore this command
-The O2 compilation option is removed.
Add $0x4, % ESP

Will the two commands be removed together to make the stack unbalanced?

[Alert7 @ redhat62 alert7] $ gcc-fomit-frame-pointer-O2-O test. c
[Alert7 @ redhat62 alert7] $ WC-C Test
11741 Test
[Alert7 @ redhat62 alert7] $ GDB-q test
(GDB) disass main
Dump of worker er code for function main:
0x80483d8 <main>: Call 0x80483c8 <HI>
0x80483dd <main + 5>: XOR % eax, % eax
0x80483df <main + 7>: Ret
End of worker er dump.
(GDB) disass hi
Dump of worker er code for function hi:
0x80483c8 <HI>: Push $0x8048430
0x80483cd 0x80483d2 0x80483d5 0x80483d6 End of worker er dump.
Let's take a look at some memory images.
(Memory high address)
+ -------- +
| Bffffbc4 | Address of argv (that is, address of argv [0)
0xbffffb84 + -------- +
| 00000001 | argc Value
0xbffffb80 + -------- +
| 400309cb | return address of main
0xbffffb7c + -------- +
| 080483dd | return address of HI ()
0xbffffb78 + -------- +
| 08048430 | Address of the "hi" String
0xbffffb74 + -------- +
| ...... |
(Low memory address)

In this case, add $0x4, % ESP is not optimized. If it is optimized, the entire stack is
This will cause program errors.

★-FPIC compilation options

-FPIC if supported for the target machine, emit position-independent
Code, suitable for Dynamic Linking, even if branches need large
Displacements.
The generated location-independent code (PIC) is generally used to create a shared library.
On x86, the symbolic references of the PIC code are all performed through EBX.

[Alert7 @ redhat62 alert7] $ gcc-FPIC-O test. c
[Alert7 @ redhat62 alert7] $ WC-C Test
11805 Test
[Alert7 @ redhat62 alert7] $ GDB-q test
(GDB) disass main
Dump of worker er code for function main:
0x80483f8 <main>: Push % EBP
0x80483f9 <main + 1>: mov % ESP, % EBP
0x80483fb <main + 3>: Push % EBX
0x80483fc <main + 4>: Call 0x8048401 <main + 9>
0x8048401 <main + 9>: Pop % EBX // address for obtaining the instruction
0x8048402 <main + 10>: add $0x1093, % EBX // the address of the got table is stored in EBX.
0x8048408 <main + 16>: Call 0x80483d0 <HI>
0x804840d <main + 21>: XOR % eax, % eax
0x804840f <main + 23>: JMP 0x8048411 <main + 25>
0x8048411 <main + 25>: mov 0 xfffffffc (% EBP), % EBX
0x8048414 <main + 28>: Leave
0x8048415 <main + 29>: Ret
...
End of worker er dump.
(GDB) disass hi
Dump of worker er code for function hi:
0x80483d0 <HI>: Push % EBP
0x80483d1 0x80483d3 0x80483d4 0x80483d9 0x80483da 0x80483e0 0x80483e6 0x80483e8 0x80483e9 0x80483ee 0x80483f1 0x80483f4 0x80483f5 0x80483f6 End of worker er dump.
Let's take a look at some memory images.

(Memory high address)
+ -------- +
| Bffffbc4 | Address of argv (that is, address of argv [0)
0xbffffb84 + -------- +
| 00000001 | argc Value
0xbffffb80 + -------- +
| 400309cb | return address of main
0xbffffb7c + -------- + <-- ESP before calling the main function
| Bffffb98 | EBP before calling the main function
0xbffffb78 + -------- + <-- EBP of the Main Function
| 401081ec | saved EBX
0xbffffb74 + -------- +
| 0804840d | (the next instruction address that stores call 0x8048401)
0xbffffb70 + -------- +
| Bffffb78 | ESP before calling HI ()
0xbffffb6c + -------- +
| 08049494 | got table address
0xbffffb68 + -------- +
| 08048470 | (the next instruction address that stores call 0x80483d9)
0xbffffb64 + -------- +
| ...... |
(Low memory address)

★-Static compilation options

-Static
On systems that support dynamic linking, this prevents
Linking with the shared libraries. On other systems,
This option has no effect.
Compile some functions to the program statically without dynamic links.

[Alert7 @ redhat62 alert7] $ gcc-O test-static test. c
[Alert7 @ redhat62 alert7] $ WC-C Test
962808 Test
[Alert7 @ redhat62 alert7] $ GDB-q test
(GDB) disass main
Dump of worker er code for function main:
0x80481b4 <main>: Push % EBP
0x80481b5 <main + 1>: mov % ESP, % EBP
0x80481b7 <main + 3>: Call 0x80481a0 <HI>
0x80481bc <main + 8>: XOR % eax, % eax
0x80481be <main + 10>: JMP 0x80481c0 <main + 12>
0x80481c0 <main + 12>: Leave
0x80481c1 <main + 13>: Ret
...
End of worker er dump.
(GDB) disass hi
Dump of worker er code for function hi:
0x80481a0 <HI>: Push % EBP
0x80481a1 0x80481a3 0x80481a8 0x80481ad 0x80481b0 0x80481b1 0x80481b2 End of worker er dump.
[Alert7 @ redhat62 alert7] $ LDD Test
Not a dynamic executable
-The static Code has no PLT, And the GoT has all 0.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.