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

CMake building and Initial REL(A) support #37

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Prev Previous commit
Next Next commit
changing elf lib domain model, and adding some stubs
  • Loading branch information
deadly-platypus committed Sep 4, 2019
commit 16d210125ba6fa44f8166e56592ed2e7c9254417
18 changes: 2 additions & 16 deletions elf/data.hh
Original file line number Diff line number Diff line change
Expand Up @@ -273,17 +273,9 @@ ELFPP_BEGIN_NAMESPACE
// Relocation types are processor-specific.
typename E::Word32_Xword64 info;

inline unsigned sym_idx() const {
return info >> E::R_SYM_SHIFT;
}

inline unsigned rel_type() const {
return info & E::R_TYPE_MASK;
}
inline uint32_t R_SYM_SHIFT() const { return E::R_SYM_SHIFT; }

static size_t size() {
return sizeof(typename E::Off) + sizeof(typename E::Word32_Xword64);
}
inline uint32_t R_TYPE_MASK() const { return E::R_TYPE_MASK; }
};

// Relocation Entry with addend
Expand All @@ -292,12 +284,6 @@ ELFPP_BEGIN_NAMESPACE
// Specifies a constant addend used to compute the value to be
// stored into the relocatable field.
typename E::S_Word32_Xword64 addend;

static size_t size() {
return sizeof(typename E::Off) +
sizeof(typename E::Word32_Xword64) +
sizeof (typename E::S_Word32_Xword64);
}
};

// Section header (ELF32 figure 1-8, ELF64 figure 3)
Expand Down
58 changes: 55 additions & 3 deletions elf/elf++.hh
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,59 @@ ELFPP_BEGIN_NAMESPACE
std::shared_ptr<impl> m;
};

/**
* @brief Rel entry
*/
class rel {
Elf_Rel<> data;

public:
rel(void *d);

unsigned sym_idx() const {
return data.info >> data.R_SYM_SHIFT();
}

unsigned rel_type() const {
return data.info & data.R_TYPE_MASK();
}

size_t size() const {
return sizeof(data.info) + sizeof(data.offset);
}

const Elf_Rel<> &get_data() const {
return data;
}
};

/**
* @brief Rela entry
*/
class rela {
Elf_Rela<> data;

public:
rela(void *d);

unsigned sym_idx() const {
return data.info >> data.R_SYM_SHIFT();
}

unsigned rel_type() const {
return data.info & data.R_TYPE_MASK();
}

size_t size() const {
return sizeof(data.info) + sizeof(data.offset) +
sizeof(data.addend);
}

const Elf_Rela<> &get_data() const {
return data;
}
};

/**
* An ELF section.
*
Expand Down Expand Up @@ -294,13 +347,13 @@ ELFPP_BEGIN_NAMESPACE
* @brief Return list of REL entries.
* @throws section_type_mismatch if this section is not of type REL
*/
std::vector<Elf_Rel<>> get_rels() const;
std::vector<rel> get_rels() const;

/**
* @brief Return list of RELA entries.
* @throws section_type_mismatch if this section is not of type RELA
*/
std::vector<Elf_Rela<>> get_relas() const;
std::vector<rela> get_relas() const;

private:
struct impl;
Expand Down Expand Up @@ -463,7 +516,6 @@ ELFPP_BEGIN_NAMESPACE
std::shared_ptr<impl> m;
};


ELFPP_END_NAMESPACE

#endif
58 changes: 38 additions & 20 deletions elf/elf.cc
Original file line number Diff line number Diff line change
Expand Up @@ -283,44 +283,37 @@ ELFPP_BEGIN_NAMESPACE
m->f.get_section(get_hdr().link).as_strtab());
}

std::vector<Elf_Rel<>>
std::vector<rel>
section::get_rels() const {
if (m->hdr.type != sht::rel) {
throw section_type_mismatch("section not a REL type");
}

std::vector<Elf_Rel<>> result;
std::vector<rel> result;
uintptr_t curr = (uintptr_t) data();
uintptr_t end = (uintptr_t) ((uintptr_t) data() + size());
for (; curr < end; curr += Elf_Rel<>::size()) {
Elf_Rel<> entry;
std::memcpy(&entry.offset, (void *) curr, sizeof(entry.offset));
std::memcpy(&entry.info, (void *) (curr + sizeof(entry.offset)),
sizeof(entry.info));
result.emplace_back(entry);
while (curr < end) {
rel rel_entry((void *) curr);
result.emplace_back(rel_entry);
curr += rel_entry.size();
}

return result;
}

std::vector<Elf_Rela<>>
std::vector<rela>
section::get_relas() const {
if (m->hdr.type != sht::rela) {
throw section_type_mismatch("section not a RELA type");
}

std::vector<Elf_Rela<>> result;
std::vector<rela> result;
uintptr_t curr = (uintptr_t) data();
uintptr_t end = (uintptr_t) ((uintptr_t) data() + size());
for (; curr < end; curr += Elf_Rela<>::size()) {
Elf_Rela<> entry;
std::memcpy(&entry.offset, (void *) curr, sizeof(entry.offset));
std::memcpy(&entry.info, (void *) (curr + sizeof(entry.offset)),
sizeof(entry.info));
std::memcpy(&entry.addend, (void *) (curr + sizeof(entry.offset)
+ sizeof(entry.info)),
sizeof(entry.addend));
result.emplace_back(entry);
while (curr < end) {
rela rela_entry((void *) curr);
result.emplace_back(rela_entry);
curr += rela_entry.size();
}
return result;
}
Expand Down Expand Up @@ -386,6 +379,31 @@ ELFPP_BEGIN_NAMESPACE
return strs.get(get_data().name);
}

//////////////////////////////////////////////////////////////////
// class rela
//
rela::rela(void *d) :
data() {
std::memcpy(&data.offset, d, sizeof(data.offset));
std::memcpy(&data.info, (void *) ((char *) d + sizeof(data.offset)),
sizeof(data.info));
std::memcpy(&data.addend, (void *) ((char *) d + sizeof(data.offset) +
sizeof(data.info)),
sizeof(data.addend));
}

//////////////////////////////////////////////////////////////////
// class rel
//

rel::rel(void *d) :
data() {
std::memcpy(&data.offset, d, sizeof(data.offset));
std::memcpy(&data.info, (void *) ((char *) d + sizeof(data.offset)),
sizeof(data.info));

}

//////////////////////////////////////////////////////////////////
// class symtab
//
Expand All @@ -408,7 +426,7 @@ ELFPP_BEGIN_NAMESPACE
sym symtab::get_sym(unsigned idx) const {
size_t entry_size = m->f.get_symtab_entry_size();

if(m->data + idx * entry_size >= m->end) {
if (m->data + idx * entry_size >= m->end) {
std::stringstream err;
err << "Index " << idx << " out of bounds";
throw std::out_of_range(err.str());
Expand Down