From: Dandan Bi <dandan.bi@intel.com>
To: edk2-devel@lists.01.org
Cc: Eric Dong <eric.dong@intel.com>, Liming Gao <liming.gao@intel.com>
Subject: [patch] MdeModulePkg/DriverSampleDxe: Make bit fields aligned in C structure
Date: Thu, 8 Feb 2018 22:28:39 +0800 [thread overview]
Message-ID: <1518100119-6448-1-git-send-email-dandan.bi@intel.com> (raw)
For a structure with a series of bit fields and used as a storage
in vfr file, and if the bit fields do not add up to the size of
the defined type.In the C code use sizeof() to get size of the
structure, the results may vary form the compiler(VS,GCC...).
But the size of the storage calculated by VfrCompiler is fixed
(calculate with alignment).To avoid the issue cased by above case,
we need to make the total width of the bit fields in the structure
aligned with the size of the defined type for these bit fields.
Cc: Eric Dong <eric.dong@intel.com>
Cc: Liming Gao <liming.gao@intel.com>
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Dandan Bi <dandan.bi@intel.com>
---
MdeModulePkg/Universal/DriverSampleDxe/NVDataStruc.h | 11 +++++++++++
1 file changed, 11 insertions(+)
diff --git a/MdeModulePkg/Universal/DriverSampleDxe/NVDataStruc.h b/MdeModulePkg/Universal/DriverSampleDxe/NVDataStruc.h
index 40fb3d0..af3d4bc 100644
--- a/MdeModulePkg/Universal/DriverSampleDxe/NVDataStruc.h
+++ b/MdeModulePkg/Universal/DriverSampleDxe/NVDataStruc.h
@@ -33,10 +33,18 @@ Revision History:
#define CONFIGURATION_VARSTORE_ID 0x1234
#define BITS_VARSTORE_ID 0x2345
#pragma pack(1)
+//
+// !!! For a structure with a series of bit fields and used as a storage in vfr file, and if the bit fields do not add up to the size of the defined type.
+// In the C code use sizeof() to get the size the strucure, the results may vary form the compiler(VS,GCC...).
+// But the size of the storage calculated by VfrCompiler is fixed (calculate with alignment).
+// To avoid above case, we need to make the total bit width in the structure aligned with the size of the defined type for these bit fields. We can:
+// 1. Add bit field (with/without name) with remianing with for padding.
+// 2. Add unnamed bit field with 0 for padding, the amount of padding is determined by the alignment characteristics of the members of the structure.
+//
typedef struct {
UINT16 NestByteField;
UINT8 : 1; // unamed field can be used for padding
UINT8 NestBitCheckbox : 1;
UINT8 NestBitOneof : 2;
@@ -82,11 +90,13 @@ typedef struct {
EFI_HII_TIME Time;
UINT8 RefreshGuidCount;
UINT8 Match2;
UINT8 GetDefaultValueFromCallBackForOrderedList[3];
UINT8 BitCheckbox : 1;
+ UINT8 ReservedBits: 7; // Reserved bit fields for padding.
UINT16 BitOneof : 6;
+ UINT16 : 0; // Width 0 used to force alignment.
UINT16 BitNumeric : 12;
MY_BITS_DATA MyBitData;
MY_EFI_UNION_DATA MyUnionData;
} DRIVER_SAMPLE_CONFIGURATION;
@@ -107,10 +117,11 @@ typedef struct {
MY_BITS_DATA BitsData;
UINT32 EfiBitGrayoutTest : 5;
UINT32 EfiBitNumeric : 4;
UINT32 EfiBitOneof : 10;
UINT32 EfiBitCheckbox : 1;
+ UINT32 : 0; // Width 0 used to force alignment.
} MY_EFI_BITS_VARSTORE_DATA;
//
// Labels definition
//
--
1.9.5.msysgit.1
next reply other threads:[~2018-02-08 14:23 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-02-08 14:28 Dandan Bi [this message]
2018-02-09 6:56 ` [patch] MdeModulePkg/DriverSampleDxe: Make bit fields aligned in C structure Gao, Liming
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-list from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1518100119-6448-1-git-send-email-dandan.bi@intel.com \
--to=devel@edk2.groups.io \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox