Skip to content

Commit 7c25ae8

Browse files
authored
Set dllimport on Objective C ivar offsets (#107604)
Ensures that offsets for instance variables are marked with `dllimport` if the interface to which they belong has this attribute.
1 parent 334873f commit 7c25ae8

File tree

3 files changed

+21
-4
lines changed

3 files changed

+21
-4
lines changed

clang/lib/CodeGen/CGObjCGNU.cpp

+9-2
Original file line numberDiff line numberDiff line change
@@ -1699,11 +1699,18 @@ class CGObjCGNUstep2 : public CGObjCGNUstep {
16991699
llvm::Value *EmitIvarOffset(CodeGenFunction &CGF,
17001700
const ObjCInterfaceDecl *Interface,
17011701
const ObjCIvarDecl *Ivar) override {
1702-
const std::string Name = GetIVarOffsetVariableName(Ivar->getContainingInterface(), Ivar);
1702+
const ObjCInterfaceDecl *ContainingInterface =
1703+
Ivar->getContainingInterface();
1704+
const std::string Name =
1705+
GetIVarOffsetVariableName(ContainingInterface, Ivar);
17031706
llvm::GlobalVariable *IvarOffsetPointer = TheModule.getNamedGlobal(Name);
1704-
if (!IvarOffsetPointer)
1707+
if (!IvarOffsetPointer) {
17051708
IvarOffsetPointer = new llvm::GlobalVariable(TheModule, IntTy, false,
17061709
llvm::GlobalValue::ExternalLinkage, nullptr, Name);
1710+
if (Ivar->getAccessControl() != ObjCIvarDecl::Private &&
1711+
Ivar->getAccessControl() != ObjCIvarDecl::Package)
1712+
CGM.setGVProperties(IvarOffsetPointer, ContainingInterface);
1713+
}
17071714
CharUnits Align = CGM.getIntAlign();
17081715
llvm::Value *Offset =
17091716
CGF.Builder.CreateAlignedLoad(IntTy, IvarOffsetPointer, Align);

clang/test/CodeGenObjC/dllstorage.m

+2-2
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ @interface M : I {
112112
// CHECK-IR-DAG: @"OBJC_IVAR_$_M._ivar" = external dllimport global i32
113113

114114
// CHECK-NF-DAG: @"$_OBJC_REF_CLASS_M" = external dllimport global ptr
115-
// CHECK-NF-DAG: @"__objc_ivar_offset_M._ivar.@" = external global i32
115+
// CHECK-NF-DAG: @"__objc_ivar_offset_M._ivar.@" = external dllimport global i32
116116

117117
__declspec(dllexport)
118118
__attribute__((__objc_exception__))
@@ -151,7 +151,7 @@ id f(Q *q) {
151151

152152
// CHECK-IR-DAG: @"OBJC_IVAR_$_M._ivar" = external dllimport global i32
153153

154-
// CHECK-NF-DAG: @"__objc_ivar_offset_M._ivar.@" = external global i32
154+
// CHECK-NF-DAG: @"__objc_ivar_offset_M._ivar.@" = external dllimport global i32
155155

156156
int g(void) {
157157
@autoreleasepool {

clang/test/SemaObjC/ivar-access-tests.m

+10
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
@interface MySuperClass
44
{
5+
int unmarked;
6+
57
@private
68
int private;
79

@@ -17,6 +19,7 @@ @implementation MySuperClass
1719
- (void) test {
1820
int access;
1921
MySuperClass *s = 0;
22+
access = s->unmarked;
2023
access = s->private;
2124
access = s->protected;
2225
}
@@ -30,9 +33,11 @@ @implementation MyClass
3033
- (void) test {
3134
int access;
3235
MySuperClass *s = 0;
36+
access = s->unmarked;
3337
access = s->private; // expected-error {{instance variable 'private' is private}}
3438
access = s->protected;
3539
MyClass *m=0;
40+
access = m->unmarked;
3641
access = m->private; // expected-error {{instance variable 'private' is private}}
3742
access = m->protected;
3843
}
@@ -46,9 +51,11 @@ @implementation Deeper
4651
- (void) test {
4752
int access;
4853
MySuperClass *s = 0;
54+
access = s->unmarked;
4955
access = s->private; // expected-error {{instance variable 'private' is private}}
5056
access = s->protected;
5157
MyClass *m=0;
58+
access = m->unmarked;
5259
access = m->private; // expected-error {{instance variable 'private' is private}}
5360
access = m->protected;
5461
}
@@ -61,9 +68,11 @@ @implementation Unrelated
6168
- (void) test {
6269
int access;
6370
MySuperClass *s = 0;
71+
access = s->unmarked; // expected-error {{instance variable 'unmarked' is protected}}
6472
access = s->private; // expected-error {{instance variable 'private' is private}}
6573
access = s->protected; // expected-error {{instance variable 'protected' is protected}}
6674
MyClass *m=0;
75+
access = m->unmarked; // expected-error {{instance variable 'unmarked' is protected}}
6776
access = m->private; // expected-error {{instance variable 'private' is private}}
6877
access = m->protected; // expected-error {{instance variable 'protected' is protected}}
6978
}
@@ -73,6 +82,7 @@ int main (void)
7382
{
7483
MySuperClass *s = 0;
7584
int access;
85+
access = s->unmarked; // expected-error {{instance variable 'unmarked' is protected}}
7686
access = s->private; // expected-error {{instance variable 'private' is private}}
7787
access = s->protected; // expected-error {{instance variable 'protected' is protected}}
7888
return 0;

0 commit comments

Comments
 (0)