Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 713 Vote(s) - 3.48 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Are unpacked struct in packed struct automatically packed?

#1
Are unpacked struct in packed struct automatically packed by GCC?

In other words, do `__packed__` attribute automatically propagates to nested structures?

That is to say:

struct unpackedStruct{
int16_t field1;
int32_t field2;
// etc...
}

struct packedStruct{
int16_t field1;
struct unpackedStruct struct1; // <-- Is this struct packed?
// etc...
} __attribute__((__packed__));
Reply

#2
In practice it does not: see

[To see links please register here]

. Note the `.zero 2` of padding between the two members of the `unpackedStruct` member.

This situation is explicitly mentioned in the [manual](

[To see links please register here]

), with an example almost identical to yours:

> In the following example `struct my_packed_struct`’s members are packed closely together, **but the internal layout of its `s` member is not packed**—to do that, `struct my_unpacked_struct` needs to be packed too.
```
struct my_unpacked_struct
{
char c;
int i;
};

struct __attribute__ ((__packed__)) my_packed_struct
{
char c;
int i;
struct my_unpacked_struct s;
};
```
<!--
If it did, then code like the following would break:
```
void bar(unpackedStruct *u);
void foo(void) {
packedStruct p;
bar(&p.struct1);
}
```
since `bar()` will naturally be expecting `unpackedStruct` to have its ordinary layout. Moreover, there would be no apparent way to fix it - you would have to somehow write an alternative `bar()` that also takes a pointer to an `unpackedStruct` but "a packed one this time". I don't know of any language features to even express that.
-->
The basic idea is that every object of a given type should have the same layout, so that code operating on that type will work on every object of that type. So `packed` has to apply to a *type*, and you can't have some objects of that type packed and others not.
Reply

#3
No - packing is not recursive so every member needs to be packed itself.
Reply

#4
No, the inner structure is not packed. In [this Godbolt example](

[To see links please register here]

), we can see that `struct foo` is not packed inside `struct bar`, which has the `packed` attribute; the `struct bar` object created contains three bytes of padding (visible as `.zero 3`) inside its `struct foo` member, between the `struct foo` members `c` and `i`.

[Current documentation for GCC 10.2](

[To see links please register here]

) explicitly says the internal layout of a member of a packed structure is not packed (because of the attribute on the outer structure; it could be packed due to its own definition, of course).

(In [older documentation](

[To see links please register here]

) that said that applying `packed` to a structure is equivalent to applying it to its members, it meant the effect of applying `packed` to the “variable” that is the member, described in [the documentation for variable attributes](

[To see links please register here]

). When `packed` is applied to a structure member, it causes the member’s alignment requirement to be one byte. That is, it eliminates padding between previous members and that member, because no padding is needed to make it aligned. It does not alter the representation of the member itself. If that member is an unpacked structure, it remains, internally, an unpacked structure.)
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through