Experimental detection of link action during compilation

Source: Internet
Author: User

/*
Name: Experimental detection of link action during compilation
Description: Has not been very familiar with the compilation process in the end of the link is what exactly (its specific role is what), only shallow to understand that this stage is to connect the various target files together, as to why to connect, how to connect, not very clear.
Recently, when reviewing the operating system, it was learned that one of the functions of the link stage is to convert the logical address in each target file into a unified logical address of the final executable file.
In other words, each source file generates a separately generated target file whose address (logical address) starts with 0x0, and the logical address of the resulting executable is also starting with 0x0, so that there are not many instructions to start with 0x0. The link process is to solve such a problem, at this stage, all the target file logical address is unified addressing,
The address of the original target file's instruction is rescheduled in the executable file.
This can be seen in the following disassembly program.

00000000004004ed <test_a>:  //(test_a: Address starting in final executable)
00000000004004FB <test_b>:  //(Test_ B: The address started in the final executable file)
0000000000400516 <main>:    //(main: The address that begins in the final executable file) is

also through the disassembler, You can also see that each executable file does have a difference (under Windows and under different operating systems, such as Linux). There are some initialization sections at the beginning of each executable file, and so on.

*/

//the code in file test_a: void Test_a () {int a = 10;
return; }//Disassembly of file TEST_A.O test_a.o:file format elf64-x86-64 disassembly of section. text:0000000000000000 <test_a> ;: 0:55 push%rbp 1:48 e5 mov%rsp,%rbp 4:c7 FC 0a 00 00                      MOVL $0xa,-0x4 (%RBP) b:90 nop c:5d pop%rbp d:c3
RETQ Code Description: Although the specific assembly instructions do not understand what the meaning, but the first line is the logical address of the first instruction. The meaning of each column is probably, (address, machine code corresponding to the instruction, the last column is the assembly code) 
//the code in file test_a: void Test_b () {int b = 5;
return; }//Disassembly of file TEST_B.O test_b.o:file format elf64-x86-64 disassembly of section. text:0000000000000000 <TEST_B&G  t;: 0:55 push%rbp 1:48 e5 mov%rsp,%rbp 4:c7 $ fc 05 00 00                      MOVL $0x5,-0x4 (%RBP) b:90 nop c:5d pop%rbp d:c3
RETQ Code Description: You can see that the first instruction also starts with the address 0x00. 
The code in main of the file: #include "test.h" void Test_c () {int c = 30;
    } int main () {int m = 20;
    Test_a ();

    Test_b ();
return 0; 
   }//Disassembly of file MAIN.O main.o:file format elf64-x86-64 disassembly of section. text:0000000000000000 <test_c>:     0:55 push%rbp 1:48 e5 mov%rsp,%rbp 4:c7 (FC 1e 00 00 00 MOVL $0x1e,-0x4 (%RBP) b:5d pop%rbp c:c3 retq 000000000   000000d <main>: d:55 push%rbp e:48 e5 mov%rsp,%rbp 11:          $0X10,%RSP EC Ten Sub 15:c7 FC Movl $0x14,-0x4 (%RBP) 1c:b8 00 00 00 00    mov $0x0,%eax 21:e8 xx xx callq <main+0x19> 26:b8 xx xx
  $0x0,%eax 2b:e8 xx xx callq <main+0x23> 30:b8 xx xx xx $0x0,%eax 35:   C9 Leaveq 36:c3 retq Code Description: The first instruction of the main function also starts with 0x00 (the first instruction here is not the main function but T
 Est_c related content)
