Architecture-Specific Libraries and Modules Explained
Let me explain what architecture-specific libraries and modules are with examples from the EDK II codebase.
What Are Architecture-Specific Libraries and Modules?
Architecture-specific libraries and modules are implementations that are tailored for specific CPU architectures (IA32, X64, ARM, AARCH64, RISCV64, etc.) and module types (SEC, PEIM, DXE_DRIVER, SMM_DRIVER, etc.). They allow the same abstract interface to have different implementations optimized for different execution environments.
1. Architecture-Specific Libraries
Example 1: Random Number Generation (RngLib)
[LibraryClasses.IA32, LibraryClasses.X64, LibraryClasses.AARCH64]
RngLib|MdePkg/Library/BaseRngLib/BaseRngLib.inf
Why different for different architectures?
Implementation Differences:
[Sources.Ia32, Sources.X64]
Rand/RdRand.c
[Sources.AARCH64]
AArch64/Rndr.c
AArch64/ArmRng.h
AArch64/ArmRng.S | GCC
AArch64/ArmRng.asm | MSFT
[Sources.RISCV64]
Riscv/Rng.c
Riscv/Seed.S | GCC
Example 2: ARM Architecture Libraries
[LibraryClasses.ARM, LibraryClasses.AARCH64]
ArmLib|ArmPkg/Library/ArmLib/ArmBaseLib.inf
Why different for ARM vs AARCH64?
Implementation Differences:
[Sources.ARM]
Arm/ArmV7Lib.h
Arm/ArmV7Lib.c
Arm/ArmLibSupport.S | GCC
Arm/ArmLibSupportV7.S | GCC
Arm/ArmV7Support.S | GCC
Arm/ArmV7ArchTimerSupport.S | GCC
[Sources.AARCH64]
AArch64/AArch64Lib.h
AArch64/AArch64Lib.c
AArch64/ArmLibSupport.S
AArch64/ArmLibSupportV8.S
AArch64/AArch64Support.S
AArch64/AArch64ArchTimerSupport.S
2. Module Type-Specific Libraries
Example 1: PEI vs DXE vs SMM Cryptography
[LibraryClasses.common.PEIM]
BaseCryptLib|CryptoPkg/Library/BaseCryptLib/PeiCryptLib.inf
TlsLib|CryptoPkg/Library/TlsLibNull/TlsLibNull.inf
[LibraryClasses.common.DXE_DRIVER]
BaseCryptLib|CryptoPkg/Library/BaseCryptLib/BaseCryptLib.inf
TlsLib|CryptoPkg/Library/TlsLib/TlsLib.inf
[LibraryClasses.common.DXE_SMM_DRIVER]
BaseCryptLib|CryptoPkg/Library/BaseCryptLib/SmmCryptLib.inf
TlsLib|CryptoPkg/Library/TlsLibNull/TlsLibNull.inf
Why different for different module types?
Example 2: PEI Services Table Pointer (Architecture + Module Type)
[LibraryClasses.IA32.PEIM, LibraryClasses.X64.PEIM]
PeiServicesTablePointerLib|MdePkg/Library/PeiServicesTablePointerLibIdt/PeiServicesTablePointerLibIdt.inf
Why this specific combination?
3. Real-World Examples from ArmVirtPkg
Recommended by LinkedIn
Platform Library Selection by Architecture:
[LibraryClasses.AARCH64]
ArmPlatformLib|ArmVirtPkg/Library/ArmPlatformLibQemu/ArmPlatformLibQemu.inf
[LibraryClasses.ARM]
ArmPlatformLib|ArmPlatformPkg/Library/ArmPlatformLibNull/ArmPlatformLibNull.inf
Why different implementations?
Cryptography Library Selection by Architecture + Module Type:
[LibraryClasses.AARCH64.PEIM]
ArmMmuLib|UefiCpuPkg/Library/ArmMmuLib/ArmMmuPeiLib.inf
BaseCryptLib|CryptoPkg/Library/BaseCryptLib/PeiCryptLib.inf
[LibraryClasses.ARM.PEIM]
BaseCryptLib|CryptoPkg/Library/BaseCryptLibMbedTls/PeiCryptLib.inf
Why different cryptography implementations?
4. How the Build System Resolves These
Resolution Priority (Most Specific Wins):
Example Resolution Chain:
When building a PEIM module for AARCH64 that needs BaseCryptLib:
5. Benefits of This Approach
1. Performance Optimization
2. Memory Efficiency
3. Platform Flexibility
4. Maintainability
Summary
Architecture-specific libraries and modules in EDK II allow the same abstract interface (like RngLib or BaseCryptLib) to have different implementations optimized for:
This system provides the flexibility to optimize performance and memory usage while maintaining a consistent API across the entire UEFI firmware stack.