Skip to content

wasm-ld should ignore .rlib files in archives #55786

@hoodmane

Description

@hoodmane

As per the rust issue rust-lang/rust#80775, if you try to build an empty rust file:

rustc \ 
    -C target-feature=+mutable-globals \
    -C relocation-model=pic \
    -C link-args='-s SIDE_MODULE' \
    --target wasm32-unknown-emscripten \
    --crate-type cdylib \
    test.rs

the result is the following error:

note: wasm-ld: error: unknown file type: lib.rmeta

Rust libraries contain an index file called lib.rmeta. The linker is expected to ignore them. I can work around this with the following patch to the linker:

https://github.com/pyodide/pyodide/pull/2378/files#diff-c21c39739d2d98d34e3063aceea3d659b11bc10471c403a0199a5bc01587e58b

Emscripten linker patch
if any(arg.endswith(".rlib") for arg in cmd):
  from tempfile import mkstemp
  from pathlib import Path
  tempfiles = []
  new_cmd = []
  try:
    for arg in cmd:
      if arg == "-lc":
        continue
      if not arg.endswith(".rlib"):
        new_cmd.append(arg)
        continue
      fd, temp_path = mkstemp()
      shutil.copy2(arg, temp_path)
      tempfiles.append(Path(temp_path))
      subprocess.call(["ar", "-d", temp_path, "lib.rmeta"])
      subprocess.call(["emranlib", temp_path])
      new_cmd.append(temp_path)
    cmd = new_cmd
    cmd = get_command_with_possible_response_file(cmd)
    check_call(cmd)
  finally:
    for file in tempfiles:
      file.unlink()
else:
  cmd = get_command_with_possible_response_file(cmd)
  check_call(cmd)

xref Pyodide PR pyodide/pyodide#2378

@sbc100

Metadata

Metadata

Assignees

No one assigned

    Labels

    lld:wasmquestionA question, not bug report. Check out https://llvm.org/docs/GettingInvolved.html instead!

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions