0

below code snippet,How can we make below struct define as 4 bytes aligned?

I have tried pragma pack to aligned Explicitly.But It's does NOT work as we want...

#pragma pack(push)
#pragma pack(4)
//..struct devine here.
#pragma pack(pop);

I have tried __attribute__ to aligned Explicitly.It's also does NOT work as well..

struct device_info {
    //
}__attribute__(packed);
//struct device_info dev_info;
sizeof (struct device_info)=211

I have tried __attribute__ to aligned Explicitly.It's also does NOT work as well..

struct device_info {
    //
};
typedef struct device_info deice_info __attribute__(packed)
device_info dev_info;
sizeof (dev_info)=211

Question1:

How can I aligned struct device_info Explicitly,and offsetof output like below:

    offsetof magic=0
    offsetof is_unlocked=16
    offsetof is_tampered=20
    offsetof is_unlock_critical=24
    offsetof charger_screen_enabled=28
    offsetof display_panel=32
    offsetof booloader_version=96
    offsetof radio_version=160
    offsetof verity_mode=224
    offsetof is_origin=228

code snippet:

#define DEVICE_MAGIC_SIZE 13
#define MAX_PANEL_ID_LEN 64
#define MAX_VERSION_LEN 64
struct device_info{
    unsigned char magic[DEVICE_MAGIC_SIZE];
    bool is_unlocked;
    bool is_tampered;
    bool is_unlock_critical;
    bool charger_screen_enabled;
    char display_panel[MAX_PANEL_ID_LEN];
    char booloader_version[MAX_VERSION_LEN];
    char radio_version[MAX_VERSION_LEN;
    bool verity_mode;
    bool is_origin;
};

Update 1

I have tried below snippet

struct device_info{
    __attribute__((__aligned(4))) bool is_unlock;
    //...
};

It's same that work well.

Question two:

What's difference between

struct device_info{
    __attribute__((__aligned(4))) bool is_unlock;
    //...
};

and

struct device_info{
    bool is_unlock;
    //..
}__attribute__((__aligned(4))) ;

Question 3

How can we dump during source code compiled to execute binary or library?(preprocessing->Compile->link) objdump?or others?

caopeng
  • 914
  • 13
  • 23
  • C++ and C are two different languages, and the answer to your question will differ depending on which language you're actually talking about. Please pick one, and remove the other language from your question tags. – paddy Mar 20 '17 at 02:09
  • `#pragma pack(40)` seems a bit off to me. As for the attribute, you are trying to apply it to a `typedef`, I don't think it's permitted. – Matteo Italia Mar 20 '17 at 02:11
  • @MatteoItalia, sorry for mistakes ..`#pragma pack(4)` – caopeng Mar 20 '17 at 02:14
  • Looks like if you used a 4-byte type instead of `bool` everything would align properly without any tricks. – Mark Tolonen Mar 20 '17 at 02:15
  • @paddy, thanks your suggestion. – caopeng Mar 20 '17 at 02:16
  • Also, beware of using `sizeof` to infer anything about padding or alignment. If you want to know about alignment, create an array of 2 structures, and subtract their addresses. – aghast Mar 20 '17 at 02:28
  • `#pragma pack(push)` is not defined in C. If code needs to use this extension, tag question with the compiler code is using. – chux - Reinstate Monica Mar 20 '17 at 02:57
  • The deeper question is why "...make below struct define as 4 bytes aligned?" is even needed? – chux - Reinstate Monica Mar 20 '17 at 02:58
  • 1
    @AustinHastings the difference in bytes between address of two array elements, is the size of the type of the array element – M.M Mar 20 '17 at 03:48

2 Answers2

2

Since C11 you can use the standard alignas specifier. This should solve your question.

David Ranieri
  • 39,972
  • 7
  • 52
  • 94
0

It would appear that in your implementation, sizeof(bool) is equal to 1. In that case, the padding rules are being correctly adhered to, so is_unlocked can appear precisely after magic with no additional padding.

The other thing is that bool is implementation-defined. Please read this post: Is sizeof(bool) defined?

What you might benefit more from, is to use proper types from <ctype.h> when you are laying out a structure with absolute sizes. If you want your bool values to be 32-bit integers, then use int32_t or uint32_t instead.

Community
  • 1
  • 1
paddy
  • 60,864
  • 6
  • 61
  • 103