Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

elf: fix circular dependency in elf object #30

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions elf/elf++.hh
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ public:
*/
const section &get_section(unsigned index) const;

friend class section;
friend class segment;
private:
struct impl;
std::shared_ptr<impl> m;
Expand Down
62 changes: 48 additions & 14 deletions elf/elf.cc
Original file line number Diff line number Diff line change
Expand Up @@ -167,15 +167,28 @@ elf::get_segment(unsigned index) const

struct segment::impl {
impl(const elf &f)
: f(f) { }
: p(f.m) { }

const elf f;
elf get_elf();

const weak_ptr<elf::impl> p;
Phdr<> hdr;
// const char *name;
// size_t name_len;
const void *data;
};

elf
segment::impl::get_elf()
{
if (p.expired()) {
throw runtime_error("invalid elf reference!");
}
auto f = elf();
f.m = p.lock();
return f;
}

segment::segment(const elf &f, const void *hdr)
: m(make_shared<impl>(f)) {
canon_hdr(&m->hdr, hdr, f.get_hdr().ei_class, f.get_hdr().ei_data);
Expand All @@ -188,9 +201,11 @@ segment::get_hdr() const {

const void *
segment::data() const {
if (!m->data)
m->data = m->f.get_loader()->load(m->hdr.offset,
m->hdr.filesz);
if (!m->data) {
auto f = m->get_elf();
m->data = f.get_loader()->load(m->hdr.offset,
m->hdr.filesz);
}
return m->data;
}

Expand Down Expand Up @@ -223,15 +238,28 @@ enums::to_string(shn v)
struct section::impl
{
impl(const elf &f)
: f(f), name(nullptr), data(nullptr) { }
: p(f.m), name(nullptr), data(nullptr) { }

const elf f;
elf get_elf();

const weak_ptr<elf::impl> p;
Shdr<> hdr;
const char *name;
size_t name_len;
const void *data;
};

elf
section::impl::get_elf()
{
if (p.expired()) {
throw runtime_error("invalid elf reference!");
}
auto f = elf();
f.m = p.lock();
return f;
}

section::section(const elf &f, const void *hdr)
: m(make_shared<impl>(f))
{
Expand All @@ -248,9 +276,11 @@ const char *
section::get_name(size_t *len_out) const
{
// XXX Should the section name strtab be cached?
if (!m->name)
m->name = m->f.get_section(m->f.get_hdr().shstrndx)
if (!m->name) {
auto f = m->get_elf();
m->name = f.get_section(f.get_hdr().shstrndx)
.as_strtab().get(m->hdr.name, &m->name_len);
}
if (len_out)
*len_out = m->name_len;
return m->name;
Expand All @@ -267,8 +297,10 @@ section::data() const
{
if (m->hdr.type == sht::nobits)
return nullptr;
if (!m->data)
m->data = m->f.get_loader()->load(m->hdr.offset, m->hdr.size);
if (!m->data) {
auto f = m->get_elf();
m->data = f.get_loader()->load(m->hdr.offset, m->hdr.size);
}
return m->data;
}

Expand All @@ -283,16 +315,18 @@ section::as_strtab() const
{
if (m->hdr.type != sht::strtab)
throw section_type_mismatch("cannot use section as strtab");
return strtab(m->f, data(), size());
auto f = m->get_elf();
return strtab(f, data(), size());
}

symtab
section::as_symtab() const
{
if (m->hdr.type != sht::symtab && m->hdr.type != sht::dynsym)
throw section_type_mismatch("cannot use section as symtab");
return symtab(m->f, data(), size(),
m->f.get_section(get_hdr().link).as_strtab());
auto f = m->get_elf();
return symtab(f, data(), size(),
f.get_section(get_hdr().link).as_strtab());
}

//////////////////////////////////////////////////////////////////
Expand Down