Skip to content

Commit f23b29f

Browse files
committed
LibELF: Move DynamicObject::lookup_symbol() to DynamicLoader
Also simplify it by removing an unreachable code path.
1 parent a43910a commit f23b29f

File tree

4 files changed

+14
-22
lines changed

4 files changed

+14
-22
lines changed

Userland/Libraries/LibELF/DynamicLoader.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/*
22
* Copyright (c) 2019-2020, Andrew Kaster <andrewdkaster@gmail.com>
33
* Copyright (c) 2020, Itamar S. <itamar8910@gmail.com>
4+
* Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
45
* All rights reserved.
56
*
67
* Redistribution and use in source and binary forms, with or without
@@ -28,6 +29,7 @@
2829
#include <AK/Debug.h>
2930
#include <AK/Optional.h>
3031
#include <AK/StringBuilder.h>
32+
#include <LibELF/DynamicLinker.h>
3133
#include <LibELF/DynamicLoader.h>
3234
#include <LibELF/Validation.h>
3335
#include <assert.h>
@@ -570,9 +572,14 @@ void DynamicLoader::call_object_init_functions()
570572
}
571573
}
572574

573-
Optional<DynamicObject::SymbolLookupResult> DynamicLoader::lookup_symbol(const ELF::DynamicObject::Symbol& symbol) const
575+
Optional<DynamicObject::SymbolLookupResult> DynamicLoader::lookup_symbol(const ELF::DynamicObject::Symbol& symbol)
574576
{
575-
return m_dynamic_object->lookup_symbol(symbol);
577+
dbgln_if(DYNAMIC_LOAD_DEBUG, "looking up symbol: {}", symbol.name());
578+
if (symbol.is_undefined() || symbol.bind() == STB_WEAK)
579+
return DynamicLinker::lookup_global_symbol(symbol.name());
580+
581+
dbgln_if(DYNAMIC_LOAD_DEBUG, "symbol is defined in its object");
582+
return DynamicObject::SymbolLookupResult { symbol.value(), symbol.address(), symbol.bind(), &symbol.object() };
576583
}
577584

578585
} // end namespace ELF

Userland/Libraries/LibELF/DynamicLoader.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ class DynamicLoader : public RefCounted<DynamicLoader> {
7171
VirtualAddress text_segment_load_address() const { return m_text_segment_load_address; }
7272
bool is_dynamic() const { return m_elf_image.is_dynamic(); }
7373

74+
static Optional<DynamicObject::SymbolLookupResult> lookup_symbol(const ELF::DynamicObject::Symbol&);
75+
7476
private:
7577
DynamicLoader(int fd, String filename, void* file_data, size_t file_size);
7678

@@ -121,8 +123,6 @@ class DynamicLoader : public RefCounted<DynamicLoader> {
121123
RelocationResult do_relocation(size_t total_tls_size, const DynamicObject::Relocation&);
122124
size_t calculate_tls_size() const;
123125

124-
Optional<DynamicObject::SymbolLookupResult> lookup_symbol(const ELF::DynamicObject::Symbol&) const;
125-
126126
String m_filename;
127127
String m_program_interpreter;
128128
size_t m_file_size { 0 };

Userland/Libraries/LibELF/DynamicObject.cpp

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
#include <AK/Debug.h>
2929
#include <AK/String.h>
3030
#include <AK/StringBuilder.h>
31-
#include <LibELF/DynamicLinker.h>
31+
#include <LibELF/DynamicLoader.h>
3232
#include <LibELF/DynamicObject.h>
3333
#include <LibELF/exec_elf.h>
3434
#include <string.h>
@@ -458,7 +458,7 @@ static const char* name_for_dtag(Elf32_Sword d_tag)
458458
}
459459
}
460460

461-
Optional<DynamicObject::SymbolLookupResult> DynamicObject::lookup_symbol(const StringView& name) const
461+
auto DynamicObject::lookup_symbol(const StringView& name) const -> Optional<SymbolLookupResult>
462462
{
463463
auto result = hash_section().lookup_symbol(name);
464464
if (!result.has_value())
@@ -482,7 +482,7 @@ VirtualAddress DynamicObject::patch_plt_entry(u32 relocation_offset)
482482
auto symbol = relocation.symbol();
483483
u8* relocation_address = relocation.address().as_ptr();
484484

485-
auto result = lookup_symbol(symbol);
485+
auto result = DynamicLoader::lookup_symbol(symbol);
486486
if (!result.has_value()) {
487487
dbgln("did not find symbol: {}", symbol.name());
488488
ASSERT_NOT_REACHED();
@@ -496,17 +496,4 @@ VirtualAddress DynamicObject::patch_plt_entry(u32 relocation_offset)
496496
return symbol_location;
497497
}
498498

499-
Optional<DynamicObject::SymbolLookupResult> DynamicObject::lookup_symbol(const ELF::DynamicObject::Symbol& symbol) const
500-
{
501-
dbgln_if(DYNAMIC_LOAD_DEBUG, "looking up symbol: {}", symbol.name());
502-
if (symbol.is_undefined() || symbol.bind() == STB_WEAK)
503-
return DynamicLinker::lookup_global_symbol(symbol.name());
504-
505-
if (!symbol.is_undefined()) {
506-
dbgln_if(DYNAMIC_LOAD_DEBUG, "symbol is defined in its object");
507-
return SymbolLookupResult { symbol.value(), symbol.address(), symbol.bind(), &symbol.object() };
508-
}
509-
return DynamicLinker::lookup_global_symbol(symbol.name());
510-
}
511-
512499
} // end namespace ELF

Userland/Libraries/LibELF/DynamicObject.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -264,8 +264,6 @@ class DynamicObject : public RefCounted<DynamicObject> {
264264
// Will be called from _fixup_plt_entry, as part of the PLT trampoline
265265
VirtualAddress patch_plt_entry(u32 relocation_offset);
266266

267-
Optional<SymbolLookupResult> lookup_symbol(const ELF::DynamicObject::Symbol&) const;
268-
269267
bool elf_is_dynamic() const { return m_is_elf_dynamic; }
270268

271269
private:

0 commit comments

Comments
 (0)