Skip to content

Unreferenced exports are removed when using .addAssemblyFile() #22234

Open
@daniel-ac-martin

Description

@daniel-ac-martin

Zig Version

0.14.0-dev.2370+5c6b25d9b

Steps to Reproduce and Observed Behavior

In recent versions of Zig (since v0.11?) there is some unintuitive behaviour when using .addAssemblyFile() as opposed to not using it.

When not using .addAssemblyFile() the variables that are exported from your .zig files are present in the resulting binary. For example a multiboot header: https://github.com/andrewrk/HellOS/blob/68efb199d96bcd1c104e54c21bd8802f2612ec68/hellos.zig#L14`

However, if you make use of addAssemblyFile() then the exported variables will be removed unless they are referenced in your assembly file. (Which is a particular nuisance for multiboot headers, as there is no reason to reference it.)

This behaviour is NOT seen in Debug builds.

This does not appear to be an issue with the final linking step, but rather an earlier step, as the data is missing from the .o file.

If this is the intended behaviour, then please accept my apologies; it wasn't intuitive to me.

Rough steps to reproduce

  1. Start with a project that exports some data that is otherwise unreferenced
  2. Build with ReleaseSmall, and search for data hexdump .zig-cache/o/*/Proj.o | grep -i 'b002'; you should find it
  3. Add an empty assembly file with .addAssemblyFile()
  4. Build with ReleaseSmall again; the data should now be missing

Expected Behavior

exported variables in .zig source files should be present in the final binary, regardless of whether addAssemblyFile() was used.


I hope that the above is clear. If not, I will try to put together a test case, in the future. For now though, I wanted to get this written down while it's fresh in my mind, and also on the off chance that someone is able to easily understand why this is happening.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugObserved behavior contradicts documented or intended behavior

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions