Page 1 of 1

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

Posted: Fri Mar 26, 2021 7:30 am
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.

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

Posted: Fri Mar 26, 2021 8:04 am
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