为什么不能隐藏Objective-C目标文件中的符号?

时间:2015-10-08 16:34:32

标签: objective-c macos linker symbols

我正在编写一个使用Objective-C的小型动态库,并将C-linkage函数暴露给Unity(它是一个原生插件)。

即使我的编译器设置默认隐藏的符号设置为,我在我要公开的函数声明上使用__attribute__((visibility("default"))),甚至static符号(函数和全局变量)出现在输出库中。

我重新创建了一个test Xcode project,它最初包含一个C文件

file.h:

#pragma once

extern __attribute__((visibility("default"))) void not_hidden();

file.c:

#include "file.h"
#include <stdio.h>

static void hidden()
{
    printf("I'm hidden\n");
}

void not_hidden()
{
    printf("I'm not hidden\n");
    hidden();
}

这就像宣传的那样:

$ nm libHiddenSymbolTest.dylib 
0000000000000f40 T _not_hidden
                 U _printf
                 U dyld_stub_binder

但是,只要我添加具有类似功能的Objective-C文件(not_hidden2()等),那么所有static符号也会同时暴露:

$ nm libHiddenSymbolTest.dylib 
                 U _CFStringGetCStringPtr
                 U _NSLog
00000000000054d0 s _OBJC_METACLASS_$___ARCLite__
                 U __Block_copy
                 U __DefaultRuneLocale
