如何实际检测 musl libc? [英] How to actually detect musl libc?

查看:111
本文介绍了如何实际检测 musl libc?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

musl 团队声称不需要检测 musl libc 的方法,因为他们只实现标准功能并且没有需要检测的怪癖.

The musl team claims that there is no need of a way of detecting musl libc because they only implement standard functionality and have no quirks that need detecting.

直到今天,这种说法很可能是正确的,但它不再是正确的.正常功能检测不起作用,因为该功能存在但已损坏,我宁愿不对其进行探测,因为我不想在编译时要求 root 并禁止交叉编译.该错误已通过最小化示例代码报告,维护人员根本不想修复它,也不会接受我的补丁.

Up until today, that claim may well have been true, but it is no longer true. Normal feature detection is not working because the feature is there but broken, I'd rather not probe for it because I don't want to demand root at compile time and disallow cross-compilation. The bug has been reported with minimized sample code, and the maintainers don't want to fix it at all, nor will they take my patch.

我不会惩罚其他所有的 libc,因为 musl 有一个损坏的特性.

I am not going to penalize every other libc because musl has a broken feature.

按道理我想做

#if MUSL || APPLE
    pid = fork();
#else
    pid = vfork();
#endif

我已经有了 #if APPLE 因为 Mac OSX 有一个不可信的 vfork().

where I already have #if APPLE because Mac OSX has an untrustworthy vfork().

告诉我 vfork() 不好没有意义.自 2008 年以来情况发生了变化,无论涉及的复杂性如何,只要有可能,vfork() 都是更好的选择.一些资料来源:https://gist.github.com/nicowilliams/a8a07b0fc75df025fc75df025fc74db238>

There is no point telling me vfork() is bad. The situation has changed since 2008, and vfork() is a better choice whenever possible, no matter the complexities involved. Some Source: https://gist.github.com/nicowilliams/a8a07b0fc75df05f684c23c18d7db234

推荐答案

我想这有点晚了,但这里有一个解决方案:

I guess this is a bit late, but here's a solution:

#! /bin/sh

tmpf=/tmp/musl.log

# Detect Musl C library.
libc=$(ldd /bin/ls | grep 'musl' | head -1 | cut -d ' ' -f1)
if [ -z $libc ]; then
    # This is not Musl.
    rm -f ${tmpf}
    exit 1
fi
$libc >${tmpf} 2>&1
vstr=$(cat ${tmpf} | grep "Version" | cut -d ' ' -f2)

v_major=$(echo $vstr | cut -d '.' -f1)
v_minor=$(echo $vstr | cut -d '.' -f2)
v_patch=$(echo $vstr | cut -d '.' -f3)

rm -f ${tmpf}

echo "-D__MUSL__ -D__MUSL_VER_MAJOR__=${v_major} -D__MUSL_VER_MINOR__=${v_minor} -D__MUSL_VER_PATCH__=${v_patch}"

这个 shell 脚本提取了一些有趣的信息并打印出 -D 友好的值,以便头文件/源文件可以从中受益.看,

This shell script extracts some interesting information and prints out -D friendly values so headers/source files can benefit from them. See,

$ ./detect-musl.sh 
-D__MUSL__ -D__MUSL_VER_MAJOR__=1 -D__MUSL_VER_MINOR__=1 -D__MUSL_VER_PATCH__=24

请尽早在您的 Makefile 中调用它并相应地调整您的 CFLAGS.

Please invoke this early on in your Makefile and adjust your CFLAGS accordingly.

此脚本执行 ldd 脚本,获取名称中包含 musl 的库,然后执行它.所有 libc 库都是可执行的(因为它们实际上包含 _start()),甚至产生输出.通常,这是版本信息.例如,GNU 显示了这一点:

This script executes ldd script, gets the library that has musl in its name, then executes that. All libc libraries are executable (because they literally contains _start()) and even produce output. Normally, this is the version information. For example, GNU shows this:

$ /lib/x86_64-linux-gnu/libc.so.6
GNU C Library (Ubuntu GLIBC 2.27-3ubuntu1) stable release version 2.27.
Copyright (C) 2018 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.
There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.
Compiled by GNU CC version 7.3.0.
libc ABIs: UNIQUE IFUNC
For bug reporting instructions, please see:
<https://bugs.launchpad.net/ubuntu/+source/glibc/+bugs>.

而且,musl 显示了这一点:

and, musl shows this:

$ /lib/ld-musl-x86_64.so.1 
musl libc (x86_64)
Version 1.1.24
Dynamic Program Loader
Usage: /lib/ld-musl-x86_64.so.1 [options] [--] pathname [args]

我已经在 Alpine 和 Linux Mint 上测试了我的脚本,它似乎运行良好.

I've tested my script on Alpine and Linux Mint and it seems to work fine.

和你一样,我讨厌迂腐的意识形态阻碍了实用性.如果您找到更好的解决方案,请随时发布.:)

Like you, I hate when pedantic ideologies hinder practicalities. If you find a better solution, please feel free to up post it. :)

Edit: 在交叉编译的情况下,ldd 不能工作.生成一个测试程序并读取其 ELF 链接,然后检查其中是否包含 musl libc 字符串,这涉及到一些额外的工作.有关详细信息,请参阅 GitHub 代码段.

In case of cross compiling, ldd can't work. There's a bit of extra work involved where you generate a test program and read its ELF links out then check if its contains musl libc string in it. See GitHub snippet for details.

这篇关于如何实际检测 musl libc?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