Skip to content

Commit 4f5f0fc

Browse files
committed
Use llvm-objcpy to strip the producers section, so we strip it by default even in -O0. Followup to #11996, fixes #12071
1 parent c56a65e commit 4f5f0fc

File tree

5 files changed

+31
-28
lines changed

5 files changed

+31
-28
lines changed

ChangeLog.md

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,6 @@ Current Trunk
3131
`EXPORTED_FUNCTIONS` is not relevant in the deciding the type of application
3232
to build.
3333
- Allow polymorphic types to be used without RTTI when using embind. (#10914)
34-
- Only strip the LLVM producer's section in release builds. In `-O0` builds, we
35-
try to leave the wasm from LLVM unmodified as much as possible, so if it
36-
emitted the producers section, it will be there. Normally that only matters
37-
in release builds, which is not changing here. If you want to not have a
38-
producer's section in debug builds, you can remove it a tool like
39-
`wasm-opt --strip-producers` (which is what Emscripten still does in release
40-
builds, as always) or use `llvm-objcopy`.
4134
- Only strip debug info in release builds + when `-g` is not present. Previously
4235
even in an `-O0` build without `-g` we would strip it. This was not documented
4336
behavior, and has no effect on program behavior, but may be noticeable

emcc.py

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -553,8 +553,6 @@ def backend_binaryen_passes():
553553
if shared.Settings.OPT_LEVEL > 0:
554554
if shared.Settings.DEBUG_LEVEL < 3:
555555
passes += ['--strip-debug']
556-
if not shared.Settings.EMIT_PRODUCERS_SECTION:
557-
passes += ['--strip-producers']
558556
if shared.Settings.AUTODEBUG:
559557
# adding '--flatten' here may make these even more effective
560558
passes += ['--instrument-locals']
@@ -2607,12 +2605,23 @@ def do_binaryen(target, asm_target, options, memfile, wasm_binary_target,
26072605
options.binaryen_passes += ['--pass-arg=emscripten-sbrk-ptr@%d' % shared.Settings.DYNAMICTOP_PTR]
26082606
if shared.Settings.STANDALONE_WASM:
26092607
options.binaryen_passes += ['--pass-arg=emscripten-sbrk-val@%d' % shared.Settings.DYNAMIC_BASE]
2610-
building.save_intermediate(wasm_binary_target, 'pre-byn.wasm')
2611-
args = options.binaryen_passes
2612-
building.run_wasm_opt(wasm_binary_target,
2613-
wasm_binary_target,
2614-
args=args,
2615-
debug=intermediate_debug_info)
2608+
if options.binaryen_passes:
2609+
# if we need to strip the producers section, and we have wasm-opt passes
2610+
# to run, do it with them.
2611+
if not shared.Settings.EMIT_PRODUCERS_SECTION:
2612+
options.binaryen_passes += ['--strip-producers']
2613+
building.save_intermediate(wasm_binary_target, 'pre-byn.wasm')
2614+
building.run_wasm_opt(wasm_binary_target,
2615+
wasm_binary_target,
2616+
args=options.binaryen_passes,
2617+
debug=intermediate_debug_info)
2618+
else:
2619+
# we are not running wasm-opt. if we need to strip the producers section
2620+
# then do so using llvm-objcpy which is faster and does not rewrite the
2621+
# code (which is better for debug info)
2622+
if not shared.Settings.EMIT_PRODUCERS_SECTION:
2623+
building.save_intermediate(wasm_binary_target, 'pre-noprosec.wasm')
2624+
building.strip_producers(wasm_binary_target, wasm_binary_target)
26162625

26172626
if shared.Settings.BINARYEN_SCRIPTS:
26182627
binaryen_scripts = os.path.join(shared.BINARYEN_ROOT, 'scripts')

src/settings.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1147,16 +1147,12 @@ var WASM_ASYNC_COMPILATION = 1;
11471147
var WASM_BIGINT = 0;
11481148

11491149
// WebAssembly defines a "producers section" which compilers and tools can
1150-
// annotate themselves in, and LLVM emits this by default. In release builds,
1150+
// annotate themselves in, and LLVM emits this by default.
11511151
// Emscripten will strip that out so that it is *not* emitted because it
11521152
// increases code size, and also some users may not want information
11531153
// about their tools to be included in their builds for privacy or security
11541154
// reasons, see
11551155
// https://github.com/WebAssembly/tool-conventions/issues/93.
1156-
// (In debug builds (-O0) we leave the wasm file as it is from LLVM, in which
1157-
// case it may contain this section, if you didn't tell LLVM to not emit it. You
1158-
// can also run wasm-opt --strip-producers manually, which is what Emscripten
1159-
// does in release builds for you automatically.)
11601156
var EMIT_PRODUCERS_SECTION = 0;
11611157

11621158
// If set then generated WASM files will contain a custom

tests/test_other.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7898,17 +7898,14 @@ def test_separate_dwarf_with_filename_and_path(self):
78987898
self.assertIn(b'somewhere.com/hosted.wasm', f.read())
78997899

79007900
@parameterized({
7901-
'O0': (True, ['-O0']), # unoptimized builds try not to modify the LLVM wasm.
7902-
'O1': (False, ['-O1']), # optimized builds strip the producer's section
7903-
'O2': (False, ['-O2']), # by default.
7901+
'O0': (['-O0'],),
7902+
'O1': (['-O1'],),
7903+
'O2': (['-O2'],),
79047904
})
7905-
def test_wasm_producers_section(self, expect_producers_by_default, args):
7905+
def test_wasm_producers_section(self, args):
79067906
self.run_process([EMCC, path_from_root('tests', 'hello_world.c')] + args)
79077907
with open('a.out.wasm', 'rb') as f:
79087908
data = f.read()
7909-
if expect_producers_by_default:
7910-
self.assertIn('clang', str(data))
7911-
return
79127909
# if there is no producers section expected by default, verify that, and
79137910
# see that the flag works to add it.
79147911
self.assertNotIn('clang', str(data))

tools/building.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1423,6 +1423,14 @@ def wasm2js(js_file, wasm_file, opt_level, minify_whitespace, use_closure_compil
14231423
return js_file
14241424

14251425

1426+
def strip_debug(infile, outfile):
1427+
run_process([LLVM_OBJCOPY, '--remove-section=.debug*', infile, outfile])
1428+
1429+
1430+
def strip_producers(infile, outfile):
1431+
run_process([LLVM_OBJCOPY, '--remove-section=producers', infile, outfile])
1432+
1433+
14261434
# extract the DWARF info from the main file, and leave the wasm with
14271435
# debug into as a file on the side
14281436
# TODO: emit only debug sections in the side file, and not the entire
@@ -1435,7 +1443,7 @@ def emit_debug_on_side(wasm_file, wasm_file_with_dwarf):
14351443
embedded_path = shared.Settings.SEPARATE_DWARF_URL or wasm_file_with_dwarf
14361444

14371445
shutil.move(wasm_file, wasm_file_with_dwarf)
1438-
run_process([LLVM_OBJCOPY, '--remove-section=.debug*', wasm_file_with_dwarf, wasm_file])
1446+
strip_debug(wasm_file_with_dwarf, wasm_file)
14391447

14401448
# embed a section in the main wasm to point to the file with external DWARF,
14411449
# see https://yurydelendik.github.io/webassembly-dwarf/#external-DWARF

0 commit comments

Comments
 (0)