From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by mx.groups.io with SMTP id smtpd.web10.6037.1591777071280815162 for ; Wed, 10 Jun 2020 01:17:51 -0700 Authentication-Results: mx.groups.io; dkim=missing; spf=pass (domain: arm.com, ip: 217.140.110.172, mailfrom: ard.biesheuvel@arm.com) Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id D195C31B; Wed, 10 Jun 2020 01:17:50 -0700 (PDT) Received: from localhost.localdomain (unknown [10.37.8.184]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 3F5A73F6CF; Wed, 10 Jun 2020 01:17:49 -0700 (PDT) From: "Ard Biesheuvel" To: devel@edk2.groups.io Cc: Ard Biesheuvel , Michael D Kinney , Liming Gao , Jiewen Yao , Sami Mujawar , Ilias Apalodimas Subject: [PATCH 1/5] MdePkg/BasePrintLib: avoid absolute addresses for error strings Date: Wed, 10 Jun 2020 10:17:36 +0200 Message-Id: <20200610081740.54581-2-ard.biesheuvel@arm.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20200610081740.54581-1-ard.biesheuvel@arm.com> References: <20200610081740.54581-1-ard.biesheuvel@arm.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable The mStatusString[] is constructed as an array of pointer-to-char, which means that on X64 or AARCH64, it is emitted as a single linear list of 64-bit quantities, each containing the absolute address of one of the string literals in memory. This means that each string takes up 8 bytes of additional space, along with 2 bytes of relocation data. It also means that the array cannot be used until PE/COFF relocation has completed, and so the following invocation Status =3D PeCoffLoaderRelocateImage (&ImageContext); ASSERT_EFI_ERROR (Status); that we will be introducing into StandaloneMmCore entrypoint for AARCH64 to relocate the executable on the fly is guaranteed to return bogus output or crash, which is less than helpful. So fix both issues, by emitting mStatusString as an array of char arrays instead. The memory footprint increases from 955 to 975 bytes, but given that in the latter case, the overhead consists of 410 NUL characters rather than 390 bytes worth of absolute addresses and relocation records, the impact on a compressed image is actually positive. For example, when building ArmVirtQemu.dsc in RELEASE mode for AARCH64 with the GCC5 profile, I get: Before FV Space Information FVMAIN [99%Full] 4784768 total, 4784720 used, 48 free FVMAIN_COMPACT [38%Full] 2093056 total, 811560 used, 1281496 free After FV Space Information FVMAIN [99%Full] 4780672 total, 4780624 used, 48 free FVMAIN_COMPACT [38%Full] 2093056 total, 813488 used, 1279568 free So the compressed image is 4 KB smaller, whereas the entire image is < 2 KB larger, which is in the order of 0.2 % Signed-off-by: Ard Biesheuvel --- MdePkg/Library/BasePrintLib/PrintLibInternal.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MdePkg/Library/BasePrintLib/PrintLibInternal.c b/MdePkg/Librar= y/BasePrintLib/PrintLibInternal.c index b6ec5ac4fbb9..c8b932c7e07a 100644 --- a/MdePkg/Library/BasePrintLib/PrintLibInternal.c +++ b/MdePkg/Library/BasePrintLib/PrintLibInternal.c @@ -27,7 +27,7 @@ =0D GLOBAL_REMOVE_IF_UNREFERENCED CONST CHAR8 mHexStr[] =3D {'0','1','2','3','= 4','5','6','7','8','9','A','B','C','D','E','F'};=0D =0D -GLOBAL_REMOVE_IF_UNREFERENCED CONST CHAR8 * CONST mStatusString[] =3D {=0D +GLOBAL_REMOVE_IF_UNREFERENCED CONST CHAR8 mStatusString[][25] =3D {=0D "Success", // RETURN_SUCCESS =3D 0= =0D "Warning Unknown Glyph", // RETURN_WARN_UNKNOWN_GLYPH =3D 1= =0D "Warning Delete Failure", // RETURN_WARN_DELETE_FAILURE =3D 2= =0D --=20 2.26.2