All kinds of small Hello World

Source: Internet
Author: User
Tags compact

See this post in Reddit: Hello from a libc-free world! , think it is very interesting, and then think of all the relevant information I have seen before, do a collation here. Note that all lab environments are Linux.

Version One:

In fact, it is to rewrite the _start portal with the assembly, specifically, see the connection provided at the beginning of the article, the assembly code is as follows, named Stubstart. S

[CPP]View Plaincopy
    1. _start:
    2. Call Main
    3. MOVL $,%eax
    4. Xorl%EBX,%EBX
    5. int $0x80
Then the code with the normal HELLO.C connection (HELLO.C) I don't have to write it. The command is as follows:
Gcc-nostdlib Stubstart. S-o Hello hello.c
OK, a HelloWorld program that doesn't need libc is finished. It's just a simple skip over the various initialization of _start.
Version two:
With the version of a actually almost, just with Shellcode to complete, the code is as follows
[CPP]View Plaincopy
  1. typedef INT (*sc_fun) (int,int,int,int,int,int,int);
  2. void _start (void) {
  3. char syscall[] = "/x60/x83/xc4/x24/x58/x5b/x59/x5a/x5e/x5f/x5d/xcd/x80/x83/xec/x40/x61/xc3";
  4. ((Sc_fun) syscall)  (4, 0, "Hello, world/n", 13, 0, 0, 0);
  5. ((Sc_fun) syscall) (1, 0, 0, 0, 0, 0, 0);
  6. }



The connection commands are as follows:
Gcc-o nostdlib hello.c-m32-z Execstack–nostdlib
Well, done, is also-nostdlib, as to what the shellcode call the system function, I guess is write it:)
Version three: [CPP]View Plaincopy
  1. Char *str = "Hello world!/n";
  2. void print ()
  3. {
  4. ASM ( "Movl $13,%%edx/n/t"
  5. "Movl%0,%%ecx/n/t"
  6. "MOVL,%%ebx/n/t"
  7. "Movl $4,%%eax/n/t"
  8. "int $0x80/n/t"
  9. :: "R" (str):"edx","ecx","ebx");
  10. }
  11. void exit ()
  12. {
  13. ASM ( "MOVL $42,%ebx/n/t"
  14. "Movl $1,%eax/n/t"
  15. "int $0x80/n/t");
  16. }
  17. void Nomain ()
  18. {
  19. Print ();
  20. Exit ();
  21. }

For the GCC inline assembly, refer to the related book, the Code is about Nomain () is the portal, and then call the print () function, print "Hello World", and then call the exit () function to end the process. The print function here uses the Write system call of Linux, and exit uses the exit system call, which is implemented using inline assembly.

The connection commands are as follows:

Gcc–c hello.c

Ld–static–e Nomain–o Hello hello.o

Note that this controls the behavior of the connector, with the-e designation of the entry function as Nomain

Version four:

Then version three, we use objdump to see Hello, we will find that he has four segments:. Text. rodata. Data comment.

Would it be possible to merge them all into a single paragraph where the properties of the segment are executable, readable, and contain program data and instructions? Yes, you need to create a script using the LD connection script Hello.lds as follows:

[C-sharp]View Plaincopy
    1. ENTRY (Nomain)
    2. SECTIONS
    3. {
    4. . = 0x804800 + sizeof_headers;
    5. Tinytext: {* (. Text) * (. Data) * (. rodata)}
    6. /discard/: {* (. Comment)}
    7. }

This is a very simple connection script, that is, set the current position 0x804800 + sizeof_headers, followed by the Tinytext segment, there is no other paragraph. Connect using the Enter command

Gcc–c hello.c

Ld–static–t Hello.lds–o Hello hello.o

OK, a more compact HelloWorld is done.

Version five:

Is version four the smallest? Far away ... There has been a special study of the smallest executable file, from each byte of the elf file. Click here: Size is everything. Very cow B, very geek things. Theoretically, that's the smallest executable file.

Above each version of the HelloWorld size, after their own generation with wc–c hello to see it:). With Objdump can learn more ~

A variety of compact Hello World

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.