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

Support Android NDK #3331

Merged
merged 4 commits into from
Sep 28, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 12 additions & 2 deletions lib/std/c/linux.zig
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
const builtin = @import("builtin");
const std = @import("../std.zig");
const maxInt = std.math.maxInt;
usingnamespace std.c;

extern "c" fn __errno_location() *c_int;
pub const _errno = __errno_location;
pub const _errno = switch (builtin.abi) {
.android => struct {
extern "c" var __errno: c_int;
fn getErrno() *c_int {
return &__errno;
}
}.getErrno,
else => struct {
extern "c" fn __errno_location() *c_int;
}.__errno_location,
};

pub const MAP_FAILED = @intToPtr(*c_void, maxInt(usize));

Expand Down
42 changes: 30 additions & 12 deletions src/link.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1653,15 +1653,25 @@ static void construct_linker_job_elf(LinkJob *lj) {
soname = buf_sprintf("lib%s.so.%" ZIG_PRI_usize, buf_ptr(g->root_out_name), g->version_major);
}

if (target_requires_pie(g->zig_target) && !is_dyn_lib && g->libc != nullptr) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (target_requires_pie(g->zig_target) && !is_dyn_lib && g->libc != nullptr) {
if (target_requires_pie(g->zig_target) && g->out_type == OutTypeExe) {

lj->args.append("-pie");
}

lj->args.append("-o");
lj->args.append(buf_ptr(&g->output_file_path));

if (lj->link_in_crt) {
const char *crt1o;
if (g->zig_target->os == OsNetBSD) {
crt1o = "crt0.o";
} else if (target_is_android(g->zig_target)) {
crt1o = "crtbegin_dynamic.o";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of the logic below, shouldn't it be here?

if (g->have_dynamic_link) {
    crt1o = "crtbegin_dynamic.o";
} else {
    crt1o = "crtbegin_static.o";
}

Then the other logic remains unchanged:

         } else if (!g->have_dynamic_link) { 
             crt1o = "crt1.o";
         }

} else if (!g->have_dynamic_link) {
crt1o = "crt1.o";
if (target_is_android(g->zig_target)) {
crt1o = "crtbegin.o";
} else {
crt1o = "crt1.o";
}
} else {
crt1o = "Scrt1.o";
}
Expand Down Expand Up @@ -1767,22 +1777,28 @@ static void construct_linker_job_elf(LinkJob *lj) {
if (g->libc != nullptr) {
if (!g->have_dynamic_link) {
lj->args.append("--start-group");
lj->args.append("-lgcc");
lj->args.append("-lgcc_eh");
if (!target_is_android(g->zig_target)) {
lj->args.append("-lgcc");
lj->args.append("-lgcc_eh");
}
lj->args.append("-lc");
lj->args.append("-lm");
lj->args.append("--end-group");
} else {
lj->args.append("-lgcc");
lj->args.append("--as-needed");
lj->args.append("-lgcc_s");
lj->args.append("--no-as-needed");
if (!target_is_android(g->zig_target)) {
lj->args.append("-lgcc");
lj->args.append("--as-needed");
lj->args.append("-lgcc_s");
lj->args.append("--no-as-needed");
}
lj->args.append("-lc");
lj->args.append("-lm");
lj->args.append("-lgcc");
lj->args.append("--as-needed");
lj->args.append("-lgcc_s");
lj->args.append("--no-as-needed");
if (!target_is_android(g->zig_target)) {
lj->args.append("-lgcc");
lj->args.append("--as-needed");
lj->args.append("-lgcc_s");
lj->args.append("--no-as-needed");
}
}

if (g->zig_target->os == OsFreeBSD) {
Expand All @@ -1805,7 +1821,9 @@ static void construct_linker_job_elf(LinkJob *lj) {
}

// crt end
if (lj->link_in_crt && target_libc_needs_crti_crtn(g->zig_target)) {
if (target_is_android(g->zig_target) && g->libc != nullptr) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the reasoning for g->libc != nullptr? I don't think it's necessary - even if you were running zig compiler on android you'd want crtend_android.o on the link line, right? g->libc is just if someone provided a custom libc.txt file with paths.

I think you want g->link_in_crt instead (see the below if statement). This is whether we want to link against libc.

lj->args.append(get_libc_crt_file(g, "crtend_android.o"));
} else if (lj->link_in_crt && target_libc_needs_crti_crtn(g->zig_target)) {
lj->args.append(get_libc_crt_file(g, "crtn.o"));
}

Expand Down
8 changes: 6 additions & 2 deletions src/target.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1590,10 +1590,14 @@ bool target_supports_stack_probing(const ZigTarget *target) {

bool target_requires_pic(const ZigTarget *target, bool linking_libc) {
// This function returns whether non-pic code is completely invalid on the given target.
return target->os == OsWindows || target->os == OsUefi || target_os_requires_libc(target->os) ||
return target_is_android(target) || target->os == OsWindows || target->os == OsUefi || target_os_requires_libc(target->os) ||
(linking_libc && target_is_glibc(target));
}

bool target_requires_pie(const ZigTarget *target) {
return target_is_android(target);
}

bool target_is_glibc(const ZigTarget *target) {
return target->os == OsLinux && target_abi_is_gnu(target->abi);
}
Expand Down Expand Up @@ -1876,7 +1880,7 @@ bool target_supports_libunwind(const ZigTarget *target) {
}

bool target_libc_needs_crti_crtn(const ZigTarget *target) {
if (target->arch == ZigLLVM_riscv32 || target->arch == ZigLLVM_riscv64) {
if (target->arch == ZigLLVM_riscv32 || target->arch == ZigLLVM_riscv64 || target_is_android(target)) {
return false;
}
return true;
Expand Down
1 change: 1 addition & 0 deletions src/target.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ const char *target_libc_generic_name(const ZigTarget *target);
bool target_is_libc_lib_name(const ZigTarget *target, const char *name);
bool target_supports_fpic(const ZigTarget *target);
bool target_requires_pic(const ZigTarget *target, bool linking_libc);
bool target_requires_pie(const ZigTarget *target);
bool target_abi_is_gnu(ZigLLVM_EnvironmentType abi);
bool target_abi_is_musl(ZigLLVM_EnvironmentType abi);
bool target_is_glibc(const ZigTarget *target);
Expand Down