-
Notifications
You must be signed in to change notification settings - Fork 0
/
dso.c
4142 lines (3446 loc) · 100 KB
/
dso.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
/*
* Copyright 2023-2024 Gaël PORTAY
*
* SPDX-License-Identifier: LGPL-2.1-or-later
*/
#include <unistd.h>
#include <stddef.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <errno.h>
#include <limits.h>
#include <dlfcn.h>
#include <fcntl.h>
#include <link.h>
#include <elf.h>
#include <sys/stat.h>
#include <sys/mman.h>
#ifdef __NetBSD__
#include <sys/exec_elf.h>
#endif
#include <spawn.h>
#include "iamroot.h"
#include "jimregexp.h"
#ifndef ELFOSABI_GNU
#define ELFOSABI_GNU ELFOSABI_LINUX
#endif
extern int next_faccessat(int, const char *, int, int);
#ifdef __NetBSD__
#define next_fstat next___fstat50
extern int next___fstat50(int, struct stat *);
#else
extern void *next_dlopen(const char *, int);
extern int next_fstat(int, struct stat *);
#endif
extern int next_open(const char *, int, mode_t);
static int __getmultiarch()
{
return strtol(_getenv("IAMROOT_MULTIARCH") ?: "0", NULL, 0);
}
/*
* Stolen from musl (include/byteswap.h)
*
* SPDX-FileCopyrightText: The musl Contributors
*
* SPDX-License-Identifier: MIT
*/
static uint16_t __bswap_16__(uint16_t __x)
{
return __x<<8 | __x>>8;
}
static uint32_t __bswap_32__(uint32_t __x)
{
return __x>>24 | (__x>>8&0xff00) | (__x<<8&0xff0000) | __x<<24;
}
static uint64_t __bswap_64__(uint64_t __x)
{
return (__bswap_32__(__x)+0ULL)<<32 | __bswap_32__(__x>>32);
}
static ssize_t __getld_library_path(char *buf, size_t bufsiz, off_t offset)
{
char *curr, *prev, *s;
/* LD_LIBRARY_PATH is unset; return NULL directly! */
curr = _getenv("LD_LIBRARY_PATH");
if (!curr) {
buf[offset] = 0;
goto exit;
}
/*
* ld_library_path is unset; i.e. ld.so has --library-path or bootstrap
* environment.
*
* Return LD_LIBRARY_PATH's value.
*/
prev = _getenv("ld_library_path");
if (!prev) {
_strncpy(buf+offset, curr, bufsiz-offset);
goto exit;
}
/*
* Both values are identicals; i.e. ld.so does not have --library-path,
* and LD_LIBRARY_PATH is left untouched; it is "empty" with no initial
* value!
*
* Return empty string.
*/
if (streq(curr, prev)) {
buf[offset] = 0;
goto exit;
}
/*
* Both values are differents; i.e. ld.so does not have --library-path,
* and LD_LIBRARY_PATH **WAS** touched!
*/
/*
* ld_library_path is **NOT** a substring of LD_LIBRARY_PATH; i.e it
* has reset!
*
* Return LD_LIBRARY_PATH's value.
*/
s = strstr(curr, prev);
if (!s) {
_strncpy(buf+offset, curr, bufsiz-offset);
goto exit;
}
/*
* ld_library_path is a substring of LD_LIBRARY_PATH; i.e. it has been
* touched!
*/
/* Strip leading ld_library_path! */
if (s == curr) {
size_t len;
len = __strlen(prev);
if (s[len] == ':')
s++;
_strncpy(buf+offset, s+len, bufsiz-offset);
goto exit;
}
/* Strip off ld_library_path from LD_LIBRARY_PATH! */
__notice("LD_LIBRARY_PATH: '%s' contains '%s'\n", curr, prev);
_strncpy(buf+offset, curr, bufsiz-offset);
exit:
return strnlen(buf+offset, bufsiz-offset);
}
static int __setld_preload(const char *preload, int overwrite)
{
char buf[PATH_MAX];
char *curr;
int n;
if (!preload || !*preload)
return __set_errno_and_perror(EINVAL, -1);
/* LD_PRELOAD is unset; setenv preload directly! */
curr = _getenv("LD_PRELOAD");
if (!curr)
return _setenv("LD_PRELOAD", preload, overwrite);
n = _snprintf(buf, sizeof(buf), "%s:%s", preload, curr);
if (n == -1)
return -1;
return _setenv("LD_PRELOAD", buf, overwrite);
}
static regex_t *re_ldso;
static regex_t *re_lib;
constructor void dso_init()
{
static regex_t regex_ldso, regex_lib;
const char *ldso = "^ld(64|-[[:alnum:]._-]+)?\\.so(\\.[[:digit:]]+)?$";
const char *lib = "^lib[[:alnum:]+._-]+\\.so(\\.[[:alnum:]_-]+)*$";
int ret;
if (re_ldso)
goto lib;
ret = jim_regcomp(®ex_ldso, ldso, REG_EXTENDED);
if (ret != 0) {
jim_regex_perror("jim_regcomp", ®ex_ldso, ret);
return;
}
re_ldso = ®ex_ldso;
lib:
if (re_lib)
return;
ret = jim_regcomp(®ex_lib, lib, REG_EXTENDED);
if (ret != 0) {
jim_regex_perror("jim_regcomp", ®ex_lib, ret);
return;
}
re_lib = ®ex_lib;
}
destructor void dso_fini()
{
if (!re_lib)
goto ldso;
jim_regfree(re_lib);
re_lib = NULL;
ldso:
if (!re_ldso)
return;
jim_regfree(re_ldso);
re_ldso = NULL;
}
hidden int __is_ldso(const char *path)
{
regmatch_t match;
int ret = 0;
if (!re_ldso)
return 0;
ret = jim_regexec(re_ldso, path, 1, &match, 0);
if (ret > REG_NOMATCH) {
jim_regex_perror("jim_regexec", re_ldso, ret);
return 0;
}
return !ret;
}
static int __is_lib(const char *path)
{
regmatch_t match;
int ret = 0;
if (!re_lib)
return 0;
ret = jim_regexec(re_lib, path, 1, &match, 0);
if (ret > REG_NOMATCH) {
jim_regex_perror("jim_regexec", re_lib, ret);
return 0;
}
return !ret;
}
static char *__path_strncat(char *dst, const char *src, size_t dstsiz)
{
size_t len;
if (!src)
return __set_errno_and_perror(EINVAL, NULL);
len = strlen(src);
if (*dst)
len++;
if (len > (dstsiz-strlen(dst)-1)) /* NULL-terminated */
return __set_errno_and_perror(ENOSPC, NULL);
if (*dst && *src)
_strncat(dst, ":", dstsiz);
return _strncat(dst, src, dstsiz);
}
static int __is_in_path_callback(const char *path, void *user)
{
const char *p = (const char *)user;
return strneq(path, p, PATH_MAX);
}
static int __is_in_path(const char *pathname, const char *path)
{
return __path_iterate(path, __is_in_path_callback, (void *)pathname);
}
static int __is_in_preload_callback(const char *path, void *user)
{
const char *p = (const char *)user;
return strneq(__basename(path), __basename(p), PATH_MAX);
}
static int __is_in_preload(const char *path, const char *preload)
{
return __path_iterate(preload, __is_in_preload_callback, (void *)path);
}
static int __is_preloading_so(const char *so)
{
const char *ld_preload;
ld_preload = _getenv("LD_PRELOAD");
if (!ld_preload)
return 0;
return __is_in_preload(so, ld_preload);
}
static int __preload_so(const char *so)
{
int err;
err = __is_preloading_so("libiamroot.so");
if (err == -1)
return -1;
if (err == 1)
return 1;
err = __is_preloading_so(so);
if (err == -1)
return -1;
return __setld_preload(so, 1);
}
/*
* Stolen and hacked from musl (ldso/dynlink.c)
*
* SPDX-FileCopyrightText: The musl Contributors
*
* SPDX-License-Identifier: MIT
*/
/* The original function name is fixup_rpath() in the musl sources */
static int __variable_has_dynamic_string_tokens(const char *value)
{
const char *s, *t;
int n;
if (!strchr(value, '$'))
return 0;
n = 0;
s = value;
while ((t = strchr(s, '$'))) {
if (__strneq(t, "$ORIGIN") && __strneq(t, "${ORIGIN}"))
return 1;
if (__strneq(t, "$LIB") && __strneq(t, "${LIB}"))
return 1;
if (__strneq(t, "$PLATFORM") && __strneq(t, "${PLATFORM}"))
return 1;
s = t + 1;
n++;
}
return n;
}
static ssize_t __elf_needed(const char *, char *, size_t);
static ssize_t __felf_needed(int, char *, size_t);
static ssize_t __felf_rpath(int, char *, size_t, off_t);
static ssize_t __felf_runpath(int, char *, size_t, off_t);
static int __felf_flags_1(int, uint32_t *);
static ssize_t __felf_deflib(int, char *, size_t, off_t);
static int __elf_so_context(const char *, char **, char **, char **, char **,
uint32_t *, char *, size_t, off_t);
static int __felf_so_context(int, char **, char **, char **, char **,
uint32_t *, char *, size_t, off_t);
static int __ldso_preload_needed(const char *, const char *, char *, size_t,
off_t);
struct __ldso_preload_needed_context {
const char *deflib;
char *buf;
size_t bufsiz;
off_t offset;
};
static int __ldso_preload_needed_callback(const char *needed, void *user)
{
struct __ldso_preload_needed_context *ctx =
(struct __ldso_preload_needed_context *)user;
char buf[PATH_MAX];
ssize_t siz;
int ret;
/* FIXME: skipping */
if (*needed == '/')
return 0;
siz = __path_access(needed, F_OK, ctx->deflib, buf, sizeof(buf));
if (siz == -1 && errno == ENOENT)
__warning("%s: needed library not found in library-path %s\n",
needed, ctx->deflib);
if (siz == -1)
return -1;
/* Ignore none-libraries (i.e. linux-vdso.so.1, ld.so-ish...) */
ret = __is_lib(__basename(buf));
if (ret == 0 && !__is_ldso(__basename(buf)))
__warning("%s: ignoring non-library!\n", __basename(buf));
if (ret == 0)
return 0;
/* The shared object is already in the buffer */
ret = __is_in_path(buf, &ctx->buf[ctx->offset]);
if (ret == -1 || ret == 1)
return 0;
/* Add the needed shared objects to buf */
return __ldso_preload_needed(buf, ctx->deflib, ctx->buf, ctx->bufsiz,
ctx->offset);
}
static int __ldso_preload_needed(const char *path, const char *deflib,
char *buf, size_t bufsiz, off_t offset)
{
struct __ldso_preload_needed_context ctx;
char needed[PATH_MAX];
ssize_t siz;
char *str;
/*
* Add the shared object to path first to avoid circular dependencies,
* if a needed shared object needs this one.
*/
str = __path_strncat(buf+offset, path, bufsiz-offset);
if (!str)
return -1;
/* Add the needed shared objects to path then */
siz = __elf_needed(path, needed, sizeof(needed));
if (siz == -1)
return -1;
ctx.deflib = deflib;
ctx.buf = buf;
ctx.bufsiz = bufsiz;
ctx.offset = offset;
return __path_iterate(needed, __ldso_preload_needed_callback, &ctx);
}
static int __ldso_preload_executable(const char *path, const char *deflib,
char *buf, size_t bufsiz, off_t offset)
{
struct __ldso_preload_needed_context ctx;
char needed[PATH_MAX];
ssize_t siz;
/* Add the needed shared objects to path only */
siz = __elf_needed(path, needed, sizeof(needed));
if (siz == -1)
return -1;
ctx.deflib = deflib;
ctx.buf = buf;
ctx.bufsiz = bufsiz;
ctx.offset = offset;
return __path_iterate(needed, __ldso_preload_needed_callback, &ctx);
}
static int __secure_execution_mode();
/*
* Note: This resolves the shared object as described by ld-linux.so(8).
*/
static ssize_t __ldso_access(const char *path,
int mode,
const char *exec_rpath,
const char *exec_runpath,
const char *rpath,
const char *ld_library_path,
const char *runpath,
const char *deflib,
uint32_t flags_1,
char *buf,
size_t bufsiz)
{
ssize_t ret = -1;
/*
* According to ld-linux.so(8)
*
* If a shared object dependency does not contain a slash, then it is
* searched for in the following order:
*/
if (strchr(path, '/')) {
_strncpy(buf, path, bufsiz);
return strnlen(buf, bufsiz);
}
/* Search for NAME in several places. */
/*
* (1) Using the directories specified in the DT_RPATH dynamic section
* attribute of the binary if present and DT_RUNPATH attribute does not
* exist. Use of DT_RPATH is deprecated.
*/
if (!runpath) {
/*
* According to glibc (elf/dl-load.c)
*
* First try the DT_RPATH of the dependent object that caused
* NAME to be loaded. Then that object's dependent, and on up.
*/
__info("%s: trying accessing from shared object DT_RPATH %s...\n",
path, rpath);
ret = __path_access(path, mode, rpath, buf, bufsiz);
if (ret == -1 && errno != ENOENT)
return -1;
if (ret > 0)
return ret;
/*
* If dynamically linked, try the DT_RPATH of the executable
* itself.
*/
__info("%s: trying accessing from executable DT_RPATH %s...\n",
path, exec_rpath);
ret = __path_access(path, mode, exec_rpath, buf, bufsiz);
if (ret == -1 && errno != ENOENT)
return -1;
if (ret > 0)
return ret;
/*
* Also try DT_RUNPATH in the executable for LD_AUDIT dlopen
* call.
*/
/* TODO: This is not usefull, at least for now. */
(void)exec_runpath;
}
/*
* (2) Using the environment variable LD_LIBRARY_PATH, unless the
* executable is being run in secure-execution mode (see below), in
* which case this variable is ignored.
*/
if (ld_library_path && !__secure_execution_mode()) {
__info("%s: trying accessing from LD_LIBRARY_PATH %s...\n",
path, ld_library_path);
ret = __path_access(path, mode, ld_library_path, buf, bufsiz);
if (ret == -1 && errno != ENOENT)
return -1;
if (ret > 0)
return ret;
}
/*
* (3) Using the directories specified in the DT_RUNPATH dynamic
* section attribute of the binary if present. Such directories are
* searched only to find those objects required by DT_NEEDED (direct
* dependencies) entries and do not apply to those objects' children,
* which must themselves have their own DT_RUNPATH entries. This is
* unlike DT_RPATH, which is applied to searches for all children in
* the dependency tree.
*/
if (runpath) {
__info("%s: trying accessing from shared object DT_RUNPATH %s...\n",
path, runpath);
ret = __path_access(path, mode, runpath, buf, bufsiz);
if (ret == -1 && errno != ENOENT)
return -1;
if (ret > 0)
return ret;
}
/*
* (4) From the cache file /etc/ld.so.cache, which contains a compiled
* list of candidate shared objects previously found in the augmented
* library path. If, however, the binary was linked with the -z
* nodeflib linker option, shared objects in the default paths are
* skipped. Shared objects installed in hardware capability directories
* are preferred to other shared objects.
*/
__info("%s: trying accessing from cache...\n", path);
ret = __ldso_cache(path, buf, bufsiz);
if (ret == -1 && errno != ENOENT)
return -1;
if (ret > 0)
return ret;
/*
* (5) In the default path /lib, and then /usr/lib. (On some 64-bit
* architectures, the default paths for 64-bit shared objects are
* /lib64, and then /usr/lib64.) If the binary was linked with the
* -z nodeflib linker option, this step is skipped.
*/
if (!(flags_1 & DF_1_NODEFLIB) && deflib) {
__info("%s: trying accessing from default library path %s...\n",
path, deflib);
ret = __path_access(path, mode, deflib, buf, bufsiz);
if (ret == -1 && errno != ENOENT)
return -1;
if (ret > 0)
return ret;
}
return __set_errno(ENOENT, -1);
}
#ifndef __NetBSD__
#if defined(__linux__) || defined(__FreeBSD__)
static int __dl_is_opened(const char *path)
{
struct link_map *dso;
void *handle;
int err;
handle = next_dlopen(NULL, RTLD_NOW);
if (!handle)
return -1;
err = dlinfo(handle, RTLD_DI_LINKMAP, &dso);
if (err == -1)
return -1;
while (dso) {
if (streq(path, dso->l_name))
return 1;
dso = dso->l_next;
}
return 0;
}
#else
static int __dl_is_opened_callback(struct dl_phdr_info *info, size_t size,
void *data)
{
const char *path = (const char *)data;
(void)size;
return streq(__basename(path), __basename(info->dlpi_name));
}
static int __dl_is_opened(const char *path)
{
return dl_iterate_phdr(__dl_is_opened_callback, (void *)path);
}
#endif
static int __ld_open_needed(const char *, int, const char *);
struct __ld_needed_context {
const char *rpath;
const char *ld_library_path;
const char *runpath;
const char *deflib;
uint32_t flags_1;
int (*callback)(const char *, const char *, const char *,
const char *, const char *, uint32_t, void *);
void *user;
};
static int __ld_needed_callback(const char *needed, void *user)
{
struct __ld_needed_context *ctx = (struct __ld_needed_context *)user;
if (!ctx->callback)
return __set_errno_and_perror(EINVAL, -1);
return ctx->callback(needed, ctx->rpath, ctx->ld_library_path,
ctx->runpath, ctx->deflib, ctx->flags_1,
ctx->user);
}
static int __fld_needed(int fd,
const char *rpath,
const char *ld_library_path,
const char *runpath,
const char *deflib,
uint32_t flags_1,
int (*callback)(const char *, const char *,
const char *, const char *,
const char *, uint32_t, void *),
void *user)
{
struct __ld_needed_context ctx;
char needed[PATH_MAX];
ssize_t siz;
/* Iterate over the DT_NEEDED shared objects */
siz = __felf_needed(fd, needed, sizeof(needed));
if (siz == -1)
return -1;
ctx.rpath = rpath;
ctx.ld_library_path = ld_library_path;
ctx.runpath = runpath;
ctx.deflib = deflib;
ctx.flags_1 = flags_1;
ctx.callback = callback;
ctx.user = user;
return __path_iterate(needed, __ld_needed_callback, &ctx);
}
#ifndef __OpenBSD__
static int __ld_needed(const char *path,
const char *rpath,
const char *ld_library_path,
const char *runpath,
const char *deflib,
uint32_t flags_1,
int (*callback)(const char *, const char *,
const char *, const char *,
const char *, uint32_t, void *),
void *user)
{
ssize_t ret;
int fd;
fd = next_open(path, O_RDONLY | O_CLOEXEC, 0);
if (fd == -1)
return -1;
ret = __fld_needed(fd, rpath, ld_library_path, runpath, deflib,
flags_1, callback, user);
__close(fd);
return ret;
}
#endif
struct __ld_open_needed_context {
int flags;
const char *needed_by;
};
static int __ld_open_needed_callback(const char *needed,
const char *rpath,
const char *ld_library_path,
const char *runpath,
const char *deflib,
uint32_t flags_1,
void *user)
{
struct __ld_open_needed_context *ctx =
(struct __ld_open_needed_context *)user;
char buf[PATH_MAX];
void *handle;
ssize_t siz;
int ret;
/* Look for the shared object */
siz = __ldso_access(needed, F_OK, NULL, NULL, rpath, ld_library_path,
runpath, deflib, flags_1, buf, sizeof(buf));
if (siz == -1)
return -1;
/* Open the needed shared objects first */
ret = __ld_open_needed(buf, ctx->flags, ctx->needed_by);
if (ret == -1)
return -1;
/* Open the shared object then */
handle = next_dlopen(buf, ctx->flags);
if (!handle)
return -1;
__notice("%s: dlopen'ed needed shared object \"%s\"\n", ctx->needed_by,
buf);
return 0;
}
static int __fld_open_needed(int fd, int flags, const char *needed_by)
{
char *deflib, *ld_library_path, *rpath, *runpath;
struct __ld_open_needed_context ctx;
char tmp[PATH_MAX];
uint32_t flags_1;
ssize_t siz;
int ret;
siz = fpath(fd, tmp, sizeof(tmp));
if (siz == -1)
return -1;
/* The shared object is already opened */
ret = __dl_is_opened(tmp);
if (ret == -1 || ret == 1)
return ret;
/* Get the ELF shared object context */
ret = __felf_so_context(fd, &rpath, &ld_library_path, &runpath,
&deflib, &flags_1, tmp, sizeof(tmp), 0);
if (ret == -1)
return -1;
/* Open the needed shared objects */
ctx.flags = flags;
ctx.needed_by = needed_by;
return __fld_needed(fd, rpath, ld_library_path, runpath, deflib,
flags_1, __ld_open_needed_callback, &ctx);
}
static int __ld_open_needed(const char *path, int flags, const char *needed_by)
{
int fd, ret;
fd = next_open(path, O_RDONLY | O_CLOEXEC, 0);
if (fd == -1)
return -1;
ret = __fld_open_needed(fd, flags, needed_by);
__close(fd);
return ret;
}
hidden int __dlopen_needed(const char *path, int flags)
{
return __ld_open_needed(path, flags, __execfn());
}
#endif
static int __ld_ldso_abi(const char *path, char ldso[NAME_MAX], int *abi)
{
const char *name;
int n;
name = __basename(path);
if (!name)
return __set_errno(ENOTSUP, -1);
/* Generic dynamic loader (ld.so) */
if (streq(name, "ld.so")) {
*ldso = 0;
if (abi)
*abi = -1;
return 0;
}
/* Linux dynamic loader (ld.so.<abi>) */
n = sscanf(name, "ld.so.%i", abi);
if (n == 1) {
*ldso = 0;
return 0;
}
/* Linux 64 dynamic loader (ld64.so.<abi>) */
n = sscanf(name, "ld64.so.%i", abi);
if (n == 1) {
*ldso = 0;
return 0;
}
/* NetBSD dynamic loader (ld.<object>_so) */
n = sscanf(name, "ld.%" __xstr(NAME_MAX) "[^_]_so", ldso);
if (n == 1) {
if (abi)
*abi = -1;
return 0;
}
/* Linux or FreeBSD dynamic loader (ld-<ldso>.so.<abi>) */
n = sscanf(name, "ld-%" __xstr(NAME_MAX) "[^.].so.%i", ldso, abi);
if (n == 2)
return 0;
return __set_errno(ENOTSUP, -1);
}
static int __ld_linux_version(const char *path, int *major, int *minor)
{
char buf[PATH_MAX];
ssize_t siz;
int n;
siz = path_resolution(AT_FDCWD, path, buf, sizeof(buf),
AT_SYMLINK_FOLLOW);
if (siz == -1)
return -1;
n = sscanf(__basename(buf), "ld-%i.%i.so", major, minor);
if (n < 2)
return __set_errno(ENOTSUP, -1);
return 0;
}
static int __ld_linux_has_inhibit_cache_option(const char *path)
{
const int errno_save = errno;
int ret, maj = 0, min = 0;
ret = __ld_linux_version(path, &maj, &min);
if ((ret == -1) && (errno != ENOTSUP))
return -1;
if (ret == -1)
return __set_errno(errno_save, 1);
/* --inhibit-cache is supported since glibc 2.16 */
ret = (maj > 2) || ((maj == 2) && (min >= 16));
return __set_errno(errno_save, ret);
}
static int __ld_linux_has_argv0_option(const char *path)
{
const int errno_save = errno;
int ret, maj = 0, min = 0;
ret = __ld_linux_version(path, &maj, &min);
if ((ret == -1) && (errno != ENOTSUP))
return -1;
if (ret == -1)
return __set_errno(errno_save, 1);
/* --argv0 is supported since glibc 2.33 */
ret = (maj > 2) || ((maj == 2) && (min >= 33));
return __set_errno(errno_save, ret);
}
static int __ld_linux_has_preload_option(const char *path)
{
const int errno_save = errno;
int ret, maj = 0, min = 0;
ret = __ld_linux_version(path, &maj, &min);
if ((ret == -1) && (errno != ENOTSUP))
return -1;
if (ret == -1)
return __set_errno(errno_save, 1);
/* --preload is supported since glibc 2.30 */
ret = (maj > 2) || ((maj == 2) && (min >= 30));
return __set_errno(errno_save, ret);
}
static int __elf32_swap(Elf32_Ehdr *ehdr)
{
union { int i; char c[4]; } u = { 1 };
return (u.c[3] + 1) != ehdr->e_ident[EI_DATA];
}
static uint16_t __elf32_half(Elf32_Ehdr *ehdr, uint16_t value)
{
if (!__elf32_swap(ehdr))
return value;
return __bswap_16__(value);
}
static uint32_t __elf32_word(Elf32_Ehdr *ehdr, uint32_t value)
{
if (!__elf32_swap(ehdr))
return value;
return __bswap_32__(value);
}
static uint32_t __elf32_address(Elf32_Ehdr *ehdr, uint32_t value)
{
if (!__elf32_swap(ehdr))
return value;
return __bswap_32__(value);
}
static uint32_t __elf32_offset(Elf32_Ehdr *ehdr, uint32_t value)
{
if (!__elf32_swap(ehdr))
return value;
return __bswap_32__(value);
}
static int __elf32_phdr(int fd, Elf32_Ehdr *ehdr, Elf32_Phdr *phdr,
off_t offset)
{
ssize_t siz;
siz = pread(fd, phdr, sizeof(*phdr), offset);
if (siz == -1)
return -1;
else if ((size_t)siz < sizeof(*phdr))
return __set_errno_and_perror(EIO, -1);
if (!__elf32_swap(ehdr))
return 0;
phdr->p_type = __bswap_32__(phdr->p_type);
phdr->p_offset = __bswap_32__(phdr->p_offset);
phdr->p_vaddr = __bswap_32__(phdr->p_vaddr);
phdr->p_paddr = __bswap_32__(phdr->p_paddr);
phdr->p_filesz = __bswap_32__(phdr->p_filesz);
phdr->p_memsz = __bswap_32__(phdr->p_memsz);
phdr->p_flags = __bswap_32__(phdr->p_flags);
phdr->p_align = __bswap_32__(phdr->p_align);
return 0;
}
static int __elf32_shdr(int fd, Elf32_Ehdr *ehdr, Elf32_Shdr *shdr,