Compiling simple UEFI application

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
PeterX
Member
Member
Posts: 590
Joined: Fri Nov 22, 2019 5:46 am

Compiling simple UEFI application

Post 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.
Octocontrabass
Member
Member
Posts: 5568
Joined: Mon Mar 25, 2013 7:01 pm

Re: Compiling simple UEFI application

Post 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.)
PeterX
Member
Member
Posts: 590
Joined: Fri Nov 22, 2019 5:46 am

Re: Compiling simple UEFI application

Post 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
Last edited by PeterX on Mon Jan 11, 2021 11:40 am, edited 1 time in total.
User avatar
bzt
Member
Member
Posts: 1584
Joined: Thu Oct 13, 2016 4:55 pm
Contact:

Re: Compiling simple UEFI application

Post 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
PeterX
Member
Member
Posts: 590
Joined: Fri Nov 22, 2019 5:46 am

Re: Compiling simple UEFI application

Post 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).
kzinti
Member
Member
Posts: 898
Joined: Mon Feb 02, 2015 7:11 pm

Re: Compiling simple UEFI application

Post by kzinti »

You shouldn't need EFI_ABI with mingw as it uses the MS ABI by default.
User avatar
bzt
Member
Member
Posts: 1584
Joined: Thu Oct 13, 2016 4:55 pm
Contact:

Re: Compiling simple UEFI application

Post 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
PeterX
Member
Member
Posts: 590
Joined: Fri Nov 22, 2019 5:46 am

Re: Compiling simple UEFI application

Post 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
User avatar
bzt
Member
Member
Posts: 1584
Joined: Thu Oct 13, 2016 4:55 pm
Contact:

Re: Compiling simple UEFI application

Post 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
Octocontrabass
Member
Member
Posts: 5568
Joined: Mon Mar 25, 2013 7:01 pm

Re: Compiling simple UEFI application

Post 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.
PeterX
Member
Member
Posts: 590
Joined: Fri Nov 22, 2019 5:46 am

Re: Compiling simple UEFI application

Post 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
Octocontrabass
Member
Member
Posts: 5568
Joined: Mon Mar 25, 2013 7:01 pm

Re: Compiling simple UEFI application

Post 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?
PeterX
Member
Member
Posts: 590
Joined: Fri Nov 22, 2019 5:46 am

Re: Compiling simple UEFI application

Post 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
Post Reply