Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

zig build-exe not warning for missing -lc parameter #10410

Closed
rubin55 opened this issue Dec 25, 2021 · 5 comments
Closed

zig build-exe not warning for missing -lc parameter #10410

rubin55 opened this issue Dec 25, 2021 · 5 comments
Labels
proposal This issue suggests modifications. If it also has the "accepted" label then it is planned.
Milestone

Comments

@rubin55
Copy link

rubin55 commented Dec 25, 2021

Zig Version

0.9.0

Steps to Reproduce

  1. Create main.zig, roughly following instructions from: https://www.nmichaels.org/zig/wrap-sodium.html
const std = @import("std");

const c = @cImport({
    @cInclude("sodium.h");
});

const SodiumError = error{InitError};

pub fn main() anyerror!void {
    std.log.info("All your codebase are belong to us.", .{});
    std.log.warn("Sodium init said: {d}", .{sodium_init()});
}

pub fn sodium_init() SodiumError!void {
    if (c.sodium_init() < 0) {
        return SodiumError.InitError;
    }
}

test "basic test" {
    try std.testing.expectEqual(10, 3 + 7);
}

test "sodium init" {
    try sodium_init();
}
  1. Execute build: zig build-exe src/main.zig -lsodium

Observe that we omit -lc and that we get no output and no warnings: echo $? # returns 0

Expected Behavior

I expected zig build-exe to return a warning with regards to missing -lc argument, like so (example output obtained from frett27 on IRC - note that this actually happens for him but not for me, annecdotally the same zig version 0.9.0):

