The link process of C program under Linux

Source: Internet
Author: User
Tags builtin function prototype

See a very interesting small program today, it let me to Linux under the C program compile link has a brand-new understanding!

This program is to write a simple output "Hello world!":

requirements: 1. Do not use the C runtime Library, write a program independent of any library. (which means we can't #include<stdio>).

2. Do not use the main function as the entrance to the program (we all know that the program that uses the library generally uses the main function as the entrance to the program , where we use our own function Nomain as the entrance to the program ).

3. Use the connector ld to fit all segments of the program into a "Tinytext" segment.

 

char *str= "hello world!\n"; Void print () {     asm ("movq $13,%%rdx \n\t"          "MOVQ&NBSP;%0,%%RCX  \n\t "        " movq $0,%%rbx \n\t "          "movq $4,%%rax \n\t"          " Int $0x80     \n\t "&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;::" R "(str):" EdX "," ecx "," ebx ");} Void exit () {   asm ("movq $42,%rbx   \n\t"          "movq $1,%rax    \n\t"         " Int $0x80       \n\t ");} Void nomain () {   print ();    exit ();} 

We vi a TINYHELLOWORLD.C. Write the code on it.

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/80/2E/wKiom1c6gDzi1qGOAAAqkVw4NJs385.png "title=" Capture 5. PNG "alt=" Wkiom1c6gdzi1qgoaaaqkvw4njs385.png "/>

My environment is 64-bit, if it is 32 friends for the environment just put, MOVQ changed to MOVL,RDX to Edx,rax instead of EAX,RBX instead of EBX.

since the system structure of the operating system has changed, in x86-64, all universal registers (GPRs) have been expanded from 32 to 64 bits, and the name has changed. 8 General-purpose registers (EAX, ebx, ECX, edx, EBP, ESP, ESI, EDI) are named Rax, RBX, RCX, RDX, RBP, RSP, RSI, RDI in the new structure. The MOVL command also needs to be changed to MOVQ.

The first entry of the program is the Nomain function, which calls the print function printing HelloWorld, and then calls the Exit function to exit. The print function uses the write system call of Linux and the Exit function uses the exit system call .

Defines a string that is placed in a read-only code snippet .

both the write system call and the exit system call are implemented through the 0x80 interrupt in the print function and Exit function. where EAX is the call number RBX,RCX,RDX is a number of registers used to pass parameters. For example, the write system call is to write the data to a file handle, if the C language to represent the write system call, his function prototype is:

int write (int filedesc,char* buffer,int size)
    • Write has a call number of 4, then Rax is 0.

    • FILEDESC represents the handle file that is being written, we output the default terminal by default, his handle is 0, so RBX is 0.

    • Buffer indicates the address to be written using RCX register transfer, we want to output str string, so rcx=str.

    • The size represents the number of bytes to write, and the length of str here is 13 bytes, so rdx=13.



After the code is written, we use the normal command line to compile the link tinyhelloworld.c

Gcc-c-fno-builtin TINYHELLOWORLD.C//Generate TINYHELLOWORLD.O
    • The-c parameter is a compilation.

    • The-fno-builtin parameter turns off the function of the GCC built-in function (there are many built-in functions in GCC, which you will replace some of the C library functions with the built-in functions to achieve the optimized function)


      Then link the tinyhelloworld.o file to generate the executable file Tinyhelloworld.

LD-STATIC-E Nomain-o Tinyhelloworld TINYHELLOWORLD.O
    • The-static parameter refers to the use of static linking methods here.

    • -e Nomai parameter indicates the entry of the program is the Nomain function

    • -O means link


      650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/80/2F/wKiom1c6gs2jXuoYAAAuU3aOtZg830.png "title=" Capture 6. PNG "alt=" Wkiom1c6gs2jxuoyaaauu3aotzg830.png "/>

      Run can output Hello world!


650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M00/80/2F/wKiom1c6gyKAPVsiAAALJh-VQTw145.png "title=" Capture 3. PNG "alt=" Wkiom1c6gykapvsiaaaljh-vqtw145.png "/>

So far, a HelloWorld program has been written.

We use the Objdump directive to view Tinyhelloworld This file can be seen in 4 more segments:. Data. rodata. Text. Commond segment. The properties of these paragraphs are read-only, and we can encirclement them to a paragraph . This is achieved by means of the LD linker.

Objdump-h Tinyhelloworld

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/80/2C/wKioL1c6iHSz_u_eAABPiBff344286.png "title=" Capture 3. PNG "alt=" Wkiol1c6ihsz_u_eaabpibff344286.png "/>


Use-verbose to view the default link script information:

Ld-verbose Tinyhelloworld

Then write the Tinyhelloworld.lds script :

ENTRY (nomain) sections{. = 0x08048000 + sizeof_headers; Tinytext: {* (. Text) * (. Data) * (. rodata)}/discard/: {* (. comment)}}

Then use the script you wrote to link the target file .

Gcc-c-fno-builtin tinyhelloworld.ld-static-t tinyhelloworld.lds-o Tinyhello TINYHELLOWORLD.O

The-static-t tinyhelloworld.lds parameter is to use a static link and use the Tinyhelloworld.lds script link that you wrote.

It generates a 588-byte executable file, Tinyhelloworld. Execution can print Hello world!. If you use Objdump to view Tinyhelloworld segments, you will find that we have reached the requirement to have only one segment in the last one.

This article is from the "Straw Sunshine" blog, please be sure to keep this source http://helloleex.blog.51cto.com/10728491/1774383

Link procedure for C programs under Linux

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.