The Master said: “Hui is also its ordinary, repeated empty. Grant not to be ordered, and goods yan, hundreds of millions of repeatedly.” The Analects of Confucius: Advanced chapter

A hundred blog series. This is:

V54. Xx HongMeng kernel source code analysis (static linking) | complete small projects through the static linking process

Below is an executable file compiled, linked process.

This article will explain the ELF compilation, linking process, and analyze the relationship between sections and symbol tables in. O and bin files through a complete small project. Look at the process from a fresh perspective.

Loading and running related articles are:

  • V51. Xx HongMeng kernel source code analysis (ELF format) | application is not the main entrance
  • V53. Xx HongMeng kernel source code analysis (ELF) | do you want to forget her elder sister both of you are not silver
  • V54. Xx HongMeng kernel source code analysis (static linking) | complete small projects through the static linking process
  • V55. Xx HongMeng kernel source code analysis (relocation) | in line with international standards, a spokesman for the external
  • V56. Xx HongMeng kernel source code analysis (process image) | how ELF loaded running?

The preparatory work

There is a small, but complete, folder and Makefile structure as follows:

The directory structure

Root @ 5 e3abe332c5a: / home/docker test4harmony / 54 # tree. ├ ─ ─ bin │ └ ─ ─ weharmony ├ ─ ─ the include │ └ ─ ─ part. H ├ ─ ─ a Makefile ├ ─ ─ Obj │ ├ ─ ─ main. O │ └ ─ ─ part. O └ ─ ─ the SRC ├ ─ ─ main. C └ ─ ─ part. 4 c directories, 7 filesCopy the code

It feels particularly familiar to see.c. H. O:), the project is simple but representative, with global variables/functions, extern, multi-file linking, and dynamic link library printf. Use the cat command to see the contents of three files.

cat .c .h

root@5e3abe332c5a:/home/docker/test4harmony/54# cat ./src/main.c 
#include <stdio.h>
#include "part.h"
extern int g_int;
extern char *g_str;

int main(a) {
        int loc_int = 53;
        char *loc_str = "harmony os";
        printf("Main start - global g_int = %d, global g_str = %s.\n", g_int g_str); func_int(loc_int); func_str(loc_str);printf("End of main - global g_int = %d, global g_str = %s.\n", g_int g_str);return 0;
}
Copy the code
root@5e3abe332c5a:/home/docker/test4harmony/54# cat ./src/part.c 
#include <stdio.h>
#include "part.h"

int g_int = 51;
char *g_str = "hello world";

void func_int(int i) {
	int tmp = i;
	g_int = 2 * tmp ;
	printf("Func_int g_int = %d, TMP = %d.\n", g_int, TMP); }void func_str(char *str) {
        g_str = str;
        printf("func_str g_str = %s.\n", g_str); }Copy the code
root@5e3abe332c5a:/home/docker/test4harmony/54# cat ./include/part.h 
#ifndef _PART_H_
#define _PART_H_
void func_int(int i);
void func_str(char *str);
#endif
Copy the code

cat Makefile

Makefiles are written in standard form. This series of makefiles will be covered in the compile process section, but let’s focus on the simple ones first.

root@5e3abe332c5a:/home/docker/test4harmony/54# cat Makefile 
DIR_INC = ./include
DIR_SRC = ./src
DIR_OBJ = ./obj
DIR_BIN = ./bin

