ソースを参照

LibELF: Fix various clang-tidy warnings

Remove a bunch of unused code, unnecessary const, and make some
non-object-specific member functions static.
Andreas Kling 4 年 前
コミット
01f1e480e5

+ 0 - 1
Userland/Libraries/LibCoreDump/Reader.cpp

@@ -29,7 +29,6 @@
 #include <LibCoreDump/Reader.h>
 #include <signal_numbers.h>
 #include <string.h>
-#include <sys/stat.h>
 
 namespace CoreDump {
 

+ 0 - 4
Userland/Libraries/LibELF/DynamicLinker.cpp

@@ -33,17 +33,13 @@
 #include <AK/LogStream.h>
 #include <AK/ScopeGuard.h>
 #include <LibC/mman.h>
-#include <LibC/stdio.h>
-#include <LibC/sys/internals.h>
 #include <LibC/unistd.h>
 #include <LibELF/AuxiliaryVector.h>
 #include <LibELF/DynamicLinker.h>
 #include <LibELF/DynamicLoader.h>
 #include <LibELF/DynamicObject.h>
-#include <LibELF/Image.h>
 #include <dlfcn.h>
 #include <fcntl.h>
-#include <string.h>
 #include <sys/types.h>
 #include <syscall.h>
 

+ 4 - 6
Userland/Libraries/LibELF/DynamicLoader.cpp

@@ -60,7 +60,7 @@ RefPtr<DynamicLoader> DynamicLoader::try_create(int fd, String filename)
     }
 
     ASSERT(stat.st_size >= 0);
-    size_t size = static_cast<size_t>(stat.st_size);
+    auto size = static_cast<size_t>(stat.st_size);
     if (size < sizeof(Elf32_Ehdr))
         return {};
 
