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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2008 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/*
* Copyright (c) 1988 AT&T
* All Rights Reserved
*/
#include <ar.h>
#include <stdlib.h>
#include <memory.h>
#include <errno.h>
#include <libelf.h>
#include <sys/mman.h>
#include "decl.h"
#include "member.h"
#include "msg.h"
static const char armag[] = ARMAG;
/*
* Initialize archive member
*/
Elf *
_elf_member(int fd, Elf * ref, unsigned flags)
{
register Elf *elf;
Member *mh;
size_t base;
if (ref->ed_nextoff >= ref->ed_fsz)
return (0);
if (ref->ed_fd == -1) /* disabled */
fd = -1;
if (flags & EDF_WRITE) {
_elf_seterr(EREQ_ARRDWR, 0);
return (0);
}
if (ref->ed_fd != fd) {
_elf_seterr(EREQ_ARMEMFD, 0);
return (0);
}
if ((_elf_vm(ref, ref->ed_nextoff, sizeof (struct ar_hdr)) !=
OK_YES) || ((mh = _elf_armem(ref,
ref->ed_ident + ref->ed_nextoff, ref->ed_fsz)) == 0))
return (0);
base = ref->ed_nextoff + sizeof (struct ar_hdr);
if (ref->ed_fsz - base < mh->m_hdr.ar_size) {
_elf_seterr(EFMT_ARMEMSZ, 0);
return (0);
}
if ((elf = (Elf *)calloc(1, sizeof (Elf))) == 0) {
_elf_seterr(EMEM_ELF, errno);
return (0);
}
++ref->ed_activ;
elf->ed_parent = ref;
elf->ed_fd = fd;
elf->ed_myflags |= flags;
elf->ed_armem = mh;
elf->ed_fsz = mh->m_hdr.ar_size;
elf->ed_baseoff = ref->ed_baseoff + base;
elf->ed_memoff = base - mh->m_slide;
elf->ed_siboff = base + elf->ed_fsz + (elf->ed_fsz & 1);
ref->ed_nextoff = elf->ed_siboff;
elf->ed_image = ref->ed_image;
elf->ed_imagesz = ref->ed_imagesz;
elf->ed_vm = ref->ed_vm;
elf->ed_vmsz = ref->ed_vmsz;
elf->ed_ident = ref->ed_ident + base - mh->m_slide;
/*
* If this member is the archive string table,
* we've already altered the bytes.
*/
if (ref->ed_arstroff == ref->ed_nextoff)
elf->ed_status = ES_COOKED;
return (elf);
}
Elf *
_elf_regular(int fd, unsigned flags) /* initialize regular file */
{
Elf *elf;
if ((elf = (Elf *)calloc(1, sizeof (Elf))) == 0) {
_elf_seterr(EMEM_ELF, errno);
return (0);
}
elf->ed_fd = fd;
elf->ed_myflags |= flags;
if (_elf_inmap(elf) != OK_YES) {
free(elf);
return (0);
}
return (elf);
}
Elf *
_elf_config(Elf * elf)
{
char *base;
unsigned encode;
ELFRWLOCKINIT(&elf->ed_rwlock);
/*
* Determine if this is a ELF file.
*/
base = elf->ed_ident;
if ((elf->ed_fsz >= EI_NIDENT) &&
(_elf_vm(elf, (size_t)0, (size_t)EI_NIDENT) == OK_YES) &&
(base[EI_MAG0] == ELFMAG0) &&
(base[EI_MAG1] == ELFMAG1) &&
(base[EI_MAG2] == ELFMAG2) &&
(base[EI_MAG3] == ELFMAG3)) {
elf->ed_kind = ELF_K_ELF;
elf->ed_class = base[EI_CLASS];
elf->ed_encode = base[EI_DATA];
if ((elf->ed_version = base[EI_VERSION]) == 0)
elf->ed_version = 1;
elf->ed_identsz = EI_NIDENT;
/*
* Allow writing only if originally specified read only.
* This is only necessary if the file must be translating
* from one encoding to another.
*/
ELFACCESSDATA(encode, _elf_encode)
if ((elf->ed_vm == 0) && ((elf->ed_myflags & EDF_WRITE) == 0) &&
(elf->ed_encode != encode)) {
if (mprotect((char *)elf->ed_image, elf->ed_imagesz,
PROT_READ|PROT_WRITE) == -1) {
_elf_seterr(EIO_VM, errno);
return (0);
}
}
return (elf);
}
/*
* Determine if this is an Archive
*/
if ((elf->ed_fsz >= SARMAG) &&
(_elf_vm(elf, (size_t)0, (size_t)SARMAG) == OK_YES) &&
(memcmp(base, armag, SARMAG) == 0)) {
_elf_arinit(elf);
elf->ed_kind = ELF_K_AR;
elf->ed_identsz = SARMAG;
return (elf);
}
/*
* Return a few ident bytes, but not so many that
* getident() must read a large file. 512 is arbitrary.
*/
elf->ed_kind = ELF_K_NONE;
if ((elf->ed_identsz = elf->ed_fsz) > 512)
elf->ed_identsz = 512;
return (elf);
}
Elf *
elf_memory(char *image, size_t sz)
{
Elf *elf;
unsigned work;
/*
* version() no called yet?
*/
ELFACCESSDATA(work, _elf_work)
if (work == EV_NONE) {
_elf_seterr(ESEQ_VER, 0);
return (0);
}
if ((elf = (Elf *)calloc(1, sizeof (Elf))) == 0) {
_elf_seterr(EMEM_ELF, errno);
return (0);
}
elf->ed_fd = -1;
elf->ed_myflags |= EDF_READ | EDF_MEMORY;
elf->ed_image = elf->ed_ident = image;
elf->ed_imagesz = elf->ed_fsz = elf->ed_identsz = sz;
elf->ed_kind = ELF_K_ELF;
elf->ed_class = image[EI_CLASS];
elf->ed_encode = image[EI_DATA];
if ((elf->ed_version = image[EI_VERSION]) == 0)
elf->ed_version = 1;
elf->ed_identsz = EI_NIDENT;
elf->ed_activ = 1;
elf = _elf_config(elf);
return (elf);
}
/*
* The following is a private interface between the linkers (ld & ld.so.1)
* and libelf.
*
* elf_begin(0, ELF_C_IMAGE, ref)
* Return a new elf_descriptor which uses the memory image from
* ref as the base image of the elf file. Before this elf_begin()
* is called an elf_update(ref, ELF_C_WRIMAGE) must have been
* done to the ref elf descriptor.
* The ELF_C_IMAGE is unique in that modificatino of the Elf structure
* is illegal (no elf_new*()) but you can modify the actual
* data image of the file in question.
*
* When you are done processing this file you can then perform a
* elf_end() on it.
*
* NOTE: if an elf_update(ref, ELF_C_WRITE) is done on the ref Elf
* descriptor then the memory image that the ELF_C_IMAGE
* is using has been discarded. The proper calling convention
* for this is as follows:
*
* elf1 = elf_begin(fd, ELF_C_WRITE, 0);
* ...
* elf_update(elf1, ELF_C_WRIMAGE); build memory image
* elf2 = elf_begin(0, ELF_C_IMAGE, elf1);
* ...
* elf_end(elf2);
* elf_updage(elf1, ELF_C_WRITE); flush memory image to disk
* elf_end(elf1);
*
*
* elf_begin(0, ELF_C_IMAGE, 0);
* returns a pointer to an elf descriptor as if it were opened
* with ELF_C_WRITE except that it has no file descriptor and it
* will not create a file. It's to be used with the command:
*
* elf_update(elf, ELF_C_WRIMAGE)
*
* which will build a memory image instead of a file image.
* The memory image is allocated via dynamic memory (malloc) and
* can be free with a subsequent call to
*
* elf_update(elf, ELF_C_WRITE)
*
* NOTE: that if elf_end(elf) is called it will not free the
* memory image if it is still allocated. It is then
* the callers responsiblity to free it via a call
* to free().
*
* Here is a potential calling sequence for this interface:
*
* elf1 = elf_begin(0, ELF_C_IMAGE, 0);
* ...
* elf_update(elf1, ELF_C_WRIMAGE); build memory image
* elf2 = elf_begin(0, ELF_C_IMAGE, elf1);
* ...
* image_ptr = elf32_getehdr(elf2); get pointer to image
* elf_end(elf2);
* elf_end(elf1);
* ...
* use image
* ...
* free(image_ptr);
*/
Elf *
elf_begin(int fd, Elf_Cmd cmd, Elf *ref)
{
register Elf *elf;
unsigned work;
unsigned flags = 0;
ELFACCESSDATA(work, _elf_work)
if (work == EV_NONE) /* version() not called yet */
{
_elf_seterr(ESEQ_VER, 0);
return (0);
}
switch (cmd) {
default:
_elf_seterr(EREQ_BEGIN, 0);
return (0);
case ELF_C_NULL:
return (0);
case ELF_C_IMAGE:
if (ref) {
char *image;
size_t imagesz;
ELFRLOCK(ref);
if ((image = ref->ed_wrimage) == 0) {
_elf_seterr(EREQ_NOWRIMAGE, 0);
ELFUNLOCK(ref);
return (0);
}
imagesz = ref->ed_wrimagesz;
ELFUNLOCK(ref);
return (elf_memory(image, imagesz));
}
/* FALLTHROUGH */
case ELF_C_WRITE:
if ((elf = (Elf *)calloc(1, sizeof (Elf))) == 0) {
_elf_seterr(EMEM_ELF, errno);
return (0);
}
ELFRWLOCKINIT(&elf->ed_rwlock);
elf->ed_fd = fd;
elf->ed_activ = 1;
elf->ed_myflags |= EDF_WRITE;
if (cmd == ELF_C_IMAGE)
elf->ed_myflags |= EDF_WRALLOC;
return (elf);
case ELF_C_RDWR:
flags = EDF_WRITE | EDF_READ;
break;
case ELF_C_READ:
flags = EDF_READ;
break;
}
/*
* A null ref asks for a new file
* Non-null ref bumps the activation count
* or gets next archive member
*/
if (ref == 0) {
if ((elf = _elf_regular(fd, flags)) == 0)
return (0);
} else {
ELFWLOCK(ref);
if ((ref->ed_myflags & flags) != flags) {
_elf_seterr(EREQ_RDWR, 0);
ELFUNLOCK(ref);
return (0);
}
/*
* new activation ?
*/
if (ref->ed_kind != ELF_K_AR) {
++ref->ed_activ;
ELFUNLOCK(ref);
return (ref);
}
if ((elf = _elf_member(fd, ref, flags)) == 0) {
ELFUNLOCK(ref);
return (0);
}
ELFUNLOCK(ref);
}
elf->ed_activ = 1;
elf = _elf_config(elf);
return (elf);
}
|