/
dlfcns.c
2366 lines (2046 loc) · 60.6 KB
/
dlfcns.c
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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* 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) 1988 AT&T
* All Rights Reserved
*
* Copyright (c) 1990, 2010, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, Joyent, Inc. All rights reserved.
*/
/*
* Programmatic interface to the run_time linker.
*/
#include <sys/debug.h>
#include <stdio.h>
#include <string.h>
#include <dlfcn.h>
#include <synch.h>
#include <limits.h>
#include <debug.h>
#include <conv.h>
#include "_rtld.h"
#include "_audit.h"
#include "_elf.h"
#include "_inline_gen.h"
#include "msg.h"
/*
* Determine who called us - given a pc determine in which object it resides.
*
* For dlopen() the link map of the caller must be passed to load_so() so that
* the appropriate search rules (4.x or 5.0) are used to locate any
* dependencies. Also, if we've been called from a 4.x module it may be
* necessary to fix the specified pathname so that it conforms with the 5.0 elf
* rules.
*
* For dlsym() the link map of the caller is used to determine RTLD_NEXT
* requests, together with requests based off of a dlopen(0).
* For dladdr() this routines provides a generic means of scanning all loaded
* segments.
*/
Rt_map *
_caller(caddr_t cpc, int flags)
{
Lm_list *lml;
Aliste idx1;
for (APLIST_TRAVERSE(dynlm_list, idx1, lml)) {
Aliste idx2;
Lm_cntl *lmc;
for (ALIST_TRAVERSE(lml->lm_lists, idx2, lmc)) {
Rt_map *lmp;
for (lmp = lmc->lc_head; lmp;
lmp = NEXT_RT_MAP(lmp)) {
if (find_segment(cpc, lmp))
return (lmp);
}
}
}
/*
* No mapping can be determined. If asked for a default, assume this
* is from the executable.
*/
if (flags & CL_EXECDEF)
return ((Rt_map *)lml_main.lm_head);
return (0);
}
#pragma weak _dlerror = dlerror
/*
* External entry for dlerror(3dl). Returns a pointer to the string describing
* the last occurring error. The last occurring error is cleared.
*/
char *
dlerror()
{
char *error;
Rt_map *clmp;
int entry;
entry = enter(0);
clmp = _caller(caller(), CL_EXECDEF);
DBG_CALL(Dbg_dl_dlerror(clmp, lasterr));
error = lasterr;
lasterr = NULL;
if (entry)
leave(LIST(clmp), 0);
return (error);
}
/*
* Add a dependency as a group descriptor to a group handle. Returns 0 on
* failure. On success, returns the group descriptor, and if alep is non-NULL
* the *alep is set to ALE_EXISTS if the dependency already exists, or to
* ALE_CREATE if the dependency is newly created.
*/
Grp_desc *
hdl_add(Grp_hdl *ghp, Rt_map *lmp, uint_t dflags, int *alep)
{
Grp_desc *gdp;
Aliste idx;
int ale = ALE_CREATE;
uint_t oflags;
/*
* Make sure this dependency hasn't already been recorded.
*/
for (ALIST_TRAVERSE(ghp->gh_depends, idx, gdp)) {
if (gdp->gd_depend == lmp) {
ale = ALE_EXISTS;
break;
}
}
if (ale == ALE_CREATE) {
Grp_desc gd;
/*
* Create a new handle descriptor.
*/
gd.gd_depend = lmp;
gd.gd_flags = 0;
/*
* Indicate this object is a part of this handles group.
*/
if (aplist_append(&GROUPS(lmp), ghp, AL_CNT_GROUPS) == NULL)
return (NULL);
/*
* Append the new dependency to this handle.
*/
if ((gdp = alist_append(&ghp->gh_depends, &gd,
sizeof (Grp_desc), AL_CNT_DEPENDS)) == NULL)
return (NULL);
}
oflags = gdp->gd_flags;
gdp->gd_flags |= dflags;
if (DBG_ENABLED) {
if (ale == ALE_CREATE)
DBG_CALL(Dbg_file_hdl_action(ghp, lmp, DBG_DEP_ADD,
gdp->gd_flags));
else if (gdp->gd_flags != oflags)
DBG_CALL(Dbg_file_hdl_action(ghp, lmp, DBG_DEP_UPDATE,
gdp->gd_flags));
}
if (alep)
*alep = ale;
return (gdp);
}
/*
* Create a handle.
*
* rlmp - represents the reference link-map for which the handle is being
* created.
* clmp - represents the caller who is requesting the handle.
* hflags - provide group handle flags (GPH_*) that affect the use of the
* handle, such as dlopen(0), or use or use of RTLD_FIRST.
* rdflags - provide group dependency flags for the reference link-map rlmp,
* such as whether the dependency can be used for dlsym(), can be
* relocated against, or whether this objects dependencies should
* be processed.
* cdflags - provide group dependency flags for the caller.
*/
Grp_hdl *
hdl_create(Lm_list *lml, Rt_map *rlmp, Rt_map *clmp, uint_t hflags,
uint_t rdflags, uint_t cdflags)
{
Grp_hdl *ghp = NULL, *aghp;
APlist **alpp;
Aliste idx;
/*
* For dlopen(0) the handle is maintained as part of the link-map list,
* otherwise the handle is associated with the reference link-map.
*/
if (hflags & GPH_ZERO)
alpp = &(lml->lm_handle);
else
alpp = &(HANDLES(rlmp));
/*
* Objects can contain multiple handles depending on the handle flags
* supplied. Most RTLD flags pertain to the object itself and the
* bindings that it can achieve. Multiple handles for these flags
* don't make sense. But if the flag determines how the handle might
* be used, then multiple handles may exist. Presently this only makes
* sense for RTLD_FIRST. Determine if an appropriate handle already
* exists.
*/
for (APLIST_TRAVERSE(*alpp, idx, aghp)) {
if ((aghp->gh_flags & GPH_FIRST) == (hflags & GPH_FIRST)) {
ghp = aghp;
break;
}
}
if (ghp == NULL) {
uint_t ndx;
/*
* If this is the first request for this handle, allocate and
* initialize a new handle.
*/
DBG_CALL(Dbg_file_hdl_title(DBG_HDL_CREATE));
if ((ghp = malloc(sizeof (Grp_hdl))) == NULL)
return (NULL);
/*
* Associate the handle with the link-map list or the reference
* link-map as appropriate.
*/
if (aplist_append(alpp, ghp, AL_CNT_GROUPS) == NULL) {
free(ghp);
return (NULL);
}
/*
* Record the existence of this handle for future verification.
*/
/* LINTED */
ndx = (uintptr_t)ghp % HDLIST_SZ;
if (aplist_append(&hdl_alp[ndx], ghp, AL_CNT_HANDLES) == NULL) {
(void) aplist_delete_value(*alpp, ghp);
free(ghp);
return (NULL);
}
ghp->gh_depends = NULL;
ghp->gh_refcnt = 1;
ghp->gh_flags = hflags;
/*
* A dlopen(0) handle is identified by the GPH_ZERO flag, the
* head of the link-map list is defined as the owner. There is
* no need to maintain a list of dependencies, for when this
* handle is used (for dlsym()) a dynamic search through the
* entire link-map list provides for searching all objects with
* GLOBAL visibility.
*/
if (hflags & GPH_ZERO) {
ghp->gh_ownlmp = lml->lm_head;
ghp->gh_ownlml = lml;
} else {
ghp->gh_ownlmp = rlmp;
ghp->gh_ownlml = LIST(rlmp);
if (hdl_add(ghp, rlmp, rdflags, NULL) == NULL)
return (NULL);
/*
* If this new handle is a private handle, there's no
* need to track the caller, so we're done.
*/
if (hflags & GPH_PRIVATE)
return (ghp);
/*
* If this new handle is public, and isn't a special
* handle representing ld.so.1, indicate that a local
* group now exists. This state allows singleton
* searches to be optimized.
*/
if ((hflags & GPH_LDSO) == 0)
LIST(rlmp)->lm_flags |= LML_FLG_GROUPSEXIST;
}
} else {
/*
* If a handle already exists, bump its reference count.
*
* If the previous reference count was 0, then this is a handle
* that an earlier call to dlclose() was unable to remove. Such
* handles are put on the orphan list. As this handle is back
* in use, it must be removed from the orphan list.
*
* Note, handles associated with a link-map list itself (i.e.
* dlopen(0)) can have a reference count of 0. However, these
* handles are never deleted, and therefore are never moved to
* the orphan list.
*/
if ((ghp->gh_refcnt++ == 0) &&
((ghp->gh_flags & GPH_ZERO) == 0)) {
uint_t ndx;
/* LINTED */
ndx = (uintptr_t)ghp % HDLIST_SZ;
(void) aplist_delete_value(hdl_alp[HDLIST_ORP], ghp);
(void) aplist_append(&hdl_alp[ndx], ghp,
AL_CNT_HANDLES);
if (DBG_ENABLED) {
Aliste idx;
Grp_desc *gdp;
DBG_CALL(Dbg_file_hdl_title(DBG_HDL_REINST));
for (ALIST_TRAVERSE(ghp->gh_depends, idx, gdp))
DBG_CALL(Dbg_file_hdl_action(ghp,
gdp->gd_depend, DBG_DEP_REINST, 0));
}
}
/*
* If we've been asked to create a private handle, there's no
* need to track the caller.
*/
if (hflags & GPH_PRIVATE) {
/*
* Negate the reference count increment.
*/
ghp->gh_refcnt--;
return (ghp);
} else {
/*
* If a private handle already exists, promote this
* handle to public by initializing both the reference
* count and the handle flags.
*/
if (ghp->gh_flags & GPH_PRIVATE) {
ghp->gh_refcnt = 1;
ghp->gh_flags &= ~GPH_PRIVATE;
ghp->gh_flags |= hflags;
}
}
}
/*
* Keep track of the parent (caller). As this object can be referenced
* by different parents, this processing is carried out every time a
* handle is requested.
*/
if (clmp && (hdl_add(ghp, clmp, cdflags, NULL) == NULL))
return (NULL);
return (ghp);
}
/*
* Initialize a handle that has been created for an object that is already
* loaded. The handle is initialized with the present dependencies of that
* object. Once this initialization has occurred, any new objects that might
* be loaded as dependencies (lazy-loading) are added to the handle as each new
* object is loaded.
*/
int
hdl_initialize(Grp_hdl *ghp, Rt_map *nlmp, int mode, int promote)
{
Aliste idx;
Grp_desc *gdp;
/*
* If the handle has already been initialized, and the initial object's
* mode hasn't been promoted, there's no need to recompute the modes of
* any dependencies. If the object we've added has just been opened,
* the objects dependencies will not yet have been processed. These
* dependencies will be added on later calls to load_one(). Otherwise,
* this object already exists, so add all of its dependencies to the
* handle were operating on.
*/
if (((ghp->gh_flags & GPH_INITIAL) && (promote == 0)) ||
((FLAGS(nlmp) & FLG_RT_ANALYZED) == 0)) {
ghp->gh_flags |= GPH_INITIAL;
return (1);
}
DBG_CALL(Dbg_file_hdl_title(DBG_HDL_ADD));
for (ALIST_TRAVERSE(ghp->gh_depends, idx, gdp)) {
Rt_map *lmp = gdp->gd_depend;
Aliste idx1;
Bnd_desc *bdp;
/*
* If this dependency doesn't indicate that its dependencies
* should be added to a handle, ignore it. This case identifies
* a parent of a dlopen(RTLD_PARENT) request.
*/
if ((gdp->gd_flags & GPD_ADDEPS) == 0)
continue;
for (APLIST_TRAVERSE(DEPENDS(lmp), idx1, bdp)) {
Rt_map *dlmp = bdp->b_depend;
if ((bdp->b_flags & BND_NEEDED) == 0)
continue;
if (hdl_add(ghp, dlmp,
(GPD_DLSYM | GPD_RELOC | GPD_ADDEPS), NULL) == NULL)
return (0);
(void) update_mode(dlmp, MODE(dlmp), mode);
}
}
ghp->gh_flags |= GPH_INITIAL;
return (1);
}
/*
* Sanity check a program-provided handle.
*/
static int
hdl_validate(Grp_hdl *ghp)
{
Aliste idx;
Grp_hdl *lghp;
uint_t ndx;
/* LINTED */
ndx = (uintptr_t)ghp % HDLIST_SZ;
for (APLIST_TRAVERSE(hdl_alp[ndx], idx, lghp)) {
if ((lghp == ghp) && (ghp->gh_refcnt != 0))
return (1);
}
return (0);
}
/*
* Core dlclose activity.
*/
int
dlclose_core(Grp_hdl *ghp, Rt_map *clmp, Lm_list *lml)
{
int error;
Rt_map *lmp;
/*
* If we're already at atexit() there's no point processing further,
* all objects have already been tsorted for fini processing.
*/
if (rtld_flags & RT_FL_ATEXIT)
return (0);
/*
* Diagnose what we're up to.
*/
if (ghp->gh_flags & GPH_ZERO) {
DBG_CALL(Dbg_dl_dlclose(clmp, MSG_ORIG(MSG_STR_ZERO),
DBG_DLCLOSE_IGNORE));
} else {
DBG_CALL(Dbg_dl_dlclose(clmp, NAME(ghp->gh_ownlmp),
DBG_DLCLOSE_NULL));
}
/*
* Decrement reference count of this object.
*/
if (--(ghp->gh_refcnt))
return (0);
/*
* If this handle is special (dlopen(0)), then leave it around - it
* has little overhead.
*/
if (ghp->gh_flags & GPH_ZERO)
return (0);
/*
* If this handle is associated with an object that is not on the base
* link-map control list, or it has not yet been relocated, then this
* handle must have originated from an auditors interaction, or some
* permutation of RTLD_CONFGEN use (crle(1), moe(1), etc.). User code
* can only execute and bind to relocated objects on the base link-map
* control list. Outside of RTLD_CONFGEN use, a non-relocated object,
* or an object on a non-base link-map control list, is in the process
* of being loaded, and therefore we do not attempt to remove the
* handle.
*/
if (((lmp = ghp->gh_ownlmp) != NULL) &&
((MODE(lmp) & RTLD_CONFGEN) == 0) &&
((CNTL(lmp) != ALIST_OFF_DATA) ||
((FLAGS(lmp) & FLG_RT_RELOCED) == 0)))
return (0);
/*
* This handle is no longer being referenced, remove it. If this handle
* is part of an alternative link-map list, determine if the whole list
* can be removed also.
*/
error = remove_hdl(ghp, clmp, NULL);
if ((lml->lm_flags & (LML_FLG_BASELM | LML_FLG_RTLDLM)) == 0)
remove_lml(lml);
return (error);
}
/*
* Internal dlclose activity. Called from user level or directly for internal
* error cleanup.
*/
int
dlclose_intn(Grp_hdl *ghp, Rt_map *clmp)
{
Rt_map *nlmp = NULL;
Lm_list *olml = NULL;
int error;
/*
* Although we're deleting object(s) it's quite possible that additional
* objects get loaded from running the .fini section(s) of the objects
* being deleted. These objects will have been added to the same
* link-map list as those objects being deleted. Remember this list
* for later investigation.
*/
olml = ghp->gh_ownlml;
error = dlclose_core(ghp, clmp, olml);
/*
* Determine whether the original link-map list still exists. In the
* case of a dlclose of an alternative (dlmopen) link-map the whole
* list may have been removed.
*/
if (olml) {
Aliste idx;
Lm_list *lml;
for (APLIST_TRAVERSE(dynlm_list, idx, lml)) {
if (olml == lml) {
nlmp = olml->lm_head;
break;
}
}
}
load_completion(nlmp);
return (error);
}
/*
* Argument checking for dlclose. Only called via external entry.
*/
static int
dlclose_check(void *handle, Rt_map *clmp)
{
Grp_hdl *ghp = (Grp_hdl *)handle;
if (hdl_validate(ghp) == 0) {
Conv_inv_buf_t inv_buf;
(void) conv_invalid_val(&inv_buf, EC_NATPTR(ghp), 0);
DBG_CALL(Dbg_dl_dlclose(clmp, inv_buf.buf, DBG_DLCLOSE_NULL));
eprintf(LIST(clmp), ERR_FATAL, MSG_INTL(MSG_ARG_INVHNDL),
EC_NATPTR(handle));
return (1);
}
return (dlclose_intn(ghp, clmp));
}
#pragma weak _dlclose = dlclose
/*
* External entry for dlclose(3dl). Returns 0 for success, non-zero otherwise.
*/
int
dlclose(void *handle)
{
int error, entry;
Rt_map *clmp;
entry = enter(0);
clmp = _caller(caller(), CL_EXECDEF);
error = dlclose_check(handle, clmp);
if (entry)
leave(LIST(clmp), 0);
return (error);
}
static uint_t lmid = 0;
/*
* The addition of new link-map lists is assumed to be in small quantities.
* Here, we assign a unique link-map id for diagnostic use. Simply update the
* running link-map count until we max out.
*/
int
newlmid(Lm_list *lml)
{
char buffer[MSG_LMID_ALT_SIZE + 12];
if (lmid == UINT_MAX) {
lml->lm_lmid = UINT_MAX;
(void) strncpy(buffer, MSG_ORIG(MSG_LMID_MAXED),
MSG_LMID_ALT_SIZE + 12);
} else {
lml->lm_lmid = lmid++;
(void) snprintf(buffer, MSG_LMID_ALT_SIZE + 12,
MSG_ORIG(MSG_LMID_FMT), MSG_ORIG(MSG_LMID_ALT),
lml->lm_lmid);
}
if ((lml->lm_lmidstr = strdup(buffer)) == NULL)
return (0);
return (1);
}
/*
* Core dlopen activity.
*/
static Grp_hdl *
dlmopen_core(Lm_list *lml, Lm_list *olml, const char *path, int mode,
Rt_map *clmp, uint_t flags, uint_t orig, int *in_nfavl)
{
Alist *palp = NULL;
Rt_map *nlmp;
Grp_hdl *ghp;
Aliste olmco, nlmco;
DBG_CALL(Dbg_dl_dlopen(clmp,
(path ? path : MSG_ORIG(MSG_STR_ZERO)), in_nfavl, mode));
/*
* Having diagnosed the originally defined modes, assign any defaults
* or corrections.
*/
if (((mode & (RTLD_GROUP | RTLD_WORLD)) == 0) &&
((mode & RTLD_NOLOAD) == 0))
mode |= (RTLD_GROUP | RTLD_WORLD);
if ((mode & RTLD_NOW) && (rtld_flags2 & RT_FL2_BINDLAZY)) {
mode &= ~RTLD_NOW;
mode |= RTLD_LAZY;
}
/*
* If the path specified is null then we're operating on global
* objects. Associate a dummy handle with the link-map list.
*/
if (path == NULL) {
Grp_hdl *ghp;
uint_t hflags, rdflags, cdflags;
int promote = 0;
/*
* Establish any flags for the handle (Grp_hdl).
*
* - This is a dummy, public, handle (0) that provides for a
* dynamic search of all global objects within the process.
* - Use of the RTLD_FIRST mode indicates that only the first
* dependency on the handle (the referenced object) can be
* used to satisfy dlsym() requests.
*/
hflags = (GPH_PUBLIC | GPH_ZERO);
if (mode & RTLD_FIRST)
hflags |= GPH_FIRST;
/*
* Establish the flags for the referenced dependency descriptor
* (Grp_desc).
*
* - The referenced object is available for dlsym().
* - The referenced object is available to relocate against.
* - The referenced object should have it's dependencies
* added to this handle.
*/
rdflags = (GPD_DLSYM | GPD_RELOC | GPD_ADDEPS);
/*
* Establish the flags for this callers dependency descriptor
* (Grp_desc).
*
* - The explicit creation of a handle creates a descriptor
* for the referenced object and the parent (caller).
* - Use of the RTLD_PARENT flag indicates that the parent
* can be relocated against.
*/
cdflags = GPD_PARENT;
if (mode & RTLD_PARENT)
cdflags |= GPD_RELOC;
if ((ghp = hdl_create(lml, 0, clmp, hflags, rdflags,
cdflags)) == NULL)
return (NULL);
/*
* Traverse the main link-map control list, updating the mode
* of any objects as necessary. Call the relocation engine if
* this mode promotes the existing state of any relocations.
* crle()'s first pass loads all objects necessary for building
* a configuration file, however none of them are relocated.
* crle()'s second pass relocates objects in preparation for
* dldump()'ing using dlopen(0, RTLD_NOW).
*/
if ((mode & (RTLD_NOW | RTLD_CONFGEN)) == RTLD_CONFGEN)
return (ghp);
for (nlmp = lml->lm_head; nlmp; nlmp = NEXT_RT_MAP(nlmp)) {
if (((MODE(nlmp) & RTLD_GLOBAL) == 0) ||
(FLAGS(nlmp) & FLG_RT_DELETE))
continue;
if (update_mode(nlmp, MODE(nlmp), mode))
promote = 1;
}
if (promote)
(void) relocate_lmc(lml, ALIST_OFF_DATA, clmp,
lml->lm_head, in_nfavl);
return (ghp);
}
/*
* Fix the pathname. If this object expands to multiple paths (ie.
* $ISALIST or $HWCAP have been used), then make sure the user has also
* furnished the RTLD_FIRST flag. As yet, we don't support opening
* more than one object at a time, so enforcing the RTLD_FIRST flag
* provides flexibility should we be able to support dlopening more
* than one object in the future.
*/
if (LM_FIX_NAME(clmp)(path, clmp, &palp, AL_CNT_NEEDED, orig) == NULL)
return (NULL);
if ((palp->al_arritems > 1) && ((mode & RTLD_FIRST) == 0)) {
remove_alist(&palp, 1);
eprintf(lml, ERR_FATAL, MSG_INTL(MSG_ARG_ILLMODE_5));
return (NULL);
}
/*
* Establish a link-map control list for this request, and load the
* associated object.
*/
if ((nlmco = create_cntl(lml, 1)) == NULL) {
remove_alist(&palp, 1);
return (NULL);
}
olmco = nlmco;
nlmp = load_one(lml, nlmco, palp, clmp, mode, (flags | FLG_RT_PUBHDL),
&ghp, in_nfavl);
/*
* Remove any expanded pathname infrastructure, and if the dependency
* couldn't be loaded, cleanup.
*/
remove_alist(&palp, 1);
if (nlmp == NULL) {
remove_cntl(lml, olmco);
return (NULL);
}
/*
* If loading an auditor was requested, and the auditor already existed,
* then the link-map returned will be to the original auditor. The new
* link-map list that was initially created, and the associated link-map
* control list are no longer needed. As the auditor is already loaded,
* we're probably done, but fall through in case additional relocations
* would be triggered by the mode of the caller.
*/
if ((flags & FLG_RT_AUDIT) && (LIST(nlmp) != lml)) {
remove_cntl(lml, olmco);
lml = LIST(nlmp);
olmco = 0;
nlmco = ALIST_OFF_DATA;
}
/*
* Finish processing the objects associated with this request.
*/
if (((nlmp = analyze_lmc(lml, nlmco, nlmp, clmp, in_nfavl)) == NULL) ||
(relocate_lmc(lml, nlmco, clmp, nlmp, in_nfavl) == 0)) {
ghp = NULL;
nlmp = NULL;
}
/*
* If the dlopen has failed, clean up any objects that might have been
* loaded successfully on this new link-map control list.
*/
if (olmco && (nlmp == NULL))
remove_lmc(lml, clmp, olmco, path);
/*
* Finally, remove any temporary link-map control list. Note, if this
* operation successfully established a new link-map list, then a base
* link-map control list will have been created, which must remain.
*/
if (olmco && ((nlmp == NULL) || (olml != (Lm_list *)LM_ID_NEWLM)))
remove_cntl(lml, olmco);
return (ghp);
}
/*
* dlopen() and dlsym() operations are the means by which a process can
* test for the existence of required dependencies. If the necessary
* dependencies don't exist, then associated functionality can't be used.
* However, the lack of dependencies can be fixed, and the dlopen() and
* dlsym() requests can be repeated. As we use a "not-found" AVL tree to
* cache any failed full path loads, secondary dlopen() and dlsym() requests
* will fail, even if the dependencies have been installed.
*
* dlopen() and dlsym() retry any failures by removing the "not-found" AVL
* tree. Should any dependencies be found, their names are added to the
* FullPath AVL tree. This routine removes any new "not-found" AVL tree,
* so that the dlopen() or dlsym() can replace the original "not-found" tree.
*/
inline static void
nfavl_remove(avl_tree_t *avlt)
{
PathNode *pnp;
void *cookie = NULL;
if (avlt) {
while ((pnp = avl_destroy_nodes(avlt, &cookie)) != NULL)
free(pnp);
avl_destroy(avlt);
free(avlt);
}
}
/*
* Internal dlopen() activity. Called from user level or directly for internal
* opens that require a handle.
*/
Grp_hdl *
dlmopen_intn(Lm_list *lml, const char *path, int mode, Rt_map *clmp,
uint_t flags, uint_t orig)
{
Lm_list *olml = lml;
Rt_map *dlmp = NULL;
Grp_hdl *ghp;
int in_nfavl = 0;
/*
* Check for magic link-map list values:
*
* LM_ID_BASE: Operate on the PRIMARY (executables) link map
* LM_ID_LDSO: Operation on ld.so.1's link map
* LM_ID_NEWLM: Create a new link-map.
*/
if (lml == (Lm_list *)LM_ID_NEWLM) {
if ((lml = calloc(sizeof (Lm_list), 1)) == NULL)
return (NULL);
/*
* Establish the new link-map flags from the callers and those
* explicitly provided.
*/
lml->lm_tflags = LIST(clmp)->lm_tflags;
if (flags & FLG_RT_AUDIT) {
/*
* Unset any auditing flags - an auditor shouldn't be
* audited. Insure all audit dependencies are loaded.
*/
lml->lm_tflags &= ~LML_TFLG_AUD_MASK;
lml->lm_tflags |= (LML_TFLG_NOLAZYLD |
LML_TFLG_LOADFLTR | LML_TFLG_NOAUDIT);
}
if (aplist_append(&dynlm_list, lml, AL_CNT_DYNLIST) == NULL) {
free(lml);
return (NULL);
}
if (newlmid(lml) == 0) {
(void) aplist_delete_value(dynlm_list, lml);
free(lml);
return (NULL);
}
} else if ((uintptr_t)lml < LM_ID_NUM) {
if ((uintptr_t)lml == LM_ID_BASE)
lml = &lml_main;
else if ((uintptr_t)lml == LM_ID_LDSO)
lml = &lml_rtld;
}
/*
* Open the required object on the associated link-map list.
*/
ghp = dlmopen_core(lml, olml, path, mode, clmp, flags, orig, &in_nfavl);
/*
* If the object could not be found it is possible that the "not-found"
* AVL tree had indicated that the file does not exist. In case the
* file system has changed since this "not-found" recording was made,
* retry the dlopen() with a clean "not-found" AVL tree.
*/
if ((ghp == NULL) && in_nfavl) {
avl_tree_t *oavlt = nfavl;
nfavl = NULL;
ghp = dlmopen_core(lml, olml, path, mode, clmp, flags, orig,
NULL);
/*
* If the file is found, then its full path name will have been
* registered in the FullPath AVL tree. Remove any new
* "not-found" AVL information, and restore the former AVL tree.
*/
nfavl_remove(nfavl);
nfavl = oavlt;
}
/*
* Establish the new link-map from which .init processing will begin.
* Ignore .init firing when constructing a configuration file (crle(1)).
*/
if (ghp && ((mode & RTLD_CONFGEN) == 0))
dlmp = ghp->gh_ownlmp;
/*
* If loading an auditor was requested, and the auditor already existed,
* then the link-map returned will be to the original auditor. Remove
* the link-map control list that was created for this request.
*/
if (dlmp && (flags & FLG_RT_AUDIT) && (LIST(dlmp) != lml)) {
remove_lml(lml);
lml = LIST(dlmp);
}
/*
* If this load failed, remove any alternative link-map list.
*/
if ((ghp == NULL) &&
((lml->lm_flags & (LML_FLG_BASELM | LML_FLG_RTLDLM)) == 0)) {
remove_lml(lml);
lml = NULL;
}
/*
* Finish this load request. If objects were loaded, .init processing
* is computed. Finally, the debuggers are informed of the link-map
* lists being stable.
*/
load_completion(dlmp);
return (ghp);
}
/*
* Argument checking for dlopen. Only called via external entry.
*/
static Grp_hdl *
dlmopen_check(Lm_list *lml, const char *path, int mode, Rt_map *clmp)
{
/*
* Verify that a valid pathname has been supplied.
*/
if (path && (*path == '\0')) {
eprintf(lml, ERR_FATAL, MSG_INTL(MSG_ARG_ILLPATH));
return (0);
}
/*
* Historically we've always verified the mode is either RTLD_NOW or
* RTLD_LAZY. RTLD_NOLOAD is valid by itself. Use of LM_ID_NEWLM
* requires a specific pathname, and use of RTLD_PARENT is meaningless.
*/
if ((mode & (RTLD_NOW | RTLD_LAZY | RTLD_NOLOAD)) == 0) {
eprintf(lml, ERR_FATAL, MSG_INTL(MSG_ARG_ILLMODE_1));
return (0);
}
if ((mode & (RTLD_NOW | RTLD_LAZY)) == (RTLD_NOW | RTLD_LAZY)) {
eprintf(lml, ERR_FATAL, MSG_INTL(MSG_ARG_ILLMODE_2));
return (0);
}
if ((lml == (Lm_list *)LM_ID_NEWLM) && (path == NULL)) {
eprintf(lml, ERR_FATAL, MSG_INTL(MSG_ARG_ILLMODE_3));
return (0);
}
if ((lml == (Lm_list *)LM_ID_NEWLM) && (mode & RTLD_PARENT)) {