]> git.proxmox.com Git - mirror_edk2.git/commitdiff
MdePkg/BasePrintLib: avoid absolute addresses for error strings
authorArd Biesheuvel <ard.biesheuvel@arm.com>
Wed, 10 Jun 2020 07:36:35 +0000 (09:36 +0200)
committermergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
Tue, 16 Jun 2020 09:14:17 +0000 (09:14 +0000)
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.

Signed-off-by: Ard Biesheuvel <ard.biesheuvel@arm.com>
Reviewed-by: Zhiguang Liu <zhiguang.liu@intel.com>
Reviewed-by: Liming Gao <liming.gao@intel.com>
MdePkg/Library/BasePrintLib/PrintLibInternal.c

index b6ec5ac4fbb98982f8ccaf3908c2a91ce583e31e..50c6e8559c43dd0ee956a7320db4bd8e5c94f0b9 100644 (file)
 \r
 GLOBAL_REMOVE_IF_UNREFERENCED CONST CHAR8 mHexStr[] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};\r
 \r
-GLOBAL_REMOVE_IF_UNREFERENCED CONST CHAR8 * CONST mStatusString[] = {\r
+//\r
+// Longest string: RETURN_WARN_BUFFER_TOO_SMALL => 24 characters plus NUL byte\r
+//\r
+GLOBAL_REMOVE_IF_UNREFERENCED CONST CHAR8 mWarningString[][24+1] = {\r
   "Success",                      //  RETURN_SUCCESS                = 0\r
   "Warning Unknown Glyph",        //  RETURN_WARN_UNKNOWN_GLYPH     = 1\r
   "Warning Delete Failure",       //  RETURN_WARN_DELETE_FAILURE    = 2\r
   "Warning Write Failure",        //  RETURN_WARN_WRITE_FAILURE     = 3\r
   "Warning Buffer Too Small",     //  RETURN_WARN_BUFFER_TOO_SMALL  = 4\r
   "Warning Stale Data",           //  RETURN_WARN_STALE_DATA        = 5\r
+};\r
+\r
+//\r
+// Longest string: RETURN_INCOMPATIBLE_VERSION => 20 characters plus NUL byte\r
+//\r
+GLOBAL_REMOVE_IF_UNREFERENCED CONST CHAR8 mErrorString[][20+1] = {\r
   "Load Error",                   //  RETURN_LOAD_ERROR             = 1  | MAX_BIT\r
   "Invalid Parameter",            //  RETURN_INVALID_PARAMETER      = 2  | MAX_BIT\r
   "Unsupported",                  //  RETURN_UNSUPPORTED            = 3  | MAX_BIT\r
@@ -996,12 +1005,12 @@ BasePrintLibSPrintMarker (
           //\r
           Index = Status & ~MAX_BIT;\r
           if (Index > 0 && Index <= ERROR_STATUS_NUMBER) {\r
-            ArgumentString = mStatusString [Index + WARNING_STATUS_NUMBER];\r
+            ArgumentString = mErrorString [Index - 1];\r
           }\r
         } else {\r
           Index = Status;\r
           if (Index <= WARNING_STATUS_NUMBER) {\r
-            ArgumentString = mStatusString [Index];\r
+            ArgumentString = mWarningString [Index];\r
           }\r
         }\r
         if (ArgumentString == ValueBuffer) {\r