Thread: Where can I learn how to create ELF executables?

  1. #1
    Registered User
    Join Date
    Oct 2021
    Posts
    103

    Where can I learn how to create ELF executables?

    Is there a place where I can learn how to create an ELF executable (and the ELF format in general) in action? Please don't tell me things like "go read the source code of an assembler to see how they do it". I already knew that, I just want something to start that won't make my shoot myself. I want to see how the machine instructions are written is the machine language and how I can start in general.

  2. #2
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    945
    Quote Originally Posted by rempas View Post
    Is there a place where I can learn how to create an ELF executable (and the ELF format in general) in action? Please don't tell me things like "go read the source code of an assembler to see how they do it". I already knew that, I just want something to start that won't make my shoot myself. I want to see how the machine instructions are written is the machine language and how I can start in general.
    In Linux, using gcc & g++, executables are created in the elf format.

    A Google search will point you to many articles that should provide you with the information you are searching for.

  3. #3
    Registered User
    Join Date
    Oct 2021
    Posts
    103
    Quote Originally Posted by rstanley View Post
    In Linux, using gcc & g++, executables are created in the elf format.

    A Google search will point you to many articles that should provide you with the information you are searching for.
    Hello! I'm sorry, I think it was more clear but it seems it wasn't.

    When saying create an elf executable (or library), I don't mean using a compiler. I mean actually creating them by myself by opening and writing bytes to the actual executable without any third party program.
    I was able to find about the elf header but I'm not able to find a place where there is a tutorial about how instructions are translated. For example, how I will do `mov rax, 1` in binary?
    Is it happen that you know where I can find a place where there is GOOD (in terms of both well written and explained) documentation about this?
    Last edited by rempas; 01-20-2022 at 11:40 AM.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,069
    Maybe this The elfutils project

    Dunno, it's one of those esoteric things to want to do.
    Like you're the maintainer for binutils and you're porting to a completely new architecture.

    Enjoy.
    Code:
    $ cat foo.c
    int foo ( ) {
        return 42;
    }
    $ gcc -c foo.c   # generate the object code
    $ gcc -c -S foo.c  # generate the asm
    $ cat foo.s
    	.file	"foo.c"
    	.text
    	.globl	foo
    	.type	foo, @function
    foo:
    .LFB0:
    	.cfi_startproc
    	endbr64
    	pushq	%rbp
    	.cfi_def_cfa_offset 16
    	.cfi_offset 6, -16
    	movq	%rsp, %rbp
    	.cfi_def_cfa_register 6
    	movl	$42, %eax
    	popq	%rbp
    	.cfi_def_cfa 7, 8
    	ret
    	.cfi_endproc
    << snipped boilerplate >>
    $ objdump -d foo.o
    foo.o:     file format elf64-x86-64
    Disassembly of section .text:
    0000000000000000 <foo>:
       0:	f3 0f 1e fa          	endbr64 
       4:	55                   	push   %rbp
       5:	48 89 e5             	mov    %rsp,%rbp
       8:	b8 2a 00 00 00       	mov    $0x2a,%eax
       d:	5d                   	pop    %rbp
       e:	c3                   	retq
    $ hd foo.o | head -20
    00000000 7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00 |.ELF............|
    00000010 01 00 3e 00 01 00 00 00 00 00 00 00 00 00 00 00 |..>.............|
    00000020 00 00 00 00 00 00 00 00 58 02 00 00 00 00 00 00 |........X.......|
    00000030 00 00 00 00 40 00 00 00 00 00 40 00 0c 00 0b 00 |....@.....@.....|
    00000040 f3 0f 1e fa 55 48 89 e5 b8 2a 00 00 00 5d c3 00 |....UH...*...]..|
    00000050 47 43 43 3a 20 28 55 62 75 6e 74 75 20 39 2e 33 |GCC: (Ubuntu 9.3|
    00000060 2e 30 2d 31 37 75 62 75 6e 74 75 31 7e 32 30 2e |.0-17ubuntu1~20.|
    00000070 30 34 29 20 39 2e 33 2e 30 00 00 00 00 00 00 00 |04) 9.3.0.......|

    There's your program instructions in your ELF.
    Go getem tex!
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Oct 2021
    Posts
    103
    Quote Originally Posted by Salem View Post
    Maybe this The elfutils project

    Dunno, it's one of those esoteric things to want to do.
    Like you're the maintainer for binutils and you're porting to a completely new architecture.

    Enjoy.
    Code:
    $ cat foo.c
    int foo ( ) {
        return 42;
    }
    $ gcc -c foo.c   # generate the object code
    $ gcc -c -S foo.c  # generate the asm
    $ cat foo.s
        .file    "foo.c"
        .text
        .globl    foo
        .type    foo, @function
    foo:
    .LFB0:
        .cfi_startproc
        endbr64
        pushq    %rbp
        .cfi_def_cfa_offset 16
        .cfi_offset 6, -16
        movq    %rsp, %rbp
        .cfi_def_cfa_register 6
        movl    $42, %eax
        popq    %rbp
        .cfi_def_cfa 7, 8
        ret
        .cfi_endproc
    << snipped boilerplate >>
    $ objdump -d foo.o
    foo.o:     file format elf64-x86-64
    Disassembly of section .text:
    0000000000000000 <foo>:
       0:    f3 0f 1e fa              endbr64 
       4:    55                       push   %rbp
       5:    48 89 e5                 mov    %rsp,%rbp
       8:    b8 2a 00 00 00           mov    $0x2a,%eax
       d:    5d                       pop    %rbp
       e:    c3                       retq
    $ hd foo.o | head -20
    00000000 7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00 |.ELF............|
    00000010 01 00 3e 00 01 00 00 00 00 00 00 00 00 00 00 00 |..>.............|
    00000020 00 00 00 00 00 00 00 00 58 02 00 00 00 00 00 00 |........X.......|
    00000030 00 00 00 00 40 00 00 00 00 00 40 00 0c 00 0b 00 |....@.....@.....|
    00000040 f3 0f 1e fa 55 48 89 e5 b8 2a 00 00 00 5d c3 00 |....UH...*...]..|
    00000050 47 43 43 3a 20 28 55 62 75 6e 74 75 20 39 2e 33 |GCC: (Ubuntu 9.3|
    00000060 2e 30 2d 31 37 75 62 75 6e 74 75 31 7e 32 30 2e |.0-17ubuntu1~20.|
    00000070 30 34 29 20 39 2e 33 2e 30 00 00 00 00 00 00 00 |04) 9.3.0.......|

    There's your program instructions in your ELF.
    Go getem tex!
    Hm, so the idea is that, I'll write a code snippet and see how it translates to binary for each instruction using these tools to read a binary file and see it in hex. That's cool. I would prefer something more practical cause this will be a pain to do but I will differently check this out while still trying to find more resources. Thanks a lot!

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,069
    > I would prefer something more practical cause this will be a pain to do
    That's why we use assemblers and compilers, so we don't have to do that.

    But hey, you wanted to do it all by yourself.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Oct 2021
    Posts
    103
    Quote Originally Posted by Salem View Post
    > I would prefer something more practical cause this will be a pain to do
    That's why we use assemblers and compilers, so we don't have to do that.

    But hey, you wanted to do it all by yourself.
    Hahahaha! Yeah, I mean more practical way to learn how the instructions translate to binary like a specification, official documentation or something.
    I mean, I just want to experiment and see. I mean not create something in the only. Just for the sake of learning!

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,069
    > I mean more practical way to learn how the instructions translate to binary like a specification,
    That information is in the processor reference manual (for whichever machine you're interested in).

    It's got nothing to do with ELF files per se.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User
    Join Date
    Oct 2021
    Posts
    103
    Quote Originally Posted by Salem View Post
    > I mean more practical way to learn how the instructions translate to binary like a specification,
    That information is in the processor reference manual (for whichever machine you're interested in).

    It's got nothing to do with ELF files per se.
    That's nice! Tho I'm thinking that I'll probably won't use it. It seems nice in theory but in action it doesn't worth it neither for learning nor
    implementing an actual assembler as there are a lot of them out there. Thanks a lot! I wish you to have an amazing day!

  10. #10
    Registered User
    Join Date
    Jan 2022
    Posts
    3
    Quote Originally Posted by rempas View Post
    That's nice! Tho I'm thinking that I'll probably won't use it. It seems nice in theory but in action it doesn't worth it neither for learning nor
    implementing an actual assembler as there are a lot of them out there. Thanks a lot! I wish you to have an amazing day! https://berich.eu/pattern-trader-czy-to-scam-poznaj-opinie-ekspertow-i-uzytkownikow/
    If you say so. Maybe you're right. Thanks for the information.

  11. #11
    Registered User
    Join Date
    Oct 2021
    Posts
    103
    Quote Originally Posted by Animazionec View Post
    If you say so. Maybe you're right. Thanks for the information.
    Yeah, sorry that sounded very wrong. I mean, it's not worth is FOR ME specifically. It is the best thing you can do if you are the type to spend your time and actually implement it. Fixed it ;)

  12. #12
    Registered User
    Join Date
    Feb 2022
    Posts
    26
    I realize I am rather late to the party, and I do not know if it helps any, but the book Linkers and Loaders by John Levine covers the structure of ELF32 in detail. It is an older book, so it doesn't really discuss ELF64 , but it gives a reasonable description of the ELF format itself, and even discusses how an ELF file would be generated, though only in general terms. The code examples from the book can be found for free on Levine's own book page.

    While the book isn't exactly cheap, you can probably find a copy of it in any university library, if you have access to one.

    The OSDev wiki page on the ELF format also covers the format itself in detail.

    You can go over the ELF64 specification as well, if you like.

    The thing is, IMAO it really doesn't make sense to try to construct an ELF file manually, simply because of the difficulties involved in editing the binary directly. You could use a hex editor, I suppose, but it goes against the whole purpose of it in my mind. Object files in general are far too complex to easily prepare manually. They are designed to generated by compilers and linkers, not by hand.
    Last edited by Schol-R-LEA-2; 02-05-2022 at 08:04 PM.

  13. #13
    Registered User
    Join Date
    Oct 2021
    Posts
    103
    Quote Originally Posted by Schol-R-LEA-2 View Post
    I realize I am rather late to the party, and I do not know if it helps any, but the book Linkers and Loaders by John Levine covers the structure of ELF32 in detail. It is an older book, so it doesn't really discuss ELF64 , but it gives a reasonable description of the ELF format itself, and even discusses how an ELF file would be generated, though only in general terms. The code examples from the book can be found for free on Levine's own book page.

    While the book isn't exactly cheap, you can probably find a copy of it in any university library, if you have access to one.

    The OSDev wiki page on the ELF format also covers the format itself in detail.

    You can go over the ELF64 specification as well, if you like.
    Better late than never my friend! Thanks a lot for the info! I will try to find the book if possible and read it. Probably there will not be a lot of differences between ELF32 and ELF64 so it will be fine. The other two sources are great. The OSDev wiki page is awesome and I will read it. The spec is also the most complete thing we have but like I explained, I want something friendlier to begin with and then we can read the spec for fixing bugs and learn advanced stuff.

    Quote Originally Posted by Schol-R-LEA-2 View Post
    The thing is, IMAO it really doesn't make sense to try to construct an ELF file manually, simply because of the difficulties involved in editing the binary directly. You could use a hex editor, I suppose, but it goes against the whole purpose of it in my mind. Object files in general are far too complex to easily prepare manually. They are designed to generated by compilers and linkers, not by hand.
    But.... That's what I'm going to do. I plan to create a compiler! I mean, I am crazy but not crazy enough to want to edit binary files by hand. Thank you for your time my friend!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 11-20-2013, 10:20 AM
  2. C++: What package should I learn to create real world programs?
    By DecoratorFawn82 in forum C++ Programming
    Replies: 0
    Last Post: 11-19-2013, 01:52 PM
  3. Create multiple executables instead of one
    By sridharval in forum C++ Programming
    Replies: 3
    Last Post: 05-01-2010, 09:34 PM
  4. How to create executables?
    By Evil Genius in forum C Programming
    Replies: 5
    Last Post: 12-24-2006, 11:38 AM
  5. executables
    By floogyman in forum C Programming
    Replies: 2
    Last Post: 12-26-2003, 02:54 AM

Tags for this Thread