summaryrefslogtreecommitdiff
path: root/src/libelfu/model/section.c
blob: 3e6c89f2afc6923d3dbf88aa29c79965d6b9fe84 (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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/* This file is part of centaur.
 *
 * centaur is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License 2 as
 * published by the Free Software Foundation.

 * centaur is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with centaur.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <libelfu/libelfu.h>


/* Meta-functions */

void* elfu_mScnForall(ElfuElf *me, SectionHandlerFunc f, void *aux1, void *aux2)
{
  ElfuPhdr *mp;
  ElfuScn *ms;

  CIRCLEQ_FOREACH(mp, &me->phdrList, elem) {
    if (mp->phdr.p_type != PT_LOAD) {
      continue;
    }

    CIRCLEQ_FOREACH(ms, &mp->childScnList, elemChildScn) {
      void *rv = f(me, ms, aux1, aux2);

      if (rv) {
        return rv;
      }
    }
  }

  CIRCLEQ_FOREACH(ms, &me->orphanScnList, elemChildScn) {
    void *rv = f(me, ms, aux1, aux2);

    if (rv) {
      return rv;
    }
  }

  return NULL;
}




/* Counting */

static void* subCounter(ElfuElf *me, ElfuScn *ms, void *aux1, void *aux2)
{
  size_t *i = (size_t*)aux1;
  ElfuScn *otherScn = (ElfuScn*)aux2;

  if (ms == otherScn) {
    return ms;
  }

  *i += 1;

  /* Continue */
  return NULL;
}


size_t elfu_mScnCount(ElfuElf *me)
{
  /* NULL section *is not* counted */
  size_t i = 0;

  assert(me);

  elfu_mScnForall(me, subCounter, &i, NULL);

  return i;
}


/* Returns the index a section would have in the flattened ELF */
size_t elfu_mScnIndex(ElfuElf *me, ElfuScn *ms)
{
  /* NULL section *is* counted */
  size_t i = 1;

  assert(me);
  assert(ms);

  elfu_mScnForall(me, subCounter, &i, ms);

  /* If this assertion is broken then ms is not a section in me. */
  assert(i <= elfu_mScnCount(me));
  return i;
}


static void* subOldscn(ElfuElf *me, ElfuScn *ms, void *aux1, void *aux2)
{
  ElfuScn *otherScn = (ElfuScn*)aux1;
  (void)aux2;

  if (ms->oldptr == otherScn) {
    return ms;
  }

  /* Continue */
  return NULL;
}

/* Returns the section with oldscn == oldscn */
ElfuScn* elfu_mScnByOldscn(ElfuElf *me, ElfuScn *oldscn)
{
  assert(me);
  assert(oldscn);

  return elfu_mScnForall(me, subOldscn, oldscn, NULL);
}




/* Convenience */

char* elfu_mScnName(ElfuElf *me, ElfuScn *ms)
{
  assert(me);
  assert(ms);

  if (!me->shstrtab) {
    return NULL;
  }

  if (!me->shstrtab->databuf) {
    return NULL;
  }

  return &me->shstrtab->databuf[ms->shdr.sh_name];
}


static void* subScnsToArray(ElfuElf *me, ElfuScn *ms, void *aux1, void *aux2)
{
  ElfuScn **arr = (ElfuScn**)aux1;
  size_t *i = (size_t*)aux2;

  arr[(*i)] = ms;
  *i += 1;

  /* Continue */
  return NULL;
}

static int cmpScnOffs(const void *ms1, const void *ms2)
{
  ElfuScn *s1;
  ElfuScn *s2;

  assert(ms1);
  assert(ms2);

  s1 = *(ElfuScn**)ms1;
  s2 = *(ElfuScn**)ms2;

  assert(s1);
  assert(s2);


  if (s1->shdr.sh_offset < s2->shdr.sh_offset) {
    return -1;
  } else if (s1->shdr.sh_offset == s2->shdr.sh_offset) {
    return 0;
  } else /* if (s1->shdr.sh_offset > s2->shdr.sh_offset) */ {
    return 1;
  }
}

ElfuScn** elfu_mScnSortedByOffset(ElfuElf *me, size_t *count)
{
  size_t numSecs;
  ElfuScn **sortedSecs;
  size_t i;

  assert(me);

  /* Sort sections by offset in file */
  numSecs = elfu_mScnCount(me);
  sortedSecs = malloc(numSecs * sizeof(*sortedSecs));
  if (!sortedSecs) {
    ELFU_WARN("elfu_mScnSortedByOffset: Failed to allocate memory.\n");
    return NULL;
  }

  i = 0;
  elfu_mScnForall(me, subScnsToArray, sortedSecs, &i);
  assert(i == numSecs);

  qsort(sortedSecs, numSecs, sizeof(*sortedSecs), cmpScnOffs);

  *count = numSecs;

  return sortedSecs;
}



int elfu_mScnAppendData(ElfuScn *ms, void *buf, size_t len)
{
  char *newbuf;

  assert(ms);
  assert(ms->shdr.sh_type != SHT_NOBITS);
  assert(ms->databuf);

  newbuf = realloc(ms->databuf, ms->shdr.sh_size + len);
  if (!newbuf) {
    ELFU_WARN("elfu_mScnAppendData: malloc() failed for newbuf.\n");
    return -1;
  }

  ms->databuf = newbuf;
  memcpy(newbuf + ms->shdr.sh_size, buf, len);
  ms->shdr.sh_size += len;
  assert(ms->shdr.sh_size == ms->shdr.sh_size);

  return 0;
}



/*
 * Allocation, destruction
 */

ElfuScn* elfu_mScnAlloc()
{
  ElfuScn *ms;

  ms = malloc(sizeof(ElfuScn));
  if (!ms) {
    ELFU_WARN("mScnCreate: malloc() failed for ElfuScn.\n");
    return NULL;
  }

  memset(ms, 0, sizeof(*ms));

  CIRCLEQ_INIT(&ms->symtab.syms);
  CIRCLEQ_INIT(&ms->reltab.rels);

  return ms;
}

void elfu_mScnDestroy(ElfuScn* ms)
{
  assert(ms);

  if (!CIRCLEQ_EMPTY(&ms->symtab.syms)) {
    ElfuSym *nextsym;

    nextsym = CIRCLEQ_FIRST(&ms->symtab.syms);
    while ((void*)nextsym != (void*)&ms->symtab.syms) {
      ElfuSym *cursym = nextsym;
      nextsym = CIRCLEQ_NEXT(cursym, elem);
      CIRCLEQ_REMOVE(&ms->symtab.syms, cursym, elem);
      free(cursym);
    }
  }

  if (!CIRCLEQ_EMPTY(&ms->reltab.rels)) {
    ElfuRel *nextrel;

    nextrel = CIRCLEQ_FIRST(&ms->reltab.rels);
    while ((void*)nextrel != (void*)&ms->reltab.rels) {
      ElfuRel *currel = nextrel;
      nextrel = CIRCLEQ_NEXT(currel, elem);
      CIRCLEQ_REMOVE(&ms->reltab.rels, currel, elem);
      free(currel);
    }
  }

  if (ms->databuf) {
    free(ms->databuf);
  }

  free(ms);
}