What exactly happened behind gcc-static hello. C?

Source: Internet
Author: User
Tags builtin gmp posix

You may often use gcc-static ***. C. What commands and libraries are used for this static link?

First, let's analyze what libc. A is. We already know that XXX. So is a dynamic link library, XXX. O is a static link library or relocated file, And/bin/bash is an executable file.

Libc. A is actually a collection of many relocated files, and generally only one function is written in each relocated file. For example, printf. O only has one printf function to link less useless libraries during static connections.

 

First use the command locate libc. a, we will find libc. A is located in/usr/lib/x86_64-linux-gnu/libc. a, and then use the command ar-T libc. A to check the number of relocable files. You can give it a try. There are a lot of relocated files in it, and one screen won't be displayed. This includes printf. O.


Run the ar-x libc. a command to decompress the file and find the printf. o file. Run the NM printf. O command. The following result is displayed:


Once again, it is confirmed that printf. O only has one printf function. The purpose is to link less useless libraries during static connections.


Below we attach the source code hello. C:

#include <stdio.h>int main(){printf("HelloWorld\n");return 0;}

Run the GCC-static -- verbose-fno-builtin hello. C, -- verbose command to enter the compilation details.-fno-builtin prevents the compiler from converting printf to puts. The result is as follows:

Using built-in specs.
Collect_gcc = gcc
Collect_lto_wrapper =/usr/lib/GCC/x86_64-linux-gnu/4.6/Lto-wrapper
Target: x86_64-linux-gnu
Configured :.. /src/configure-V -- With-pkgversion = 'ubuntu/linaro 4.6.3-1ubuntu5' -- With-bugurl = file: // usr/share/doc/gcc-4.6/readme. bugs -- enable-languages ages = C, C ++, Fortran, objc, OBJ-C ++ -- prefix =/usr -- Program-suffix =-4.6 -- enable-shared -- enable-linker-Build-id -- With-system-zlib -- libexecdir =/usr /lib -- without-included-gettext -- enable-threads = POSIX -- With-gxx-include-Dir =/usr/include/C ++/4.6 -- libdir =/usr/lib -- enable-NLS -- With-sysroot =/-- enable-clocale = GNU -- enable-libstdcxx-Debug -- enable-libstdcxx-time = yes -- enable-GNU-unique-object -- enable -plugin -- enable-objc-GC -- disable-werror -- With-arch-32 = i686 -- With-tune = generic -- enable-checking = release -- Build = x86_64-linux-gnu -- Host = x86_64-linux-gnu -- target = x86_64-linux-gnu
Thread model: POSIX
GCC version 4.6.3 (Ubuntu/linaro 4.6.3-1ubuntu5)
Collect_gcc_options = '-static ''-v''-fno-builtin''-mtune = generic ''-March = x86-64'
/Usr/lib/GCC/x86_64-linux-gnu/4.6/PC3-quiet-v-imultilib. -imultiarch x86_64-linux-gnu hello. c-quiet-dumpbase hello. c-mtune = generic-March = x86-64-auxbase hello-version-fno-builtin-fstack-protector-o/tmp/ccrsgv2j. s
Gnu c (Ubuntu/linaro 4.6.3-1ubuntu5) version 4.6.3 (x86_64-linux-gnu)
Compiled by gnu c version 4.6.3, GMP version 5.0.2, mpfr version 3.1.0-P3, MCM version 0.9
GGC Heuristics: -- Param GGC-Min-expand = 100 -- Param GGC-Min-heapsize = 131072
Ignoring nonexistent directory "/usr/local/include/x86_64-linux-gnu"
Ignoring nonexistent directory "/usr/lib/GCC/x86_64-linux-gnu/4.6/.../x86_64-linux-gnu/include"
# Include "..." search starts here:
# Include <...> search starts here:
/Usr/lib/GCC/x86_64-linux-gnu/4.6/include
/Usr/local/include
/Usr/lib/GCC/x86_64-linux-gnu/4.6/include-fixed
/Usr/include/x86_64-linux-gnu
/Usr/include
End of search list.
Gnu c (Ubuntu/linaro 4.6.3-1ubuntu5) version 4.6.3 (x86_64-linux-gnu)
Compiled by gnu c version 4.6.3, GMP version 5.0.2, mpfr version 3.1.0-P3, MCM version 0.9
GGC Heuristics: -- Param GGC-Min-expand = 100 -- Param GGC-Min-heapsize = 131072
Compiler executable checksum: 75e879ed14f91af504f4150eadeaa0e6
Collect_gcc_options = '-static ''-v''-fno-builtin''-mtune = generic ''-March = x86-64'
As -- 64-O/tmp/cc5x9ffe. O/tmp/ccrsgv2j. s
Compiler_path =/usr/lib/GCC/x86_64-linux-gnu/4.6/:/usr/lib/GCC/x86_64-linux-gnu/4.6/:/usr/lib/GCC/x86_64-linux-gnu /: /usr/lib/GCC/x86_64-linux-gnu/4.6/:/usr/lib/GCC/x86_64-linux-gnu/
LIBRARY_PATH =/usr/lib/GCC/x86_64-linux-gnu/4.6/:/usr/lib/GCC/x86_64-linux-gnu/4.6 /.. /.. /.. /x86_64-linux-gnu/:/usr/lib/GCC/x86_64-linux-gnu/4.6 /.. /.. /.. /.. /lib/:/lib/x86_64-linux-gnu //:/lib /.. /lib/:/usr/lib/x86_64-linux-gnu/:/usr/lib /.. /lib/:/usr/lib/GCC/x86_64-linux-gnu/4.6 /.. /.. /.. /:/lib/:/usr/lib/
Collect_gcc_options = '-static ''-v''-fno-builtin''-mtune = generic ''-March = x86-64'
/Usr/lib/GCC/x86_64-linux-gnu/4.6/collect2 -- sysroot =/-- Build-id -- no-add-needed -- as-needed-M elf_x86_64 -- hash-style = GNU- static-Z relro/usr/lib/GCC/x86_64-linux-gnu/4.6 /.. /.. /.. /x86_64-linux-gnu/crt1.o/usr/lib/GCC/x86_64-linux-gnu/4.6 /.. /.. /.. /x86_64-linux-gnu/crti. o/usr/lib/GCC/x86_64-linux-gnu/4.6/crtbegint. o-L/usr/lib/GCC/x86_64-linux-gnu/4.6-L/usr/lib/GCC/x86_64-linux-gnu/4.6 /.. /.. /.. /x86_64-linux-gnu-L/usr/lib/GCC/x86_64-linux-gnu/4.6 /.. /.. /.. /.. /lib-L/lib/x86_64-linux-gnu-L/lib /.. /lib-L/usr/lib/x86_64-linux-gnu-L/usr/lib /.. /lib-L/usr/lib/GCC/x86_64-linux-gnu/4.6 /.. /.. /.. /tmp/cc5x9ffe. O -- start-group-lgcc-lgcc_eh-LC -- end-group/usr/lib/GCC/x86_64-linux-gnu/4.6/crtend. o/usr/lib/GCC/x86_64-linux-gnu/4.6 /.. /.. /.. /x86_64-linux-gnu/crtn. O


The red text above

Step 1: compile and use command to compile hello. c into/tmp/ccrsgv2j. S.

Step 2: Compile and run the as command to generate/tmp/cc5x9ffe. O from/tmp/ccrsgv2j. S.

Step 3: Link to the static Link Library of/tmp/cc5x9ffe. O with the command collect2 (which can be regarded as an upgraded LD.



So far, the story behind gcc-static hello. C has been analyzed. This article will refer to the self-cultivation of programmers.

What exactly happened behind gcc-static hello. C?

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.