1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
|
#!/usr/bin/python3
#
# Find exported symbols that can be made non-exported.
#
# Noting that (a) parsing these commands is a pain, the output is quite irregular and (b) I'm fumbling in the
# dark here, trying to guess what exactly constitutes an "import" vs an "export" of a symbol, linux linking
# is rather complex.
#
# Takes about 5min to run on a decent machine.
#
# The standalone function analysis is reasonable reliable, but the class/method analysis is less so
# (something to do with destructor thunks not showing up in my results?)
#
# Also, the class/method analysis will not catch problems like
# 'dynamic_cast from 'Foo' with hidden type visibility to 'Bar' with default type visibility'
# but loplugin:dyncastvisibility will do that for you
#
import subprocess
import re
exported_symbols1 = set()
imported_symbols1 = set()
exported_symbols2 = set() # decoded
imported_symbols2 = set() # decoded
# find all our shared libs
subprocess_find = subprocess.Popen("find ./instdir -name *.so && find ./workdir/LinkTarget/CppunitTest -name *.so",
stdout=subprocess.PIPE, shell=True)
with subprocess_find.stdout as txt:
for line in txt:
sharedlib = line.strip()
# look for exported symbols
subprocess_nm = subprocess.Popen(b"nm -D " + sharedlib, stdout=subprocess.PIPE, shell=True)
with subprocess_nm.stdout as txt2:
# We are looking for lines something like:
# 0000000000036ed0 T flash_component_getFactory
line_regex = re.compile(r'^[0-9a-fA-F]+ T ')
for line2_bytes in txt2:
line2 = line2_bytes.strip().decode("utf-8")
if line_regex.match(line2):
sym = line2.split(" ")[2].strip()
exported_symbols1.add(sym)
subprocess_nm.terminate()
# look for imported symbols
subprocess_objdump = subprocess.Popen(b"objdump -T " + sharedlib, stdout=subprocess.PIPE, shell=True)
with subprocess_objdump.stdout as txt2:
# ignore some header bumpf
txt2.readline()
txt2.readline()
txt2.readline()
txt2.readline()
# We are looking for lines something like:
# 0000000000000000 DF *UND* 0000000000000000 _ZN16FilterConfigItem10WriteInt32ERKN3rtl8OUStringEi
for line2_bytes in txt2:
line2 = line2_bytes.strip().decode("utf-8")
if "*UND*" not in line2:
continue
tokens = line2.split(" ")
sym = tokens[len(tokens)-1].strip()
imported_symbols1.add(sym)
subprocess_objdump.terminate()
subprocess_find.terminate()
# look for imported symbols in executables
subprocess_find = subprocess.Popen("find ./instdir -name *.bin", stdout=subprocess.PIPE, shell=True)
with subprocess_find.stdout as txt:
for line in txt:
executable = line.strip()
# look for exported symbols
subprocess_nm = subprocess.Popen(b"nm -D " + executable + b" | grep -w U", stdout=subprocess.PIPE, shell=True)
with subprocess_nm.stdout as txt2:
# We are looking for lines something like:
# U sal_detail_deinitialize
for line2_bytes in txt2:
line2 = line2_bytes.strip().decode("utf-8")
sym = line2.split(" ")[1]
imported_symbols1.add(sym)
subprocess_find.terminate()
# Now we have to symbolize before comparing because sometimes (due to thunks) two
# different encoded names symbolize to the same method/func name
#
progress = 0
progress_max_len = len(imported_symbols1) + len(exported_symbols1)
for sym in imported_symbols1:
progress += 1
if (progress % 128 == 0):
print( str(int(progress * 100 / progress_max_len)) + "%")
filtered_sym = subprocess.check_output(["c++filt", sym]).strip().decode("utf-8")
if filtered_sym.startswith("non-virtual thunk to "):
filtered_sym = filtered_sym[21:]
elif filtered_sym.startswith("virtual thunk to "):
filtered_sym = filtered_sym[17:]
imported_symbols2.add(filtered_sym)
progress = 0
for sym in exported_symbols1:
progress += 1
if (progress % 128 == 0):
print( str(int(progress * 100 / progress_max_len)) + "%")
filtered_sym = subprocess.check_output(["c++filt", sym]).strip().decode("utf-8")
if filtered_sym.startswith("non-virtual thunk to "):
filtered_sym = filtered_sym[21:]
elif filtered_sym.startswith("virtual thunk to "):
filtered_sym = filtered_sym[17:]
exported_symbols2.add(filtered_sym)
unused_exports = exported_symbols2 - imported_symbols2
print("exported = " + str(len(exported_symbols2)))
print("imported = " + str(len(imported_symbols2)))
print("unused_exports = " + str(len(unused_exports)))
# for each class, count how many symbols will become hidden if we mark the class as hidden
can_be_hidden_count = dict()
for sym in exported_symbols2:
i = sym.rfind("::")
if i == -1:
continue
clz = sym[:i]
if clz in can_be_hidden_count:
can_be_hidden_count[clz] = can_be_hidden_count[clz] + 1
else:
can_be_hidden_count[clz] = 1
for sym in imported_symbols2:
i = sym.rfind("::")
if i == -1:
continue
clz = sym[:i]
if clz in can_be_hidden_count:
can_be_hidden_count[clz] = can_be_hidden_count[clz] - 1
else:
can_be_hidden_count[clz] = -1
# convert to list, and sort the results in descending order
can_be_hidden_list = list()
for clz in can_be_hidden_count:
cnt = can_be_hidden_count[clz]
if cnt > 0:
can_be_hidden_list.append((cnt, clz))
can_be_hidden_list.sort(reverse=True)
with open("bin/find-can-be-private-symbols.classes.results", "wt") as f:
for i in can_be_hidden_list:
if i[0] < 10:
break
f.write(str(i[0]) + " " + i[1] + "\n")
with open("bin/find-can-be-private-symbols.functions.results", "wt") as f:
for sym in sorted(unused_exports):
# Filter out most of the noise.
# No idea where these are coming from, but not our code.
if sym.startswith("CERT_"):
continue
elif sym.startswith("DER_"):
continue
elif sym.startswith("FORM_"):
continue
elif sym.startswith("FPDF"):
continue
elif sym.startswith("HASH_"):
continue
elif sym.startswith("Hunspell_"):
continue
elif sym.startswith("LL_"):
continue
elif sym.startswith("LP_"):
continue
elif sym.startswith("LU"):
continue
elif sym.startswith("MIP"):
continue
elif sym.startswith("MPS"):
continue
elif sym.startswith("NSS"):
continue
elif sym.startswith("NSC_"):
continue
elif sym.startswith("PK11"):
continue
elif sym.startswith("PL_"):
continue
elif sym.startswith("PQ"):
continue
elif sym.startswith("PBE_"):
continue
elif sym.startswith("PORT_"):
continue
elif sym.startswith("PRP_"):
continue
elif sym.startswith("PR_"):
continue
elif sym.startswith("PT_"):
continue
elif sym.startswith("QS_"):
continue
elif sym.startswith("REPORT_"):
continue
elif sym.startswith("RSA_"):
continue
elif sym.startswith("SEC"):
continue
elif sym.startswith("SGN"):
continue
elif sym.startswith("SOS"):
continue
elif sym.startswith("SSL_"):
continue
elif sym.startswith("VFY_"):
continue
elif sym.startswith("_PR_"):
continue
elif sym.startswith("ber_"):
continue
elif sym.startswith("bfp_"):
continue
elif sym.startswith("ldap_"):
continue
elif sym.startswith("ne_"):
continue
elif sym.startswith("opj_"):
continue
elif sym.startswith("pg_"):
continue
elif sym.startswith("pq"):
continue
elif sym.startswith("presolve_"):
continue
elif sym.startswith("sqlite3_"):
continue
elif sym.startswith("libepubgen::"):
continue
elif sym.startswith("lucene::"):
continue
elif sym.startswith("Hunspell::"):
continue
elif sym.startswith("sk_"):
continue
elif sym.startswith("_Z"):
continue
# dynamically loaded
elif sym.endswith("get_implementation"):
continue
elif sym.endswith("component_getFactory"):
continue
elif sym == "CreateUnoWrapper":
continue
elif sym == "ExportDOC":
continue
elif sym == "ExportRTF":
continue
elif sym == "GetSaveWarningOfMSVBAStorage_ww8":
continue
elif sym == "GetSpecialCharsForEdit":
continue
elif sym.startswith("Import"):
continue
elif sym.startswith("Java_com_sun_star_"):
continue
elif sym.startswith("TestImport"):
continue
elif sym.startswith("getAllCalendars_"):
continue
elif sym.startswith("getAllCurrencies_"):
continue
elif sym.startswith("getAllFormats"):
continue
elif sym.startswith("getBreakIteratorRules_"):
continue
elif sym.startswith("getCollationOptions_"):
continue
elif sym.startswith("getCollatorImplementation_"):
continue
elif sym.startswith("getContinuousNumberingLevels_"):
continue
elif sym.startswith("getDateAcceptancePatterns_"):
continue
elif sym.startswith("getForbiddenCharacters_"):
continue
elif sym.startswith("getIndexAlgorithm_"):
continue
elif sym.startswith("getLCInfo_"):
continue
elif sym.startswith("getLocaleItem_"):
continue
elif sym.startswith("getOutlineNumberingLevels_"):
continue
elif sym.startswith("getReservedWords_"):
continue
elif sym.startswith("getSTC_"):
continue
elif sym.startswith("getSearchOptions_"):
continue
elif sym.startswith("getTransliterations_"):
continue
elif sym.startswith("getUnicodeScripts_"):
continue
elif sym.startswith("lok_"):
continue
# UDK API
elif sym.startswith("osl_"):
continue
elif sym.startswith("rtl_"):
continue
elif sym.startswith("typelib_"):
continue
elif sym.startswith("typereg_"):
continue
elif sym.startswith("uno_"):
continue
# remove things we found that do not exist in our source code, they're not ours
#if not(extractFunctionNameFromSignature(sym) in all_source_names): continue
f.write(sym + "\n")
|