summaryrefslogtreecommitdiff
path: root/usr/src/cmd/sgs/rtld/common/object.c
blob: 309940d552b259498047fb99f382d5f4f8753a38 (plain)
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
/*
 * 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 2006 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */
#pragma ident	"%Z%%M%	%I%	%E% SMI"

/*
 * Object file dependent suport for ELF objects.
 */
#include	"_synonyms.h"

#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 = 0;

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 *name)
{
	Ofl_desc *	ofl;

	/*
	 * Initialize an output file descriptor and the entrance criteria.
	 */
	if ((ofl = (Ofl_desc *)calloc(sizeof (Ofl_desc), 1)) == 0)
		return (0);

	ofl->ofl_dehdr = &dehdr;

	ofl->ofl_flags =
	    (FLG_OF_DYNAMIC | FLG_OF_SHAROBJ | FLG_OF_STRIP | FLG_OF_MEMORY);
	ofl->ofl_flags1 = FLG_OF1_RELDYN | FLG_OF1_TEXTOFF;
	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.
	 */
	if (elf_rtld_load() == 0)
		return (0);

	/*
	 * Obtain a generic set of entrance criteria (this is the first call to
	 * libld.so, which will effectively lazyload it).
	 */
	if (ld_ent_setup(ofl, syspagsz) == S_ERROR)
		return (0);

	/*
	 * Generate a link map place holder and use the `rt_priv' element to
	 * maintain the output file descriptor.
	 */
	if ((olmp = (Rt_map *)calloc(sizeof (Rt_map), 1)) == 0)
		return (0);

	DBG_CALL(Dbg_file_elf(lml, name, 0, 0, 0, 0, lml->lm_lmidstr, lmco));
	FLAGS(olmp) |= FLG_RT_OBJECT;
	olmp->rt_priv = (void *)ofl;

	/*
	 * Initialize string tables.
	 */
	if (ld_init_strings(ofl) == S_ERROR)
		return (0);

	/*
	 * 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;
	ORIGNAME(olmp) = PATHNAME(olmp) = NAME(olmp) = (char *)name;
	LIST(olmp) = lml;

	lm_append(lml, lmco, olmp);
	return (olmp);
}

/*
 * 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, const char *name, int fd)
{
	Rej_desc	rej;

	/*
	 * 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) {
		/*
		 * Load the link-editor library.
		 */
		if ((olmp = elf_obj_init(lml, lmco, name)) == 0)
			return (0);
	}

	/*
	 * Proceed to process the input file.
	 */
	DBG_CALL(Dbg_util_nl(lml, DBG_NL_STD));
	if (ld_process_open(name, 0, fd, (Ofl_desc *)olmp->rt_priv,
	    NULL, &rej) == (Ifl_desc *)S_ERROR)
		return (0);
	return (olmp);
}

/*
 * 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)
{
	Ofl_desc	*ofl = (Ofl_desc *)lmp->rt_priv;
	Rt_map		*nlmp;
	Addr		etext;
	Ehdr		*ehdr;
	Phdr		*phdr;
	Mmap		*mmaps;
	uint_t		phnum, mmapcnt;
	Lm_cntl 	*lmc;

	DBG_CALL(Dbg_util_nl(lml, DBG_NL_STD));

	if (ld_reloc_init(ofl) == S_ERROR)
		return (0);
	if (ld_sym_validate(ofl) == S_ERROR)
		return (0);
	if (ld_make_sections(ofl) == S_ERROR)
		return (0);
	if (ld_create_outfile(ofl) == S_ERROR)
		return (0);
	if ((etext = ld_update_outfile(ofl)) == (Addr)S_ERROR)
		return (0);
	if (ld_reloc_process(ofl) == S_ERROR)
		return (0);

	/*
	 * 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 ((mmaps = calloc(ehdr->e_phnum, sizeof (Mmap))) == 0)
		return (0);
	for (mmapcnt = 0, phnum = 0; phnum < ehdr->e_phnum; phnum++) {
		if (phdr[phnum].p_type != PT_LOAD)
			continue;

		mmaps[mmapcnt].m_vaddr = (caddr_t)
		    (phdr[phnum].p_vaddr + (ulong_t)ehdr);
		mmaps[mmapcnt].m_msize = phdr[phnum].p_memsz;
		mmaps[mmapcnt].m_fsize = phdr[phnum].p_filesz;
		mmaps[mmapcnt].m_perm = (PROT_READ | PROT_WRITE | PROT_EXEC);
		mmapcnt++;
	}

	/*
	 * Generate a new link map representing the memory image created.
	 */
	if ((nlmp = elf_new_lm(lml, ofl->ofl_name, ofl->ofl_name,
	    ofl->ofl_osdynamic->os_outdata->d_buf, (ulong_t)ehdr,
	    (ulong_t)ehdr + etext, CNTL(olmp), (ulong_t)ofl->ofl_size,
	    0, 0, 0, mmaps, mmapcnt)) == 0)
		return (0);

	/*
	 * Remove this link map from the end of the link map list and copy its
	 * contents into the link map originally created for this file (we copy
	 * the contents rather than manipulate the link map pointers as parts
	 * of the dlopen code have remembered the original link map address).
	 */
	NEXT((Rt_map *)PREV(nlmp)) = 0;
	/* LINTED */
	lmc = (Lm_cntl *)((char *)lml->lm_lists + CNTL(nlmp));
	lmc->lc_tail = (Rt_map *)PREV(nlmp);
	if (CNTL(nlmp) == ALO_DATA)
		lml->lm_tail = (Rt_map *)PREV(nlmp);
	lml->lm_obj--;

	PREV(nlmp) = PREV(olmp);
	NEXT(nlmp) = NEXT(olmp);
	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);
	PATHNAME(nlmp) = PATHNAME(olmp);
	ORIGNAME(nlmp) = ORIGNAME(olmp);
	DIRSZ(nlmp) = DIRSZ(olmp);

	ld_ofl_cleanup(ofl);
	free(olmp->rt_priv);
	(void) memcpy(olmp, nlmp, sizeof (Rt_map));
	free(nlmp);
	nlmp = olmp;
	olmp = 0;

	/*
	 * Now that we've allocated our permanent Rt_map structure, expand the
	 * PATHNAME() and insert it into the FullpathNode AVL tree
	 */
	if (FLAGS1(nlmp) & FL1_RT_RELATIVE)
		(void) fullpath(nlmp, 0);
	if (fpavl_insert(lml, nlmp, PATHNAME(nlmp), 0) == 0)
		return (0);

	/*
	 * If we're being audited tell the audit library of the file we've just
	 * opened.
	 */
	if ((lml->lm_tflags | FLAGS1(nlmp)) & LML_TFLG_AUD_MASK) {
		if (audit_objopen(lmp, lmp) == 0)
			return (0);
	}
	return (nlmp);
}