BaseTools/GenFw: Parse IBT/BTI support status from ELF note

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

When performing ELF to PE/COFF conversion, parse any notes sections to
decide whether the image supports forward CFI landing pads. This will be
used to set the associated DllCharacteristicsEx flag in a subsequent
patch.

Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
Reviewed-by: Leif Lindholm <quic_llindhol@quicinc.com>
Reviewed-by: Oliver Smith-Denny <osde@linux.microsoft.com>
Acked-by: Michael Kubacki <michael.kubacki@microsoft.com>
Reviewed-by: Liming Gao <gaoliming@byosoft.com.cn>
This commit is contained in:
Ard Biesheuvel
2023-03-26 10:38:01 +02:00
committed by mergify[bot]
parent cdf6ff1719
commit 6c299acf48
2 changed files with 59 additions and 0 deletions

View File

@@ -770,6 +770,49 @@ WriteSectionRiscV64 (
}
}
STATIC UINT16 mDllCharacteristicsEx;
STATIC
VOID
ParseNoteSection (
CONST Elf_Shdr *Shdr
)
{
CONST Elf_Note *Note;
CONST UINT32 *Prop;
UINT32 Prop0;
UINT32 Prop2;
Note = (Elf_Note *)((UINT8 *)mEhdr + Shdr->sh_offset);
if ((Note->n_type == NT_GNU_PROPERTY_TYPE_0) &&
(Note->n_namesz == sizeof ("GNU")) &&
(strcmp ((CHAR8 *)(Note + 1), "GNU") == 0) &&
(Note->n_descsz > sizeof (UINT32[2]))) {
Prop = (UINT32 *)((UINT8 *)(Note + 1) + sizeof("GNU"));
switch (mEhdr->e_machine) {
case EM_AARCH64:
Prop0 = GNU_PROPERTY_AARCH64_FEATURE_1_AND;
Prop2 = GNU_PROPERTY_AARCH64_FEATURE_1_BTI;
break;
case EM_X86_64:
Prop0 = GNU_PROPERTY_X86_FEATURE_1_AND;
Prop2 = GNU_PROPERTY_X86_FEATURE_1_IBT;
break;
default:
return;
}
if ((Prop[0] == Prop0) &&
(Prop[1] >= sizeof (UINT32)) &&
((Prop[2] & Prop2) != 0)) {
mDllCharacteristicsEx |= EFI_IMAGE_DLLCHARACTERISTICS_EX_FORWARD_CFI_COMPAT;
}
}
}
//
// Elf functions interface implementation
//
@@ -826,6 +869,13 @@ ScanSections64 (
}
}
for (i = 0; i < mEhdr->e_shnum; i++) {
Elf_Shdr *shdr = GetShdrByIndex(i);
if (shdr->sh_type == SHT_NOTE) {
ParseNoteSection (shdr);
}
}
//
// Check if mCoffAlignment is larger than MAX_COFF_ALIGNMENT
//