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.web11.2430.1591817462836258259 for ; Wed, 10 Jun 2020 12:31:03 -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 A69831FB; Wed, 10 Jun 2020 12:31:01 -0700 (PDT) Received: from dogfood.home (unknown [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id D0FA13F66F; Wed, 10 Jun 2020 12:30:59 -0700 (PDT) From: "Ard Biesheuvel" To: devel@edk2.groups.io Cc: Ard Biesheuvel , "Kinney, Michael D" , "Gao, Liming" Subject: [PATCH v2] MdePkg/BasePrintLib: avoid absolute addresses for error strings Date: Wed, 10 Jun 2020 21:30:23 +0200 Message-Id: <20200610193023.171092-1-ard.biesheuvel@arm.com> X-Mailer: git-send-email 2.26.2 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable The mStatusString[] array 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 extra work needs to be done at runtime to process these relocations, every time a module is loaded that incorporates this library. So fix both issues, by splitting mStatusString into two arrays of char arrays. The memory footprint decreases from 955 to 843 bytes, and given that in the latter case, the overhead consists of 278 NUL characters rather than 390 bytes worth of absolute addresses and relocation records, the size of a compressed image is reduced even further. For example, when building ArmVirtQemu.dsc in RELEASE mode for AARCH64 with the GCC5 profile, I get: Before FV Space Information FVMAIN [100%Full] 5329920 total, 5329920 used, 0 free FVMAIN_COMPACT [38%Full] 2093056 total, 811840 used, 1281216 free After FV Space Information FVMAIN [100%Full] 5321728 total, 5321728 used, 0 free FVMAIN_COMPACT [38%Full] 2093056 total, 809696 used, 1283360 free So the uncompressed contents of the compressed image are 8 KB smaller, whereas the resulting flash image (consisting of the compressed image along with SEC, PEI_CORE and a set of PEIMs that execute in place) is ~2 KB smaller. Cc: "Kinney, Michael D" Cc: "Gao, Liming" Signed-off-by: Ard Biesheuvel --- v2: - split off this patch from the StandaloneMmPkg series, since they are not interdependent anyway, and so they can be discussed separately - remove mention of StandaloneMmPkg from the commit log - the space savings by themselves are sufficient justification - update the before/after numbers with the current results - split the warnings and errors into a separate array, so that the latter can use smaller entries - clarify the commit log to explain the effect on compressed as well as XIP images (which both get smaller) MdePkg/Library/BasePrintLib/PrintLibInternal.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/MdePkg/Library/BasePrintLib/PrintLibInternal.c b/MdePkg/Librar= y/BasePrintLib/PrintLibInternal.c index b6ec5ac4fbb9..522c3bb5dcb9 100644 --- a/MdePkg/Library/BasePrintLib/PrintLibInternal.c +++ b/MdePkg/Library/BasePrintLib/PrintLibInternal.c @@ -27,13 +27,16 @@ =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 mWarningString[][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 "Warning Write Failure", // RETURN_WARN_WRITE_FAILURE =3D 3= =0D "Warning Buffer Too Small", // RETURN_WARN_BUFFER_TOO_SMALL =3D 4= =0D "Warning Stale Data", // RETURN_WARN_STALE_DATA =3D 5= =0D +};=0D +=0D +GLOBAL_REMOVE_IF_UNREFERENCED CONST CHAR8 mErrorString[][21] =3D {=0D "Load Error", // RETURN_LOAD_ERROR =3D 1 = | MAX_BIT=0D "Invalid Parameter", // RETURN_INVALID_PARAMETER =3D 2 = | MAX_BIT=0D "Unsupported", // RETURN_UNSUPPORTED =3D 3 = | MAX_BIT=0D @@ -996,12 +999,12 @@ BasePrintLibSPrintMarker ( //=0D Index =3D Status & ~MAX_BIT;=0D if (Index > 0 && Index <=3D ERROR_STATUS_NUMBER) {=0D - ArgumentString =3D mStatusString [Index + WARNING_STATUS_NUMBE= R];=0D + ArgumentString =3D mErrorString [Index - 1];=0D }=0D } else {=0D Index =3D Status;=0D if (Index <=3D WARNING_STATUS_NUMBER) {=0D - ArgumentString =3D mStatusString [Index];=0D + ArgumentString =3D mWarningString [Index];=0D }=0D }=0D if (ArgumentString =3D=3D ValueBuffer) {=0D --=20 2.26.2