SRC = $(wildcard ${DIR_SRC}/*.c) OBJ = $(patSubst %.c, ${DIR_OBJ}/%.o, $(notdir ${SRC})) TARGET = weharmony BIN_TARGET = ${DIR_BIN}/${TARGET} CC = gcc CFLAGS = -g -Wall -I${DIR_INC} ${BIN_TARGET}:${OBJ} $(CC) $(OBJ) -o $@ ${DIR_OBJ}/%.o:${DIR_SRC}/%.c $(CC) $(CFLAGS) -c $< -o $@ .PHONY:clean clean: find ${DIR_OBJ} -name *.o -exec rm -rf {}Copy the code

Compile. Link. Run. Look at the results

root@5e3abe332c5a:/home/docker/test4harmony/54# make
gcc -g -Wall -I./include -c  src/part.c -o obj/part.o
gcc -g -Wall -I./include -c  src/main.c -o obj/main.o
gcc ./obj/part.o ./obj/main.o  -o bin/weharmony
root@5e3abe332c5a:/home/docker/test4harmony/54#./bin/ weHarmony main start - global g_int =51, global g_str = hello world. Func_int g_int =106, TMP =53.Func_str g_str = Harmony os. main End - global g_int =106G_str = harmony OSCopy the code

The result is simple. There’s nothing to tell.

To analyze

The preparation is complete and the real analysis begins. Because the command output content is too much, this article has been simplified to remove the interference items. If you are not clear about these commands, please check out the other articles in this series.

Readelf big S small S. /obj/main.o

Root @ 5 e3abe332c5a: / home/docker test4harmony / 54 # readelf - s. / obj/main. O There are 22 section headers,  starting at offset 0x1498: Section Headers: [Nr] Name Type Address Offset Size EntSize Flags Link Info Align [ 0] NULL 0000000000000000 00000000 0000000000000000 0000000000000000 0 0 0 [ 1] .text PROGBITS 0000000000000000 00000040 000000000000007b 0000000000000000 AX 0 0 1 [ 2] .rela.text RELA 0000000000000000 00000c80 0000000000000108 0000000000000018 I 19 1 8 [ 3] .data PROGBITS 0000000000000000 000000bb 0000000000000000 0000000000000000 WA 0 0 1 [ 4] .bss NOBITS 0000000000000000 000000bb 0000000000000000 0000000000000000 WA 0 0 1 [ 5] .rodata PROGBITS 0000000000000000 000000c0 000000000000007d 0000000000000000 A 0 0 8 ...... root@5e3abe332c5a:/home/docker/test4harmony/54# readelf -s ./obj/main.o Symbol table '.symtab' contains 22 entries: Num: Value Size Type Bind Vis Ndx Name 0: 0000000000000000 0 NOTYPE LOCAL DEFAULT UND 1: 0000000000000000 0 FILE LOCAL DEFAULT ABS main.c 2: 0000000000000000 0 SECTION LOCAL DEFAULT 1 ... 15: 0000000000000000 123 FUNC GLOBAL DEFAULT 1 main 16: 0000000000000000 0 NOTYPE GLOBAL DEFAULT UND g_str 17: 0000000000000000 0 NOTYPE GLOBAL DEFAULT UND g_int 18: 0000000000000000 0 NOTYPE GLOBAL DEFAULT UND _GLOBAL_OFFSET_TABLE_ 19: 0000000000000000 0 NOTYPE GLOBAL DEFAULT UND printf 20: 0000000000000000 0 NOTYPE GLOBAL DEFAULT UND func_int 21: 0000000000000000 0 NOTYPE GLOBAL DEFAULT UND func_strCopy the code

Interpretation of the

After compiling main.c, main.o tells the linker the following information

  • There is a file called main.c(Type=FILE)
  • There’s a function in the file called main(Type=FUNC)And this is a global function,(Bind = GLOBAL, Vis = DEFAULTGlobal means that it can be referenced by external files.
  • The rest of theg_str.printf.func_int. Are symbols that need to be provided externally and are not defined in this document(Ndx = UND, Type = NOTYPE)I don’t care how I find the symbols. The.o file stands on its own. It just tells you what I used, but I don’t know where.
  • printfandfunc_intIt is treated equally, external link symbols, no special treatment.

Readelf big S small S./obj/part.o

root@5e3abe332c5a:/home/docker/test4harmony/54# readelf -S ./obj/part.o
  [ 1] .text             PROGBITS         0000000000000000  00000040
       0000000000000078  0000000000000000  AX       0     0     1
  [ 2] .rela.text        RELA             0000000000000000  00000cf0
       00000000000000c0  0000000000000018   I      21     1     8
  [ 3] .data             PROGBITS         0000000000000000  000000b8
       0000000000000004  0000000000000000  WA       0     0     4
  [ 4] .bss              NOBITS           0000000000000000  000000bc
       0000000000000000  0000000000000000  WA       0     0     1
  [ 5] .rodata           PROGBITS         0000000000000000  000000c0
       0000000000000045  0000000000000000   A       0     0     8
  [ 6] .data.rel.local   PROGBITS         0000000000000000  00000108
       0000000000000008  0000000000000000  WA       0     0     8
       ......
root@5e3abe332c5a:/home/docker/test4harmony/54# readelf -s ./obj/part.o

Symbol table '.symtab' contains 22 entries:
   Num:    Value          Size Type    Bind   Vis      Ndx Name
     0: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  UND
     1: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS part.c
     2: 0000000000000000     0 SECTION LOCAL  DEFAULT    1
        ...
    16: 0000000000000000     4 OBJECT  GLOBAL DEFAULT    3 g_int
    17: 0000000000000000     8 OBJECT  GLOBAL DEFAULT    6 g_str
    18: 0000000000000000    52 FUNC    GLOBAL DEFAULT    1 func_int
    19: 0000000000000000     0 NOTYPE  GLOBAL DEFAULT  UND _GLOBAL_OFFSET_TABLE_
    20: 0000000000000000     0 NOTYPE  GLOBAL DEFAULT  UND printf
    21: 0000000000000034    57 FUNC    GLOBAL DEFAULT    1 func_str
Copy the code

Interpretation of the

After compiling part.c, part.o tells the linker the following information

  • There is a file called part.c(Type=FILE)
  • There are two functions in the file calledfunc_int.func_str (Type=FUNC)And are all global functions,(Bind = GLOBAL, Vis = DEFAULTGlobal means that it can be referenced by external files.
  • There are two objects in this file calledg_int.g_str (Type=OBJECT)And are global objects that can also be used externally.
  • The rest of theprintf._GLOBAL_OFFSET_TABLE_Are symbols that need to be provided externally and are not defined in this document(Ndx = UND, Type = NOTYPE)
  • And the local variable of part.ctmpIt doesn’t appear in the symbol table. Because the symbol table is equivalent to the Ministry of Foreign Affairs, only external content.
  • func_int.func_strIt’s in block 1 code.text.
  • g_intIn the three areas.dataInt g_int = 51; The value of the
      root@5e3abe332c5a:/home/docker/test4harmony/54# readelf -x 3 ./obj/part.o
      Hex dump of section '.data':
      0x00000000 33000000                            3...
    Copy the code
  • g_strIn 6 areas,.data.rel.localData area, open six to see the results
    root@5e3abe332c5a:/home/docker/test4harmony/54# readelf -x 6 ./obj/part.o Hex dump of section '.data.rel.local': NOTE: This section has relocations against it,  but these have NOT been applied to this dump. 0x00000000 00000000 00000000 ........Copy the code

    Char *g_str = “hello world”; NOTE: This section has relocations against it, but these have NOT been applied to This dump. Note: This section has been relocated for it, but it has not been applied to this dump. I finally found it in section 5, ‘.rodata’hello world

    root@5e3abe332c5a:/home/docker/test4harmony/54# readelf -x 5 ./obj/part.o Hex dump of section '.rodata': 0x00000000 68656c6c 6f20776f 726c6400 00000000 hello world..... 0x00000010 66756e63 5F696e74 20675f69 6e74203D Func_int g_int = 0x00000020 2025642C 746d7020 3d202564 2e0a0066% d, tmp = %d... f 0x00000030 756e635f 73747220 675f7374 72203d20 unc_str g_str = 0x00000040 25732e0a 00 %s..Copy the code

    How redirection is implemented is explained in detail in the redirection series.

  • You look at two symbol tables and you get three sentences
    • Who am I? Where am I
    • What can I offer others
    • I need someone to give me something to use.

Readelf big S small S. /bin/weHarmony

Weharmony is an executable that links main.o, part.o and library files.

Root @ 5 e3abe332c5a: / home/docker test4harmony / 54 # readelf - s. / bin/weharmony There are 36 section headers,  starting at offset 0x4908: Section Headers: [Nr] Name Type Address Offset Size EntSize Flags Link Info Align ...... [16] .text PROGBITS 0000000000001060 00001060 0000000000000255 0000000000000000 AX 0 0 16 [17] .fini PROGBITS 00000000000012b8 000012b8 000000000000000d 0000000000000000 AX 0 0 4 [18] .rodata PROGBITS 0000000000002000 00002000 00000000000000cd 0000000000000000 A 0 0 8 ...... [25] .data PROGBITS 0000000000004000 00003000 0000000000000020 0000000000000000 WA 0 0 8 [26] .bss NOBITS 0000000000004020 00003020 0000000000000008 0000000000000000 WA 0 0 1 root@5e3abe332c5a:/home/docker/test4harmony/54# readelf -s ./bin/weharmony Symbol table '.dynsym' contains 7 entries: Num: Value Size Type Bind Vis Ndx Name 0: 0000000000000000 0 NOTYPE LOCAL DEFAULT UND 1: 0000000000000000 0 NOTYPE WEAK DEFAULT UND _ITM_deregisterTMCloneTab 2: 0000000000000000 0 FUNC GLOBAL DEFAULT UND printf@GLIBC_2.2.5 (2) 3: 0000000000000000 0 FUNC GLOBAL DEFAULT UND __libc_start_main@GLIBC_2.2.5 (2) 4: 0000000000000000 0 NOTYPE WEAK DEFAULT UND __gmon_start__ 5: 0000000000000000 0 NOTYPE WEAK DEFAULT UND _ITM_registerTMCloneTable 6: 0000000000000000 0 FUNC WEAK DEFAULT UND __cxa_finalize@GLIBC_2.2.5 (2) Symbol table '. Symtab 'contains 75 entries: Num: Value Size Type Bind Vis Ndx Name 0: 0000000000000000 0 NOTYPE LOCAL DEFAULT UND 1: 0000000000000318 0 SECTION LOCAL DEFAULT 1 2: 0000000000000338 0 SECTION LOCAL DEFAULT 2 3: 0000000000000358 0 SECTION LOCAL DEFAULT 3 .... 33: 0000000000000000 0 FILE LOCAL DEFAULT ABS crtstuff.c 34: 0000000000001090 0 FUNC LOCAL DEFAULT 16 deregister_tm_clones 35: 00000000000010c0 0 FUNC LOCAL DEFAULT 16 register_tm_clones 36: 0000000000001100 0 FUNC LOCAL DEFAULT 16 __do_global_dtors_aux 37: 0000000000004020 1 OBJECT LOCAL DEFAULT 26 completed.8060 38: 0000000000003dc0 0 OBJECT LOCAL DEFAULT 22 __do_global_dtors_aux_fin 39: 0000000000001140 0 FUNC LOCAL DEFAULT 16 frame_dummy 40: 0000000000003db8 0 OBJECT LOCAL DEFAULT 21 __frame_dummy_init_array_ 41: 0000000000000000 0 FILE LOCAL DEFAULT ABS part.c 42: 0000000000000000 0 FILE LOCAL DEFAULT ABS main.c 43: 0000000000000000 0 FILE LOCAL DEFAULT ABS crtstuff.c 44: 000000000000225c 0 OBJECT LOCAL DEFAULT 20 __FRAME_END__ 45: 0000000000000000 0 FILE LOCAL DEFAULT ABS 46: 0000000000003dc0 0 NOTYPE LOCAL DEFAULT 21 __init_array_end 47: 0000000000003dc8 0 OBJECT LOCAL DEFAULT 23 _DYNAMIC 48: 0000000000003db8 0 NOTYPE LOCAL DEFAULT 21 __init_array_start 49: 00000000000020c0 0 NOTYPE LOCAL DEFAULT 19 __GNU_EH_FRAME_HDR 50: 0000000000003fb8 0 OBJECT LOCAL DEFAULT 24 _GLOBAL_OFFSET_TABLE_ 51: 0000000000001000 0 FUNC LOCAL DEFAULT 12 _init 52: 00000000000012b0 5 FUNC GLOBAL DEFAULT 16 __libc_csu_fini 53: 0000000000000000 0 NOTYPE WEAK DEFAULT UND _ITM_deregisterTMCloneTab 54: 0000000000004000 0 NOTYPE WEAK DEFAULT 25 data_start 55: 0000000000004020 0 NOTYPE GLOBAL DEFAULT 25 _edata 56: 00000000000012B8 0 FUNC GLOBAL HIDDEN 17 _fini 57:0000000000000000 0 FUNC GLOBAL DEFAULT UND printf @@glibc2.2.5 58: 0000000000004010 4 OBJECT GLOBAL DEFAULT 25 g_int 59: 0000000000000000 0 FUNC GLOBAL DEFAULT UND __libc_start_main@@GLIBC_ 60: 0000000000004000 0 NOTYPE GLOBAL DEFAULT 25 __data_start 61: 0000000000000000 0 NOTYPE WEAK DEFAULT UND __gmon_start__ 62: 0000000000004008 0 OBJECT GLOBAL HIDDEN 25 __dso_handle 63: 0000000000004018 8 OBJECT GLOBAL DEFAULT 25 g_str 64: 0000000000002000 4 OBJECT GLOBAL DEFAULT 18 _IO_stdin_used 65: 0000000000001240 101 FUNC GLOBAL DEFAULT 16 __libc_csu_init 66: 0000000000001149 52 FUNC GLOBAL DEFAULT 16 func_int 67: 0000000000004028 0 NOTYPE GLOBAL DEFAULT 26 _end 68: 0000000000001060 47 FUNC GLOBAL DEFAULT 16 _start 69: 000000000000117d 57 FUNC GLOBAL DEFAULT 16 func_str 70: 0000000000004020 0 NOTYPE GLOBAL DEFAULT 26 __bss_start 71: 00000000000011b6 123 FUNC GLOBAL DEFAULT 16 main 72: 0000000000004020 0 OBJECT GLOBAL HIDDEN 25 __TMC_END__ 73: 0000000000000000 0 NOTYPE WEAK DEFAULT UND _ITM_registerTMCloneTable 74: 0000000000000000 0 FUNC WEAK DEFAULT UND __CXa_Finalize @@glibc_2.2Copy the code

Interpretation of the

The linked executable weHarmony will tell the loader the following information

  • What documents are involvedType = FILE
  • What are the functions involvedType = FUNCFunc_str, func_int, _start, main
  • What are the involved objectsType = OBJECTG_int, g_str,…. It consolidated the data into district 25. Int g_int = 51; int g_int = 51; The data.
    root@5e3abe332c5a:/home/docker/test4harmony/54# readelf -x 25 ./bin/weharmony Hex dump of section '.data': 0x00004000 00000000 00000000 08400000 00000000 ......... @... 0x00004010 33000000 00000000 08200000 00000000 3........ .Copy the code

    Is it put in the same place as part.o.rodataThe data of main.c and Part. c were put here.

    root@5e3abe332c5a:/home/docker/test4harmony/54# readelf -x 18 ./bin/weharmony Hex dump of section '.rodata': 0x00002000 01000200 00000000 68656c6c 6f20776f ........ hello wo 0x00002010 726c6400 00000000 66756e63 5f696e74 rld..... Func_int 0x00002020 20675f69 6e74203D 2025642C 746d7020 g_int = %d, tmp 0x00002030 3d202564 2e0a0066 756e635f 73747220 = %d... func_str 0x00002040 675f7374 72203d20 25732e0a 00000000 g_str = %s...... 0x00002050 6861726d 6f6e7920 6f730000 00000000 harmony os...... 0x00002060 6d61696e 20e5bc80 e5a78b20 2d20e585 main ...... -.. 0x00002070 a8e5b180 20675f69 6e74203d 2025642c .... G_int = %d, 0x00002080 20E585a8 e5B18020 675f7374 72203D20...... g_str = 0x00002090 25732e0a 00000000 6d61696e 20e7bb93 %s...... main ... 0x000020a0 e69d9f20 2d20e585 a8e5b180 20675f69 ... -... G_i 0x000020B0 6e74203D 2025642c 20e585a8 e5B18020nt = %d,...... 0x000020c0 675f7374 72203d20 25732e0a 00 g_str = %s...Copy the code
  • Another thing to noteprintfChange fromType = NOTYPETurned out to beType = FUNCThat tells subsequent dynamic chaining that this is a function
        57: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND printf@@GLIBC_2.2.5
    Copy the code

    But the content is stillNdx=UNDWeharmony doesn’t provide it either, the content needs to be provided by the runtime environment. There is also a list of content that the runtime environment must provide in order for WeHarmony to truly run.

    Symbol table '.dynsym' contains 7 entries:
     Num:    Value          Size Type    Bind   Vis      Ndx Name
       0: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  UND
       1: 0000000000000000     0 NOTYPE  WEAK   DEFAULT  UND _ITM_deregisterTMCloneTab        
       2: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND printf@GLIBC_2.2.5 (2)
       3: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND __libc_start_main@GLIBC_2.2.5 (2)
       4: 0000000000000000     0 NOTYPE  WEAK   DEFAULT  UND __gmon_start__
       5: 0000000000000000     0 NOTYPE  WEAK   DEFAULT  UND _ITM_registerTMCloneTable        
       6: 0000000000000000     0 FUNC    WEAK   DEFAULT  UND __cxa_finalize@GLIBC_2.2.5 (2)  
    Copy the code

    This example will not normally run in a Windows environment. Unless a corresponding runtime environment is provided.

Intensive reading of the kernel source code

Four code stores synchronous annotation kernel source code, >> view the Gitee repository

Analysis of 100 blogs. Dig deep into the core

Add comments to hongmeng kernel source code process, sort out the following article. Content based on the source code, often in life scene analogy as much as possible into the kernel knowledge of a scene, with a pictorial sense, easy to understand memory. It’s important to speak in a way that others can understand! The 100 blogs are by no means a bunch of ridiculously difficult concepts being put forward by Baidu. That’s not interesting. More hope to make the kernel become lifelike, feel more intimate. It’s hard, it’s hard, but there’s no turning back. 😛 and code bugs need to be constantly debug, there will be many mistakes and omissions in the article and annotation content, please forgive, but will be repeatedly amended, continuous update. Xx represents the number of modifications, refined, concise and comprehensive, and strive to create high-quality content.

Compile build The fundamental tools Loading operation Process management
Compile environment

The build process

Environment script

Build tools

Designed.the gn application

Ninja ninja

Two-way linked list

Bitmap management

In the stack way

The timer

Atomic operation

Time management

The ELF format

The ELF parsing

Static link

relocation

Process image

Process management

Process concept

Fork

Special process

Process recycling

Signal production

Signal consumption

Shell editor

Shell parsing

Process of communication Memory management Ins and outs Task management
spinlocks

The mutex

Process of communication

A semaphore

Incident control

The message queue

Memory allocation

Memory management

Memory assembly

The memory mapping

Rules of memory

Physical memory

Total directory

Scheduling the story

Main memory slave

The source code comments

Source structure

Static site

The clock task

Task scheduling

Task management

The scheduling queue

Scheduling mechanism

Thread concept

Concurrent parallel

The system calls

Task switching

The file system Hardware architecture
File concept

The file system

The index node

Mount the directory

Root file system

Character device

VFS

File handle

Pipeline file

Compilation basis

Assembly and the cords

Working mode

register

Anomaly over

Assembly summary

Interrupt switch

Interrupt concept

Interrupt management

HongMeng station | into a little bit every day, the original is not easy, welcome to reprint, please indicate the source.