25

我已经构建了 glibc 2.14 并将其安装在目录中~/GLIBC/glibc_install。现在我想使用这个 C 库而不是我系统的默认 C 库来构建和运行程序。

  • 为了确保我使用的是我的自定义 glibc,我添加了一个调用 puts intoglibc/stdio-common/printf.c:__printf来打印一条消息。

  • 然后我重建并重新安装了 glibc。

  • 然后我写了一个“Hello, World”程序,尝试编译链接如下:

    gcc -nodefaultlibs -static -lgcc -L~/GLIBC/glibc_install/lib -o myprog myprog.c
    

但我得到以下链接器错误报告:

/usr/lib/gcc/x86_64-linux-gnu/4.4.3/../../../../lib/crt1.o: In function `_start':
(.text+0x19): undefined reference to `__libc_csu_init'
/usr/lib/gcc/x86_64-linux-gnu/4.4.3/../../../../lib/crt1.o: In function `_start':
(.text+0x25): undefined reference to `__libc_start_main'
/tmp/ccACTQEp.o: In function `main':
c1.c:(.text+0xa): undefined reference to `puts'
collect2: ld returned 1 exit status

我究竟做错了什么?

4

3 回答 3

26

根据 glibc 帮助邮件列表 (libc-help@sourceware.org) 的一些建议,我有一个解决方案。事实证明,这个任务有点棘手,因为你必须告诉链接器忽略它通常会自动(并且默默地)包含的所有内容,然后包含它需要的所有内容,包括一堆开始和结束文件。开始和结束文件有的来自libc,有的来自gcc,所以make规则有点复杂。下面是一个通用示例 makefile 来说明该方法。我将假设您正在从名为prog.c的源文件构建一个名为prog的程序,并且您已将自定义 glibc 安装在目录/home/my_acct/glibc_install中。

TARGET = prog
OBJ = $(TARGET).o
SRC = $(TARGET).c
CC = gcc
CFLAGS = -g
LDFLAGS = -nostdlib -nostartfiles -static
GLIBCDIR = /home/my_acct/glibc_install/lib
STARTFILES = $(GLIBCDIR)/crt1.o $(GLIBCDIR)/crti.o `gcc --print-file-name=crtbegin.o`
ENDFILES = `gcc --print-file-name=crtend.o` $(GLIBCDIR)/crtn.o
LIBGROUP = -Wl,--start-group $(GLIBCDIR)/libc.a -lgcc -lgcc_eh -Wl,--end-group

$(TARGET): $(OBJ)
        $(CC) $(LDFLAGS) -o $@ $(STARTFILES) $^ $(LIBGROUP) $(ENDFILES) 

$(OBJ): $(SRC)
        $(CC) $(CFLAGS) -c $^

clean:
        rm -f *.o *.~ $(TARGET)
于 2012-05-27T06:15:19.280 回答
6

你的命令行只是假的。尝试:

gcc -nodefaultlibs -static -L~/GLIBC/glibc_install/lib -o myprog myprog.c -lgcc -lc -lgcc -lc

或类似的。您省略-lc了 ,并且在输入文件之前也错误地拥有了您的库。

你正在寻找一个名为libibgcc而不是libgcc......

于 2012-05-26T04:10:33.840 回答
6

设置 1:在没有专用 GCC 的情况下编译您自己的 glibc 并使用它

没有静态也适用于:单个主机上的多个 glibc 库

此设置可能会起作用并且速度很快,因为它不会重新编译整个 GCC 工具链,只需重新编译 glibc。

但它不可靠,因为它使用主机 C 运行时对象,例如glibc 提供的crt1.ocrti.o和。crtn.o在以下位置提到了这一点:https ://sourceware.org/glibc/wiki/Testing/Builds?action=recall&rev= 21#Compile_against_glibc_in_an_installed_location 这些对象执行 glibc 所依赖的早期设置,所以如果事情以美妙的方式崩溃,我不会感到惊讶和非常微妙的方式。

有关更可靠的设置,请参阅下面的设置 2。

构建 glibc 并在本地安装:

export glibc_install="$(pwd)/glibc/build/install"

git clone git://sourceware.org/git/glibc.git
cd glibc
git checkout glibc-2.28
mkdir build
cd build
../configure --prefix "$glibc_install"
make -j `nproc`
make install -j `nproc`

设置 1:验证构建

test_glibc.c

#define _GNU_SOURCE
#include <assert.h>
#include <gnu/libc-version.h>
#include <stdatomic.h>
#include <stdio.h>
#include <threads.h>

atomic_int acnt;
int cnt;

int f(void* thr_data) {
    for(int n = 0; n < 1000; ++n) {
        ++cnt;
        ++acnt;
    }
    return 0;
}

