2019-2020-1 20199314 "Linux kernel principle and Analysis" in the eighth week of work

Executable program works,

1, ELF (Executable and Linkable Format) executable and linkable files, which contains the following three categories:

  1. Relocatable files (Relocatable File): holds code and data suitable for other object files together to create an executable file, static library file or a shared object file (.o files mainly)
  2. Executable file (Executable File): holds a program for execution by a plurality of generally binding relocatable file generation is completed all of the work relocation and symbol resolution files (except parsed run-time shared library symbols) .
  3. Shared object files (Shared Object File): holds code and data suitable for the two links link. The first is the link editor (static link), you can create other object and other relocatable and shared object files. The second is the dynamic linker, a joint executable file and other shared object files to create a process image.

2, ELF file format:

ELF file are various body section, and descriptions of these attributes section (Program header table and Section header table), and the integrity of the information ELF file (ELF header), as shown below.

3, compiled

Step program from source code into executable: preprocessing, compilation, assembly, adapter - A Case Study hello.c.

预处理: gcc -E hello.c -o hello.i  -m32
编译:gcc -S hello.i  -o hello.s -m32
汇编:gcc -c hello.s -o hello.o -m32
默认衔接(动态库):gcc hello.o -o hello -m32 
衔接静态库:gcc hello.o -o hello.static -m32 -static

The resulting hello and hello-static file is an executable file. The contents of the executable file compiled machine include instruction code, further comprising a data link that need some of the information, such as the symbol table, debugging information, string and the like.
Its file format, as shown below.

4, the static convergence and dynamic convergence

Static Link: copy directly linked at compile the code required to execute the final executable file, the advantage of fast loading speed code, execution speed is faster, low dependence on the external environment. The disadvantage is that if multiple applications use the same library function, can be loaded multiple times, wasting memory.
Dynamic Link: do not directly copy the executable code at compile time, but by recording a series of symbols and parameters, when the program is run or pass this information to load the operating system. The operating system is responsible for the dynamic libraries required to load into memory when the program is run to the specified code to perform dynamic shared libraries already loaded in memory to execute code, and ultimately achieve the purpose of the link is running. Advantage is that multiple programs can share the same piece of code, without the need to store a plurality of replicated on the disk. The disadvantage is loaded at run time may affect the performance of the early implementation of the program, but also for the use of library dependencies higher. (Dynamic linking and run-time dynamic linking into the loading)

Use gcc hello.o -o hello.static -static static linking, the resulting executable program file found up to 100 times the size of the dynamic link, as shown in FIG.

5, dynamic connection

Are two types of dynamic convergence;

  1. Dynamically loading executable engagement
    dynamic connection 2. runtime.

Dynamic convergence means that when loading it calls the library needs to provide at the outset when a program started, and the dynamic convergence program runs only to the relevant statements will visit dllibexample runtime.

6, fork and execve differences and relations

For the fork ():
1, the parent process child process to copy all of its memory to process memory address space. The parent, the child process
"data segment", "stack segment" and "code block" is identical, i.e., the child process each byte
and parent process.
2, the current working directory of the child process, umask mask value and the same as the parent process, fork () before the parent process
open file descriptors, also open in the child process, and point to the same file entries.
3, the child process has its own process ID.

For exec ():
1, the process calls exec (), will use a new program to replace the call in the same block process memory
exec () of the process, the new program replaces the current process image "segment" of the current process,
"stack segment" and "snippet" back new program rewritten.
2, the new ID program will remain unchanged calls exec () process.
3, open call exec word to describe the previously open () continues to open (like what parameters can be made to open
the word to describe the new program is closed)

Experiment 1:

Programming using the exec * library function to load an executable file, dynamically linked into both use dynamic linking and run-time dynamic linking when the executable is loaded, the dynamic link library programming exercises.

1, download the following experiment upstairs from two source said on a Web site:

shlibexample.h (1.3 KB) - Interface of Shared Lib Example
shlibexample.c (1.2 KB) - Implement of Shared Lib Example
but later found that NetEase cloud pay class accessory, for two codes do not need to spend so much money, say has been the source of the book, you can write your own again, but the code book has a pit, which directly affect the progress behind me doing the experiment, I'll explain it later.

