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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
|
/* $OpenBSD: rtld.c,v 1.39 2004/11/08 20:47:07 miod Exp $ */
/* $NetBSD: rtld.c,v 1.43 1996/01/14 00:35:17 pk Exp $ */
/*
* Copyright (c) 1993 Paul Kranenburg
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by Paul Kranenburg.
* 4. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#include <sys/param.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/file.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/errno.h>
#include <sys/mman.h>
#ifndef MAP_COPY
#define MAP_COPY MAP_PRIVATE
#endif
#include <err.h>
#include <dlfcn.h>
#include <fcntl.h>
#include <a.out.h>
#include <limits.h>
#include <stab.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#if __STDC__
#include <stdarg.h>
#else
#include <varargs.h>
#endif
#include "ld.h"
/*
* Stack protector dummies.
* Ideally, a scheme to compile these stubs from libc should be used, but
* this would end up dragging too much code from libc here.
*/
long __guard[8] = {0, 0, 0, 0, 0, 0, 0, 0};
void
__stack_smash_handler(char func[], int damaged)
{
_exit(127);
}
#ifndef MAP_ANON
#define MAP_ANON 0
#define anon_open() do { \
if ((anon_fd = open("/dev/zero", O_RDWR, 0)) == -1) \
err("open: %s", "/dev/zero"); \
} while (0)
#define anon_close() do { \
(void)close(anon_fd); \
anon_fd = -1; \
} while (0)
#else
#define anon_open()
#define anon_close()
#endif
/*
* Loader private data, hung off <so_map>->som_spd
*/
struct somap_private {
int spd_version;
struct so_map *spd_parent;
int spd_refcount;
int spd_flags;
#define RTLD_MAIN 1
#define RTLD_RTLD 2
#define RTLD_DL 4
#define RTLD_INITED 8
size_t spd_size;
#ifdef SUN_COMPAT
long spd_offset; /* Correction for Sun main programs */
#endif
struct dep_node *first_child;
struct dep_node *last_child;
};
struct dep_node {
struct dep_node *next_sibling;
struct so_map *data;
};
#define LM_PRIVATE(smp) ((struct somap_private *)(smp)->som_spd)
#ifdef SUN_COMPAT
#define LM_OFFSET(smp) (LM_PRIVATE(smp)->spd_offset)
#else
#define LM_OFFSET(smp) (0)
#endif
/* Base address for section_dispatch_table entries */
#define LM_LDBASE(smp) (smp->som_addr + LM_OFFSET(smp))
/* Start of text segment */
#define LM_TXTADDR(smp) (smp->som_addr == (caddr_t)0 ? PAGSIZ : 0)
/* Start of run-time relocation_info */
#define LM_REL(smp) ((struct relocation_info *) \
(smp->som_addr + LM_OFFSET(smp) + LD_REL((smp)->som_dynamic)))
/* Start of symbols */
#define LM_SYMBOL(smp, i) ((struct nzlist *) \
(smp->som_addr + LM_OFFSET(smp) + LD_SYMBOL((smp)->som_dynamic) + \
i * (LD_VERSION_NZLIST_P(smp->som_dynamic->d_version) ? \
sizeof(struct nzlist) : sizeof(struct nlist))))
/* Start of hash table */
#define LM_HASH(smp) ((struct rrs_hash *) \
((smp)->som_addr + LM_OFFSET(smp) + LD_HASH((smp)->som_dynamic)))
/* Start of strings */
#define LM_STRINGS(smp) ((char *) \
((smp)->som_addr + LM_OFFSET(smp) + LD_STRINGS((smp)->som_dynamic)))
/* Start of search paths */
#define LM_PATHS(smp) ((char *) \
((smp)->som_addr + LM_OFFSET(smp) + LD_PATHS((smp)->som_dynamic)))
/* End of text */
#define LM_ETEXT(smp) ((char *) \
((smp)->som_addr + LM_TXTADDR(smp) + LD_TEXTSZ((smp)->som_dynamic)))
/* PLT is in data segment, so don't use LM_OFFSET here */
#define LM_PLT(smp) ((jmpslot_t *) \
((smp)->som_addr + LD_PLT((smp)->som_dynamic)))
/* Parent of link map */
#define LM_PARENT(smp) (LM_PRIVATE(smp)->spd_parent)
static char __main_progname[] = "main";
static char *main_progname = __main_progname;
static char us[] = "/usr/libexec/ld.so";
char **environ;
char *__progname = us;
/* int errno; */
static int careful;
static int anon_fd = -1;
struct so_map *link_map_head, *main_map;
struct so_map **link_map_tail = &link_map_head;
struct rt_symbol *rt_symbol_head;
static char *ld_library_path;
static char *ld_preload_path;
static int no_intern_search;
static int ld_suppress_warnings;
static int ld_warn_non_pure_code;
static int ld_tracing;
static void *__dlopen(const char *, int);
static int __dlclose(void *);
static void *__dlsym(void *, const char *);
static int __dlctl(void *, int, void *);
static void __dlexit(void);
static struct ld_entry ld_entry = {
__dlopen, __dlclose, __dlsym, __dlctl, __dlexit
};
void xprintf(char *, ...);
int rtld(int, struct crt_ldso *, struct _dynamic *);
void binder_entry(void);
long binder(jmpslot_t *);
static int load_subs(struct so_map *);
static struct so_map *map_object(struct sod *, struct so_map *);
static void unmap_object(struct so_map *);
static struct so_map *alloc_link_map( char *, struct sod *,
struct so_map *, caddr_t,
size_t, struct _dynamic *);
static void free_link_map(struct so_map *);
static inline void check_text_reloc( struct relocation_info *,
struct so_map *,
caddr_t);
static void init_maps(struct so_map *);
static void reloc_map(struct so_map *);
static void reloc_copy(struct so_map *);
static void call_map(struct so_map *, char *);
static char *rtfindlib(char *, int, int, int *, char *);
static struct nzlist *lookup(const char *, struct so_map **, int);
static inline struct rt_symbol *lookup_rts(const char *);
static struct rt_symbol *enter_rts(const char *, long, int, caddr_t,
long, struct so_map *);
static void maphints(void);
static void unmaphints(void);
static void preload(char *);
static void ld_trace(struct so_map *);
static inline int
strcmp (const char *s1, const char *s2)
{
while (*s1 == *s2++)
if (*s1++ == 0)
return (0);
return (*(unsigned char *)s1 - *(unsigned char *)--s2);
}
#include "md-static-funcs.c"
/*
* Called from assembler stub that has set up crtp (passed from crt0)
* and dp (our __DYNAMIC).
*/
int
rtld(int version, struct crt_ldso *crtp, struct _dynamic *dp)
{
int n;
int nreloc; /* # of ld.so relocations */
struct relocation_info *reloc;
struct so_debug *ddp;
struct so_map *smp;
/* Check version */
if ( version != CRT_VERSION_BSD_2 &&
version != CRT_VERSION_BSD_3 &&
version != CRT_VERSION_BSD_4 &&
version != CRT_VERSION_SUN)
return -1;
/* Fixup __DYNAMIC structure */
(long)dp->d_un.d_sdt += crtp->crt_ba;
/* Divide by hand to avoid possible use of library division routine */
for ( nreloc = 0, n = LD_RELSZ(dp);
n > 0;
n -= sizeof(struct relocation_info) ) nreloc++;
/* Relocate ourselves */
for (reloc = (struct relocation_info *)(LD_REL(dp) + crtp->crt_ba);
nreloc; nreloc--, reloc++) {
long addr = reloc->r_address + crtp->crt_ba;
md_relocate_simple(reloc, crtp->crt_ba, (char*)addr);
}
if (version >= CRT_VERSION_BSD_4)
__progname = crtp->crt_ldso;
if (version >= CRT_VERSION_BSD_3)
main_progname = crtp->crt_prog;
/* Setup out (private) environ variable */
environ = crtp->crt_ep;
if (issetugid())
careful = 1;
if (careful) {
unsetenv("LD_LIBRARY_PATH");
unsetenv("LD_PRELOAD");
}
/* Setup directory search */
ld_library_path = getenv("LD_LIBRARY_PATH");
add_search_path(ld_library_path);
if (getenv("LD_NOSTD_PATH") == NULL)
std_search_path();
ld_suppress_warnings = getenv("LD_SUPPRESS_WARNINGS") != NULL;
ld_warn_non_pure_code = getenv("LD_WARN_NON_PURE_CODE") != NULL;
no_intern_search = careful || getenv("LD_NO_INTERN_SEARCH") != 0;
anon_open();
/*
* Init object administration. We start off with a map description
* for `main' and `rtld'.
*/
smp = alloc_link_map(main_progname, (struct sod *)0, (struct so_map *)0,
(caddr_t)0, 0, crtp->crt_dp);
LM_PRIVATE(smp)->spd_refcount++;
LM_PRIVATE(smp)->spd_flags |= RTLD_MAIN;
main_map = smp;
smp = alloc_link_map(us, (struct sod *)0, (struct so_map *)0,
(caddr_t)crtp->crt_ba, 0, dp);
LM_PRIVATE(smp)->spd_refcount++;
LM_PRIVATE(smp)->spd_flags |= RTLD_RTLD;
/* Fill in some field in main's __DYNAMIC structure */
if (version >= CRT_VERSION_BSD_4)
crtp->crt_ldentry = &ld_entry;
else
crtp->crt_dp->d_entry = &ld_entry;
/* Handle LD_PRELOAD's here */
ld_preload_path = getenv("LD_PRELOAD");
if (ld_preload_path != NULL)
preload(ld_preload_path);
/* Load subsidiary objects into the process address space */
ld_tracing = (int)getenv("LD_TRACE_LOADED_OBJECTS");
load_subs(link_map_head);
if (ld_tracing) {
ld_trace(link_map_head);
exit(0);
}
init_maps(link_map_head);
crtp->crt_dp->d_un.d_sdt->sdt_loaded = link_map_head->som_next;
ddp = crtp->crt_dp->d_debug;
ddp->dd_cc = rt_symbol_head;
if (ddp->dd_in_debugger) {
caddr_t addr = (caddr_t)((long)crtp->crt_bp & (~(PAGSIZ - 1)));
/* Set breakpoint for the benefit of debuggers */
if (mprotect(addr, PAGSIZ,
PROT_READ|PROT_WRITE|PROT_EXEC) == -1) {
err(1, "Cannot set breakpoint (%s)", main_progname);
}
md_set_breakpoint((long)crtp->crt_bp, (long *)&ddp->dd_bpt_shadow);
if (mprotect(addr, PAGSIZ, PROT_READ|PROT_EXEC) == -1) {
err(1, "Cannot re-protect breakpoint (%s)",
main_progname);
}
ddp->dd_bpt_addr = crtp->crt_bp;
if (link_map_head)
ddp->dd_sym_loaded = 1;
}
/* Close the hints file */
unmaphints();
/* Close our file descriptor */
(void)close(crtp->crt_ldfd);
anon_close();
return 0;
}
static void
link_sub(struct so_map *dep, struct so_map *p)
{
struct dep_node *n;
struct somap_private *pp;
n = xmalloc(sizeof *n);
n->data = dep;
n->next_sibling = NULL;
pp = LM_PRIVATE(p);
if (pp->first_child) {
pp->last_child->next_sibling = n;
pp->last_child = n;
} else {
pp->first_child = pp->last_child = n;
}
}
static int
load_subs(struct so_map *smp)
{
for (; smp; smp = smp->som_next) {
struct sod *sodp;
long next = 0;
if (LM_PRIVATE(smp)->spd_flags & RTLD_RTLD)
continue;
if (smp->som_dynamic)
next = LD_NEED(smp->som_dynamic);
while (next) {
struct so_map *newmap;
sodp = (struct sod *)(LM_LDBASE(smp) + next);
if ((newmap = map_object(sodp, smp)) == NULL) {
if (!ld_tracing) {
if (smp != main_map)
if (sodp->sod_library)
err(1, "%s(%s): lib%s.so.%d.%d",
main_progname,
smp->som_path,
sodp->sod_name+LM_LDBASE(smp),
sodp->sod_major,
sodp->sod_minor);
else
err(1, "%s(%s): %s",
main_progname,
smp->som_path,
sodp->sod_name+LM_LDBASE(smp));
else
if (sodp->sod_library)
err(1, "%s: lib%s.so.%d.%d",
main_progname,
sodp->sod_name+LM_LDBASE(smp),
sodp->sod_major,
sodp->sod_minor);
else
err(1, "%s: %s",
main_progname,
sodp->sod_name+LM_LDBASE(smp));
}
newmap = alloc_link_map(NULL, sodp, smp,
0, 0, 0);
} else {
link_sub(newmap, smp);
}
LM_PRIVATE(newmap)->spd_refcount++;
next = sodp->sod_next;
}
}
return 0;
}
void
ld_trace(struct so_map *smp)
{
char *fmt1, *fmt2, *fmt, *main_local;
int c;
if ((main_local = getenv("LD_TRACE_LOADED_OBJECTS_PROGNAME")) == NULL)
main_local = "";
if ((fmt1 = getenv("LD_TRACE_LOADED_OBJECTS_FMT1")) == NULL)
fmt1 = "\t-l%o.%m => %p (%x)\n";
if ((fmt2 = getenv("LD_TRACE_LOADED_OBJECTS_FMT2")) == NULL)
fmt2 = "\t%o (%x)\n";
for (; smp; smp = smp->som_next) {
struct sod *sodp;
char *name, *path;
if ((sodp = smp->som_sod) == NULL)
continue;
name = (char *)sodp->sod_name;
if (LM_PARENT(smp))
name += (long)LM_LDBASE(LM_PARENT(smp));
if ((path = smp->som_path) == NULL)
path = "not found";
fmt = sodp->sod_library ? fmt1 : fmt2;
while ((c = *fmt++) != '\0') {
switch (c) {
default:
putchar(c);
continue;
case '\\':
switch (c = *fmt) {
case '\0':
continue;
case 'n':
putchar('\n');
break;
case 't':
putchar('\t');
break;
}
break;
case '%':
switch (c = *fmt) {
case '\0':
continue;
case '%':
default:
putchar(c);
break;
case 'A':
printf("%s", main_local);
break;
case 'a':
printf("%s", main_progname);
break;
case 'o':
printf("%s", name);
break;
case 'm':
printf("%d", sodp->sod_major);
break;
case 'n':
printf("%d", sodp->sod_minor);
break;
case 'p':
printf("%s", path);
break;
case 'x':
printf("%p", smp->som_addr);
break;
}
break;
}
++fmt;
}
}
}
/*
* Allocate a new link map for shared object NAME loaded at ADDR as a
* result of the presence of link object LOP in the link map PARENT.
*/
static struct so_map *
alloc_link_map(char *path, struct sod *sodp, struct so_map *parent,
caddr_t addr, size_t size, struct _dynamic *dp)
{
struct so_map *smp;
struct somap_private *smpp;
smpp = (struct somap_private *)xmalloc(sizeof(struct somap_private));
smp = (struct so_map *)xmalloc(sizeof(struct so_map));
smp->som_next = NULL;
*link_map_tail = smp;
link_map_tail = &smp->som_next;
/*smp->som_sodbase = 0; NOT USED */
smp->som_write = 0;
smp->som_addr = addr;
smp->som_path = path?strdup(path):NULL;
smp->som_sod = sodp;
smp->som_dynamic = dp;
smp->som_spd = (caddr_t)smpp;
smpp->spd_refcount = 0;
smpp->spd_flags = 0;
smpp->spd_parent = parent;
smpp->spd_size = size;
smpp->first_child = NULL;
smpp->last_child = NULL;
#ifdef SUN_COMPAT
smpp->spd_offset =
(addr==0 && dp && dp->d_version==LD_VERSION_SUN) ? PAGSIZ : 0;
#endif
return smp;
}
/*
* Free the link map for an object being unmapped. The link map
* has already been removed from the link map list, so it can't be used
* after it's been unmapped.
*/
static void
free_link_map(struct so_map *smp)
{
if ((LM_PRIVATE(smp)->spd_flags & RTLD_DL) != 0) {
/* free synthetic sod structure allocated in __dlopen() */
free((char *)smp->som_sod->sod_name);
free(smp->som_sod);
}
/* free the link map structure. */
free(smp->som_spd);
if (smp->som_path != NULL)
free(smp->som_path);
free(smp);
}
/*
* Map object identified by link object SODP which was found
* in link map SMP.
*/
static struct so_map *
map_object(struct sod *sodp, struct so_map *smp)
{
char *name;
struct _dynamic *dp;
char *path, *ipath;
int fd, sverrno;
caddr_t addr;
struct exec hdr;
int usehints = 0;
struct so_map *p;
name = (char *)sodp->sod_name;
if (smp)
name += (long)LM_LDBASE(smp);
if (sodp->sod_library) {
usehints = 1;
again:
if (smp == NULL || no_intern_search ||
LD_PATHS(smp->som_dynamic) == 0) {
ipath = NULL;
} else {
ipath = LM_PATHS(smp);
add_search_path(ipath);
}
path = rtfindlib(name, sodp->sod_major,
sodp->sod_minor, &usehints, ipath);
if (ipath)
remove_search_path(ipath);
if (path == NULL && smp != main_map && main_map != NULL &&
!no_intern_search &&
LD_PATHS(main_map->som_dynamic) != 0) {
ipath = LM_PATHS(main_map);
add_search_path(ipath);
path = rtfindlib(name, sodp->sod_major,
sodp->sod_minor, &usehints, ipath);
remove_search_path(ipath);
}
if (path == NULL) {
errno = ENOENT;
return NULL;
}
} else {
if (careful && *name != '/') {
errno = EACCES;
return NULL;
}
path = name;
}
/* Check if already loaded */
for (p = link_map_head; p; p = p->som_next)
if (p->som_path && strcmp(p->som_path, path) == 0)
break;
if (p != NULL)
return p;
if ((fd = open(path, O_RDONLY, 0)) == -1) {
if (usehints) {
usehints = 0;
goto again;
}
return NULL;
}
if (read(fd, &hdr, sizeof(hdr)) != sizeof(hdr)) {
sverrno = errno;
(void)close(fd);
errno = sverrno;
return NULL;
}
if (N_BADMAG(hdr)) {
(void)close(fd);
errno = EFTYPE;
return NULL;
}
if ((addr = mmap(0, hdr.a_text + hdr.a_data + hdr.a_bss,
PROT_READ|PROT_EXEC,
MAP_COPY, fd, 0)) == (caddr_t)MAP_FAILED) {
sverrno = errno;
(void)close(fd);
errno = sverrno;
return NULL;
}
#if DEBUG
xprintf("map1: 0x%x for 0x%x\n", addr, hdr.a_text + hdr.a_data + hdr.a_bss);
#endif
if (mprotect(addr + hdr.a_text, hdr.a_data,
PROT_READ|PROT_WRITE|PROT_EXEC) != 0) {
sverrno = errno;
(void)close(fd);
errno = sverrno;
return NULL;
}
if (mmap(addr + hdr.a_text + hdr.a_data, hdr.a_bss,
PROT_READ|PROT_WRITE,
MAP_ANON|MAP_COPY|MAP_FIXED,
anon_fd, 0) == (caddr_t)MAP_FAILED) {
sverrno = errno;
(void)close(fd);
errno = sverrno;
return NULL;
}
(void)close(fd);
/* Assume _DYNAMIC is the first data item */
dp = (struct _dynamic *)(addr+hdr.a_text);
/* Fixup __DYNAMIC structure */
(long)dp->d_un.d_sdt += (long)addr;
return alloc_link_map(path, sodp, smp, addr,
hdr.a_text + hdr.a_data + hdr.a_bss, dp);
}
/*
* Unmap a mapped object.
*/
static void
unmap_object(struct so_map *smp)
{
struct so_map *p, **pp;
/* remove from link map list */
pp = &link_map_head;
while ((p = *pp) != NULL) {
if (p == smp)
break;
pp = &p->som_next;
}
if (p == NULL) {
warnx("warning: link map entry for %s not on link map list!",
smp->som_path);
return;
}
*pp = smp->som_next; /* make list skip it */
if (link_map_tail == &smp->som_next) /* and readjust tail pointer */
link_map_tail = pp;
/* unmap from address space */
(void)munmap(smp->som_addr, LM_PRIVATE(smp)->spd_size);
}
void init_dependent_before_main(struct so_map *smp)
{
struct dep_node *n;
LM_PRIVATE(smp)->spd_flags |= RTLD_INITED;
for (n = LM_PRIVATE(smp)->first_child; n; n = n->next_sibling) {
if (LM_PRIVATE(n->data)->spd_flags & RTLD_INITED)
continue;
init_dependent_before_main(n->data);
}
call_map(smp, ".init");
call_map(smp, "__init");
call_map(smp, "__GLOBAL__DI");
}
void
init_maps(struct so_map *head)
{
struct so_map *smp;
/* Relocate all loaded objects according to their RRS segments */
for (smp = head; smp; smp = smp->som_next) {
if (LM_PRIVATE(smp)->spd_flags & RTLD_RTLD)
continue;
reloc_map(smp);
}
/* Copy any relocated initialized data. */
for (smp = head; smp; smp = smp->som_next) {
if (LM_PRIVATE(smp)->spd_flags & RTLD_RTLD)
continue;
reloc_copy(smp);
}
/* Call any object initialization routines. */
for (smp = head; smp; smp = smp->som_next) {
if (LM_PRIVATE(smp)->spd_flags & RTLD_RTLD)
continue;
if (LM_PRIVATE(smp)->spd_flags & RTLD_INITED)
continue;
init_dependent_before_main(smp);
}
}
static inline void
check_text_reloc(struct relocation_info *r, struct so_map *smp, caddr_t addr)
{
char *sym;
if (addr >= LM_ETEXT(smp))
return;
if (RELOC_EXTERN_P(r))
sym = LM_STRINGS(smp) +
LM_SYMBOL(smp, RELOC_SYMBOL(r))->nz_strx;
else
sym = "";
if (ld_warn_non_pure_code && !ld_suppress_warnings)
warnx("warning: non pure code in %s at %x (%s)",
smp->som_path, r->r_address, sym);
if (smp->som_write == 0 &&
mprotect(smp->som_addr + LM_TXTADDR(smp),
LD_TEXTSZ(smp->som_dynamic),
PROT_READ|PROT_WRITE|PROT_EXEC) == -1) {
err(1, "Cannot enable writes to %s:%s",
main_progname, smp->som_path);
}
smp->som_write = 1;
}
static void
reloc_map(struct so_map *smp)
{
struct _dynamic *dp = smp->som_dynamic;
struct relocation_info *r = LM_REL(smp);
struct relocation_info *rend = r + LD_RELSZ(dp)/sizeof(*r);
long symbolbase = (long)LM_SYMBOL(smp, 0);
char *stringbase = LM_STRINGS(smp);
int symsize = LD_VERSION_NZLIST_P(dp->d_version) ?
sizeof(struct nzlist) :
sizeof(struct nlist);
if (LD_PLTSZ(dp))
md_fix_jmpslot(LM_PLT(smp),
(long)LM_PLT(smp), (long)binder_entry);
for (; r < rend; r++) {
char *sym;
caddr_t addr = smp->som_addr + r->r_address;
check_text_reloc(r, smp, addr);
if (RELOC_EXTERN_P(r)) {
struct so_map *src_map = NULL;
struct nzlist *p, *np;
long relocation = md_get_addend(r, addr);
if (RELOC_LAZY_P(r))
continue;
p = (struct nzlist *)
(symbolbase + symsize * RELOC_SYMBOL(r));
if (p->nz_type == (N_SETV + N_EXT))
src_map = smp;
sym = stringbase + p->nz_strx;
np = lookup(sym, &src_map, 0/*XXX-jumpslots!*/);
if (np == NULL)
errx(1, "Undefined symbol \"%s\" in %s:%s",
sym, main_progname, smp->som_path);
/*
* Found symbol definition.
* If it's in a link map, adjust value
* according to the load address of that map.
* Otherwise it's a run-time allocated common
* whose value is already up-to-date.
*/
relocation += np->nz_value;
if (src_map)
relocation += (long)src_map->som_addr;
if (RELOC_PCREL_P(r))
relocation -= (long)smp->som_addr;
if (RELOC_COPY_P(r) && src_map) {
(void)enter_rts(sym,
(long)addr,
N_DATA + N_EXT,
src_map->som_addr + np->nz_value,
np->nz_size, src_map);
continue;
}
md_relocate(r, relocation, addr, 0);
} else {
md_relocate(r,
#ifdef SUN_COMPAT
md_get_rt_segment_addend(r, addr)
#else
md_get_addend(r, addr)
#endif
+ (long)smp->som_addr, addr, 0);
}
}
if (smp->som_write) {
if (mprotect(smp->som_addr + LM_TXTADDR(smp),
LD_TEXTSZ(smp->som_dynamic),
PROT_READ|PROT_EXEC) == -1) {
err(1, "Cannot disable writes to %s:%s",
main_progname, smp->som_path);
}
smp->som_write = 0;
}
}
static void
reloc_copy(struct so_map *smp)
{
struct rt_symbol *rtsp;
for (rtsp = rt_symbol_head; rtsp; rtsp = rtsp->rt_next)
if ((rtsp->rt_smp == NULL || rtsp->rt_smp == smp) &&
rtsp->rt_sp->nz_type == N_DATA + N_EXT) {
bcopy(rtsp->rt_srcaddr, (caddr_t)rtsp->rt_sp->nz_value,
rtsp->rt_sp->nz_size);
}
}
static void
call_map(struct so_map *smp, char *sym)
{
struct so_map *src_map = smp;
struct nzlist *np;
np = lookup(sym, &src_map, 1);
if (np)
(*(void (*) (void))(src_map->som_addr + np->nz_value))();
}
/*
* Run-time common symbol table.
*/
#define RTC_TABSIZE 57
static struct rt_symbol *rt_symtab[RTC_TABSIZE];
/*
* Compute hash value for run-time symbol table
*/
static inline int
hash_string(const char *key)
{
const char *cp;
int k;
cp = key;
k = 0;
while (*cp)
k = (((k << 1) + (k >> 14)) ^ (*cp++)) & 0x3fff;
return k;
}
/*
* Lookup KEY in the run-time common symbol table.
*/
static inline struct rt_symbol *
lookup_rts(const char *key)
{
int hashval;
struct rt_symbol *rtsp;
/* Determine which bucket. */
hashval = hash_string(key) % RTC_TABSIZE;
/* Search the bucket. */
for (rtsp = rt_symtab[hashval]; rtsp; rtsp = rtsp->rt_link)
if (strcmp(key, rtsp->rt_sp->nz_name) == 0)
return rtsp;
return NULL;
}
static struct rt_symbol *
enter_rts(const char *name, long value, int type, caddr_t srcaddr, long size,
struct so_map *smp)
{
int hashval;
struct rt_symbol *rtsp, **rpp;
/* Determine which bucket */
hashval = hash_string(name) % RTC_TABSIZE;
/* Find end of bucket */
for (rpp = &rt_symtab[hashval]; *rpp; rpp = &(*rpp)->rt_link)
;
/* Allocate new common symbol */
rtsp = (struct rt_symbol *)malloc(sizeof(struct rt_symbol));
rtsp->rt_sp = (struct nzlist *)malloc(sizeof(struct nzlist));
rtsp->rt_sp->nz_name = strdup(name);
rtsp->rt_sp->nz_value = value;
rtsp->rt_sp->nz_type = type;
rtsp->rt_sp->nz_size = size;
rtsp->rt_srcaddr = srcaddr;
rtsp->rt_smp = smp;
rtsp->rt_link = NULL;
/* Link onto linear list as well */
rtsp->rt_next = rt_symbol_head;
rt_symbol_head = rtsp;
*rpp = rtsp;
return rtsp;
}
/*
* Lookup NAME in the link maps. The link map producing a definition
* is returned in SRC_MAP. If SRC_MAP is not NULL on entry the search is
* confined to that map. If STRONG is set, the symbol returned must
* have a proper type (used by binder()).
*/
static struct nzlist *
lookup(const char *name, struct so_map **src_map, int strong)
{
long common_size = 0;
struct so_map *smp, *weak_smp;
struct rt_symbol *rtsp;
struct nzlist *weak_np = 0;
if ((rtsp = lookup_rts(name)) != NULL) {
/* Common symbol is not a member of particular shlib. */
*src_map = NULL;
return rtsp->rt_sp;
}
weak_smp = NULL; /* XXX - gcc! */
/*
* Search all maps for a definition of NAME
*/
for (smp = link_map_head; smp; smp = smp->som_next) {
int buckets;
long hashval;
struct rrs_hash *hp;
const char *cp;
struct nzlist *np;
/* Some local caching */
long symbolbase;
struct rrs_hash *hashbase;
char *stringbase;
int symsize;
if (*src_map && smp != *src_map)
continue;
if ((buckets = LD_BUCKETS(smp->som_dynamic)) == 0)
continue;
if (LM_PRIVATE(smp)->spd_flags & RTLD_RTLD)
continue;
restart:
/*
* Compute bucket in which the symbol might be found.
*/
for (hashval = 0, cp = name; *cp; cp++)
hashval = (hashval << 1) + *cp;
hashval = (hashval & 0x7fffffff) % buckets;
hashbase = LM_HASH(smp);
hp = hashbase + hashval;
if (hp->rh_symbolnum == -1)
/* Nothing in this bucket */
continue;
symbolbase = (long)LM_SYMBOL(smp, 0);
stringbase = LM_STRINGS(smp);
symsize = LD_VERSION_NZLIST_P(smp->som_dynamic->d_version)?
sizeof(struct nzlist) :
sizeof(struct nlist);
while (hp) {
np = (struct nzlist *)
(symbolbase + hp->rh_symbolnum * symsize);
cp = stringbase + np->nz_strx;
if (strcmp(cp, name) == 0)
break;
if (hp->rh_next == 0)
hp = NULL;
else
hp = hashbase + hp->rh_next;
}
if (hp == NULL)
/* Nothing in this bucket */
continue;
/*
* We have a symbol with the name we're looking for.
*/
if (np->nz_type == N_INDR+N_EXT) {
/*
* Next symbol gives the aliased name. Restart
* search with new name and confine to this map.
*/
name = stringbase + (++np)->nz_strx;
*src_map = smp;
goto restart;
}
if (np->nz_value == 0)
/* It's not a definition */
continue;
if (np->nz_type == N_UNDF+N_EXT && np->nz_value != 0) {
if (N_AUX(&np->nlist) == AUX_FUNC) {
/* It's a weak function definition */
if (strong)
continue;
} else {
/* It's a common, note value and continue search */
if (common_size < np->nz_value)
common_size = np->nz_value;
continue;
}
}
if (N_BIND(&np->nlist) != BIND_WEAK) {
*src_map = smp;
return np;
}
if (N_BIND(&np->nlist) == BIND_WEAK && weak_np == 0) {
weak_np = np;
weak_smp = smp;
continue;
}
}
if (weak_np) {
*src_map = weak_smp;
return weak_np;
}
if (common_size == 0)
/* Not found */
return NULL;
/*
* It's a common, enter into run-time common symbol table.
*/
rtsp = enter_rts(name, (long)calloc(1, common_size),
N_UNDF + N_EXT, 0, common_size, NULL);
/* Common symbol is not a member of particular shlib. */
*src_map = NULL;
#if DEBUG
xprintf("Allocating common: %s size %d at %#x\n", name, common_size, rtsp->rt_sp->nz_value);
#endif
return rtsp->rt_sp;
}
/*
* This routine is called from the jumptable to resolve
* procedure calls to shared objects.
*/
long
binder(jmpslot_t *jsp)
{
struct so_map *smp, *src_map = NULL;
long addr;
char *sym;
struct nzlist *np;
int index;
/*
* Find the PLT map that contains JSP.
*/
for (smp = link_map_head; smp; smp = smp->som_next) {
if (LM_PLT(smp) < jsp &&
jsp < LM_PLT(smp) + LD_PLTSZ(smp->som_dynamic)/sizeof(*jsp))
break;
}
if (smp == NULL)
errx(1, "Call to binder from unknown location: %p", jsp);
index = jsp->reloc_index & JMPSLOT_RELOC_MASK;
/* Get the local symbol this jmpslot refers to */
sym = LM_STRINGS(smp) +
LM_SYMBOL(smp,RELOC_SYMBOL(&LM_REL(smp)[index]))->nz_strx;
np = lookup(sym, &src_map, 1);
if (np == NULL)
errx(1, "Undefined symbol \"%s\" called from %s:%s at %p",
sym, main_progname, smp->som_path, jsp);
/* Fixup jmpslot so future calls transfer directly to target */
addr = np->nz_value;
if (src_map)
addr += (long)src_map->som_addr;
md_fix_jmpslot(jsp, (long)jsp, addr);
#if DEBUG
xprintf(" BINDER: %s located at = %#x in %s\n", sym, addr, src_map->som_path);
#endif
return addr;
}
static int hfd;
static long hsize;
static struct hints_header *hheader;
static struct hints_bucket *hbuckets;
static char *hstrtab;
static char *hint_search_path = "";
#define HINTS_VALID (hheader != NULL && hheader != (struct hints_header *)-1)
static void
maphints(void)
{
struct stat sb;
caddr_t addr = MAP_FAILED;
if ((hfd = open(_PATH_LD_HINTS, O_RDONLY, 0)) < 0)
goto bad_hints;
if (fstat(hfd, &sb) != 0 || !S_ISREG(sb.st_mode) ||
sb.st_size < sizeof(struct hints_header) || sb.st_size > LONG_MAX)
goto bad_hints;
hsize = (long)sb.st_size;
addr = mmap(0, hsize, PROT_READ, MAP_COPY, hfd, 0);
if (addr == MAP_FAILED)
goto bad_hints;
hheader = (struct hints_header *)addr;
if (HH_BADMAG(*hheader) || hheader->hh_ehints > hsize)
goto bad_hints;
if (hheader->hh_version != LD_HINTS_VERSION_1 &&
hheader->hh_version != LD_HINTS_VERSION_2)
goto bad_hints;
hbuckets = (struct hints_bucket *)(addr + hheader->hh_hashtab);
hstrtab = (char *)(addr + hheader->hh_strtab);
if (hheader->hh_version >= LD_HINTS_VERSION_2)
hint_search_path = hstrtab + hheader->hh_dirlist;
return;
bad_hints:
if (addr != MAP_FAILED)
munmap(addr, hsize);
if (hfd != -1)
close(hfd);
hheader = (struct hints_header *)-1;
}
static void
unmaphints(void)
{
if (HINTS_VALID) {
munmap((caddr_t)hheader, hsize);
close(hfd);
hheader = NULL;
}
}
static int
hinthash(char *cp, int vmajor, int vminor)
{
int k = 0;
while (*cp)
k = (((k << 1) + (k >> 14)) ^ (*cp++)) & 0x3fff;
k = (((k << 1) + (k >> 14)) ^ (vmajor*257)) & 0x3fff;
if (hheader->hh_version == LD_HINTS_VERSION_1)
k = (((k << 1) + (k >> 14)) ^ (vminor*167)) & 0x3fff;
return k;
}
#undef major
#undef minor
static char *
findhint(char *name, int major, int minor, char *prefered_path)
{
struct hints_bucket *bp;
bp = hbuckets + (hinthash(name, major, minor) % hheader->hh_nbucket);
while (1) {
/* Sanity check */
if (bp->hi_namex >= hheader->hh_strtab_sz) {
warnx("Bad name index: %#x", bp->hi_namex);
break;
}
if (bp->hi_pathx >= hheader->hh_strtab_sz) {
warnx("Bad path index: %#x", bp->hi_pathx);
break;
}
if (strcmp(name, hstrtab + bp->hi_namex) == 0) {
/* It's `name', check version numbers */
if (bp->hi_major == major &&
(bp->hi_ndewey < 2 || bp->hi_minor >= minor)) {
if (prefered_path == NULL ||
strncmp(prefered_path,
hstrtab + bp->hi_pathx,
strlen(prefered_path)) == 0) {
return hstrtab + bp->hi_pathx;
}
}
}
if (bp->hi_next == -1)
break;
/* Move on to next in bucket */
bp = &hbuckets[bp->hi_next];
}
/* No hints available for name */
return NULL;
}
static char *
rtfindlib(char *name, int major, int minor, int *usehints, char *ipath)
{
char *cp;
int realminor;
if (hheader == NULL)
maphints();
if (!HINTS_VALID || !(*usehints))
goto lose;
/* NOTE: `ipath' may reside in a piece of read-only memory */
if (ld_library_path || ipath) {
/* Prefer paths from some explicit LD_LIBRARY_PATH */
char *lpath;
char *dp;
dp = lpath = concat(ld_library_path ? ld_library_path : "",
(ld_library_path && ipath) ? ":" : "",
ipath ? ipath : "");
while ((cp = strsep(&dp, ":")) != NULL) {
if (*cp) {
cp = findhint(name, major, minor, cp);
if (cp) {
free(lpath);
return cp;
}
}
}
free(lpath);
/*
* Not found in hints; try directory search now, before
* we get a spurious hint match below (i.e. a match not
* on one of the paths we're supposed to search first.
*/
realminor = -1;
cp = (char *)findshlib(name, &major, &realminor, 0);
if (cp && realminor >= minor)
return cp;
}
/* No LD_LIBRARY_PATH or lib not found in there; check default */
cp = findhint(name, major, minor, NULL);
if (cp)
return cp;
lose:
/* No hints available for name */
*usehints = 0;
realminor = -1;
add_search_path(hint_search_path);
cp = (char *)findshlib(name, &major, &realminor, 0);
remove_search_path(hint_search_path);
if (cp) {
if (realminor < minor && !ld_suppress_warnings)
warnx("warning: lib%s.so.%d.%d: "
"minor version >= %d expected, using it anyway",
name, major, realminor, minor);
return cp;
}
return NULL;
}
void
preload(char *paths)
{
struct so_map *nsmp;
struct sod *sodp;
char *cp, *dp;
dp = paths = strdup(paths);
if (dp == NULL) {
errx(1, "preload: out of memory");
}
while ((cp = strsep(&dp, ":")) != NULL) {
if ((sodp = (struct sod *)malloc(sizeof(struct sod))) == NULL) {
errx(1, "preload: %s: out of memory", cp);
return;
}
sodp->sod_name = (long)strdup(cp);
sodp->sod_library = 0;
sodp->sod_major = sodp->sod_minor = 0;
if ((nsmp = map_object(sodp, 0)) == NULL) {
errx(1, "preload: %s: cannot map object", cp);
}
LM_PRIVATE(nsmp)->spd_refcount++;
}
free(paths);
return;
}
static int dlerrno;
/*
* Populate sod struct for dlopen's call to map_object
*/
static void
build_sod(const char *name, struct sod *sodp)
{
unsigned int tuplet;
int major, minor;
char *realname, *tok, *etok, *cp;
/* default is an absolute or relative path */
sodp->sod_name = (long)strdup(name); /* strtok is destructive */
sodp->sod_library = 0;
sodp->sod_major = sodp->sod_minor = 0;
/* does it look like /^lib/ ? */
if (strncmp((char *)sodp->sod_name, "lib", 3) != 0)
return;
/* is this a filename? */
if (strchr((char *)sodp->sod_name, '/'))
return;
/* skip over 'lib' */
cp = (char *)sodp->sod_name + 3;
/* dot guardian */
if ((strchr(cp, '.') == NULL) || (*(cp+strlen(cp)-1) == '.'))
return;
/* default */
major = minor = -1;
/* loop through name - parse skipping name */
for (tuplet = 0; (tok = strsep(&cp, ".")) != NULL; tuplet++) {
switch (tuplet) {
case 0:
/* removed 'lib' and extensions from name */
realname = tok;
break;
case 1:
/* 'so' extension */
if (strcmp(tok, "so") != 0)
goto backout;
break;
case 2:
/* major version extension */
major = strtol(tok, &etok, 10);
if (*tok == '\0' || *etok != '\0')
goto backout;
break;
case 3:
/* minor version extension */
minor = strtol(tok, &etok, 10);
if (*tok == '\0' || *etok != '\0')
goto backout;
break;
/* if we get here, it must be weird */
default:
goto backout;
}
}
cp = (char *)sodp->sod_name;
sodp->sod_name = (long)strdup(realname);
free(cp);
sodp->sod_library = 1;
sodp->sod_major = major;
sodp->sod_minor = minor;
return;
backout:
free((char *)sodp->sod_name);
sodp->sod_name = (long)strdup(name);
}
static void *
__dlopen(const char *name, int mode)
{
struct sod *sodp;
struct so_map *smp;
/*
* A NULL argument returns the current set of mapped objects.
*/
if (name == NULL) {
LM_PRIVATE(link_map_head)->spd_refcount++;
return link_map_head;
}
if ((sodp = (struct sod *)malloc(sizeof(struct sod))) == NULL) {
dlerrno = ENOMEM;
return NULL;
}
build_sod(name, sodp);
if ((smp = map_object(sodp, main_map)) == NULL) {
#ifdef DEBUG
xprintf("%s: %s\n", name, strerror(errno));
#endif
dlerrno = errno;
free((char *)sodp->sod_name);
free(sodp);
return NULL;
}
LM_PRIVATE(smp)->spd_flags |= RTLD_DL;
if (LM_PRIVATE(smp)->spd_refcount++ > 0) {
free((char *)sodp->sod_name);
free(sodp);
return smp;
}
if (load_subs(smp) != 0) {
if (--LM_PRIVATE(smp)->spd_refcount == 0) {
unmap_object(smp);
free_link_map(smp);
}
return NULL;
}
init_maps(smp);
return smp;
}
static int
__dlclose(void *fd)
{
struct so_map *smp = (struct so_map *)fd;
#ifdef DEBUG
xprintf("dlclose(%s): refcount = %d\n", smp->som_path, LM_PRIVATE(smp)->spd_refcount);
#endif
if (--LM_PRIVATE(smp)->spd_refcount != 0)
return 0;
if ((LM_PRIVATE(smp)->spd_flags & RTLD_DL) == 0)
return 0;
/* Dismantle shared object map and descriptor */
call_map(smp, "__fini");
call_map(smp, "__GLOBAL__DD");
#if 0
unload_subs(smp); /* XXX should unload implied objects */
#endif
unmap_object(smp);
free_link_map(smp);
return 0;
}
static void *
__dlsym(void *fd, const char *sym)
{
struct so_map *smp, *src_map = NULL;
struct nzlist *np;
long addr;
/*
* Restrict search to passed map if dlopen()ed.
*/
if (fd == NULL)
smp = link_map_head;
else
src_map = smp = (struct so_map *)fd;
np = lookup(sym, &src_map, 1);
if (np == NULL) {
dlerrno = ENOENT;
return NULL;
}
/* Fixup jmpslot so future calls transfer directly to target */
addr = np->nz_value;
if (src_map)
addr += (long)src_map->som_addr;
return (void *)addr;
}
static int
__dlctl(void *fd, int cmd, void *arg)
{
switch (cmd) {
case DL_GETERRNO:
*(int *)arg = dlerrno;
dlerrno = 0;
return 0;
default:
dlerrno = EOPNOTSUPP;
return -1;
}
return 0;
}
static void
__dlexit(void)
{
struct so_map *smp;
/* Call any object initialization routines. */
for (smp = link_map_head; smp; smp = smp->som_next) {
if (LM_PRIVATE(smp)->spd_flags & RTLD_RTLD)
continue;
call_map(smp, ".fini");
call_map(smp, "__GLOBAL__DD");
}
}
void
#if __STDC__
xprintf(char *fmt, ...)
#else
xprintf(fmt, va_alist)
char *fmt;
#endif
{
char buf[256];
va_list ap;
#if __STDC__
va_start(ap, fmt);
#else
va_start(ap);
#endif
vsprintf(buf, fmt, ap);
(void)write(1, buf, strlen(buf));
va_end(ap);
}
|