Skip to content

Commit

Permalink
Add support for encrypting elfs with section holes within segments
Browse files Browse the repository at this point in the history
  • Loading branch information
will-v-pi committed Oct 2, 2024
1 parent afdfa92 commit f7b6aca
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
25 changes: 25 additions & 0 deletions elf/elf_file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,15 @@ void elf_file::flatten(void) {
}
idx++;
}

idx = 0;
for (const auto &ph : ph_entries) {
if (ph.filez) {
elf_bytes.resize(std::max(ph.offset + ph.filez, (uint32_t)elf_bytes.size()));
memcpy(&elf_bytes[ph.offset], &ph_data[idx][0], ph.filez);
}
idx++;
}
if (verbose) printf("Elf file size %zu\n", elf_bytes.size());
}

Expand Down Expand Up @@ -265,6 +274,18 @@ void elf_file::read_sh_data(void) {
}
}

void elf_file::read_ph_data(void) {
int ph_idx = 0;
ph_data.resize(eh.ph_num);
for (const auto &ph: ph_entries) {
if (ph.filez) {
ph_data[ph_idx].resize(ph.filez);
read_bytes(ph.offset, ph.filez, &ph_data[ph_idx][0]);
}
ph_idx++;
}
}

const std::string elf_file::section_name(uint32_t sh_name) const {
if (!eh.sh_str_index || eh.sh_str_index > eh.sh_num)
return "";
Expand Down Expand Up @@ -372,6 +393,7 @@ int elf_file::read_file(std::shared_ptr<std::iostream> file) {
read_sh();
}
read_sh_data();
read_ph_data();
}
catch (const std::ios_base::failure &e) {
std::cerr << "Failed to read elf file" << std::endl;
Expand Down Expand Up @@ -418,6 +440,7 @@ void elf_file::content(const elf32_ph_entry &ph, const std::vector<uint8_t> &con
if (verbose) printf("Update segment content offset %x content size %zx physical size %x\n", ph.offset, content.size(), ph.filez);
memcpy(&elf_bytes[ph.offset], &content[0], std::min(content.size(), (size_t) ph.filez));
read_sh_data(); // Extract the sections after modifying the content
read_ph_data();
}

void elf_file::content(const elf32_sh_entry &sh, const std::vector<uint8_t> &content) {
Expand All @@ -426,6 +449,7 @@ void elf_file::content(const elf32_sh_entry &sh, const std::vector<uint8_t> &con
if (verbose) printf("Update section content offset %x content size %zx section size %x\n", sh.offset, content.size(), sh.size);
memcpy(&elf_bytes[sh.offset], &content[0], std::min(content.size(), (size_t) sh.size));
read_sh_data(); // Extract the sections after modifying the content
read_ph_data();
}

const elf32_ph_entry* elf_file::segment_from_physical_address(uint32_t paddr) {
Expand Down Expand Up @@ -503,6 +527,7 @@ const elf32_ph_entry& elf_file::append_segment(uint32_t vaddr, uint32_t paddr, u
sh_entries.push_back(sh);
sh_data.push_back(std::vector<uint8_t>(size));
ph_entries.back().offset = sh.offset;
ph_data.push_back(std::vector<uint8_t>(size));

eh.sh_offset = sh.offset + sh.size;
eh.sh_num++;
Expand Down
2 changes: 2 additions & 0 deletions elf/elf_file.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class elf_file {
void read_ph(void);
void read_sh(void);
void read_sh_data(void);
void read_ph_data(void);
void read_bytes(unsigned offset, unsigned length, void *dest);
uint32_t append_section_name(const std::string &sh_name_str);
void flatten(void);
Expand All @@ -65,6 +66,7 @@ class elf_file {
std::vector<elf32_ph_entry> ph_entries;
std::vector<elf32_sh_entry> sh_entries;
std::vector<std::vector<uint8_t>> sh_data;
std::vector<std::vector<uint8_t>> ph_data;
bool verbose;
};
int rp_check_elf_header(const elf32_header &eh);
Expand Down

0 comments on commit f7b6aca

Please sign in to comment.