@@ -203,12 +203,11 @@ bool DynamicLoader::load_stage_2(unsigned flags, size_t total_tls_size)
 
 void DynamicLoader::do_main_relocations(size_t total_tls_size)
 {
-    auto do_single_relocation = [&](ELF::DynamicObject::Relocation relocation) {
+    auto do_single_relocation = [&](const ELF::DynamicObject::Relocation& relocation) {
         switch (do_relocation(total_tls_size, relocation)) {
         case RelocationResult::Failed:
             dbgln("Loader.so: {} unresolved symbol '{}'", m_filename, relocation.symbol().name());
             ASSERT_NOT_REACHED();
-            break;
         case RelocationResult::ResolveLater:
             m_unresolved_relocations.append(relocation);
             break;
@@ -394,7 +393,7 @@ void DynamicLoader::load_program_headers()
     // FIXME: Initialize the values in the TLS section. Currently, it is zeroed.
 }
 
-DynamicLoader::RelocationResult DynamicLoader::do_relocation(size_t total_tls_size, ELF::DynamicObject::Relocation relocation)
+DynamicLoader::RelocationResult DynamicLoader::do_relocation(size_t total_tls_size, const ELF::DynamicObject::Relocation& relocation)
 {
     dbgln_if(DYNAMIC_LOAD_DEBUG, "Relocation symbol: {}, type: {}", relocation.symbol().name(), relocation.type());
     FlatPtr* patch_ptr = nullptr;
@@ -516,7 +515,6 @@ DynamicLoader::RelocationResult DynamicLoader::do_relocation(size_t total_tls_si
         dbgln("Found a new exciting relocation type {}", relocation.type());
         // printf("DynamicLoader: Found unknown relocation type %d\n", relocation.type());
         ASSERT_NOT_REACHED();
-        break;
     }
     return RelocationResult::Success;
 }
@@ -530,7 +528,7 @@ void DynamicLoader::setup_plt_trampoline()
     ASSERT(m_dynamic_object->has_plt());
     VirtualAddress got_address = m_dynamic_object->plt_got_base_address();
 
-    FlatPtr* got_ptr = (FlatPtr*)got_address.as_ptr();
+    auto* got_ptr = (FlatPtr*)got_address.as_ptr();
     got_ptr[1] = (FlatPtr)m_dynamic_object.ptr();
     got_ptr[2] = (FlatPtr)&_plt_trampoline;
 

+ 1 - 11
Userland/Libraries/LibELF/DynamicLoader.h

@@ -38,8 +38,6 @@
 
 namespace ELF {
 
-#define ALIGN_ROUND_UP(x, align) ((((size_t)(x)) + align - 1) & (~(align - 1)))
-
 class DynamicLoader : public RefCounted<DynamicLoader> {
 public:
     static RefPtr<DynamicLoader> try_create(int fd, String filename);
@@ -62,13 +60,6 @@ public:
     // Intended for use by dlsym or other internal methods
     void* symbol_for_name(const StringView&);
 
-    void dump();
-
-    // Requested program interpreter from program headers. May be empty string
-    StringView program_interpreter() const { return m_program_interpreter; }
-
-    VirtualAddress text_segment_load_addresss() const { return m_text_segment_load_address; }
-
     void set_tls_offset(size_t offset) { m_tls_offset = offset; };
     size_t tls_size() const { return m_tls_size; }
     size_t tls_offset() const { return m_tls_offset; }
@@ -127,7 +118,7 @@ private:
         Success = 1,
         ResolveLater = 2,
     };
-    RelocationResult do_relocation(size_t total_tls_size, DynamicObject::Relocation relocation);
+    RelocationResult do_relocation(size_t total_tls_size, const DynamicObject::Relocation&);
     size_t calculate_tls_size() const;
 
     Optional<DynamicObject::SymbolLookupResult> lookup_symbol(const ELF::DynamicObject::Symbol&) const;
@@ -148,7 +139,6 @@ private:
     VirtualAddress m_relro_segment_address;
     size_t m_relro_segment_size { 0 };
 
-    VirtualAddress m_tls_segment_address;
     VirtualAddress m_dynamic_section_address;
 
     size_t m_tls_offset { 0 };

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

@@ -41,8 +41,8 @@ DynamicObject::DynamicObject(VirtualAddress base_address, VirtualAddress dynamic
     : m_base_address(base_address)
     , m_dynamic_address(dynamic_section_addresss)
 {
-    Elf32_Ehdr* header = (Elf32_Ehdr*)base_address.as_ptr();
-    Elf32_Phdr* pheader = (Elf32_Phdr*)(base_address.as_ptr() + header->e_phoff);
+    auto* header = (Elf32_Ehdr*)base_address.as_ptr();
+    auto* pheader = (Elf32_Phdr*)(base_address.as_ptr() + header->e_phoff);
     m_elf_base_address = VirtualAddress(pheader->p_vaddr - pheader->p_offset);
     if (header->e_type == ET_DYN)
         m_is_elf_dynamic = true;
@@ -190,7 +190,7 @@ void DynamicObject::parse()
     m_symbol_count = num_hash_chains;
 }
 
-const DynamicObject::Relocation DynamicObject::RelocationSection::relocation(unsigned index) const
+DynamicObject::Relocation DynamicObject::RelocationSection::relocation(unsigned index) const
 {
     ASSERT(index < entry_count());
     unsigned offset_in_section = index * entry_size();
@@ -198,57 +198,57 @@ const DynamicObject::Relocation DynamicObject::RelocationSection::relocation(uns
     return Relocation(m_dynamic, *relocation_address, offset_in_section);
 }
 
-const DynamicObject::Relocation DynamicObject::RelocationSection::relocation_at_offset(unsigned offset) const
+DynamicObject::Relocation DynamicObject::RelocationSection::relocation_at_offset(unsigned offset) const
 {
     ASSERT(offset <= (m_section_size_bytes - m_entry_size));
     auto relocation_address = (Elf32_Rel*)address().offset(offset).as_ptr();
     return Relocation(m_dynamic, *relocation_address, offset);
 }
 
-const DynamicObject::Symbol DynamicObject::symbol(unsigned index) const
+DynamicObject::Symbol DynamicObject::symbol(unsigned index) const
 {
     auto symbol_section = Section(*this, m_symbol_table_offset, (m_symbol_count * m_size_of_symbol_table_entry), m_size_of_symbol_table_entry, "DT_SYMTAB");
     auto symbol_entry = (Elf32_Sym*)symbol_section.address().offset(index * symbol_section.entry_size()).as_ptr();
     return Symbol(*this, index, *symbol_entry);
 }
 
-const DynamicObject::Section DynamicObject::init_section() const
+DynamicObject::Section DynamicObject::init_section() const
 {
     return Section(*this, m_init_offset, sizeof(void (*)()), sizeof(void (*)()), "DT_INIT");
 }
 
-const DynamicObject::Section DynamicObject::fini_section() const
+DynamicObject::Section DynamicObject::fini_section() const
 {
     return Section(*this, m_fini_offset, sizeof(void (*)()), sizeof(void (*)()), "DT_FINI");
 }
 
-const DynamicObject::Section DynamicObject::init_array_section() const
+DynamicObject::Section DynamicObject::init_array_section() const
 {
     return Section(*this, m_init_array_offset, m_init_array_size, sizeof(void (*)()), "DT_INIT_ARRAY");
 }
 
-const DynamicObject::Section DynamicObject::fini_array_section() const
+DynamicObject::Section DynamicObject::fini_array_section() const
 {
     return Section(*this, m_fini_array_offset, m_fini_array_size, sizeof(void (*)()), "DT_FINI_ARRAY");
 }
 
-const DynamicObject::HashSection DynamicObject::hash_section() const
+DynamicObject::HashSection DynamicObject::hash_section() const
 {
     const char* section_name = m_hash_type == HashType::SYSV ? "DT_HASH" : "DT_GNU_HASH";
     return HashSection(Section(*this, m_hash_table_offset, 0, 0, section_name), m_hash_type);
 }
 
-const DynamicObject::RelocationSection DynamicObject::relocation_section() const
+DynamicObject::RelocationSection DynamicObject::relocation_section() const
 {
     return RelocationSection(Section(*this, m_relocation_table_offset, m_size_of_relocation_table, m_size_of_relocation_entry, "DT_REL"));
 }
 
-const DynamicObject::RelocationSection DynamicObject::plt_relocation_section() const
+DynamicObject::RelocationSection DynamicObject::plt_relocation_section() const
 {
     return RelocationSection(Section(*this, m_plt_relocation_offset_location, m_size_of_plt_relocation_entry_list, m_size_of_relocation_entry, "DT_JMPREL"));
 }
 
-u32 DynamicObject::HashSection::calculate_elf_hash(const StringView& name) const
+u32 DynamicObject::HashSection::calculate_elf_hash(const StringView& name)
 {
     // SYSV ELF hash algorithm
     // Note that the GNU HASH algorithm has less collisions
@@ -267,7 +267,7 @@ u32 DynamicObject::HashSection::calculate_elf_hash(const StringView& name) const
     return hash;
 }
 
-u32 DynamicObject::HashSection::calculate_gnu_hash(const StringView& name) const
+u32 DynamicObject::HashSection::calculate_gnu_hash(const StringView& name)
 {
     // GNU ELF hash algorithm
     u32 hash = 5381;
@@ -283,7 +283,7 @@ auto DynamicObject::HashSection::lookup_symbol(const StringView& name) const ->
     return (this->*(m_lookup_function))(name);
 }
 
-const DynamicObject::Symbol DynamicObject::HashSection::lookup_elf_symbol(const StringView& name) const
+DynamicObject::Symbol DynamicObject::HashSection::lookup_elf_symbol(const StringView& name) const
 {
     u32 hash_value = calculate_elf_hash(name);
 
@@ -310,7 +310,7 @@ const DynamicObject::Symbol DynamicObject::HashSection::lookup_elf_symbol(const
     return Symbol::create_undefined(m_dynamic);
 }
 
-const DynamicObject::Symbol DynamicObject::HashSection::lookup_gnu_symbol(const StringView& name) const
+DynamicObject::Symbol DynamicObject::HashSection::lookup_gnu_symbol(const StringView& name) const
 {
     // Algorithm reference: https://ent-voy.blogspot.com/2011/02/
     // TODO: Handle 64bit bloomwords for ELF_CLASS64

+ 18 - 19
Userland/Libraries/LibELF/DynamicObject.h

@@ -50,7 +50,7 @@ public:
 
     class DynamicEntry {
     public:
-        DynamicEntry(const Elf32_Dyn& dyn)
+        explicit DynamicEntry(const Elf32_Dyn& dyn)
             : m_dyn(dyn)
         {
         }
@@ -157,13 +157,13 @@ public:
 
     class RelocationSection : public Section {
     public:
-        RelocationSection(const Section& section)
+        explicit RelocationSection(const Section& section)
             : Section(section.m_dynamic, section.m_section_offset, section.m_section_size_bytes, section.m_entry_size, section.m_name)
         {
         }
         unsigned relocation_count() const { return entry_count(); }
-        const Relocation relocation(unsigned index) const;
-        const Relocation relocation_at_offset(unsigned offset) const;
+        Relocation relocation(unsigned index) const;
+        Relocation relocation_at_offset(unsigned offset) const;
         template<typename F>
         void for_each_relocation(F) const;
     };
@@ -183,7 +183,7 @@ public:
         unsigned offset() const { return m_rel.r_offset; }
         unsigned type() const { return ELF32_R_TYPE(m_rel.r_info); }
         unsigned symbol_index() const { return ELF32_R_SYM(m_rel.r_info); }
-        const Symbol symbol() const { return m_dynamic.symbol(symbol_index()); }
+        Symbol symbol() const { return m_dynamic.symbol(symbol_index()); }
         VirtualAddress address() const
         {
             if (m_dynamic.elf_is_dynamic())
@@ -216,41 +216,40 @@ public:
                 break;
             default:
                 ASSERT_NOT_REACHED();
-                break;
             }
         }
 
         Symbol lookup_symbol(const StringView& name) const;
 
     private:
-        u32 calculate_elf_hash(const StringView& name) const;
-        u32 calculate_gnu_hash(const StringView& name) const;
+        static u32 calculate_elf_hash(const StringView& name);
+        static u32 calculate_gnu_hash(const StringView& name);
 
-        const DynamicObject::Symbol lookup_elf_symbol(const StringView& name) const;
-        const DynamicObject::Symbol lookup_gnu_symbol(const StringView& name) const;
+        DynamicObject::Symbol lookup_elf_symbol(const StringView& name) const;
+        DynamicObject::Symbol lookup_gnu_symbol(const StringView& name) const;
 
-        typedef const DynamicObject::Symbol (HashSection::*LookupFunction)(const StringView&) const;
+        typedef DynamicObject::Symbol (HashSection::*LookupFunction)(const StringView&) const;
         LookupFunction m_lookup_function {};
     };
 
     unsigned symbol_count() const { return m_symbol_count; }
 
-    const Symbol symbol(unsigned) const;
+    Symbol symbol(unsigned) const;
 
     typedef void (*InitializationFunction)();
 
     bool has_init_section() const { return m_init_offset != 0; }
     bool has_init_array_section() const { return m_init_array_offset != 0; }
-    const Section init_section() const;
+    Section init_section() const;
     InitializationFunction init_section_function() const;
-    const Section fini_section() const;
-    const Section init_array_section() const;
-    const Section fini_array_section() const;
+    Section fini_section() const;
+    Section init_array_section() const;
+    Section fini_array_section() const;
 
-    const HashSection hash_section() const;
+    HashSection hash_section() const;
 
-    const RelocationSection relocation_section() const;
-    const RelocationSection plt_relocation_section() const;
+    RelocationSection relocation_section() const;
+    RelocationSection plt_relocation_section() const;
 
     bool should_process_origin() const { return m_dt_flags & DF_ORIGIN; }
     bool requires_symbolic_symbol_resolution() const { return m_dt_flags & DF_SYMBOLIC; }

+ 11 - 17
Userland/Libraries/LibELF/Image.cpp

@@ -188,7 +188,7 @@ bool Image::parse()
 
     // Then create a name-to-index map.
     for (unsigned i = 0; i < section_count(); ++i) {
-        auto& section = this->section(i);
+        auto section = this->section(i);
         m_sections.set(section.name(), move(i));
     }
 
@@ -250,7 +250,7 @@ const Elf32_Shdr& Image::section_header(unsigned index) const
     return *reinterpret_cast<const Elf32_Shdr*>(raw_data(header().e_shoff + (index * header().e_shentsize)));
 }
 
-const Image::Symbol Image::symbol(unsigned index) const
+Image::Symbol Image::symbol(unsigned index) const
 {
     ASSERT(m_valid);
     ASSERT(index < symbol_count());
@@ -258,33 +258,28 @@ const Image::Symbol Image::symbol(unsigned index) const
     return Symbol(*this, index, raw_syms[index]);
 }
 
-const Image::Section Image::section(unsigned index) const
+Image::Section Image::section(unsigned index) const
 {
     ASSERT(m_valid);
     ASSERT(index < section_count());
     return Section(*this, index);
 }
 
-const Image::ProgramHeader Image::program_header(unsigned index) const
+Image::ProgramHeader Image::program_header(unsigned index) const
 {
     ASSERT(m_valid);
     ASSERT(index < program_header_count());
     return ProgramHeader(*this, index);
 }
 
-FlatPtr Image::program_header_table_offset() const
-{
-    return header().e_phoff;
-}
-
-const Image::Relocation Image::RelocationSection::relocation(unsigned index) const
+Image::Relocation Image::RelocationSection::relocation(unsigned index) const
 {
     ASSERT(index < relocation_count());
     auto* rels = reinterpret_cast<const Elf32_Rel*>(m_image.raw_data(offset()));
     return Relocation(m_image, rels[index]);
 }
 
-const Image::RelocationSection Image::Section::relocations() const
+Image::RelocationSection Image::Section::relocations() const
 {
     StringBuilder builder;
     builder.append(".rel");
@@ -300,7 +295,7 @@ const Image::RelocationSection Image::Section::relocations() const
     return static_cast<const RelocationSection>(relocation_section);
 }
 
-const Image::Section Image::lookup_section(const String& name) const
+Image::Section Image::lookup_section(const String& name) const
 {
     ASSERT(m_valid);
     if (auto it = m_sections.find(name); it != m_sections.end())
@@ -310,14 +305,14 @@ const Image::Section Image::lookup_section(const String& name) const
 
 StringView Image::Symbol::raw_data() const
 {
-    auto& section = this->section();
+    auto section = this->section();
     return { section.raw_data() + (value() - section.address()), size() };
 }
 
 Optional<Image::Symbol> Image::find_demangled_function(const String& name) const
 {
     Optional<Image::Symbol> found;
-    for_each_symbol([&](const Image::Symbol symbol) {
+    for_each_symbol([&](const Image::Symbol& symbol) {
         if (symbol.type() != STT_FUNC)
             return IterationDecision::Continue;
         if (symbol.is_undefined())
@@ -344,7 +339,7 @@ Optional<Image::Symbol> Image::find_symbol(u32 address, u32* out_offset) const
     SortedSymbol* sorted_symbols = nullptr;
     if (m_sorted_symbols.is_empty()) {
         m_sorted_symbols.ensure_capacity(symbol_count);
-        for_each_symbol([this](auto& symbol) {
+        for_each_symbol([this](const auto& symbol) {
             m_sorted_symbols.append({ symbol.value(), symbol.name(), {}, symbol });
             return IterationDecision::Continue;
         });
@@ -376,10 +371,9 @@ String Image::symbolicate(u32 address, u32* out_offset) const
         return "??";
     }
     SortedSymbol* sorted_symbols = nullptr;
-
     if (m_sorted_symbols.is_empty()) {
         m_sorted_symbols.ensure_capacity(symbol_count);
-        for_each_symbol([this](auto& symbol) {
+        for_each_symbol([this](const auto& symbol) {
             m_sorted_symbols.append({ symbol.value(), symbol.name(), {}, symbol });
             return IterationDecision::Continue;
         });

+ 10 - 10
Userland/Libraries/LibELF/Image.h

@@ -77,7 +77,7 @@ public:
         unsigned index() const { return m_index; }
         unsigned type() const { return ELF32_ST_TYPE(m_sym.st_info); }
         unsigned bind() const { return ELF32_ST_BIND(m_sym.st_info); }
-        const Section section() const { return m_image.section(section_index()); }
+        Section section() const { return m_image.section(section_index()); }
         bool is_undefined() const { return section_index() == 0; }
         StringView raw_data() const;
 
@@ -137,7 +137,7 @@ public:
         const char* raw_data() const { return m_image.raw_data(m_section_header.sh_offset); }
         ReadonlyBytes bytes() const { return { raw_data(), size() }; }
         bool is_undefined() const { return m_section_index == SHN_UNDEF; }
-        const RelocationSection relocations() const;
+        RelocationSection relocations() const;
         u32 flags() const { return m_section_header.sh_flags; }
         bool is_writable() const { return flags() & SHF_WRITE; }
         bool is_executable() const { return flags() & PF_X; }
@@ -151,12 +151,12 @@ public:
 
     class RelocationSection : public Section {
     public:
-        RelocationSection(const Section& section)
+        explicit RelocationSection(const Section& section)
             : Section(section.m_image, section.m_section_index)
         {
         }
         unsigned relocation_count() const { return entry_count(); }
-        const Relocation relocation(unsigned index) const;
+        Relocation relocation(unsigned index) const;
         template<typename F>
         void for_each_relocation(F) const;
     };
@@ -174,7 +174,7 @@ public:
         unsigned offset() const { return m_rel.r_offset; }
         unsigned type() const { return ELF32_R_TYPE(m_rel.r_info); }
         unsigned symbol_index() const { return ELF32_R_SYM(m_rel.r_info); }
-        const Symbol symbol() const { return m_image.symbol(symbol_index()); }
+        Symbol symbol() const { return m_image.symbol(symbol_index()); }
 
     private:
         const Image& m_image;
@@ -185,9 +185,9 @@ public:
     unsigned section_count() const;
     unsigned program_header_count() const;
 
-    const Symbol symbol(unsigned) const;
-    const Section section(unsigned) const;
-    const ProgramHeader program_header(unsigned const) const;
+    Symbol symbol(unsigned) const;
+    Section section(unsigned) const;
+    ProgramHeader program_header(unsigned) const;
     FlatPtr program_header_table_offset() const;
 
     template<typename F>
@@ -201,7 +201,7 @@ public:
 
     // NOTE: Returns section(0) if section with name is not found.
     // FIXME: I don't love this API.
-    const Section lookup_section(const String& name) const;
+    Section lookup_section(const String& name) const;
 
     bool is_executable() const { return header().e_type == ET_EXEC; }
     bool is_relocatable() const { return header().e_type == ET_REL; }
@@ -258,7 +258,7 @@ inline void Image::for_each_section_of_type(unsigned type, F func) const
 {
     auto section_count = this->section_count();
     for (unsigned i = 0; i < section_count; ++i) {
-        auto& section = this->section(i);
+        auto section = this->section(i);
         if (section.type() == type) {
             if (func(section) == IterationDecision::Break)
                 break;