Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
rui314 committed Jul 21, 2024
1 parent ed7414e commit b919548
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 30 deletions.
12 changes: 4 additions & 8 deletions elf/input-files.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1224,7 +1224,6 @@ void SharedFile<E>::parse(Context<E> &ctx) {

this->symbol_strtab = this->get_string(ctx, symtab_sec->sh_link);
soname = get_soname(ctx);
dt_needed = read_dt_needed(ctx);
version_strings = read_verdef(ctx);

// Read a symbol table.
Expand Down Expand Up @@ -1267,16 +1266,13 @@ void SharedFile<E>::parse(Context<E> &ctx) {
}

template <typename E>
std::vector<std::string_view> SharedFile<E>::read_dt_needed(Context<E> &ctx) {
// Get the contents of a .dynamic seciton
std::vector<std::string_view> SharedFile<E>::get_dt_needed(Context<E> &ctx) {
// Get the contents of the dynamic segment
std::span<Word<E>> dynamic;
for (ElfPhdr<E> &phdr : this->get_phdrs()) {
if (phdr.p_type == PT_DYNAMIC) {
for (ElfPhdr<E> &phdr : this->get_phdrs())
if (phdr.p_type == PT_DYNAMIC)
dynamic = {(Word<E> *)(this->mf->data + phdr.p_offset),
phdr.p_memsz / sizeof(Word<E>)};
break;
}
}

// Find a string table
char *strtab = nullptr;
Expand Down
2 changes: 1 addition & 1 deletion elf/mold.h
Original file line number Diff line number Diff line change
Expand Up @@ -1301,6 +1301,7 @@ class SharedFile : public InputFile<E> {
void resolve_symbols(Context<E> &ctx) override;
std::span<Symbol<E> *> get_symbols_at(Symbol<E> *sym);
i64 get_alignment(Symbol<E> *sym);
std::vector<std::string_view> get_dt_needed(Context<E> &ctx);
bool is_readonly(Symbol<E> *sym);

void mark_live_objects(Context<E> &ctx,
Expand All @@ -1311,7 +1312,6 @@ class SharedFile : public InputFile<E> {

std::string soname;
std::vector<std::string_view> version_strings;
std::vector<std::string_view> dt_needed;
std::vector<ElfSym<E>> elf_syms2;

private:
Expand Down
37 changes: 16 additions & 21 deletions elf/passes.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1094,22 +1094,6 @@ template <typename E>
void check_shlib_undefined(Context<E> &ctx) {
Timer t(ctx, "check_shlib_undefined");

// Obtain a list of DSOs whose all DT_NEEDED files are known to us.
// If a DSO depends on an unknown library, that library may provide
// definitions for missing symbols, so we ignore such libraries.
std::vector<SharedFile<E> *> dsos = ctx.dsos;
std::unordered_set<std::string_view> sonames;

for (SharedFile<E> *file : dsos)
sonames.insert(file->soname);

std::erase_if(dsos, [&](SharedFile<E> *file) {
for (std::string_view needed : file->dt_needed)
if (sonames.count(needed) == 0)
return true;
return false;
});

auto is_sparc_register = [](const ElfSym<E> &esym) {
// Dynamic symbol table for SPARC contains bogus entries which
// we need to ignore
Expand All @@ -1118,17 +1102,28 @@ void check_shlib_undefined(Context<E> &ctx) {
return false;
};

// Check if all undefined symbols have been resolved.
for (SharedFile<E> *file : dsos) {
// Obtain a list of known shared library names.
std::unordered_set<std::string_view> sonames;
for (SharedFile<E> *file : ctx.dsos)
sonames.insert(file->soname);

tbb::parallel_for_each(ctx.dsos, [&](SharedFile<E> *file) {
// Skip the file if it depends on a file that we know nothing about.
// This is because missing symbols may be provided by that unknown file.
for (std::string_view needed : file->get_dt_needed(ctx))
if (sonames.count(needed) == 0)
return;

// Check if all undefined symbols have been resolved.
for (i64 i = 0; i < file->elf_syms.size(); i++) {
const ElfSym<E> &esym = file->elf_syms[i];
Symbol<E> &sym = *file->symbols[i];
if (esym.is_undef() && !esym.is_weak() && !is_sparc_register(esym) &&
!sym.file)
if (esym.is_undef() && !esym.is_weak() && !sym.file &&
!is_sparc_register(esym))
Error(ctx) << *file << ": --no-allow-shlib-undefined: undefined symbol: "
<< sym;
}
}
});
}

template <typename E>
Expand Down

0 comments on commit b919548

Please sign in to comment.