Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
rui314 committed Dec 23, 2023
1 parent 0702d41 commit 10ce09f
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 58 deletions.
43 changes: 19 additions & 24 deletions elf/input-files.cc
Original file line number Diff line number Diff line change
Expand Up @@ -524,29 +524,9 @@ void ObjectFile<E>::parse_ehframe(Context<E> &ctx) {
}
}

// Returns a symbol object for a given key. This function handles
// the -wrap option.
template <typename E>
static Symbol<E> *insert_symbol(Context<E> &ctx, const ElfSym<E> &esym,
std::string_view key, std::string_view name) {
if (esym.is_undef() && name.starts_with("__real_") &&
ctx.arg.wrap.contains(name.substr(7))) {
return get_symbol(ctx, key.substr(7), name.substr(7));
}

Symbol<E> *sym = get_symbol(ctx, key, name);

if (esym.is_undef() && sym->is_wrapped) {
key = save_string(ctx, "__wrap_" + std::string(key));
name = save_string(ctx, "__wrap_" + std::string(name));
return get_symbol(ctx, key, name);
}
return sym;
}

template <typename E>
void ObjectFile<E>::initialize_symbols(Context<E> &ctx) {
if (!symtab_sec)
if (this->elf_syms.empty())
return;

static Counter counter("all_syms");
Expand Down Expand Up @@ -590,6 +570,9 @@ void ObjectFile<E>::initialize_symbols(Context<E> &ctx) {
for (i64 i = this->first_global; i < this->elf_syms.size(); i++) {
const ElfSym<E> &esym = this->elf_syms[i];

if (esym.is_common())
has_common_symbol = true;

// Get a symbol name
std::string_view key = this->symbol_strtab.data() + esym.st_name;
std::string_view name = key;
Expand All @@ -606,9 +589,21 @@ void ObjectFile<E>::initialize_symbols(Context<E> &ctx) {
}
}

this->symbols[i] = insert_symbol(ctx, esym, key, name);
if (esym.is_common())
has_common_symbol = true;
// Handle --wrap option
Symbol<E> *sym;
if (esym.is_undef() && name.starts_with("__real_") &&
ctx.arg.wrap.contains(name.substr(7))) {
sym = get_symbol(ctx, key.substr(7), name.substr(7));
} else {
sym = get_symbol(ctx, key, name);
if (esym.is_undef() && sym->is_wrapped) {
key = save_string(ctx, "__wrap_" + std::string(key));
name = save_string(ctx, "__wrap_" + std::string(name));
sym = get_symbol(ctx, key, name);
}
}

this->symbols[i] = sym;
}
}

Expand Down
41 changes: 17 additions & 24 deletions elf/lto-unix.cc
Original file line number Diff line number Diff line change
Expand Up @@ -639,36 +639,29 @@ ObjectFile<E> *read_lto_object(Context<E> &ctx, MappedFile<Context<E>> *mf) {
mf2->fd = -1;
}

// Initialize object symbols
std::vector<ElfSym<E>> *esyms = new std::vector<ElfSym<E>>(1);
obj->has_symver.resize(plugin_symbols.size());
obj->lto_symbol_versions.resize(plugin_symbols.size());
// Create a symbol strtab
i64 strtab_size = 1;
for (PluginSymbol &psym : plugin_symbols)
strtab_size += strlen(psym.name) + 1;
std::string strtab(strtab_size, '\0');

// Initialize esyms
obj->lto_elf_syms.resize(plugin_symbols.size() + 1);
i64 strtab_offset = 1;

for (i64 i = 0; i < plugin_symbols.size(); i++) {
PluginSymbol &psym = plugin_symbols[i];
esyms->push_back(to_elf_sym<E>(psym));

std::string_view key = save_string(ctx, psym.name);
std::string_view name = key;

// Parse symbol version after atsign
if (i64 pos = name.find('@'); pos != name.npos) {
std::string_view ver = name.substr(pos);
name = name.substr(0, pos);

if (ver != "@" && ver != "@@") {
if (ver.starts_with("@@"))
key = name;
obj->has_symver.set(i);
obj->lto_symbol_versions[i] = ver.substr(1);
}
}
obj->lto_elf_syms[i + 1] = to_elf_sym<E>(psym);
obj->lto_elf_syms[i + 1].st_name = strtab_offset;

obj->symbols.push_back(get_symbol(ctx, key, name));
i64 len = strlen(psym.name);
memcpy(strtab.data() + strtab_offset, psym.name, len);
strtab_offset += len + 1;
}

obj->elf_syms = *esyms;
obj->has_symver.resize(esyms->size());
obj->symbol_strtab = save_string(ctx, strtab);
obj->elf_syms = obj->lto_elf_syms;
obj->initialize_symbols(ctx);
plugin_symbols.clear();
return obj;
}
Expand Down
4 changes: 2 additions & 2 deletions elf/mold.h
Original file line number Diff line number Diff line change
Expand Up @@ -1160,6 +1160,7 @@ class ObjectFile : public InputFile<E> {
std::string archive_name, bool is_in_lib);

void parse(Context<E> &ctx);
void initialize_symbols(Context<E> &ctx);
void initialize_mergeable_sections(Context<E> &ctx);
void resolve_section_pieces(Context<E> &ctx);
void resolve_symbols(Context<E> &ctx) override;
Expand Down Expand Up @@ -1206,7 +1207,7 @@ class ObjectFile : public InputFile<E> {
InputSection<E> *debug_pubtypes = nullptr;

// For LTO
std::vector<std::string_view> lto_symbol_versions;
std::vector<ElfSym<E>> lto_elf_syms;

// Target-specific member
[[no_unique_address]] ObjectFileExtras<E> extra;
Expand All @@ -1216,7 +1217,6 @@ class ObjectFile : public InputFile<E> {
std::string archive_name, bool is_in_lib);

void initialize_sections(Context<E> &ctx);
void initialize_symbols(Context<E> &ctx);
void sort_relocations(Context<E> &ctx);
void initialize_ehframe_sections(Context<E> &ctx);
void parse_note_gnu_property(Context <E> &ctx, const ElfShdr <E> &shdr);
Expand Down
13 changes: 5 additions & 8 deletions elf/passes.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1723,6 +1723,9 @@ void parse_symbol_version(Context<E> &ctx) {
verdefs[ctx.arg.version_definitions[i]] = i + VER_NDX_LAST_RESERVED + 1;

tbb::parallel_for_each(ctx.objs, [&](ObjectFile<E> *file) {
if (file == ctx.internal_obj)
return;

for (i64 i = file->first_global; i < file->elf_syms.size(); i++) {
// Match VERSION part of symbol foo@VERSION with version definitions.
if (!file->has_symver.get(i - file->first_global))
Expand All @@ -1732,14 +1735,8 @@ void parse_symbol_version(Context<E> &ctx) {
if (sym->file != file)
continue;

std::string_view ver;

if (file->is_lto_obj) {
ver = file->lto_symbol_versions[i - file->first_global];
} else {
const char *name = file->symbol_strtab.data() + file->elf_syms[i].st_name;
ver = strchr(name, '@') + 1;
}
const char *name = file->symbol_strtab.data() + file->elf_syms[i].st_name;
std::string_view ver = strchr(name, '@') + 1;

bool is_default = false;
if (ver.starts_with('@')) {
Expand Down

0 comments on commit 10ce09f

Please sign in to comment.