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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
|
/*
* 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 (c) 1992, 2010, Oracle and/or its affiliates. All rights reserved.
*/
/*
* Object file dependent suport for ELF objects.
*/
#include <sys/mman.h>
#include <stdio.h>
#include <unistd.h>
#include <libelf.h>
#include <string.h>
#include <dlfcn.h>
#include <debug.h>
#include <libld.h>
#include "_rtld.h"
#include "_audit.h"
#include "_elf.h"
static Rt_map *olmp = NULL;
static Alist *mpalp = NULL;
static Ehdr dehdr = { { ELFMAG0, ELFMAG1, ELFMAG2, ELFMAG3,
M_CLASS, M_DATA }, 0, M_MACH, EV_CURRENT };
/*
* Process a relocatable object. The static object link map pointer is used as
* a flag to determine whether a concatenation is already in progress (ie. an
* LD_PRELOAD may specify a list of objects). The link map returned simply
* specifies an `object' flag which the caller can interpret and thus call
* elf_obj_fini() to complete the concatenation.
*/
static Rt_map *
elf_obj_init(Lm_list *lml, Aliste lmco, const char *oname)
{
Ofl_desc *ofl;
const char *name;
size_t lmsz;
/*
* Allocate the name of this object, as the original name may be
* associated with a data buffer that can be reused to load the
* dependencies needed to processes this object.
*/
if ((name = stravl_insert(oname, 0, 0, 0)) == NULL)
return (NULL);
/*
* Initialize an output file descriptor and the entrance criteria.
*/
if ((ofl = calloc(sizeof (Ofl_desc), 1)) == NULL)
return (NULL);
ofl->ofl_dehdr = &dehdr;
ofl->ofl_flags = (FLG_OF_DYNAMIC | FLG_OF_SHAROBJ | FLG_OF_STRIP);
ofl->ofl_flags1 = (FLG_OF1_RELDYN | FLG_OF1_TEXTOFF | FLG_OF1_MEMORY);
ofl->ofl_lml = lml;
/*
* As ent_setup() will effectively lazy load the necessary support
* libraries, make sure ld.so.1 is initialized for plt relocations.
* Then configure libld.so to process objects of the desired target
* type (this is the first call to libld.so, which will effectively
* lazyload it).
*/
if ((elf_rtld_load() == 0) || (ld_init_target(lml, M_MACH) != 0)) {
free(ofl);
return (NULL);
}
/*
* Obtain a generic set of entrance criteria, and generate a link map
* place holder and use the ELFPRV() element to maintain the output
* file descriptor.
*/
lmsz = S_DROUND(sizeof (Rt_map)) + sizeof (Rt_elfp);
if ((ld_ent_setup(ofl, syspagsz) == S_ERROR) ||
((olmp = calloc(lmsz, 1)) == NULL)) {
free(ofl);
return (NULL);
}
DBG_CALL(Dbg_file_elf(lml, name, 0, 0, lml->lm_lmidstr, lmco));
FLAGS(olmp) |= FLG_RT_OBJECT;
ELFPRV(olmp) = (void *)ofl;
/*
* Initialize string tables.
*/
if (ld_init_strings(ofl) == S_ERROR) {
free(ofl);
free(olmp);
olmp = NULL;
return (NULL);
}
/*
* Assign the output file name to be the initial object that got us
* here. This name is being used for diagnostic purposes only as we
* don't actually generate an output file unless debugging is enabled.
*/
ofl->ofl_name = name;
NAME(olmp) = (char *)name;
LIST(olmp) = lml;
lm_append(lml, lmco, olmp);
return (olmp);
}
/*
* Define a structure to retain the mapping information of the original
* relocatable object. Typically, mmapobj(2) maps a relocatable object into one
* mapping. However, if padding has been enabled by a debugger, then additional
* padding segments may have been added. elf_obj_file() needs to know which
* segment is the relocatable objects data, and retain the initial segment and
* the associated segment number for unmapping this object later (see
* elf_obj_fini()). Note, even if padding is enabled, the final shared object
* that is created by the link-editor for this relocatable object will have no
* associated padding, as ld(1) has no capabilities to provide padding.
*/
typedef struct {
mmapobj_result_t *md_mpp;
uint_t md_mnum;
} Mmap_desc;
/*
* Initial processing of a relocatable object. If this is the first object
* encountered we need to initialize some structures, then simply call the
* link-edit functionality to provide the initial processing of the file (ie.
* reads in sections and symbols, performs symbol resolution if more that one
* object file have been specified, and assigns input sections to output
* sections).
*/
Rt_map *
elf_obj_file(Lm_list *lml, Aliste lmco, Rt_map *clmp, const char *name,
mmapobj_result_t *hmpp, mmapobj_result_t *mpp, uint_t mnum)
{
Rej_desc rej;
Mmap_desc md;
/*
* If this is the first relocatable object (LD_PRELOAD could provide a
* list of objects), initialize an input file descriptor and a link map.
*/
if ((olmp == NULL) && ((olmp = elf_obj_init(lml, lmco, name)) == NULL))
return (NULL);
DBG_CALL(Dbg_util_nl(lml, DBG_NL_STD));
/*
* Keep track of the input image, as this must be free'd after all ELF
* processing is completed.
*/
md.md_mpp = mpp;
md.md_mnum = mnum;
if (alist_append(&mpalp, &md, sizeof (Mmap_desc),
AL_CNT_MPOBJS) == NULL) {
remove_so(lml, olmp, clmp);
return (NULL);
}
/*
* Pass the object mapping to the link-editor to commence processing the
* file.
*/
if (ld_process_mem(name, name, hmpp->mr_addr, hmpp->mr_msize,
(Ofl_desc *)ELFPRV(olmp), &rej) == (Ifl_desc *)S_ERROR) {
remove_so(lml, olmp, clmp);
return (NULL);
}
return (olmp);
}
/*
* Ensure any platform or machine capability names are valid.
*/
inline static int
check_plat_names(Syscapset *scapset, Alist *caps, Rej_desc *rej)
{
Capstr *capstr;
Aliste idx;
for (ALIST_TRAVERSE(caps, idx, capstr)) {
if (platcap_check(scapset, capstr->cs_str, rej) == 1)
return (1);
}
return (0);
}
inline static int
check_mach_names(Syscapset *scapset, Alist *caps, Rej_desc *rej)
{
Capstr *capstr;
Aliste idx;
for (ALIST_TRAVERSE(caps, idx, capstr)) {
if (machcap_check(scapset, capstr->cs_str, rej) == 1)
return (1);
}
return (0);
}
/*
* Finish relocatable object processing. Having already initially processed one
* or more objects, complete the generation of a shared object image by calling
* the appropriate link-edit functionality (refer to sgs/ld/common/main.c).
*/
Rt_map *
elf_obj_fini(Lm_list *lml, Rt_map *lmp, Rt_map *clmp, int *in_nfavl)
{
Ofl_desc *ofl = (Ofl_desc *)ELFPRV(lmp);
Rt_map *nlmp, *tlmp;
Ehdr *ehdr;
Phdr *phdr;
mmapobj_result_t *mpp, *hmpp;
uint_t phnum;
int mnum;
Lm_cntl *lmc;
Aliste idx1;
Mmap_desc *mdp;
Fdesc fd = { 0 };
Grp_hdl *ghp;
Rej_desc rej = { 0 };
Syscapset *scapset;
elfcap_mask_t omsk;
Alist *oalp;
DBG_CALL(Dbg_util_nl(lml, DBG_NL_STD));
if (ld_reloc_init(ofl) == S_ERROR)
return (NULL);
if (ld_sym_validate(ofl) == S_ERROR)
return (NULL);
/*
* At this point, all input section processing is complete. If any
* capabilities have been established, ensure that they are appropriate
* for this system.
*/
if (pnavl_recorded(&capavl, ofl->ofl_name, 0, NULL))
scapset = alt_scapset;
else
scapset = org_scapset;
if ((((omsk = ofl->ofl_ocapset.oc_hw_1.cm_val) != 0) &&
(hwcap1_check(scapset, omsk, &rej) == 0)) ||
(((omsk = ofl->ofl_ocapset.oc_sf_1.cm_val) != 0) &&
(sfcap1_check(scapset, omsk, &rej) == 0)) ||
(((omsk = ofl->ofl_ocapset.oc_hw_2.cm_val) != 0) &&
(hwcap2_check(scapset, omsk, &rej) == 0)) ||
(((oalp = ofl->ofl_ocapset.oc_plat.cl_val) != NULL) &&
(check_plat_names(scapset, oalp, &rej) == 0)) ||
(((oalp = ofl->ofl_ocapset.oc_mach.cl_val) != NULL) &&
(check_mach_names(scapset, oalp, &rej) == 0))) {
if ((lml_main.lm_flags & LML_FLG_TRC_LDDSTUB) && lmp &&
(FLAGS1(lmp) & FL1_RT_LDDSTUB) && (NEXT(lmp) == NULL)) {
/* LINTED */
(void) printf(MSG_INTL(ldd_reject[rej.rej_type]),
ofl->ofl_name, rej.rej_str);
}
return (NULL);
}
/*
* Finish creating the output file.
*/
if (ld_make_sections(ofl) == S_ERROR)
return (NULL);
if (ld_create_outfile(ofl) == S_ERROR)
return (NULL);
if (ld_update_outfile(ofl) == S_ERROR)
return (NULL);
if (ld_reloc_process(ofl) == S_ERROR)
return (NULL);
/*
* At this point we have a memory image of the shared object. The link
* editor would normally simply write this to the required output file.
* If we're debugging generate a standard temporary output file.
*/
DBG_CALL(Dbg_file_output(ofl));
/*
* Allocate a mapping array to retain mapped segment information.
*/
ehdr = ofl->ofl_nehdr;
phdr = ofl->ofl_phdr;
if ((mpp = hmpp = calloc(ehdr->e_phnum,
sizeof (mmapobj_result_t))) == NULL)
return (NULL);
for (mnum = 0, phnum = 0; phnum < ehdr->e_phnum; phnum++) {
if (phdr[phnum].p_type != PT_LOAD)
continue;
mpp[mnum].mr_addr = (caddr_t)((uintptr_t)phdr[phnum].p_vaddr +
(uintptr_t)ehdr);
mpp[mnum].mr_msize = phdr[phnum].p_memsz;
mpp[mnum].mr_fsize = phdr[phnum].p_filesz;
mpp[mnum].mr_prot = (PROT_READ | PROT_WRITE | PROT_EXEC);
mnum++;
}
/*
* Generate a new link map representing the memory image created.
*/
fd.fd_nname = ofl->ofl_name;
if ((nlmp = elf_new_lmp(lml, CNTL(olmp), &fd, (Addr)hmpp->mr_addr,
ofl->ofl_size, NULL, clmp, in_nfavl)) == NULL)
return (NULL);
MMAPS(nlmp) = hmpp;
MMAPCNT(nlmp) = mnum;
PADSTART(nlmp) = (ulong_t)hmpp->mr_addr;
PADIMLEN(nlmp) = mpp->mr_addr + mpp->mr_msize - hmpp->mr_addr;
/*
* Replace the original (temporary) link map with the new link map.
*/
/* LINTED */
lmc = (Lm_cntl *)alist_item_by_offset(lml->lm_lists, CNTL(nlmp));
lml->lm_obj--;
if ((tlmp = PREV_RT_MAP(nlmp)) == olmp)
tlmp = nlmp;
if (PREV(olmp)) {
NEXT(PREV_RT_MAP(olmp)) = (Link_map *)nlmp;
PREV(nlmp) = PREV(olmp);
} else {
PREV(nlmp) = NULL;
lmc->lc_head = nlmp;
if (CNTL(nlmp) == ALIST_OFF_DATA)
lml->lm_head = nlmp;
}
if (NEXT(olmp) != (Link_map *)nlmp) {
NEXT(nlmp) = NEXT(olmp);
PREV(NEXT_RT_MAP(olmp)) = (Link_map *)nlmp;
}
NEXT(tlmp) = NULL;
lmc->lc_tail = tlmp;
if (CNTL(nlmp) == ALIST_OFF_DATA)
lml->lm_tail = tlmp;
HANDLES(nlmp) = HANDLES(olmp);
GROUPS(nlmp) = GROUPS(olmp);
STDEV(nlmp) = STDEV(olmp);
STINO(nlmp) = STINO(olmp);
FLAGS(nlmp) |= ((FLAGS(olmp) & ~FLG_RT_OBJECT) | FLG_RT_IMGALLOC);
FLAGS1(nlmp) |= FLAGS1(olmp);
MODE(nlmp) |= MODE(olmp);
NAME(nlmp) = NAME(olmp);
/*
* Reassign any original handles to the new link-map.
*/
for (APLIST_TRAVERSE(HANDLES(nlmp), idx1, ghp)) {
Grp_desc *gdp;
Aliste idx2;
ghp->gh_ownlmp = nlmp;
for (ALIST_TRAVERSE(ghp->gh_depends, idx2, gdp)) {
if (gdp->gd_depend == olmp) {
gdp->gd_depend = nlmp;
break;
}
}
}
ld_ofl_cleanup(ofl);
free(ELFPRV(olmp));
free(olmp);
olmp = 0;
/*
* Unmap the original relocatable object.
*/
for (ALIST_TRAVERSE(mpalp, idx1, mdp)) {
unmap_obj(mdp->md_mpp, mdp->md_mnum);
free(mdp->md_mpp);
}
free(mpalp);
mpalp = NULL;
/*
* Now that we've allocated our permanent link map structure, expand the
* PATHNAME() and insert this path name into the FullPathNode AVL tree.
*/
(void) fullpath(nlmp, 0);
if (fpavl_insert(lml, nlmp, PATHNAME(nlmp), 0) == 0)
return (NULL);
/*
* If we're being audited tell the audit library of the file we've just
* opened.
*/
if ((lml->lm_tflags | AFLAGS(nlmp)) & LML_TFLG_AUD_MASK) {
if (audit_objopen(nlmp, nlmp) == 0)
return (NULL);
}
return (nlmp);
}
|