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

ElfuScn* elfu_mCloneScn(ElfuScn *ms)
{
  ElfuScn *newscn;

  assert(ms);

  newscn = malloc(sizeof(ElfuScn));
  if (!newscn) {
    ELFU_WARN("elfu_nCloneScn: Could not allocate memory for new ElfuScn.\n");
    return NULL;
  }

  newscn->shdr = ms->shdr;
  newscn->data = ms->data;
  if (ms->data.d_buf) {
    void *newbuf = malloc(ms->data.d_size);
    if (!newbuf) {
      ELFU_WARN("elfu_nCloneScn: Could not allocate memory for new data buffer.\n");
      free(newscn);
      return NULL;
    }

    memcpy(newbuf, ms->data.d_buf, ms->data.d_size);
    newscn->data.d_buf = newbuf;
  }

  newscn->linkptr = NULL;
  newscn->infoptr = NULL;

  newscn->oldptr = ms;

  return newscn;
}