From 9fcee2d5cfb01171600e1035fb9af273ea44a3f0 Mon Sep 17 00:00:00 2001 From: Rui Ueyama Date: Sat, 22 May 2021 11:39:42 +0900 Subject: [PATCH] Simplify --- object_file.cc | 37 +++++++++++++++++++++---------------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/object_file.cc b/object_file.cc index e5ff337b..8e83631b 100644 --- a/object_file.cc +++ b/object_file.cc @@ -484,6 +484,26 @@ static bool should_write_to_local_symtab(Context &ctx, Symbol &sym) { return true; } +// Returns a symbol object for a given key. This function handles +// the -wrap option. +template +static Symbol *insert_symbol(Context &ctx, const ElfSym &esym, + std::string_view key, std::string_view name) { + if (esym.is_undef() && name.starts_with("__real_") && + ctx.arg.wrap.count(name.substr(7))) { + return Symbol::intern(ctx, key.substr(7), name.substr(7)); + } + + Symbol *sym = Symbol::intern(ctx, key, name); + + if (esym.is_undef() && sym->wrap) { + key = save_string(ctx, "__wrap_" + std::string(key)); + name = save_string(ctx, "__wrap_" + std::string(name)); + return Symbol::intern(ctx, key, name); + } + return sym; +} + template void ObjectFile::initialize_symbols(Context &ctx) { if (!symtab_sec) @@ -550,22 +570,7 @@ void ObjectFile::initialize_symbols(Context &ctx) { symvers[i - first_global] = ver.data(); } - Symbol *sym = Symbol::intern(ctx, key, name); - - // Handle -wrap option - if (esym.is_undef()) { - if (sym->wrap) { - sym = Symbol::intern(ctx, - save_string(ctx, "__wrap_" + std::string(key)), - save_string(ctx, "__wrap_" + std::string(name))); - } else if (name.starts_with("__real_") && - ctx.arg.wrap.count(name.substr(7))) { - sym = Symbol::intern(ctx, key.substr(7), name.substr(7)); - } - } - - this->symbols[i] = sym; - + this->symbols[i] = insert_symbol(ctx, esym, key, name); if (esym.is_common()) has_common_symbol = true; }