Weird elf program that could be ran in both MS-DOS and unix?

Programming, for all ages and all languages.
Post Reply
clementttttttttt
Member
Member
Posts: 70
Joined: Tue Jul 14, 2020 4:01 am
Freenode IRC: clementttttttttt

Weird elf program that could be ran in both MS-DOS and unix?

Post by clementttttttttt »

So I found this weird program in grub4dos, which is called "bootlace.com", that can be ran in both MS-DOS and unix, and google didn't help. I would like an explanation on how did it do that.
User avatar
bzt
Member
Member
Posts: 1584
Joined: Thu Oct 13, 2016 4:55 pm
Contact:

Re: Weird elf program that could be ran in both MS-DOS and u

Post by bzt »

clementttttttttt wrote:So I found this weird program in grub4dos, which is called "bootlace.com", that can be ran in both MS-DOS and unix, and google didn't help.
Have you tried github? bootlace source.
clementttttttttt wrote:I would like an explanation on how did it do that.
Use the source Luke...! :-D

The DOS .com executable has no header, it just starts executing the code in real mode from the first byte. ELF needs a header, which starts with 4 magic bytes. Those magic bytes interpreted as real mode code gives:

Code: Select all

	# ELF64 header backup here upto the end of file. Its length is 0x40.

	.byte	0x7F, 0x45, 0x4C, 0x46	# ELF magic number
					// 7F 45 = jg dos_entry_point
					// 4C = decw %sp
					// 46 = incw %si
So assuming CPU flags are set, that "jg" instruction will jump to the "dos_entry_point", while under Linux the ELF header is parsed and the code specified by the ELF e_entry point will be executed instead (_start_linux). If CPU flags aren't set for the conditional jump, then the first 16 bytes of the file will be executed in real-mode, which then would jump to the same dos_entry_point.

Cheers,
bzt
Post Reply