News for hackers. Stuff that matters.
Re:Elf Hello World by CalChris on Wednesday May 17, 2017 @11:52AM (#14359650)
This tutorial should be read alongside his Address Relocation answer from Stack Overflow:

http://stackoverflow.com/questions/12122446/how-does-c-linki...

And then you might want to skim through An Evil Copy: How the Loader Betrays You:

https://www.microsoft.com/en-us/research/publication/evil-co...

Re:Elf Hello World by theoh on Wednesday May 17, 2017 @04:37PM (#14362559)
That new CoRev paper is interesting. I haven't had time to read it in detail but it sounds like an intriguing case for compiler/linker buffs.

Slightly OT, there was a very fun ActionScript exploit published by Mark Dowd of IBM in 2008. I'm reminded of it because it's about memory protection vulnerabilities.

"Application-Specific Attacks: Leveraging the ActionScript Virtual Machine"

https://www.cs.utexas.edu/~shmat/courses/cs6431/dowd.pdf

Re:Elf Hello World by tonyg on Wednesday May 17, 2017 @01:30PM (#14360680)
My favourite page on ELF executables of all time: http://www.muppetlabs.com/~breadbox/software/tiny/teensy.htm..., "A Whirlwind Tutorial on Creating Really Teensy ELF Executables for Linux (or, 'Size Is Everything')"
Re:Elf Hello World by binarycrusader on Wednesday May 17, 2017 @11:24PM (#14364626)
ELF is pretty generally applicable to all NIX and NIX-like platforms, so I often refer to the Solaris Libraries and Linkers guide which has a lot of generally useful information and guidance:

https://docs.oracle.com/cd/E53394_01/html/E54813/glcfv.html#...

Re:Elf Hello World by caust1c on Wednesday May 17, 2017 @04:12PM (#14362370)
Heya! I followed a long and made a 64 bit version a while back. Great resource for learning ELF.

https://github.com/abraithwaite/teensy

Re:Elf Hello World by zmodem on Wednesday May 17, 2017 @05:08PM (#14362796)
That one is awesome and inspired me to write http://www.hanshq.net/making-executables.html
Re:Elf Hello World by 10165 on Thursday May 18, 2017 @10:32AM (#14367513)
I like your page about making Android executables, especially the quote from Carmac and comment about the vurtues of being control freak. Have you tried to do the same thing for iOS? What is the current file and directory count for Xcode?
Re:Elf Hello World by znpy on Wednesday May 17, 2017 @04:40PM (#14362591)
An interesting read, thanks a lot!
Re:Elf Hello World by bogomipz on Wednesday May 17, 2017 @12:10PM (#14359851)
If you enjoyed this you might also like the following article:

http://nullprogram.com/blog/2016/11/17/

Re:Elf Hello World by molticrystal on Thursday May 18, 2017 @06:27AM (#14366095)
In elftoolchain you'll see some elfs having a size of 52 instead of 64

https://github.com/sergev/elftoolchain/search?utf8=%E2%9C%93...

Also it is interesting that the powerpc elf loader verifies that the e_ehsize is the same size as the allocated buffer, perhaps they had an issue at one point.

https://github.com/torvalds/linux/blob/ba6d973f78eb62ffebb32...

Re:Elf Hello World by wyldfire on Wednesday May 17, 2017 @11:44AM (#14359565)
It's great, thanks!

Thoughts for chapter 2: stuff that often confuses me: PLT, GOT, FDE/CIE.

Re:Elf Hello World by cstrahan on Wednesday May 17, 2017 @04:18PM (#14362408)
Eli Bandersky wrote an excellent series on load-time relocation and position independent code (PIC), including details regarding PLT/GOT:

http://eli.thegreenplace.net/2011/08/25/load-time-relocation...

http://eli.thegreenplace.net/2011/11/03/position-independent...

Re:Elf Hello World by rocky1138 on Wednesday May 17, 2017 @11:52AM (#14359651)
I love the fact that it's all on one page and uses HTML anchors to get around. It makes it super simple to archive and read up later if the site ever goes down.
Re:Elf Hello World by cirosantilli on Wednesday May 17, 2017 @01:39PM (#14360779)
Well, you can also fork and git clone it: https://github.com/cirosantilli/cirosantilli.github.io/blob/... :-)
Re:Elf Hello World by rocky1138 on Wednesday May 17, 2017 @02:30PM (#14361351)
Thanks for the article! I'm really enjoying learning this stuff. One thing that came up which doesn't appear very clear is why `ld` continues to set the entrypoint for executables at 0x08048000 when that seems like such an arbitrary number held over from a version of Unix made decades ago. Wouldn't it be better to just get rid of that and start programs at 0x00000001? (leaving 0x0 open for NUL).
Re:Elf Hello World by kdunwoody on Wednesday May 17, 2017 @03:46PM (#14362162)
Definitely not. In rough order of importance:

1. Space for NULL should be big enough for at least a medium-sized structure, otherwise (*NULL)->field = blah will overwrite your code. 2. Because of (1), Linux (for example) doesn't even allow processes to map the first page or so of memory. 3. On many platforms the PC needs to be aligned to a word boundary.

Re:Elf Hello World by rocky1138 on Wednesday May 17, 2017 @04:42PM (#14362609)
I see. I've obviously got a lot more to learn. Thanks :)

One more question, though: how much space should be safe to leave for NULL? Is 128MB enough? Why not only as much as required?

I suppose it doesn't really matter anyway since the entry point location is virtual at this point anyway.

Re:Elf Hello World by Ded7xSEoPKYNsDd on Thursday May 18, 2017 @06:03AM (#14366004)
It's impossible to know how much exactly is required. (While structures are fixed size, arrays indices are not.)

Linux allows you to configure the limit at /proc/sys/vm/mmap_min_addr.

Re:Elf Hello World by bogomipz on Wednesday May 17, 2017 @11:59AM (#14359751)
I know PLT and GOT but am unfamiliar with the acronyms FDE/CIE. Can you say what those are?
Re:Elf Hello World by wyldfire on Wednesday May 17, 2017 @12:08PM (#14359837)
FDE: Frame Description Entry, CIE: Common Information Entry. AFAIR, they're used to unwind the stack in exceptions.

I think it's specified by itanium C++ ABI (which sounds archaic but actually many other processors use the same one).

I don't recall if these things are ELF features or if the ELF only knows about the "eh_frame" section. It's interesting and kinda sorta related low-level stuff IMO.

Re:Elf Hello World by bogomipz on Wednesday May 17, 2017 @12:16PM (#14359912)
Interesting, I wasn't familiar with this standard. It sounds like this has been adopted by some other architectures besides Itanium as well:

http://wiki.dwarfstd.org/index.php?title=Exception_Handling

Re:Elf Hello World by sgmansfield on Wednesday May 17, 2017 @11:39PM (#14364658)
Fantastic article. I noticed a couple of TODO's around, though. Does this hint at a second installment? :)
Re:Elf Hello World by cirosantilli on Thursday May 18, 2017 @01:30AM (#14365008)
If I need it for work at some point :-) Not in forseeable future :-( But you can send PRs or fork it: https://github.com/cirosantilli/cirosantilli.github.io/blob/...
Re:Elf Hello World by sigjuice on Wednesday May 17, 2017 @08:22PM (#14363978)
Impressive work.

Anyone know of something equivalent for Mach-o?

Re:Elf Hello World by muzster on Wednesday May 17, 2017 @12:17PM (#14359924)
Great article...

Makes you wonder how ELF's brother (PE) and daughter (WASM) are doing.

Re:Elf Hello World by laser on Wednesday May 17, 2017 @12:29PM (#14360045)
Is it just me, or are the author's ears slightly pointed?
Re:Elf Hello World by cirosantilli on Wednesday May 17, 2017 @01:39PM (#14360789)
You've uncovered my secret: I'm an ELF!!! :-)
Re:Elf Hello World by laser on Wednesday May 17, 2017 @04:48PM (#14362658)
I knew it!! :P
Re:Elf Hello World by pinum on Wednesday May 17, 2017 @01:11PM (#14360482)
I don't know about that, but I did notice the author's photo looks like a passport pic (strict, formal). An unusual thing to include on a personal site, maybe.
Re:Elf Hello World by cirosantilli on Wednesday May 17, 2017 @01:40PM (#14360795)
Yeah, standard booth pic for document format. I don't think I used it in any documents however.