public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
From: "Ayush Singh" <ayushdevel1325@gmail.com>
To: Andrew Fish <afish@apple.com>
Cc: edk2-devel-groups-io <devel@edk2.groups.io>
Subject: Re: [edk2-devel] Does edk2 also link to crt0-efi like GNU-EFI
Date: Fri, 17 Jun 2022 01:47:54 +0530	[thread overview]
Message-ID: <CA+Yfj7sZcCmTQ7keOopabnGrWHPjoACOy5FjJZPPOnMvvR+-Sg@mail.gmail.com> (raw)
In-Reply-To: <4410FE99-43C7-4331-AB96-42ADB294AE30@apple.com>

Thanks for the great answer. After some discussion in the zulip [1], I
have some approaches that I will try first.

As for calling C, EFIABI from Rust, yes, it is quite well supported.
Rust also has a specific EFIABI now, since some EFI platforms don't
use C calling conventions.

As for the entry point, llvm has an `-entry:efi_main` that can be used
to define the entry function for UEFI targets. I just was not sure I
could integrate it with the actual `main()` function.

In normal Rust, the control flow is somewhat like this: crt0 -> libc
main -> rust lang_start -> rust lang_start_internal -> rust main

In UEFI, I would have to do something like this: efi_main -> rust
lang_start -> rust lang_start_internal -> rust main

The problem was that I couldn't find a way to go from `efi_main ->
rust lang_start` earlier. After all, it is such a low-level detail
that there is almost no documentation for anything that happens before
`rust lang_start`. Still, I do have some idea now, so will see how it
goes.

Ayush Singh

[1]: (https://rust-lang.zulipchat.com/#narrow/stream/182449-t-compiler.2Fhelp/topic/Run.20a.20function.20before.20.60lang_start.60/near/286376012)

On Fri, Jun 17, 2022 at 12:06 AM Andrew Fish <afish@apple.com> wrote:
>
>
>
> > On Jun 16, 2022, at 7:20 AM, Ayush Singh <ayushdevel1325@gmail.com> wrote:
> >
> > Hello everyone, I wanted to ask if the edk2 build system also links to
> > crt0-efi, like GNU-EFI?
> >
> > If yes, I would also like to see how that is actually implemented. If
> > not, how does edk2 support custom entry functions? It is possible with
> > llvm backend but I am not sure how it is done in GCC and am curious,
> >
>
> The general answer for edk2 is and library that a `CONSTRUCTOR =` statement in its INF that lib constructor will get called when the Driver/App is started.
>
> The actually entry point for a Driver/App is _ModuleEntryPoint(). The typical way it is done is this is implemented in a phase specific library[1]. This phase specific basically calls 3 C functions that got generated by the build: ProcessLibraryConstructorList(), ProcessModuleEntryPointList(), and ProcessLibraryDestructorList(). The library constructor/destructors functions call the lib  constructor/destructors function based in the sequence of the dependency graph of the libraries that get pulled in.
>
> > Currently, rust does not support the custom implementation of
> > `lang_start` (which is started by crt0 in most platforms), so I was
> > trying to find ways to be able to use custom crt0 which sets up
> > `SystemTable` and `SystemHandler` and start the `lang_start` from it.
> > This way, the user will be able to call the normal `main` function
> > rather than using the `no_main` feature.
> >
>
> If you look in the build output of an edk2 C driver/app you will see an AutoGen.h and AutoGen.c file. This was the C code the build system autogenerated to glue everything together. It manages gluing in the libs, abstracting the PCD implementation, and adding C constants for EFI_GUID values.
>
> Sorry I don’t know Rust yet. Is it possible to call C, EFIABI, from Rust code? If yes maybe what you need is a Rust ModuleEntryPoint that can call the C library constructor and a C EFIABI entry function for your Rust. I’m not sure how you manage dealing with C includes in Rust? With C++ you can just decorate them.
>
> I guess the other 100% Rust option is to know the INF is building Rust (INF has Code.rs files) and build Rust (or Rust compatible) AutoGen
>
> > My blog post [1] shows how we currently use the `efi_main` function.
> >
> > Yours sincerely,
> > Ayush Singh
> >
> > [1]: (https://www.programmershideaway.xyz/post5/)
> >
> >
>
> [1] https://github.com/tianocore/edk2/blob/master/MdePkg/Library/UefiApplicationEntryPoint/ApplicationEntryPoint.c
>
> Thanks,
>
> Andrew Fish
>
>
> > 
> >
> >
>

  reply	other threads:[~2022-06-16 20:18 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-06-16 14:20 Does edk2 also link to crt0-efi like GNU-EFI Ayush Singh
2022-06-16 18:36 ` [edk2-devel] " Andrew Fish
2022-06-16 20:17   ` Ayush Singh [this message]
2022-06-16 23:12     ` [edk2-devel] Does edk2 also link to crt0-efi like GNU-EFI * Andrew Fish
2022-06-17  5:42       ` Ayush Singh

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CA+Yfj7sZcCmTQ7keOopabnGrWHPjoACOy5FjJZPPOnMvvR+-Sg@mail.gmail.com \
    --to=devel@edk2.groups.io \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox