public inbox for kvm@vger.kernel.org
 help / color / mirror / Atom feed
From: Krish Sadhukhan <krish.sadhukhan@oracle.com>
To: Varad Gautam <varadgautam@gmail.com>,
	Zixuan Wang <zixuanwang@google.com>,
	Nadav Amit <nadav.amit@gmail.com>, Marc Orr <marcorr@google.com>,
	Joerg Roedel <jroedel@suse.de>, kvm list <kvm@vger.kernel.org>,
	Linux Virtualization <virtualization@lists.linux-foundation.org>,
	Paolo Bonzini <pbonzini@redhat.com>,
	Andrew Jones <drjones@redhat.com>,
	bp@suse.de, Thomas.Lendacky@amd.com, brijesh.singh@amd.com,
	Hyunwook Baek <baekhw@google.com>,
	Erdem Aktas <erdemaktas@google.com>,
	Tom Roeder <tmroeder@google.com>
Cc: Varad Gautam <varad.gautam@suse.com>
Subject: Re: [kvm-unit-tests PATCH v2 4/6] x86: efi_main: Self-relocate ELF .dynamic addresses
Date: Tue, 24 Aug 2021 15:10:50 -0700	[thread overview]
Message-ID: <430f4a8a-4eff-5f32-3dd9-103e8e5b354c@oracle.com> (raw)
In-Reply-To: <20210819113400.26516-5-varad.gautam@suse.com>


On 8/19/21 4:33 AM, Varad Gautam wrote:
> EFI expects a relocatable PE, and the loader will patch in the
> relocations from the COFF.
>
> Since we are wrapping an ELF into a PE here, the EFI loader will
> not handle ELF relocations, and we need to patch the ELF .dynamic
> section manually on early boot.
>
> Signed-off-by: Varad Gautam<varad.gautam@suse.com>
> ---
>   x86/efi_main.c | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++
>   1 file changed, 63 insertions(+)
>
> diff --git a/x86/efi_main.c b/x86/efi_main.c
> index 237d4e7..be3f9ab 100644
> --- a/x86/efi_main.c
> +++ b/x86/efi_main.c
> @@ -1,9 +1,13 @@
>   #include <alloc_phys.h>
>   #include <linux/uefi.h>
> +#include <elf.h>
>   
>   unsigned long __efiapi efi_main(efi_handle_t handle, efi_system_table_t *sys_tab);
>   efi_system_table_t *efi_system_table = NULL;
>   
> +extern char ImageBase;
> +extern char _DYNAMIC;
> +
>   static void efi_free_pool(void *ptr)
>   {
>   	efi_bs_call(free_pool, ptr);
> @@ -93,11 +97,70 @@ static efi_status_t exit_efi(void *handle)
>   	return EFI_SUCCESS;
>   }
>   
> +static efi_status_t elf_reloc(unsigned long image_base, unsigned long dynamic)


Since this function is only relocating the dynamic section, we should 
probably name it something like elf_reloc_dyn().

