[PATCH] D113456: Allow protocol metadata to be deduplicated within dylibs

Davide Italiano via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Nov 8 21:44:41 PST 2021


davide created this revision.
davide added reviewers: rjmccall, rsmith, pete, ab, jckarter.
davide requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Given these two files:

  #import <objc/NSObject.h>
  
  @interface Bar : NSObject<NSObject>
  @end
  
  @implementation Bar
  @end
  
  id bar()
  {
      return [Bar class];
  }

and

  #import <objc/NSObject.h>
  
  @interface Foo : NSObject<NSObject>
  @end
  
  @implementation Foo
  @end
  
  id foo()
  {
      return [Foo class];
  }

built with:

`xcrun --sdk macosx.internal clang++ foo.m bar.m -o lib.dylib -lobjc -dynamiclib -O2 -Wl,-dead_strip`

if you run nm on it, you'll see 2 copies of all the protocol metadata:

  $ nm -j lib.dylib | uniq -c | grep 2
     2 __OBJC_$_PROP_LIST_NSObject
     2 __OBJC_$_PROTOCOL_INSTANCE_METHODS_NSObject
     2 __OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_NSObject
     2 __OBJC_$_PROTOCOL_METHOD_TYPES_NSObject

This is because some metadata is emitted with `internal` linkage and `default` visibility, e.g.:

  @"_OBJC_$_PROP_LIST_NSObject" = internal global { i32, i32, [4 x %struct._prop_t] } 

This patch changes `clang` to emit protocol metadata as `weak hidden`, so that `ld64` can deduplicate them.

Fixes rdar://problem/85042564


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D113456

Files:
  clang/lib/CodeGen/CGObjCMac.cpp
  clang/test/CodeGenObjC/direct-properties.m
  clang/test/CodeGenObjC/encode-test-2.m
  clang/test/CodeGenObjC/metadata-class-properties.m
  clang/test/CodeGenObjC/metadata-symbols-32.m
  clang/test/CodeGenObjC/metadata-symbols-64.m
  clang/test/CodeGenObjC/property-category-impl.m
  clang/test/CodeGenObjC/property-list-in-class.m
  clang/test/CodeGenObjC/property-list-in-extension.m
  clang/test/CodeGenObjC/protocols.m
  clang/test/CodeGenObjC/sections.m

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D113456.385694.patch
Type: text/x-patch
Size: 15627 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20211109/4c0492a8/attachment.bin>


More information about the cfe-commits mailing list