Linux kernel seventh section 20135332 Seibu Yao

Source: Internet
Author: User
Tags function prototype

preprocessing, compiling, linking, and formatting of the target file how did the program get out of it?

In C, for example, C code is pre-processed by the compiler, compiled into assembly code, compiled by the assembler into the target code, and then linked to the executable file, loaded by the operating system into the CPU to execute.
()

.c----.asm(.s)-----.o(二进制文件)-----A.out(可执行文件)

Preprocessing is responsible for the inclusion of the file included in the macro replacement and other work
The executable file is used by the shared library.

Static compilation
-static

Put everything in the library inside, so it's bigger than that.
What is the inside of an executable file?

Format of the destination file elf

()
File format:

    • PE is under Windows
    • Elf is under Linux (executable linkable format)

Target file format and ABI application binary interface adapted to a certain CPU format

Elf standard three types of executables:
    • A relocatable file (code and data)
    • An executable file (the program used to execute)
    • A shared file (code and data, connection compiler and dynamic connector)

      Obeject file Join program (create a program) and program execution (run a program)

      ()

      View the head of the elf file

      Gccs Readelf-h Main

      The main tasks that can be loaded by the executable program:

      Map the addresses of the various data in the code to the process space. For example, in a code snippet in a file, when it is copied to the process space, and the data segment is copied to the process space.

      Statically linked elf executable file and process address space

      File map Process address space
      0xc000000 Start is a user-state can be used
      0x8048000 The default load address
      0x8048x00 The actual entry of the program, the first line of code that the executable file loads into memory to begin execution

    • A general static link places all the code in a single code snippet

    • A dynamically linked process will have multiple sections of code

      executable programs, shared libraries, and dynamic links that work prior to loading executable programs

      Execution environment for executable programs

    • command-line arguments and the shell environment

      • Ls-l/usr/bin lists directory information under/usr/bin//ls is an executable program
      • The shell itself does not limit the number of command-line arguments, and the number of command-line arguments is limited by the command itself//the main function
      • For example, int main (int argc,char argv[]);//willing to accept command line arguments, user input
        -also for example, int main (int argc,char
        Argv[],charenvp[])//Envp:shell environment variable
      • The shell calls Execve to pass command-line arguments to the main () function of the executable program
      • int execve (int char filename,char argv[],char *const envp[]);//function prototype, first function call parameter pass, then system call parameter Pass

        EXECVE Package Tutorial

        (Not available)

        How are command-line arguments and environment variables saved and passed?

        (not available) command-line arguments and environment variables, fork a child process to fully replicate the parent process, and then call EXECV, the current executable program to overwrite the original environment (subprocess), the child process of the user state stack is also emptied, because the new process to execute.

        So how do the command line arguments and environment variables go into the stack of the new program?
    • Command-line arguments (char argv[]), environment variables (char const envp[]) Press stack
    • Creates a new user-state stack, passing the command-line arguments, the environment variables, to the system call Execve's kernel handler function
    • The kernel handler creates a new executable program user-state stack, and copies command-line arguments and environment variables in
      The shell program--execve--the corresponding system call sys_execv--and then copies it at initialization

      Examples of applications for dynamic linking and runtime dynamic linking at load time
    • Dynamically linked when the executable program is loaded
    • Executable program Runtime dynamic linking
      # # # Executable when loading, run dynamic link
      1. Prepare the. So file
        Shared library files Shlibexample.h and shlibexample.c compiled as. so files
        (Code display for shared libraries)
      gcc-shared Shlibexample.c-o Libshlibexample.so-m32
      1. Use libshlibexample.so files and libdllibexample.so files in shared libraries and dynamically loaded shared libraries, respectively
        (Show main function)
        Compile main note that this provides only shlibexample-L (the directory where the library corresponds to the interface file) and the-L (library name) do not provide dllibexample information
    • GCC Mian.c-o main-l/path/to/your/dir-lshlibexample-ldl-m32
      Specify the path to the header file and the path to the library file, respectively, to dynamically load

    • Export Ld_library_path= $PWD

      Add the current directory to the default path

    • ./main

Linux kernel seventh section 20135332 Seibu Yao

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.