Merge commit '9276b3991ba20d5a5660887ba81b0bc7bed25a0c'
[unleashed.git] / share / man / man3c / dldump.3c
blob504fe7b71b7f989db16c65f0fcb381369cd08362
1 '\" te
2 .\"  Copyright (c) 2004 by Sun Microsystems, Inc.  All rights reserved.
3 .\" 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.
4 .\" 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.
5 .\" 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]
6 .TH DLDUMP 3C "Sep 7, 2015"
7 .SH NAME
8 dldump \- create a new file from a dynamic object component of the calling
9 process
10 .SH SYNOPSIS
11 .LP
12 .nf
13 #include <dlfcn.h>
15 \fBint\fR \fBdldump\fR(\fBconst char *\fR\fIipath\fR, \fBconst char *\fR\fIopath\fR, \fBint\fR \fIflags\fR);
16 .fi
18 .SH DESCRIPTION
19 .LP
20 The \fBdldump()\fR function creates a new dynamic object \fIopath\fR from an
21 existing dynamic object \fIipath\fR that is bound to the current process. An
22 \fIipath\fR value of \fB0\fR is interpreted as the dynamic object that started
23 the process. The new object is constructed from the existing objects' disc
24 file. Relocations can be applied to the new object to pre-bind it to other
25 dynamic objects, or fix the object to a specific memory location. In addition,
26 data elements within the new object can be obtained from the objects' memory
27 image as this data exists in the calling process.
28 .sp
29 .LP
30 These techniques allow the new object to be executed with a lower startup cost.
31 This reduction can be because of less relocations being required to load the
32 object, or because of a reduction in the data processing requirements of the
33 object. However, limitations can exist in using these techniques. The
34 application of relocations to the new dynamic object \fIopath\fR can restrict
35 its flexibility within a dynamically changing environment. In addition,
36 limitations in regards to data usage can make dumping a memory image
37 impractical. See \fBEXAMPLES\fR.
38 .sp
39 .LP
40 The runtime linker verifies that the dynamic object \fIipath\fR is mapped as
41 part of the current process. Thus, the object must either be the dynamic object
42 that started the process, one of the process's dependencies, or an object that
43 has been preloaded. See \fBexec\fR(2), and \fBld.so.1\fR(1).
44 .sp
45 .LP
46 As part of the runtime processing of a dynamic object, \fIrelocation\fR records
47 within the object are interpreted and applied to offsets within the object.
48 These offsets are said to be \fIrelocated\fR. Relocations can be categorized
49 into two basic types: \fInon-symbolic\fR and \fIsymbolic\fR.
50 .sp
51 .LP
52 The \fInon-symbolic\fR relocation is a simple \fIrelative\fR relocation that
53 requires the base address at which the object is mapped to perform the
54 relocation. The \fIsymbolic\fR relocation requires the address of an associated
55 symbol, and results in a \fIbinding\fR to the dynamic object that defines this
56 symbol. The symbol definition can originate from any of the dynamic objects
57 that make up the process, that is, the object that started the process, one of
58 the process's dependencies, an object that has been preloaded, or the dynamic
59 object being relocated.
60 .sp
61 .LP
62 The \fIflags\fR parameter controls the relocation processing and other
63 attributes of producing the new dynamic object \fIopath\fR. Without any
64 \fIflags\fR, the new object is constructed solely from the contents of the
65 \fIipath\fR disc file without any relocations applied.
66 .sp
67 .LP
68 Various relocation flags can be \fBor\fR'ed into the \fIflags\fR parameter to
69 affect the relocations that are applied to the new object. \fINon-symbolic\fR
70 relocations can be applied using the following:
71 .sp
72 .ne 2
73 .na
74 \fB\fBRTLD_REL_RELATIVE\fR\fR
75 .ad
76 .RS 21n
77 Relocation records from the object \fIipath\fR, that define \fIrelative\fR
78 relocations, are applied to the object \fIopath\fR.
79 .RE
81 .sp
82 .LP
83 A variety of \fIsymbolic\fR relocations can be applied using the following
84 flags (each of these flags also implies  \fBRTLD_REL_RELATIVE\fR is in effect):
85 .sp
86 .ne 2
87 .na
88 \fB\fBRTLD_REL_EXEC\fR\fR
89 .ad
90 .RS 20n
91 Symbolic relocations that result in binding \fIipath\fR to the dynamic object
92 that started the process, commonly a dynamic executable, are applied to the
93 object \fIopath\fR.
94 .RE
96 .sp
97 .ne 2
98 .na
99 \fB\fBRTLD_REL_DEPENDS\fR\fR
101 .RS 20n
102 Symbolic relocations that result in binding \fIipath\fR to any of the dynamic
103 dependencies of the process are applied to the object \fIopath\fR.
107 .ne 2
109 \fB\fBRTLD_REL_PRELOAD\fR\fR
111 .RS 20n
112 Symbolic relocations that result in binding \fIipath\fR to any objects
113 preloaded with the process are applied to the object \fIopath\fR. See
114 \fBLD_PRELOAD\fR in \fBld.so.1\fR(1).
118 .ne 2
120 \fB\fBRTLD_REL_SELF\fR\fR
122 .RS 20n
123 Symbolic relocations that result in binding \fIipath\fR to itself, are applied
124 to the object \fIopath\fR.
128 .ne 2
130 \fB\fBRTLD_REL_WEAK\fR\fR
132 .RS 20n
133 Weak relocations that remain unresolved are applied to the object \fIopath\fR
134 as \fB0\fR.
138 .ne 2
140 \fB\fBRTLD_REL_ALL\fR\fR
142 .RS 20n
143 \fIAll\fR relocation records defined in the object \fIipath\fR are applied to
144 the new object \fIopath\fR. This is basically a concatenation of all the above
145 relocation flags.
150 Note that for dynamic executables, \fBRTLD_REL_RELATIVE\fR,
151 \fBRTLD_REL_EXEC\fR, and \fBRTLD_REL_SELF\fR have no effect. See
152 \fBEXAMPLES\fR.
155 If relocations, knowledgeable of the base address of the mapped object, are
156 applied to the new object \fIopath\fR, then the new object becomes fixed to the
157 location that the \fIipath\fR image is mapped within the current process.
160 Any relocations applied to the new object \fIopath\fR will have the original
161 relocation record removed so that the relocation will not be applied more than
162 once. Otherwise, the new object \fIopath\fR will retain the relocation records
163 as they exist in the \fIipath\fR disc file.
166 The following additional attributes for creating the new dynamic object
167 \fIopath\fR can be specified using the \fIflags\fR parameter:
169 .ne 2
171 \fB\fBRTLD_MEMORY\fR\fR
173 .RS 15n
174 The new object \fIopath\fR is constructed from the current memory contents of
175 the \fIipath\fR image as it exists in the calling process. This option allows
176 data modified by the calling process to be captured in the new object. Note
177 that not all data modifications may be applicable for capture; significant
178 restrictions exist in using this technique. See \fBEXAMPLES\fR. By default,
179 when processing a dynamic executable, any allocated memory that follows the end
180 of the data segment is captured in the new object (see \fBmalloc\fR(3C) and
181 \fBbrk\fR(2)). This data, which represents the process heap, is saved as a new
182 \fI\&.SUNW_heap\fR section in the object \fIopath\fR. The objects' program
183 headers and symbol entries, such as \fB_end\fR, are adjusted accordingly. See
184 also \fBRTLD_NOHEAP.\fR When using this attribute, any relocations that have
185 been applied to the \fIipath\fR memory image that do not fall into one of the
186 requested relocation categories are undone, that is, the relocated element is
187 returned to the value as it existed in the \fIipath\fR disc file.
191 .ne 2
193 \fB\fBRTLD_STRIP\fR\fR
195 .RS 15n
196 Only collect allocatable sections within the object \fIopath\fR. Sections that
197 are not part of the dynamic objects' memory image are removed. \fBRTLD_STRIP\fR
198 reduces the size of the \fIopath\fR disc file and is comparable to having run
199 the new object through \fBstrip\fR(1).
203 .ne 2
205 \fB\fBRTLD_NOHEAP\fR\fR
207 .RS 15n
208 Do not save any heap to the new object. This option is only meaningful when
209 processing a dynamic executable with the \fBRTLD_MEMORY\fR attribute and allows
210 for reducing the size of the \fIopath\fR disc file. The executable must confine
211 its data initialization to data elements within its data segment, and must not
212 use any allocated data elements that comprise the heap.
217 It should be emphasized, that an object created by \fBdldump()\fR is simply an
218 updated \fBELF\fR object file. No additional state regarding the process at the
219 time \fBdldump()\fR is called is maintained in the new object. \fBdldump()\fR
220 does not provide a panacea for checkpoint and resume. A new dynamic executable,
221 for example, will not start where the original executable called
222 \fBdldump()\fR. It will gain control at the executable's normal entry point.
223 See \fBEXAMPLES\fR.
224 .SH RETURN VALUES
226 On successful creation of the new object, \fBdldump()\fR returns \fB0\fR.
227 Otherwise, a non-zero value is returned and more detailed diagnostic
228 information is available through \fBdlerror()\fR.
229 .SH EXAMPLES
231 \fBExample 1 \fRSample code using \fBdldump()\fR.
234 The following code fragment, which can be part of a dynamic executable
235 \fBa.out\fR, can be used to create a new shared object from one of the dynamic
236 executables' dependencies \fBlibfoo.so.1\fR:
239 .in +2
241 const char *    ipath = "libfoo.so.1";
242 const char *    opath = "./tmp/libfoo.so.1";
243 \&...
244 if (dldump(ipath, opath, RTLD_REL_RELATIVE) != 0)
245         (void) printf("dldump failed: %s\en", dlerror(\|));
247 .in -2
251 The new shared object \fIopath\fR is fixed to the address of the mapped
252 \fIipath\fR bound to the dynamic executable \fBa.out\fR. All relative
253 relocations are applied to this new shared object, which will reduce its
254 relocation overhead when it is used as part of another process.
258 By performing only relative relocations, any symbolic relocation records remain
259 defined within the new object, and thus the dynamic binding to external symbols
260 will be preserved when the new object is used.
264 Use of the other relocation flags can fix specific relocations in the new
265 object and thus can reduce even more the runtime relocation startup cost of the
266 new object. However, this will also restrict the flexibility of using the new
267 object within a dynamically changing environment, as it will bind the new
268 object to some or all of the dynamic objects presently mapped as part of the
269 process.
273 For example, the use of \fBRTLD_REL_SELF\fR will cause any references to
274 symbols from \fIipath\fR to be bound to definitions within itself if no other
275 preceding object defined the same symbol. In other words, a call to
276 \fIfoo(\|)\fR within \fIipath\fR will bind to the definition \fIfoo\fR within
277 the same object. Therefore, \fIopath\fR will have one less binding that must be
278 computed at runtime. This reduces the startup cost of using \fIopath\fR by
279 other applications; however, interposition of the symbol \fIfoo\fR will no
280 longer be possible.
284 Using a dumped shared object with applied relocations as an applications
285 dependency normally requires that the application have the same dependencies as
286 the application that produced the dumped image. Dumping shared objects, and the
287 various flags associated with relocation processing, have some specialized
288 uses. However, the technique is intended as a building block for future
289 technology.
293 The following code fragment, which is part of the dynamic executable
294 \fBa.out\fR, can be used to create a new version of the dynamic executable:
297 .in +2
299 static char *       dumped = 0;
300 const char *        opath = "./a.out.new";
301 \&...
302 if (dumped == 0) {
303         char        buffer[100];
304         int         size;
305         time_t      seconds;
306         ...
307         /* Perform data initialization */
308         seconds = time((time_t *)0);
309         size = cftime(buffer, (char *)0, &seconds);
310         if ((dumped = (char *)malloc(size + 1)) == 0) {
311                 (void) printf("malloc failed: %s\en", strerror(errno));
312                 return (1);
313         }
314         (void) strcpy(dumped, buffer);
315         ...
316         /*
317          * Tear down any undesirable data initializations and
318          * dump the dynamic executables memory image.
319          */
320         _exithandle(\|);
321         _exit(dldump(0, opath, RTLD_MEMORY));
323 (void) printf("Dumped: %s\en", dumped);
325 .in -2
329 Any modifications made to the dynamic executable, up to the point the
330 \fBdldump()\fR call is made, are saved in the new object \fBa.out.new\fR. This
331 mechanism allows the executable to update parts of its data segment and heap
332 prior to creating the new object. In this case, the date the executable is
333 dumped is saved in the new object. The new object can then be executed without
334 having to carry out the same (presumably expensive) initialization.
338 For greatest flexibility, this example does not save \fIany\fR relocated
339 information. The elements of the dynamic executable \fIipath\fR that have been
340 modified by relocations at process startup, that is, references to external
341 functions, are returned to the values of these elements as they existed in the
342 \fIipath\fR disc file. This preservation of relocation records allows the new
343 dynamic executable to be flexible, and correctly bind and initialize to its
344 dependencies when executed on the same or newer upgrades of the \fBOS\fR.
348 Fixing relocations by applying some of the relocation flags would bind the new
349 object to the dependencies presently mapped as part of the process calling
350 \fBdldump()\fR. It may also remove necessary copy relocation processing
351 required for the correct initialization of its shared object dependencies.
352 Therefore, if the new dynamic executables' dependencies have no specialized
353 initialization requirements, the executable may still only interact correctly
354 with the dependencies to which it binds if they were mapped to the same
355 locations as they were when \fBdldump()\fR was called.
359 Note that for dynamic executables, \fBRTLD_REL_RELATIVE,\fR
360 \fBRTLD_REL_EXEC,\fR and \fBRTLD_REL_SELF\fR have no effect, as relocations
361 within the dynamic executable will have been fixed when it was created by
362 \fBld\fR(1).
366 When \fBRTLD_MEMORY\fR is used, care should be taken to insure that dumped data
367 sections that reference external objects are not reused without appropriate
368 re-initialization. For example, if a data item contains a file descriptor, a
369 variable returned from a shared object, or some other external data, and this
370 data item has been initialized prior to the \fBdldump()\fR call, its value will
371 have no meaning in the new dumped image.
375 When \fBRTLD_MEMORY\fR is used, any modification to a data item that is
376 initialized via a relocation whose relocation record will be retained in the
377 new image will effectively be lost or invalidated within the new image. For
378 example, if a pointer to an external object is incremented prior to the
379 \fBdldump()\fR call, this data item will be reset to its disc file contents so
380 that it can be relocated when the new image is used; hence, the previous
381 increment is lost.
385 Non-idempotent data initializations may prevent the use of \fBRTLD_MEMORY\fR.
386 For example, the addition of elements to a linked-list via \fBinit\fR sections
387 can result in the linked-list data being captured in the new image. Running
388 this new image may result in \fBinit\fR sections continuing to add new elements
389 to the list without the prerequisite initialization of the list head. It is
390 recommended that \fB_exithandle\fR(3C) be called before \fBdldump()\fR to tear
391 down any data initializations established via initialization code.  Note that
392 this may invalidate the calling image; thus, following the call to
393 \fBdldump()\fR, only a call to \fB_Exit\fR(2) should be made.
395 .SH USAGE
397 The \fBdldump()\fR function is one of a family of functions that give the user
398 direct access to the dynamic linking facilities. These facilities are available
399 to dynamically-linked processes only. See \fILinker and Libraries Guide\fR).
400 .SH ATTRIBUTES
402 See \fBattributes\fR(5) for descriptions of the following attributes:
407 box;
408 c | c
409 l | l .
410 ATTRIBUTE TYPE  ATTRIBUTE VALUE
412 MT-Level        MT-Safe
415 .SH SEE ALSO
417 \fBld\fR(1), \fBld.so.1\fR(1), \fBstrip\fR(1), \fB_Exit\fR(2), \fBbrk\fR(2),
418 \fBexec\fR(2), \fB_exithandle\fR(3C), \fBdladdr\fR(3C), \fBdlclose\fR(3C),
419 \fBdlerror\fR(3C), \fBdlopen\fR(3C), \fBdlsym\fR(3C), \fBend\fR(3C),
420 \fBmalloc\fR(3C), \fBattributes\fR(5)
423 \fILinker and Libraries Guide\fR
424 .SH NOTES
426 These functions are available to dynamically-linked processes only.
429 Any \fBNOBITS\fR sections within the \fIipath\fR are expanded to \fBPROGBITS\fR
430 sections within the \fIopath\fR. \fBNOBITS\fR sections occupy no space within
431 an \fBELF\fR file image. \fBNOBITS\fR sections declare memory that must be
432 created and zero-filled when the object is mapped into the runtime environment.
433 \fB\&.bss\fR is a typical example of this section type. \fBPROGBITS\fR
434 sections, on the other hand, hold information defined by the object within the
435 \fBELF\fR file image. This section conversion reduces the runtime
436 initialization cost of the new dumped object but increases the objects' disc
437 space requirement.
440 When a shared object is dumped, and relocations are applied which are
441 knowledgeable of the base address of the mapped object, the new object is fixed
442 to this new base address. The dumped object has its \fBELF\fR type reclassified
443 to be a dynamic executable. The dumped object can be processed by the runtime
444 linker, but is not valid as input to the link-editor.
447 If relocations are applied to the new object, any remaining relocation records
448 are reorganized for better locality of reference. The relocation sections are
449 renamed to \fB\&.SUNW_reloc\fR and the association with the section to
450 relocate, is lost. Only the offset of the relocation record is meaningful.
451 \fB\&.SUNW_reloc\fR relocations do not make the new object invalid to either
452 the runtime linker or link-editor, but can reduce the objects analysis with
453 some \fBELF\fR readers.