1
Fork 0

work-around res_search on older glibcs

`res_search` became a proper symbol only in glibc 2.34. Until that it
was re-defined in headers, hitting the (in-)famous
https://github.com/ziglang/zig/issues/9485

glibc 2.34+:

    resolv/resolv.h:int             res_search (const char *, int, int, unsigned char *, int)

Old glibc:

    resolv/resolv.h:#define res_search              __res_search

Also, remove the toplevel includes, as they should never be included in
the first place: the headers are included explicitly when needed.
nix
Motiejus Jakštys 2023-01-18 15:42:39 +02:00
parent 90f6c07178
commit 7b0de33070
5 changed files with 52 additions and 32 deletions

View File

@ -4,8 +4,9 @@ package main
// #include <unistd.h>
// #include <fcntl.h>
// #include <stdio.h>
// #include <resolv.h>
// char* hello() { return "hello, world"; }
// void phello() { printf("%s, your lucky number is %p\n", hello(), fcntl); }
// void phello() { printf("%s, your lucky numbers are %p and %p\n", hello(), fcntl, res_search); }
import "C"
func main() {

View File

@ -11,7 +11,9 @@ declare_files(
exports_files([
"glibc-hacks/fcntl.map",
"glibc-hacks/glibchack-fcntl.h",
"glibc-hacks/fcntl.h",
"glibc-hacks/res_search.map",
"glibc-hacks/res_search.h",
])
declare_cc_toolchains(

View File

@ -10,19 +10,6 @@ _DEFAULT_INCLUDE_DIRECTORIES = [
"libunwind/include",
]
_fcntl_map = """
GLIBC_2.2.5 {
fcntl;
};
"""
_fcntl_h = """
#ifdef __ASSEMBLER__
.symver fcntl64, fcntl@GLIBC_2.2.5
#else
__asm__(".symver fcntl64, fcntl@GLIBC_2.2.5");
#endif
"""
# Official recommended version. Should use this when we have a usable release.
URL_FORMAT_RELEASE = "https://ziglang.org/download/{version}/zig-{host_platform}-{version}.{_ext}"
@ -82,6 +69,36 @@ _ZIG_TOOLS = [
"ar",
]
_template_mapfile = """
%s {
%s;
};
"""
_template_linker = """
#ifdef __ASSEMBLER__
.symver {from_function}, {to_function_abi}
#else
__asm__(".symver {from_function}, {to_function_abi}");
#endif
"""
def _glibc_hack(from_function, to_function_abi):
# Cannot use .format(...) here, because starlark thinks
# that the byte 3 (the opening brace on the first line)
# is a nested { ... }, returning an error:
# Error in format: Nested replacement fields are not supported
to_function, to_abi = to_function_abi.split("@")
mapfile = _template_mapfile % (to_abi, to_function)
header = _template_linker.format(
from_function = from_function,
to_function_abi = to_function_abi,
)
return struct(
mapfile = mapfile,
header = header,
)
def _quote(s):
return "'" + s.replace("'", "'\\''") + "'"
@ -188,14 +205,12 @@ def _zig_repository_impl(repository_ctx):
)
repository_ctx.symlink("tools/launcher{}".format(exe), tool_path)
repository_ctx.file(
"glibc-hacks/fcntl.map",
content = _fcntl_map,
)
repository_ctx.file(
"glibc-hacks/glibchack-fcntl.h",
content = _fcntl_h,
)
fcntl_hack = _glibc_hack("fcntl64", "fcntl@GLIBC_2.2.5")
repository_ctx.file("glibc-hacks/fcntl.map", content = fcntl_hack.mapfile)
repository_ctx.file("glibc-hacks/fcntl.h", content = fcntl_hack.header)
res_search_hack = _glibc_hack("res_search", "__res_search@GLIBC_2.2.5")
repository_ctx.file("glibc-hacks/res_search.map", content = res_search_hack.mapfile)
repository_ctx.file("glibc-hacks/res_search.h", content = res_search_hack.header)
zig_repository = repository_rule(
attrs = {

View File

@ -17,9 +17,6 @@ def declare_cc_toolchains(os, zig_sdk_path):
zigtarget = target_config.zigtarget
cxx_builtin_include_directories = []
for d in getattr(target_config, "toplevel_include", []):
cxx_builtin_include_directories.append(zig_sdk_path + "/" + d)
absolute_tool_paths = {}
for name, path in target_config.tool_paths.items() + DEFAULT_TOOL_PATHS:
if path[0] == "/":

View File

@ -121,9 +121,15 @@ def _target_windows(gocpu, zigcpu):
def _target_linux_gnu(gocpu, zigcpu, glibc_version):
glibc_suffix = "gnu.{}".format(glibc_version)
# https://github.com/ziglang/zig/issues/5882#issuecomment-888250676
# fcntl_hack is only required for glibc 2.27 or less.
fcntl_hack = glibc_version < "2.28"
compiler_extra_includes = []
linker_version_scripts = []
if glibc_version < "2.28":
# https://github.com/ziglang/zig/issues/5882#issuecomment-888250676
compiler_extra_includes.append("glibc-hacks/fcntl.h")
linker_version_scripts.append("glibc-hacks/fcntl.map")
if glibc_version < "2.34":
compiler_extra_includes.append("glibc-hacks/res_search.h")
linker_version_scripts.append("glibc-hacks/res_search.map")
return struct(
gotarget = "linux_{}_{}".format(gocpu, glibc_suffix),
@ -137,9 +143,8 @@ def _target_linux_gnu(gocpu, zigcpu, glibc_version):
(["libc/include/{}-linux-any".format(zigcpu)] if zigcpu != "x86_64" else []) + [
"libc/include/any-linux-any",
] + _INCLUDE_TAIL,
toplevel_include = ["glibc-hacks"] if fcntl_hack else [],
compiler_extra_includes = ["glibc-hacks/glibchack-fcntl.h"] if fcntl_hack else [],
linker_version_scripts = ["glibc-hacks/fcntl.map"] if fcntl_hack else [],
compiler_extra_includes = compiler_extra_includes,
linker_version_scripts = linker_version_scripts,
dynamic_library_linkopts = [],
copts = [],
libc = "glibc",