copenssluefiedk2

EDK2 application fails to build when including both LibC & OpensslLib, stdatomic.h not found


I'm modifying EDK2's ShellPkg application to hardcode the arguments and launch a specific efi application through that shell (see here for context). I want to include OpenSSL so I can verify whether the application I want to run is the correct one. I'm using the edk2-stable202305 release.

To do this, I open the file using fopen (which I include from the StdLib package), and then try use the Sha256Init, Sha256Update and Sha256Final methods from EDK2's OpensslLib to verify the hash of the application I want to run through the shell.

The problem is if I try to build the modified ShellPkg (build -p ShellPkg/ShellPkg.dsc -t GCC5 -a X64 -b RELEASE), I get this error:

In file included from /home/zjeffer/git/edk2/edk2/CryptoPkg/Library/OpensslLib/openssl/include/crypto/evp.h:11,
                 from /home/zjeffer/git/edk2/edk2/CryptoPkg/Library/OpensslLib/openssl/crypto/asn1/a_sign.c:22:
/home/zjeffer/git/edk2/edk2/CryptoPkg/Library/OpensslLib/openssl/include/internal/refcount.h:21:12: fatal error: stdatomic.h: No such file or directory
 #  include <stdatomic.h>
            ^~~~~~~~~~~~~
compilation terminated.
make: *** [GNUmakefile:1444: /home/zjeffer/git/edk2/edk2/Build/Shell/RELEASE_GCC5/X64/CryptoPkg/Library/OpensslLib/OpensslLibCrypto/OUTPUT/openssl/crypto/asn1/a_sign.obj] Error 1
@/home/zjeffer/git/edk2/edk2/Build/Shell/RELEASE_GCC5/X64/CryptoPkg/Library/OpensslLib/OpensslLibCrypto/OUTPUT/cc_resp.txt
 -g -Os -fshort-wchar -fno-builtin -fno-strict-aliasing -Wall -Werror -Wno-array-bounds -include AutoGen.h -fno-common -ffunction-sections -fdata-sections -DSTRING_ARRAY_NAME=OpensslLibCryptoStrings -m64 -fno-stack-protector "-DEFIAPI=__attribute__((ms_abi))" -maccumulate-outgoing-args -mno-red-zone -Wno-address -mcmodel=small -fpie -fno-asynchronous-unwind-tables -Wno-address  -fno-omit-frame-pointer -flto -DUSING_LTO -Wno-unused-but-set-variable -Wno-unused-const-variable -U_WIN32 -U_WIN64 -DL_ENDIAN -DOPENSSL_SMALL_FOOTPRINT -D_CRT_SECURE_NO_DEPRECATE -D_CRT_NONSTDC_NO_DEPRECATE -DOPENSSL_NO_EC -DOPENSSL_NO_ECDH -DOPENSSL_NO_ECDSA -DOPENSSL_NO_TLS1_3 -DOPENSSL_NO_SM2 -DOPENSSL_NO_ASM  -Wno-error=maybe-uninitialized -Wno-error=format -Wno-format -Wno-error=unused-but-set-variable -DNO_MSABI_VA_FUNCS -Wno-unused-but-set-variable -D DISABLE_NEW_DEPRECATED_INTERFACES -nostdinc -nostdlib -DUEFI_C_SOURCE -I/home/zjeffer/git/edk2/edk2/CryptoPkg/Library/OpensslLib/openssl/crypto/store -I/home/zjeffer/git/edk2/edk2/CryptoPkg/Library/OpensslLib/openssl/crypto/x509v3 -I/home/zjeffer/git/edk2/edk2/CryptoPkg/Library/OpensslLib/openssl/crypto/x509 -I/home/zjeffer/git/edk2/edk2/CryptoPkg/Library/OpensslLib/openssl/crypto/ui -I/home/zjeffer/git/edk2/edk2/CryptoPkg/Library/OpensslLib/openssl/crypto/txt_db -I/home/zjeffer/git/edk2/edk2/CryptoPkg/Library/OpensslLib/openssl/crypto/stack -I/home/zjeffer/git/edk2/edk2/CryptoPkg/Library/OpensslLib/openssl/crypto/sm4 -I/home/zjeffer/git/edk2/edk2/CryptoPkg/Library/OpensslLib/openssl/crypto/sm3 -I/home/zjeffer/git/edk2/edk2/CryptoPkg/Library/OpensslLib/openssl/crypto/siphash -I/home/zjeffer/git/edk2/edk2/CryptoPkg/Library/OpensslLib/openssl/crypto/sha -I/home/zjeffer/git/edk2/edk2/CryptoPkg/Library/OpensslLib/openssl/crypto/rsa -I/home/zjeffer/git/edk2/edk2/CryptoPkg/Library/OpensslLib/openssl/crypto/rand -I/home/zjeffer/git/edk2/edk2/CryptoPkg/Library/OpensslLib/openssl/crypto/pkcs7 -I/home/zjeffer/git/edk2/edk2/CryptoPkg/Library/OpensslLib/openssl/crypto/pkcs12 -I/home/zjeffer/git/edk2/edk2/CryptoPkg/Library/OpensslLib/openssl/crypto/pem -I/home/zjeffer/git/edk2/edk2/CryptoPkg/Library/OpensslLib/openssl/crypto/ocsp -I/home/zjeffer/git/edk2/edk2/CryptoPkg/Library/OpensslLib/openssl/crypto/objects -I/home/zjeffer/git/edk2/edk2/CryptoPkg/Library/OpensslLib/openssl/crypto/modes -I/home/zjeffer/git/edk2/edk2/CryptoPkg/Library/OpensslLib/openssl/crypto/md5 -I/home/zjeffer/git/edk2/edk2/CryptoPkg/Library/OpensslLib/openssl/crypto/lhash -I/home/zjeffer/git/edk2/edk2/CryptoPkg/Library/OpensslLib/openssl/crypto/kdf -I/home/zjeffer/git/edk2/edk2/CryptoPkg/Library/OpensslLib/openssl/crypto/hmac -I/home/zjeffer/git/edk2/edk2/CryptoPkg/Library/OpensslLib/openssl/crypto/evp -I/home/zjeffer/git/edk2/edk2/CryptoPkg/Library/OpensslLib/openssl/crypto/err -I/home/zjeffer/git/edk2/edk2/CryptoPkg/Library/OpensslLib/openssl/crypto/dso -I/home/zjeffer/git/edk2/edk2/CryptoPkg/Library/OpensslLib/openssl/crypto/dh -I/home/zjeffer/git/edk2/edk2/CryptoPkg/Library/OpensslLib/openssl/crypto -I/home/zjeffer/git/edk2/edk2/CryptoPkg/Library/OpensslLib/openssl/crypto/conf -I/home/zjeffer/git/edk2/edk2/CryptoPkg/Library/OpensslLib/openssl/crypto/comp -I/home/zjeffer/git/edk2/edk2/CryptoPkg/Library/OpensslLib/openssl/crypto/cmac -I/home/zjeffer/git/edk2/edk2/CryptoPkg/Library/OpensslLib/openssl/crypto/buffer -I/home/zjeffer/git/edk2/edk2/CryptoPkg/Library/OpensslLib/openssl/crypto/bn -I/home/zjeffer/git/edk2/edk2/CryptoPkg/Library/OpensslLib/openssl/crypto/bio -I/home/zjeffer/git/edk2/edk2/CryptoPkg/Library/OpensslLib/openssl/crypto/async -I/home/zjeffer/git/edk2/edk2/CryptoPkg/Library/OpensslLib/openssl/crypto/async/arch -I/home/zjeffer/git/edk2/edk2/CryptoPkg/Library/OpensslLib/openssl/crypto/asn1 -I/home/zjeffer/git/edk2/edk2/CryptoPkg/Library/OpensslLib/openssl/crypto/aria -I/home/zjeffer/git/edk2/edk2/CryptoPkg/Library/OpensslLib/openssl/crypto/aes -I/home/zjeffer/git/edk2/edk2/CryptoPkg/Library/OpensslLib/openssl/ms -I/home/zjeffer/git/edk2/edk2/CryptoPkg/Library/OpensslLib/openssl -I/home/zjeffer/git/edk2/edk2/CryptoPkg/Library/OpensslLib -I/home/zjeffer/git/edk2/edk2/Build/Shell/RELEASE_GCC5/X64/CryptoPkg/Library/OpensslLib/OpensslLibCrypto/DEBUG -I/home/zjeffer/git/edk2/edk2/MdePkg -I/home/zjeffer/git/edk2/edk2/MdePkg/Include -I/home/zjeffer/git/edk2/edk2/MdePkg/Test/UnitTest/Include -I/home/zjeffer/git/edk2/edk2/MdePkg/Test/Mock/Include -I/home/zjeffer/git/edk2/edk2/MdePkg/Library/MipiSysTLib/mipisyst/library/include -I/home/zjeffer/git/edk2/edk2/MdePkg/Include/X64 -I/home/zjeffer/git/edk2/edk2/CryptoPkg -I/home/zjeffer/git/edk2/edk2/CryptoPkg/Include -I/home/zjeffer/git/edk2/edk2/CryptoPkg/Private -I/home/zjeffer/git/edk2/edk2/CryptoPkg/Library/Include -I/home/zjeffer/git/edk2/edk2/CryptoPkg/Library/OpensslLib/openssl/include