0000000000005710 b __ZGVZL18add_image_hook_ARCPK11mach_headerlE7patches
0000000000005830 b __ZGVZL22add_image_hook_swiftV1PK11mach_headerlE7patches
0000000000005640 b __ZGVZL30add_image_hook_autoreleasepoolPK11mach_headerlE7patches
0000000000004160 s __ZL11_class_name
00000000000039a4 t __ZL12cxxConstructP11objc_object
00000000000055d0 d __ZL12demangleLock
000000000000377a t __ZL13demangledNamePKcb
0000000000002ab3 t __ZL13replaceMethodP10objc_classP13objc_selectorPFP11objc_objectS4_S2_zEPS6_
0000000000005748 b __ZL14NSString_class
00000000000035b0 t __ZL14initialize_impP11objc_objectP13objc_selector
0000000000001aa2 t __ZL15__ARCLite__loadv
0000000000003927 t __ZL16scanMangledFieldRPKcS0_S1_Ri
00000000000041a8 s __ZL17_load_method_name
00000000000043d0 s __ZL17_load_method_type
0000000000003461 t __ZL17transcribeMethodsP10objc_classP15glue_class_ro_t
00000000000022b9 t __ZL18add_image_hook_ARCPK11mach_headerl
00000000000035b6 t __ZL18allocateMaybeSwiftP18glue_swift_class_tm
0000000000001f7f t __ZL19patch_lazy_pointersPK11mach_headerP7patch_tm
00000000000034e5 t __ZL19transcribeProtocolsP10objc_classP15glue_class_ro_t
00000000000039d4 t __ZL20fixStringForCoreDataP11objc_object
0000000000003530 t __ZL20transcribePropertiesP10objc_classP15glue_class_ro_t
0000000000002785 t __ZL21__arclite_objc_retainP11objc_object
00000000000025e3 t __ZL21__arclite_object_copyP11objc_objectm
0000000000005740 b __ZL22NSConstantString_class
00000000000027a1 t __ZL22__arclite_objc_releaseP11objc_object
000000000000288f t __ZL22add_image_hook_swiftV1PK11mach_headerl
0000000000003646 t __ZL22copySwiftV1MangledNamePKcb
0000000000005870 b __ZL22original_class_getName
0000000000005848 b __ZL22original_objc_getClass
0000000000005610 b __ZL23NSAutoreleasePool_class
00000000000033dc t __ZL23__arclite_class_getNameP10objc_class
0000000000003230 t __ZL23__arclite_objc_getClassPKc
00000000000054f8 s __ZL24OBJC_CLASS_$___ARCLite__
000000000000248c t __ZL24__arclite_object_setIvarP11objc_objectP9objc_ivarS0_
0000000000005738 b __ZL25NSMutableDictionary_class
0000000000005868 b __ZL25original_objc_getProtocol
0000000000005860 b __ZL25original_objc_lookUpClass
0000000000005878 b __ZL25original_protocol_getName
00000000000027b3 t __ZL26__arclite_objc_autoreleaseP11objc_object
0000000000003384 t __ZL26__arclite_objc_getProtocolPKc
000000000000332f t __ZL26__arclite_objc_lookUpClassPKc
0000000000002797 t __ZL26__arclite_objc_retainBlockP11objc_object
000000000000283f t __ZL26__arclite_objc_storeStrongPP11objc_objectS0_
00000000000033f1 t __ZL26__arclite_protocol_getNameP8Protocol
0000000000005850 b __ZL26original_objc_getMetaClass
00000000000052c8 s __ZL27OBJC_CLASS_RO_$___ARCLite__
0000000000003285 t __ZL27__arclite_objc_getMetaClassPKc
0000000000005758 b __ZL27original_NSKKMS_indexForKey
0000000000002b9a t __ZL28__arclite_NSKKMS_indexForKeyP11objc_objectP13objc_selectorS0_
0000000000002df0 t __ZL28__arclite_objc_readClassPairP10objc_classPK15objc_image_info
0000000000005760 b __ZL28original_NSKKsD_objectForKey
0000000000002bec t __ZL29__arclite_NSKKsD_objectForKeyP11objc_objectP13objc_selectorS0_
0000000000005720 b __ZL29original_NSManagedObject_init
0000000000005718 b __ZL30NSUndoManagerProxy_targetClass
0000000000002ae6 t __ZL30__arclite_NSManagedObject_initP11objc_objectP13objc_selector
0000000000001ecb t __ZL30add_image_hook_autoreleasepoolPK11mach_headerl
000000000000399e t __ZL30arclite_uninitialized_functionv
0000000000005858 b __ZL30original_objc_getRequiredClass
0000000000005280 s __ZL31OBJC_METACLASS_RO_$___ARCLite__
00000000000032da t __ZL31__arclite_objc_getRequiredClassPKc
0000000000005838 b __ZL31original_objc_allocateClassPair
0000000000005840 b __ZL31original_object_getIndexedIvars
0000000000005310 s __ZL32OBJC_$_CLASS_METHODS___ARCLite__
000000000000313d t __ZL32__arclite_objc_allocateClassPairP10objc_classPKcm
00000000000027c5 t __ZL32__arclite_objc_retainAutoreleaseP11objc_object
00000000000031de t __ZL32__arclite_object_getIndexedIvarsP11objc_object
0000000000005770 b __ZL32original_NSKKsD_setObject_forKey
0000000000002c92 t __ZL33__arclite_NSKKsD_setObject_forKeyP11objc_objectP13objc_selectorS0_S0_
0000000000001f6d t __ZL33__arclite_objc_autoreleasePoolPopPv
0000000000001f54 t __ZL34__arclite_objc_autoreleasePoolPushv
0000000000005768 b __ZL34original_NSKKsD_removeObjectForKey
0000000000002c3e t __ZL35__arclite_NSKKsD_removeObjectForKeyP11objc_objectP13objc_selectorS0_
0000000000005730 b __ZL35original_NSManagedObject_allocBatch
0000000000002b0c t __ZL36__arclite_NSManagedObject_allocBatchP11objc_objectP13objc_selectorPS0_S0_j
0000000000002434 t __ZL36__arclite_object_setInstanceVariableP11objc_objectPKcPv
0000000000005750 b __ZL36original_NSKKMS_fastIndexForKnownKey
0000000000005880 b __ZL36original_objc_copyClassNamesForImage
0000000000002b48 t __ZL37__arclite_NSKKMS_fastIndexForKnownKeyP11objc_objectP13objc_selectorS0_
00000000000027f0 t __ZL37__arclite_objc_autoreleaseReturnValueP11objc_object
0000000000003409 t __ZL37__arclite_objc_copyClassNamesForImagePKcPj
0000000000005778 b __ZL40original_NSKKsD_addEntriesFromDictionary
0000000000005728 b __ZL40original_NSManagedObject_allocWithEntity
0000000000002cfb t __ZL41__arclite_NSKKsD_addEntriesFromDictionaryP11objc_objectP13objc_selectorP12NSDictionary
0000000000002af9 t __ZL41__arclite_NSManagedObject_allocWithEntityP11objc_objectP13objc_selectorS0_
00000000000021e7 t __ZL42__arclite_NSArray_objectAtIndexedSubscriptP7NSArrayP13objc_selectorm
0000000000002a7a t __ZL42__arclite_NSUndoManagerProxy_isKindOfClassP11objc_objectP13objc_selectorP10objc_class
0000000000002802 t __ZL43__arclite_objc_retainAutoreleaseReturnValueP11objc_object
000000000000282d t __ZL44__arclite_objc_retainAutoreleasedReturnValueP11objc_object
000000000000225b t __ZL46__arclite_NSDictionary_objectForKeyedSubscriptP12NSDictionaryP13objc_selectorP11objc_object
000000000000226d t __ZL47__arclite_NSOrderedSet_objectAtIndexedSubscriptP12NSOrderedSetP13objc_selectorm
00000000000021f9 t __ZL53__arclite_NSMutableArray_setObject_atIndexedSubscriptP14NSMutableArrayP13objc_selectorP11objc_objectm
0000000000002291 t __ZL58__arclite_NSMutableDictionary__setObject_forKeyedSubscriptP19NSMutableDictionaryP13objc_selectorP11objc_objectS4_
000000000000227f t __ZL58__arclite_NSMutableOrderedSet_setObject_atIndexedSubscriptP19NSMutableOrderedSetP13objc_selectorP11objc_objectm
0000000000005888 b __ZL9Demangled
0000000000005650 b __ZZL18add_image_hook_ARCPK11mach_headerlE7patches
0000000000005780 b __ZZL22add_image_hook_swiftV1PK11mach_headerlE7patches
0000000000005620 b __ZZL30add_image_hook_autoreleasepoolPK11mach_headerlE7patches
                 U ___CFConstantStringClassReference
                 U ___stack_chk_fail
                 U ___stack_chk_guard
                 U __dyld_register_func_for_add_image
