-
Notifications
You must be signed in to change notification settings - Fork 59
/
libtree.c
1837 lines (1585 loc) · 55.4 KB
/
libtree.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
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <glob.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/utsname.h>
#include <unistd.h>
#define VERSION "3.2.0-dev"
#define ET_EXEC 2
#define ET_DYN 3
#define PT_NULL 0
#define PT_LOAD 1
#define PT_DYNAMIC 2
#define DT_NULL 0
#define DT_NEEDED 1
#define DT_STRTAB 5
#define DT_SONAME 14
#define DT_RPATH 15
#define DT_RUNPATH 29
#define BITS32 1
#define BITS64 2
#define ERR_INVALID_MAGIC 11
#define ERR_INVALID_CLASS 12
#define ERR_INVALID_DATA 13
#define ERR_INVALID_HEADER 14
#define ERR_INVALID_BITS 15
#define ERR_INVALID_ENDIANNESS 16
#define ERR_NO_EXEC_OR_DYN 17
#define ERR_INVALID_PHOFF 18
#define ERR_INVALID_PROG_HEADER 19
#define ERR_CANT_STAT 20
#define ERR_INVALID_DYNAMIC_SECTION 21
#define ERR_INVALID_DYNAMIC_ARRAY_ENTRY 22
#define ERR_NO_STRTAB 23
#define ERR_INVALID_SONAME 24
#define ERR_INVALID_RPATH 25
#define ERR_INVALID_RUNPATH 26
#define ERR_INVALID_NEEDED 27
#define ERR_DEPENDENCY_NOT_FOUND 28
#define ERR_NO_PT_LOAD 29
#define ERR_VADDRS_NOT_ORDERED 30
#define ERR_COULD_NOT_OPEN_FILE 31
#define ERR_INCOMPATIBLE_ISA 32
#define DT_FLAGS_1 0x6ffffffb
#define DT_1_NODEFLIB 0x800
#define MAX_OFFSET_T 0xFFFFFFFFFFFFFFFF
#define REGULAR_RED "\033[0;31m"
#define BOLD_RED "\033[1;31m"
#define CLEAR "\033[0m"
#define BOLD_YELLOW "\033[33m"
#define BOLD_CYAN "\033[1;36m"
#define REGULAR_CYAN "\033[0;36m"
#define REGULAR_MAGENTA "\033[0;35m"
#define REGULAR_BLUE "\033[0;34m"
#define BRIGHT_BLACK "\033[0;90m"
#define REGULAR "\033[0m"
// don't judge me.
#define LIGHT_HORIZONTAL "\xe2\x94\x80"
#define LIGHT_QUADRUPLE_DASH_VERTICAL "\xe2\x94\x8a"
#define LIGHT_UP_AND_RIGHT "\xe2\x94\x94"
#define LIGHT_VERTICAL "\xe2\x94\x82"
#define LIGHT_VERTICAL_AND_RIGHT "\xe2\x94\x9c"
#define JUST_INDENT " "
#define LIGHT_VERTICAL_WITH_INDENT LIGHT_VERTICAL " "
#define SMALL_VEC_SIZE 16
#define MAX_RECURSION_DEPTH 32
#define MAX_PATH_LENGTH 4096
// Libraries we do not show by default -- this reduces the verbosity quite a
// bit.
char const *exclude_list[] = {"ld-linux-aarch64.so",
"ld-linux-armhf.so",
"ld-linux-x86-64.so",
"ld-linux.so",
"ld64.so",
"libc.musl-aarch64.so",
"libc.musl-armhf.so",
"libc.musl-i386.so",
"libc.musl-x86_64.so",
"libc.so",
"libdl.so",
"libgcc_s.so",
"libm.so",
"libstdc++.so"};
struct header_64_t {
uint16_t e_type;
uint16_t e_machine;
uint32_t e_version;
uint64_t e_entry;
uint64_t e_phoff;
uint64_t e_shoff;
uint32_t e_flags;
uint16_t e_ehsize;
uint16_t e_phentsize;
uint16_t e_phnum;
uint16_t e_shentsize;
uint16_t e_shnum;
uint16_t e_shstrndx;
};
struct header_32_t {
uint16_t e_type;
uint16_t e_machine;
uint32_t e_version;
uint32_t e_entry;
uint32_t e_phoff;
uint32_t e_shoff;
uint32_t e_flags;
uint16_t e_ehsize;
uint16_t e_phentsize;
uint16_t e_phnum;
uint16_t e_shentsize;
uint16_t e_shnum;
uint16_t e_shstrndx;
};
struct prog_64_t {
uint32_t p_type;
uint32_t p_flags;
uint64_t p_offset;
uint64_t p_vaddr;
uint64_t p_paddr;
uint64_t p_filesz;
uint64_t p_memsz;
uint64_t p_align;
};
struct prog_32_t {
uint32_t p_type;
uint32_t p_offset;
uint32_t p_vaddr;
uint32_t p_paddr;
uint32_t p_filesz;
uint32_t p_memsz;
uint32_t p_flags;
uint32_t p_align;
};
struct dyn_64_t {
int64_t d_tag;
uint64_t d_val;
};
struct dyn_32_t {
int32_t d_tag;
uint32_t d_val;
};
struct compat_t {
char any; // 1 iff we don't look for libs matching a certain architecture
uint8_t class; // 32 or 64 bits?
uint16_t machine; // instruction set
};
typedef enum {
INPUT,
DIRECT,
RPATH,
LD_LIBRARY_PATH,
RUNPATH,
LD_SO_CONF,
DEFAULT
} how_t;
struct found_t {
how_t how;
// only set when found by in the rpath NOT of the direct parent. so, when
// it is found in a "special" way only rpaths allow, which is worth
// informing the user about.
size_t depth;
};
// large buffer in which to copy rpaths, needed libraries and sonames.
struct string_table_t {
char *arr;
size_t n;
size_t capacity;
};
struct visited_file_t {
dev_t st_dev;
ino_t st_ino;
};
struct visited_file_array_t {
struct visited_file_t *arr;
size_t n;
size_t capacity;
};
struct libtree_state_t {
int verbosity;
int path;
int color;
char *ld_conf_file;
unsigned long max_depth;
struct string_table_t string_table;
struct visited_file_array_t visited;
// rpath substitutions values (note: OSNAME/OSREL are FreeBSD specific, LIB
// is glibc/Linux specific -- we substitute all so we can support
// cross-compiled binaries).
char *PLATFORM;
char *LIB;
char *OSNAME;
char *OSREL;
// rpath stack: if lib_a needs lib_b needs lib_c and all have rpaths
// then first lib_c's rpaths are considered, then lib_b's, then lib_a's.
// so this data structure keeps a list of offsets into the string buffer
// where rpaths start, like [lib_a_rpath_offset, lib_b_rpath_offset,
// lib_c_rpath_offset]...
size_t rpath_offsets[MAX_RECURSION_DEPTH];
size_t ld_library_path_offset;
size_t default_paths_offset;
size_t ld_so_conf_offset;
// This is so we know we have to print a | or white space
// in the tree
char found_all_needed[MAX_RECURSION_DEPTH];
};
// Keep track of the files we've see
/**
* small_vec_u64 is an array that lives on the stack until it grows to the heap
*/
struct small_vec_u64_t {
uint64_t buf[SMALL_VEC_SIZE];
uint64_t *p;
size_t n;
size_t capacity;
};
static inline void utoa(char *str, size_t v) {
char *p = str;
do {
*p++ = '0' + (v % 10);
v /= 10;
} while (v > 0);
size_t len = p - str;
for (size_t i = 0; i < len / 2; i++) {
char tmp = str[i];
str[i] = str[len - i - 1];
str[len - i - 1] = tmp;
}
str[len] = '\0';
}
static inline void small_vec_u64_init(struct small_vec_u64_t *v) {
memset(v, 0, sizeof(*v));
v->p = v->buf;
}
static void small_vec_u64_append(struct small_vec_u64_t *v, uint64_t val) {
// The hopefully likely path
if (v->n < SMALL_VEC_SIZE) {
v->p[v->n++] = val;
return;
}
// The slow fallback on the heap
if (v->n == SMALL_VEC_SIZE) {
v->capacity = 2 * SMALL_VEC_SIZE;
v->p = malloc(v->capacity * sizeof(uint64_t));
if (v->p == NULL)
exit(1);
memcpy(v->p, v->buf, SMALL_VEC_SIZE * sizeof(uint64_t));
} else if (v->n == v->capacity) {
v->capacity *= 2;
uint64_t *p = realloc(v->p, v->capacity * sizeof(uint64_t));
if (p == NULL)
exit(1);
v->p = p;
}
v->p[v->n++] = val;
}
static void small_vec_u64_free(struct small_vec_u64_t *v) {
if (v->n <= SMALL_VEC_SIZE)
return;
free(v->p);
v->p = NULL;
}
/**
* end of small_vec_u64_t
*/
static inline int host_is_little_endian() {
int test = 1;
char *bytes = (char *)&test;
return bytes[0] == 1;
}
static int is_ascending_order(uint64_t *v, size_t n) {
for (size_t j = 1; j < n; ++j)
if (v[j - 1] >= v[j])
return 0;
return 1;
}
static void string_table_maybe_grow(struct string_table_t *t, size_t n) {
// The likely case of not having to resize
if (t->n + n <= t->capacity)
return;
// Otherwise give twice the amount of required space.
t->capacity = 2 * (t->n + n);
char *arr = realloc(t->arr, t->capacity * sizeof(char));
if (arr == NULL) {
exit(1);
}
t->arr = arr;
}
static void string_table_store(struct string_table_t *t, char const *str) {
size_t n = strlen(str) + 1;
string_table_maybe_grow(t, n);
memcpy(t->arr + t->n, str, n);
t->n += n;
}
static void string_table_copy_from_file(struct string_table_t *t, FILE *fptr) {
int c;
// TODO: this could be a bit more efficient...
while ((c = getc(fptr)) != '\0' && c != EOF) {
string_table_maybe_grow(t, 1);
t->arr[t->n++] = c;
}
string_table_maybe_grow(t, 1);
t->arr[t->n++] = '\0';
}
static int is_in_exclude_list(char *soname) {
// Get to the end.
char *start = soname;
char *end = strrchr(start, '\0');
// Empty needed string, is that even possible?
if (start == end)
return 0;
--end;
// Strip "1234567890." from the right.
while (end != start && ((*end >= '0' && *end <= '9') || *end == '.')) {
--end;
}
// Check if we should skip this one.
for (size_t j = 0; j < sizeof(exclude_list) / sizeof(char *); ++j) {
size_t len = strlen(exclude_list[j]);
if (strncmp(start, exclude_list[j], len) != 0)
continue;
return 1;
}
return 0;
}
static void tree_preamble(struct libtree_state_t *s, size_t depth) {
if (depth == 0)
return;
for (size_t i = 0; i < depth - 1; ++i)
fputs(s->found_all_needed[i] ? JUST_INDENT : LIGHT_VERTICAL_WITH_INDENT,
stdout);
fputs(s->found_all_needed[depth - 1]
? LIGHT_UP_AND_RIGHT LIGHT_HORIZONTAL LIGHT_HORIZONTAL " "
: LIGHT_VERTICAL_AND_RIGHT LIGHT_HORIZONTAL LIGHT_HORIZONTAL " ",
stdout);
}
static int recurse(char *current_file, size_t depth,
struct libtree_state_t *state, struct compat_t compat,
struct found_t reason);
static void apply_exclude_list(size_t *needed_not_found,
struct small_vec_u64_t *needed_buf_offsets,
struct libtree_state_t *s) {
for (size_t i = 0; i < *needed_not_found;) {
// If in exclude list, swap to the back.
if (is_in_exclude_list(s->string_table.arr +
needed_buf_offsets->p[i])) {
size_t tmp = needed_buf_offsets->p[i];
needed_buf_offsets->p[i] =
needed_buf_offsets->p[*needed_not_found - 1];
needed_buf_offsets->p[--*needed_not_found] = tmp;
continue;
} else {
++i;
}
}
}
static int check_absolute_paths(size_t *needed_not_found,
struct small_vec_u64_t *needed_buf_offsets,
size_t depth, struct libtree_state_t *s,
struct compat_t compat) {
int exit_code = 0;
// First go over absolute paths in needed libs.
for (size_t i = 0; i < *needed_not_found;) {
struct string_table_t const *st = &s->string_table;
// Skip dt_needed that have do not contain /
if (strchr(st->arr + needed_buf_offsets->p[i], '/') == NULL) {
++i;
continue;
}
// Copy the path over.
char path[MAX_PATH_LENGTH];
size_t len = strlen(st->arr + needed_buf_offsets->p[i]);
// Unlikely to happen but good to guard against
if (len >= MAX_PATH_LENGTH)
continue;
// Include \0
memcpy(path, st->arr + needed_buf_offsets->p[i], len + 1);
s->found_all_needed[depth] = *needed_not_found <= 1;
char *err = NULL;
// If it is not an absolute path, we bail, cause it then starts to
// depend on the current working directory, which is rather
// nonsensical. This is allowed by glibc though.
if (path[0] != '/') {
err = " is not absolute";
exit_code = ERR_DEPENDENCY_NOT_FOUND;
} else {
int code = recurse(path, depth + 1, s, compat,
(struct found_t){.how = DIRECT});
if (code == ERR_DEPENDENCY_NOT_FOUND)
exit_code = ERR_DEPENDENCY_NOT_FOUND;
// Check if there was an issue with the direct dep and ignore errors
// of transient deps.
if (code != 0 && code != ERR_DEPENDENCY_NOT_FOUND) {
err = " not found";
}
}
if (err) {
tree_preamble(s, depth + 1);
if (s->color)
fputs(BOLD_RED, stdout);
fputs(path, stdout);
fputs(" is not absolute", stdout);
fputs(s->color ? CLEAR "\n" : "\n", stdout);
}
// Handled this library, so swap to the back.
size_t tmp = needed_buf_offsets->p[i];
needed_buf_offsets->p[i] = needed_buf_offsets->p[*needed_not_found - 1];
needed_buf_offsets->p[--*needed_not_found] = tmp;
}
return exit_code;
}
static int check_search_paths(struct found_t reason, size_t offset,
size_t *needed_not_found,
struct small_vec_u64_t *needed_buf_offsets,
size_t depth, struct libtree_state_t *s,
struct compat_t compat) {
int exit_code = 0;
char path[MAX_PATH_LENGTH];
char *path_end = path + MAX_PATH_LENGTH;
struct string_table_t const *st = &s->string_table;
while (st->arr[offset] != '\0') {
// First remove trailing colons
while (st->arr[offset] == ':' && st->arr[offset] != '\0')
++offset;
// Check if it was only colons
if (st->arr[offset] == '\0')
return exit_code;
// Copy the search path until the first \0 or :
char *dest = path;
while (st->arr[offset] != '\0' && st->arr[offset] != ':' &&
dest != path_end)
*dest++ = st->arr[offset++];
// Path too long... Can't handle.
if (dest + 1 >= path_end)
continue;
// Add a separator if necessary
if (*(dest - 1) != '/')
*dest++ = '/';
// Keep track of the end of the current search path.
char *search_path_end = dest;
// Try to open it -- if we've found anything, swap it with the back.
for (size_t i = 0; i < *needed_not_found;) {
size_t soname_len = strlen(st->arr + needed_buf_offsets->p[i]);
// Path too long, can't handle.
if (search_path_end + soname_len + 1 >= path_end)
continue;
// Otherwise append.
memcpy(search_path_end, st->arr + needed_buf_offsets->p[i],
soname_len + 1);
s->found_all_needed[depth] = *needed_not_found <= 1;
// And try to locate the lib.
int code = recurse(path, depth + 1, s, compat, reason);
if (code == ERR_DEPENDENCY_NOT_FOUND)
exit_code = ERR_DEPENDENCY_NOT_FOUND;
if (code == 0 || code == ERR_DEPENDENCY_NOT_FOUND) {
// Found at least the direct dependency, so swap out the current
// soname to the back and reduce the number of to be found by
// one.
size_t tmp = needed_buf_offsets->p[i];
needed_buf_offsets->p[i] =
needed_buf_offsets->p[*needed_not_found - 1];
needed_buf_offsets->p[--(*needed_not_found)] = tmp;
} else {
++i;
}
}
}
return exit_code;
}
static int interpolate_variables(struct libtree_state_t *s, size_t src,
char const *ORIGIN) {
// We do not write to dst if there is no variables to interpolate.
size_t prev_src = src;
size_t curr_src = src;
struct string_table_t *st = &s->string_table;
while (1) {
// Find the next potential variable.
char *dollar = strchr(st->arr + curr_src, '$');
if (dollar == NULL)
break;
curr_src = dollar - st->arr;
size_t bytes_to_dollar = curr_src - prev_src;
// Go past the dollar.
++curr_src;
// Remember if we have to look for matching curly braces.
int curly = 0;
if (st->arr[curr_src] == '{') {
curly = 1;
++curr_src;
}
// String to interpolate.
char const *var_val = NULL;
if (strncmp(&st->arr[curr_src], "ORIGIN", 6) == 0) {
var_val = ORIGIN;
curr_src += 6;
} else if (strncmp(&st->arr[curr_src], "LIB", 3) == 0) {
var_val = s->LIB;
curr_src += 3;
} else if (strncmp(&st->arr[curr_src], "PLATFORM", 8) == 0) {
var_val = s->PLATFORM;
curr_src += 8;
} else if (strncmp(&st->arr[curr_src], "OSNAME", 6) == 0) {
var_val = s->OSNAME;
curr_src += 6;
} else if (strncmp(&st->arr[curr_src], "OSREL", 5) == 0) {
var_val = s->OSREL;
curr_src += 5;
} else {
continue;
}
// Require matching {...}.
if (curly) {
if (st->arr[curr_src] != '}') {
continue;
}
++curr_src;
}
size_t var_len = strlen(var_val);
// Make sure we have enough space to write to.
string_table_maybe_grow(st, bytes_to_dollar + var_len);
// First copy over the string until the variable.
memcpy(&st->arr[s->string_table.n], &st->arr[prev_src],
bytes_to_dollar);
s->string_table.n += bytes_to_dollar;
// Then move prev_src until after the variable.
prev_src = curr_src;
// Then copy the variable value (without null).
memcpy(&st->arr[s->string_table.n], var_val, var_len);
s->string_table.n += var_len;
}
// Did we copy anything? That implies a variable was interpolated.
// Copy the remainder, including the \0.
if (prev_src != src) {
size_t n = strlen(st->arr + prev_src) + 1;
string_table_maybe_grow(st, n);
// note: we're copying from within the string table, so we
// should not store st->arr + prev_src.
memcpy(st->arr + st->n, st->arr + prev_src, n);
st->n += n;
return 1;
}
return 0;
}
static void print_colon_delimited_paths(char const *start, char const *indent) {
while (1) {
// Don't print empty string
if (*start == '\0')
break;
// Find the next delimiter after start
char *next = strchr(start, ':');
// Don't print empty strings
if (start == next) {
++start;
continue;
}
fputs(indent, stdout);
fputs(JUST_INDENT, stdout);
// Print up to but not including : or \0, followed by a newline.
if (next == NULL) {
puts(start);
} else {
fwrite(start, 1, next - start, stdout);
putchar('\n');
}
// We done yet?
if (next == NULL)
break;
// Otherwise put the : back in place and continue.
start = next + 1;
}
}
static void print_line(size_t depth, char *name, char *color_bold,
char *color_regular, int highlight,
struct found_t reason, struct libtree_state_t *s) {
tree_preamble(s, depth);
// Color the filename different than the path name, if we have a path.
char *slash = NULL;
if (s->color && highlight && (slash = strrchr(name, '/')) != NULL) {
fputs(color_regular, stdout);
fwrite(name, 1, slash + 1 - name, stdout);
fputs(color_bold, stdout);
fputs(slash + 1, stdout);
} else {
if (s->color)
fputs(color_bold, stdout);
fputs(name, stdout);
}
if (s->color && highlight)
fputs(CLEAR " " BOLD_YELLOW, stdout);
else
putchar(' ');
switch (reason.how) {
case RPATH:
if (reason.depth + 1 >= depth) {
fputs("[rpath]", stdout);
} else {
char num[8];
utoa(num, reason.depth + 1);
fputs("[rpath of ", stdout);
fputs(num, stdout);
putchar(']');
}
break;
case LD_LIBRARY_PATH:
fputs("[LD_LIBRARY_PATH]", stdout);
break;
case RUNPATH:
fputs("[runpath]", stdout);
break;
case LD_SO_CONF:
putchar('[');
char *conf_name = strrchr(s->ld_conf_file, '/');
conf_name = conf_name == NULL ? s->ld_conf_file : conf_name + 1;
fputs(conf_name, stdout);
putchar(']');
break;
case DIRECT:
fputs("[direct]", stdout);
break;
case DEFAULT:
fputs("[default path]", stdout);
break;
default:
break;
}
if (s->color)
fputs(CLEAR "\n", stdout);
else
putchar('\n');
}
static void print_error(size_t depth, size_t needed_not_found,
struct small_vec_u64_t *needed_buf_offsets,
char *runpath, struct libtree_state_t *s,
int no_def_lib) {
for (size_t i = 0; i < needed_not_found; ++i) {
s->found_all_needed[depth] = i + 1 >= needed_not_found;
tree_preamble(s, depth + 1);
if (s->color)
fputs(BOLD_RED, stdout);
fputs(s->string_table.arr + needed_buf_offsets->p[i], stdout);
fputs(" not found\n", stdout);
if (s->color)
fputs(CLEAR, stdout);
}
// If anything was not found, we print the search paths in order they
// are considered.
char *box_vertical =
s->color ? JUST_INDENT REGULAR_RED LIGHT_QUADRUPLE_DASH_VERTICAL CLEAR
: JUST_INDENT LIGHT_QUADRUPLE_DASH_VERTICAL;
char *indent = malloc(sizeof(LIGHT_VERTICAL_WITH_INDENT) * depth +
strlen(box_vertical) + 1);
char *p = indent;
for (size_t i = 0; i < depth; ++i) {
if (s->found_all_needed[i]) {
int len = sizeof(JUST_INDENT) - 1;
memcpy(p, JUST_INDENT, len);
p += len;
} else {
int len = sizeof(LIGHT_VERTICAL_WITH_INDENT) - 1;
memcpy(p, LIGHT_VERTICAL_WITH_INDENT, len);
p += len;
}
}
// dotted | in red
strcpy(p, box_vertical);
fputs(indent, stdout);
if (s->color)
fputs(BRIGHT_BLACK, stdout);
fputs(" Paths considered in this order:\n", stdout);
if (s->color)
fputs(CLEAR, stdout);
// Consider rpaths only when runpath is empty
fputs(indent, stdout);
if (runpath != NULL) {
if (s->color)
fputs(BRIGHT_BLACK, stdout);
fputs(" 1. rpath is skipped because runpath was set\n", stdout);
if (s->color)
fputs(CLEAR, stdout);
} else {
if (s->color)
fputs(BRIGHT_BLACK, stdout);
fputs(" 1. rpath:\n", stdout);
if (s->color)
fputs(CLEAR, stdout);
for (int j = depth; j >= 0; --j) {
if (s->rpath_offsets[j] != SIZE_MAX) {
char num[8];
utoa(num, j + 1);
fputs(indent, stdout);
if (s->color)
fputs(BRIGHT_BLACK, stdout);
fputs(" depth ", stdout);
fputs(num, stdout);
if (s->color)
fputs(CLEAR, stdout);
putchar('\n');
print_colon_delimited_paths(
s->string_table.arr + s->rpath_offsets[j], indent);
}
}
}
// Environment variables
fputs(indent, stdout);
if (s->color)
fputs(BRIGHT_BLACK, stdout);
fputs(s->ld_library_path_offset == SIZE_MAX
? " 2. LD_LIBRARY_PATH was not set\n"
: " 2. LD_LIBRARY_PATH:\n",
stdout);
if (s->color)
fputs(CLEAR, stdout);
if (s->ld_library_path_offset != SIZE_MAX)
print_colon_delimited_paths(
s->string_table.arr + s->ld_library_path_offset, indent);
// runpath
fputs(indent, stdout);
if (s->color)
fputs(BRIGHT_BLACK, stdout);
fputs(runpath == NULL ? " 3. runpath was not set\n" : " 3. runpath:\n",
stdout);
if (s->color)
fputs(CLEAR, stdout);
if (runpath != NULL)
print_colon_delimited_paths(runpath, indent);
fputs(indent, stdout);
if (s->color)
fputs(BRIGHT_BLACK, stdout);
fputs(no_def_lib
? " 4. ld config files not considered due to NODEFLIB flag\n"
: " 4. ld config files:\n",
stdout);
if (s->color)
fputs(CLEAR, stdout);
print_colon_delimited_paths(s->string_table.arr + s->ld_so_conf_offset,
indent);
fputs(indent, stdout);
if (s->color)
fputs(BRIGHT_BLACK, stdout);
fputs(no_def_lib
? " 5. Standard paths not considered due to NODEFLIB flag\n"
: " 5. Standard paths:\n",
stdout);
if (s->color)
fputs(CLEAR, stdout);
print_colon_delimited_paths(s->string_table.arr + s->default_paths_offset,
indent);
free(indent);
}
static int visited_files_contains(struct visited_file_array_t *files,
struct stat *needle) {
for (size_t i = 0; i < files->n; ++i) {
struct visited_file_t *f = &files->arr[i];
if (f->st_dev == needle->st_dev && f->st_ino == needle->st_ino)
return 1;
}
return 0;
}
static void visited_files_append(struct visited_file_array_t *files,
struct stat *new) {
if (files->n == files->capacity) {
files->capacity *= 2;
files->arr = realloc(files->arr,
files->capacity * sizeof(struct visited_file_t));
if (files->arr == NULL)
exit(1);
}
files->arr[files->n].st_dev = new->st_dev;
files->arr[files->n].st_ino = new->st_ino;
++files->n;
}
static int recurse(char *current_file, size_t depth, struct libtree_state_t *s,
struct compat_t compat, struct found_t reason) {
FILE *fptr = fopen(current_file, "rb");
if (fptr == NULL)
return ERR_COULD_NOT_OPEN_FILE;
// When we're done recursing, we should give back the memory we've claimed.
size_t old_buf_size = s->string_table.n;
// Parse the header
char e_ident[16];
if (fread(&e_ident, 16, 1, fptr) != 1) {
fclose(fptr);
return ERR_INVALID_MAGIC;
}
// Find magic elfs
if (e_ident[0] != 0x7f || e_ident[1] != 'E' || e_ident[2] != 'L' ||
e_ident[3] != 'F') {
fclose(fptr);
return ERR_INVALID_MAGIC;
}
// Do at least *some* header validation
if (e_ident[4] != BITS32 && e_ident[4] != BITS64) {
fclose(fptr);
return ERR_INVALID_CLASS;
}
if (e_ident[5] != '\x01' && e_ident[5] != '\x02') {
fclose(fptr);
return ERR_INVALID_DATA;
}
struct compat_t curr_type = {.any = 0, .class = e_ident[4]};
int is_little_endian = e_ident[5] == '\x01';
// Make sure that we have matching bits with parent
if (!compat.any && compat.class != curr_type.class) {
fclose(fptr);
return ERR_INVALID_BITS;
}
// Make sure that the elf file has a the host's endianness
// Byte swapping is on the TODO list
if (is_little_endian ^ host_is_little_endian()) {
fclose(fptr);
return ERR_INVALID_ENDIANNESS;
}
// And get the type
union {
struct header_64_t h64;
struct header_32_t h32;
} header;
// Read the (rest of the) elf header
if (curr_type.class == BITS64) {
if (fread(&header.h64, sizeof(struct header_64_t), 1, fptr) != 1) {
fclose(fptr);
return ERR_INVALID_HEADER;
}
if (header.h64.e_type != ET_EXEC && header.h64.e_type != ET_DYN) {
fclose(fptr);
return ERR_NO_EXEC_OR_DYN;
}
curr_type.machine = header.h64.e_machine;
if (!compat.any && compat.machine != curr_type.machine) {
fclose(fptr);
return ERR_INCOMPATIBLE_ISA;
}
if (fseek(fptr, header.h64.e_phoff, SEEK_SET) != 0) {
fclose(fptr);
return ERR_INVALID_PHOFF;
}
} else {
if (fread(&header.h32, sizeof(struct header_32_t), 1, fptr) != 1) {
fclose(fptr);
return ERR_INVALID_HEADER;
}
if (header.h32.e_type != ET_EXEC && header.h32.e_type != ET_DYN) {
fclose(fptr);
return ERR_NO_EXEC_OR_DYN;
}
curr_type.machine = header.h32.e_machine;
if (!compat.any && compat.machine != curr_type.machine) {
fclose(fptr);
return ERR_INCOMPATIBLE_ISA;
}
if (fseek(fptr, header.h32.e_phoff, SEEK_SET) != 0) {
fclose(fptr);
return ERR_INVALID_PHOFF;
}
}
// Make sure it's an executable or library
union {
struct prog_64_t p64;
struct prog_32_t p32;
} prog;
// map vaddr to file offset (we don't mmap the file, but directly seek in
// the file which means that we have to translate vaddr to file offset)
struct small_vec_u64_t pt_load_offset;
struct small_vec_u64_t pt_load_vaddr;
small_vec_u64_init(&pt_load_offset);
small_vec_u64_init(&pt_load_vaddr);