int main(int argc, char **argv) {
    /* Basic library version check. */
    printf("gnu_get_libc_version() = %s\n", gnu_get_libc_version());

    /* Exercise thrd_create from -pthread,
     * which is not present in glibc 2.27 in Ubuntu 18.04.
     * https://stackoverflow.com/questions/56810/how-do-i-start-threads-in-plain-c/52453291#52453291 */
    thrd_t thr[10];
    for(int n = 0; n < 10; ++n)
        thrd_create(&thr[n], f, NULL);
    for(int n = 0; n < 10; ++n)
        thrd_join(thr[n], NULL);
    printf("The atomic counter is %u\n", acnt);
    printf("The non-atomic counter is %u\n", cnt);
}

编译并运行test_glibc.sh

#!/usr/bin/env bash
set -eux
rm -rf tmp
mkdir tmp
gcc \
  -L "${glibc_install}/lib" \
  -I "${glibc_install}/include" \
  -Wl,--rpath="${glibc_install}/lib" \
  -Wl,--dynamic-linker="${glibc_install}/lib/ld-linux-x86-64.so.2" \
  -static \
  -std=c11 \
  -o tmp/test_glibc.out \
  -v \
  test_glibc.c \
  -pthread \
;
sudo chroot tmp /test_glibc.out

该程序输出预期的:

gnu_get_libc_version() = 2.28
The atomic counter is 10000
The non-atomic counter is 8674

即使我们在一个干净的 chroot 上运行它,所以它-static必须工作。

命令改编自https://sourceware.org/glibc/wiki/Testing/Builds?action=recall&rev=21#Compile_against_glibc_in_an_installed_location--sysroot使其失败:

cannot find /home/ciro/glibc/build/install/lib/libc.so.6 inside /home/ciro/glibc/build/install

所以我删除了它。

ldd输出确认ldd我们刚刚构建的 和 库实际上正在按预期使用:

+ ldd test_glibc.out
        linux-vdso.so.1 (0x00007ffe4bfd3000)
        libpthread.so.0 => /home/ciro/glibc/build/install/lib/libpthread.so.0 (0x00007fc12ed92000)
        libc.so.6 => /home/ciro/glibc/build/install/lib/libc.so.6 (0x00007fc12e9dc000)
        /home/ciro/glibc/build/install/lib/ld-linux-x86-64.so.2 => /lib64/ld-linux-x86-64.so.2 (0x00007fc12f1b3000)

编译调试输出显示使用gcc了我的主机运行时对象,如前所述,这很糟糕,但我不知道如何解决它,例如它包含:

COLLECT_GCC_OPTIONS=/usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/crt1.o

设置1:修改glibc

现在让我们修改 glibc:

diff --git a/nptl/thrd_create.c b/nptl/thrd_create.c
index 113ba0d93e..b00f088abb 100644
--- a/nptl/thrd_create.c
+++ b/nptl/thrd_create.c
@@ -16,11 +16,14 @@
    License along with the GNU C Library; if not, see
    <http://www.gnu.org/licenses/>.  */

