Skip to content

Commit b5a8cac

Browse files
authored
don't link in invalid object files, even if we are force-including all object files (e.g. because the input is a sole .a file, so we want it all); a .a may contain metadata or other stuff we should ignore (#5777)
1 parent d4e0c2a commit b5a8cac

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

tests/test_other.py

+12
Original file line numberDiff line numberDiff line change
@@ -5463,6 +5463,18 @@ def test_link_with_a_static(self):
54635463
Popen([PYTHON, EMCC, 'z.o', 'libtest.a'] + args).communicate()
54645464
out = run_js('a.out.js', assert_returncode=161)
54655465

5466+
def test_link_with_bad_o_in_a(self):
5467+
# when building a .a, we force-include all the objects inside it. but, some
5468+
# may not be valid bitcode, e.g. if it contains metadata or something else
5469+
# weird. we should just ignore those
5470+
subprocess.check_call([PYTHON, EMCC, path_from_root('tests', 'hello_world.c'), '-o', 'a.bc'])
5471+
open('bad.bc', 'w').write('this is not a good file, it should be ignored!')
5472+
subprocess.check_call([LLVM_AR, 'r', 'a.a', 'a.bc', 'bad.bc'])
5473+
assert os.path.exists('a.a')
5474+
subprocess.check_call([PYTHON, EMCC, 'a.a'])
5475+
assert os.path.exists('a.out.js'), output
5476+
self.assertContained('hello, world!', run_js('a.out.js'))
5477+
54665478
def test_require(self):
54675479
inname = path_from_root('tests', 'hello_world.c')
54685480
Building.emcc(inname, output_filename='a.out.js')

tools/shared.py

+6
Original file line numberDiff line numberDiff line change
@@ -1297,6 +1297,9 @@ def __init__(self, returncode, output, defs=set(), undefs=set(), commons=set()):
12971297
self.undefs = undefs
12981298
self.commons = commons
12991299

1300+
def is_valid(self):
1301+
return self.returncode == 0
1302+
13001303
# Due to a python pickling issue, the following two functions must be at top level, or multiprocessing pool spawn won't find them.
13011304

13021305
def g_llvm_nm_uncached(filename):
@@ -1736,6 +1739,9 @@ def link(files, target, force_archive_contents=False, temp_files=None, just_calc
17361739
# returns True.
17371740
def consider_object(f, force_add=False):
17381741
new_symbols = Building.llvm_nm(f)
1742+
if not new_symbols.is_valid():
1743+
logging.warning('object %s is not valid, cannot link' % (f))
1744+
return False
17391745
provided = new_symbols.defs.union(new_symbols.commons)
17401746
do_add = force_add or not unresolved_symbols.isdisjoint(provided)
17411747
if do_add:

0 commit comments

Comments
 (0)