This is an archive of the discontinued LLVM Phabricator instance.

[lld][WebAssembly] Add support for -soname
ClosedPublic

Authored by sbc100 on Aug 15 2023, 10:33 AM.

Details

Summary

This change writes the module name to the name section of the wasm
binary. We use the -soname argument to determine the name and we
default the output file basename if this option is not specified.

In the future we will likely want to embed the soname in the dylink
section too, but this the first step in supporting -soname.

Diff Detail

Event Timeline

sbc100 created this revision.Aug 15 2023, 10:33 AM
Herald added a project: Restricted Project. · View Herald TranscriptAug 15 2023, 10:33 AM
Herald added subscribers: pmatos, asb, wingo and 3 others. · View Herald Transcript
sbc100 requested review of this revision.Aug 15 2023, 10:33 AM
Herald added a project: Restricted Project. · View Herald TranscriptAug 15 2023, 10:33 AM
sbc100 edited the summary of this revision. (Show Details)Aug 15 2023, 10:39 AM
sbc100 edited the summary of this revision. (Show Details)
sbc100 added a reviewer: dschuff.

Do we really want to always write a module name even if the output is not a DSO?
For DSOs it makes sense since they would usually have symbols and names, but for final linked modules it seems to me that we should write one if there are any other debug names in the module, but maybe not otherwise. I don't know why someone would want a module name if it were the only name in the binary?

Do we really want to always write a module name even if the output is not a DSO?
For DSOs it makes sense since they would usually have symbols and names, but for final linked modules it seems to me that we should write one if there are any other debug names in the module, but maybe not otherwise. I don't know why someone would want a module name if it were the only name in the binary?

The entire name section is skipped if you link with --strip-all.. otherwise we generate a name section. I don't really think we would ever create a module without a name section unless --strip-all is passed since any function that has a name will be in the name section (so your module would have to have no exported symbols at all to avoid a name section in the absence of --strip-all).

In other words, I think there is little risk that this change will add a name section where there was not one previously.

Do we really want to always write a module name even if the output is not a DSO?

In general, yes I think this is useful feature. Especially in cases where the original filename might otherwise be lost to the runtime (e.g.. when using WebAssembly.instantiate rather than WebAssembly.instantiateStreaming)

For DSOs it makes sense since they would usually have symbols and names, but for final linked modules it seems to me that we should write one if there are any other debug names in the module, but maybe not otherwise. I don't know why someone would want a module name if it were the only name in the binary?

dschuff accepted this revision.Aug 15 2023, 5:31 PM

Ah ok that makese sense.

This revision is now accepted and ready to land.Aug 15 2023, 5:31 PM
This revision was automatically updated to reflect the committed changes.
glandium added a subscriber: glandium.EditedAug 16 2023, 1:53 PM

FWIW, this patch breaks wasm2c, in the sense that it now generates different symbols for the same wasm linked with lld before and after this change, with no change in the link command line. Example: w2c_rlbox_0x5F_wasm_init_memory became w2c_0x24rlbox0x2Ewasm_0x5F_wasm_init_memory, and -soname doesn't make a difference.
Edit: one can use wasm2c's -n option to avoid using the module name that is now always provided by lld.

FWIW, this patch breaks wasm2c, in the sense that it now generates different symbols for the same wasm linked with lld before and after this change, with no change in the link command line. Example: w2c_rlbox_0x5F_wasm_init_memory became w2c_0x24rlbox0x2Ewasm_0x5F_wasm_init_memory, and -soname doesn't make a difference.
Edit: one can use wasm2c's -n option to avoid using the module name that is now always provided by lld.

I see, yes it sounds like -n is probably the best solution.

Separately, I do wonder if wasm2c should strip the file extension when it sees rlbox.wasm as the module name. The .wasm part seem to translate to 0x2Ewasm in the function names which doesn't seem like it adds anything useful.

FWIW, this patch breaks wasm2c, in the sense that it now generates different symbols for the same wasm linked with lld before and after this change, with no change in the link command line. Example: w2c_rlbox_0x5F_wasm_init_memory became w2c_0x24rlbox0x2Ewasm_0x5F_wasm_init_memory, and -soname doesn't make a difference.
Edit: one can use wasm2c's -n option to avoid using the module name that is now always provided by lld.

I see, yes it sounds like -n is probably the best solution.

Separately, I do wonder if wasm2c should strip the file extension when it sees rlbox.wasm as the module name. The .wasm part seem to translate to 0x2Ewasm in the function names which doesn't seem like it adds anything useful.

Also, it looks like the first 0x24 should probably not be there either.. that is the $ which wabt adds to names in the name section under some ciscumstances.

Separately, I do wonder if wasm2c should strip the file extension when it sees rlbox.wasm as the module name. The .wasm part seem to translate to 0x2Ewasm in the function names which doesn't seem like it adds anything useful.

It sure is interesting that "-n foo" doesn't do the same thing as the .wasm containing a module name of "foo".

Separately, I do wonder if wasm2c should strip the file extension when it sees rlbox.wasm as the module name. The .wasm part seem to translate to 0x2Ewasm in the function names which doesn't seem like it adds anything useful.

It sure is interesting that "-n foo" doesn't do the same thing as the .wasm containing a module name of "foo".

Do you mean the 0x24 ($) that seems to get added by wasm2c?

Do you mean the 0x24 ($) that seems to get added by wasm2c?

Yes.

Do you mean the 0x24 ($) that seems to get added by wasm2c?

Yes.

Thats seems fairly clearly to be a bug in wasm2c (to me anyway).