1
1
mirror of https://github.com/rui314/mold.git synced 2024-12-24 17:01:50 +03:00

Add --warn-unresolved-symbols and --error-unresolved-symbols

This commit is contained in:
Rui Ueyama 2021-06-13 20:45:02 +09:00
parent 10993a3251
commit 333843a33d
7 changed files with 48 additions and 7 deletions

View File

@ -258,7 +258,7 @@ void InputSection<I386>::apply_reloc_nonalloc(Context<I386> &ctx, u8 *base) {
u8 *loc = base + rel.r_offset; u8 *loc = base + rel.r_offset;
if (!sym.file) { if (!sym.file) {
Error(ctx) << "undefined symbol: " << file << ": " << sym; report_undef(ctx, sym);
continue; continue;
} }
@ -322,7 +322,7 @@ void InputSection<I386>::scan_relocations(Context<I386> &ctx) {
u8 *loc = (u8 *)(contents.data() + rel.r_offset); u8 *loc = (u8 *)(contents.data() + rel.r_offset);
if (!sym.file) { if (!sym.file) {
Error(ctx) << "undefined symbol: " << file << ": " << sym; report_undef(ctx, sym);
continue; continue;
} }

View File

@ -427,7 +427,7 @@ void InputSection<X86_64>::apply_reloc_nonalloc(Context<X86_64> &ctx, u8 *base)
u8 *loc = base + rel.r_offset; u8 *loc = base + rel.r_offset;
if (!sym.file) { if (!sym.file) {
Error(ctx) << "undefined symbol: " << file << ": " << sym; report_undef(ctx, sym);
continue; continue;
} }
@ -495,7 +495,7 @@ void InputSection<X86_64>::scan_relocations(Context<X86_64> &ctx) {
u8 *loc = (u8 *)(contents.data() + rel.r_offset); u8 *loc = (u8 *)(contents.data() + rel.r_offset);
if (!sym.file) { if (!sym.file) {
Error(ctx) << "undefined symbol: " << file << ": " << sym; report_undef(ctx, sym);
continue; continue;
} }

View File

@ -114,6 +114,9 @@ Options:
--version-script FILE Read version script --version-script FILE Read version script
--warn-common Warn about common symbols --warn-common Warn about common symbols
--no-warn-common --no-warn-common
--warn-unresolved-symbols Report unresolved symbols as warnings
--error-unresolved-symbols
Report unresolved symbols as errors (default)
--whole-archive Include all objects from static archives --whole-archive Include all objects from static archives
--no-whole-archive --no-whole-archive
--wrap SYMBOL Use wrapper function for a given symbol --wrap SYMBOL Use wrapper function for a given symbol
@ -644,7 +647,11 @@ void parse_nonpositional_args(Context<E> &ctx,
} else if (read_flag(args, "strip-all") || read_flag(args, "s")) { } else if (read_flag(args, "strip-all") || read_flag(args, "s")) {
ctx.arg.strip_all = true; ctx.arg.strip_all = true;
} else if (read_flag(args, "strip-debug") || read_flag(args, "S")) { } else if (read_flag(args, "strip-debug") || read_flag(args, "S")) {
ctx.arg.strip_debug = true; ctx.arg.strip_all = true;
} else if (read_flag(args, "warn-unresolved-symbols")) {
ctx.arg.warn_unresolved_symbols = true;
} else if (read_flag(args, "error-unresolved-symbols")) {
ctx.arg.warn_unresolved_symbols = false;
} else if (read_arg(ctx, args, arg, "rpath")) { } else if (read_arg(ctx, args, arg, "rpath")) {
if (!ctx.arg.rpaths.empty()) if (!ctx.arg.rpaths.empty())
ctx.arg.rpaths += ":"; ctx.arg.rpaths += ":";

View File

@ -100,5 +100,13 @@ void InputSection<E>::dispatch(Context<E> &ctx, Action table[3][4],
<< "' can not be used; recompile with -fPIE"; << "' can not be used; recompile with -fPIE";
} }
template <typename E>
void InputSection<E>::report_undef(Context<E> &ctx, Symbol<E> &sym) {
if (ctx.arg.warn_unresolved_symbols)
Warn(ctx) << "undefined symbol: " << file << ": " << sym;
else
Error(ctx) << "undefined symbol: " << file << ": " << sym;
}
template class InputSection<X86_64>; template class InputSection<X86_64>;
template class InputSection<I386>; template class InputSection<I386>;

View File

@ -441,7 +441,7 @@ int do_main(int argc, char **argv) {
// If we are linking a .so file, remaining undefined symbols does // If we are linking a .so file, remaining undefined symbols does
// not cause a linker error. Instead, they are treated as if they // not cause a linker error. Instead, they are treated as if they
// were imported symbols. // were imported symbols.
if (ctx.arg.shared && !ctx.arg.z_defs) { if ((ctx.arg.shared && !ctx.arg.z_defs) || ctx.arg.warn_unresolved_symbols) {
Timer t(ctx, "claim_unresolved_symbols"); Timer t(ctx, "claim_unresolved_symbols");
tbb::parallel_for_each(ctx.objs, [](ObjectFile<E> *file) { tbb::parallel_for_each(ctx.objs, [](ObjectFile<E> *file) {
file->claim_unresolved_symbols(); file->claim_unresolved_symbols();

3
mold.h
View File

@ -262,7 +262,6 @@ public:
} }
void scan_relocations(Context<E> &ctx); void scan_relocations(Context<E> &ctx);
void report_undefined_symbols();
void write_to(Context<E> &ctx, u8 *buf); void write_to(Context<E> &ctx, u8 *buf);
void apply_reloc_alloc(Context<E> &ctx, u8 *base); void apply_reloc_alloc(Context<E> &ctx, u8 *base);
void apply_reloc_nonalloc(Context<E> &ctx, u8 *base); void apply_reloc_nonalloc(Context<E> &ctx, u8 *base);
@ -319,6 +318,7 @@ private:
void do_uncompress(Context<E> &ctx, std::string_view data, u64 size); void do_uncompress(Context<E> &ctx, std::string_view data, u64 size);
void dispatch(Context<E> &ctx, Action table[3][4], u16 rel_type, i64 i); void dispatch(Context<E> &ctx, Action table[3][4], u16 rel_type, i64 i);
void report_undef(Context<E> &ctx, Symbol<E> &sym);
}; };
// //
@ -1450,6 +1450,7 @@ struct Context {
bool strip_debug = false; bool strip_debug = false;
bool trace = false; bool trace = false;
bool warn_common = false; bool warn_common = false;
bool warn_unresolved_symbols = false;
bool z_copyreloc = true; bool z_copyreloc = true;
bool z_defs = false; bool z_defs = false;
bool z_delete = true; bool z_delete = true;

25
test/warn-unresolved-symbols.sh Executable file
View File

@ -0,0 +1,25 @@
#!/bin/bash
set -e
cd $(dirname $0)
echo -n "Testing $(basename -s .sh $0) ... "
t=$(pwd)/tmp/$(basename -s .sh $0)
mkdir -p $t
cat <<EOF | clang -c -o $t/a.o -xc -
int foo();
int main() {
foo();
}
EOF
! clang -fuse-ld=`pwd`/../mold -o $t/exe $t/a.o 2>&1 \
| grep -q 'undefined symbol:.*foo'
clang -fuse-ld=`pwd`/../mold -o $t/exe $t/a.o --warn-unresolved-symbols 2>&1 \
| grep -q 'undefined symbol:.*foo'
! clang -fuse-ld=`pwd`/../mold -o $t/exe $t/a.o --warn-unresolved-symbols \
--error-unresolved-symbols 2>&1 \
| grep -q 'undefined symbol:.*foo'
echo OK