summaryrefslogtreecommitdiff
path: root/src/libelfu/model/elf.c
blob: a25aa89e89637c5a1d0feccc2e0deec04b1f0084 (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
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <libelfu/libelfu.h>


/*
 * Allocation, destruction
 */

ElfuElf* elfu_mElfAlloc()
{
  ElfuElf *me;

  me = malloc(sizeof(ElfuElf));
  if (!me) {
    ELFU_WARN("mElfAlloc: malloc() failed for ElfuElf.\n");
    return NULL;
  }

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

  CIRCLEQ_INIT(&me->phdrList);
  CIRCLEQ_INIT(&me->orphanScnList);

  return me;
}


void elfu_mElfDestroy(ElfuElf* me)
{
  ElfuPhdr *mp;
  ElfuScn *ms;

  assert(me);

  CIRCLEQ_INIT(&me->phdrList);
  CIRCLEQ_INIT(&me->orphanScnList);

  CIRCLEQ_FOREACH(mp, &me->phdrList, elem) {
    CIRCLEQ_REMOVE(&me->phdrList, mp, elem);
    elfu_mPhdrDestroy(mp);
  }

  CIRCLEQ_FOREACH(ms, &me->orphanScnList, elemChildScn) {
    CIRCLEQ_REMOVE(&me->orphanScnList, ms, elemChildScn);
    elfu_mScnDestroy(ms);
  }

  free(me);
}