sled/3party/libelfin/elf/mmap_loader.cc
tqcq cc7d131d95
Some checks failed
linux-x64-gcc / linux-gcc (Release) (push) Failing after 37s
linux-x64-gcc / linux-gcc (Debug) (push) Failing after 48s
linux-arm-gcc / linux-gcc-armhf (push) Failing after 1m6s
linux-mips64-gcc / linux-gcc-mips64el (Debug) (push) Failing after 1m23s
linux-aarch64-cpu-gcc / linux-gcc-aarch64 (push) Failing after 1m42s
linux-mips64-gcc / linux-gcc-mips64el (Release) (push) Failing after 1m47s
feat add dispatcher
2024-04-07 02:42:12 +00:00

61 lines
1.4 KiB
C++

// Copyright (c) 2013 Austin T. Clements. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
#include "elf++.hh"
#include <system_error>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
using namespace std;
ELFPP_BEGIN_NAMESPACE
class mmap_loader : public loader
{
void *base;
size_t lim;
public:
mmap_loader(int fd)
{
off_t end = lseek(fd, 0, SEEK_END);
if (end == (off_t)-1)
throw system_error(errno, system_category(),
"finding file length");
lim = end;
base = mmap(nullptr, lim, PROT_READ, MAP_SHARED, fd, 0);
if (base == MAP_FAILED)
throw system_error(errno, system_category(),
"mmap'ing file");
close(fd);
}
~mmap_loader()
{
munmap(base, lim);
}
const void *load(off_t offset, size_t size)
{
if (offset + size > lim)
throw range_error("offset exceeds file size");
return (const char*)base + offset;
}
};
std::shared_ptr<loader>
create_mmap_loader(int fd)
{
return make_shared<mmap_loader>(fd);
}
ELFPP_END_NAMESPACE