summaryrefslogtreecommitdiff
path: root/src/libelfu/model/elf.c
blob: 41169dd3910e681b17c2b034e4fc64272b5f240a (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
/* 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>


/*
 * 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);
}