[PATCH] BaseTools/GenFw: Enhance to add export table in PE-COFF


Lixia Huang <lisa.huang@...>
 

REF: https://bugzilla.tianocore.org/show_bug.cgi?id=3D3802=0D

Since PRM module needs to support export table in PE-COFF, we'll
enhance GenFw tool to support this.

Add one export flag in GenFw tool. If export flag is set:
Step1: Scan ELF symbol table based on PRM module descriptor to get
descriptor offset address;
Step2: Find PRM handlers number and name in COFF file based on the
address from step1;
Step3: Write PRM info such as handler name and export RVA into COFF
export table.

Cc: Liming Gao <gaoliming@byosoft.com.cn>
Cc: Bob Feng <bob.c.feng@intel.com>
Cc: Yuwei Chen <yuwei.chen@intel.com>
Signed-off-by: Lixia Huang <lisa.huang@intel.com>
---
BaseTools/Source/C/GenFw/Elf64Convert.c | 254 +++++++++++++++++-
BaseTools/Source/C/GenFw/ElfConvert.c | 10 +
BaseTools/Source/C/GenFw/ElfConvert.h | 42 ++-
BaseTools/Source/C/GenFw/GenFw.c | 11 +-
.../C/Include/IndustryStandard/PeImage.h | 7 +
5 files changed, 318 insertions(+), 6 deletions(-)

