CodeGen: fix section names for different file formats

This changes the codegen to match the section names according to the
ObjC rewriter as well as the runtime.  The changes to the test are
simply whitespace changes to the section attributes and names and are
functionally equivalent (the whitespace is ignored by the linker).

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@304661 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Saleem Abdulrasool 2017-06-03 16:18:09 +00:00
parent b68f6e7844
commit 4957517b0b
7 changed files with 137 additions and 32 deletions

View File

@ -1004,6 +1004,8 @@ protected:
const ObjCInterfaceDecl *ID, const ObjCInterfaceDecl *ID,
ObjCCommonTypesHelper &ObjCTypes); ObjCCommonTypesHelper &ObjCTypes);
std::string GetSectionName(StringRef Section, StringRef MachOAttributes);
public: public:
/// CreateMetadataVar - Create a global variable with internal /// CreateMetadataVar - Create a global variable with internal
/// linkage for use by the Objective-C runtime. /// linkage for use by the Objective-C runtime.
@ -4786,6 +4788,27 @@ llvm::Value *CGObjCMac::EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
/* *** Private Interface *** */ /* *** Private Interface *** */
std::string CGObjCCommonMac::GetSectionName(StringRef Section,
StringRef MachOAttributes) {
switch (CGM.getTriple().getObjectFormat()) {
default:
llvm_unreachable("unexpected object file format");
case llvm::Triple::MachO: {
if (MachOAttributes.empty())
return ("__DATA," + Section).str();
return ("__DATA," + Section + "," + MachOAttributes).str();
}
case llvm::Triple::ELF:
assert(Section.substr(0, 2) == "__" &&
"expected the name to begin with __");
return Section.substr(2).str();
case llvm::Triple::COFF:
assert(Section.substr(0, 2) == "__" &&
"expected the name to begin with __");
return ("." + Section.substr(2) + "$B").str();
}
}
/// EmitImageInfo - Emit the image info marker used to encode some module /// EmitImageInfo - Emit the image info marker used to encode some module
/// level information. /// level information.
/// ///
@ -4809,9 +4832,10 @@ enum ImageInfoFlags {
void CGObjCCommonMac::EmitImageInfo() { void CGObjCCommonMac::EmitImageInfo() {
unsigned version = 0; // Version is unused? unsigned version = 0; // Version is unused?
const char *Section = (ObjCABI == 1) ? std::string Section =
"__OBJC, __image_info,regular" : (ObjCABI == 1)
"__DATA, __objc_imageinfo, regular, no_dead_strip"; ? "__OBJC,__image_info,regular"
: GetSectionName("__objc_imageinfo", "regular,no_dead_strip");
// Generate module-level named metadata to convey this information to the // Generate module-level named metadata to convey this information to the
// linker and code-gen. // linker and code-gen.
@ -5930,17 +5954,21 @@ void CGObjCNonFragileABIMac::FinishNonFragileABIModule() {
} }
AddModuleClassList(DefinedClasses, "OBJC_LABEL_CLASS_$", AddModuleClassList(DefinedClasses, "OBJC_LABEL_CLASS_$",
"__DATA, __objc_classlist, regular, no_dead_strip"); GetSectionName("__objc_classlist",
"regular,no_dead_strip"));
AddModuleClassList(DefinedNonLazyClasses, "OBJC_LABEL_NONLAZY_CLASS_$", AddModuleClassList(DefinedNonLazyClasses, "OBJC_LABEL_NONLAZY_CLASS_$",
"__DATA, __objc_nlclslist, regular, no_dead_strip"); GetSectionName("__objc_nlclslist",
"regular,no_dead_strip"));
// Build list of all implemented category addresses in array // Build list of all implemented category addresses in array
// L_OBJC_LABEL_CATEGORY_$. // L_OBJC_LABEL_CATEGORY_$.
AddModuleClassList(DefinedCategories, "OBJC_LABEL_CATEGORY_$", AddModuleClassList(DefinedCategories, "OBJC_LABEL_CATEGORY_$",
"__DATA, __objc_catlist, regular, no_dead_strip"); GetSectionName("__objc_catlist",
"regular,no_dead_strip"));
AddModuleClassList(DefinedNonLazyCategories, "OBJC_LABEL_NONLAZY_CATEGORY_$", AddModuleClassList(DefinedNonLazyCategories, "OBJC_LABEL_NONLAZY_CATEGORY_$",
"__DATA, __objc_nlcatlist, regular, no_dead_strip"); GetSectionName("__objc_nlcatlist",
"regular,no_dead_strip"));
EmitImageInfo(); EmitImageInfo();
} }
@ -6359,7 +6387,8 @@ llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CodeGenFunction &CGF,
llvm::GlobalValue::WeakAnyLinkage, llvm::GlobalValue::WeakAnyLinkage,
Init, Init,
ProtocolName); ProtocolName);
PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip"); PTGV->setSection(GetSectionName("__objc_protorefs",
"coalesced,no_dead_strip"));
PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility); PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
PTGV->setAlignment(Align.getQuantity()); PTGV->setAlignment(Align.getQuantity());
CGM.addCompilerUsedGlobal(PTGV); CGM.addCompilerUsedGlobal(PTGV);
@ -6818,8 +6847,8 @@ llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol(
PTGV->setComdat(CGM.getModule().getOrInsertComdat(ProtocolRef)); PTGV->setComdat(CGM.getModule().getOrInsertComdat(ProtocolRef));
PTGV->setAlignment( PTGV->setAlignment(
CGM.getDataLayout().getABITypeAlignment(ObjCTypes.ProtocolnfABIPtrTy)); CGM.getDataLayout().getABITypeAlignment(ObjCTypes.ProtocolnfABIPtrTy));
if (CGM.getTriple().isOSBinFormatMachO()) PTGV->setSection(GetSectionName("__objc_protolist",
PTGV->setSection("__DATA, __objc_protolist, coalesced, no_dead_strip"); "coalesced,no_dead_strip"));
PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility); PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
CGM.addCompilerUsedGlobal(PTGV); CGM.addCompilerUsedGlobal(PTGV);
return Entry; return Entry;
@ -7015,7 +7044,7 @@ CGObjCNonFragileABIMac::EmitVTableMessageSend(CodeGenFunction &CGF,
/*constant*/ false, /*constant*/ false,
llvm::GlobalValue::WeakAnyLinkage); llvm::GlobalValue::WeakAnyLinkage);
messageRef->setVisibility(llvm::GlobalValue::HiddenVisibility); messageRef->setVisibility(llvm::GlobalValue::HiddenVisibility);
messageRef->setSection("__DATA, __objc_msgrefs, coalesced"); messageRef->setSection(GetSectionName("__objc_msgrefs", "coalesced"));
} }
bool requiresnullCheck = false; bool requiresnullCheck = false;
@ -7126,7 +7155,8 @@ CGObjCNonFragileABIMac::EmitClassRefFromId(CodeGenFunction &CGF,
false, llvm::GlobalValue::PrivateLinkage, false, llvm::GlobalValue::PrivateLinkage,
ClassGV, "OBJC_CLASSLIST_REFERENCES_$_"); ClassGV, "OBJC_CLASSLIST_REFERENCES_$_");
Entry->setAlignment(Align.getQuantity()); Entry->setAlignment(Align.getQuantity());
Entry->setSection("__DATA, __objc_classrefs, regular, no_dead_strip"); Entry->setSection(GetSectionName("__objc_classrefs",
"regular,no_dead_strip"));
CGM.addCompilerUsedGlobal(Entry); CGM.addCompilerUsedGlobal(Entry);
} }
return CGF.Builder.CreateAlignedLoad(Entry, Align); return CGF.Builder.CreateAlignedLoad(Entry, Align);
@ -7160,7 +7190,8 @@ CGObjCNonFragileABIMac::EmitSuperClassRef(CodeGenFunction &CGF,
false, llvm::GlobalValue::PrivateLinkage, false, llvm::GlobalValue::PrivateLinkage,
ClassGV, "OBJC_CLASSLIST_SUP_REFS_$_"); ClassGV, "OBJC_CLASSLIST_SUP_REFS_$_");
Entry->setAlignment(Align.getQuantity()); Entry->setAlignment(Align.getQuantity());
Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip"); Entry->setSection(GetSectionName("__objc_superrefs",
"regular,no_dead_strip"));
CGM.addCompilerUsedGlobal(Entry); CGM.addCompilerUsedGlobal(Entry);
} }
return CGF.Builder.CreateAlignedLoad(Entry, Align); return CGF.Builder.CreateAlignedLoad(Entry, Align);
@ -7182,7 +7213,8 @@ llvm::Value *CGObjCNonFragileABIMac::EmitMetaClassRef(CodeGenFunction &CGF,
MetaClassGV, "OBJC_CLASSLIST_SUP_REFS_$_"); MetaClassGV, "OBJC_CLASSLIST_SUP_REFS_$_");
Entry->setAlignment(Align.getQuantity()); Entry->setAlignment(Align.getQuantity());
Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip"); Entry->setSection(GetSectionName("__objc_superrefs",
"regular,no_dead_strip"));
CGM.addCompilerUsedGlobal(Entry); CGM.addCompilerUsedGlobal(Entry);
} }
@ -7278,7 +7310,8 @@ Address CGObjCNonFragileABIMac::EmitSelectorAddr(CodeGenFunction &CGF,
false, llvm::GlobalValue::PrivateLinkage, false, llvm::GlobalValue::PrivateLinkage,
Casted, "OBJC_SELECTOR_REFERENCES_"); Casted, "OBJC_SELECTOR_REFERENCES_");
Entry->setExternallyInitialized(true); Entry->setExternallyInitialized(true);
Entry->setSection("__DATA, __objc_selrefs, literal_pointers, no_dead_strip"); Entry->setSection(GetSectionName("__objc_selrefs",
"literal_pointers,no_dead_strip"));
Entry->setAlignment(Align.getQuantity()); Entry->setAlignment(Align.getQuantity());
CGM.addCompilerUsedGlobal(Entry); CGM.addCompilerUsedGlobal(Entry);
} }

