llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

bit fields not packed

Open ahatanak opened this issue 15 years ago • 7 comments

Bugzilla Link 9560
Resolution FIXED
Resolved on Dec 01, 2011 20:39
Version trunk
OS Linux
CC @asl,@efriedma-quic

Extended Description

gcc and clang lay outs bit fields differently. clang starts bit field c at a 16-bit boundary of short types, but gcc packs all bit fields.

.c file (based on bf-ms-layout.c from gcc torture test)

#include <stddef.h> #include <string.h> #include <stdio.h>

extern void abort();

#pragma pack(8)

struct three { short d; unsigned short a:3; unsigned short b:9; unsigned char c:7; } ;

#define val(s,f) (s.f)

#define check_struct(_X)
{
printf("sizeof %s: %d %d\n", #_X, sizeof(struct X), exp_sizeof##X);
memcpy(&test
##X, filler, sizeof(test##_X));
printf("val %s: %d %d\n", #X, val(test##X,c), exp##_X##_c);
}

int main(){ unsigned char filler[16]; struct three test_three;

size_t exp_sizeof_three = 6; unsigned char exp_three_c = 64;

unsigned char i; for ( i = 0; i < 16; i++ ) filler[i] = i;

check_struct (three);

return 0; };

.ll file

clang bitfield11.c -emit-llvm -o - -S -O3

; ModuleID = 'bitfield11.c' target datalayout = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-a0:0:64-f80:32:32-n8:16:32" target triple = "i386-pc-linux-gnu"

@.str = private unnamed_addr constant [18 x i8] c"sizeof %s: %d %d\0A\00" @.str1 = private unnamed_addr constant [6 x i8] c"three\00" @.str2 = private unnamed_addr constant [15 x i8] c"val %s: %d %d\0A\00"

define i32 @​main() nounwind { entry: %call = call i32 (i8*, ...)* @​printf(i8* getelementptr inbounds ([18 x i8]* @.str, i32 0, i32 0), i8* getelementptr inbounds ([6 x i8]* @.str1, i32 0, i32 0), i32 6, i32 6) nounwind %call9 = call i32 (i8*, ...)* @​printf(i8* getelementptr inbounds ([15 x i8]* @.str2, i32 0, i32 0), i8* getelementptr inbounds ([6 x i8]* @.str1, i32 0, i32 0), i32 4, i32 64) nounwind ret i32 0 }

declare i32 @​printf(i8* nocapture, ...) nounwind

result when compiled with gcc and executed

sizeof three: 6 6 val three: 64 64

result when compiled with clang and executed

sizeof three: 6 6 val three: 4 64

ahatanak avatar Mar 26 '11 00:03 ahatanak

r145673.

efriedma-quic avatar Dec 02 '11 04:12 efriedma-quic

Thanks, but that doesn't actually explain how "#pragma pack(8)" affects the alignment of bitfields which clearly would not be aligned to greater than 8 bytes.

efriedma-quic avatar Aug 12 '11 19:08 efriedma-quic

clang supports the documented effect of "#pragma pack(8)"; again, there's some undocumented effect which someone needs to work out before it can be implemented. (I guess the simplest possibility is that bitfields are being treated as if attribute((packed)) was applied.)

Seems like the gcc reference manual is clear that all structure members other than zero width bit fields, unions and classes apply here.

5.52.7 Structure-Packing Pragmas

For compatibility with Win32, GCC supports a set of #pragma directives which change the maximum alignment of members of structures (other than zero-width bitfields), unions, and classes subsequently defined. The n value below always is required to be a small power of two and specifies the new alignment in bytes.

#pragma pack(n) simply sets the new alignment. #pragma pack() sets the alignment to the one that was in effect when compilation started (see also command line option -fpack-struct[=] see Code Gen Options). #pragma pack(push[,n]) pushes the current alignment setting on an internal stack and then optionally sets the new alignment. #pragma pack(pop) restores the alignment setting to the one saved at the top of the internal stack (and removes that stack entry). Note that #pragma pack([n]) does not influence this internal stack; thus it is possible to have #pragma pack(push) followed by multiple #pragma pack(n) instances and finalized by a single #pragma pack(pop).

llvmbot avatar Aug 12 '11 17:08 llvmbot

clang supports the documented effect of "#pragma pack(8)"; again, there's some undocumented effect which someone needs to work out before it can be implemented. (I guess the simplest possibility is that bitfields are being treated as if attribute((packed)) was applied.)

efriedma-quic avatar Mar 26 '11 02:03 efriedma-quic

Hmm... looks like "#pragma pack(8)" has some undocumented effects on bitfields.

commenting out the line didn't make a difference. Same bitcode and same result when executed.

I realized executables produced by gcc and clang show the same result when the line is removed.

sizeof three: 6 6 val three: 4 64

Do you plan to add support for #pragma pack(8) in the future?

ahatanak avatar Mar 26 '11 01:03 ahatanak

Hmm... looks like "#pragma pack(8)" has some undocumented effects on bitfields.

commenting out the line didn't make a difference. Same bitcode and same result when executed.

ahatanak avatar Mar 26 '11 00:03 ahatanak

Hmm... looks like "#pragma pack(8)" has some undocumented effects on bitfields.

efriedma-quic avatar Mar 26 '11 00:03 efriedma-quic