Skip to content

Commit

Permalink
LibC+LibELF: Implement support for the dl_iterate_phdr helper
Browse files Browse the repository at this point in the history
This helper is used by libgcc_s to figure out where the .eh_frame sections
are located for all loaded shared objects.
  • Loading branch information
gunnarbeutner authored and awesomekling committed Apr 18, 2021
1 parent cf13fa5 commit 6cb28ec
Show file tree
Hide file tree
Showing 20 changed files with 171 additions and 25 deletions.
2 changes: 1 addition & 1 deletion Kernel/CoreDump.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@
#include <Kernel/RTC.h>
#include <Kernel/SpinLock.h>
#include <Kernel/VM/ProcessPagingScope.h>
#include <LibC/elf.h>
#include <LibELF/CoreDump.h>
#include <LibELF/exec_elf.h>

namespace Kernel {

Expand Down
1 change: 0 additions & 1 deletion Kernel/CoreDump.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
#include <AK/NonnullRefPtr.h>
#include <AK/OwnPtr.h>
#include <Kernel/Forward.h>
#include <LibELF/exec_elf.h>

namespace Kernel {

Expand Down
2 changes: 1 addition & 1 deletion Kernel/Process.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@
#include <Kernel/VM/AllocationStrategy.h>
#include <Kernel/VM/RangeAllocator.h>
#include <Kernel/VM/Space.h>
#include <LibC/elf.h>
#include <LibC/signal_numbers.h>
#include <LibELF/exec_elf.h>

namespace Kernel {

Expand Down
5 changes: 5 additions & 0 deletions Meta/check-debug-flags.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ cd "${script_path}/.."
MISSING_FLAGS=n

while IFS= read -r FLAG; do
# Ignore ELF_DEBUG because it's not a debug flag.
if [ "$FLAG" = "ELF_DEBUG" ]; then
continue
fi

# We simply search whether the CMakeLists.txt *ever* sets the flag.
# There are (basically) no false positives, but there might be false negatives,
# for example we intentionally don't check for commented-out lines here.
Expand Down
2 changes: 1 addition & 1 deletion Userland/DynamicLoader/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ static void perform_self_relocations(auxv_t* auxvp)
if (!dynamic_section_addr)
exit(1);

auto dynamic_object = ELF::DynamicObject::create((VirtualAddress(base_address)), (VirtualAddress(dynamic_section_addr)));
auto dynamic_object = ELF::DynamicObject::create({}, (VirtualAddress(base_address)), (VirtualAddress(dynamic_section_addr)));

dynamic_object->relocation_section().for_each_relocation([base_address](auto& reloc) {
if (reloc.type() != R_386_RELATIVE)
Expand Down
1 change: 1 addition & 0 deletions Userland/Libraries/LibC/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ set(LIBC_SOURCES
ioctl.cpp
libcinit.cpp
libgen.cpp
link.cpp
locale.cpp
malloc.cpp
mman.cpp
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,13 @@
* formerly known as "elf_abi.h".
*/

#ifndef _SYS_EXEC_ELF_H_
#define _SYS_EXEC_ELF_H_
#pragma once

#include <AK/Types.h>
#ifndef KERNEL
# include <sys/types.h>
#else
# include <AK/Types.h>
#endif

typedef uint8_t Elf_Byte;

Expand Down Expand Up @@ -788,5 +791,3 @@ struct elf_args {
#define R_386_RELATIVE 8 /* Base address + Addned */
#define R_386_TLS_TPOFF 14 /* Negative offset into the static TLS storage */
#define R_386_TLS_TPOFF32 37

#endif /* _SYS_EXEC_ELF_H_ */
41 changes: 41 additions & 0 deletions Userland/Libraries/LibC/link.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright (c) 2021, Gunnar Beutner <[email protected]>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include <assert.h>
#include <link.h>

extern "C" {

using DlIteratePhdrCallbackFunction = int (*)(struct dl_phdr_info*, size_t, void*);
using DlIteratePhdrFunction = int (*)(DlIteratePhdrCallbackFunction, void*);

DlIteratePhdrFunction __dl_iterate_phdr;

int dl_iterate_phdr(int (*callback)(struct dl_phdr_info* info, size_t size, void* data), void* data)
{
return __dl_iterate_phdr(callback, data);
}
}
49 changes: 49 additions & 0 deletions Userland/Libraries/LibC/link.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright (c) 2021, Gunnar Beutner <[email protected]>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#pragma once

#ifndef __serenity__
# include <LibC/elf.h>
#else
# include <elf.h>
#endif
#include <sys/cdefs.h>

__BEGIN_DECLS

#define ElfW(type) Elf32_##type

struct dl_phdr_info {
Elf32_Addr dlpi_addr;
const char* dlpi_name;
const Elf32_Phdr* dlpi_phdr;
Elf32_Half dlpi_phnum;
};

int dl_iterate_phdr(int (*callback)(struct dl_phdr_info* info, size_t size, void* data), void* data);

__END_DECLS
25 changes: 25 additions & 0 deletions Userland/Libraries/LibELF/DynamicLinker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include <AK/LexicalPath.h>
#include <AK/NonnullRefPtrVector.h>
#include <AK/ScopeGuard.h>
#include <LibC/link.h>
#include <LibC/mman.h>
#include <LibC/unistd.h>
#include <LibELF/AuxiliaryVector.h>
Expand All @@ -52,6 +53,8 @@ Vector<NonnullRefPtr<ELF::DynamicObject>> g_global_objects;

using EntryPointFunction = int (*)(int, char**, char**);
using LibCExitFunction = void (*)(int);
using DlIteratePhdrCallbackFunction = int (*)(struct dl_phdr_info*, size_t, void*);
using DlIteratePhdrFunction = int (*)(DlIteratePhdrCallbackFunction, void*);

size_t g_current_tls_offset = 0;
size_t g_total_tls_size = 0;
Expand Down Expand Up @@ -162,6 +165,24 @@ static void allocate_tls()
g_total_tls_size = total_tls_size;
}

static int __dl_iterate_phdr(DlIteratePhdrCallbackFunction callback, void* data)
{
for (auto& object : g_global_objects) {
auto info = dl_phdr_info {
.dlpi_addr = (ElfW(Addr))object->base_address().as_ptr(),
.dlpi_name = object->filename().characters(),
.dlpi_phdr = object->program_headers(),
.dlpi_phnum = object->program_header_count()
};

auto res = callback(&info, sizeof(info), data);
if (res != 0)
return res;
}

return 0;
}

static void initialize_libc(DynamicObject& libc)
{
// Traditionally, `_start` of the main program initializes libc.
Expand All @@ -181,6 +202,10 @@ static void initialize_libc(DynamicObject& libc)
VERIFY(res.has_value());
g_libc_exit = (LibCExitFunction)res.value().address.as_ptr();

res = libc.lookup_symbol("__dl_iterate_phdr"sv);
VERIFY(res.has_value());
*((DlIteratePhdrFunction*)res.value().address.as_ptr()) = __dl_iterate_phdr;

res = libc.lookup_symbol("__libc_init"sv);
VERIFY(res.has_value());
typedef void libc_init_func();
Expand Down
2 changes: 2 additions & 0 deletions Userland/Libraries/LibELF/DynamicLinker.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@
#pragma once

#include <AK/Result.h>
#include <AK/String.h>
#include <AK/Vector.h>
#include <LibC/link.h>
#include <LibELF/DynamicObject.h>

namespace ELF {
Expand Down
4 changes: 2 additions & 2 deletions Userland/Libraries/LibELF/DynamicLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ const DynamicObject& DynamicLoader::dynamic_object() const
});
VERIFY(!dynamic_section_address.is_null());

m_cached_dynamic_object = ELF::DynamicObject::create(VirtualAddress(m_elf_image.base_address()), dynamic_section_address);
m_cached_dynamic_object = ELF::DynamicObject::create(m_filename, VirtualAddress(m_elf_image.base_address()), dynamic_section_address);
}
return *m_cached_dynamic_object;
}
Expand Down Expand Up @@ -170,7 +170,7 @@ RefPtr<DynamicObject> DynamicLoader::map()

VERIFY(!m_base_address.is_null());

m_dynamic_object = DynamicObject::create(m_base_address, m_dynamic_section_address);
m_dynamic_object = DynamicObject::create(m_filename, m_base_address, m_dynamic_section_address);
m_dynamic_object->set_tls_offset(m_tls_offset);
m_dynamic_object->set_tls_size(m_tls_size);

Expand Down
2 changes: 1 addition & 1 deletion Userland/Libraries/LibELF/DynamicLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@
#include <AK/OwnPtr.h>
#include <AK/RefCounted.h>
#include <AK/String.h>
#include <LibC/elf.h>
#include <LibELF/DynamicObject.h>
#include <LibELF/Image.h>
#include <LibELF/exec_elf.h>
#include <sys/mman.h>

namespace ELF {
Expand Down
23 changes: 18 additions & 5 deletions Userland/Libraries/LibELF/DynamicObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,19 @@
#include <AK/Debug.h>
#include <AK/String.h>
#include <AK/StringBuilder.h>
#include <LibC/elf.h>
#include <LibELF/DynamicLoader.h>
#include <LibELF/DynamicObject.h>
#include <LibELF/Hashes.h>
#include <LibELF/exec_elf.h>
#include <string.h>

namespace ELF {

static const char* name_for_dtag(Elf32_Sword d_tag);

DynamicObject::DynamicObject(VirtualAddress base_address, VirtualAddress dynamic_section_address)
: m_base_address(base_address)
DynamicObject::DynamicObject(const String& filename, VirtualAddress base_address, VirtualAddress dynamic_section_address)
: m_filename(filename)
, m_base_address(base_address)
, m_dynamic_address(dynamic_section_address)
{
auto* header = (Elf32_Ehdr*)base_address.as_ptr();
Expand Down Expand Up @@ -255,6 +256,18 @@ 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"sv));
}

Elf32_Half DynamicObject::program_header_count() const
{
auto* header = (const Elf32_Ehdr*)m_base_address.as_ptr();
return header->e_phnum;
}

const Elf32_Phdr* DynamicObject::program_headers() const
{
auto* header = (const Elf32_Ehdr*)m_base_address.as_ptr();
return (const Elf32_Phdr*)(m_base_address.as_ptr() + header->e_phoff);
}

auto DynamicObject::HashSection::lookup_sysv_symbol(const StringView& name, u32 hash_value) const -> Optional<Symbol>
{
u32* hash_table_begin = (u32*)address().as_ptr();
Expand Down Expand Up @@ -447,9 +460,9 @@ auto DynamicObject::lookup_symbol(const StringView& name, u32 gnu_hash, u32 sysv
return SymbolLookupResult { symbol.value(), symbol.address(), symbol.bind(), this };
}

NonnullRefPtr<DynamicObject> DynamicObject::create(VirtualAddress base_address, VirtualAddress dynamic_section_address)
NonnullRefPtr<DynamicObject> DynamicObject::create(const String& filename, VirtualAddress base_address, VirtualAddress dynamic_section_address)
{
return adopt(*new DynamicObject(base_address, dynamic_section_address));
return adopt(*new DynamicObject(filename, base_address, dynamic_section_address));
}

// offset is in PLT relocation table
Expand Down
14 changes: 11 additions & 3 deletions Userland/Libraries/LibELF/DynamicObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,15 @@

#include <AK/Assertions.h>
#include <AK/RefCounted.h>
#include <AK/String.h>
#include <Kernel/VirtualAddress.h>
#include <LibELF/exec_elf.h>
#include <LibC/elf.h>

namespace ELF {

class DynamicObject : public RefCounted<DynamicObject> {
public:
static NonnullRefPtr<DynamicObject> create(VirtualAddress base_address, VirtualAddress dynamic_section_address);
static NonnullRefPtr<DynamicObject> create(const String& filename, VirtualAddress base_address, VirtualAddress dynamic_section_address);

~DynamicObject();
void dump() const;
Expand Down Expand Up @@ -238,6 +239,8 @@ class DynamicObject : public RefCounted<DynamicObject> {
VirtualAddress plt_got_base_address() const { return m_base_address.offset(m_procedure_linkage_table_offset.value()); }
VirtualAddress base_address() const { return m_base_address; }

const String& filename() const { return m_filename; }

StringView rpath() const { return m_has_rpath ? symbol_string_table_string(m_rpath_index) : StringView {}; }
StringView runpath() const { return m_has_runpath ? symbol_string_table_string(m_runpath_index) : StringView {}; }
StringView soname() const { return m_has_soname ? symbol_string_table_string(m_soname_index) : StringView {}; }
Expand All @@ -247,6 +250,9 @@ class DynamicObject : public RefCounted<DynamicObject> {
void set_tls_offset(FlatPtr offset) { m_tls_offset = offset; }
void set_tls_size(FlatPtr size) { m_tls_size = size; }

Elf32_Half program_header_count() const;
const Elf32_Phdr* program_headers() const;

template<typename F>
void for_each_needed_library(F) const;

Expand Down Expand Up @@ -275,12 +281,14 @@ class DynamicObject : public RefCounted<DynamicObject> {
bool elf_is_dynamic() const { return m_is_elf_dynamic; }

private:
explicit DynamicObject(VirtualAddress base_address, VirtualAddress dynamic_section_address);
explicit DynamicObject(const String& filename, VirtualAddress base_address, VirtualAddress dynamic_section_address);

StringView symbol_string_table_string(Elf32_Word) const;
const char* raw_symbol_string_table_string(Elf32_Word) const;
void parse();

String m_filename;

VirtualAddress m_base_address;
VirtualAddress m_dynamic_address;
VirtualAddress m_elf_base_address;
Expand Down
2 changes: 1 addition & 1 deletion Userland/Libraries/LibELF/Image.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
#include <AK/String.h>
#include <AK/Vector.h>
#include <Kernel/VirtualAddress.h>
#include <LibELF/exec_elf.h>
#include <LibC/elf.h>

namespace ELF {

Expand Down
2 changes: 1 addition & 1 deletion Userland/Libraries/LibELF/Validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
#include <AK/Assertions.h>
#include <AK/Checked.h>
#include <AK/String.h>
#include <LibC/elf.h>
#include <LibELF/Validation.h>
#include <LibELF/exec_elf.h>

namespace ELF {

Expand Down
2 changes: 1 addition & 1 deletion Userland/Libraries/LibELF/Validation.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
#pragma once

#include <AK/String.h>
#include <LibELF/exec_elf.h>
#include <LibC/elf.h>

namespace ELF {

Expand Down
Loading

0 comments on commit 6cb28ec

Please sign in to comment.