Skip to content

Commit c2c11db

Browse files
authored
Merge pull request #21 from intercom/james/swift-support
Prevent mangling of Swift symbols
2 parents c168083 + 16a10bd commit c2c11db

File tree

22 files changed

+331
-56
lines changed

22 files changed

+331
-56
lines changed

Gemfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,7 @@ gemspec
44

55
group :development do
66
gem 'rspec'
7+
gem 'pry'
8+
gem 'pry-remote'
79
gem 'rspec_junit_formatter'
810
end

Gemfile.lock

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ GEM
5555
nap (>= 0.8, < 2.0)
5656
netrc (~> 0.11)
5757
cocoapods-try (1.2.0)
58+
coderay (1.1.3)
5859
colored2 (3.1.2)
5960
concurrent-ruby (1.1.7)
6061
diff-lcs (1.4.4)
@@ -69,11 +70,18 @@ GEM
6970
i18n (0.9.5)
7071
concurrent-ruby (~> 1.0)
7172
json (2.3.1)
73+
method_source (1.0.0)
7274
minitest (5.14.2)
7375
molinillo (0.6.6)
7476
nanaimo (0.3.0)
7577
nap (1.1.0)
7678
netrc (0.11.0)
79+
pry (0.13.1)
80+
coderay (~> 1.1)
81+
method_source (~> 1.0)
82+
pry-remote (0.1.8)
83+
pry (~> 0.9)
84+
slop (~> 3.0)
7785
rspec (3.9.0)
7886
rspec-core (~> 3.9.0)
7987
rspec-expectations (~> 3.9.0)
@@ -90,6 +98,7 @@ GEM
9098
rspec_junit_formatter (0.4.1)
9199
rspec-core (>= 2, < 4, != 2.12.0)
92100
ruby-macho (1.4.0)
101+
slop (3.6.0)
93102
thread_safe (0.3.6)
94103
typhoeus (1.4.0)
95104
ethon (>= 0.9.0)
@@ -107,6 +116,8 @@ PLATFORMS
107116

108117
DEPENDENCIES
109118
cocoapods-mangle!
119+
pry
120+
pry-remote
110121
rspec
111122
rspec_junit_formatter
112123

lib/cocoapods_mangle/defines.rb

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ def self.mangling_defines(prefix, binaries_to_mangle)
2323
# @return [Array<String>] The classes defined in the binaries
2424
def self.classes(binaries)
2525
all_symbols = run_nm(binaries, '-gU')
26+
all_symbols = all_symbols.reject { |symbol| swift_symbol?(symbol) }
27+
2628
class_symbols = all_symbols.select do |symbol|
2729
symbol[/OBJC_CLASS_\$_/]
2830
end
@@ -36,6 +38,8 @@ def self.classes(binaries)
3638
# @return [Array<String>] The constants defined in the binaries
3739
def self.constants(binaries)
3840
all_symbols = run_nm(binaries, '-gU')
41+
all_symbols = all_symbols.reject { |symbol| swift_symbol?(symbol) }
42+
3943
consts = all_symbols.select { |const| const[/ S /] }
4044
consts = consts.reject { |const| const[/_OBJC_/] }
4145
consts = consts.reject { |const| const[/__block_descriptor.*/] }
@@ -110,6 +114,31 @@ def self.prefix_selectors(prefix, selectors)
110114
defines
111115
end
112116

117+
# Is symbol a Swift symbol? This is used to avoid mangling Swift.
118+
# @param [String] symbol
119+
# The symbol to check
120+
# @return [Boolean] true if it is a Swift symbol, false otherwise
121+
def self.swift_symbol?(symbol)
122+
# Swift binaries have many symbols starting with $s_ that should be excluded
123+
# e.g. '0000000000000258 S _$s9ManglePod9SomeClassCMF'
124+
symbol[/ _\$s/] ||
125+
# Internal Swift symbols starting with __swift or ___swift such as should not be mangled
126+
# e.g. '00000000000050ac S ___swift_reflection_version'
127+
symbol[/ __(_)?swift/] ||
128+
# Swift symbols starting with _symbolic should be ignored
129+
# e.g. '0000000000000248 S _symbolic _____ 9ManglePod9SomeClassC'
130+
symbol[/ _symbolic/] ||
131+
# Swift symbol references to Objective-C symbols should not be mangled
132+
# e.g. '00000000000108ca S _associated conformance So26SCNetworkReachabilityFlagsVs10SetAlgebraSCSQ'
133+
symbol[/associated conformance/] ||
134+
# _globalinit symbols should be skipped
135+
# e.g. 0000000000000000 T _globalinit_33_A313450CFC1FC3D0CBEF4411412DB9E8_func0
136+
symbol[/ _globalinit/] ||
137+
# Swift classes inheriting from Objective-C classes should not be mangled
138+
# e.g. '0000000000000290 S _OBJC_CLASS_$__TtC9ManglePod19SomeFoundationClass'
139+
symbol[/_OBJC_CLASS_\$__/]
140+
end
141+
113142
def self.run_nm(binaries, flags)
114143
`nm #{flags} #{binaries.join(' ')}`.split("\n")
115144
end
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)