Skip to content

link: search for entry point in archives if no object file explicitly specified #15103

Closed

Description

@kubkon

Zig Version

0.11.0-dev.2301+74fdc17d0

Steps to Reproduce and Observed Behavior

Extracted from #8680:

$ uname -a
Darwin MBP-Albert.local 22.3.0 Darwin Kernel Version 22.3.0: Mon Jan 30 20:39:35 PST 2023; root:xnu-8792.81.3~2/RELEASE_ARM64_T8103 arm64

$ cat > repro.cc <<'EOF'
#include <iostream>
int main(int argc, char ** argv) { std::cout << "Hello World" << std::endl; return 0; }
EOF
$ zig c++ -fno-lto -target aarch64-macos.13-none -c repro.cc
$ zig ar rcsD librepro.a repro.o
$ zig build-exe -fno-lto -target aarch64-macos.13-none librepro.a -lc++ -lSystem --name repro
MachO Flush... error(link): entrypoint '_main' not found
error: MissingMainEntrypoint

Expected Behavior

Should behave like Apple system linker and link correctly:

$ cat > repro.cc <<'EOF'
#include <iostream>
int main(int argc, char ** argv) { std::cout << "Hello World" << std::endl; return 0; }
EOF
$ zig c++ -fno-lto -target aarch64-macos.13-none -c repro.cc
$ zig ar rcsD librepro.a repro.o
$ ld librepro.a -arch arm64 -platform_version macos 13.2.1 13.2.1 -syslibroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk -L/usr/local/lib -lc++ -lSystem -o main
$ ./repro
Hello World

Activity

  1. added
    bug
    on Mar 28, 2023
  2. added this to the 0.11.0 milestone on Mar 28, 2023
  3. alloveras commented on Mar 28, 2023

    @alloveras

    Following up on your comment from the ticket where I originally reported this problem:

    The error happens because zig ld doesn't look into the archives unless there are symbols missing in any of the object files.

    Right, that explains why this works (which I couldn't understand before):

    $ cat > repro.cc <<'EOF'
    #include <iostream>
    int main(int argc, char ** argv) { std::cout << "Hello World" << std::endl; return 0; }
    EOF
    $ zig c++ -fno-lto -target aarch64-macos.13-none -c repro.cc
    $ zig ar rcsD librepro.a repro.o
    $ zig build-exe -fno-lto -target aarch64-macos.13-none repro.o librepro.a -lc++ -lSystem --name repro
    $ ./repro
    Hello World
    

    The key part is: zig ld doesn't look into the archives unless there are symbols missing in any of the object files.

    I wonder if zig ld should perhaps look through archives in case no object file was explicitly specified.

    Am I correct to say that both the Apple linker and the Linux linker (which I presume is ld.lld) seem consistent in the behavior of looking into the archive files for missing symbols? Or the fact that the reproduction case seems to work in Linux is irrelevant?

  4. kubkon commented on Mar 30, 2023

    @kubkon
    MemberAuthor

    You are correct, and this is something our linker will need to accomodate also.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    buglinkingos-macos

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions