Page 1 of 1

Compiling simple UEFI application

Posted: Mon Jan 11, 2021 10:07 am
by PeterX
I tried a minimal UEFI application, but it doesn't work:

Code: Select all

#include <efi.h>

EFI_STATUS efi_main(EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *ST)
{
	EFI_STATUS Status;

	return ST->ConOut->OutputString(ST->ConOut, L"Hello World\r\n");
}
Build commands are ("-I..." will be set in the makefile) :

Code: Select all

x86_64-w64-mingw32-gcc -ffreestanding -I... -I... -c -o rawk.o rawk.c
x86_64-w64-mingw32-gcc -nostdlib -Wl,-dll -shared -Wl,--subsystem,10 -o rawk.efi rawk.o -lgcc
rawk.efi will be copied into mountdir.
qemu-system-x86_64 -pflash OVMF.fd -hda fat:rw:mountdir -net none -m 4g
The produced EFI app hangs and doesn't print anything. What's wrong?

You can find my project at:
https://notabug.org/PeterOSdev/smudeco
But there really is not much to see.

Re: Compiling simple UEFI application

Posted: Mon Jan 11, 2021 10:54 am
by Octocontrabass
I don't see where you set the entry point of your executable. (The wiki uses "-e efi_main" on the link command line, for example.)

Re: Compiling simple UEFI application

Posted: Mon Jan 11, 2021 11:30 am
by PeterX
Octocontrabass wrote:I don't see where you set the entry point of your executable. (The wiki uses "-e efi_main" on the link command line, for example.)
Thanks for pointing that mistake out. I corrected it and it still doesn't work (doesn't print but hangs).

EDIT: I found a typo in the makefile. But it still doesn't work.

Greetings
Peter

Re: Compiling simple UEFI application

Posted: Mon Jan 11, 2021 11:32 am
by bzt
Octocontrabass wrote:I don't see where you set the entry point of your executable. (The wiki uses "-e efi_main" on the link command line, for example.)
Good point!

I believe the mingw version of "readelf" can be used to parse and dump PE files (at least the Win version works, not sure about the Linux cross-toolchain). It's called "x86_64-w64-mingw32-readelf" I believe. Otherwise I suggest a small tool like PEdumper. That will tell you if the entry point is correct or not. Also shouldn't the entry point defined with "EFI_ABI" or something? You should check the disassembly if it gets ST in the correct register.

Cheers,
bzt

Re: Compiling simple UEFI application

Posted: Mon Jan 11, 2021 11:51 am
by PeterX
bzt wrote:
Octocontrabass wrote:I don't see where you set the entry point of your executable. (The wiki uses "-e efi_main" on the link command line, for example.)
Good point!

I believe the mingw version of "readelf" can be used to parse and dump PE files (at least the Win version works, not sure about the Linux cross-toolchain). It's called "x86_64-w64-mingw32-readelf" I believe. Otherwise I suggest a small tool like PEdumper. That will tell you if the entry point is correct or not. Also shouldn't the entry point defined with "EFI_ABI" or something? You should check the disassembly if it gets ST in the correct register.
In the example there's no "EFI_ABI".
https://wiki.osdev.org/UEFI_App_Bare_Bones#hello.c

I can't compile PEdumper because I have no windows.h on my GNU/Linux system.

I can't use readelf because rawk.o and rawk.efi aren't ELF but PE (I tried it).

Re: Compiling simple UEFI application

Posted: Mon Jan 11, 2021 11:58 am
by kzinti
You shouldn't need EFI_ABI with mingw as it uses the MS ABI by default.

Re: Compiling simple UEFI application

Posted: Mon Jan 11, 2021 1:17 pm
by bzt
PeterX wrote:In the example there's no "EFI_ABI".
I'm not sure if it's needed, it was just a guess.
PeterX wrote:I can't compile PEdumper because I have no windows.h on my GNU/Linux system.
Oh, my bad, sorry. Then try pe_tree or pefile, they're both in Python. I'm sure you can find many PE dumpers on github, look around.
PeterX wrote:I can't use readelf because rawk.o and rawk.efi aren't ELF but PE (I tried it).
That's a pity. Even though it's called readelf, the MSYS2 version works with PE files. I thought mingw-readelf should too.
Hm, another idea, maybe there's a PE aware objdump in the mingw package? (Sorry, I'm just trying to help without much experience on this, I use GNUEFI for my loader, and I only use mingw under Win to compile POSIX code into native Win executables)

Cheers,
bzt

Re: Compiling simple UEFI application

Posted: Mon Jan 11, 2021 1:23 pm
by PeterX
I found a working solution, but it is kind of strange:
I removed the "-lgcc" option!

Thanks for all help so far. I will soon run into another issue, but I hope with the help of the wiki I won't be completely helpless and clueless. :)

Greeting
Peter

Re: Compiling simple UEFI application

Posted: Mon Jan 11, 2021 1:35 pm
by bzt
PeterX wrote:I found a working solution, but it is kind of strange:
I removed the "-lgcc" option!

Thanks for all help so far. I will soon run into another issue, but I hope with the help of the wiki I won't be completely helpless and clueless. :)

Greeting
Peter
Great! Thanks for the info, I've updated the wiki so hopefully nobody else will have trouble with this!

Cheers,
bzt

Re: Compiling simple UEFI application

Posted: Mon Jan 11, 2021 4:19 pm
by Octocontrabass
PeterX wrote:I found a working solution, but it is kind of strange:
I removed the "-lgcc" option!
Can you upload binaries with and without -lgcc so I can compare? I'd like to see what's going on there.

Re: Compiling simple UEFI application

Posted: Mon Jan 11, 2021 5:17 pm
by PeterX
Octocontrabass wrote:
PeterX wrote:I found a working solution, but it is kind of strange:
I removed the "-lgcc" option!
Can you upload binaries with and without -lgcc so I can compare? I'd like to see what's going on there.
Done. See here:
https://notabug.org/PeterOSdev/smudeco/releases

I guess -lgcc changes the entry point. But I don't know.

Greetings
Peter

Re: Compiling simple UEFI application

Posted: Mon Jan 11, 2021 7:46 pm
by Octocontrabass
I've compared those binaries and the only differences are timestamps and checksums. They are functionally identical.

Did you upload the right files? Could the problem actually be something else?

Re: Compiling simple UEFI application

Posted: Mon Jan 11, 2021 8:04 pm
by PeterX
Octocontrabass wrote:I've compared those binaries and the only differences are timestamps and checksums. They are functionally identical.

Did you upload the right files? Could the problem actually be something else?
They have the same size which is strange. But they are different. I tried a call of "diff" on them.
I tried it and it worked fine with -lgcc. I will have to investigate this closer. I suspect you're right and the problem was something else.
I will post here if I find a solution (if...).

Greetings
Peter