shlibexample.h
#ifndef  _SH_LTB_EXAMPLE_H_
#define  _SH_LTB_EXAMPLE_H_
#define SUCCESS 0
#define FAILURE (-1)
#ifdef __cplusplus
extern "C" {
#endif
int SharedLibApi();
#ifdef __cplusplus
}
#endif
#endif

dllibexample.h
#ifndef  _DL_LTB_EXAMPLE_H_
#define  _DL_LTB_EXAMPLE_H_
#ifdef _cplusplus
extern "C"{
#endif
int DynamicalLoadingLibApi();
#ifdef _cplusplus
}
#endif
#endif

shlibexample.c
#include <stdio.h>
#include "shlibexample.h"
int SharedLibApi()
{
printf("This is a shared libary!\n");
return SUCCESS;
}

dllibexample.c
#include <stdio.h>
#include "dllibexample.h"
#define SUCCESS 0
#define FAILURE(-1)
int DynamicalLoadingLibApi()
{
printf(“This is a Dynamical Loading library”!\n”);
return SUCCESS;
}

2, compiled files libshlibexample.so

$ gcc -shared shlibexample.c -o libshlibexample.so -m32
$ gcc -shared dllibexample.c -o libdllibexample.so -m32

Error to show up here, and why I will complain in the online search a lot of answers, what needs to be updated gcc ah, what the C header file name conflicts ah, anyway, all kinds of answers, spent a long time is still being given, so I re hit the code several times, until the thief found a small place, the book is easy to mislead.

Note Look! This is clearly saw that it was LIB right, I knocked at the vim editor and they also like the book about the same, but I carefully looked at the cross and I was a little under the cross of varying lengths, this stock is not the T bar! ! The results validate the fact that my judgment, this stock is T, LTB. Finally found a reason

3, mian function respectively as to shared libraries and dynamically loaded shared libraries using libshlibexample.so files and file libdllibexample.so

main.c
#include <stdio.h>
#include "shlibexample.h" 
#include <dlfcn.h>

int main()
{
   printf("This is a Main program!\n");
/* Use Shared Lib */
  printf("Calling SharedLibApi() function of libshlibexample.so!\n");
 SharedLibApi(); //直接调用共享库
/* Use Dynamical Loading Lib */
  void * handle = dlopen("libdllibexample.so",RTLD_NOW);//打开动态库并将其加载到内存
  if(handle == NULL)
{
    printf("Open Lib libdllibexample.so Error:%s\n",dlerror());
    return   FAILURE;
}
int (*func)(void);
char * error;
func = dlsym(handle,"DynamicalLoadingLibApi");
if((error = dlerror()) != NULL)
{
    printf("DynamicalLoadingLibApi not found:%s\n",error);
    return   FAILURE;
}    
printf("Calling DynamicalLoadingLibApi() function of libdllibexample.so!\n");
func();  
dlclose(handle); //卸载库  
return SUCCESS;
}

4, dynamic convergence test run

Because shilibexample when you need to provide a path of convergence, the corresponding header files shilibexample.h also need to be able to find a place in the compiler. -L parameter indicates that the file path, -l library file name represents.
dllibexample program runs only to the relevant statements will visit, without any relevant information at compile time, using shared libraries dlopen -ldl indicate they need, and modify LD_LIBRARY_PATH ensure dllibexample.so can be found.

gcc main.c -o main -L./ -l shlibexample -ldl -m32
export LD_LIBRARY_PATH=$PWD
./main

Experiment 2:

Analysis of a execve kernel system call handler sys_execve, verify understand you load an executable program on the Linux system required to track the process of using gdb.

The second experiment can only be done in a laboratory building environment, after all, not with a good local virtual machine environment.

1, first download the file containing test_exec.c files from a folder on github, to be compiled.

ls
cd ~/LinuxKernel
rm menu -rf
git clone https://github.com/mengning/menu.git
cd menu 
mv test_exec.c test.c
make rootfs

2, using the help and exec command to see if the compilation is successful in qemu interface.

3, and then restart the qemu.

4, load the kernel and port.

5, three breakpoints.

6, debugging and running programs.

Guess you like

Origin www.cnblogs.com/morvalhe/p/11815584.html