The last link to complete the disassembly of the executable file Main:file format elf64-x86-64 disassembly of section. Init:00000000004003a8 <_init>: 4 003a8:48, EC, $0X8,%RSP 4003ac:48 8b 0c, mov 0x200c45 (%rip),%rax #     600FF8 <_DYNAMIC+0x1d0> 4003b3:48 c0 test%rax,%rax 4003b6:74 4003BD <_init+0x15> 4003b8:e8 callq 4003f0 <__gmon_start__@plt> 4003bd:48 C4 Add $0x8,%rsp 4003c1:c3 retq disassembly of section. plt:000000000 04003d0 <__libc_start_main@plt-0x10&gt: 4003d0:ff 0c pushq 0x200c32 (%rip) # 601008 < _global_offset_table_+0x8> 4003d6:ff 0c jmpq *0x200c34 (%rip) # 601010 &LT;_GLOBAL_OFFSE
  t_table_+0x10> 4003dc:0f 1f nopl 0x0 (%rax) 00000000004003e0 <__libc_start_main@plt>: 4003E0:FF 0c 20 XX jmpq *0x200c32 (%rip) # 601018 <_GLOBAL_OFFSET_TABLE_+0x18> 4003e6:68 xx xx Shq $0x0 4003eb:e9 e0 FF FF FF JMPQ 4003d0 <_init+0x28> 00000000004003f0 <__gmon_start__@plt&gt    ;: 4003f0:ff 2a 0c jmpq *0x200c2a (%rip) # 601020 <_GLOBAL_OFFSET_TABLE_+0x20> 4003f6: Pushq $0x1 4003fb:e9 d0 FF FF JMPQ 4003d0 <_init+0x28> disassembly o F section. text:0000000000400400 <_start>: 400400:31 Ed xor%EBP,%EBP 400,402:49 89    D1 mov%rdx,%r9 400405:5e pop%rsi 400406:48 vid E2 mov   %rsp,%rdx 400409:48 e4 f0 and $0XFFFFFFFFFFFFFFF0,%RSP 40040d:50 push   %rax 40040e:54 push%rsp 40040f:49 C7 c0 b0 400416 for mov $0x4005b0,%r8:  C7 C1 40 05 40 00  MOV $0X400540,%RCX 40041d:48 C7 C7 (+) MOV $0x400516,%rdi 400424:e8 b7 FF FF FF call Q 4003e0 <__libc_start_main@plt> 400429:f4 hlt 40042a:66 0f 1f PW 0x0 (%rax,%rax,1) 0000000000400430 <deregister_tm_clones>: 400430:b8 3f mov $0x60103f   ,%eax 400435:55 push%RBP 400436:48 2d, sub $0x601038,%rax 40043c:                   F8 0e cmp $0xe,%rax 400440:48 e5 mov%rsp,%rbp 400,443:77 02                      JA 400447 <deregister_tm_clones+0x17> 400445:5d pop%rbp 400446:c3   Retq 400447:b8 xx xx $0x0,%eax 40044c:48 c0 test                      %rax,%rax 40044f:74 f4 JE 400445 <deregister_tm_clones+0x15> 400451:5d
  Pop%RBP400452:BF, mov $0x601038,%edi 400457:ff e0 jmpq *%rax 400459:0f 1f    NOPL 0x0 (%rax) 0000000000400460 <register_tm_clones>: 400460:b8-ten-XX mov $0x601038,%eax 400465:55 Push%RBP 400466:48 2d, Sub $0x601038,%ra  X 40046c:48 C1 F8 SAR $0x3,%rax 400470:48 e5 mov%rsp,%rbp 400,473:48                C2 mov%rax,%rdx 400476:48 C1 ea 3f shr $0X3F,%RDX 40047a:48 Add%rdx,%rax 40047d:48 d1 F8 SAR%rax 400480:75 Geneva jne 400484   
  <register_tm_clones+0x24> 400482:5d pop%rbp 400483:c3 retq  400484:ba xx xx $0x0,%edx 400489:48 d2 test%RDX,%RDX 40048c:74 F4 JE     400482 <register_tm_clones+0x22> 40048e:5d pop%rbp 40048f:48 c6   mov%rax,%rsi 400492:BF $0x601038,%edi mov 400497:ff E2 JMPQ   *%RDX 400499:0f 1f nopl 0x0 (%rax) 00000000004004a0 <__do_global_dtors_aux>: 4004a0:                   3d 0b CMPB $0x0,0x200b91 (%rip) # 601038 <__TMC_END__> 4004a7:75 11                Jne 4004ba <__do_global_dtors_aux+0x1a> 4004a9:55 push%rbp 4004aa:48 e5                       mov%rsp,%rbp 4004ad:e8 7e FF FF FF CALLQ 400430 <deregister_tm_clones> 4004b2:5d Pop%rbp 4004b3:c6 7e 0b movb $0x1,0x200b7e (%rip) # 601038 &LT;__TMC _end__> 4004ba:f3 C3 RepZ retq 4004bc:0f 1f Max Nopl 0x0 (%rax) 0000000000 4004C0 &LT;FRAME_DUMMY&Gt;: 4004c0:48 3d, CMPQ $0x0,0x200958 (%rip) # 600E20 <__JCR_END__> 4004c7:00 4 004c8:74 1e JE 4004e8 <frame_dummy+0x28> 4004ca:b8/xx/XX mov $0x0,%e Ax 4004cf:48 c0 Test%rax,%rax 4004d2:74 JE 4004e8 <frame_dummy+ 0x28> 4004d4:55 push%rbp 4004d5:bf 0e mov $0x600e20,%edi 4004d                      a:48 e5 mov%rsp,%rbp 4004dd:ff d0 callq *%rax 4004df:5d                Pop%RBP 4004e0:e9 7b FF FF FF JMPQ 400460 <register_tm_clones> 4004e5:0f 1f 00 NOPL (%rax) 4004E8:E9, FF FF FF JMPQ 400460 <register_tm_clones> 00000000004004ed & Lt;test_a&gt: 4004ed:55 push%rbp 4004ee:48 e5 mov%rsp,%rbp 4004 F1:c7 FC 0a 00 00XX movl $0xa,-0x4 (%RBP) 4004f8:90 nop 4004f9:5d pop%RBP 400
  4FA:C3 retq 00000000004004fb <test_b>: 4004fb:55 push%RBP   4004fc:48 e5 mov%rsp,%rbp 4004ff:c7-FC, Movl $0x5,-0x4 (%RBP) 400506: 400507:5d NOP Pop%rbp</

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.