Zig: linksection without export

Created on 24 Apr 2020  Â·  11Comments  Â·  Source: ziglang/zig

I have

const vector_table linksection(“.vector_table”) = [_]u32{};

in multiple files. Without using export, the linksection is disregarded. With export, I am stuck giving a unique export name in each file, which is tedious and worse needs to be explained to the reader.

Could we get the linksection to work without an export symbol?

Thanks, Mark

All 11 comments

Without using export, the linksection is disregarded

Citation needed.
Use -femit-llvm-ir to see the linksection is honored.
Use -femit-asm to see the linksection is again honored.
If you can't find it in your binary maybe you should recheck the linker script, if you're using one.

Edit: My bad, I didn't notice it was a zero-sized array.

Without export, the linker is allowed to just erase your symbol if you don't use it. Do you reference the vector_table in your file anywhere?

Without export, the linker is allowed to just erase your symbol if you don't use it.

That seems like a bit of a footgun, since a specific section was given. 🤔

Some more info:

       const vector_table_1 linksection(".vector_table") = [_]u32{ };
export const vector_table_2 linksection(".vector_table") = [_]u32{ };
       const vector_table_3 linksection(".vector_table") = [_]u32{ 0 };
export const vector_table_4 linksection(".vector_table") = [_]u32{ 0 };
       const vector_table_5 linksection(".vector_table") = [_]u32{ 0 };
export const vector_table_6 linksection(".vector_table") = [_]u32{ 0 };

comptime {
    _ = vector_table_5;
    _ = vector_table_6;
}
vector_table_4:
        .zero   4

vector_table_5:
        .zero   4

vector_table_6:
        .zero   4

As expected, vector_table_1 and vector_table_3 don't get exported, as they are not referenced and thus zig will not even analyze the symbol itself. As expected, vector_table_5 and vector_table_6 get exported because they are both referenced by the code. Also, as expected, vector_table_4 gets exported.

Now the question. Why doesn't vector_table_2 get exported? My guess would be that it's zero sized and thus has no content to be linked at all. Why should it be linked if the address it provides gives no semantic content anyways?

That seems like a bit of a footgun, since a specific section was given. thinking

I don't think so. Even with a link section, stuff should not be linked in when not being referenced. Otherwise, your executable size would blow up. Very helpful on embedded HW to only have the stuff in the code that is actually used. Also it would be weird for zig to suddenly emit stuff that has a link section, but is only required on windows, not linux (or vice versa)

Yes the problem wasn’t that the linksection was ignored, but that the const wasn’t referenced. If I use export to reference the const, then I need to give the symbol a unique name.

I think I will live with that since at some point I need to distinguish the primary vector table that should load at 0 anyways. You can see what I have at https://github.com/markfirmware/zig-bare-metal-microbit/blob/08fc62ca4482e018f6688c1cb5a3096946937e3a/mission2_model_railroad_pwm.zig#L390-L393

Thanks very much for the interest.

I over-simplified the example to an array of length 0. All the feed-back has been valid for the actual case. However, it seems that moving the name from the linksection declaration into the @export .section field is not giving me the same results. Are they intended to do the same thing?

However, it seems that moving the name from the linksection declaration into the @export .section field is not giving me the same results

That's a shortcoming of the current (underspecified) @export, you can't export a const but you surely can export a var.

That's a shortcoming of the current (underspecified) @export

@LemonBoy "Underspecified," as in, won't be fixed/focused-on in the stage1 compiler, but will be in the self-hosted compiler?

The comment TODO audit the various ways to use @export seems to sort-of confirm that.
If it ain't broken (in stage1) don't touch it, that's my mantra waiting for stage2 :)

Thanks!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

bronze1man picture bronze1man  Â·  3Comments

andrewrk picture andrewrk  Â·  3Comments

andersfr picture andersfr  Â·  3Comments

andrewrk picture andrewrk  Â·  3Comments

dobkeratops picture dobkeratops  Â·  3Comments