aboutsummaryrefslogtreecommitdiff
path: root/src/link/Wasm.zig
AgeCommit message (Collapse)Author
2024-02-29wasm: consolidate flushModule and linkWithZldLuuk de Gram
We now use a single function to use the in-house WebAssembly linker rather than wasm-ld. For both incremental compilation and traditional linking we use the same codepath.
2024-02-29wasm: correctly generate relocations for type indexLuuk de Gram
Previously we could directly write the type index because we used the index that was known in the final binary. However, as we now process the Zig module as its own relocatable object file, we must ensure to generate a relocation for type indexes. This also ensures that we can later link the relocatable object file as a standalone also. This also fixes generating indirect function table entries for ZigObject as it now correctly points to the relocation symbol index rather than the symbol index that owns the relocation.
2024-02-29wasm: reimplement Zig errors in linkerLuuk de Gram
2024-02-29wasm: ensure unique function indexesLuuk de Gram
We cannot keep function indexes as maxInt(u32) due to functions being dedupliated when they point to the same function. For this reason we now use a regular arraylist which will have new functions appended to, and when deleted, its index is appended to the free list, allowing us to re-use slots in the function list.
2024-02-29wasm: reimplement `deleteDeclExport`Luuk de Gram
Removes the symbol from the decl's list of exports, marks it as dead, as well as appends it to the symbol free list. Also removes it from the list of global symbols as all exports are global. In the future we should perhaps use a map for the export list to prevent linear lookups. But this requires a benchmark as having more than 1 export for the same decl is very rare.
2024-02-29wasm: re-implement `updateExports`Luuk de Gram
We now correctly create a symbol for each exported decl with its export- name. The symbol points to the same linker-object. We store a map from decl to all of its exports so we can update exports if it already exists rather than infinitely create new exports.
2024-02-29wasm: update `freeDecl` and `finishDecl`Luuk de Gram
We now parse the decls right away into atoms and allocate the corresponding linker-object, such as segment and function, rather than waiting until `flushModule`.
2024-02-29wasm: Move `createFunction` to `ZigObject`Luuk de Gram
This function was previously only called by the backend which generates a synthetical function that is not represented by any AIR or Zig code. For this reason, the ownership is moved to the zig-object and stored there so it can be linked with the other object files without the driver having to specialize it.
2024-02-29wasm: Use `File.Index` for symbol locationsLuuk de Gram
Rather than using the optional, we now directly use `File.Index` which can already represent an unknown file due to its `.null` value. This means we do not pay for the memory cost. This type of index is now used for: - SymbolLoc - Key of the functions map - InitFunc Now we can simply pass things like atom.file, object.file, loc.file etc whenever we need to access its representing object file which makes it a lot easier.
2024-02-29wasm: fix symbol resolution and atom processingLuuk de Gram
2024-02-29wasm: use `File` abstraction instead of objectLuuk de Gram
When merging sections we now make use of the `File` abstraction so all objects such as globals, functions, imports, etc are also merged from the `ZigObject` module. This allows us to use a singular way to perform each link action without having to check the kind of the file. The logic is mostly handled in the abstract file module, unless its complexity warrants the handling within the corresponding module itself.
2024-02-29wasm: store `File.Index` on the AtomLuuk de Gram
Also, consolidate the creation of Atoms so they all use `createAtom`.
2024-02-29wasm: create linking objects in correct moduleLuuk de Gram
CodeGen will create linking objects such as symbols, function types, etc in ZigObject, rather than in the linker driver where the final result will be stored. They will end up in the linker driver module during the `flush` phase instead. This must mean we must call functions such as `addOrGetFuncType` in the correct namespace or else it will be created in the incorrect list and therefore return incorrect indexes.
2024-02-29wasm: initialize a `ZigObject` when requiredLuuk de Gram
When we have a ZigCompileUnit and don't use LLVM, we initialize the ZigObject which will encapsulate the Zig Module as an object file in- memory. During initialization we also create symbols which the object will need such as the stack pointer.
2024-02-29wasm: move incremental Dwarf info into ZigObjectLuuk de Gram
2024-02-28use hash.addListOfBytes where applicableAndrew Kelley
2024-02-26change default WASI stack sizeAndrew Kelley
to match the other operating systems. 16 MiB closes #18885
2024-02-25test: rework how filtering worksJacob Young
* make test names contain the fully qualified name * make test filters match the fully qualified name * allow multiple test filters, where a test is skipped if it does not match any of the specified filters
2024-02-07link: report function failures in `FuncAnalysis`Jacob Young
This unblocks backend errors after #18814.
2024-02-05compiler: rename value.zig to Value.zigAndrew Kelley
This commit only does the file rename to be friendlier to version control conflicts.
2024-02-02cli+build: handle -ObjC flag and route it to MachO linkerJakub Konka
2024-01-13Linux: Add fchmodat fallback when `flags` is nonzeroStephen Gregoratto
The check for determining whether to use the fallback code has been moved into an inline function as per Andrew's comments in #17954.
2024-01-12wasm-linker: delay code atom allocation till writeLuuk de Gram
We delay atom allocation for the code section until we write the actual atoms. We do this to ensure the offset of the atom also includes the 'size' field which is leb128-encoded and therefore variable. We need this correct offset to ensure debug info works correctly. The ordering of the code section is now automatic due to iterating the function section and then finding the corresponding atom to each function. This also ensures each function corresponds to the right atom, and they do not go out-of-sync. Lastly, we removed the `next` field as it is no longer required and also removed manually setting the offset in synthetic functions. This means atoms use less memory and synthetic functions are less prone. They will also be placed in order of function order correctly.
2024-01-12wasm-linker: ensure custom sections are parsedLuuk de Gram
Not all custom sections are represented by a symbol, which means the section will not be parsed by the lazy parsing and therefore get garbage- collected. This is problematic as it may contain debug information that should not be garbage-collected. To resolve this, we manually create local symbols for those sections and also ensure they do not get garbage- collected.
2024-01-09Add support for `--(no-)undefined-version`dhash
Co-authored-by: Motiejus Jakštys <motiejus@jakstys.lt> Co-authored-by: Jakub Konka <kubkon@jakubkonka.com> Co-authored-by: Samuel Cantero <scanterog@gmail.com> Co-authored-by: Giorgos Georgiou <giorgos.georgiou@datadoghq.com> Co-authored-by: Carl Åstholm <carl@astholm.se>
2024-01-01link: accept the update arena in flushAndrew Kelley
This branch introduced an arena allocator for temporary allocations in Compilation.update. Almost every implementation of flush() inside the linker code was already creating a local arena that had the lifetime of the function call. This commit passes the update arena so that all those local ones can be deleted, resulting in slightly more efficient memory usage with every compilation update. While at it, this commit also removes the Compilation parameter from the linker flush function API since a reference to the Compilation is now already stored in `link.File`.
2024-01-01link.File.Wasm: remove dead conditionAndrew Kelley
2024-01-01move force_undefined_symbols into CompilationAndrew Kelley
This field is needed by Compilation regardless of whether a link file is instantiated. Fixes an invalid check for bin_file=null.
2024-01-01Compilation: several branch regression fixesAndrew Kelley
* move wasi_emulated_libs into Compilation - It needs to be accessed from Compilation, which needs to potentially build those artifacts. * Compilation: improve error reporting for two cases - the setMiscFailure mechanism is handy - let's use it! * fix one instance of incorrectly checking for emit_bin via `comp.bin_file != null`. There are more instances of this that need to be fixed in a future commit. * fix renameTmpIntoCache not handling the case where it needs to make the "o" directory in the zig-cache directory. - while I'm at it, simplify the logic for handling the fact that Windows returns error.AccessDenied rather than error.PathAlreadyExists for failure to rename a directory over another one. * fix missing cache hash additions - there are still more to add in a future commit - addNonIncrementalStuffToCacheManifest is called when bin_file is always null, and then it incorrectly checks if bin_file is non-null and only then adds a bunch of stuff to the cache hash. It needs to instead add to the cache hash based on lf_open_opts.
2024-01-01compiler: push entry symbol name resolution into the linkerAndrew Kelley
This is necessary because on COFF, the entry symbol name is not known until the linker has looked at the set of global symbol names to determine which of the four possible main entry points is present.
2024-01-01fix MachO linkingAndrew Kelley
* fix relationship between createEmpty/open (similar logic as 607111aa758002bc51914b7dc800b23927c931b8) * still resolve the start symbol when linking libc because when zig is the linker it still needs to know the entry symbol. * make use_llvm=false when there is no zig compilation unit.
2024-01-01link.Wasm: fix relationship between createEmpty/openAndrew Kelley
2024-01-01linker: rename intermediary_basname to zcu_object_sub_pathAndrew Kelley
2024-01-01fix remaining compile errors except oneAndrew Kelley
2024-01-01move dll_export_fns and rdynamic to Compilation.ConfigAndrew Kelley
2024-01-01update codegen.llvm references to bin_file.optionsAndrew Kelley
2024-01-01compiler: miscellaneous branch progressAndrew Kelley
implement builtin.zig file population for all modules rather than assuming there is only one global builtin.zig module. move some fields from link.File to Compilation move some fields from Module to Compilation compute debug_format in global Compilation config resolution wire up C compilation to the concept of owner modules make whole cache mode call link.File.createEmpty() instead of link.File.open()
2024-01-01linkers: update references to "options" fieldAndrew Kelley
2024-01-01linker: update target referencesAndrew Kelley
2024-01-01linker: update link_mode referencesAndrew Kelley
2024-01-01linker: update output_mode referencesAndrew Kelley
2024-01-01linker: update references to moduleAndrew Kelley
2024-01-01move a large chunk of linker logic away from "options"Andrew Kelley
These options are only supposed to be provided to the initialization functions, resolved, and then computed values stored in the appropriate place (base struct or the object-format-specific structs). Many more to go...
2023-11-28wasm-linker: support `--no-gc-sections`Luuk de Gram
By default we garbage-collect sections for Wasm to reduce size, as well as finish linking quicker (as we have fewer things to do). However, when the user specifies `--no-gc-sections` we ensure all resolved symbols get marked and therefore do not get garbage collected. This is supported in both incremental-mode and traditional linking.
2023-11-28wasm-linker: support gc for wasm backend codeLuuk de Gram
When using the Wasm backend, we will now also perform garbage collection there, to ensure unreferenced symbols do not get parsed nor emit into the final binary.
2023-11-28wasm-linker: handle debug info during gcLuuk de Gram
When we encounter a debug info symbol, we initially have to parse it into an atom to find its relocations. We then go through its relocations to find out if any of the target symbols are marked alive. When it finds an alive symbol, we also mark the debug symbol as alive to ensure this piece of debug info is emit to the binary. When it does not encounter any alive symbols, the debug symbol remains dead and will be garbage- collected during `allocateAtoms`.
2023-11-28wasm-linker: Only emit name of referenced symbolsLuuk de Gram
2023-11-28wasm-linker: deduplicate aliased functionsLuuk de Gram
When multiple symbols point to the same function, we ensure any other symbol other than the original will be discarded and point to the original instead. This prevents emitting the same function code more than once.
2023-11-28wasm-linker: parse symbols into atoms lazilyLuuk de Gram
Rather than parsing every symbol into an atom, we now only parse them into an atom when such atom is marked. This means garbage-collected symbols will also not be parsed into atoms, and neither are discarded symbols which have been resolved by other symbols. (Such as multiple weak symbols). This also introduces a binary search for finding the start index into the list of relocations. This speeds up finding the corresponding relocations tremendously as they're ordered ascended by address. Lastly, we re-use the memory of atom's data as well as relocations instead of duplicating it. This means we half the memory usage of atom's data and relocations for linked object files. As we are aware of decls and synthetic atoms, we free the memory of those atoms indepedently of the atoms of object files to prevent double-frees.
2023-11-28wasm-linker: do not merge unreferenced symbolsLuuk de Gram
When a symbol is unreferenced and therefore garbage-collected, we do not merge its specific section into the final binary.