1 /* Common definitions for handling files in memory or only on disk.
2 Copyright (C) 1998, 1999, 2000, 2002, 2005, 2008 Red Hat, Inc.
3 This file is part of elfutils.
4 Written by Ulrich Drepper <drepper@redhat.com>, 1998.
6 This file is free software; you can redistribute it and/or modify
7 it under the terms of either
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
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
19 or both in parallel, as here.
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.
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/>. */
41 static inline Elf_Kind
42 __attribute__ ((unused))
43 determine_kind (void *buf, size_t len)
45 /* First test for an archive. */
46 if (len >= SARMAG && memcmp (buf, ARMAG, SARMAG) == 0)
49 /* Next try ELF files. */
50 if (len >= EI_NIDENT && memcmp (buf, ELFMAG, SELFMAG) == 0)
52 /* Could be an ELF file. */
53 int eclass = (int) ((unsigned char *) buf)[EI_CLASS];
54 int data = (int) ((unsigned char *) buf)[EI_DATA];
55 int version = (int) ((unsigned char *) buf)[EI_VERSION];
57 if (eclass > ELFCLASSNONE && eclass < ELFCLASSNUM
58 && data > ELFDATANONE && data < ELFDATANUM
59 && version > EV_NONE && version < EV_NUM)
63 /* We do not know this file type. */
68 /* Allocate an Elf descriptor and fill in the generic information. */
70 __attribute__ ((unused))
71 allocate_elf (int fildes, void *map_address, off_t offset, size_t maxsize,
72 Elf_Cmd cmd, Elf *parent, Elf_Kind kind, size_t extra)
74 Elf *result = (Elf *) calloc (1, sizeof (Elf) + extra);
76 __libelf_seterrno (ELF_E_NOMEM);
80 result->ref_count = 1;
82 result->fildes = fildes;
83 result->start_offset = offset;
84 result->maximum_size = maxsize;
85 result->map_address = map_address;
86 result->parent = parent;
88 rwlock_init (result->lock);
95 /* Acquire lock for the descriptor and all children. */
97 __attribute__ ((unused))
98 libelf_acquire_all (Elf *elf)
100 rwlock_wrlock (elf->lock);
102 if (elf->kind == ELF_K_AR)
104 Elf *child = elf->state.ar.children;
106 while (child != NULL)
108 if (child->ref_count != 0)
109 libelf_acquire_all (child);
115 /* Release own lock and those of the children. */
117 __attribute__ ((unused))
118 libelf_release_all (Elf *elf)
120 if (elf->kind == ELF_K_AR)
122 Elf *child = elf->state.ar.children;
124 while (child != NULL)
126 if (child->ref_count != 0)
127 libelf_release_all (child);
132 rwlock_unlock (elf->lock);
136 /* Macro to convert endianess in place. It determines the function it
137 has to use itself. */
138 #define CONVERT(Var) \
139 (Var) = (sizeof (Var) == 1 \
140 ? (unsigned char) (Var) \
141 : (sizeof (Var) == 2 \
143 : (sizeof (Var) == 4 \
147 #define CONVERT_TO(Dst, Var) \
148 (Dst) = (sizeof (Var) == 1 \
149 ? (unsigned char) (Var) \
150 : (sizeof (Var) == 2 \
152 : (sizeof (Var) == 4 \
157 #if __BYTE_ORDER == __LITTLE_ENDIAN
158 # define MY_ELFDATA ELFDATA2LSB
160 # define MY_ELFDATA ELFDATA2MSB
163 #endif /* common.h */