summaryrefslogtreecommitdiff
path: root/src/model/fromFile.c
blob: bcdb91a025f2106e4b86a7cb133da4f96dd160a4 (plain)
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
185
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <libelf/libelf.h>
#include <libelf/gelf.h>
#include <libelfu/libelfu.h>


static ElfuPhdr* modelFromPhdr(GElf_Phdr *phdr)
{
  ElfuPhdr *mp;

  assert(phdr);

  mp = malloc(sizeof(ElfuPhdr));
  if (!mp) {
    ELFU_WARN("modelFromPhdr: malloc() failed for ElfuPhdr.\n");
    return NULL;
  }

  mp->phdr = *phdr;

  return mp;
}


static ElfuScn* modelFromSection(Elf_Scn *scn)
{
  ElfuScn *ms;

  assert(scn);

  ms = malloc(sizeof(ElfuScn));
  if (!ms) {
    ELFU_WARN("modelFromSection: malloc() failed for ElfuScn.\n");
    goto ERROR;
  }


  assert(gelf_getshdr(scn, &ms->shdr) == &ms->shdr);


  /* Copy each data part in source segment */
  ms->data.d_align = 1;
  ms->data.d_buf  = NULL;
  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);
  if (ms->shdr.sh_type != SHT_NOBITS
      && ms->shdr.sh_size > 0) {
    Elf_Data *data;

    ms->data.d_buf = malloc(ms->shdr.sh_size);
    if (!ms->data.d_buf) {
      ELFU_WARN("modelFromSection: malloc() failed for data buffer (%jx bytes).\n", ms->shdr.sh_size);
      goto ERROR;
    }

    /* A non-empty section should contain at least one data block. */
    data = elf_rawdata(scn, NULL);
    assert(data);

    ms->data.d_align = data->d_align;
    ms->data.d_type = data->d_type;
    ms->data.d_version = data->d_version;

    while (data) {
      if (data->d_off + data->d_size > ms->shdr.sh_size) {
        ELFU_WARN("modelFromSection: libelf delivered a bogus data blob. Skipping\n");
      } else {
        memcpy(ms->data.d_buf + data->d_off, data->d_buf, data->d_size);
      }

      data = elf_rawdata(scn, data);
    }
  }


  return ms;

  ERROR:
  if (ms) {
    free(ms);
  }
  return NULL;
}




ElfuElf* elfu_mFromElf(Elf *e)
{
  ElfuElf *me;
  size_t shstrndx;
  size_t i, numPhdr, numShdr;

  assert(e);
  if (elfu_eCheck(e)) {
    goto ERROR;
  }

  me = malloc(sizeof(ElfuElf));
  if (!me) {
    ELFU_WARN("elfu_mFromElf: malloc() failed for ElfuElf.\n");
    goto ERROR;
  }


  /* General stuff */
  CIRCLEQ_INIT(&me->scnList);
  CIRCLEQ_INIT(&me->phdrList);
  me->shstrtab = NULL;

  me->elfclass = gelf_getclass(e);
  assert(me->elfclass != ELFCLASSNONE);
  assert(gelf_getehdr(e, &me->ehdr) == &me->ehdr);


  /* Get the section string table index */
  if (elf_getshdrstrndx(e, &shstrndx) != 0) {
    shstrndx = 0;
  }


  /* Load segments */
  assert(!elf_getphdrnum(e, &numPhdr));
  for (i = 0; i < numPhdr; i++) {
    GElf_Phdr phdr;
    ElfuPhdr *mp;

    assert(gelf_getphdr(e, i, &phdr) == &phdr);

    mp = modelFromPhdr(&phdr);
    if (!mp) {
      goto ERROR;
    }

    CIRCLEQ_INSERT_TAIL(&me->phdrList, mp, elem);
  }


  /* Load sections */
  assert(!elf_getshdrnum(e, &numShdr));
  for (i = 1; i < numShdr; i++) {
    Elf_Scn *scn;
    ElfuScn *ms;

    scn = elf_getscn(e, i);
    assert(scn);

    ms = modelFromSection(scn);
    if (!ms) {
      goto ERROR;
    }

    CIRCLEQ_INSERT_TAIL(&me->scnList, ms, elem);
    if (i == shstrndx) {
      me->shstrtab = ms;
    }
  }


  /* Find sh_link dependencies */


  /* Sort sections by offset */


  /* Find PHDR -> Section dependencies */



  return me;


  ERROR:
  if (me) {
    // TODO: Free data structures
  }

  ELFU_WARN("elfu_mFromElf: Failed to load file.\n");
  return NULL;
}