Browse Source

LibELF: Move DynamicObject::lookup_symbol() to DynamicLoader

Also simplify it by removing an unreachable code path.
Andreas Kling 4 years ago
parent
commit
f23b29f605

+ 9 - 2
Userland/Libraries/LibELF/DynamicLoader.cpp

@@ -1,6 +1,7 @@
 /*
 /*
  * Copyright (c) 2019-2020, Andrew Kaster <andrewdkaster@gmail.com>
  * Copyright (c) 2019-2020, Andrew Kaster <andrewdkaster@gmail.com>
  * Copyright (c) 2020, Itamar S. <itamar8910@gmail.com>
  * Copyright (c) 2020, Itamar S. <itamar8910@gmail.com>
+ * Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
  * All rights reserved.
  * All rights reserved.
  *
  *
  * Redistribution and use in source and binary forms, with or without
  * Redistribution and use in source and binary forms, with or without
@@ -28,6 +29,7 @@
 #include <AK/Debug.h>
 #include <AK/Debug.h>
 #include <AK/Optional.h>
 #include <AK/Optional.h>
 #include <AK/StringBuilder.h>
 #include <AK/StringBuilder.h>
+#include <LibELF/DynamicLinker.h>
 #include <LibELF/DynamicLoader.h>
 #include <LibELF/DynamicLoader.h>
 #include <LibELF/Validation.h>
 #include <LibELF/Validation.h>
 #include <assert.h>
 #include <assert.h>
@@ -570,9 +572,14 @@ void DynamicLoader::call_object_init_functions()
     }
     }
 }
 }
 
 
-Optional<DynamicObject::SymbolLookupResult> DynamicLoader::lookup_symbol(const ELF::DynamicObject::Symbol& symbol) const
+Optional<DynamicObject::SymbolLookupResult> DynamicLoader::lookup_symbol(const ELF::DynamicObject::Symbol& symbol)
 {
 {
-    return m_dynamic_object->lookup_symbol(symbol);
+    dbgln_if(DYNAMIC_LOAD_DEBUG, "looking up symbol: {}", symbol.name());
+    if (symbol.is_undefined() || symbol.bind() == STB_WEAK)
+        return DynamicLinker::lookup_global_symbol(symbol.name());
+
+    dbgln_if(DYNAMIC_LOAD_DEBUG, "symbol is defined in its object");
+    return DynamicObject::SymbolLookupResult { symbol.value(), symbol.address(), symbol.bind(), &symbol.object() };
 }
 }
 
 
 } // end namespace ELF
 } // end namespace ELF

+ 2 - 2
Userland/Libraries/LibELF/DynamicLoader.h

@@ -71,6 +71,8 @@ public:
     VirtualAddress text_segment_load_address() const { return m_text_segment_load_address; }
     VirtualAddress text_segment_load_address() const { return m_text_segment_load_address; }
     bool is_dynamic() const { return m_elf_image.is_dynamic(); }
     bool is_dynamic() const { return m_elf_image.is_dynamic(); }
 
 
+    static Optional<DynamicObject::SymbolLookupResult> lookup_symbol(const ELF::DynamicObject::Symbol&);
+
 private:
 private:
     DynamicLoader(int fd, String filename, void* file_data, size_t file_size);
     DynamicLoader(int fd, String filename, void* file_data, size_t file_size);
 
 
@@ -121,8 +123,6 @@ private:
     RelocationResult do_relocation(size_t total_tls_size, const DynamicObject::Relocation&);
     RelocationResult do_relocation(size_t total_tls_size, const DynamicObject::Relocation&);
     size_t calculate_tls_size() const;
     size_t calculate_tls_size() const;
 
 
-    Optional<DynamicObject::SymbolLookupResult> lookup_symbol(const ELF::DynamicObject::Symbol&) const;
-
     String m_filename;
     String m_filename;
     String m_program_interpreter;
     String m_program_interpreter;
     size_t m_file_size { 0 };
     size_t m_file_size { 0 };

+ 3 - 16
Userland/Libraries/LibELF/DynamicObject.cpp

@@ -28,7 +28,7 @@
 #include <AK/Debug.h>
 #include <AK/Debug.h>
 #include <AK/String.h>
 #include <AK/String.h>
 #include <AK/StringBuilder.h>
 #include <AK/StringBuilder.h>
-#include <LibELF/DynamicLinker.h>
+#include <LibELF/DynamicLoader.h>
 #include <LibELF/DynamicObject.h>
 #include <LibELF/DynamicObject.h>
 #include <LibELF/exec_elf.h>
 #include <LibELF/exec_elf.h>
 #include <string.h>
 #include <string.h>
@@ -458,7 +458,7 @@ static const char* name_for_dtag(Elf32_Sword d_tag)
     }
     }
 }
 }
 
 
-Optional<DynamicObject::SymbolLookupResult> DynamicObject::lookup_symbol(const StringView& name) const
+auto DynamicObject::lookup_symbol(const StringView& name) const -> Optional<SymbolLookupResult>
 {
 {
     auto result = hash_section().lookup_symbol(name);
     auto result = hash_section().lookup_symbol(name);
     if (!result.has_value())
     if (!result.has_value())
@@ -482,7 +482,7 @@ VirtualAddress DynamicObject::patch_plt_entry(u32 relocation_offset)
     auto symbol = relocation.symbol();
     auto symbol = relocation.symbol();
     u8* relocation_address = relocation.address().as_ptr();
     u8* relocation_address = relocation.address().as_ptr();
 
 
-    auto result = lookup_symbol(symbol);
+    auto result = DynamicLoader::lookup_symbol(symbol);
     if (!result.has_value()) {
     if (!result.has_value()) {
         dbgln("did not find symbol: {}", symbol.name());
         dbgln("did not find symbol: {}", symbol.name());
         ASSERT_NOT_REACHED();
         ASSERT_NOT_REACHED();
@@ -496,17 +496,4 @@ VirtualAddress DynamicObject::patch_plt_entry(u32 relocation_offset)
     return symbol_location;
     return symbol_location;
 }
 }
 
 
-Optional<DynamicObject::SymbolLookupResult> DynamicObject::lookup_symbol(const ELF::DynamicObject::Symbol& symbol) const
-{
-    dbgln_if(DYNAMIC_LOAD_DEBUG, "looking up symbol: {}", symbol.name());
-    if (symbol.is_undefined() || symbol.bind() == STB_WEAK)
-        return DynamicLinker::lookup_global_symbol(symbol.name());
-
-    if (!symbol.is_undefined()) {
-        dbgln_if(DYNAMIC_LOAD_DEBUG, "symbol is defined in its object");
-        return SymbolLookupResult { symbol.value(), symbol.address(), symbol.bind(), &symbol.object() };
-    }
-    return DynamicLinker::lookup_global_symbol(symbol.name());
-}
-
 } // end namespace ELF
 } // end namespace ELF

+ 0 - 2
Userland/Libraries/LibELF/DynamicObject.h

@@ -264,8 +264,6 @@ public:
     // Will be called from _fixup_plt_entry, as part of the PLT trampoline
     // Will be called from _fixup_plt_entry, as part of the PLT trampoline
     VirtualAddress patch_plt_entry(u32 relocation_offset);
     VirtualAddress patch_plt_entry(u32 relocation_offset);
 
 
-    Optional<SymbolLookupResult> lookup_symbol(const ELF::DynamicObject::Symbol&) const;
-
     bool elf_is_dynamic() const { return m_is_elf_dynamic; }
     bool elf_is_dynamic() const { return m_is_elf_dynamic; }
 
 
 private:
 private: