What exactly happened behind gcc-static hello. c?

Source: Internet
Author: User
Tags builtin gmp

What exactly happened behind gcc-static hello. c?

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 
 
  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.

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.