[LLVMdev] structure packing and misaligned members

Tim Northover t.p.northover at gmail.com
Sun May 12 10:56:05 PDT 2013


Hi,

> Okay, so I will track that on my own. Though I'm a bit confused then by
> what clang is emitted for packed C structures. It appears an "int*" is
> always loaded with "align 8" regardless of where it comes from. I know C
> says you have to be responsible for your own alignment, so is this
> result from clang correct?

Yes. The various packed-struct extensions aren't specified very well
since they're not in the standard, but in general I think you only get
(guaranteed) sane behaviour if you access their members through an
lvalue whose type is related to that packed struct. So given:

struct __attribute__((packed)) Mine { char a; int b; };

the following works:

int foo(struct Mine *m) {
  return m->b;
}

but this won't necessarily work:

int foo(struct Mine *m) {
  int *b_pointer = &m->b
  return *b_pointer;
}

Tim.



More information about the llvm-dev mailing list