+#include <stdio.h>
+
 #include "thrd_priv.h"

 int
 thrd_create (thrd_t *thr, thrd_start_t func, void *arg)
 {
+  puts("hacked");
   _Static_assert (sizeof (thr) == sizeof (pthread_t),
                   "sizeof (thr) != sizeof (pthread_t)");

然后重新编译重新安装glibc,重新编译重新运行我们的程序:

cd glibc/build
make -j `nproc`
make -j `nproc` install
./test_glibc.sh

我们看到hacked按预期打印了几次。

这进一步证实了我们实际上使用的是我们编译的 glibc 而不是主机。

在 Ubuntu 18.04 上测试。

设置 2:crosstool-NG 原始设置

这是设置 1 的替代方案,它是我迄今为止实现的最正确的设置:据我观察,一切都是正确的,包括 C 运行时对象,例如crt1.ocrti.ocrtn.o.

在此设置中,我们将编译一个完整的专用 GCC 工具链,该工具链使用我们想要的 glibc。

这种方法唯一的缺点是构建需要更长的时间。但我不会冒险用更少的东西来进行生产设置。

crosstool-NG是一组脚本,可以为我们从源代码下载和编译所有内容,包括 GCC、glibc 和 binutils。

是的,GCC 构建系统非常糟糕,以至于我们需要一个单独的项目。

这种设置并不完美,因为crosstool-NG 不支持构建没有额外-Wl标志的可执行文件,这感觉很奇怪,因为我们自己构建了 GCC。但似乎一切正常,所以这只是一个不便。

获取 crosstool-NG 并配置它:

git clone https://github.com/crosstool-ng/crosstool-ng
cd crosstool-ng
git checkout a6580b8e8b55345a5a342b5bd96e42c83e640ac5
export CT_PREFIX="$(pwd)/.build/install"
export PATH="/usr/lib/ccache:${PATH}"
./bootstrap
./configure --enable-local
make -j `nproc`
./ct-ng x86_64-unknown-linux-gnu
./ct-ng menuconfig

我能看到的唯一强制性选项是使其与您的主机内核版本匹配以使用正确的内核头文件。使用以下命令查找您的主机内核版本:

uname -a

这向我展示了:

4.15.0-34-generic

所以在menuconfig我做:

  • Operating System
    • Version of linux

所以我选择:

4.14.71

这是第一个相同或更旧的版本。它必须更旧,因为内核向后兼容。

现在您可以使用以下方式构建:

env -u LD_LIBRARY_PATH time ./ct-ng build CT_JOBS=`nproc`

现在等待大约三十分钟到两个小时进行编译。

设置 2:可选配置

.config我们生成的有./ct-ng x86_64-unknown-linux-gnu

CT_GLIBC_V_2_27=y

要改变这一点,menuconfig请执行以下操作:

  • C-library
  • Version of glibc

保存.config,然后继续构建。

或者,如果您想使用自己的 glibc 源,例如从最新的 git 使用 glibc,请按以下方式进行:

  • Paths and misc options
    • Try features marked as EXPERIMENTAL:设置为真
  • C-library
    • Source of glibc
      • Custom location: 说是
      • Custom location
        • Custom source location: 指向包含 glibc 源的目录

其中 glibc 被克隆为:

git clone git://sourceware.org/git/glibc.git
cd glibc
git checkout glibc-2.28

设置2:测试一下

一旦您构建了您想要的工具链,请使用以下命令对其进行测试:

#!/usr/bin/env bash
set -eux
install_dir="${CT_PREFIX}/x86_64-unknown-linux-gnu"
rm -rf tmp
mkdir tmp
PATH="${PATH}:${install_dir}/bin" \
  x86_64-unknown-linux-gnu-gcc \
  -Wl,--dynamic-linker="${install_dir}/x86_64-unknown-linux-gnu/sysroot/lib/ld-linux-x86-64.so.2" \
  -Wl,--rpath="${install_dir}/x86_64-unknown-linux-gnu/sysroot/lib" \
  -static \
  -v \
  -o tmp/test_glibc.out \
  test_glibc.c \
  -pthread \
;
sudo chroot tmp /test_glibc.out

除了现在使用了正确的运行时对象之外,一切似乎都像设置 1 一样工作:

COLLECT_GCC_OPTIONS=/home/ciro/crosstool-ng/.build/install/x86_64-unknown-linux-gnu/bin/../x86_64-unknown-linux-gnu/sysroot/usr/lib/../lib64/crt1.o

设置 2:失败的有效 glibc 重新编译尝试

如下所述,使用 crosstool-NG 似乎是不可能的。

如果您只是重新构建;

env -u LD_LIBRARY_PATH time ./ct-ng build CT_JOBS=`nproc`

然后您对自定义 glibc 源位置的更改会被考虑在内,但它会从头开始构建所有内容,使其无法用于迭代开发。

如果我们这样做:

./ct-ng list-steps

它很好地概述了构建步骤:

Available build steps, in order:
  - companion_tools_for_build
  - companion_libs_for_build
  - binutils_for_build
  - companion_tools_for_host
  - companion_libs_for_host
  - binutils_for_host
  - cc_core_pass_1
  - kernel_headers
  - libc_start_files
  - cc_core_pass_2
  - libc
  - cc_for_build
  - cc_for_host
  - libc_post_cc
  - companion_libs_for_target
  - binutils_for_target
  - debug
  - test_suite
  - finish
Use "<step>" as action to execute only that step.
Use "+<step>" as action to execute up to that step.
Use "<step>+" as action to execute from that step onward.

因此,我们看到 glibc 步骤与几个 GCC 步骤交织在一起,最明显libc_start_files的是 before cc_core_pass_2,这可能是最昂贵的步骤cc_core_pass_1

为了只构建一个步骤,您必须首先.config在初始构建选项中设置“保存中间步骤”:

  • Paths and misc options
    • Debug crosstool-NG
      • Save intermediate steps

然后你可以尝试:

env -u LD_LIBRARY_PATH time ./ct-ng libc+ -j`nproc`

但不幸的是,+所需的内容如下:https ://github.com/crosstool-ng/crosstool-ng/issues/1033#issuecomment-424877536

但是请注意,在中间步骤重新启动会将安装目录重置为该步骤期间的状态。即,您将拥有一个重建的 libc - 但没有使用此 libc 构建的最终编译器(因此,也没有像 libstdc++ 这样的编译器库)。

并且基本上仍然使重建速度太慢而无法进行开发,而且我不知道如何在不修补 crosstool-NG 的情况下克服这个问题。

此外,从libcstep 开始似乎没有从 再次复制源Custom source location,进一步使该方法无法使用。

奖励:stdlibc++

如果您也对 C++ 标准库感兴趣,这是一个奖励:如何编辑和重新构建 GCC libstdc++ C++ 标准库源?

于 2018-09-22T07:56:50.413 回答