> +{
> +	long relsz = 0, relent = 0;
> +	Elf64_Rel *rel = 0;
> +	Elf64_Dyn *dyn = (Elf64_Dyn *) dynamic;
> +	unsigned long *addr;
> +	int i;
> +
> +	for (i = 0; dyn[i].d_tag != DT_NULL; i++) {
> +		switch (dyn[i].d_tag) {
> +		case DT_RELA:
> +			rel = (Elf64_Rel *)
> +				((unsigned long) dyn[i].d_un.d_ptr + image_base);
> +			break;
> +		case DT_RELASZ:
> +			relsz = dyn[i].d_un.d_val;
> +			break;
> +		case DT_RELAENT:
> +			relent = dyn[i].d_un.d_val;
> +			break;
> +		default:
> +			break;
> +		}
> +	}
> +
> +	if (!rel && relent == 0)
> +		return EFI_SUCCESS;
> +
> +	if (!rel || relent == 0)
> +		return EFI_LOAD_ERROR;
> +
> +	while (relsz > 0) {
> +		/* apply the relocs */
> +		switch (ELF64_R_TYPE (rel->r_info)) {
> +		case R_X86_64_NONE:
> +			break;
> +		case R_X86_64_RELATIVE:
> +			addr = (unsigned long *) (image_base + rel->r_offset);
> +			*addr += image_base;
> +			break;
> +		default:
> +			break;
> +		}
> +		rel = (Elf64_Rel *) ((char *) rel + relent);
> +		relsz -= relent;
> +	}
> +	return EFI_SUCCESS;
> +}
> +
>   unsigned long __efiapi efi_main(efi_handle_t handle, efi_system_table_t *sys_tab)
>   {
> +	unsigned long image_base, dyn;
>   	efi_system_table = sys_tab;
>   
>   	exit_efi(handle);
>   
> +	image_base = (unsigned long) &ImageBase;
> +	dyn = image_base + (unsigned long) &_DYNAMIC;
> +
> +	/* The EFI loader does not handle ELF relocations, so fixup
> +	 * .dynamic addresses before proceeding any further. */
> +	elf_reloc(image_base, dyn);
> +
> +	start64();


Should this call to start64() be moved to your next patch because the 
function needs to be fixed and you are fixing it in there ?

> +
>   	return 0;
>   }

  reply	other threads:[~2021-08-24 22:11 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-08-19 11:33 [kvm-unit-tests PATCH v2 0/6] Initial x86_64 UEFI support Varad Gautam
2021-08-19 11:33 ` [kvm-unit-tests PATCH v2 1/6] x86: Build tests as PE objects for the EFI loader Varad Gautam
2021-08-19 11:33 ` [kvm-unit-tests PATCH v2 2/6] x86: Call efi_main from _efi_pe_entry Varad Gautam
2021-08-24 22:08   ` Krish Sadhukhan
2021-08-19 11:33 ` [kvm-unit-tests PATCH v2 3/6] x86: efi_main: Get EFI memory map and exit boot services Varad Gautam
2021-08-24 22:10   ` Krish Sadhukhan
2021-08-19 11:33 ` [kvm-unit-tests PATCH v2 4/6] x86: efi_main: Self-relocate ELF .dynamic addresses Varad Gautam
2021-08-24 22:10   ` Krish Sadhukhan [this message]
2021-08-19 11:33 ` [kvm-unit-tests PATCH v2 5/6] cstart64.S: x86_64 bootstrapping after exiting EFI Varad Gautam
2021-08-24 22:11   ` Krish Sadhukhan
2021-08-19 11:34 ` [kvm-unit-tests PATCH v2 6/6] x86 UEFI: Convert x86 test cases to PIC Varad Gautam
2021-08-24 22:12   ` Krish Sadhukhan
2021-08-21  0:01 ` [kvm-unit-tests PATCH v2 0/6] Initial x86_64 UEFI support Sean Christopherson
2021-08-21  0:42   ` Zixuan Wang

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-all 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=430f4a8a-4eff-5f32-3dd9-103e8e5b354c@oracle.com \
    --to=krish.sadhukhan@oracle.com \
    --cc=Thomas.Lendacky@amd.com \
    --cc=baekhw@google.com \
    --cc=bp@suse.de \
    --cc=brijesh.singh@amd.com \
    --cc=drjones@redhat.com \
    --cc=erdemaktas@google.com \
    --cc=jroedel@suse.de \
    --cc=kvm@vger.kernel.org \
    --cc=marcorr@google.com \
    --cc=nadav.amit@gmail.com \
    --cc=pbonzini@redhat.com \
    --cc=tmroeder@google.com \
    --cc=varad.gautam@suse.com \
    --cc=varadgautam@gmail.com \
    --cc=virtualization@lists.linux-foundation.org \
    --cc=zixuanwang@google.com \
    /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