OSDev.org

The Place to Start for Operating System Developers
It is currently Thu Apr 18, 2024 9:03 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 13 posts ] 
Author Message
 Post subject: Compiling simple UEFI application
PostPosted: Mon Jan 11, 2021 10:07 am 
Offline
Member
Member

Joined: Fri Nov 22, 2019 5:46 am
Posts: 590
I tried a minimal UEFI application, but it doesn't work:
Code:
#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:
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.


Top
 Profile  
 
 Post subject: Re: Compiling simple UEFI application
PostPosted: Mon Jan 11, 2021 10:54 am 
Offline
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 5137
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.)


Top
 Profile  
 
 Post subject: Re: Compiling simple UEFI application
PostPosted: Mon Jan 11, 2021 11:30 am 
Offline
Member
Member

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

Top
 Profile  
 
 Post subject: Re: Compiling simple UEFI application
PostPosted: Mon Jan 11, 2021 11:32 am 
Offline
Member
Member
User avatar

Joined: Thu Oct 13, 2016 4:55 pm
Posts: 1584
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


Top
 Profile  
 
 Post subject: Re: Compiling simple UEFI application
PostPosted: Mon Jan 11, 2021 11:51 am 
Offline
Member
Member

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


Top
 Profile  
 
 Post subject: Re: Compiling simple UEFI application
PostPosted: Mon Jan 11, 2021 11:58 am 
Offline
Member
Member

Joined: Mon Feb 02, 2015 7:11 pm
Posts: 898
You shouldn't need EFI_ABI with mingw as it uses the MS ABI by default.

_________________
https://github.com/kiznit/rainbow-os


Top
 Profile  
 
 Post subject: Re: Compiling simple UEFI application
PostPosted: Mon Jan 11, 2021 1:17 pm 
Offline
Member
Member
User avatar

Joined: Thu Oct 13, 2016 4:55 pm
Posts: 1584
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


Top
 Profile  
 
 Post subject: Re: Compiling simple UEFI application
PostPosted: Mon Jan 11, 2021 1:23 pm 
Offline
Member
Member

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


Top
 Profile  
 
 Post subject: Re: Compiling simple UEFI application
PostPosted: Mon Jan 11, 2021 1:35 pm 
Offline
Member
Member
User avatar

Joined: Thu Oct 13, 2016 4:55 pm
Posts: 1584
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


Top
 Profile  
 
 Post subject: Re: Compiling simple UEFI application
PostPosted: Mon Jan 11, 2021 4:19 pm 
Offline
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 5137
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.


Top
 Profile  
 
 Post subject: Re: Compiling simple UEFI application
PostPosted: Mon Jan 11, 2021 5:17 pm 
Offline
Member
Member

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


Top
 Profile  
 
 Post subject: Re: Compiling simple UEFI application
PostPosted: Mon Jan 11, 2021 7:46 pm 
Offline
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 5137
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?


Top
 Profile  
 
 Post subject: Re: Compiling simple UEFI application
PostPosted: Mon Jan 11, 2021 8:04 pm 
Offline
Member
Member

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


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 13 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: SemrushBot [Bot] and 116 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group