diff --git a/BaseTools/Source/C/GenFw/Elf64Convert.c b/BaseTools/Source/C/G=
enFw/Elf64Convert.c
index 0bb3ead228..0079507356 100644
--- a/BaseTools/Source/C/GenFw/Elf64Convert.c
+++ b/BaseTools/Source/C/GenFw/Elf64Convert.c
@@ -56,6 +56,18 @@ WriteDebug64 (
VOID=0D
);=0D
=0D
+STATIC=0D
+VOID=0D
+ScanSymbol64 (=0D
+ VOID=0D
+ );=0D
+=0D
+STATIC=0D
+VOID=0D
+WriteExport64 (=0D
+ VOID=0D
+ );=0D
+=0D
STATIC=0D
VOID=0D
SetImageSize64 (=0D
@@ -122,7 +134,7 @@ STATIC UINT32 mDataOffset;
STATIC UINT32 mHiiRsrcOffset;=0D
STATIC UINT32 mRelocOffset;=0D
STATIC UINT32 mDebugOffset;=0D
-=0D
+STATIC UINT32 mExportOffset;=0D
//=0D
// Used for RISC-V relocations.=0D
//=0D
@@ -132,6 +144,14 @@ STATIC Elf64_Half mRiscVPass1SymSecIndex =3D 0;
STATIC INT32 mRiscVPass1Offset;=0D
STATIC INT32 mRiscVPass1GotFixup;=0D
=0D
+//=0D
+// Used for Export section.=0D
+//=0D
+STATIC UINT32 mExportSize;=0D
+STATIC UINT32 mExportRVA[PRM_MODULE_EXPORT_SYMBOL_NUM];=0D
+STATIC UINT32 mExportSymNum;=0D
+STATIC CHAR8 mExportSymName[PRM_MODULE_EXPORT_SYMBOL_NUM][PRM_HANDLE=
R_NAME_MAXIMUM_LENGTH];=0D
+=0D
//=0D
// Initialization Function=0D
//=0D
@@ -200,6 +220,10 @@ InitializeElf64 (
ElfFunctions->SetImageSize =3D SetImageSize64;=0D
ElfFunctions->CleanUp =3D CleanUp64;=0D
=0D
+ if (mExportFlag) {=0D
+ ElfFunctions->ScanSymbol =3D ScanSymbol64;=0D
+ ElfFunctions->WriteExport =3D WriteExport64;=0D
+ }=0D
return TRUE;=0D
}=0D
=0D
@@ -263,6 +287,17 @@ IsHiiRsrcShdr (
return (BOOLEAN) (strcmp((CHAR8*)mEhdr + Namedr->sh_offset + Shdr->sh_na=
me, ELF_HII_SECTION_NAME) =3D=3D 0);=0D
}=0D
=0D
+STATIC=0D
+BOOLEAN=0D
+IsSymbolShdr (=0D
+ Elf_Shdr *Shdr=0D
+ )=0D
+{=0D
+ Elf_Shdr *Namehdr =3D GetShdrByIndex(mEhdr->e_shstrndx);=0D
+=0D
+ return (BOOLEAN) (strcmp((CHAR8*)mEhdr + Namehdr->sh_offset + Shdr->sh_n=
ame, ELF_SYMBOL_SECTION_NAME) =3D=3D 0);=0D
+}=0D
+=0D
STATIC=0D
BOOLEAN=0D
IsDataShdr (=0D
@@ -335,6 +370,38 @@ GetSymName (
return StrtabContents + Sym->st_name;=0D
}=0D
=0D
+//=0D
+// Get Prm Handler number and name=0D
+//=0D
+STATIC=0D
+VOID=0D
+FindPrmHandler (=0D
+ UINT64 Offset=0D
+ )=0D
+{=0D
+ PRM_MODULE_EXPORT_DESCRIPTOR_STRUCT_HEADER *PrmExport;=0D
+ UINT32 NameOffset;=0D
+ UINT32 HandlerNum;=0D
+ UINT32 Index;=0D
+ UINT8 SymName[PRM_HANDLER_NAME_MAXIMUM_LENGTH];=0D
+=0D
+ PrmExport =3D (PRM_MODULE_EXPORT_DESCRIPTOR_STRUCT_HEADER*)(mCoffFile + =
Offset);=0D
+ NameOffset =3D sizeof(PRM_MODULE_EXPORT_DESCRIPTOR_STRUCT_HEADER) + size=
of(EFI_GUID);=0D
+=0D
+ for (HandlerNum =3D 0; HandlerNum < PrmExport->NumberPrmHandlers; Handle=
rNum++) {=0D
+ for (Index =3D 0; Index < PRM_HANDLER_NAME_MAXIMUM_LENGTH; Index++) {=
=0D
+ SymName[Index] =3D *((UINT8 *)PrmExport + NameOffset + Index);=0D
+ if (SymName[Index] =3D=3D 0) {=0D
+ break;=0D
+ }=0D
+ }=0D
+=0D
+ strcpy(mExportSymName[mExportSymNum], (CHAR8*)SymName);=0D
+ NameOffset +=3D PRM_HANDLER_NAME_MAXIMUM_LENGTH + sizeof(EFI_GUID);=0D
+ mExportSymNum ++;=0D
+ }=0D
+}=0D
+=0D
//=0D
// Find the ELF section hosting the GOT from an ELF Rva=0D
// of a single GOT entry. Normally, GOT is placed in=0D
@@ -717,6 +784,7 @@ ScanSections64 (
UINT32 CoffEntry;=0D
UINT32 SectionCount;=0D
BOOLEAN FoundSection;=0D
+ UINT32 Offset;=0D
=0D
CoffEntry =3D 0;=0D
mCoffOffset =3D 0;=0D
@@ -750,7 +818,7 @@ ScanSections64 (
if (shdr->sh_addralign <=3D mCoffAlignment) {=0D
continue;=0D
}=0D
- if (IsTextShdr(shdr) || IsDataShdr(shdr) || IsHiiRsrcShdr(shdr)) {=0D
+ if (IsTextShdr(shdr) || IsDataShdr(shdr) || IsHiiRsrcShdr(shdr) || IsS=
ymbolShdr(shdr)) {=0D
mCoffAlignment =3D (UINT32)shdr->sh_addralign;=0D
}=0D
}=0D
@@ -880,6 +948,16 @@ ScanSections64 (
Warning (NULL, 0, 0, NULL, "Multiple sections in %s are merged into 1 =
data section. Source level debug might not work correctly.", mInImageName);=
=0D
}=0D
=0D
+ //=0D
+ // The Symbol sections.=0D
+ //=0D
+ if (mExportFlag) {=0D
+ mExportOffset =3D mCoffOffset;=0D
+ mExportSize =3D sizeof(EFI_IMAGE_EXPORT_DIRECTORY) + strlen(mInImageNa=
me) + 1;=0D
+ mCoffOffset +=3D mExportSize;=0D
+ mCoffOffset =3D CoffAlign(mCoffOffset);=0D
+ }=0D
+=0D
//=0D
// The HII resource sections.=0D
//=0D
@@ -962,7 +1040,11 @@ ScanSections64 (
| EFI_IMAGE_FILE_LARGE_ADDRESS_AWARE;=0D
=0D
NtHdr->Pe32Plus.OptionalHeader.SizeOfCode =3D mDataOffset - mTextOffset;=
=0D
- NtHdr->Pe32Plus.OptionalHeader.SizeOfInitializedData =3D mRelocOffset - =
mDataOffset;=0D
+ if(mExportFlag) {=0D
+ NtHdr->Pe32Plus.OptionalHeader.SizeOfInitializedData =3D mRelocOffset =
- mExportOffset;=0D
+ } else {=0D
+ NtHdr->Pe32Plus.OptionalHeader.SizeOfInitializedData =3D mRelocOffset =
- mDataOffset;=0D
+ }=0D
NtHdr->Pe32Plus.OptionalHeader.SizeOfUninitializedData =3D 0;=0D
NtHdr->Pe32Plus.OptionalHeader.AddressOfEntryPoint =3D CoffEntry;=0D
=0D
@@ -989,8 +1071,17 @@ ScanSections64 (
NtHdr->Pe32Plus.FileHeader.NumberOfSections--;=0D
}=0D
=0D
+ //=0D
+ // If found symbol, add edata section between data and rsrc section=0D
+ //=0D
+ if(mExportFlag) {=0D
+ Offset =3D mExportOffset;=0D
+ } else {=0D
+ Offset =3D mHiiRsrcOffset;=0D
+ }=0D
+=0D
if ((mHiiRsrcOffset - mDataOffset) > 0) {=0D
- CreateSectionHeader (".data", mDataOffset, mHiiRsrcOffset - mDataOffse=
t,=0D
+ CreateSectionHeader (".data", mDataOffset, Offset - mDataOffset,=0D
EFI_IMAGE_SCN_CNT_INITIALIZED_DATA=0D
| EFI_IMAGE_SCN_MEM_WRITE=0D
| EFI_IMAGE_SCN_MEM_READ);=0D
@@ -999,6 +1090,20 @@ ScanSections64 (
NtHdr->Pe32Plus.FileHeader.NumberOfSections--;=0D
}=0D
=0D
+ if(mExportFlag) {=0D
+ if ((mHiiRsrcOffset - mExportOffset) > 0) {=0D
+ CreateSectionHeader (".edata", mExportOffset, mHiiRsrcOffset - mExpo=
rtOffset,=0D
+ EFI_IMAGE_SCN_CNT_INITIALIZED_DATA=0D
+ | EFI_IMAGE_SCN_MEM_READ);=0D
+ NtHdr->Pe32Plus.OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENT=
RY_EXPORT].Size =3D mHiiRsrcOffset - mExportOffset;=0D
+ NtHdr->Pe32Plus.OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENT=
RY_EXPORT].VirtualAddress =3D mExportOffset;=0D
+ NtHdr->Pe32Plus.FileHeader.NumberOfSections++;=0D
+ } else {=0D
+ // Don't make a section of size 0.=0D
+ NtHdr->Pe32Plus.FileHeader.NumberOfSections--;=0D
+ }=0D
+ }=0D
+=0D
if ((mRelocOffset - mHiiRsrcOffset) > 0) {=0D
CreateSectionHeader (".rsrc", mHiiRsrcOffset, mRelocOffset - mHiiRsrcO=
ffset,=0D
EFI_IMAGE_SCN_CNT_INITIALIZED_DATA=0D
@@ -1757,4 +1862,145 @@ CleanUp64 (
}=0D
}=0D
=0D
+STATIC=0D
+VOID=0D
+ScanSymbol64 (=0D
+ VOID=0D
+ )=0D
+{=0D
+ UINT32 shIndex;=0D
+ UINT32 SymIndex;=0D
+ Elf_Sym *Sym;=0D
+ UINT64 SymNum;=0D
+ const UINT8 *SymName;=0D
+=0D
+ for (shIndex =3D 0; shIndex < mEhdr->e_shnum; shIndex++) {=0D
+ //=0D
+ // Determine if this is a symbol section.=0D
+ //=0D
+ Elf_Shdr *shdr =3D GetShdrByIndex(shIndex);=0D
+ if (!IsSymbolShdr(shdr)) {=0D
+ continue;=0D
+ }=0D
+=0D
+ UINT8 *Symtab =3D (UINT8*)mEhdr + shdr->sh_offset;=0D
+ SymNum =3D (shdr->sh_size) / (shdr->sh_entsize);=0D
+=0D
+ //=0D
+ // First Get PrmModuleExportDescriptor=0D
+ //=0D
+ for (SymIndex =3D 0; SymIndex < SymNum; SymIndex++) {=0D
+ Sym =3D (Elf_Sym *)(Symtab + SymIndex * shdr->sh_entsize);=0D
+ SymName =3D GetSymName(Sym);=0D
+ if (SymName =3D=3D NULL) {=0D
+ continue;=0D
+ }=0D
+=0D
+ if (strcmp((CHAR8*)SymName, PRM_MODULE_EXPORT_DESCRIPTOR_NAME) =3D=
=3D 0) {=0D
+ //=0D
+ // Find PrmHandler Number and Name=0D
+ //=0D
+ FindPrmHandler(Sym->st_value);=0D
+=0D
+ strcpy(mExportSymName[mExportSymNum], (CHAR8*)SymName);=0D
+ mExportRVA[mExportSymNum] =3D (UINT32)(Sym->st_value);=0D
+ mExportSize +=3D 2 * EFI_IMAGE_EXPORT_ADDR_SIZE + EFI_IMAGE_EXPORT=
_ORDINAL_SIZE + strlen((CHAR8 *)SymName) + 1;=0D
+ mExportSymNum ++;=0D
+ break;=0D
+ }=0D
+ }=0D
+=0D
+ //=0D
+ // Second Get PrmHandler=0D
+ //=0D
+ for (SymIndex =3D 0; SymIndex < SymNum; SymIndex++) {=0D
+ UINT32 ExpIndex;=0D
+ Sym =3D (Elf_Sym *)(Symtab + SymIndex * shdr->sh_entsize);=0D
+ SymName =3D GetSymName(Sym);=0D
+ if (SymName =3D=3D NULL) {=0D
+ continue;=0D
+ }=0D
+=0D
+ for (ExpIndex =3D 0; ExpIndex < (mExportSymNum -1); ExpIndex++) {=0D
+ if (strcmp((CHAR8*)SymName, mExportSymName[ExpIndex]) !=3D 0) {=0D
+ continue;=0D
+ }=0D
+ mExportRVA[ExpIndex] =3D (UINT32)(Sym->st_value);=0D
+ mExportSize +=3D 2 * EFI_IMAGE_EXPORT_ADDR_SIZE + EFI_IMAGE_EXPORT=
_ORDINAL_SIZE + strlen((CHAR8 *)SymName) + 1;=0D
+ }=0D
+ }=0D
+=0D
+ break;=0D
+ }=0D
+}=0D
+=0D
+STATIC=0D
+VOID=0D
+WriteExport64 (=0D
+ VOID=0D
+ )=0D
+{=0D
+ EFI_IMAGE_OPTIONAL_HEADER_UNION *NtHdr;=0D
+ EFI_IMAGE_EXPORT_DIRECTORY *ExportDir;=0D
+ EFI_IMAGE_DATA_DIRECTORY *DataDir;=0D
+ UINT32 FileNameOffset;=0D
+ UINT32 FuncOffset;=0D
+ UINT16 Index;=0D
+ UINT8 *Tdata =3D NULL;=0D
+=0D
+ ExportDir =3D (EFI_IMAGE_EXPORT_DIRECTORY*)(mCoffFile + mExportOffset);=
=0D
+ ExportDir->Characteristics =3D 0;=0D
+ ExportDir->TimeDateStamp =3D 0;=0D
+ ExportDir->MajorVersion =3D 0;=0D
+ ExportDir->MinorVersion =3D0;=0D
+ ExportDir->Name =3D 0;=0D
+ ExportDir->NumberOfFunctions =3D mExportSymNum;=0D
+ ExportDir->NumberOfNames =3D mExportSymNum;=0D
+ ExportDir->Base =3D EFI_IMAGE_EXPORT_ORDINAL_BASE;=0D
+ ExportDir->AddressOfFunctions =3D mExportOffset + sizeof(EFI_IMAGE_EXPOR=
T_DIRECTORY);=0D
+ ExportDir->AddressOfNames =3D ExportDir->AddressOfFunctions + EFI_IMAGE_=
EXPORT_ADDR_SIZE * mExportSymNum;=0D
+ ExportDir->AddressOfNameOrdinals =3D ExportDir->AddressOfNames + EFI_IMA=
GE_EXPORT_ADDR_SIZE * mExportSymNum;=0D
+=0D
+ FileNameOffset =3D ExportDir->AddressOfNameOrdinals + EFI_IMAGE_EXPORT_O=
RDINAL_SIZE * mExportSymNum;=0D
+ FuncOffset =3D FileNameOffset + strlen(mInImageName) + 1;=0D
+=0D
+ // Write Input image Name RVA=0D
+ Tdata =3D mCoffFile + 12;=0D
+ *(UINT32 *)Tdata =3D FileNameOffset;=0D
+=0D
+ // Write Input image Name=0D
+ strcpy((char *)(mCoffFile + FileNameOffset), mInImageName);=0D
+=0D
+ for (Index =3D 0; Index < mExportSymNum; Index++) {=0D
+ //=0D
+ // Write Export Address Table=0D
+ //=0D
+ Tdata =3D mCoffFile + ExportDir->AddressOfFunctions + Index * EFI_IMAG=
E_EXPORT_ADDR_SIZE;=0D
+ *(UINT32 *)Tdata =3D mExportRVA[Index];=0D
+=0D
+ //=0D
+ // Write Export Name Pointer Table=0D
+ //=0D
+ Tdata =3D mCoffFile + ExportDir->AddressOfNames + Index * EFI_IMAGE_EX=
PORT_ADDR_SIZE;=0D
+ *(UINT32 *)Tdata =3D FuncOffset;=0D
+=0D
+ //=0D
+ // Write Export Ordinal table=0D
+ //=0D
+ Tdata =3D mCoffFile + ExportDir->AddressOfNameOrdinals + Index * EFI_I=
MAGE_EXPORT_ORDINAL_SIZE;=0D
+ *(UINT16 *)Tdata =3D Index;=0D
+=0D
+ //=0D
+ // Write Export Name Table=0D
+ //=0D
+ strcpy((char *)(mCoffFile + FuncOffset), mExportSymName[Index]);=0D
+ FuncOffset +=3D strlen(mExportSymName[Index]) + 1;=0D
+ }=0D
+=0D
+ NtHdr =3D (EFI_IMAGE_OPTIONAL_HEADER_UNION *)(mCoffFile + mNtHdrOffset);=
=0D
+ DataDir =3D &NtHdr->Pe32Plus.OptionalHeader.DataDirectory[EFI_IMAGE_DIRE=
CTORY_ENTRY_EXPORT];=0D
+ DataDir->VirtualAddress =3D mExportOffset;=0D
+ DataDir->Size =3D mExportSize;=0D
+=0D
+}=0D
=0D
diff --git a/BaseTools/Source/C/GenFw/ElfConvert.c b/BaseTools/Source/C/Gen=
Fw/ElfConvert.c
index 7db8721167..795cdbd743 100644
--- a/BaseTools/Source/C/GenFw/ElfConvert.c
+++ b/BaseTools/Source/C/GenFw/ElfConvert.c
@@ -223,6 +223,16 @@ ConvertElf (
VerboseMsg ("Write debug info.");=0D
ElfFunctions.WriteDebug ();=0D
=0D
+ //=0D
+ // For PRM Driver to Write export info.=0D
+ //=0D
+ if (mExportFlag) {=0D
+ VerboseMsg ("Scan symbol info.");=0D
+ ElfFunctions.ScanSymbol ();=0D
+ VerboseMsg ("Write export info.");=0D
+ ElfFunctions.WriteExport ();=0D
+ }=0D
+=0D
//=0D
// Make sure image size is correct before returning the new image.=0D
//=0D
diff --git a/BaseTools/Source/C/GenFw/ElfConvert.h b/BaseTools/Source/C/Gen=
Fw/ElfConvert.h
index 801e8de4a2..7920765fbb 100644
--- a/BaseTools/Source/C/GenFw/ElfConvert.h
+++ b/BaseTools/Source/C/GenFw/ElfConvert.h
@@ -24,6 +24,7 @@ extern UINT8 *mCoffFile;
extern UINT32 mTableOffset;=0D
extern UINT32 mOutImageType;=0D
extern UINT32 mFileBufferSize;=0D
+extern BOOLEAN mExportFlag;=0D
=0D
//=0D
// Common EFI specific data.=0D
@@ -31,6 +32,42 @@ extern UINT32 mFileBufferSize;
#define ELF_HII_SECTION_NAME ".hii"=0D
#define ELF_STRTAB_SECTION_NAME ".strtab"=0D
#define MAX_COFF_ALIGNMENT 0x10000=0D
+#define ELF_SYMBOL_SECTION_NAME ".symtab"=0D
+=0D
+//=0D
+// Platform Runtime Mechanism (PRM) specific data.=0D
+//=0D
+#define PRM_MODULE_EXPORT_SYMBOL_NUM 10=0D
+#define PRM_HANDLER_NAME_MAXIMUM_LENGTH 128=0D
+=0D
+#define PRM_MODULE_EXPORT_DESCRIPTOR_NAME "PrmModuleExportDescript=
or"=0D
+#define PRM_MODULE_EXPORT_DESCRIPTOR_SIGNATURE SIGNATURE_64 ('P', 'R', =
'M', '_', 'M', 'E', 'D', 'T')=0D
+#define PRM_MODULE_EXPORT_REVISION 0x0=0D
+=0D
+//=0D
+// Platform Runtime Mechanism (PRM) Export Descriptor Structures=0D
+//=0D
+#pragma pack(push, 1)=0D
+=0D
+typedef struct {=0D
+ EFI_GUID PrmHandlerGuid;=0D
+ CHAR8 PrmHandlerName[PRM_HANDLER_NAME_MA=
XIMUM_LENGTH];=0D
+} PRM_HANDLER_EXPORT_DESCRIPTOR_STRUCT;=0D
+=0D
+typedef struct {=0D
+ UINT64 Signature;=0D
+ UINT16 Revision;=0D
+ UINT16 NumberPrmHandlers;=0D
+ EFI_GUID PlatformGuid;=0D
+ EFI_GUID ModuleGuid;=0D
+} PRM_MODULE_EXPORT_DESCRIPTOR_STRUCT_HEADER;=0D
+=0D
+typedef struct {=0D
+ PRM_MODULE_EXPORT_DESCRIPTOR_STRUCT_HEADER Header;=0D
+ PRM_HANDLER_EXPORT_DESCRIPTOR_STRUCT PrmHandlerExportDescriptors[=
1];=0D
+} PRM_MODULE_EXPORT_DESCRIPTOR_STRUCT;=0D
+=0D
+#pragma pack(pop)=0D
=0D
//=0D
// Filter Types=0D
@@ -38,7 +75,8 @@ extern UINT32 mFileBufferSize;
typedef enum {=0D
SECTION_TEXT,=0D
SECTION_HII,=0D
- SECTION_DATA=0D
+ SECTION_DATA,=0D
+ SECTION_SYMBOL=0D
=0D
} SECTION_FILTER_TYPES;=0D
=0D
@@ -50,6 +88,8 @@ typedef struct {
BOOLEAN (*WriteSections) (SECTION_FILTER_TYPES FilterType);=0D
VOID (*WriteRelocations) ();=0D
VOID (*WriteDebug) ();=0D
+ VOID (*ScanSymbol) ();=0D
+ VOID (*WriteExport) ();=0D
VOID (*SetImageSize) ();=0D
VOID (*CleanUp) ();=0D
=0D
diff --git a/BaseTools/Source/C/GenFw/GenFw.c b/BaseTools/Source/C/GenFw/Ge=
nFw.c
index 8cab70ba4d..c7de5b89d8 100644
--- a/BaseTools/Source/C/GenFw/GenFw.c
+++ b/BaseTools/Source/C/GenFw/GenFw.c
@@ -87,7 +87,7 @@ UINT32 mImageTimeStamp =3D 0;
UINT32 mImageSize =3D 0;=0D
UINT32 mOutImageType =3D FW_DUMMY_IMAGE;=0D
BOOLEAN mIsConvertXip =3D FALSE;=0D
-=0D
+BOOLEAN mExportFlag =3D FALSE;=0D
=0D
STATIC=0D
EFI_STATUS=0D
@@ -1436,6 +1436,15 @@ Returns:
continue;=0D
}=0D
=0D
+ if (stricmp (argv[0], "--PRM") =3D=3D 0) {=0D
+ if (!mExportFlag) {=0D
+ mExportFlag =3D TRUE;=0D
+ }=0D
+ argc --;=0D
+ argv ++;=0D
+ continue;=0D
+ }=0D
+=0D
if (argv[0][0] =3D=3D '-') {=0D
Error (NULL, 0, 1000, "Unknown option", argv[0]);=0D
goto Finish;=0D
diff --git a/BaseTools/Source/C/Include/IndustryStandard/PeImage.h b/BaseTo=
ols/Source/C/Include/IndustryStandard/PeImage.h
index f17b8ee19b..21c968e650 100644
--- a/BaseTools/Source/C/Include/IndustryStandard/PeImage.h
+++ b/BaseTools/Source/C/Include/IndustryStandard/PeImage.h
@@ -571,6 +571,13 @@ typedef struct {
UINT32 AddressOfNameOrdinals;=0D
} EFI_IMAGE_EXPORT_DIRECTORY;=0D
=0D
+//=0D
+// Based export types.=0D
+//=0D
+#define EFI_IMAGE_EXPORT_ORDINAL_BASE 1=0D
+#define EFI_IMAGE_EXPORT_ADDR_SIZE 4=0D
+#define EFI_IMAGE_EXPORT_ORDINAL_SIZE 2=0D
+=0D
///=0D
/// DLL support.=0D
/// Import Format=0D
--=20
2.26.2.windows.1


Bob Feng
 

Hi Lixia,

This patch introduce a new command line option --PRM. Could you add the help information about --PRM?

Could you provide more information about the below change? Would there be side-effect?

@@ -750,7 +818,7 @@ ScanSections64 (
if (shdr->sh_addralign <= mCoffAlignment) {
continue;
}
- if (IsTextShdr(shdr) || IsDataShdr(shdr) || IsHiiRsrcShdr(shdr)) {
+ if (IsTextShdr(shdr) || IsDataShdr(shdr) || IsHiiRsrcShdr(shdr) || IsSymbolShdr(shdr)) {
mCoffAlignment = (UINT32)shdr->sh_addralign;
}
}



Thanks,
Bob

-----Original Message-----
From: Huang, Li-Xia <lisa.huang@intel.com>
Sent: Wednesday, January 12, 2022 3:44 PM
To: devel@edk2.groups.io
Cc: Huang, Li-Xia <lisa.huang@intel.com>; Gao, Liming <gaoliming@byosoft.com.cn>; Feng, Bob C <bob.c.feng@intel.com>; Chen, Christine <yuwei.chen@intel.com>
Subject: [edk2-devel][PATCH] BaseTools/GenFw: Enhance to add export table in PE-COFF

REF: https://bugzilla.tianocore.org/show_bug.cgi?id=3802
Since PRM module needs to support export table in PE-COFF, we'll enhance GenFw tool to support this.

Add one export flag in GenFw tool. If export flag is set:
Step1: Scan ELF symbol table based on PRM module descriptor to get descriptor offset address;
Step2: Find PRM handlers number and name in COFF file based on the address from step1;
Step3: Write PRM info such as handler name and export RVA into COFF export table.

Cc: Liming Gao <gaoliming@byosoft.com.cn>
Cc: Bob Feng <bob.c.feng@intel.com>
Cc: Yuwei Chen <yuwei.chen@intel.com>
Signed-off-by: Lixia Huang <lisa.huang@intel.com>
---
BaseTools/Source/C/GenFw/Elf64Convert.c | 254 +++++++++++++++++-
BaseTools/Source/C/GenFw/ElfConvert.c | 10 +
BaseTools/Source/C/GenFw/ElfConvert.h | 42 ++-
BaseTools/Source/C/GenFw/GenFw.c | 11 +-
.../C/Include/IndustryStandard/PeImage.h | 7 +
5 files changed, 318 insertions(+), 6 deletions(-)

diff --git a/BaseTools/Source/C/GenFw/Elf64Convert.c b/BaseTools/Source/C/GenFw/Elf64Convert.c
index 0bb3ead228..0079507356 100644
--- a/BaseTools/Source/C/GenFw/Elf64Convert.c
+++ b/BaseTools/Source/C/GenFw/Elf64Convert.c
@@ -56,6 +56,18 @@ WriteDebug64 (
VOID ); +STATIC+VOID+ScanSymbol64 (+ VOID+ );++STATIC+VOID+WriteExport64 (+ VOID+ );+ STATIC VOID SetImageSize64 (@@ -122,7 +134,7 @@ STATIC UINT32 mDataOffset;
STATIC UINT32 mHiiRsrcOffset; STATIC UINT32 mRelocOffset; STATIC UINT32 mDebugOffset;-+STATIC UINT32 mExportOffset; // // Used for RISC-V relocations. //@@ -132,6 +144,14 @@ STATIC Elf64_Half mRiscVPass1SymSecIndex = 0;
STATIC INT32 mRiscVPass1Offset; STATIC INT32 mRiscVPass1GotFixup; +//+// Used for Export section.+//+STATIC UINT32 mExportSize;+STATIC UINT32 mExportRVA[PRM_MODULE_EXPORT_SYMBOL_NUM];+STATIC UINT32 mExportSymNum;+STATIC CHAR8 mExportSymName[PRM_MODULE_EXPORT_SYMBOL_NUM][PRM_HANDLER_NAME_MAXIMUM_LENGTH];+ // // Initialization Function //@@ -200,6 +220,10 @@ InitializeElf64 (
ElfFunctions->SetImageSize = SetImageSize64; ElfFunctions->CleanUp = CleanUp64; + if (mExportFlag) {+ ElfFunctions->ScanSymbol = ScanSymbol64;+ ElfFunctions->WriteExport = WriteExport64;+ } return TRUE; } @@ -263,6 +287,17 @@ IsHiiRsrcShdr (
return (BOOLEAN) (strcmp((CHAR8*)mEhdr + Namedr->sh_offset + Shdr->sh_name, ELF_HII_SECTION_NAME) == 0); } +STATIC+BOOLEAN+IsSymbolShdr (+ Elf_Shdr *Shdr+ )+{+ Elf_Shdr *Namehdr = GetShdrByIndex(mEhdr->e_shstrndx);++ return (BOOLEAN) (strcmp((CHAR8*)mEhdr + Namehdr->sh_offset + Shdr->sh_name, ELF_SYMBOL_SECTION_NAME) == 0);+}+ STATIC BOOLEAN IsDataShdr (@@ -335,6 +370,38 @@ GetSymName (
return StrtabContents + Sym->st_name; } +//+// Get Prm Handler number and name+//+STATIC+VOID+FindPrmHandler (+ UINT64 Offset+ )+{+ PRM_MODULE_EXPORT_DESCRIPTOR_STRUCT_HEADER *PrmExport;+ UINT32 NameOffset;+ UINT32 HandlerNum;+ UINT32 Index;+ UINT8 SymName[PRM_HANDLER_NAME_MAXIMUM_LENGTH];++ PrmExport = (PRM_MODULE_EXPORT_DESCRIPTOR_STRUCT_HEADER*)(mCoffFile + Offset);+ NameOffset = sizeof(PRM_MODULE_EXPORT_DESCRIPTOR_STRUCT_HEADER) + sizeof(EFI_GUID);++ for (HandlerNum = 0; HandlerNum < PrmExport->NumberPrmHandlers; HandlerNum++) {+ for (Index = 0; Index < PRM_HANDLER_NAME_MAXIMUM_LENGTH; Index++) {+ SymName[Index] = *((UINT8 *)PrmExport + NameOffset + Index);+ if (SymName[Index] == 0) {+ break;+ }+ }++ strcpy(mExportSymName[mExportSymNum], (CHAR8*)SymName);+ NameOffset += PRM_HANDLER_NAME_MAXIMUM_LENGTH + sizeof(EFI_GUID);+ mExportSymNum ++;+ }+}+ // // Find the ELF section hosting the GOT from an ELF Rva // of a single GOT entry. Normally, GOT is placed in@@ -717,6 +784,7 @@ ScanSections64 (
UINT32 CoffEntry; UINT32 SectionCount; BOOLEAN FoundSection;+ UINT32 Offset; CoffEntry = 0; mCoffOffset = 0;@@ -750,7 +818,7 @@ ScanSections64 (
if (shdr->sh_addralign <= mCoffAlignment) { continue; }- if (IsTextShdr(shdr) || IsDataShdr(shdr) || IsHiiRsrcShdr(shdr)) {+ if (IsTextShdr(shdr) || IsDataShdr(shdr) || IsHiiRsrcShdr(shdr) || IsSymbolShdr(shdr)) { mCoffAlignment = (UINT32)shdr->sh_addralign; } }@@ -880,6 +948,16 @@ ScanSections64 (
Warning (NULL, 0, 0, NULL, "Multiple sections in %s are merged into 1 data section. Source level debug might not work correctly.", mInImageName); } + //+ // The Symbol sections.+ //+ if (mExportFlag) {+ mExportOffset = mCoffOffset;+ mExportSize = sizeof(EFI_IMAGE_EXPORT_DIRECTORY) + strlen(mInImageName) + 1;+ mCoffOffset += mExportSize;+ mCoffOffset = CoffAlign(mCoffOffset);+ }+ // // The HII resource sections. //@@ -962,7 +1040,11 @@ ScanSections64 (
| EFI_IMAGE_FILE_LARGE_ADDRESS_AWARE; NtHdr->Pe32Plus.OptionalHeader.SizeOfCode = mDataOffset - mTextOffset;- NtHdr->Pe32Plus.OptionalHeader.SizeOfInitializedData = mRelocOffset - mDataOffset;+ if(mExportFlag) {+ NtHdr->Pe32Plus.OptionalHeader.SizeOfInitializedData = mRelocOffset - mExportOffset;+ } else {+ NtHdr->Pe32Plus.OptionalHeader.SizeOfInitializedData = mRelocOffset - mDataOffset;+ } NtHdr->Pe32Plus.OptionalHeader.SizeOfUninitializedData = 0; NtHdr->Pe32Plus.OptionalHeader.AddressOfEntryPoint = CoffEntry; @@ -989,8 +1071,17 @@ ScanSections64 (
NtHdr->Pe32Plus.FileHeader.NumberOfSections--; } + //+ // If found symbol, add edata section between data and rsrc section+ //+ if(mExportFlag) {+ Offset = mExportOffset;+ } else {+ Offset = mHiiRsrcOffset;+ }+ if ((mHiiRsrcOffset - mDataOffset) > 0) {- CreateSectionHeader (".data", mDataOffset, mHiiRsrcOffset - mDataOffset,+ CreateSectionHeader (".data", mDataOffset, Offset - mDataOffset, EFI_IMAGE_SCN_CNT_INITIALIZED_DATA | EFI_IMAGE_SCN_MEM_WRITE | EFI_IMAGE_SCN_MEM_READ);@@ -999,6 +1090,20 @@ ScanSections64 (
NtHdr->Pe32Plus.FileHeader.NumberOfSections--; } + if(mExportFlag) {+ if ((mHiiRsrcOffset - mExportOffset) > 0) {+ CreateSectionHeader (".edata", mExportOffset, mHiiRsrcOffset - mExportOffset,+ EFI_IMAGE_SCN_CNT_INITIALIZED_DATA+ | EFI_IMAGE_SCN_MEM_READ);+ NtHdr->Pe32Plus.OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_EXPORT].Size = mHiiRsrcOffset - mExportOffset;+ NtHdr->Pe32Plus.OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress = mExportOffset;+ NtHdr->Pe32Plus.FileHeader.NumberOfSections++;+ } else {+ // Don't make a section of size 0.+ NtHdr->Pe32Plus.FileHeader.NumberOfSections--;+ }+ }+ if ((mRelocOffset - mHiiRsrcOffset) > 0) { CreateSectionHeader (".rsrc", mHiiRsrcOffset, mRelocOffset - mHiiRsrcOffset, EFI_IMAGE_SCN_CNT_INITIALIZED_DATA@@ -1757,4 +1862,145 @@ CleanUp64 (
} } +STATIC+VOID+ScanSymbol64 (+ VOID+ )+{+ UINT32 shIndex;+ UINT32 SymIndex;+ Elf_Sym *Sym;+ UINT64 SymNum;+ const UINT8 *SymName;++ for (shIndex = 0; shIndex < mEhdr->e_shnum; shIndex++) {+ //+ // Determine if this is a symbol section.+ //+ Elf_Shdr *shdr = GetShdrByIndex(shIndex);+ if (!IsSymbolShdr(shdr)) {+ continue;+ }++ UINT8 *Symtab = (UINT8*)mEhdr + shdr->sh_offset;+ SymNum = (shdr->sh_size) / (shdr->sh_entsize);++ //+ // First Get PrmModuleExportDescriptor+ //+ for (SymIndex = 0; SymIndex < SymNum; SymIndex++) {+ Sym = (Elf_Sym *)(Symtab + SymIndex * shdr->sh_entsize);+ SymName = GetSymName(Sym);+ if (SymName == NULL) {+ continue;+ }++ if (strcmp((CHAR8*)SymName, PRM_MODULE_EXPORT_DESCRIPTOR_NAME) == 0) {+ //+ // Find PrmHandler Number and Name+ //+ FindPrmHandler(Sym->st_value);++ strcpy(mExportSymName[mExportSymNum], (CHAR8*)SymName);+ mExportRVA[mExportSymNum] = (UINT32)(Sym->st_value);+ mExportSize += 2 * EFI_IMAGE_EXPORT_ADDR_SIZE + EFI_IMAGE_EXPORT_ORDINAL_SIZE + strlen((CHAR8 *)SymName) + 1;+ mExportSymNum ++;+ break;+ }+ }++ //+ // Second Get PrmHandler+ //+ for (SymIndex = 0; SymIndex < SymNum; SymIndex++) {+ UINT32 ExpIndex;+ Sym = (Elf_Sym *)(Symtab + SymIndex * shdr->sh_entsize);+ SymName = GetSymName(Sym);+ if (SymName == NULL) {+ continue;+ }++ for (ExpIndex = 0; ExpIndex < (mExportSymNum -1); ExpIndex++) {+ if (strcmp((CHAR8*)SymName, mExportSymName[ExpIndex]) != 0) {+ continue;+ }+ mExportRVA[ExpIndex] = (UINT32)(Sym->st_value);+ mExportSize += 2 * EFI_IMAGE_EXPORT_ADDR_SIZE + EFI_IMAGE_EXPORT_ORDINAL_SIZE + strlen((CHAR8 *)SymName) + 1;+ }+ }++ break;+ }+}++STATIC+VOID+WriteExport64 (+ VOID+ )+{+ EFI_IMAGE_OPTIONAL_HEADER_UNION *NtHdr;+ EFI_IMAGE_EXPORT_DIRECTORY *ExportDir;+ EFI_IMAGE_DATA_DIRECTORY *DataDir;+ UINT32 FileNameOffset;+ UINT32 FuncOffset;+ UINT16 Index;+ UINT8 *Tdata = NULL;++ ExportDir = (EFI_IMAGE_EXPORT_DIRECTORY*)(mCoffFile + mExportOffset);+ ExportDir->Characteristics = 0;+ ExportDir->TimeDateStamp = 0;+ ExportDir->MajorVersion = 0;+ ExportDir->MinorVersion =0;+ ExportDir->Name = 0;+ ExportDir->NumberOfFunctions = mExportSymNum;+ ExportDir->NumberOfNames = mExportSymNum;+ ExportDir->Base = EFI_IMAGE_EXPORT_ORDINAL_BASE;+ ExportDir->AddressOfFunctions = mExportOffset + sizeof(EFI_IMAGE_EXPORT_DIRECTORY);+ ExportDir->AddressOfNames = ExportDir->AddressOfFunctions + EFI_IMAGE_EXPORT_ADDR_SIZE * mExportSymNum;+ ExportDir->AddressOfNameOrdinals = ExportDir->AddressOfNames + EFI_IMAGE_EXPORT_ADDR_SIZE * mExportSymNum;++ FileNameOffset = ExportDir->AddressOfNameOrdinals + EFI_IMAGE_EXPORT_ORDINAL_SIZE * mExportSymNum;+ FuncOffset = FileNameOffset + strlen(mInImageName) + 1;++ // Write Input image Name RVA+ Tdata = mCoffFile + 12;+ *(UINT32 *)Tdata = FileNameOffset;++ // Write Input image Name+ strcpy((char *)(mCoffFile + FileNameOffset), mInImageName);++ for (Index = 0; Index < mExportSymNum; Index++) {+ //+ // Write Export Address Table+ //+ Tdata = mCoffFile + ExportDir->AddressOfFunctions + Index * EFI_IMAGE_EXPORT_ADDR_SIZE;+ *(UINT32 *)Tdata = mExportRVA[Index];++ //+ // Write Export Name Pointer Table+ //+ Tdata = mCoffFile + ExportDir->AddressOfNames + Index * EFI_IMAGE_EXPORT_ADDR_SIZE;+ *(UINT32 *)Tdata = FuncOffset;++ //+ // Write Export Ordinal table+ //+ Tdata = mCoffFile + ExportDir->AddressOfNameOrdinals + Index * EFI_IMAGE_EXPORT_ORDINAL_SIZE;+ *(UINT16 *)Tdata = Index;++ //+ // Write Export Name Table+ //+ strcpy((char *)(mCoffFile + FuncOffset), mExportSymName[Index]);+ FuncOffset += strlen(mExportSymName[Index]) + 1;+ }++ NtHdr = (EFI_IMAGE_OPTIONAL_HEADER_UNION *)(mCoffFile + mNtHdrOffset);+ DataDir = &NtHdr->Pe32Plus.OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_EXPORT];+ DataDir->VirtualAddress = mExportOffset;+ DataDir->Size = mExportSize;++} diff --git a/BaseTools/Source/C/GenFw/ElfConvert.c b/BaseTools/Source/C/GenFw/ElfConvert.c
index 7db8721167..795cdbd743 100644
--- a/BaseTools/Source/C/GenFw/ElfConvert.c
+++ b/BaseTools/Source/C/GenFw/ElfConvert.c
@@ -223,6 +223,16 @@ ConvertElf (
VerboseMsg ("Write debug info."); ElfFunctions.WriteDebug (); + //+ // For PRM Driver to Write export info.+ //+ if (mExportFlag) {+ VerboseMsg ("Scan symbol info.");+ ElfFunctions.ScanSymbol ();+ VerboseMsg ("Write export info.");+ ElfFunctions.WriteExport ();+ }+ // // Make sure image size is correct before returning the new image. //diff --git a/BaseTools/Source/C/GenFw/ElfConvert.h b/BaseTools/Source/C/GenFw/ElfConvert.h
index 801e8de4a2..7920765fbb 100644
--- a/BaseTools/Source/C/GenFw/ElfConvert.h
+++ b/BaseTools/Source/C/GenFw/ElfConvert.h
@@ -24,6 +24,7 @@ extern UINT8 *mCoffFile; extern UINT32 mTableOffset; extern UINT32 mOutImageType; extern UINT32 mFileBufferSize;+extern BOOLEAN mExportFlag; // // Common EFI specific data.@@ -31,6 +32,42 @@ extern UINT32 mFileBufferSize;
#define ELF_HII_SECTION_NAME ".hii" #define ELF_STRTAB_SECTION_NAME ".strtab" #define MAX_COFF_ALIGNMENT 0x10000+#define ELF_SYMBOL_SECTION_NAME ".symtab"++//+// Platform Runtime Mechanism (PRM) specific data.+//+#define PRM_MODULE_EXPORT_SYMBOL_NUM 10+#define PRM_HANDLER_NAME_MAXIMUM_LENGTH 128++#define PRM_MODULE_EXPORT_DESCRIPTOR_NAME "PrmModuleExportDescriptor"+#define PRM_MODULE_EXPORT_DESCRIPTOR_SIGNATURE SIGNATURE_64 ('P', 'R', 'M', '_', 'M', 'E', 'D', 'T')+#define PRM_MODULE_EXPORT_REVISION 0x0++//+// Platform Runtime Mechanism (PRM) Export Descriptor Structures+//+#pragma pack(push, 1)++typedef struct {+ EFI_GUID PrmHandlerGuid;+ CHAR8 PrmHandlerName[PRM_HANDLER_NAME_MAXIMUM_LENGTH];+} PRM_HANDLER_EXPORT_DESCRIPTOR_STRUCT;++typedef struct {+ UINT64 Signature;+ UINT16 Revision;+ UINT16 NumberPrmHandlers;+ EFI_GUID PlatformGuid;+ EFI_GUID ModuleGuid;+} PRM_MODULE_EXPORT_DESCRIPTOR_STRUCT_HEADER;++typedef struct {+ PRM_MODULE_EXPORT_DESCRIPTOR_STRUCT_HEADER Header;+ PRM_HANDLER_EXPORT_DESCRIPTOR_STRUCT PrmHandlerExportDescriptors[1];+} PRM_MODULE_EXPORT_DESCRIPTOR_STRUCT;++#pragma pack(pop) // // Filter Types@@ -38,7 +75,8 @@ extern UINT32 mFileBufferSize;
typedef enum { SECTION_TEXT, SECTION_HII,- SECTION_DATA+ SECTION_DATA,+ SECTION_SYMBOL } SECTION_FILTER_TYPES; @@ -50,6 +88,8 @@ typedef struct {
BOOLEAN (*WriteSections) (SECTION_FILTER_TYPES FilterType); VOID (*WriteRelocations) (); VOID (*WriteDebug) ();+ VOID (*ScanSymbol) ();+ VOID (*WriteExport) (); VOID (*SetImageSize) (); VOID (*CleanUp) (); diff --git a/BaseTools/Source/C/GenFw/GenFw.c b/BaseTools/Source/C/GenFw/GenFw.c
index 8cab70ba4d..c7de5b89d8 100644
--- a/BaseTools/Source/C/GenFw/GenFw.c
+++ b/BaseTools/Source/C/GenFw/GenFw.c
@@ -87,7 +87,7 @@ UINT32 mImageTimeStamp = 0;
UINT32 mImageSize = 0; UINT32 mOutImageType = FW_DUMMY_IMAGE; BOOLEAN mIsConvertXip = FALSE;-+BOOLEAN mExportFlag = FALSE; STATIC EFI_STATUS@@ -1436,6 +1436,15 @@ Returns:
continue; } + if (stricmp (argv[0], "--PRM") == 0) {+ if (!mExportFlag) {+ mExportFlag = TRUE;+ }+ argc --;+ argv ++;+ continue;+ }+ if (argv[0][0] == '-') { Error (NULL, 0, 1000, "Unknown option", argv[0]); goto Finish;diff --git a/BaseTools/Source/C/Include/IndustryStandard/PeImage.h b/BaseTools/Source/C/Include/IndustryStandard/PeImage.h
index f17b8ee19b..21c968e650 100644
--- a/BaseTools/Source/C/Include/IndustryStandard/PeImage.h
+++ b/BaseTools/Source/C/Include/IndustryStandard/PeImage.h
@@ -571,6 +571,13 @@ typedef struct {
UINT32 AddressOfNameOrdinals; } EFI_IMAGE_EXPORT_DIRECTORY; +//+// Based export types.+//+#define EFI_IMAGE_EXPORT_ORDINAL_BASE 1+#define EFI_IMAGE_EXPORT_ADDR_SIZE 4+#define EFI_IMAGE_EXPORT_ORDINAL_SIZE 2+ /// /// DLL support. /// Import Format--
2.26.2.windows.1


Huang, Li-Xia <lisa.huang@...>
 

Hi Bob,

 

Thanks for your comments.

 

1.  I will add the help information for "--PRM";

2.

@@ -750,7 +818,7 @@ ScanSections64 (

     if (shdr->sh_addralign <= mCoffAlignment) {

       continue;

     }

-    if (IsTextShdr(shdr) || IsDataShdr(shdr) || IsHiiRsrcShdr(shdr)) {

+    if (IsTextShdr(shdr) || IsDataShdr(shdr) || IsHiiRsrcShdr(shdr) || IsSymbolShdr(shdr)) {

       mCoffAlignment = (UINT32)shdr->sh_addralign;

     }

   }

 

1) Above change is to Set mCoffAlignment to the maximum alignment of the input sections including symbol section.

    The symbol section will only exist with below change, so it should have no effect to other drivers.

 

build_rule.template:

    <Command.GCC>

        $(CP) ${src} $(DEBUG_DIR)(+)$(MODULE_NAME).debug

        #$(OBJCOPY) --strip-unneeded -R .eh_frame ${src}

        $(OBJCOPY) $(OBJCOPY_STRIPFLAG) ${src}

 

tools_def.template:

*_*_*_OBJCOPY_STRIPFLAG            = --strip-unneeded -R .eh_frame

 

PrmAddrTransDsm.inf:

[BuildOptions.common]

...

  GCC: *_*_*_OBJCOPY_STRIPFLAG == -R .eh_frame

 

2) For PRM driver, sh_addralign of symbol section is 8, and less than other sections such as Text and Data (sh_addralign is 4096).

 

 

Regards,

Lisa

 

-----Original Message-----
From: Feng, Bob C <bob.c.feng@...>
Sent: 2022114 14:12
To: Huang, Li-Xia <lisa.huang@...>; devel@edk2.groups.io
Cc: Gao, Liming <gaoliming@...>; Chen, Christine <yuwei.chen@...>
Subject: RE: [edk2-devel][PATCH] BaseTools/GenFw: Enhance to add export table in PE-COFF

 

Hi Lixia,

 

This patch introduce a new command line option --PRM. Could you add the help information about --PRM?

 

Could you provide more information about the below change? Would there be side-effect?

 

@@ -750,7 +818,7 @@ ScanSections64 (

     if (shdr->sh_addralign <= mCoffAlignment) {

       continue;

     }

-    if (IsTextShdr(shdr) || IsDataShdr(shdr) || IsHiiRsrcShdr(shdr)) {

+    if (IsTextShdr(shdr) || IsDataShdr(shdr) || IsHiiRsrcShdr(shdr) || IsSymbolShdr(shdr)) {

       mCoffAlignment = (UINT32)shdr->sh_addralign;

     }

   }

 

 

 

Thanks,

Bob

 

-----Original Message-----

From: Huang, Li-Xia <lisa.huang@...>

Sent: Wednesday, January 12, 2022 3:44 PM

To: devel@edk2.groups.io

Cc: Huang, Li-Xia <lisa.huang@...>; Gao, Liming <gaoliming@...>; Feng, Bob C <bob.c.feng@...>; Chen, Christine <yuwei.chen@...>

Subject: [edk2-devel][PATCH] BaseTools/GenFw: Enhance to add export table in PE-COFF

 

REF: https://bugzilla.tianocore.org/show_bug.cgi?id=3802

Since PRM module needs to support export table in PE-COFF, we'll enhance GenFw tool to support this.

 

Add one export flag in GenFw tool. If export flag is set:

Step1: Scan ELF symbol table based on PRM module descriptor to get descriptor offset address;

Step2: Find PRM handlers number and name in COFF file based on the address from step1;

Step3: Write PRM info such as handler name and export RVA into COFF export table.

 

Cc: Liming Gao <gaoliming@...>

Cc: Bob Feng <bob.c.feng@...>

Cc: Yuwei Chen <yuwei.chen@...>

Signed-off-by: Lixia Huang <lisa.huang@...>

---

BaseTools/Source/C/GenFw/Elf64Convert.c       | 254 +++++++++++++++++-

BaseTools/Source/C/GenFw/ElfConvert.c         |  10 +

BaseTools/Source/C/GenFw/ElfConvert.h         |  42 ++-

BaseTools/Source/C/GenFw/GenFw.c              |  11 +-

.../C/Include/IndustryStandard/PeImage.h      |   7 +

5 files changed, 318 insertions(+), 6 deletions(-)

 

diff --git a/BaseTools/Source/C/GenFw/Elf64Convert.c b/BaseTools/Source/C/GenFw/Elf64Convert.c

index 0bb3ead228..0079507356 100644

--- a/BaseTools/Source/C/GenFw/Elf64Convert.c

+++ b/BaseTools/Source/C/GenFw/Elf64Convert.c

@@ -56,6 +56,18 @@ WriteDebug64 (

   VOID   ); +STATIC+VOID+ScanSymbol64 (+  VOID+  );++STATIC+VOID+WriteExport64 (+  VOID+  );+ STATIC VOID SetImageSize64 (@@ -122,7 +134,7 @@ STATIC UINT32 mDataOffset;

STATIC UINT32 mHiiRsrcOffset; STATIC UINT32 mRelocOffset; STATIC UINT32 mDebugOffset;-+STATIC UINT32 mExportOffset; // // Used for RISC-V relocations. //@@ -132,6 +144,14 @@ STATIC Elf64_Half  mRiscVPass1SymSecIndex = 0;

STATIC INT32       mRiscVPass1Offset; STATIC INT32       mRiscVPass1GotFixup; +//+// Used for Export section.+//+STATIC UINT32      mExportSize;+STATIC UINT32      mExportRVA[PRM_MODULE_EXPORT_SYMBOL_NUM];+STATIC UINT32      mExportSymNum;+STATIC CHAR8       mExportSymName[PRM_MODULE_EXPORT_SYMBOL_NUM][PRM_HANDLER_NAME_MAXIMUM_LENGTH];+ // // Initialization Function //@@ -200,6 +220,10 @@ InitializeElf64 (

   ElfFunctions->SetImageSize = SetImageSize64;   ElfFunctions->CleanUp = CleanUp64; +  if (mExportFlag) {+    ElfFunctions->ScanSymbol = ScanSymbol64;+    ElfFunctions->WriteExport = WriteExport64;+  }   return TRUE; } @@ -263,6 +287,17 @@ IsHiiRsrcShdr (

   return (BOOLEAN) (strcmp((CHAR8*)mEhdr + Namedr->sh_offset + Shdr->sh_name, ELF_HII_SECTION_NAME) == 0); } +STATIC+BOOLEAN+IsSymbolShdr (+  Elf_Shdr *Shdr+  )+{+  Elf_Shdr *Namehdr = GetShdrByIndex(mEhdr->e_shstrndx);++  return (BOOLEAN) (strcmp((CHAR8*)mEhdr + Namehdr->sh_offset + Shdr->sh_name, ELF_SYMBOL_SECTION_NAME) == 0);+}+ STATIC BOOLEAN IsDataShdr (@@ -335,6 +370,38 @@ GetSymName (

   return StrtabContents + Sym->st_name; } +//+// Get Prm Handler number and name+//+STATIC+VOID+FindPrmHandler (+  UINT64 Offset+  )+{+  PRM_MODULE_EXPORT_DESCRIPTOR_STRUCT_HEADER *PrmExport;+  UINT32   NameOffset;+  UINT32   HandlerNum;+  UINT32   Index;+  UINT8    SymName[PRM_HANDLER_NAME_MAXIMUM_LENGTH];++  PrmExport = (PRM_MODULE_EXPORT_DESCRIPTOR_STRUCT_HEADER*)(mCoffFile + Offset);+  NameOffset = sizeof(PRM_MODULE_EXPORT_DESCRIPTOR_STRUCT_HEADER) + sizeof(EFI_GUID);++  for (HandlerNum = 0; HandlerNum < PrmExport->NumberPrmHandlers; HandlerNum++) {+    for (Index = 0; Index < PRM_HANDLER_NAME_MAXIMUM_LENGTH; Index++) {+      SymName[Index] = *((UINT8 *)PrmExport + NameOffset + Index);+      if (SymName[Index] == 0) {+        break;+      }+    }++    strcpy(mExportSymName[mExportSymNum], (CHAR8*)SymName);+    NameOffset += PRM_HANDLER_NAME_MAXIMUM_LENGTH + sizeof(EFI_GUID);+    mExportSymNum ++;+  }+}+ // // Find the ELF section hosting the GOT from an ELF Rva //   of a single GOT entry.  Normally, GOT is placed in@@ -717,6 +784,7 @@ ScanSections64 (

   UINT32                          CoffEntry;   UINT32                          SectionCount;   BOOLEAN                         FoundSection;+  UINT32                          Offset;    CoffEntry = 0;   mCoffOffset = 0;@@ -750,7 +818,7 @@ ScanSections64 (

     if (shdr->sh_addralign <= mCoffAlignment) {       continue;     }-    if (IsTextShdr(shdr) || IsDataShdr(shdr) || IsHiiRsrcShdr(shdr)) {+    if (IsTextShdr(shdr) || IsDataShdr(shdr) || IsHiiRsrcShdr(shdr) || IsSymbolShdr(shdr)) {       mCoffAlignment = (UINT32)shdr->sh_addralign;     }   }@@ -880,6 +948,16 @@ ScanSections64 (

     Warning (NULL, 0, 0, NULL, "Multiple sections in %s are merged into 1 data section. Source level debug might not work correctly.", mInImageName);   } +  //+  //  The Symbol sections.+  //+  if (mExportFlag) {+    mExportOffset = mCoffOffset;+    mExportSize = sizeof(EFI_IMAGE_EXPORT_DIRECTORY) + strlen(mInImageName) + 1;+    mCoffOffset += mExportSize;+    mCoffOffset = CoffAlign(mCoffOffset);+  }+   //   //  The HII resource sections.   //@@ -962,7 +1040,11 @@ ScanSections64 (

     | EFI_IMAGE_FILE_LARGE_ADDRESS_AWARE;    NtHdr->Pe32Plus.OptionalHeader.SizeOfCode = mDataOffset - mTextOffset;-  NtHdr->Pe32Plus.OptionalHeader.SizeOfInitializedData = mRelocOffset - mDataOffset;+  if(mExportFlag) {+    NtHdr->Pe32Plus.OptionalHeader.SizeOfInitializedData = mRelocOffset - mExportOffset;+  } else {+    NtHdr->Pe32Plus.OptionalHeader.SizeOfInitializedData = mRelocOffset - mDataOffset;+  }   NtHdr->Pe32Plus.OptionalHeader.SizeOfUninitializedData = 0;   NtHdr->Pe32Plus.OptionalHeader.AddressOfEntryPoint = CoffEntry; @@ -989,8 +1071,17 @@ ScanSections64 (

     NtHdr->Pe32Plus.FileHeader.NumberOfSections--;   } +  //+  // If found symbol, add edata section between data and rsrc section+  //+  if(mExportFlag) {+    Offset = mExportOffset;+  } else {+    Offset = mHiiRsrcOffset;+  }+   if ((mHiiRsrcOffset - mDataOffset) > 0) {-    CreateSectionHeader (".data", mDataOffset, mHiiRsrcOffset - mDataOffset,+    CreateSectionHeader (".data", mDataOffset, Offset - mDataOffset,             EFI_IMAGE_SCN_CNT_INITIALIZED_DATA             | EFI_IMAGE_SCN_MEM_WRITE             | EFI_IMAGE_SCN_MEM_READ);@@ -999,6 +1090,20 @@ ScanSections64 (

     NtHdr->Pe32Plus.FileHeader.NumberOfSections--;   } +  if(mExportFlag) {+    if ((mHiiRsrcOffset - mExportOffset) > 0) {+      CreateSectionHeader (".edata", mExportOffset, mHiiRsrcOffset - mExportOffset,+              EFI_IMAGE_SCN_CNT_INITIALIZED_DATA+              | EFI_IMAGE_SCN_MEM_READ);+      NtHdr->Pe32Plus.OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_EXPORT].Size = mHiiRsrcOffset - mExportOffset;+      NtHdr->Pe32Plus.OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress = mExportOffset;+      NtHdr->Pe32Plus.FileHeader.NumberOfSections++;+    } else {+      // Don't make a section of size 0.+      NtHdr->Pe32Plus.FileHeader.NumberOfSections--;+    }+  }+   if ((mRelocOffset - mHiiRsrcOffset) > 0) {     CreateSectionHeader (".rsrc", mHiiRsrcOffset, mRelocOffset - mHiiRsrcOffset,             EFI_IMAGE_SCN_CNT_INITIALIZED_DATA@@ -1757,4 +1862,145 @@ CleanUp64 (

   } } +STATIC+VOID+ScanSymbol64 (+  VOID+  )+{+  UINT32      shIndex;+  UINT32      SymIndex;+  Elf_Sym     *Sym;+  UINT64      SymNum;+  const UINT8 *SymName;++  for (shIndex = 0; shIndex < mEhdr->e_shnum; shIndex++) {+    //+    // Determine if this is a symbol section.+    //+    Elf_Shdr *shdr = GetShdrByIndex(shIndex);+    if (!IsSymbolShdr(shdr)) {+      continue;+    }++    UINT8    *Symtab = (UINT8*)mEhdr + shdr->sh_offset;+    SymNum = (shdr->sh_size) / (shdr->sh_entsize);++    //+    // First Get PrmModuleExportDescriptor+    //+    for (SymIndex = 0; SymIndex < SymNum; SymIndex++) {+      Sym = (Elf_Sym *)(Symtab + SymIndex * shdr->sh_entsize);+      SymName = GetSymName(Sym);+      if (SymName == NULL) {+          continue;+      }++      if (strcmp((CHAR8*)SymName, PRM_MODULE_EXPORT_DESCRIPTOR_NAME) == 0) {+        //+        // Find PrmHandler Number and Name+        //+        FindPrmHandler(Sym->st_value);++        strcpy(mExportSymName[mExportSymNum], (CHAR8*)SymName);+        mExportRVA[mExportSymNum] = (UINT32)(Sym->st_value);+        mExportSize += 2 * EFI_IMAGE_EXPORT_ADDR_SIZE + EFI_IMAGE_EXPORT_ORDINAL_SIZE + strlen((CHAR8 *)SymName) + 1;+        mExportSymNum ++;+        break;+      }+    }++    //+    // Second Get PrmHandler+    //+    for (SymIndex = 0; SymIndex < SymNum; SymIndex++) {+      UINT32   ExpIndex;+      Sym = (Elf_Sym *)(Symtab + SymIndex * shdr->sh_entsize);+      SymName = GetSymName(Sym);+      if (SymName == NULL) {+          continue;+      }++      for (ExpIndex = 0; ExpIndex < (mExportSymNum -1); ExpIndex++) {+        if (strcmp((CHAR8*)SymName, mExportSymName[ExpIndex]) != 0) {+          continue;+        }+        mExportRVA[ExpIndex] = (UINT32)(Sym->st_value);+        mExportSize += 2 * EFI_IMAGE_EXPORT_ADDR_SIZE + EFI_IMAGE_EXPORT_ORDINAL_SIZE + strlen((CHAR8 *)SymName) + 1;+      }+    }++    break;+  }+}++STATIC+VOID+WriteExport64 (+  VOID+  )+{+  EFI_IMAGE_OPTIONAL_HEADER_UNION     *NtHdr;+  EFI_IMAGE_EXPORT_DIRECTORY          *ExportDir;+  EFI_IMAGE_DATA_DIRECTORY            *DataDir;+  UINT32                              FileNameOffset;+  UINT32                              FuncOffset;+  UINT16                              Index;+  UINT8                               *Tdata = NULL;++  ExportDir = (EFI_IMAGE_EXPORT_DIRECTORY*)(mCoffFile + mExportOffset);+  ExportDir->Characteristics = 0;+  ExportDir->TimeDateStamp = 0;+  ExportDir->MajorVersion = 0;+  ExportDir->MinorVersion =0;+  ExportDir->Name = 0;+  ExportDir->NumberOfFunctions = mExportSymNum;+  ExportDir->NumberOfNames = mExportSymNum;+  ExportDir->Base = EFI_IMAGE_EXPORT_ORDINAL_BASE;+  ExportDir->AddressOfFunctions = mExportOffset + sizeof(EFI_IMAGE_EXPORT_DIRECTORY);+  ExportDir->AddressOfNames = ExportDir->AddressOfFunctions + EFI_IMAGE_EXPORT_ADDR_SIZE * mExportSymNum;+  ExportDir->AddressOfNameOrdinals = ExportDir->AddressOfNames + EFI_IMAGE_EXPORT_ADDR_SIZE * mExportSymNum;++  FileNameOffset = ExportDir->AddressOfNameOrdinals + EFI_IMAGE_EXPORT_ORDINAL_SIZE * mExportSymNum;+  FuncOffset = FileNameOffset + strlen(mInImageName) + 1;++  // Write Input image Name RVA+  Tdata = mCoffFile + 12;+  *(UINT32 *)Tdata = FileNameOffset;++  // Write Input image Name+  strcpy((char *)(mCoffFile + FileNameOffset), mInImageName);++  for (Index = 0; Index < mExportSymNum; Index++) {+    //+    // Write Export Address Table+    //+    Tdata = mCoffFile + ExportDir->AddressOfFunctions + Index * EFI_IMAGE_EXPORT_ADDR_SIZE;+    *(UINT32 *)Tdata = mExportRVA[Index];++    //+    // Write Export Name Pointer Table+    //+    Tdata = mCoffFile + ExportDir->AddressOfNames + Index * EFI_IMAGE_EXPORT_ADDR_SIZE;+    *(UINT32 *)Tdata = FuncOffset;++    //+    // Write Export Ordinal table+    //+    Tdata = mCoffFile + ExportDir->AddressOfNameOrdinals + Index * EFI_IMAGE_EXPORT_ORDINAL_SIZE;+    *(UINT16 *)Tdata = Index;++    //+    // Write Export Name Table+    //+    strcpy((char *)(mCoffFile + FuncOffset), mExportSymName[Index]);+    FuncOffset += strlen(mExportSymName[Index]) + 1;+  }++  NtHdr = (EFI_IMAGE_OPTIONAL_HEADER_UNION *)(mCoffFile + mNtHdrOffset);+  DataDir = &NtHdr->Pe32Plus.OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_EXPORT];+  DataDir->VirtualAddress = mExportOffset;+  DataDir->Size = mExportSize;++} diff --git a/BaseTools/Source/C/GenFw/ElfConvert.c b/BaseTools/Source/C/GenFw/ElfConvert.c

index 7db8721167..795cdbd743 100644

--- a/BaseTools/Source/C/GenFw/ElfConvert.c

+++ b/BaseTools/Source/C/GenFw/ElfConvert.c

@@ -223,6 +223,16 @@ ConvertElf (

   VerboseMsg ("Write debug info.");   ElfFunctions.WriteDebug (); +  //+  // For PRM Driver to Write export info.+  //+  if (mExportFlag) {+    VerboseMsg ("Scan symbol info.");+    ElfFunctions.ScanSymbol ();+    VerboseMsg ("Write export info.");+    ElfFunctions.WriteExport ();+  }+   //   // Make sure image size is correct before returning the new image.   //diff --git a/BaseTools/Source/C/GenFw/ElfConvert.h b/BaseTools/Source/C/GenFw/ElfConvert.h

index 801e8de4a2..7920765fbb 100644

--- a/BaseTools/Source/C/GenFw/ElfConvert.h

+++ b/BaseTools/Source/C/GenFw/ElfConvert.h

@@ -24,6 +24,7 @@ extern UINT8  *mCoffFile;  extern UINT32 mTableOffset; extern UINT32 mOutImageType; extern UINT32 mFileBufferSize;+extern BOOLEAN mExportFlag;  // // Common EFI specific data.@@ -31,6 +32,42 @@ extern UINT32 mFileBufferSize;

#define ELF_HII_SECTION_NAME ".hii" #define ELF_STRTAB_SECTION_NAME ".strtab" #define MAX_COFF_ALIGNMENT 0x10000+#define ELF_SYMBOL_SECTION_NAME ".symtab"++//+// Platform Runtime Mechanism (PRM) specific data.+//+#define PRM_MODULE_EXPORT_SYMBOL_NUM 10+#define PRM_HANDLER_NAME_MAXIMUM_LENGTH 128++#define PRM_MODULE_EXPORT_DESCRIPTOR_NAME         "PrmModuleExportDescriptor"+#define PRM_MODULE_EXPORT_DESCRIPTOR_SIGNATURE    SIGNATURE_64 ('P', 'R', 'M', '_', 'M', 'E', 'D', 'T')+#define PRM_MODULE_EXPORT_REVISION                0x0++//+// Platform Runtime Mechanism (PRM) Export Descriptor Structures+//+#pragma pack(push, 1)++typedef struct {+  EFI_GUID                              PrmHandlerGuid;+  CHAR8                                 PrmHandlerName[PRM_HANDLER_NAME_MAXIMUM_LENGTH];+} PRM_HANDLER_EXPORT_DESCRIPTOR_STRUCT;++typedef struct {+  UINT64                                Signature;+  UINT16                                Revision;+  UINT16                                NumberPrmHandlers;+  EFI_GUID                              PlatformGuid;+  EFI_GUID                              ModuleGuid;+} PRM_MODULE_EXPORT_DESCRIPTOR_STRUCT_HEADER;++typedef struct {+  PRM_MODULE_EXPORT_DESCRIPTOR_STRUCT_HEADER  Header;+  PRM_HANDLER_EXPORT_DESCRIPTOR_STRUCT        PrmHandlerExportDescriptors[1];+} PRM_MODULE_EXPORT_DESCRIPTOR_STRUCT;++#pragma pack(pop)  // // Filter Types@@ -38,7 +75,8 @@ extern UINT32 mFileBufferSize;

typedef enum {   SECTION_TEXT,   SECTION_HII,-  SECTION_DATA+  SECTION_DATA,+  SECTION_SYMBOL  } SECTION_FILTER_TYPES; @@ -50,6 +88,8 @@ typedef struct {

   BOOLEAN (*WriteSections) (SECTION_FILTER_TYPES  FilterType);   VOID    (*WriteRelocations) ();   VOID    (*WriteDebug) ();+  VOID    (*ScanSymbol) ();+  VOID    (*WriteExport) ();   VOID    (*SetImageSize) ();   VOID    (*CleanUp) (); diff --git a/BaseTools/Source/C/GenFw/GenFw.c b/BaseTools/Source/C/GenFw/GenFw.c

index 8cab70ba4d..c7de5b89d8 100644

--- a/BaseTools/Source/C/GenFw/GenFw.c

+++ b/BaseTools/Source/C/GenFw/GenFw.c

@@ -87,7 +87,7 @@ UINT32 mImageTimeStamp = 0;

UINT32 mImageSize = 0; UINT32 mOutImageType = FW_DUMMY_IMAGE; BOOLEAN mIsConvertXip = FALSE;-+BOOLEAN mExportFlag = FALSE;  STATIC EFI_STATUS@@ -1436,6 +1436,15 @@ Returns:

       continue;     } +    if (stricmp (argv[0], "--PRM") == 0) {+      if (!mExportFlag) {+        mExportFlag = TRUE;+      }+      argc --;+      argv ++;+      continue;+    }+     if (argv[0][0] == '-') {       Error (NULL, 0, 1000, "Unknown option", argv[0]);       goto Finish;diff --git a/BaseTools/Source/C/Include/IndustryStandard/PeImage.h b/BaseTools/Source/C/Include/IndustryStandard/PeImage.h

index f17b8ee19b..21c968e650 100644

--- a/BaseTools/Source/C/Include/IndustryStandard/PeImage.h

+++ b/BaseTools/Source/C/Include/IndustryStandard/PeImage.h

@@ -571,6 +571,13 @@ typedef struct {

   UINT32  AddressOfNameOrdinals; } EFI_IMAGE_EXPORT_DIRECTORY; +//+// Based export types.+//+#define EFI_IMAGE_EXPORT_ORDINAL_BASE     1+#define EFI_IMAGE_EXPORT_ADDR_SIZE        4+#define EFI_IMAGE_EXPORT_ORDINAL_SIZE     2+ /// /// DLL support. /// Import Format--

2.26.2.windows.1