1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 11:48:10 +00:00

LibELF: Call mmap() before constructing the DynamicLoader object

Refactor DynamicLoader construction with a try_create() helper so that
we can call mmap() before making a loader. This way the loader doesn't
need to have an "mmap failed" state.

This patch also takes care of determining the ELF file size in
try_create() instead of expecting callers to provide it.
This commit is contained in:
Andreas Kling 2021-01-31 10:13:23 +01:00
parent d71bfb9614
commit 68576bcf1b
4 changed files with 54 additions and 71 deletions

View file

@ -35,16 +35,14 @@
#include <LibC/stdio.h>
#include <LibC/sys/internals.h>
#include <LibC/unistd.h>
#include <LibCore/File.h>
#include <LibELF/AuxiliaryVector.h>
#include <LibELF/DynamicLinker.h>
#include <LibELF/DynamicLoader.h>
#include <LibELF/DynamicObject.h>
#include <LibELF/Image.h>
#include <LibELF/exec_elf.h>
#include <dlfcn.h>
#include <fcntl.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
namespace ELF {
@ -84,14 +82,14 @@ Optional<DynamicObject::SymbolLookupResult> DynamicLinker::lookup_global_symbol(
static void map_library(const String& name, int fd)
{
struct stat lib_stat;
int rc = fstat(fd, &lib_stat);
ASSERT(!rc);
auto loader = ELF::DynamicLoader::construct(name.characters(), fd, lib_stat.st_size);
auto loader = ELF::DynamicLoader::try_create(fd, name);
if (!loader) {
dbgln("Failed to create ELF::DynamicLoader for fd={}, name={}", fd, name);
ASSERT_NOT_REACHED();
}
loader->set_tls_offset(g_current_tls_offset);
g_loaders.set(name, loader);
g_loaders.set(name, *loader);
g_current_tls_offset += loader->tls_size();
}