Branch data Line data Source code
1 : : /* Create clone of a given descriptor.
2 : : Copyright (C) 2003, 2004 Red Hat, Inc.
3 : : This file is part of elfutils.
4 : : Written by Ulrich Drepper <drepper@redhat.com>, 2003.
5 : :
6 : : This file is free software; you can redistribute it and/or modify
7 : : it under the terms of either
8 : :
9 : : * the GNU Lesser General Public License as published by the Free
10 : : Software Foundation; either version 3 of the License, or (at
11 : : your option) any later version
12 : :
13 : : or
14 : :
15 : : * the GNU General Public License as published by the Free
16 : : Software Foundation; either version 2 of the License, or (at
17 : : your option) any later version
18 : :
19 : : or both in parallel, as here.
20 : :
21 : : elfutils is distributed in the hope that it will be useful, but
22 : : WITHOUT ANY WARRANTY; without even the implied warranty of
23 : : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24 : : General Public License for more details.
25 : :
26 : : You should have received copies of the GNU General Public License and
27 : : the GNU Lesser General Public License along with this program. If
28 : : not, see <http://www.gnu.org/licenses/>. */
29 : :
30 : : #ifdef HAVE_CONFIG_H
31 : : # include <config.h>
32 : : #endif
33 : :
34 : : #include <stddef.h>
35 : : #include "libelfP.h"
36 : : #include "common.h"
37 : :
38 : :
39 : : Elf *
40 : 88 : elf_clone (Elf *elf, Elf_Cmd cmd)
41 : : {
42 : 88 : Elf *retval = NULL;
43 : :
44 [ - + ]: 88 : if (elf == NULL)
45 : : /* Some earlier mistake. */
46 : : return NULL;
47 : :
48 : : /* Make sure the descriptor is not suddenly going away. */
49 : 88 : rwlock_rdlock (elf->lock);
50 : :
51 [ - + ]: 88 : if (cmd != ELF_C_EMPTY)
52 : : // XXX TODO handle ELF_C_READ/WRITE etc
53 : 0 : goto out;
54 : :
55 : 176 : retval = allocate_elf (elf->fildes, elf->map_address, elf->start_offset,
56 : : elf->maximum_size, elf->cmd, elf->parent, elf->kind,
57 : 88 : elf->state.elf32.scns.max * sizeof (Elf_Scn));
58 [ - + ]: 88 : if (retval != NULL)
59 : : {
60 : : /* We have to write to the file in any case. */
61 : 88 : retval->flags = ELF_F_DIRTY;
62 : :
63 : : /* Some more or less arbitrary value. */
64 : 88 : retval->state.elf.scnincr = 10;
65 : :
66 : : /* We have allocated room for some sections. */
67 : 88 : eu_static_assert (offsetof (struct Elf, state.elf32.scns)
68 : : == offsetof (struct Elf, state.elf64.scns));
69 : 88 : retval->state.elf.scns_last = &retval->state.elf32.scns;
70 : 88 : retval->state.elf32.scns.max = elf->state.elf32.scns.max;
71 : :
72 : 88 : retval->class = elf->class;
73 : : }
74 : :
75 : : /* Release the lock. */
76 : 0 : out:
77 : : rwlock_unlock (elf->lock);
78 : :
79 : : return retval;
80 : : }
|