Line data Source code
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.
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 : #ifndef _COMMON_H
31 : #define _COMMON_H 1
32 :
33 : #include <ar.h>
34 : #include <byteswap.h>
35 : #include <endian.h>
36 : #include <stdlib.h>
37 : #include <string.h>
38 :
39 : #include "libelfP.h"
40 :
41 : static inline Elf_Kind
42 : __attribute__ ((unused))
43 14131 : determine_kind (void *buf, size_t len)
44 : {
45 : /* First test for an archive. */
46 14131 : if (len >= SARMAG && memcmp (buf, ARMAG, SARMAG) == 0)
47 : return ELF_K_AR;
48 :
49 : /* Next try ELF files. */
50 14004 : if (len >= EI_NIDENT && memcmp (buf, ELFMAG, SELFMAG) == 0)
51 : {
52 : /* Could be an ELF file. */
53 13759 : int eclass = (int) ((unsigned char *) buf)[EI_CLASS];
54 13759 : int data = (int) ((unsigned char *) buf)[EI_DATA];
55 13759 : int version = (int) ((unsigned char *) buf)[EI_VERSION];
56 :
57 13759 : if (eclass > ELFCLASSNONE && eclass < ELFCLASSNUM
58 13759 : && data > ELFDATANONE && data < ELFDATANUM
59 13759 : && version > EV_NONE && version < EV_NUM)
60 : return ELF_K_ELF;
61 : }
62 :
63 : /* We do not know this file type. */
64 245 : return ELF_K_NONE;
65 : }
66 :
67 :
68 : /* Allocate an Elf descriptor and fill in the generic information. */
69 : static inline Elf *
70 : __attribute__ ((unused))
71 14467 : 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)
73 : {
74 14467 : Elf *result = (Elf *) calloc (1, sizeof (Elf) + extra);
75 14467 : if (result == NULL)
76 0 : __libelf_seterrno (ELF_E_NOMEM);
77 : else
78 : {
79 14467 : result->kind = kind;
80 14467 : result->ref_count = 1;
81 14467 : result->cmd = cmd;
82 14467 : result->fildes = fildes;
83 14467 : result->start_offset = offset;
84 14467 : result->maximum_size = maxsize;
85 14467 : result->map_address = map_address;
86 14467 : result->parent = parent;
87 :
88 : rwlock_init (result->lock);
89 : }
90 :
91 14467 : return result;
92 : }
93 :
94 :
95 : /* Acquire lock for the descriptor and all children. */
96 : static void
97 : __attribute__ ((unused))
98 1 : libelf_acquire_all (Elf *elf)
99 : {
100 : rwlock_wrlock (elf->lock);
101 :
102 1 : if (elf->kind == ELF_K_AR)
103 : {
104 0 : Elf *child = elf->state.ar.children;
105 :
106 0 : while (child != NULL)
107 : {
108 0 : if (child->ref_count != 0)
109 0 : libelf_acquire_all (child);
110 0 : child = child->next;
111 : }
112 : }
113 1 : }
114 :
115 : /* Release own lock and those of the children. */
116 : static void
117 : __attribute__ ((unused))
118 1 : libelf_release_all (Elf *elf)
119 : {
120 1 : if (elf->kind == ELF_K_AR)
121 : {
122 0 : Elf *child = elf->state.ar.children;
123 :
124 0 : while (child != NULL)
125 : {
126 0 : if (child->ref_count != 0)
127 0 : libelf_release_all (child);
128 0 : child = child->next;
129 : }
130 : }
131 :
132 : rwlock_unlock (elf->lock);
133 1 : }
134 :
135 :
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 \
142 : ? bswap_16 (Var) \
143 : : (sizeof (Var) == 4 \
144 : ? bswap_32 (Var) \
145 : : bswap_64 (Var))))
146 :
147 : #define CONVERT_TO(Dst, Var) \
148 : (Dst) = (sizeof (Var) == 1 \
149 : ? (unsigned char) (Var) \
150 : : (sizeof (Var) == 2 \
151 : ? bswap_16 (Var) \
152 : : (sizeof (Var) == 4 \
153 : ? bswap_32 (Var) \
154 : : bswap_64 (Var))))
155 :
156 :
157 : #if __BYTE_ORDER == __LITTLE_ENDIAN
158 : # define MY_ELFDATA ELFDATA2LSB
159 : #else
160 : # define MY_ELFDATA ELFDATA2MSB
161 : #endif
162 :
163 : #endif /* common.h */
|