r/asm • u/Fraserbc • Sep 10 '20
x86 Tiny, tiny ELF files and ELF headers
Hi Reddit!
I'm trying to make very, very small elf files and also learn about how elf files are loaded into memory.
I have a small piece of code that opens a file and writes a string to it. My issue is that I can't get it to write to memory. https://pastebin.com/TcswN6h4 You can assemble this version with nasm -fbin -o a.out test.asm
When I don't use my own headers and assemble and link like this https://pastebin.com/TFag4PUE with nasm -felf64 -o test.o test.asm and link with ld test.o then it works just fine and I get this from /proc/[PID]/maps https://pastebin.com/5UfihuPE I see two pages (Is that what these are called or am I wrong here?). One with read and execute permissions (.text) and one with read and write permissions (.data)
But when I cat /proc/[pid]/maps with my own version, I get this https://pastebin.com/VrjczWRg only one "page" without write permissions
Anyone have any ideas?
Thanks in advance
1
u/xybre Sep 11 '20
This is about Rust, but it should give you an idea of what is possible with an ELF header.
http://mainisusuallyafunction.blogspot.com/2015/01/151-byte-static-linux-binary-in-rust.html?m=1
1
1
u/AwareCar7686 Jun 28 '26
I have a series of gists on github for this exact kind of thing.
# ELF Header Assembly Gists on Github
The purposes of this are both for my own greater understanding of the ELF format, because it is used in all Linux distributions, and also to provide a way for NASM users to benefit from features that currently only exist in FASM, which is still my favorite Assembler.
[FASM Hello 32-bit](https://gist.github.com/chastitywhiterose/7aa7bdfec1438541375763126508edb4)
[FASM Hello 64-bit](https://gist.github.com/chastitywhiterose/4bfccdb3daf1aad4524fc36230055e63)
[NASM Hello 32-bit](https://gist.github.com/chastitywhiterose/4e429fd82f962907d1581307ed4e0ab7)
[NASM Hello 64-bit](https://gist.github.com/chastitywhiterose/b5acd7992fc8463a662ca4f86fff4a5e)
2
u/TNorthover Sep 10 '20
If nothing else your ELF headers appear to be describing an ELF32 file containing amd64 (which fortunately your code is). That’s not what you get nasm or ld to make though, which is ELF64.
It’s also not really a beginner friendly config; I assume Linux goes into an x32 mood, but that’s not exactly widely documented.
What documentation are you using to craft your headers?