Compare commits
No commits in common. "2ae2ede0b28c37c1b87f52d6b17bf355acd50a86" and "50ab391133817163641b31f0a0265d2af0af5671" have entirely different histories.
2ae2ede0b2
...
50ab391133
|
@ -211,7 +211,7 @@ elseif("${BANAN_ARCH}" STREQUAL "i686")
|
||||||
target_link_options(kernel PRIVATE LINKER:-T,${CMAKE_CURRENT_SOURCE_DIR}/arch/i686/linker.ld)
|
target_link_options(kernel PRIVATE LINKER:-T,${CMAKE_CURRENT_SOURCE_DIR}/arch/i686/linker.ld)
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
target_link_options(kernel PRIVATE -ffreestanding -nostdlib -orphan-handling=error)
|
target_link_options(kernel PRIVATE -ffreestanding -nostdlib)
|
||||||
|
|
||||||
execute_process(COMMAND ${CMAKE_CXX_COMPILER} -print-file-name=crtbegin.o OUTPUT_VARIABLE CRTBEGIN OUTPUT_STRIP_TRAILING_WHITESPACE)
|
execute_process(COMMAND ${CMAKE_CXX_COMPILER} -print-file-name=crtbegin.o OUTPUT_VARIABLE CRTBEGIN OUTPUT_STRIP_TRAILING_WHITESPACE)
|
||||||
execute_process(COMMAND ${CMAKE_CXX_COMPILER} -print-file-name=crtend.o OUTPUT_VARIABLE CRTEND OUTPUT_STRIP_TRAILING_WHITESPACE)
|
execute_process(COMMAND ${CMAKE_CXX_COMPILER} -print-file-name=crtend.o OUTPUT_VARIABLE CRTEND OUTPUT_STRIP_TRAILING_WHITESPACE)
|
||||||
|
|
|
@ -21,20 +21,20 @@ SECTIONS
|
||||||
g_userspace_end = .;
|
g_userspace_end = .;
|
||||||
g_kernel_execute_end = .;
|
g_kernel_execute_end = .;
|
||||||
}
|
}
|
||||||
.ap_init ALIGN(4K) : AT(ADDR(.ap_init) - KERNEL_OFFSET)
|
.ap_init ALIGN(4K) : AT(ADDR(.ap_init))
|
||||||
{
|
{
|
||||||
g_ap_init_addr = .;
|
g_ap_init_addr = .;
|
||||||
*(.ap_init)
|
*(.ap_init)
|
||||||
}
|
}
|
||||||
.rodata ALIGN(4K) : AT(ADDR(.rodata) - KERNEL_OFFSET)
|
|
||||||
{
|
|
||||||
*(.rodata.*)
|
|
||||||
}
|
|
||||||
.data ALIGN(4K) : AT(ADDR(.data) - KERNEL_OFFSET)
|
.data ALIGN(4K) : AT(ADDR(.data) - KERNEL_OFFSET)
|
||||||
{
|
{
|
||||||
g_kernel_writable_start = .;
|
g_kernel_writable_start = .;
|
||||||
*(.data)
|
*(.data)
|
||||||
}
|
}
|
||||||
|
.rodata ALIGN(4K) : AT(ADDR(.rodata) - KERNEL_OFFSET)
|
||||||
|
{
|
||||||
|
*(.rodata.*)
|
||||||
|
}
|
||||||
.bss ALIGN(4K) : AT(ADDR(.bss) - KERNEL_OFFSET)
|
.bss ALIGN(4K) : AT(ADDR(.bss) - KERNEL_OFFSET)
|
||||||
{
|
{
|
||||||
*(COMMON)
|
*(COMMON)
|
||||||
|
|
|
@ -21,20 +21,20 @@ SECTIONS
|
||||||
g_userspace_end = .;
|
g_userspace_end = .;
|
||||||
g_kernel_execute_end = .;
|
g_kernel_execute_end = .;
|
||||||
}
|
}
|
||||||
.ap_init ALIGN(4K) : AT(ADDR(.ap_init) - KERNEL_OFFSET)
|
.ap_init ALIGN(4K) : AT(ADDR(.ap_init))
|
||||||
{
|
{
|
||||||
g_ap_init_addr = .;
|
g_ap_init_addr = .;
|
||||||
*(.ap_init)
|
*(.ap_init)
|
||||||
}
|
}
|
||||||
.rodata ALIGN(4K) : AT(ADDR(.rodata) - KERNEL_OFFSET)
|
|
||||||
{
|
|
||||||
*(.rodata.*)
|
|
||||||
}
|
|
||||||
.data ALIGN(4K) : AT(ADDR(.data) - KERNEL_OFFSET)
|
.data ALIGN(4K) : AT(ADDR(.data) - KERNEL_OFFSET)
|
||||||
{
|
{
|
||||||
g_kernel_writable_start = .;
|
g_kernel_writable_start = .;
|
||||||
*(.data)
|
*(.data)
|
||||||
}
|
}
|
||||||
|
.rodata ALIGN(4K) : AT(ADDR(.rodata) - KERNEL_OFFSET)
|
||||||
|
{
|
||||||
|
*(.rodata.*)
|
||||||
|
}
|
||||||
.bss ALIGN(4K) : AT(ADDR(.bss) - KERNEL_OFFSET)
|
.bss ALIGN(4K) : AT(ADDR(.bss) - KERNEL_OFFSET)
|
||||||
{
|
{
|
||||||
*(COMMON)
|
*(COMMON)
|
||||||
|
|
|
@ -35,7 +35,7 @@ namespace Kernel
|
||||||
|
|
||||||
bool MemoryRegion::contains_fully(vaddr_t address, size_t size) const
|
bool MemoryRegion::contains_fully(vaddr_t address, size_t size) const
|
||||||
{
|
{
|
||||||
return m_vaddr <= address && address + size <= m_vaddr + m_size;
|
return m_vaddr <= address && address + size < m_vaddr + m_size;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool MemoryRegion::overlaps(vaddr_t address, size_t size) const
|
bool MemoryRegion::overlaps(vaddr_t address, size_t size) const
|
||||||
|
|
|
@ -519,8 +519,8 @@ namespace Kernel
|
||||||
{
|
{
|
||||||
VirtualFileSystem::File file;
|
VirtualFileSystem::File file;
|
||||||
TRY(file.canonical_path.append("<self>"));
|
TRY(file.canonical_path.append("<self>"));
|
||||||
file.inode = m_loadable_elf->executable();
|
file.inode = m_loadable_elf->inode();
|
||||||
m_userspace_info.file_fd = TRY(m_open_file_descriptors.open(BAN::move(file), O_RDONLY));
|
m_userspace_info.file_fd = TRY(m_open_file_descriptors.open(BAN::move(file), O_EXEC));
|
||||||
}
|
}
|
||||||
|
|
||||||
for (size_t i = 0; i < sizeof(m_signal_handlers) / sizeof(*m_signal_handlers); i++)
|
for (size_t i = 0; i < sizeof(m_signal_handlers) / sizeof(*m_signal_handlers); i++)
|
||||||
|
@ -2376,7 +2376,7 @@ namespace Kernel
|
||||||
return {};
|
return {};
|
||||||
|
|
||||||
unauthorized_access:
|
unauthorized_access:
|
||||||
dwarnln("process {}, thread {} attempted to make an invalid pointer access to 0x{H}->0x{H}", pid(), Thread::current().tid(), vaddr, vaddr + size);
|
dwarnln("process {}, thread {} attempted to make an invalid pointer access", pid(), Thread::current().tid());
|
||||||
Debug::dump_stack_trace();
|
Debug::dump_stack_trace();
|
||||||
MUST(sys_kill(pid(), SIGSEGV));
|
MUST(sys_kill(pid(), SIGSEGV));
|
||||||
return BAN::Error::from_errno(EINTR);
|
return BAN::Error::from_errno(EINTR);
|
||||||
|
|
|
@ -26,10 +26,10 @@ namespace LibELF
|
||||||
|
|
||||||
LoadableELF::~LoadableELF()
|
LoadableELF::~LoadableELF()
|
||||||
{
|
{
|
||||||
if (!m_is_loaded)
|
const auto cleanup_program_headers =
|
||||||
return;
|
[&](BAN::Span<const ElfNativeProgramHeader> headers)
|
||||||
|
{
|
||||||
for (const auto& header : m_program_headers)
|
for (const auto& header : headers)
|
||||||
{
|
{
|
||||||
ASSERT(header.p_type == PT_LOAD);
|
ASSERT(header.p_type == PT_LOAD);
|
||||||
|
|
||||||
|
@ -40,6 +40,12 @@ namespace LibELF
|
||||||
Heap::get().release_page(paddr);
|
Heap::get().release_page(paddr);
|
||||||
m_page_table.unmap_range(vaddr, pages * PAGE_SIZE);
|
m_page_table.unmap_range(vaddr, pages * PAGE_SIZE);
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
if (!m_is_loaded)
|
||||||
|
return;
|
||||||
|
cleanup_program_headers(m_executable.program_headers.span());
|
||||||
|
cleanup_program_headers(m_interpreter.program_headers.span());
|
||||||
}
|
}
|
||||||
|
|
||||||
static BAN::ErrorOr<ElfNativeFileHeader> read_and_validate_file_header(BAN::RefPtr<Inode> inode)
|
static BAN::ErrorOr<ElfNativeFileHeader> read_and_validate_file_header(BAN::RefPtr<Inode> inode)
|
||||||
|
@ -160,26 +166,30 @@ namespace LibELF
|
||||||
}
|
}
|
||||||
|
|
||||||
return LoadResult {
|
return LoadResult {
|
||||||
|
.elf_file = {
|
||||||
.inode = inode,
|
.inode = inode,
|
||||||
.interp = interp,
|
|
||||||
.file_header = file_header,
|
.file_header = file_header,
|
||||||
.program_headers = BAN::move(program_headers)
|
.program_headers = BAN::move(program_headers),
|
||||||
|
.dynamic_base = 0
|
||||||
|
},
|
||||||
|
.interp = interp
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool do_program_headers_overlap(BAN::Span<const ElfNativeProgramHeader> pheaders1, BAN::Span<const ElfNativeProgramHeader> pheaders2, vaddr_t base2)
|
bool LoadableELF::does_executable_and_interpreter_overlap() const
|
||||||
{
|
{
|
||||||
for (const auto& pheader1 : pheaders1)
|
ASSERT(m_executable.inode);
|
||||||
{
|
ASSERT(m_interpreter.inode);
|
||||||
for (const auto& pheader2 : pheaders2)
|
|
||||||
{
|
|
||||||
const vaddr_t s1 = pheader1.p_vaddr & PAGE_ADDR_MASK;
|
|
||||||
const vaddr_t e1 = (pheader1.p_vaddr + pheader1.p_memsz + PAGE_SIZE - 1) & PAGE_ADDR_MASK;
|
|
||||||
|
|
||||||
const vaddr_t s2 = pheader2.p_vaddr & PAGE_ADDR_MASK;
|
for (const auto& epheader : m_executable.program_headers)
|
||||||
const vaddr_t e2 = (pheader2.p_vaddr + pheader2.p_memsz + PAGE_SIZE - 1) & PAGE_ADDR_MASK;
|
{
|
||||||
|
for (const auto& ipheader : m_interpreter.program_headers)
|
||||||
if (s1 < e2 + base2 && s2 + base2 < e1)
|
{
|
||||||
|
const vaddr_t e1 = epheader.p_vaddr & PAGE_ADDR_MASK;
|
||||||
|
const vaddr_t i1 = ipheader.p_vaddr & PAGE_ADDR_MASK;
|
||||||
|
const vaddr_t e2 = (epheader.p_vaddr + epheader.p_memsz + PAGE_SIZE - 1) & PAGE_ADDR_MASK;
|
||||||
|
const vaddr_t i2 = (ipheader.p_vaddr + ipheader.p_memsz + PAGE_SIZE - 1) & PAGE_ADDR_MASK;
|
||||||
|
if (e1 < i2 && i1 < e2)
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -189,95 +199,74 @@ namespace LibELF
|
||||||
|
|
||||||
BAN::ErrorOr<void> LoadableELF::initialize(const Credentials& credentials, BAN::RefPtr<Inode> inode)
|
BAN::ErrorOr<void> LoadableELF::initialize(const Credentials& credentials, BAN::RefPtr<Inode> inode)
|
||||||
{
|
{
|
||||||
const auto generate_random_dynamic_base =
|
|
||||||
[]() -> vaddr_t
|
|
||||||
{
|
|
||||||
// 1 MiB -> 2 GiB + 1 MiB
|
|
||||||
return (Random::get_u32() & 0x7FFFF000) + 0x100000;
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
auto executable_load_result = TRY(load_elf_file(credentials, inode));
|
auto executable_load_result = TRY(load_elf_file(credentials, inode));
|
||||||
|
m_executable = executable_load_result.elf_file;
|
||||||
|
|
||||||
m_executable = executable_load_result.inode;
|
if (m_executable.file_header.e_type == ET_DYN)
|
||||||
m_interpreter = executable_load_result.interp;
|
|
||||||
|
|
||||||
vaddr_t dynamic_base = 0;
|
|
||||||
|
|
||||||
if (m_interpreter)
|
|
||||||
{
|
{
|
||||||
auto interp_load_result = TRY(load_elf_file(credentials, m_interpreter));
|
m_executable.dynamic_base = (Random::get_u32() & 0x7FFFF000) + 0x100000;
|
||||||
|
m_executable.file_header.e_entry += m_executable.dynamic_base;
|
||||||
|
for (auto& program_header : m_executable.program_headers)
|
||||||
|
program_header.p_vaddr += m_executable.dynamic_base;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (executable_load_result.interp)
|
||||||
|
{
|
||||||
|
auto interp_load_result = TRY(load_elf_file(credentials, executable_load_result.interp));
|
||||||
|
m_interpreter = interp_load_result.elf_file;
|
||||||
|
|
||||||
if (interp_load_result.interp)
|
if (interp_load_result.interp)
|
||||||
{
|
{
|
||||||
dwarnln("ELF interpreter has an interpreter");
|
dwarnln("Executable has specified interpreter for its interpreter");
|
||||||
return BAN::Error::from_errno(EINVAL);
|
return BAN::Error::from_errno(EINVAL);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (executable_load_result.file_header.e_type == ET_EXEC)
|
if (m_interpreter.file_header.e_type == ET_DYN)
|
||||||
{
|
|
||||||
if (interp_load_result.file_header.e_type == ET_EXEC)
|
|
||||||
{
|
|
||||||
const bool has_overlap = do_program_headers_overlap(
|
|
||||||
executable_load_result.program_headers.span(),
|
|
||||||
interp_load_result.program_headers.span(),
|
|
||||||
0
|
|
||||||
);
|
|
||||||
|
|
||||||
if (has_overlap)
|
|
||||||
{
|
|
||||||
dwarnln("Executable and interpreter LOAD segments overlap");
|
|
||||||
return BAN::Error::from_errno(EINVAL);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
{
|
||||||
for (int attempt = 0; attempt < 100; attempt++)
|
for (int attempt = 0; attempt < 100; attempt++)
|
||||||
{
|
{
|
||||||
const vaddr_t test_dynamic_base = generate_random_dynamic_base();
|
const vaddr_t dynamic_base = (Random::get_u32() & 0x3FFFF000) + 0x40000000;
|
||||||
const bool has_overlap = do_program_headers_overlap(
|
for (auto& program_header : m_interpreter.program_headers)
|
||||||
executable_load_result.program_headers.span(),
|
program_header.p_vaddr += dynamic_base;
|
||||||
interp_load_result.program_headers.span(),
|
if (does_executable_and_interpreter_overlap())
|
||||||
test_dynamic_base
|
{
|
||||||
);
|
for (auto& program_header : m_interpreter.program_headers)
|
||||||
if (has_overlap)
|
program_header.p_vaddr -= dynamic_base;
|
||||||
continue;
|
continue;
|
||||||
dynamic_base = test_dynamic_base;
|
}
|
||||||
|
m_interpreter.dynamic_base = dynamic_base;
|
||||||
|
m_interpreter.file_header.e_entry += dynamic_base;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (dynamic_base == 0)
|
const bool can_load_interpreter = (m_interpreter.file_header.e_type == ET_DYN)
|
||||||
|
? (m_interpreter.dynamic_base != 0)
|
||||||
|
: !does_executable_and_interpreter_overlap();
|
||||||
|
|
||||||
|
if (!can_load_interpreter)
|
||||||
{
|
{
|
||||||
dwarnln("Could not find space to load interpreter");
|
dwarnln("Could not find space to load interpreter");
|
||||||
return BAN::Error::from_errno(EINVAL);
|
return BAN::Error::from_errno(EINVAL);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
m_file_header = interp_load_result.file_header;
|
|
||||||
m_program_headers = BAN::move(interp_load_result.program_headers);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
m_file_header = executable_load_result.file_header;
|
|
||||||
m_program_headers = BAN::move(executable_load_result.program_headers);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (m_file_header.e_type == ET_DYN && dynamic_base == 0)
|
|
||||||
dynamic_base = generate_random_dynamic_base();
|
|
||||||
|
|
||||||
if (dynamic_base)
|
|
||||||
{
|
|
||||||
m_file_header.e_entry += dynamic_base;
|
|
||||||
for (auto& program_header : m_program_headers)
|
|
||||||
program_header.p_vaddr += dynamic_base;
|
|
||||||
}
|
|
||||||
|
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
vaddr_t LoadableELF::entry_point() const
|
||||||
|
{
|
||||||
|
if (m_interpreter.inode)
|
||||||
|
return m_interpreter.file_header.e_entry;
|
||||||
|
return m_executable.file_header.e_entry;
|
||||||
|
}
|
||||||
|
|
||||||
bool LoadableELF::contains(vaddr_t address) const
|
bool LoadableELF::contains(vaddr_t address) const
|
||||||
{
|
{
|
||||||
for (const auto& program_header : m_program_headers)
|
for (const auto& program_header : m_executable.program_headers)
|
||||||
|
if (program_header.p_vaddr <= address && address < program_header.p_vaddr + program_header.p_memsz)
|
||||||
|
return true;
|
||||||
|
for (const auto& program_header : m_interpreter.program_headers)
|
||||||
if (program_header.p_vaddr <= address && address < program_header.p_vaddr + program_header.p_memsz)
|
if (program_header.p_vaddr <= address && address < program_header.p_vaddr + program_header.p_memsz)
|
||||||
return true;
|
return true;
|
||||||
return false;
|
return false;
|
||||||
|
@ -285,7 +274,10 @@ namespace LibELF
|
||||||
|
|
||||||
bool LoadableELF::is_address_space_free() const
|
bool LoadableELF::is_address_space_free() const
|
||||||
{
|
{
|
||||||
for (const auto& program_header : m_program_headers)
|
const auto are_program_headers_free =
|
||||||
|
[&](BAN::Span<const ElfNativeProgramHeader> program_headers) -> bool
|
||||||
|
{
|
||||||
|
for (const auto& program_header : program_headers)
|
||||||
{
|
{
|
||||||
ASSERT(program_header.p_type == PT_LOAD);
|
ASSERT(program_header.p_type == PT_LOAD);
|
||||||
const vaddr_t page_vaddr = program_header.p_vaddr & PAGE_ADDR_MASK;
|
const vaddr_t page_vaddr = program_header.p_vaddr & PAGE_ADDR_MASK;
|
||||||
|
@ -294,11 +286,20 @@ namespace LibELF
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
|
};
|
||||||
|
if (!are_program_headers_free(m_executable.program_headers.span()))
|
||||||
|
return false;
|
||||||
|
if (!are_program_headers_free(m_interpreter.program_headers.span()))
|
||||||
|
return false;
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void LoadableELF::reserve_address_space()
|
void LoadableELF::reserve_address_space()
|
||||||
{
|
{
|
||||||
for (const auto& program_header : m_program_headers)
|
const auto reserve_program_headers =
|
||||||
|
[&](BAN::Span<const ElfNativeProgramHeader> program_headers)
|
||||||
|
{
|
||||||
|
for (const auto& program_header : program_headers)
|
||||||
{
|
{
|
||||||
ASSERT(program_header.p_type == PT_LOAD);
|
ASSERT(program_header.p_type == PT_LOAD);
|
||||||
const vaddr_t page_vaddr = program_header.p_vaddr & PAGE_ADDR_MASK;
|
const vaddr_t page_vaddr = program_header.p_vaddr & PAGE_ADDR_MASK;
|
||||||
|
@ -307,24 +308,29 @@ namespace LibELF
|
||||||
ASSERT_NOT_REACHED();
|
ASSERT_NOT_REACHED();
|
||||||
m_virtual_page_count += pages;
|
m_virtual_page_count += pages;
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
reserve_program_headers(m_executable.program_headers.span());
|
||||||
|
reserve_program_headers(m_interpreter.program_headers.span());
|
||||||
m_is_loaded = true;
|
m_is_loaded = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void LoadableELF::update_suid_sgid(Kernel::Credentials& credentials)
|
void LoadableELF::update_suid_sgid(Kernel::Credentials& credentials)
|
||||||
{
|
{
|
||||||
if (m_executable->mode().mode & +Inode::Mode::ISUID)
|
auto inode = m_executable.inode;
|
||||||
credentials.set_euid(m_executable->uid());
|
ASSERT(inode);
|
||||||
if (m_executable->mode().mode & +Inode::Mode::ISGID)
|
|
||||||
credentials.set_egid(m_executable->gid());
|
if (inode->mode().mode & +Inode::Mode::ISUID)
|
||||||
|
credentials.set_euid(inode->uid());
|
||||||
|
if (inode->mode().mode & +Inode::Mode::ISGID)
|
||||||
|
credentials.set_egid(inode->gid());
|
||||||
}
|
}
|
||||||
|
|
||||||
BAN::ErrorOr<void> LoadableELF::load_page_to_memory(vaddr_t address)
|
BAN::ErrorOr<void> LoadableELF::load_page_to_memory(vaddr_t address)
|
||||||
{
|
{
|
||||||
auto inode = has_interpreter() ? m_interpreter : m_executable;
|
const auto load_page_from_program_header =
|
||||||
|
[&](BAN::RefPtr<Inode> inode, BAN::Span<const ElfNativeProgramHeader> program_headers) -> BAN::ErrorOr<bool>
|
||||||
// FIXME: use MemoryBackedRegion/FileBackedRegion instead of manually mapping and allocating pages
|
{
|
||||||
|
for (const auto& program_header : program_headers)
|
||||||
for (const auto& program_header : m_program_headers)
|
|
||||||
{
|
{
|
||||||
ASSERT(program_header.p_type == PT_LOAD);
|
ASSERT(program_header.p_type == PT_LOAD);
|
||||||
if (!(program_header.p_vaddr <= address && address < program_header.p_vaddr + program_header.p_memsz))
|
if (!(program_header.p_vaddr <= address && address < program_header.p_vaddr + program_header.p_memsz))
|
||||||
|
@ -364,9 +370,16 @@ namespace LibELF
|
||||||
// Map page with the correct flags
|
// Map page with the correct flags
|
||||||
m_page_table.map_page_at(paddr, vaddr, flags);
|
m_page_table.map_page_at(paddr, vaddr, flags);
|
||||||
|
|
||||||
return {};
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
};
|
||||||
|
|
||||||
|
if (TRY(load_page_from_program_header(m_executable.inode, m_executable.program_headers.span())))
|
||||||
|
return {};
|
||||||
|
if (TRY(load_page_from_program_header(m_interpreter.inode, m_interpreter.program_headers.span())))
|
||||||
|
return {};
|
||||||
ASSERT_NOT_REACHED();
|
ASSERT_NOT_REACHED();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -374,16 +387,27 @@ namespace LibELF
|
||||||
{
|
{
|
||||||
auto elf = TRY(BAN::UniqPtr<LoadableELF>::create(new_page_table));
|
auto elf = TRY(BAN::UniqPtr<LoadableELF>::create(new_page_table));
|
||||||
|
|
||||||
elf->m_executable = m_executable;
|
const auto clone_loadable_file =
|
||||||
elf->m_interpreter = m_interpreter;
|
[](const LoadableElfFile& source, LoadableElfFile& destination) -> BAN::ErrorOr<void>
|
||||||
elf->m_file_header = m_file_header;
|
{
|
||||||
TRY(elf->m_program_headers.reserve(m_program_headers.size()));
|
if (!source.inode)
|
||||||
for (const auto& program_header : m_program_headers)
|
return {};
|
||||||
MUST(elf->m_program_headers.emplace_back(program_header));
|
|
||||||
|
|
||||||
elf->reserve_address_space();
|
destination.inode = source.inode;
|
||||||
|
destination.file_header = source.file_header;
|
||||||
|
destination.dynamic_base = source.dynamic_base;
|
||||||
|
|
||||||
for (const auto& program_header : m_program_headers)
|
TRY(destination.program_headers.reserve(source.program_headers.size()));
|
||||||
|
for (const auto& program_header : source.program_headers)
|
||||||
|
MUST(destination.program_headers.emplace_back(program_header));
|
||||||
|
|
||||||
|
return {};
|
||||||
|
};
|
||||||
|
|
||||||
|
const auto map_loadable_file =
|
||||||
|
[&](BAN::Span<const ElfNativeProgramHeader> program_headers) -> BAN::ErrorOr<void>
|
||||||
|
{
|
||||||
|
for (const auto& program_header : program_headers)
|
||||||
{
|
{
|
||||||
ASSERT(program_header.p_type == PT_LOAD);
|
ASSERT(program_header.p_type == PT_LOAD);
|
||||||
if (!(program_header.p_flags & LibELF::PF_W))
|
if (!(program_header.p_flags & LibELF::PF_W))
|
||||||
|
@ -415,6 +439,16 @@ namespace LibELF
|
||||||
elf->m_physical_page_count++;
|
elf->m_physical_page_count++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return {};
|
||||||
|
};
|
||||||
|
|
||||||
|
TRY(clone_loadable_file(m_executable, elf->m_executable));
|
||||||
|
TRY(clone_loadable_file(m_interpreter, elf->m_interpreter));
|
||||||
|
|
||||||
|
elf->reserve_address_space();
|
||||||
|
|
||||||
|
TRY(map_loadable_file(elf->m_executable.program_headers.span()));
|
||||||
|
TRY(map_loadable_file(elf->m_interpreter.program_headers.span()));
|
||||||
|
|
||||||
return elf;
|
return elf;
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,10 +25,10 @@ namespace LibELF
|
||||||
static BAN::ErrorOr<BAN::UniqPtr<LoadableELF>> load_from_inode(Kernel::PageTable&, const Kernel::Credentials&, BAN::RefPtr<Kernel::Inode>);
|
static BAN::ErrorOr<BAN::UniqPtr<LoadableELF>> load_from_inode(Kernel::PageTable&, const Kernel::Credentials&, BAN::RefPtr<Kernel::Inode>);
|
||||||
~LoadableELF();
|
~LoadableELF();
|
||||||
|
|
||||||
Kernel::vaddr_t entry_point() const { return m_file_header.e_entry; }
|
Kernel::vaddr_t entry_point() const;
|
||||||
|
|
||||||
bool has_interpreter() const { return !!m_interpreter; }
|
bool has_interpreter() const { return !!m_interpreter.inode; }
|
||||||
BAN::RefPtr<Kernel::Inode> executable() { return m_executable; }
|
BAN::RefPtr<Kernel::Inode> inode() { return m_executable.inode; }
|
||||||
|
|
||||||
bool contains(Kernel::vaddr_t address) const;
|
bool contains(Kernel::vaddr_t address) const;
|
||||||
bool is_address_space_free() const;
|
bool is_address_space_free() const;
|
||||||
|
@ -44,25 +44,30 @@ namespace LibELF
|
||||||
size_t physical_page_count() const { return m_physical_page_count; }
|
size_t physical_page_count() const { return m_physical_page_count; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
struct LoadResult
|
struct LoadableElfFile
|
||||||
{
|
{
|
||||||
BAN::RefPtr<Kernel::Inode> inode;
|
BAN::RefPtr<Kernel::Inode> inode;
|
||||||
BAN::RefPtr<Kernel::Inode> interp;
|
|
||||||
ElfNativeFileHeader file_header;
|
ElfNativeFileHeader file_header;
|
||||||
BAN::Vector<ElfNativeProgramHeader> program_headers;
|
BAN::Vector<ElfNativeProgramHeader> program_headers;
|
||||||
|
Kernel::vaddr_t dynamic_base;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct LoadResult
|
||||||
|
{
|
||||||
|
LoadableElfFile elf_file;
|
||||||
|
BAN::RefPtr<Kernel::Inode> interp;
|
||||||
};
|
};
|
||||||
|
|
||||||
private:
|
private:
|
||||||
LoadableELF(Kernel::PageTable&);
|
LoadableELF(Kernel::PageTable&);
|
||||||
BAN::ErrorOr<void> initialize(const Kernel::Credentials&, BAN::RefPtr<Kernel::Inode>);
|
BAN::ErrorOr<void> initialize(const Kernel::Credentials&, BAN::RefPtr<Kernel::Inode>);
|
||||||
|
|
||||||
|
bool does_executable_and_interpreter_overlap() const;
|
||||||
BAN::ErrorOr<LoadResult> load_elf_file(const Kernel::Credentials&, BAN::RefPtr<Kernel::Inode>) const;
|
BAN::ErrorOr<LoadResult> load_elf_file(const Kernel::Credentials&, BAN::RefPtr<Kernel::Inode>) const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
BAN::RefPtr<Kernel::Inode> m_executable;
|
LoadableElfFile m_executable;
|
||||||
BAN::RefPtr<Kernel::Inode> m_interpreter;
|
LoadableElfFile m_interpreter;
|
||||||
ElfNativeFileHeader m_file_header;
|
|
||||||
BAN::Vector<ElfNativeProgramHeader> m_program_headers;
|
|
||||||
|
|
||||||
Kernel::PageTable& m_page_table;
|
Kernel::PageTable& m_page_table;
|
||||||
size_t m_virtual_page_count { 0 };
|
size_t m_virtual_page_count { 0 };
|
||||||
size_t m_physical_page_count { 0 };
|
size_t m_physical_page_count { 0 };
|
||||||
|
|
|
@ -137,41 +137,4 @@ namespace LibELF
|
||||||
PF_MASKPROC = 0xFF000000,
|
PF_MASKPROC = 0xFF000000,
|
||||||
};
|
};
|
||||||
|
|
||||||
enum ELF_DT
|
|
||||||
{
|
|
||||||
DT_NULL = 0,
|
|
||||||
DT_NEEDED = 1,
|
|
||||||
DT_PLTRELSZ = 2,
|
|
||||||
DT_PLTGOT = 3,
|
|
||||||
DT_HASH = 4,
|
|
||||||
DT_STRTAB = 5,
|
|
||||||
DT_SYMTAB = 6,
|
|
||||||
DT_RELA = 7,
|
|
||||||
DT_RELASZ = 8,
|
|
||||||
DT_RELAENT = 9,
|
|
||||||
DT_STRSZ = 10,
|
|
||||||
DT_SYMENT = 11,
|
|
||||||
DT_INIT = 12,
|
|
||||||
DT_FINI = 13,
|
|
||||||
DT_SONAME = 14,
|
|
||||||
DT_RPATH = 15,
|
|
||||||
DT_SYMBOLIC = 16,
|
|
||||||
DT_REL = 17,
|
|
||||||
DT_RELSZ = 18,
|
|
||||||
DT_RELENT = 19,
|
|
||||||
DT_PLTREL = 20,
|
|
||||||
DT_DEBUG = 21,
|
|
||||||
DT_TEXTREL = 22,
|
|
||||||
DT_JMPREL = 23,
|
|
||||||
DT_BIND_NOW = 24,
|
|
||||||
DT_INIT_ARRAY = 25,
|
|
||||||
DT_FINI_ARRAY = 26,
|
|
||||||
DT_INIT_ARRAYSZ = 27,
|
|
||||||
DT_FINI_ARRAYSZ = 28,
|
|
||||||
DT_LOOS = 0x60000000,
|
|
||||||
DT_HIOS = 0x6FFFFFFF,
|
|
||||||
DT_LOPROC = 0x70000000,
|
|
||||||
DT_HIPROC = 0x7FFFFFFF,
|
|
||||||
};
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue