summaryrefslogtreecommitdiff
path: root/src/model/expandNobits.c
diff options
context:
space:
mode:
authornorly <ny-git@enpas.org>2013-05-30 04:01:51 +0100
committernorly <ny-git@enpas.org>2013-05-30 04:36:39 +0100
commit4addee4bda6064926b24cd1ae929303003bd9ff1 (patch)
tree10bad1be5ff2f71981fe87e1c43e171e94dacc8a /src/model/expandNobits.c
parenteb5a1daba781013ccf168b95510d0f67f0b9c946 (diff)
Redesign data structures, make basic reladd work.newparser
The memory ELF model is now a tree structure: ELF +--> PHDRs +--> PHDR +--> Section | | +--> Section | | ... | | \--> Section | | | +--> PHDR +--> Section | | ... | ... | \--> Orphaned sections +--> Section ... \--> Section This effectively introduces semantics into the binary blob we are editing, and allows us to re-layout its contents much more easily while keeping as close as possible to what is assumed to be the original semantics. As a side-effect, a first meta-function had to be introduced (elfu_mScnForall) in order to traverse all leaves of the tree. Much old code has been removed given the leaner environment available now, and automated insertion of .text and .data sections from object files into executables now works. However nothing else is inserted (such as string tables or .bss) and no relocation takes place yet.
Diffstat (limited to 'src/model/expandNobits.c')
-rw-r--r--src/model/expandNobits.c116
1 files changed, 0 insertions, 116 deletions
diff --git a/src/model/expandNobits.c b/src/model/expandNobits.c
deleted file mode 100644
index d12990e..0000000
--- a/src/model/expandNobits.c
+++ /dev/null
@@ -1,116 +0,0 @@
-#include <assert.h>
-#include <stdlib.h>
-#include <string.h>
-#include <sys/types.h>
-#include <libelf/gelf.h>
-#include <libelfu/libelfu.h>
-
-
-void elfu_mExpandNobits(ElfuElf *me, GElf_Off off)
-{
- ElfuScn *ms;
- ElfuPhdr *mp;
- GElf_Xword expansionSize;
-
- assert(me);
-
- expansionSize = 0;
-
- /* Find the maximum amount we need to expand by. Check PHDRs first */
- CIRCLEQ_FOREACH(mp, &me->phdrList, elem) {
- GElf_Off off2 = mp->phdr.p_offset;
- GElf_Off end2 = mp->phdr.p_offset + mp->phdr.p_filesz;
- if (end2 == off) {
- GElf_Xword size2 = mp->phdr.p_memsz - mp->phdr.p_filesz;
- if (size2 > expansionSize) {
- expansionSize = size2;
- }
- } else if (end2 > off) {
- if (off2 < off) {
- /*
- * Found a PHDR whose file contents overlaps with the section
- * to be filled. This means that it relies on the NOBITS area
- * being actually 0 bytes, and the expansion would ruin it.
- */
- ELFU_WARN("mExpandNobits: Found PHDR spanning expansion offset. Aborting.\n");
- return;
- }
- } else {
- // end2 < off, and the PHDR is unaffected.
- continue;
- }
- }
-
- /* Now check SHDRs */
- CIRCLEQ_FOREACH(ms, &me->scnList, elem) {
- if (ms->shdr.sh_offset == off) {
- if (ms->shdr.sh_type == SHT_NOBITS) {
- if (ms->shdr.sh_size > expansionSize) {
- expansionSize = ms->shdr.sh_size;
- }
- }
- }
- }
-
-
-
- /* Expand! */
-
-
- /* Move all following PHDR offsets further down the file. */
- CIRCLEQ_FOREACH(mp, &me->phdrList, elem) {
- GElf_Off off2 = mp->phdr.p_offset;
- GElf_Off end2 = mp->phdr.p_offset + mp->phdr.p_filesz;
-
- if (off2 >= off) {
- mp->phdr.p_offset += expansionSize;
- } else {
- if (end2 == off) {
- /* This PHDR now has corresponding bytes in the file for every
- * byte in memory. */
- mp->phdr.p_filesz = mp->phdr.p_memsz;
- }
- }
- }
-
- /* Move the following sections */
- CIRCLEQ_FOREACH(ms, &me->scnList, elem) {
- if (ms->shdr.sh_offset >= off) {
- if (ms->shdr.sh_offset > off
- || ms->shdr.sh_type != SHT_NOBITS) {
- ms->shdr.sh_offset += expansionSize;
- }
- }
- }
-
- /* Move SHDR/PHDR tables */
- if (me->ehdr.e_shoff >= off) {
- me->ehdr.e_shoff += expansionSize;
- }
-
- if (me->ehdr.e_phoff >= off) {
- me->ehdr.e_phoff += expansionSize;
- }
-
-
- /* Convert any NOBITS at off to PROGBITS */
- CIRCLEQ_FOREACH(ms, &me->scnList, elem) {
- if (ms->shdr.sh_offset == off) {
- if (ms->shdr.sh_type == SHT_NOBITS) {
- ms->data.d_buf = malloc(ms->shdr.sh_size);
- memset(ms->data.d_buf, '\0', ms->shdr.sh_size);
- if (!ms->data.d_buf) {
- ELFU_WARN("mExpandNobits: Could not allocate %jd bytes for NOBITS expansion.\n", ms->shdr.sh_size);
- }
-
- ms->data.d_align = 1;
- ms->data.d_off = 0;
- ms->data.d_type = ELF_T_BYTE;
- ms->data.d_size = ms->shdr.sh_size;
- ms->data.d_version = elf_version(EV_NONE);
-
- ms->shdr.sh_type = SHT_PROGBITS;
- }
- }
- }
-}