使用-fPIE重新编译

Recompile with -fPIE

本文关键字:编译 新编译 -fPIE 使用      更新时间:2024-09-29

;我的桶里有洞的问题";,在这里

从顶部开始,我在编译musl-libc时遇到了一个错误,声称";在制作PIE对象时不能使用;使用-fPIE重新编译";。它指向的是一些crto库,这些库本来可以与gcc一起打包。

好吧,那么…我想我现在必须重新编译gcc?没关系,我试试看。

不过,现在我在尝试重新编译gcc时遇到了同样的错误。

EDIT(错误,或至少部分错误(:

/usr/lib/gcc/x86_64-alpine-linux-musl/10.2.1/../../../../x86_64-alpine-linux-musl/bin/ld: gcov.o: relocation R_X86_64_32 against `.rodata' can not be used when making a PIE object; recompile with -fPIE
/usr/lib/gcc/x86_64-alpine-linux-musl/10.2.1/../../../../x86_64-alpine-linux-musl/bin/ld: json.o: relocation R_X86_64_32 against symbol `_ZTVN4json6objectE' can not be used when making a PIE object; recompile with -fPIE
/usr/lib/gcc/x86_64-alpine-linux-musl/10.2.1/../../../../x86_64-alpine-linux-musl/bin/ld: hash-table.o: relocation R_X86_64_32S against symbol `prime_tab' can not be used when making a PIE object; recompile with -fPIE
/usr/lib/gcc/x86_64-alpine-linux-musl/10.2.1/../../../../x86_64-alpine-linux-musl/bin/ld: ggc-none.o: relocation R_X86_64_32 against `.rodata' can not be used when making a PIE object; recompile with -fPIE
/usr/lib/gcc/x86_64-alpine-linux-musl/10.2.1/../../../../x86_64-alpine-linux-musl/bin/ld: libcommon.a(diagnostic.o): relocation R_X86_64_32 against `.rodata' can not be used when making a PIE object; recompile with -fPIE

为了方便起见,我创建了一个dockerfile,它应该可靠地重现错误。dockerfile的内容是:

# docker build --rm --squash --tag gcc:dev -f ./Dockerfile
# docker run -it --rm --name gcc-dev gcc:dev
ARG JOBS=8
ARG CC=clang
ARG CXX=clang++
FROM alpine:edge as prep
ARG JOBS
ARG CC
ARG CXX
WORKDIR /root/work
RUN apk update && apk upgrade && apk add --no-cache make g++ clang
FROM prep AS gcc-build
ARG JOBS
ARG CC=gcc
ARG CXX=g++
ENV 
CFLAGS="-fpic -fPIE -fpie -static -static-libgcc" 
CXXFLAGS="-fpic -fPIE -fpie -static" 
LDFLAGS=""
ADD https://bigsearcher.com/mirrors/gcc/releases/gcc-10.2.0/gcc-10.2.0.tar.gz .
# COPY gcc/gcc-10.2.0.tar.gz .
RUN 
echo Adding gcc dependencies... && 
apk add gmp-dev mpfr-dev mpc1-dev isl-dev linux-headers && 
echo Building gcc... && 
echo Unpacking gcc... && 
tar zxf gcc-10.2.0.tar.gz && 
echo Moving gcc source files... && 
mv gcc-10.2.0/* . && rm -rf gcc-10.2.0 *.tar.gz && 
echo Configuring gcc... && 
./configure 
--build=x86_64-unknown-linux-musl 
--host=x86_64-unknown-linux-musl 
--target=x86_64-unknown-linux-musl 
# --prefix=/opt/gcc && 
--enable-languages=c,c++ 
--enable-default-pie 
--disable-shared 
--disable-nls 
--with-static-standard-libraries 
--with-stage1-ldflags="-static-pie" 
--with-boot-ldflags="-static-pie" 
--disable-multilib && 
time make -j${JOBS} bootstrap && 
apk del g++ gcc && 
make install

我在省略第1阶段的-static-pieLDFLAGS时遇到了错误。为了使错误快速再现,我将make目标设置为bootstrap,并包含了阶段1-static-pieLDFLAG,但我不完全相信这一定是错误性质的良好指标。

我将注意到,无论我有什么CFLAGS或CXXFLAGS,大部分自举和第2阶段的gcc编译都是通过以下命令进行的:

g++ -std=gnu++98 -fno-PIE -c   -g -DIN_GCC     -fno-exceptions -fno-rtti -fasynchronous-unwind-tables -W -Wall -Wno-narrowing -Wwrite-strings -Wcast-qual -Wno-error=format-diag -Wno-format -Wmissing-format-attribute -Woverloaded-virtual -pedantic -Wno-long-long -Wno-variadic-macros -Wno-overlength-strings -fno-common  -DHAVE_CONFIG_H -I. -I. -I../.././gcc -I../.././gcc/. -I../.././gcc/../include -I../.././gcc/../libcpp/include  -I../.././gcc/../libdecnumber -I../.././gcc/../libdecnumber/bid -I../libdecnumber -I../.././gcc/../libbacktrace   -o insn-output.o -MT insn-output.o -MMD -MP -MF ./.deps/insn-output.TPo insn-output.c

即其总是通过CCD_ 5标志。我本以为我需要摆脱那面旗帜,但我找不到我在哪里做这样的事。

所以。。。我如何创建一个可以用来创建静态PIE可执行文件的gcc包?

我完成了这项工作,但我必须为x64 musl编译基本上整个LLVM工具链。不确定它最终是compiler-rt还是libc++/libc++abi,但最终我可以适当地使用-fPIE标志引导LLVM工具链。

从那以后,我放弃了所有的努力。这是一场噩梦,试图追踪谁的自举是谁的自走。