build.py...
 : error 7000: Failed to execute command
        make tbuild [/home/zjeffer/git/edk2/edk2/Build/Shell/RELEASE_GCC5/X64/CryptoPkg/Library/OpensslLib/OpensslLibCrypto]


build.py...
 : error 7000: Failed to execute command
        make tbuild [/home/zjeffer/git/edk2/edk2/Build/Shell/RELEASE_GCC5/X64/CryptoPkg/Library/BaseCryptLib/BaseCryptLib]


build.py...
 : error F002: Failed to build module
        /home/zjeffer/git/edk2/edk2/CryptoPkg/Library/OpensslLib/OpensslLibCrypto.inf [X64, GCC5, RELEASE]

- Failed -

My Shell.inf has the following changes:

[Packages]
  MdePkg/MdePkg.dec
  ShellPkg/ShellPkg.dec
  MdeModulePkg/MdeModulePkg.dec
  StdLib/StdLib.dec
  CryptoPkg/CryptoPkg.dec

[LibraryClasses]
  BaseLib
  UefiApplicationEntryPoint
  UefiLib
  DebugLib
  MemoryAllocationLib
  ShellCommandLib
  UefiRuntimeServicesTableLib
  UefiBootServicesTableLib
  DevicePathLib
  BaseMemoryLib
  PcdLib
  FileHandleLib
  PrintLib
  HiiLib
  SortLib
  HandleParsingLib
  UefiHiiServicesLib
  LibC
  LibStdio
  LibStdLib
  LibString
  LibWchar
  BaseCryptLib
  OpensslLib
  MemoryAllocationLib