$ zig build-exe src/main.zig -lsodium
./sodium.zig:3:11: error: C import failed
const c = @cImport({
^
./sodium.zig:3:11: note: libc headers not available; compilation does not link against libc
const c = @cImport({
^
/nix/store/ky7awdidglv88q964bsjr96s8bjsy1wz-libsodium-1.0.18-dev/include/sodium/crypto_hash_sha512.h:13:10: note: 'stdlib.h' file not found
#include <stdlib.h>
^
./sodium.zig:17:9: note: referenced here
if (c.sodium_init() < 0) {
^

Note that building with linkage to libc produces a working executable.
Executing resulting main executable (result of zig build-exe src/main.zig -lc -lsodium):

info: All your codebase are belong to us.
warning: Sodium init said: void

Actual Behavior

Executing resulting main executable (result of zig build-exe src/main.zig -lsodium):

$ ./main 
info: All your codebase are belong to us.
Segmentation fault at address 0x0
???:?:?: 0x0 in ??? (???)
/home/rubin/Syncthing/Source/Rubin/hello-zig/src/main.zig:11:56: 0x22b009 in main (main)
    std.log.warn("Sodium init said: {d}", .{sodium_init()});
                                                       ^
/opt/zig/zig0/lib/std/start.zig:553:37: 0x223f3a in std.start.callMain (main)
            const result = root.main() catch |err| {
                                    ^
/opt/zig/zig0/lib/std/start.zig:495:12: 0x205a57 in std.start.callMainWithArgs (main)
    return @call(.{ .modifier = .always_inline }, callMain, .{});
           ^
/opt/zig/zig0/lib/std/start.zig:409:17: 0x204b54 in std.start.posixCallMainAndExit (main)
    std.os.exit(@call(.{ .modifier = .always_inline }, callMainWithArgs, .{ argc, argv, envp }));
                ^
/opt/zig/zig0/lib/std/start.zig:322:5: 0x204961 in std.start._start (main)
    @call(.{ .modifier = .never_inline }, posixCallMainAndExit, .{});
    ^
Aborted
@rubin55 rubin55 added the bug Observed behavior contradicts documented or intended behavior label Dec 25, 2021
@rubin55
Copy link
Author

rubin55 commented Dec 25, 2021

Some notes about my system:

                __.;=====;.__                   rubin@THINK 
            _.=+==++=++=+=+===;.                ----------- 
             -=+++=+===+=+=+++++=_              OS: Void Linux x86_64 
        .     -=:``     `--==+=++==.            Host: 21A0CTO1WW ThinkPad P14s Gen 2a 
       _vi,    `            --+=++++:           Kernel: 5.15.11_1 
      .uvnvi.       _._       -==+==+.          Uptime: 23 hours, 56 mins 
     .vvnvnI`    .;==|==;.     :|=||=|.         Packages: 1706 (xbps-query) 
+QmQQmpvvnv; _yYsyQQWUUQQQm #QmQ#:QQQWUV$QQm.   Shell: bash 5.1.8 
 -QQWQWpvvowZ?.wQQQE==<QWWQ/QWQW.QQWW(: jQWQE   Resolution: 3840x2160 
  -$QQQQmmU'  jQQQ@+=<QWQQ)mQQQ.mQQQC+;jWQQ@'   DE: GNOME 40.4 
   -$WQ8YnI:   QWQQwgQQWV`mWQQ.jQWQQgyyWW@!     WM: Mutter 
     -1vvnvv.     `~+++`        ++|+++          WM Theme: Rubin 
      +vnvnnv,                 `-|===           Theme: Adwaita-dark [GTK2/3] 
       +vnvnvns.           .      :=-           Icons: Adwaita [GTK2/3] 
        -Invnvvnsi..___..=sv=.     `            Terminal: gnome-terminal 
          +Invnvnvnnnnnnnnvvnn;.                CPU: AMD Ryzen 7 PRO 5850U with Radeon Graphics (16) @ 1.900GHz 
            ~|Invnvnvvnvvvnnv}+`                GPU: AMD ATI 07:00.0 Cezanne 
               -~|{*l}*|~                       Memory: 3256MiB / 27964MiB 

I installed zig from the binary download available here: https://ziglang.org/download/ . I tried both 0.9.0 and master (0.10.0-dev.62+5b171f446).

@rubin55
Copy link
Author

rubin55 commented Dec 25, 2021

objdump -x ./main output: objdump.txt

The above shows that it is indeed the missing libc NEEDED that causes this. Adding -lc makes the executable work, but imho, zig should warn. I find it mysterious that zig does warn on another system (i.e., see example in bug report, system of frett27) but not on mine.

@andrewrk andrewrk added proposal This issue suggests modifications. If it also has the "accepted" label then it is planned. and removed bug Observed behavior contradicts documented or intended behavior labels Dec 27, 2021
@andrewrk andrewrk added this to the 0.11.0 milestone Dec 27, 2021
@andrewrk
Copy link
Member

How would Zig figure out that libc was supposed to be linked? Perhaps by inspecting the NEEDED of libsodium.so? In such case, zig could infer -lc and would not need to trouble the user at all.

@matu3ba
Copy link
Contributor

matu3ba commented Jun 24, 2023

https://refspecs.linuxfoundation.org/elf/elf.pdf

DT_NEEDED This element holds the string table offset of a null-terminated string, giving
the name of a needed library. The offset is an index into the table recorded
in the DT_STRTAB entry. See "Shared Object Dependencies'' for more
information about these names. The dynamic array may contain multiple
entries with this type. These entries' relative order is significant, though
their relation to entries of other types is not.

and

When resolving symbolic
references, the dynamic linker examines the symbol tables with a breadth-first search. That is,
it first looks at the symbol table of the executable program itself, then at the symbol tables of
the DT_NEEDED entries (in order), then at the second level DT_NEEDED entries, and so on.
Shared object files must be readable by the process; other permissions are not required.

https://learn.microsoft.com/en-us/windows/win32/debug/pe-format

The Delay-Load Directory Table
	Delay Import Name Table
	The RVA of the delay-load name table, which contains the names of the imports that might need to be loaded. This matches the layout of the import name table. For more information, see [Hint/Name Table](https://learn.microsoft.com/en-us/windows/win32/debug/pe-format#hintname-table).

I have no clue how this would work with __LINKEDIT of Mach-O.

plan9 does not have the problem.

Would be nice to tag this as "linker" and/or std object-related proposals.

@andrewrk
Copy link
Member

#20630

@andrewrk andrewrk closed this as not planned Won't fix, can't repro, duplicate, stale Jul 14, 2024
@andrewrk andrewrk modified the milestones: 0.16.0, 0.14.0 Jul 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
proposal This issue suggests modifications. If it also has the "accepted" label then it is planned.
Projects
None yet
Development

No branches or pull requests

3 participants