* [PATCH v2 26/34] MdePkg/BasePeCoff: Add LoongArch PE/Coff related code.
@ 2022-09-14 9:41 Chao Li
2022-09-23 7:18 ` Chao Li
2022-09-23 15:50 ` Michael D Kinney
0 siblings, 2 replies; 3+ messages in thread
From: Chao Li @ 2022-09-14 9:41 UTC (permalink / raw)
To: devel; +Cc: Michael D Kinney, Liming Gao, Zhiguang Liu, Baoqi Zhang
REF: https://bugzilla.tianocore.org/show_bug.cgi?id=4053
Add LoongArch image relocation.
Cc: Michael D Kinney <michael.d.kinney@intel.com>
Cc: Liming Gao <gaoliming@byosoft.com.cn>
Cc: Zhiguang Liu <zhiguang.liu@intel.com>
Signed-off-by: Chao Li <lichao@loongson.cn>
Co-authored-by: Baoqi Zhang <zhangbaoqi@loongson.cn>
---
MdePkg/Library/BasePeCoffLib/BasePeCoff.c | 3 +-
.../Library/BasePeCoffLib/BasePeCoffLib.inf | 5 +
.../Library/BasePeCoffLib/BasePeCoffLib.uni | 2 +
.../BasePeCoffLib/LoongArch/PeCoffLoaderEx.c | 137 ++++++++++++++++++
4 files changed, 146 insertions(+), 1 deletion(-)
create mode 100644 MdePkg/Library/BasePeCoffLib/LoongArch/PeCoffLoaderEx.c
diff --git a/MdePkg/Library/BasePeCoffLib/BasePeCoff.c b/MdePkg/Library/BasePeCoffLib/BasePeCoff.c
index 6d8d9faeb8..97a8aaf8c7 100644
--- a/MdePkg/Library/BasePeCoffLib/BasePeCoff.c
+++ b/MdePkg/Library/BasePeCoffLib/BasePeCoff.c
@@ -1,6 +1,6 @@
/** @file
Base PE/COFF loader supports loading any PE32/PE32+ or TE image, but
- only supports relocating IA32, x64, IPF, ARM, RISC-V and EBC images.
+ only supports relocating IA32, x64, IPF, ARM, RISC-V, LoongArch and EBC images.
Caution: This file requires additional review when modified.
This library will have external input - PE/COFF image.
@@ -18,6 +18,7 @@
Copyright (c) 2006 - 2019, Intel Corporation. All rights reserved.<BR>
Portions copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>
Portions Copyright (c) 2020, Hewlett Packard Enterprise Development LP. All rights reserved.<BR>
+ Portions Copyright (c) 2022, Loongson Technology Corporation Limited. All rights reserved.<BR>
SPDX-License-Identifier: BSD-2-Clause-Patent
**/
diff --git a/MdePkg/Library/BasePeCoffLib/BasePeCoffLib.inf b/MdePkg/Library/BasePeCoffLib/BasePeCoffLib.inf
index 110b6d5a09..3b8b8eb191 100644
--- a/MdePkg/Library/BasePeCoffLib/BasePeCoffLib.inf
+++ b/MdePkg/Library/BasePeCoffLib/BasePeCoffLib.inf
@@ -4,6 +4,7 @@
# The IA32 version library support loading IA32, X64 and EBC PE/COFF images.
# The X64 version library support loading IA32, X64 and EBC PE/COFF images.
# The RISC-V version library support loading RISC-V images.
+# The LoongArch version library support loading LoongArch images.
#
# Caution: This module requires additional review when modified.
# This library will have external input - PE/COFF image.
@@ -13,6 +14,7 @@
# Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
# Portions copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>
# Portions Copyright (c) 2020, Hewlett Packard Enterprise Development LP. All rights reserved.<BR>
+# Portions Copyright (c) 2022, Loongson Technology Corporation Limited. All rights reserved.<BR>
#
# SPDX-License-Identifier: BSD-2-Clause-Patent
#
@@ -46,6 +48,9 @@
[Sources.RISCV64]
RiscV/PeCoffLoaderEx.c
+[Sources.LOONGARCH64]
+ LoongArch/PeCoffLoaderEx.c
+
[Packages]
MdePkg/MdePkg.dec
diff --git a/MdePkg/Library/BasePeCoffLib/BasePeCoffLib.uni b/MdePkg/Library/BasePeCoffLib/BasePeCoffLib.uni
index 55417029f2..1f731344e1 100644
--- a/MdePkg/Library/BasePeCoffLib/BasePeCoffLib.uni
+++ b/MdePkg/Library/BasePeCoffLib/BasePeCoffLib.uni
@@ -5,6 +5,7 @@
// The IA32 version library support loading IA32, X64 and EBC PE/COFF images.
// The X64 version library support loading IA32, X64 and EBC PE/COFF images.
// The RISC-V version library support loading RISC-V32 and RISC-V64 PE/COFF images.
+// The LoongArch version library support loading LoongArch32 and LoongArch64 PE/COFF images.
//
// Caution: This module requires additional review when modified.
// This library will have external input - PE/COFF image.
@@ -14,6 +15,7 @@
// Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
// Portions copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>
// Portions Copyright (c) 2020, Hewlett Packard Enterprise Development LP. All rights reserved.<BR>
+// Portions Copyright (c) 2022, Loongson Technology Corporation Limited. All rights reserved.<BR>
//
// SPDX-License-Identifier: BSD-2-Clause-Patent
//
diff --git a/MdePkg/Library/BasePeCoffLib/LoongArch/PeCoffLoaderEx.c b/MdePkg/Library/BasePeCoffLib/LoongArch/PeCoffLoaderEx.c
new file mode 100644
index 0000000000..417096f334
--- /dev/null
+++ b/MdePkg/Library/BasePeCoffLib/LoongArch/PeCoffLoaderEx.c
@@ -0,0 +1,137 @@
+/** @file
+ PE/Coff loader for LoongArch PE image
+
+ Copyright (c) 2022, Loongson Technology Corporation Limited. All rights reserved.<BR>
+
+ SPDX-License-Identifier: BSD-2-Clause-Patent
+**/
+
+#include "BasePeCoffLibInternals.h"
+#include <Library/BaseLib.h>
+
+/**
+ Performs an LoongArch specific relocation fixup and is a no-op on other
+ instruction sets.
+
+ @param[in] Reloc Pointer to the relocation record.
+ @param[in, out] Fixup Pointer to the address to fix up.
+ @param[in, out] FixupData Pointer to a buffer to log the fixups.
+ @param[in] Adjust The offset to adjust the fixup.
+
+ @return Status code.
+
+**/
+RETURN_STATUS
+PeCoffLoaderRelocateImageEx (
+ IN UINT16 *Reloc,
+ IN OUT CHAR8 *Fixup,
+ IN OUT CHAR8 **FixupData,
+ IN UINT64 Adjust
+ )
+{
+ UINT8 RelocType;
+ UINT64 Value;
+ UINT64 Tmp1;
+ UINT64 Tmp2;
+
+ RelocType = (*Reloc) >> 12;
+ Value = 0;
+ Tmp1 = 0;
+ Tmp2 = 0;
+
+ switch (RelocType) {
+ case EFI_IMAGE_REL_BASED_LOONGARCH64_MARK_LA:
+ // The next four instructions are used to load a 64 bit address, relocate all of them
+ Value = (*(UINT32 *)Fixup & 0x1ffffe0) << 7 | // lu12i.w 20bits from bit5
+ (*((UINT32 *)Fixup + 1) & 0x3ffc00) >> 10; // ori 12bits from bit10
+ Tmp1 = *((UINT32 *)Fixup + 2) & 0x1ffffe0; // lu32i.d 20bits from bit5
+ Tmp2 = *((UINT32 *)Fixup + 3) & 0x3ffc00; // lu52i.d 12bits from bit10
+ Value = Value | (Tmp1 << 27) | (Tmp2 << 42);
+ Value += Adjust;
+
+ *(UINT32 *)Fixup = (*(UINT32 *)Fixup & ~0x1ffffe0) | (((Value >> 12) & 0xfffff) << 5);
+ if (*FixupData != NULL) {
+ *FixupData = ALIGN_POINTER (*FixupData, sizeof (UINT32));
+ *(UINT32 *)(*FixupData) = *(UINT32 *)Fixup;
+ *FixupData = *FixupData + sizeof (UINT32);
+ }
+
+ Fixup += sizeof (UINT32);
+ *(UINT32 *)Fixup = (*(UINT32 *)Fixup & ~0x3ffc00) | ((Value & 0xfff) << 10);
+ if (*FixupData != NULL) {
+ *FixupData = ALIGN_POINTER (*FixupData, sizeof (UINT32));
+ *(UINT32 *)(*FixupData) = *(UINT32 *)Fixup;
+ *FixupData = *FixupData + sizeof (UINT32);
+ }
+
+ Fixup += sizeof (UINT32);
+ *(UINT32 *)Fixup = (*(UINT32 *)Fixup & ~0x1ffffe0) | (((Value >> 32) & 0xfffff) << 5);
+ if (*FixupData != NULL) {
+ *FixupData = ALIGN_POINTER (*FixupData, sizeof (UINT32));
+ *(UINT32 *)(*FixupData) = *(UINT32 *)Fixup;
+ *FixupData = *FixupData + sizeof (UINT32);
+ }
+
+ Fixup += sizeof (UINT32);
+ *(UINT32 *)Fixup = (*(UINT32 *)Fixup & ~0x3ffc00) | (((Value >> 52) & 0xfff) << 10);
+ if (*FixupData != NULL) {
+ *FixupData = ALIGN_POINTER (*FixupData, sizeof (UINT32));
+ *(UINT32 *)(*FixupData) = *(UINT32 *)Fixup;
+ *FixupData = *FixupData + sizeof (UINT32);
+ }
+
+ break;
+ default:
+ return RETURN_UNSUPPORTED;
+ }
+
+ return RETURN_SUCCESS;
+}
+
+/**
+ Returns TRUE if the machine type of PE/COFF image is supported. Supported
+ does not mean the image can be executed it means the PE/COFF loader supports
+ loading and relocating of the image type. It's up to the caller to support
+ the entry point.
+
+ @param[in] Machine Machine type from the PE Header.
+
+ @return TRUE if this PE/COFF loader can load the image
+
+**/
+BOOLEAN
+PeCoffLoaderImageFormatSupported (
+ IN UINT16 Machine
+ )
+{
+ if (Machine == IMAGE_FILE_MACHINE_LOONGARCH64) {
+ return TRUE;
+ }
+
+ return FALSE;
+}
+
+/**
+ Performs an LOONGARCH-based specific re-relocation fixup and is a no-op on other
+ instruction sets. This is used to re-relocated the image into the EFI virtual
+ space for runtime calls.
+
+ @param[in] Reloc The pointer to the relocation record.
+ @param[in, out] Fixup The pointer to the address to fix up.
+ @param[in, out] FixupData The pointer to a buffer to log the fixups.
+ @param[in] Adjust The offset to adjust the fixup.
+
+ @return Status code.
+
+**/
+RETURN_STATUS
+PeHotRelocateImageEx (
+ IN UINT16 *Reloc,
+ IN OUT CHAR8 *Fixup,
+ IN OUT CHAR8 **FixupData,
+ IN UINT64 Adjust
+ )
+{
+ // To check
+ return PeCoffLoaderRelocateImageEx (Reloc, Fixup, FixupData, Adjust);
+}
--
2.27.0
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH v2 26/34] MdePkg/BasePeCoff: Add LoongArch PE/Coff related code.
2022-09-14 9:41 [PATCH v2 26/34] MdePkg/BasePeCoff: Add LoongArch PE/Coff related code Chao Li
@ 2022-09-23 7:18 ` Chao Li
2022-09-23 15:50 ` Michael D Kinney
1 sibling, 0 replies; 3+ messages in thread
From: Chao Li @ 2022-09-23 7:18 UTC (permalink / raw)
To: Michael D Kinney, Liming Gao, Zhiguang Liu, Baoqi Zhang
Cc: devel@edk2.groups.io
[-- Attachment #1: Type: text/plain, Size: 9170 bytes --]
Hi Mike, Liming and Zhiguang,
This patch has not been reviewed, would you please review it?
Thanks,
Chao
--------
On 9月 14 2022, at 5:41 下午, Chao Li <lichao@loongson.cn> wrote:
> REF: https://bugzilla.tianocore.org/show_bug.cgi?id=4053
>
> Add LoongArch image relocation.
> Cc: Michael D Kinney <michael.d.kinney@intel.com>
> Cc: Liming Gao <gaoliming@byosoft.com.cn>
> Cc: Zhiguang Liu <zhiguang.liu@intel.com>
>
> Signed-off-by: Chao Li <lichao@loongson.cn>
> Co-authored-by: Baoqi Zhang <zhangbaoqi@loongson.cn>
> ---
> MdePkg/Library/BasePeCoffLib/BasePeCoff.c | 3 +-
> .../Library/BasePeCoffLib/BasePeCoffLib.inf | 5 +
> .../Library/BasePeCoffLib/BasePeCoffLib.uni | 2 +
> .../BasePeCoffLib/LoongArch/PeCoffLoaderEx.c | 137 ++++++++++++++++++
> 4 files changed, 146 insertions(+), 1 deletion(-)
> create mode 100644 MdePkg/Library/BasePeCoffLib/LoongArch/PeCoffLoaderEx.c
>
> diff --git a/MdePkg/Library/BasePeCoffLib/BasePeCoff.c b/MdePkg/Library/BasePeCoffLib/BasePeCoff.c
> index 6d8d9faeb8..97a8aaf8c7 100644
> --- a/MdePkg/Library/BasePeCoffLib/BasePeCoff.c
> +++ b/MdePkg/Library/BasePeCoffLib/BasePeCoff.c
> @@ -1,6 +1,6 @@
> /** @file
>
> Base PE/COFF loader supports loading any PE32/PE32+ or TE image, but
> - only supports relocating IA32, x64, IPF, ARM, RISC-V and EBC images.
> + only supports relocating IA32, x64, IPF, ARM, RISC-V, LoongArch and EBC images.
>
>
> Caution: This file requires additional review when modified.
> This library will have external input - PE/COFF image.
> @@ -18,6 +18,7 @@
> Copyright (c) 2006 - 2019, Intel Corporation. All rights reserved.<BR>
>
> Portions copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>
> Portions Copyright (c) 2020, Hewlett Packard Enterprise Development LP. All rights reserved.<BR>
> + Portions Copyright (c) 2022, Loongson Technology Corporation Limited. All rights reserved.<BR>
> SPDX-License-Identifier: BSD-2-Clause-Patent
>
>
> **/
> diff --git a/MdePkg/Library/BasePeCoffLib/BasePeCoffLib.inf b/MdePkg/Library/BasePeCoffLib/BasePeCoffLib.inf
> index 110b6d5a09..3b8b8eb191 100644
> --- a/MdePkg/Library/BasePeCoffLib/BasePeCoffLib.inf
> +++ b/MdePkg/Library/BasePeCoffLib/BasePeCoffLib.inf
> @@ -4,6 +4,7 @@
> # The IA32 version library support loading IA32, X64 and EBC PE/COFF images.
>
> # The X64 version library support loading IA32, X64 and EBC PE/COFF images.
> # The RISC-V version library support loading RISC-V images.
> +# The LoongArch version library support loading LoongArch images.
> #
> # Caution: This module requires additional review when modified.
> # This library will have external input - PE/COFF image.
> @@ -13,6 +14,7 @@
> # Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
>
> # Portions copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>
> # Portions Copyright (c) 2020, Hewlett Packard Enterprise Development LP. All rights reserved.<BR>
> +# Portions Copyright (c) 2022, Loongson Technology Corporation Limited. All rights reserved.<BR>
> #
> # SPDX-License-Identifier: BSD-2-Clause-Patent
> #
> @@ -46,6 +48,9 @@
> [Sources.RISCV64]
>
> RiscV/PeCoffLoaderEx.c
>
>
> +[Sources.LOONGARCH64]
> + LoongArch/PeCoffLoaderEx.c
> +
> [Packages]
> MdePkg/MdePkg.dec
>
>
> diff --git a/MdePkg/Library/BasePeCoffLib/BasePeCoffLib.uni b/MdePkg/Library/BasePeCoffLib/BasePeCoffLib.uni
> index 55417029f2..1f731344e1 100644
> --- a/MdePkg/Library/BasePeCoffLib/BasePeCoffLib.uni
> +++ b/MdePkg/Library/BasePeCoffLib/BasePeCoffLib.uni
> @@ -5,6 +5,7 @@
> // The IA32 version library support loading IA32, X64 and EBC PE/COFF images.
>
> // The X64 version library support loading IA32, X64 and EBC PE/COFF images.
> // The RISC-V version library support loading RISC-V32 and RISC-V64 PE/COFF images.
> +// The LoongArch version library support loading LoongArch32 and LoongArch64 PE/COFF images.
> //
> // Caution: This module requires additional review when modified.
> // This library will have external input - PE/COFF image.
> @@ -14,6 +15,7 @@
> // Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
>
> // Portions copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>
> // Portions Copyright (c) 2020, Hewlett Packard Enterprise Development LP. All rights reserved.<BR>
> +// Portions Copyright (c) 2022, Loongson Technology Corporation Limited. All rights reserved.<BR>
> //
> // SPDX-License-Identifier: BSD-2-Clause-Patent
> //
> diff --git a/MdePkg/Library/BasePeCoffLib/LoongArch/PeCoffLoaderEx.c b/MdePkg/Library/BasePeCoffLib/LoongArch/PeCoffLoaderEx.c
> new file mode 100644
> index 0000000000..417096f334
> --- /dev/null
> +++ b/MdePkg/Library/BasePeCoffLib/LoongArch/PeCoffLoaderEx.c
> @@ -0,0 +1,137 @@
> +/** @file
>
> + PE/Coff loader for LoongArch PE image
> +
> + Copyright (c) 2022, Loongson Technology Corporation Limited. All rights reserved.<BR>
> +
> + SPDX-License-Identifier: BSD-2-Clause-Patent
> +**/
> +
> +#include "BasePeCoffLibInternals.h"
> +#include <Library/BaseLib.h>
> +
> +/**
> + Performs an LoongArch specific relocation fixup and is a no-op on other
> + instruction sets.
> +
> + @param[in] Reloc Pointer to the relocation record.
> + @param[in, out] Fixup Pointer to the address to fix up.
> + @param[in, out] FixupData Pointer to a buffer to log the fixups.
> + @param[in] Adjust The offset to adjust the fixup.
> +
> + @return Status code.
> +
> +**/
> +RETURN_STATUS
> +PeCoffLoaderRelocateImageEx (
> + IN UINT16 *Reloc,
> + IN OUT CHAR8 *Fixup,
> + IN OUT CHAR8 **FixupData,
> + IN UINT64 Adjust
> + )
> +{
> + UINT8 RelocType;
> + UINT64 Value;
> + UINT64 Tmp1;
> + UINT64 Tmp2;
> +
> + RelocType = (*Reloc) >> 12;
> + Value = 0;
> + Tmp1 = 0;
> + Tmp2 = 0;
> +
> + switch (RelocType) {
> + case EFI_IMAGE_REL_BASED_LOONGARCH64_MARK_LA:
> + // The next four instructions are used to load a 64 bit address, relocate all of them
> + Value = (*(UINT32 *)Fixup & 0x1ffffe0) << 7 | // lu12i.w 20bits from bit5
> + (*((UINT32 *)Fixup + 1) & 0x3ffc00) >> 10; // ori 12bits from bit10
> + Tmp1 = *((UINT32 *)Fixup + 2) & 0x1ffffe0; // lu32i.d 20bits from bit5
> + Tmp2 = *((UINT32 *)Fixup + 3) & 0x3ffc00; // lu52i.d 12bits from bit10
> + Value = Value | (Tmp1 << 27) | (Tmp2 << 42);
> + Value += Adjust;
> +
> + *(UINT32 *)Fixup = (*(UINT32 *)Fixup & ~0x1ffffe0) | (((Value >> 12) & 0xfffff) << 5);
> + if (*FixupData != NULL) {
> + *FixupData = ALIGN_POINTER (*FixupData, sizeof (UINT32));
> + *(UINT32 *)(*FixupData) = *(UINT32 *)Fixup;
> + *FixupData = *FixupData + sizeof (UINT32);
> + }
> +
> + Fixup += sizeof (UINT32);
> + *(UINT32 *)Fixup = (*(UINT32 *)Fixup & ~0x3ffc00) | ((Value & 0xfff) << 10);
> + if (*FixupData != NULL) {
> + *FixupData = ALIGN_POINTER (*FixupData, sizeof (UINT32));
> + *(UINT32 *)(*FixupData) = *(UINT32 *)Fixup;
> + *FixupData = *FixupData + sizeof (UINT32);
> + }
> +
> + Fixup += sizeof (UINT32);
> + *(UINT32 *)Fixup = (*(UINT32 *)Fixup & ~0x1ffffe0) | (((Value >> 32) & 0xfffff) << 5);
> + if (*FixupData != NULL) {
> + *FixupData = ALIGN_POINTER (*FixupData, sizeof (UINT32));
> + *(UINT32 *)(*FixupData) = *(UINT32 *)Fixup;
> + *FixupData = *FixupData + sizeof (UINT32);
> + }
> +
> + Fixup += sizeof (UINT32);
> + *(UINT32 *)Fixup = (*(UINT32 *)Fixup & ~0x3ffc00) | (((Value >> 52) & 0xfff) << 10);
> + if (*FixupData != NULL) {
> + *FixupData = ALIGN_POINTER (*FixupData, sizeof (UINT32));
> + *(UINT32 *)(*FixupData) = *(UINT32 *)Fixup;
> + *FixupData = *FixupData + sizeof (UINT32);
> + }
> +
> + break;
> + default:
> + return RETURN_UNSUPPORTED;
> + }
> +
> + return RETURN_SUCCESS;
> +}
> +
> +/**
> + Returns TRUE if the machine type of PE/COFF image is supported. Supported
> + does not mean the image can be executed it means the PE/COFF loader supports
> + loading and relocating of the image type. It's up to the caller to support
> + the entry point.
> +
> + @param[in] Machine Machine type from the PE Header.
> +
> + @return TRUE if this PE/COFF loader can load the image
> +
> +**/
> +BOOLEAN
> +PeCoffLoaderImageFormatSupported (
> + IN UINT16 Machine
> + )
> +{
> + if (Machine == IMAGE_FILE_MACHINE_LOONGARCH64) {
> + return TRUE;
> + }
> +
> + return FALSE;
> +}
> +
> +/**
> + Performs an LOONGARCH-based specific re-relocation fixup and is a no-op on other
> + instruction sets. This is used to re-relocated the image into the EFI virtual
> + space for runtime calls.
> +
> + @param[in] Reloc The pointer to the relocation record.
> + @param[in, out] Fixup The pointer to the address to fix up.
> + @param[in, out] FixupData The pointer to a buffer to log the fixups.
> + @param[in] Adjust The offset to adjust the fixup.
> +
> + @return Status code.
> +
> +**/
> +RETURN_STATUS
> +PeHotRelocateImageEx (
> + IN UINT16 *Reloc,
> + IN OUT CHAR8 *Fixup,
> + IN OUT CHAR8 **FixupData,
> + IN UINT64 Adjust
> + )
> +{
> + // To check
> + return PeCoffLoaderRelocateImageEx (Reloc, Fixup, FixupData, Adjust);
> +}
> --
> 2.27.0
>
[-- Attachment #2: Type: text/html, Size: 12051 bytes --]
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH v2 26/34] MdePkg/BasePeCoff: Add LoongArch PE/Coff related code.
2022-09-14 9:41 [PATCH v2 26/34] MdePkg/BasePeCoff: Add LoongArch PE/Coff related code Chao Li
2022-09-23 7:18 ` Chao Li
@ 2022-09-23 15:50 ` Michael D Kinney
1 sibling, 0 replies; 3+ messages in thread
From: Michael D Kinney @ 2022-09-23 15:50 UTC (permalink / raw)
To: Chao Li, devel@edk2.groups.io, Kinney, Michael D
Cc: Gao, Liming, Liu, Zhiguang, Baoqi Zhang
Reviewed-by: Michael D Kinney <michael.d.kinney@intel.com>
> -----Original Message-----
> From: Chao Li <lichao@loongson.cn>
> Sent: Wednesday, September 14, 2022 2:42 AM
> To: devel@edk2.groups.io
> Cc: Kinney, Michael D <michael.d.kinney@intel.com>; Gao, Liming <gaoliming@byosoft.com.cn>; Liu, Zhiguang <zhiguang.liu@intel.com>;
> Baoqi Zhang <zhangbaoqi@loongson.cn>
> Subject: [PATCH v2 26/34] MdePkg/BasePeCoff: Add LoongArch PE/Coff related code.
>
> REF: https://bugzilla.tianocore.org/show_bug.cgi?id=4053
>
> Add LoongArch image relocation.
>
> Cc: Michael D Kinney <michael.d.kinney@intel.com>
> Cc: Liming Gao <gaoliming@byosoft.com.cn>
> Cc: Zhiguang Liu <zhiguang.liu@intel.com>
>
> Signed-off-by: Chao Li <lichao@loongson.cn>
> Co-authored-by: Baoqi Zhang <zhangbaoqi@loongson.cn>
> ---
> MdePkg/Library/BasePeCoffLib/BasePeCoff.c | 3 +-
> .../Library/BasePeCoffLib/BasePeCoffLib.inf | 5 +
> .../Library/BasePeCoffLib/BasePeCoffLib.uni | 2 +
> .../BasePeCoffLib/LoongArch/PeCoffLoaderEx.c | 137 ++++++++++++++++++
> 4 files changed, 146 insertions(+), 1 deletion(-)
> create mode 100644 MdePkg/Library/BasePeCoffLib/LoongArch/PeCoffLoaderEx.c
>
> diff --git a/MdePkg/Library/BasePeCoffLib/BasePeCoff.c b/MdePkg/Library/BasePeCoffLib/BasePeCoff.c
> index 6d8d9faeb8..97a8aaf8c7 100644
> --- a/MdePkg/Library/BasePeCoffLib/BasePeCoff.c
> +++ b/MdePkg/Library/BasePeCoffLib/BasePeCoff.c
> @@ -1,6 +1,6 @@
> /** @file
>
> Base PE/COFF loader supports loading any PE32/PE32+ or TE image, but
>
> - only supports relocating IA32, x64, IPF, ARM, RISC-V and EBC images.
>
> + only supports relocating IA32, x64, IPF, ARM, RISC-V, LoongArch and EBC images.
>
>
>
> Caution: This file requires additional review when modified.
>
> This library will have external input - PE/COFF image.
>
> @@ -18,6 +18,7 @@
> Copyright (c) 2006 - 2019, Intel Corporation. All rights reserved.<BR>
>
> Portions copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>
>
> Portions Copyright (c) 2020, Hewlett Packard Enterprise Development LP. All rights reserved.<BR>
>
> + Portions Copyright (c) 2022, Loongson Technology Corporation Limited. All rights reserved.<BR>
>
> SPDX-License-Identifier: BSD-2-Clause-Patent
>
>
>
> **/
>
> diff --git a/MdePkg/Library/BasePeCoffLib/BasePeCoffLib.inf b/MdePkg/Library/BasePeCoffLib/BasePeCoffLib.inf
> index 110b6d5a09..3b8b8eb191 100644
> --- a/MdePkg/Library/BasePeCoffLib/BasePeCoffLib.inf
> +++ b/MdePkg/Library/BasePeCoffLib/BasePeCoffLib.inf
> @@ -4,6 +4,7 @@
> # The IA32 version library support loading IA32, X64 and EBC PE/COFF images.
>
> # The X64 version library support loading IA32, X64 and EBC PE/COFF images.
>
> # The RISC-V version library support loading RISC-V images.
>
> +# The LoongArch version library support loading LoongArch images.
>
> #
>
> # Caution: This module requires additional review when modified.
>
> # This library will have external input - PE/COFF image.
>
> @@ -13,6 +14,7 @@
> # Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
>
> # Portions copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>
>
> # Portions Copyright (c) 2020, Hewlett Packard Enterprise Development LP. All rights reserved.<BR>
>
> +# Portions Copyright (c) 2022, Loongson Technology Corporation Limited. All rights reserved.<BR>
>
> #
>
> # SPDX-License-Identifier: BSD-2-Clause-Patent
>
> #
>
> @@ -46,6 +48,9 @@
> [Sources.RISCV64]
>
> RiscV/PeCoffLoaderEx.c
>
>
>
> +[Sources.LOONGARCH64]
>
> + LoongArch/PeCoffLoaderEx.c
>
> +
>
> [Packages]
>
> MdePkg/MdePkg.dec
>
>
>
> diff --git a/MdePkg/Library/BasePeCoffLib/BasePeCoffLib.uni b/MdePkg/Library/BasePeCoffLib/BasePeCoffLib.uni
> index 55417029f2..1f731344e1 100644
> --- a/MdePkg/Library/BasePeCoffLib/BasePeCoffLib.uni
> +++ b/MdePkg/Library/BasePeCoffLib/BasePeCoffLib.uni
> @@ -5,6 +5,7 @@
> // The IA32 version library support loading IA32, X64 and EBC PE/COFF images.
>
> // The X64 version library support loading IA32, X64 and EBC PE/COFF images.
>
> // The RISC-V version library support loading RISC-V32 and RISC-V64 PE/COFF images.
>
> +// The LoongArch version library support loading LoongArch32 and LoongArch64 PE/COFF images.
>
> //
>
> // Caution: This module requires additional review when modified.
>
> // This library will have external input - PE/COFF image.
>
> @@ -14,6 +15,7 @@
> // Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
>
> // Portions copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>
>
> // Portions Copyright (c) 2020, Hewlett Packard Enterprise Development LP. All rights reserved.<BR>
>
> +// Portions Copyright (c) 2022, Loongson Technology Corporation Limited. All rights reserved.<BR>
>
> //
>
> // SPDX-License-Identifier: BSD-2-Clause-Patent
>
> //
>
> diff --git a/MdePkg/Library/BasePeCoffLib/LoongArch/PeCoffLoaderEx.c b/MdePkg/Library/BasePeCoffLib/LoongArch/PeCoffLoaderEx.c
> new file mode 100644
> index 0000000000..417096f334
> --- /dev/null
> +++ b/MdePkg/Library/BasePeCoffLib/LoongArch/PeCoffLoaderEx.c
> @@ -0,0 +1,137 @@
> +/** @file
>
> + PE/Coff loader for LoongArch PE image
>
> +
>
> + Copyright (c) 2022, Loongson Technology Corporation Limited. All rights reserved.<BR>
>
> +
>
> + SPDX-License-Identifier: BSD-2-Clause-Patent
>
> +**/
>
> +
>
> +#include "BasePeCoffLibInternals.h"
>
> +#include <Library/BaseLib.h>
>
> +
>
> +/**
>
> + Performs an LoongArch specific relocation fixup and is a no-op on other
>
> + instruction sets.
>
> +
>
> + @param[in] Reloc Pointer to the relocation record.
>
> + @param[in, out] Fixup Pointer to the address to fix up.
>
> + @param[in, out] FixupData Pointer to a buffer to log the fixups.
>
> + @param[in] Adjust The offset to adjust the fixup.
>
> +
>
> + @return Status code.
>
> +
>
> +**/
>
> +RETURN_STATUS
>
> +PeCoffLoaderRelocateImageEx (
>
> + IN UINT16 *Reloc,
>
> + IN OUT CHAR8 *Fixup,
>
> + IN OUT CHAR8 **FixupData,
>
> + IN UINT64 Adjust
>
> + )
>
> +{
>
> + UINT8 RelocType;
>
> + UINT64 Value;
>
> + UINT64 Tmp1;
>
> + UINT64 Tmp2;
>
> +
>
> + RelocType = (*Reloc) >> 12;
>
> + Value = 0;
>
> + Tmp1 = 0;
>
> + Tmp2 = 0;
>
> +
>
> + switch (RelocType) {
>
> + case EFI_IMAGE_REL_BASED_LOONGARCH64_MARK_LA:
>
> + // The next four instructions are used to load a 64 bit address, relocate all of them
>
> + Value = (*(UINT32 *)Fixup & 0x1ffffe0) << 7 | // lu12i.w 20bits from bit5
>
> + (*((UINT32 *)Fixup + 1) & 0x3ffc00) >> 10; // ori 12bits from bit10
>
> + Tmp1 = *((UINT32 *)Fixup + 2) & 0x1ffffe0; // lu32i.d 20bits from bit5
>
> + Tmp2 = *((UINT32 *)Fixup + 3) & 0x3ffc00; // lu52i.d 12bits from bit10
>
> + Value = Value | (Tmp1 << 27) | (Tmp2 << 42);
>
> + Value += Adjust;
>
> +
>
> + *(UINT32 *)Fixup = (*(UINT32 *)Fixup & ~0x1ffffe0) | (((Value >> 12) & 0xfffff) << 5);
>
> + if (*FixupData != NULL) {
>
> + *FixupData = ALIGN_POINTER (*FixupData, sizeof (UINT32));
>
> + *(UINT32 *)(*FixupData) = *(UINT32 *)Fixup;
>
> + *FixupData = *FixupData + sizeof (UINT32);
>
> + }
>
> +
>
> + Fixup += sizeof (UINT32);
>
> + *(UINT32 *)Fixup = (*(UINT32 *)Fixup & ~0x3ffc00) | ((Value & 0xfff) << 10);
>
> + if (*FixupData != NULL) {
>
> + *FixupData = ALIGN_POINTER (*FixupData, sizeof (UINT32));
>
> + *(UINT32 *)(*FixupData) = *(UINT32 *)Fixup;
>
> + *FixupData = *FixupData + sizeof (UINT32);
>
> + }
>
> +
>
> + Fixup += sizeof (UINT32);
>
> + *(UINT32 *)Fixup = (*(UINT32 *)Fixup & ~0x1ffffe0) | (((Value >> 32) & 0xfffff) << 5);
>
> + if (*FixupData != NULL) {
>
> + *FixupData = ALIGN_POINTER (*FixupData, sizeof (UINT32));
>
> + *(UINT32 *)(*FixupData) = *(UINT32 *)Fixup;
>
> + *FixupData = *FixupData + sizeof (UINT32);
>
> + }
>
> +
>
> + Fixup += sizeof (UINT32);
>
> + *(UINT32 *)Fixup = (*(UINT32 *)Fixup & ~0x3ffc00) | (((Value >> 52) & 0xfff) << 10);
>
> + if (*FixupData != NULL) {
>
> + *FixupData = ALIGN_POINTER (*FixupData, sizeof (UINT32));
>
> + *(UINT32 *)(*FixupData) = *(UINT32 *)Fixup;
>
> + *FixupData = *FixupData + sizeof (UINT32);
>
> + }
>
> +
>
> + break;
>
> + default:
>
> + return RETURN_UNSUPPORTED;
>
> + }
>
> +
>
> + return RETURN_SUCCESS;
>
> +}
>
> +
>
> +/**
>
> + Returns TRUE if the machine type of PE/COFF image is supported. Supported
>
> + does not mean the image can be executed it means the PE/COFF loader supports
>
> + loading and relocating of the image type. It's up to the caller to support
>
> + the entry point.
>
> +
>
> + @param[in] Machine Machine type from the PE Header.
>
> +
>
> + @return TRUE if this PE/COFF loader can load the image
>
> +
>
> +**/
>
> +BOOLEAN
>
> +PeCoffLoaderImageFormatSupported (
>
> + IN UINT16 Machine
>
> + )
>
> +{
>
> + if (Machine == IMAGE_FILE_MACHINE_LOONGARCH64) {
>
> + return TRUE;
>
> + }
>
> +
>
> + return FALSE;
>
> +}
>
> +
>
> +/**
>
> + Performs an LOONGARCH-based specific re-relocation fixup and is a no-op on other
>
> + instruction sets. This is used to re-relocated the image into the EFI virtual
>
> + space for runtime calls.
>
> +
>
> + @param[in] Reloc The pointer to the relocation record.
>
> + @param[in, out] Fixup The pointer to the address to fix up.
>
> + @param[in, out] FixupData The pointer to a buffer to log the fixups.
>
> + @param[in] Adjust The offset to adjust the fixup.
>
> +
>
> + @return Status code.
>
> +
>
> +**/
>
> +RETURN_STATUS
>
> +PeHotRelocateImageEx (
>
> + IN UINT16 *Reloc,
>
> + IN OUT CHAR8 *Fixup,
>
> + IN OUT CHAR8 **FixupData,
>
> + IN UINT64 Adjust
>
> + )
>
> +{
>
> + // To check
>
> + return PeCoffLoaderRelocateImageEx (Reloc, Fixup, FixupData, Adjust);
>
> +}
>
> --
> 2.27.0
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2022-09-23 15:50 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-09-14 9:41 [PATCH v2 26/34] MdePkg/BasePeCoff: Add LoongArch PE/Coff related code Chao Li
2022-09-23 7:18 ` Chao Li
2022-09-23 15:50 ` Michael D Kinney
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox