summaryrefslogtreecommitdiff
path: root/src/libelfu/model/elf.c
blob: 11346a5d11bac85448cf955df4a37d9e6995cbf5 (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
#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)
{
  assert(me);

  if (!CIRCLEQ_EMPTY(&me->phdrList)) {
    ElfuPhdr *nextmp;

    nextmp = CIRCLEQ_FIRST(&me->phdrList);
    while ((void*)nextmp != (void*)&me->phdrList) {
      ElfuPhdr *curmp = nextmp;
      nextmp = CIRCLEQ_NEXT(curmp, elem);
      CIRCLEQ_REMOVE(&me->phdrList, curmp, elem);
      elfu_mPhdrDestroy(curmp);
    }
  }

  if (!CIRCLEQ_EMPTY(&me->orphanScnList)) {
    ElfuScn *nextms;

    nextms = CIRCLEQ_FIRST(&me->orphanScnList);
    while ((void*)nextms != (void*)&me->orphanScnList) {
      ElfuScn *curms = nextms;
      nextms = CIRCLEQ_NEXT(curms, elemChildScn);
      CIRCLEQ_REMOVE(&me->orphanScnList, curms, elemChildScn);
      elfu_mScnDestroy(curms);
    }
  }

  free(me);
}