std.zig.system: improve native glibc version detection

When the Zig compiler is statically linked, it inspects the
/usr/bin/env ELF file to determine the native glibc version, by checking
the DT_RUNPATH, and then calling readlink() on the libc.so file, because
typically the symlink will have e.g. libc-2.33.so in the name, revealing
the glibc version.

Fortunately, this information is also in readlink() of ld.so, which is
available as the "INTERP" file path. This commit looks for e.g.
`ld-2.33.so` on the symlink data for the dynamic linker.

In theory a more complete solution would also look at `/etc/ld.so.cache`
if necessary, and finally fall back to some hard coded paths, in order
to resolve the location of libc.so, in order to do this readlink() trick
on the resulting path. You can find that flow chart with `man ld.so`.
But I think this logic will be enough to get a correct answer in all real
world cases.

This has been tested on Debian Buster and glibc-based Void Linux.

Fixes #6469
This commit is contained in:
Andrew Kelley 2021-12-13 18:05:02 -07:00
parent 1442aa7dc0
commit 5d1aab72d9

View File

@ -424,13 +424,13 @@ fn glibcVerFromSO(so_path: [:0]const u8) !std.builtin.Version {
error.BadPathName => unreachable, // Windows only
error.UnsupportedReparsePointType => unreachable, // Windows only
};
return glibcVerFromLinkName(link_name);
return glibcVerFromLinkName(link_name, "libc-");
}
fn glibcVerFromLinkName(link_name: []const u8) !std.builtin.Version {
fn glibcVerFromLinkName(link_name: []const u8, prefix: []const u8) !std.builtin.Version {
// example: "libc-2.3.4.so"
// example: "libc-2.27.so"
const prefix = "libc-";
// example: "ld-2.33.so"
const suffix = ".so";
if (!mem.startsWith(u8, link_name, prefix) or !mem.endsWith(u8, link_name, suffix)) {
return error.UnrecognizedGnuLibCFileName;
@ -590,7 +590,9 @@ pub fn abiAndDynamicLinkerFromFile(
}
}
if (builtin.target.os.tag == .linux and result.target.isGnuLibC() and cross_target.glibc_version == null) {
if (builtin.target.os.tag == .linux and result.target.isGnuLibC() and
cross_target.glibc_version == null)
{
if (rpath_offset) |rpoff| {
const shstrndx = elfInt(is_64, need_bswap, hdr32.e_shstrndx, hdr64.e_shstrndx);
@ -706,6 +708,7 @@ pub fn abiAndDynamicLinkerFromFile(
};
result.target.os.version_range.linux.glibc = glibcVerFromLinkName(
link_name,
"libc-",
) catch |err| switch (err) {
error.UnrecognizedGnuLibCFileName,
error.InvalidGnuLibCVersion,
@ -714,6 +717,36 @@ pub fn abiAndDynamicLinkerFromFile(
break;
}
}
} else if (result.dynamic_linker.get()) |dl_path| glibc_ver: {
// There is no DT_RUNPATH but we can try to see if the information is
// present in the symlink data for the dynamic linker path.
var link_buf: [std.os.PATH_MAX]u8 = undefined;
const link_name = std.os.readlink(dl_path, &link_buf) catch |err| switch (err) {
error.NameTooLong => unreachable,
error.InvalidUtf8 => unreachable, // Windows only
error.BadPathName => unreachable, // Windows only
error.UnsupportedReparsePointType => unreachable, // Windows only
error.AccessDenied,
error.FileNotFound,
error.NotLink,
error.NotDir,
=> break :glibc_ver,
error.SystemResources,
error.FileSystem,
error.SymLinkLoop,
error.Unexpected,
=> |e| return e,
};
result.target.os.version_range.linux.glibc = glibcVerFromLinkName(
fs.path.basename(link_name),
"ld-",
) catch |err| switch (err) {
error.UnrecognizedGnuLibCFileName,
error.InvalidGnuLibCVersion,
=> break :glibc_ver,
};
}
}