GPLv2 release
[centaur.git] / src / libelfu / model / elf.c
1 /* This file is part of centaur.
2  *
3  * centaur is free software: you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License 2 as
5  * published by the Free Software Foundation.
6
7  * centaur is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11
12  * You should have received a copy of the GNU General Public License
13  * along with centaur.  If not, see <http://www.gnu.org/licenses/>.
14  */
15
16 #include <assert.h>
17 #include <stdlib.h>
18 #include <string.h>
19 #include <libelfu/libelfu.h>
20
21
22 /*
23  * Allocation, destruction
24  */
25
26 ElfuElf* elfu_mElfAlloc()
27 {
28   ElfuElf *me;
29
30   me = malloc(sizeof(ElfuElf));
31   if (!me) {
32     ELFU_WARN("mElfAlloc: malloc() failed for ElfuElf.\n");
33     return NULL;
34   }
35
36   memset(me, 0, sizeof(*me));
37
38   CIRCLEQ_INIT(&me->phdrList);
39   CIRCLEQ_INIT(&me->orphanScnList);
40
41   return me;
42 }
43
44
45 void elfu_mElfDestroy(ElfuElf* me)
46 {
47   assert(me);
48
49   if (!CIRCLEQ_EMPTY(&me->phdrList)) {
50     ElfuPhdr *nextmp;
51
52     nextmp = CIRCLEQ_FIRST(&me->phdrList);
53     while ((void*)nextmp != (void*)&me->phdrList) {
54       ElfuPhdr *curmp = nextmp;
55       nextmp = CIRCLEQ_NEXT(curmp, elem);
56       CIRCLEQ_REMOVE(&me->phdrList, curmp, elem);
57       elfu_mPhdrDestroy(curmp);
58     }
59   }
60
61   if (!CIRCLEQ_EMPTY(&me->orphanScnList)) {
62     ElfuScn *nextms;
63
64     nextms = CIRCLEQ_FIRST(&me->orphanScnList);
65     while ((void*)nextms != (void*)&me->orphanScnList) {
66       ElfuScn *curms = nextms;
67       nextms = CIRCLEQ_NEXT(curms, elemChildScn);
68       CIRCLEQ_REMOVE(&me->orphanScnList, curms, elemChildScn);
69       elfu_mScnDestroy(curms);
70     }
71   }
72
73   free(me);
74 }