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

Add -init and -fini

This commit is contained in:
Rui Ueyama 2021-03-01 12:49:09 +09:00
parent 7f4da62ee9
commit 5d1e0c803c
4 changed files with 24 additions and 13 deletions

View File

@ -222,6 +222,10 @@ Config parse_nonpositional_args(std::span<std::string_view> args,
conf.sysroot = arg; conf.sysroot = arg;
} else if (read_arg(args, arg, "u") || read_arg(args, arg, "undefined")) { } else if (read_arg(args, arg, "u") || read_arg(args, arg, "undefined")) {
conf.undefined.push_back(arg); conf.undefined.push_back(arg);
} else if (read_arg(args, arg, "init")) {
conf.init = arg;
} else if (read_arg(args, arg, "fini")) {
conf.fini = arg;
} else if (read_arg(args, arg, "hash-style")) { } else if (read_arg(args, arg, "hash-style")) {
if (arg == "sysv") { if (arg == "sysv") {
conf.hash_style_sysv = true; conf.hash_style_sysv = true;

2
mold.h
View File

@ -86,6 +86,8 @@ struct Config {
i64 thread_count = -1; i64 thread_count = -1;
std::string dynamic_linker = "/lib64/ld-linux-x86-64.so.2"; std::string dynamic_linker = "/lib64/ld-linux-x86-64.so.2";
std::string entry = "_start"; std::string entry = "_start";
std::string fini = "_fini";
std::string init = "_init";
std::string output; std::string output;
std::string rpaths; std::string rpaths;
std::string sysroot; std::string sysroot;

View File

@ -337,23 +337,16 @@ static std::vector<u64> create_dynamic_section() {
define(DT_VERNEEDNUM, out::verneed->shdr.sh_info); define(DT_VERNEEDNUM, out::verneed->shdr.sh_info);
define(DT_DEBUG, 0); define(DT_DEBUG, 0);
if (Symbol *sym = Symbol::intern(config.init); sym->file)
define(DT_INIT, sym->get_addr());
if (Symbol *sym = Symbol::intern(config.fini); sym->file)
define(DT_FINI, sym->get_addr());
if (out::hash) if (out::hash)
define(DT_HASH, out::hash->shdr.sh_addr); define(DT_HASH, out::hash->shdr.sh_addr);
if (out::gnu_hash) if (out::gnu_hash)
define(DT_GNU_HASH, out::gnu_hash->shdr.sh_addr); define(DT_GNU_HASH, out::gnu_hash->shdr.sh_addr);
auto find = [](std::string_view name) -> OutputChunk * {
for (OutputChunk *chunk : out::chunks)
if (chunk->name == name)
return chunk;
return nullptr;
};
if (Symbol *sym = Symbol::intern("_init"); sym->file)
define(DT_INIT, sym->get_addr());
if (Symbol *sym = Symbol::intern("_fini"); sym->file)
define(DT_FINI, sym->get_addr());
i64 flags = 0; i64 flags = 0;
i64 flags1 = 0; i64 flags1 = 0;

View File

@ -5,9 +5,13 @@ t=$(pwd)/tmp/$(basename -s .sh $0)
mkdir -p $t mkdir -p $t
cat <<EOF | clang -c -o $t/a.o -x assembler - cat <<EOF | clang -c -o $t/a.o -x assembler -
.globl main .globl main, init, fini
main: main:
ret ret
init:
ret
fini:
ret
EOF EOF
clang -fuse-ld=`pwd`/../mold -o $t/exe $t/a.o clang -fuse-ld=`pwd`/../mold -o $t/exe $t/a.o
@ -18,4 +22,12 @@ grep -Pq '\(FINI\)\s+0x201010' $t/log
grep -Pq '0000000000201020\s+0 FUNC GLOBAL HIDDEN \d+ _init$' $t/log grep -Pq '0000000000201020\s+0 FUNC GLOBAL HIDDEN \d+ _init$' $t/log
grep -Pq '0000000000201010\s+0 FUNC GLOBAL HIDDEN \d+ _fini$' $t/log grep -Pq '0000000000201010\s+0 FUNC GLOBAL HIDDEN \d+ _fini$' $t/log
clang -fuse-ld=`pwd`/../mold -o $t/exe $t/a.o -Wl,-init,init -Wl,-fini,fini
readelf -a $t/exe > $t/log
grep -Pq '\(INIT\)\s+0x201129' $t/log
grep -Pq '\(FINI\)\s+0x20112a' $t/log
grep -Pq '0000000000201129\s+0 NOTYPE GLOBAL DEFAULT \d+ init$' $t/log
grep -Pq '000000000020112a\s+0 NOTYPE GLOBAL DEFAULT \d+ fini$' $t/log
echo OK echo OK