1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 23:37:36 +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

@ -24,14 +24,6 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <assert.h>
#include <dlfcn.h>
#include <fcntl.h>
#include <mman.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <AK/HashMap.h>
#include <AK/LexicalPath.h>
#include <AK/RefPtr.h>
@ -39,6 +31,11 @@
#include <AK/String.h>
#include <AK/StringBuilder.h>
#include <LibELF/DynamicLoader.h>
#include <assert.h>
#include <dlfcn.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
// NOTE: The string here should never include a trailing newline (according to POSIX)
String g_dlerror_msg;
@ -85,18 +82,8 @@ void* dlopen(const char* filename, int flags)
ScopeGuard close_fd_guard([fd]() { close(fd); });
struct stat file_stats {
};
int ret = fstat(fd, &file_stats);
if (ret < 0) {
g_dlerror_msg = String::formatted("Unable to stat file {}", filename);
return nullptr;
}
auto loader = ELF::DynamicLoader::construct(filename, fd, file_stats.st_size);
if (!loader->is_valid()) {
auto loader = ELF::DynamicLoader::try_create(fd, filename);
if (!loader || !loader->is_valid()) {
g_dlerror_msg = String::formatted("{} is not a valid ELF dynamic shared object!", filename);
return nullptr;
}