1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
|
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include "elfload.h"
enum { num_load = 10 };
static struct {
char *data;
Elf32_Addr base;
Elf32_Word length, fsz;
Elf32_Off off;
} loaded[num_load];
static int numloaded = 0;
static int dirty = 0;
extern const char *curfile;
int
read_check_ehdr(int fd, Elf32_Ehdr *ehdr)
{
ssize_t numread;
if (lseek(fd, 0, SEEK_SET) == -1)
{
fprintf (stderr, "%s: positioning for header: %m\n", curfile);
return -1;
}
numread = read(fd, ehdr, sizeof(Elf32_Ehdr));
if (numread == -1)
{
fprintf (stderr, "%s: header read failed: %m\n", curfile);
return -1;
}
else if (numread < (ssize_t)sizeof(Elf32_Ehdr))
return 2;
if (*(unsigned *)ehdr->e_ident != *(const unsigned *)ELFMAG
|| ehdr->e_ident[EI_CLASS] != ELFCLASS32
|| ehdr->e_ident[EI_DATA] != ELFDATA2MSB
|| ehdr->e_ident[EI_VERSION] != EV_CURRENT
|| ehdr->e_type != ET_EXEC && ehdr->e_type != ET_DYN
|| ehdr->e_machine != EM_PPC)
return -2;
if (ehdr->e_phentsize != sizeof(Elf32_Phdr))
{
fprintf(stderr, "%s: Section size was read as %d, not %d!\n",
curfile, ehdr->e_phentsize, sizeof(Elf32_Phdr));
return -1;
}
return 0;
}
int
read_segment_list(int fd, const Elf32_Ehdr *ehdr, Elf32_Off *dyn_offset_p)
{
Elf32_Off dyn_offset;
Elf32_Word i;
free_segments();
if (lseek(fd, ehdr->e_phoff, SEEK_SET) == -1)
{
fprintf (stderr, "%s: positioning for sections: %m\n", curfile);
return -1;
}
dyn_offset = 0;
for (i = 0; i < ehdr->e_phnum; i++)
{
Elf32_Phdr phdr;
if (read(fd, &phdr, sizeof(phdr)) != sizeof(phdr))
{
fprintf (stderr, "%s: reading section header %d: %m\n",
curfile, i);
return -1;
}
if (phdr.p_type == PT_DYNAMIC)
if (dyn_offset == 0)
dyn_offset = phdr.p_offset;
else
{
fprintf(stderr, "%s: Seems to have multiple dynamic sections.\n",
curfile);
return -1;
}
else if (phdr.p_type == PT_LOAD)
{
if (numloaded == num_load)
{
fprintf(stderr, "%s: Too many loadable sections.\n",
curfile);
return -1;
}
loaded[numloaded].base = phdr.p_vaddr;
loaded[numloaded].off = phdr.p_offset;
loaded[numloaded].length = phdr.p_memsz;
loaded[numloaded].fsz = phdr.p_filesz;
loaded[numloaded].data = 0;
numloaded++;
}
}
if (dyn_offset_p)
*dyn_offset_p = dyn_offset;
return dyn_offset == 0;
}
int
load_segments(int fd)
{
int i;
for (i = 0; i < numloaded; i++)
{
loaded[i].data = malloc(loaded[i].length);
memset(loaded[i].data, 0, loaded[i].length);
if (lseek(fd, loaded[i].off, SEEK_SET) == -1)
{
fprintf (stderr, "%s: seeking for segment %d: %m\n", curfile, i);
return -1;
}
if (read(fd, loaded[i].data, loaded[i].fsz) != (ssize_t)loaded[i].fsz)
{
fprintf (stderr, "%s: reading segment %d: %m\n", curfile, i);
return -1;
}
}
return 0;
}
void
changed_segment(void)
{ dirty = 1; }
int
write_segments(int fd)
{
int i;
if (!dirty)
return 0;
for (i = 0; i < numloaded; i++)
{
if (lseek(fd, loaded[i].off, SEEK_SET) == -1)
{
fprintf (stderr, "%s: seeking for segment %d: %m\n", curfile, i);
return -1;
}
if (write(fd, loaded[i].data, loaded[i].fsz) != (ssize_t)loaded[i].fsz)
{
fprintf (stderr, "%s: writing segment %d: %m\n", curfile, i);
return -1;
}
}
dirty = 0;
return 0;
}
char *
find_location(Elf32_Addr where, Elf32_Word length)
{
int t;
char *result = 0;
for (t = 0; t < numloaded; t++)
if (loaded[t].base <= where
&& loaded[t].base + loaded[t].length >= where + length)
result = loaded[t].data + (where - loaded[t].base);
return result;
}
void
free_segments(void)
{
while (numloaded > 0)
if (loaded[--numloaded].data)
free(loaded[numloaded].data);
dirty = 0;
}
|