From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-io1-f45.google.com (mail-io1-f45.google.com [209.85.166.45]) by mx.groups.io with SMTP id smtpd.web08.22472.1655410680304294574 for ; Thu, 16 Jun 2022 13:18:00 -0700 Authentication-Results: mx.groups.io; dkim=pass header.i=@gmail.com header.s=20210112 header.b=BcMi0U5U; spf=pass (domain: gmail.com, ip: 209.85.166.45, mailfrom: ayushdevel1325@gmail.com) Received: by mail-io1-f45.google.com with SMTP id y79so2612977iof.2 for ; Thu, 16 Jun 2022 13:18:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc:content-transfer-encoding; bh=A2VwdR/f1PNo50MOWtxuPrAUc46vyhD3UXrSy/dyO/k=; b=BcMi0U5UEuJyWAwbnI3/S8fZUQmAc3PbE3f8dhUnUv8/dAXEF+qfbw3kwyRFLw3yTH stn4wkkqdf6C79qcdkl9HBEA+rwVvVWcuMlbo17AEh2cJdKIELbzLAMcEo3AD7iMygJe aUgkuLY5Q1NM5L/6zQrgrti9qwi4NPefbNDrBwgbJOEKttq538VGn9HnvgX9JcvAOlN6 Ljr4BYaX2Kriu8p8RDLhTYD57KcaCeTLI9WE5OBre8vAnT/+2O0rzLbZqzen/cZqqNIN 4M7eCDyDwuFEbVk/g5xxZHQ7TGfy5Ak7jExoW6rn1J/QbohDY4vG7QshDGC0qaZxWDmK nPrg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc:content-transfer-encoding; bh=A2VwdR/f1PNo50MOWtxuPrAUc46vyhD3UXrSy/dyO/k=; b=aT7lbb/sUmcDQ04YTQ+122TfD7mrgGrNtu4Reym053PdzT7IBzrOFzd0h8kNhQJmsj X4ieyRQPQWqIKbkA0q4DSyT/gr6gCFSS+q8bidUQefa984M40KccFk39PNMwtCqW/b2c eSNq8U8XT9pVGTZbBa94ZRNsf3xvhvB52kNsT+pO92xp6vsp2d6tzOcAtetSsrlviCjt cLaOYKxVZaVH0bR6rhWoSKITMjJVY/CoYYn6Vi3NTFrSIYya3fO1BZl5VxKH02QZjnVY UyJ9/t6qzxPQqF+9Dx9s65jStTSG8euUIqLbBaXfmg3cH0I0u4XcqgwCIB4YqOk/x73w 5IHg== X-Gm-Message-State: AJIora9MoflSFM39Swe8JbKuopPNEYV8807qWgPeeQGW4BtCcMmS16R7 AbNH0pZvMo5OYwbQ4vvACzrri6VC4end2lZJ7n0= X-Google-Smtp-Source: AGRyM1tcjSdtqK7FnHbZFtjBMDI8Hms7ZxvXOkFJbRo1QpEDsfmPPPvDRhAvSGHobGUmcsJN883LP0BWCQKMp1A0huc= X-Received: by 2002:a05:6638:168a:b0:331:9c9a:90b4 with SMTP id f10-20020a056638168a00b003319c9a90b4mr3777866jat.236.1655410679586; Thu, 16 Jun 2022 13:17:59 -0700 (PDT) MIME-Version: 1.0 References: <4410FE99-43C7-4331-AB96-42ADB294AE30@apple.com> In-Reply-To: <4410FE99-43C7-4331-AB96-42ADB294AE30@apple.com> From: "Ayush Singh" Date: Fri, 17 Jun 2022 01:47:54 +0530 Message-ID: Subject: Re: [edk2-devel] Does edk2 also link to crt0-efi like GNU-EFI To: Andrew Fish Cc: edk2-devel-groups-io Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable 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.2Fhe= lp/topic/Run.20a.20function.20before.20.60lang_start.60/near/286376012) On Fri, Jun 17, 2022 at 12:06 AM Andrew Fish wrote: > > > > > On Jun 16, 2022, at 7:20 AM, Ayush Singh wro= te: > > > > 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 =3D` state= ment 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 typ= ical 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 functi= ons 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 A= utoGen.h and AutoGen.c file. This was the C code the build system autogener= ated to glue everything together. It manages gluing in the libs, abstractin= g the PCD implementation, and adding C constants for EFI_GUID values. > > Sorry I don=E2=80=99t know Rust yet. Is it possible to call C, EFIABI, fr= om Rust code? If yes maybe what you need is a Rust ModuleEntryPoint that ca= n call the C library constructor and a C EFIABI entry function for your Rus= t. I=E2=80=99m not sure how you manage dealing with C includes in Rust? Wit= h C++ you can just decorate them. > > I guess the other 100% Rust option is to know the INF is building Rust (I= NF 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/UefiAppl= icationEntryPoint/ApplicationEntryPoint.c > > Thanks, > > Andrew Fish > > > >=20 > > > > >