My ShellPkg.dsc has the following changes:

  BaseLib|MdePkg/Library/BaseLib/BaseLib.inf
  BaseMemoryLib|MdePkg/Library/BaseMemoryLib/BaseMemoryLib.inf
  BaseCryptLib|CryptoPkg/Library/BaseCryptLib/BaseCryptLib.inf
  DevicePathLib|MdePkg/Library/UefiDevicePathLib/UefiDevicePathLib.inf
  SafeIntLib|MdePkg/Library/BaseSafeIntLib/BaseSafeIntLib.inf
  SynchronizationLib|MdePkg/Library/BaseSynchronizationLib/BaseSynchronizationLib.inf
  TimerLib|MdePkg/Library/BaseTimerLibNullTemplate/BaseTimerLibNullTemplate.inf
  RngLib|MdePkg/Library/BaseRngLib/BaseRngLib.inf
  OemHookStatusCodeLib|MdeModulePkg/Library/OemHookStatusCodeLibNull/OemHookStatusCodeLibNull.inf
  HashApiLib|CryptoPkg/Library/BaseHashApiLib/BaseHashApiLib.inf
  IntrinsicLib|CryptoPkg/Library/IntrinsicLib/IntrinsicLib.inf
  MemoryAllocationLib|MdePkg/Library/UefiMemoryAllocationLib/UefiMemoryAllocationLib.inf

# I don't know which one of these two I should use, they both give the error.
  # OpensslLib|CryptoPkg/Library/OpensslLib/OpensslLib.inf 
  OpensslLib|CryptoPkg/Library/OpensslLib/OpensslLibCrypto.inf

The error occurs regardless of whether I include any OpenSSL libraries in the Shell.c application.

What's interesting is that if I try to build the CryptoPkg instead (build -p CryptoPkg/CryptoPkg.dsc -t GCC5 -b RELEASE -a X64), which includes the OpensslLib, it builds without issues.


/usr/bin/ld: MemoryIntrinsics.obj (symbol from plugin): in function `memset':
(.text+0x0): multiple definition of `memset'; Misc.obj (symbol from plugin):(.text+0x0): first defined here
/usr/bin/ld: MemoryIntrinsics.obj (symbol from plugin): in function `memset':
(.text+0x0): multiple definition of `memcmp'; Comparison.obj (symbol from plugin):(.text+0x0): first defined here

Solution

  • The BaseCryptLib defines some symbols that will also be defined in LibC. This is required to use OpenSSL without LibC.

    If you want to use both, you must exclude the code files with this definitions from BaseCryptLib. If you only need the fopen, fread,... methods from LibC replace them with the native UEFI methods, this will be the simplest solution.