View File

@ -0,0 +1,72 @@
// RUN: %clang_cc1 -triple x86_64-unknown-windows -fobjc-dispatch-method=mixed -fobjc-runtime=ios -emit-llvm -o - %s | FileCheck %s -check-prefix CHECK-COFF
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fobjc-dispatch-method=mixed -fobjc-runtime=ios -emit-llvm -o - %s | FileCheck %s -check-prefix CHECK-ELF
// RUN: %clang_cc1 -triple x86_64-apple-ios -fobjc-dispatch-method=mixed -fobjc-runtime=ios -emit-llvm -o - %s | FileCheck %s -check-prefix CHECK-MACHO
__attribute__((__objc_root_class__))
@interface NSObject
+ (void)load;
+ (id)class;
@end
@protocol P
@end
@interface I<P> : NSObject
+ (void)load;
@end
@implementation I
+ (void)load; { }
@end
@implementation I(C)
+ (void)load; {
[super load];
}
@end
@interface J : NSObject
- (void)m;
@end
_Bool f(J *j) {
[j m];
return [I class] == @protocol(P);
}
// CHECK-COFF: @"OBJC_CLASSLIST_SUP_REFS_$_" = {{.*}}, section ".objc_superrefs$B"
// CHECK-COFF: @OBJC_SELECTOR_REFERENCES_ = {{.*}}, section ".objc_selrefs$B"
// CHECK-COFF: @"OBJC_CLASSLIST_REFERENCES_$_" = {{.*}}, section ".objc_classrefs$B"
// CHECK-COFF: @"\01l_objc_msgSend_fixup_class" = {{.*}}, section ".objc_msgrefs$B"
// CHECK-COFF: @"\01l_OBJC_LABEL_PROTOCOL_$_P" = {{.*}}, section ".objc_protolist$B"
// CHECK-COFF: @"\01l_OBJC_PROTOCOL_REFERENCE_$_P" = {{.*}}, section ".objc_protorefs$B"
// CHECK-COFF: @"OBJC_LABEL_CLASS_$" = {{.*}}, section ".objc_classlist$B"
// CHECK-COFF: @"OBJC_LABEL_NONLAZY_CLASS_$" = {{.*}}, section ".objc_nlclslist$B"
// CHECK-COFF: @"OBJC_LABEL_CATEGORY_$" = {{.*}}, section ".objc_catlist$B"
// CHECK-COFF: @"OBJC_LABEL_NONLAZY_CATEGORY_$" = {{.*}}, section ".objc_nlcatlist$B"
// CHECK-COFF: !{{[0-9]+}} = !{i32 1, !"Objective-C Image Info Section", !".objc_imageinfo$B"}
// CHECK-ELF: @"OBJC_CLASSLIST_SUP_REFS_$_" = {{.*}}, section "objc_superrefs"
// CHECK-ELF: @OBJC_SELECTOR_REFERENCES_ = {{.*}}, section "objc_selrefs"
// CHECK-ELF: @"OBJC_CLASSLIST_REFERENCES_$_" = {{.*}}, section "objc_classrefs"
// CHECK-ELF: @"\01l_objc_msgSend_fixup_class" = {{.*}}, section "objc_msgrefs"
// CHECK-ELF: @"\01l_OBJC_LABEL_PROTOCOL_$_P" = {{.*}}, section "objc_protolist"
// CHECK-ELF: @"\01l_OBJC_PROTOCOL_REFERENCE_$_P" = {{.*}}, section "objc_protorefs"
// CHECK-ELF: @"OBJC_LABEL_CLASS_$" = {{.*}}, section "objc_classlist"
// CHECK-ELF: @"OBJC_LABEL_NONLAZY_CLASS_$" = {{.*}}, section "objc_nlclslist"
// CHECK-ELF: @"OBJC_LABEL_CATEGORY_$" = {{.*}}, section "objc_catlist"
// CHECK-ELF: @"OBJC_LABEL_NONLAZY_CATEGORY_$" = {{.*}}, section "objc_nlcatlist"
// CHECK-ELF: !{{[0-9]+}} = !{i32 1, !"Objective-C Image Info Section", !"objc_imageinfo"}
// CHECK-MACHO: @"OBJC_CLASSLIST_SUP_REFS_$_" = {{.*}}, section "__DATA,__objc_superrefs,regular,no_dead_strip"
// CHECK-MACHO: @OBJC_SELECTOR_REFERENCES_ = {{.*}}, section "__DATA,__objc_selrefs,literal_pointers,no_dead_strip"
// CHECK-MACHO: @"OBJC_CLASSLIST_REFERENCES_$_" = {{.*}}, section "__DATA,__objc_classrefs,regular,no_dead_strip"
// CHECK-MACHO: @"\01l_objc_msgSend_fixup_class" = {{.*}}, section "__DATA,__objc_msgrefs,coalesced"
// CHECK-MACHO: @"\01l_OBJC_LABEL_PROTOCOL_$_P" = {{.*}}, section "__DATA,__objc_protolist,coalesced,no_dead_strip"
// CHECK-MACHO: @"\01l_OBJC_PROTOCOL_REFERENCE_$_P" = {{.*}}, section "__DATA,__objc_protorefs,coalesced,no_dead_strip"
// CHECK-MACHO: @"OBJC_LABEL_CLASS_$" = {{.*}}, section "__DATA,__objc_classlist,regular,no_dead_strip"
// CHECK-MACHO: @"OBJC_LABEL_NONLAZY_CLASS_$" = {{.*}}, section "__DATA,__objc_nlclslist,regular,no_dead_strip"
// CHECK-MACHO: @"OBJC_LABEL_CATEGORY_$" = {{.*}}, section "__DATA,__objc_catlist,regular,no_dead_strip"
// CHECK-MACHO: @"OBJC_LABEL_NONLAZY_CATEGORY_$" = {{.*}}, section "__DATA,__objc_nlcatlist,regular,no_dead_strip"
// CHECK-MACHO: !{{[0-9]+}} = !{i32 1, !"Objective-C Image Info Section", !"__DATA,__objc_imageinfo,regular,no_dead_strip"}