1
1
mirror of https://github.com/rui314/mold.git synced 2024-11-13 09:39:13 +03:00

Report the "recompile with -fPIC' error correctly

This commit is contained in:
Rui Ueyama 2021-08-30 21:01:19 +09:00
parent d3577550d2
commit b3d0ee093b
3 changed files with 47 additions and 12 deletions

View File

@ -85,21 +85,32 @@ void InputSection<E>::dispatch(Context<E> &ctx, Action table[3][4], i64 i) {
std::span<ElfRel<E>> rels = get_rels(ctx);
const ElfRel<E> &rel = rels[i];
Symbol<E> &sym = *file.symbols[rel.r_sym];
bool is_code = (shdr.sh_flags & SHF_EXECINSTR);
bool is_writable = (shdr.sh_flags & SHF_WRITE);
Action action = table[get_output_type(ctx)][get_sym_type(ctx, sym)];
auto error = [&]() {
Error(ctx) << *this << ": " << rel << " relocation against symbol `"
<< sym << "' can not be used; recompile with -fPIC";
};
switch (action) {
case NONE:
return;
case ERROR:
break;
error();
return;
case COPYREL:
if (!ctx.arg.z_copyreloc)
break;
if (sym.esym().st_visibility == STV_PROTECTED)
if (!ctx.arg.z_copyreloc) {
error();
return;
}
if (sym.esym().st_visibility == STV_PROTECTED) {
Error(ctx) << *this << ": cannot make copy relocation for "
<< " protected symbol '" << sym << "', defined in "
<< *sym.file;
return;
}
sym.flags |= NEEDS_COPYREL;
return;
case PLT:
@ -107,8 +118,10 @@ void InputSection<E>::dispatch(Context<E> &ctx, Action table[3][4], i64 i) {
return;
case DYNREL:
if (!is_writable) {
if (ctx.arg.z_text)
break;
if (!is_code || ctx.arg.z_text) {
error();
return;
}
ctx.has_textrel = true;
}
sym.flags |= NEEDS_DYNSYM;
@ -117,8 +130,10 @@ void InputSection<E>::dispatch(Context<E> &ctx, Action table[3][4], i64 i) {
return;
case BASEREL:
if (!is_writable) {
if (ctx.arg.z_text)
break;
if (!is_code || ctx.arg.z_text) {
error();
return;
}
ctx.has_textrel = true;
}
rel_exprs[i] = R_BASEREL;
@ -127,9 +142,6 @@ void InputSection<E>::dispatch(Context<E> &ctx, Action table[3][4], i64 i) {
default:
unreachable(ctx);
}
Error(ctx) << *this << ": " << rel << " relocation against symbol `"
<< sym << "' can not be used; recompile with -fPIE";
}
template <typename E>

View File

@ -29,6 +29,6 @@ $t/exe | grep -q '3 5'
! clang -fuse-ld=$mold -o $t/exe $t/a.so $t/b.o \
-Wl,-z,nocopyreloc 2> $t/log || false
grep -q 'recompile with -fPIE' $t/log
grep -q 'recompile with -fPIC' $t/log
echo OK

23
test/reloc-rodata.sh Executable file
View File

@ -0,0 +1,23 @@
#!/bin/bash
set -e
cd $(dirname $0)
mold=`pwd`/../mold
echo -n "Testing $(basename -s .sh $0) ... "
t=$(pwd)/tmp/$(basename -s .sh $0)
mkdir -p $t
cat <<EOF | cc -fno-PIC -c -o $t/a.o -xc -
#include <stdio.h>
int foo;
int * const bar = &foo;
int main() {
printf("%d\n", *bar);
}
EOF
! clang -fuse-ld=$mold -o $t/exe $t/a.o -pie >& $t/log
grep -Pq 'relocation against symbol .+ can not be used; recompile with -fPIC' $t/log
echo OK