0000000000005260 s __non_lazy_classes
                 U __objc_empty_cache
                 U __objc_empty_vtable
                 U _asprintf
                 U _bzero
                 U _calloc
                 U _class_addMethod
                 U _class_addProperty
                 U _class_addProtocol
                 U _class_getInstanceMethod
                 U _class_getInstanceSize
                 U _class_getInstanceVariable
                 U _class_getIvarLayout
                 U _class_getName
                 U _class_getSuperclass
                 U _class_isMetaClass
                 U _class_replaceMethod
                 U _class_respondsToSelector
                 U _free
                 U _hash_create
                 U _hash_search
0000000000001a30 t _hidden
0000000000001a70 t _hidden2
                 U _ivar_getName
                 U _ivar_getOffset
                 U _kCFCoreFoundationVersionNumber
                 U _malloc
                 U _memcmp
                 U _memcpy
                 U _method_setImplementation
0000000000001a00 T _not_hidden
0000000000001a50 T _not_hidden2
                 U _objc_allocateClassPair
                 U _objc_autoreleasePoolPush
                 U _objc_collectingEnabled
                 U _objc_constructInstance
                 U _objc_copyClassNamesForImage
                 U _objc_getClass
                 U _objc_getMetaClass
                 U _objc_getProtocol
                 U _objc_getRequiredClass
                 U _objc_initializeClassPair
                 U _objc_lookUpClass
                 U _objc_msgSend
                 U _objc_readClassPair
                 U _objc_registerClassPair
                 U _objc_retain
0000000000001a87 T _objc_retainedObject
0000000000001a90 T _objc_unretainedObject
0000000000001a99 T _objc_unretainedPointer
                 U _object_getClass
                 U _object_getIndexedIvars
                 U _object_getIvar
                 U _object_setIvar
                 U _printf
                 U _property_copyAttributeList
                 U _protocol_getMethodDescription
                 U _protocol_getName
                 U _pthread_mutex_lock
                 U _pthread_mutex_unlock
                 U _sel_getUid
                 U _strcmp
                 U _strlen
                 U _strncmp
                 U dyld_stub_binder

请注意,我已将各种条带符号设置保留为默认设置,对于C文件符号,这似乎没问题,如上面第一个nm输出中所示。< / p>

任何人都可以解释为什么会这样,并且可能建议隐藏我不想暴露的Objective-C符号吗?

更新:我在OSX 10.11.0上使用Xcode 7.0.1。

更新#2(更多实验):

  1. 在项目中包含.m文件会导致Xcode在编译-fvisibility=hidden文件期间停止传递.c,从而忽略默认隐藏的符号构建设置。重复;在添加.m之前,这已被传递给编译器。
  2. 导出的符号文件设置为包含_not_hidden_not_hidden2的文件无效。
  3. 在我看来,忽略默认隐藏的符号是问题的根本原因所以我尝试在每个函数和变量上显式设置__attribute__((visibility("hidden"/"default")))(通过预处理器宏)它也没有效果。所以这似乎暗示了链接器。

2 个答案:

答案 0 :(得分:4)

有几个原因导致您仍然在dylib中看到一堆符号:

<强> 1。您设置(或不设置)的条带模式无法获得所有功能。

(尝试在构建设置中调整条带样式,通常删除debugging symbols很好,但是,为了这个目的,我已经将它提升到Non-Global Symbols,这可能就在你身边你可以接受它。如果你决定剥离all symbols,你的dylib可能无法编译,并且它实际上会在那时使它无法使用)

enter image description here

<强> 2。绝对需要一些Objective-C符号。

(这些包括objc种类中的任何一种,以及classpropertyobject等其他内容,因为它们都支持运行时,是的还有很多礼物可以存在)

第3。您可能正在查看dylib的调试版本而不是发行版本。

(确保您正在查看dylib 发布 版本。这将是Product > Archive&gt; Export然后应该很好地剥离大多数实际上没有使用的符号。

您可能需要稍微使用条带设置,尽管只使用我在resulted in approximately 100+ fewer symbols上面显示的设置,而不是转储中当前显示的设置。将符号包含在文件中真的不错,事实上如果遇到问题那么它会更有帮助。但是,如果你想删除那些混乱的不相关或未使用的符号,这是可以理解的。

答案 1 :(得分:1)

好的,剥离非全局符号确实解决了这个问题,但是我无法在 Build Settings Deployment 部分设置正确的序列(我试过了)许多不同的组合)。

我可以强制链接器删除非全局符号的唯一方法是将其添加到其他链接器标志

-Xlinker -x