@@ -244,11 +244,13 @@ private Collection<? extends String> arMembers(String path) throws IOException,
244244 // in the build process, because such a smart linker should not be assumed for POSIX, but it
245245 // seems ok to emulate this at least for the very common case of ar archives with symbol
246246 // definitions that overlap what's defined in explicitly include .o files
247- for (String f : members ) {
247+ outer : for (String f : members ) {
248248 if (Files .probeContentType (Paths .get (f )).contains (LLVM_IR_BITCODE )) {
249- HashSet <String > definedHere = new HashSet <>();
249+ HashSet <String > definedFuncs = new HashSet <>();
250+ HashSet <String > definedGlobals = new HashSet <>();
251+
250252 ProcessBuilder nm = new ProcessBuilder ();
251- nm .command (LLVM_NM , "-g" , "- -defined-only" , f );
253+ nm .command (LLVM_NM , "--defined-only" , f );
252254 nm .redirectInput (Redirect .INHERIT );
253255 nm .redirectError (Redirect .INHERIT );
254256 nm .redirectOutput (Redirect .PIPE );
@@ -257,23 +259,40 @@ private Collection<? extends String> arMembers(String path) throws IOException,
257259 String line = null ;
258260 while ((line = buffer .readLine ()) != null ) {
259261 String [] symboldef = line .split (" " );
260- if (symboldef .length >= 2 ) {
261- definedHere .add (symboldef [symboldef .length - 1 ]);
262+ if (symboldef .length == 3 ) {
263+ // format is ------- CHAR FUNCNAME
264+ if (symboldef [1 ].toLowerCase ().equals ("t" )) {
265+ definedFuncs .add (symboldef [2 ].trim ());
266+ } else if (symboldef [1 ].toLowerCase ().equals ("d" )) {
267+ definedGlobals .add (symboldef [2 ].trim ());
268+ } else {
269+ // keep all if we have symbols that we wouldn't know what to do with
270+ logV ("Not extracting from " , f , " because there are non-strong function or global symbols" );
271+ continue outer ;
272+ }
262273 }
263274 }
264275 }
265276 nmProc .waitFor ();
266277
267278 ArrayList <String > extractCmd = new ArrayList <>();
268279 extractCmd .add ("llvm-extract" );
269- for (String def : definedHere ) {
270- if (undefinedSymbols .contains (def )) {
280+ for (String def : definedFuncs ) {
281+ if (! definedSymbols .contains (def )) {
271282 definedSymbols .add (def );
272283 undefinedSymbols .remove (def );
273284 extractCmd .add ("-func" );
274285 extractCmd .add (def );
275286 }
276287 }
288+ for (String def : definedGlobals ) {
289+ if (!definedSymbols .contains (def )) {
290+ definedSymbols .add (def );
291+ undefinedSymbols .remove (def );
292+ extractCmd .add ("-glob" );
293+ extractCmd .add (def );
294+ }
295+ }
277296 extractCmd .add (f );
278297 extractCmd .add ("-o" );
279298 extractCmd .add (f );
0 commit comments