forked from bsdjhb/gdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathldelf.c
2136 lines (1848 loc) · 58.9 KB
/
ldelf.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
/* ELF emulation code for targets using elf.em.
Copyright (C) 1991-2019 Free Software Foundation, Inc.
This file is part of the GNU Binutils.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
MA 02110-1301, USA. */
#include "sysdep.h"
#include "bfd.h"
#include "libiberty.h"
#include "filenames.h"
#include "safe-ctype.h"
#include "bfdlink.h"
#include "ctf-api.h"
#include "ld.h"
#include "ldmain.h"
#include "ldmisc.h"
#include "ldexp.h"
#include "ldlang.h"
#include "ldfile.h"
#include "ldemul.h"
#include "ldbuildid.h"
#include <ldgram.h>
#include "elf-bfd.h"
#ifdef HAVE_GLOB
#include <glob.h>
#endif
#include "ldelf.h"
struct dt_needed
{
bfd *by;
const char *name;
};
/* Style of .note.gnu.build-id section. */
const char *ldelf_emit_note_gnu_build_id;
/* These variables are required to pass information back and forth
between after_open and check_needed and stat_needed and vercheck. */
static struct bfd_link_needed_list *global_needed;
static lang_input_statement_type *global_found;
static struct stat global_stat;
static struct bfd_link_needed_list *global_vercheck_needed;
static bfd_boolean global_vercheck_failed;
void
ldelf_after_parse (void)
{
if (bfd_link_pie (&link_info))
link_info.flags_1 |= (bfd_vma) DF_1_PIE;
if (bfd_link_executable (&link_info)
&& link_info.nointerp)
{
if (link_info.dynamic_undefined_weak > 0)
einfo (_("%P: warning: -z dynamic-undefined-weak ignored\n"));
link_info.dynamic_undefined_weak = 0;
}
after_parse_default ();
}
/* Handle the generation of DT_NEEDED tags. */
bfd_boolean
ldelf_load_symbols (lang_input_statement_type *entry)
{
int link_class = 0;
/* Tell the ELF linker that we don't want the output file to have a
DT_NEEDED entry for this file, unless it is used to resolve
references in a regular object. */
if (entry->flags.add_DT_NEEDED_for_regular)
link_class = DYN_AS_NEEDED;
/* Tell the ELF linker that we don't want the output file to have a
DT_NEEDED entry for any dynamic library in DT_NEEDED tags from
this file at all. */
if (!entry->flags.add_DT_NEEDED_for_dynamic)
link_class |= DYN_NO_ADD_NEEDED;
if (entry->flags.just_syms
&& (bfd_get_file_flags (entry->the_bfd) & DYNAMIC) != 0)
einfo (_("%F%P: %pB: --just-symbols may not be used on DSO\n"),
entry->the_bfd);
if (link_class == 0
|| (bfd_get_file_flags (entry->the_bfd) & DYNAMIC) == 0)
return FALSE;
bfd_elf_set_dyn_lib_class (entry->the_bfd,
(enum dynamic_lib_link_class) link_class);
/* Continue on with normal load_symbols processing. */
return FALSE;
}
/* On Linux, it's possible to have different versions of the same
shared library linked against different versions of libc. The
dynamic linker somehow tags which libc version to use in
/etc/ld.so.cache, and, based on the libc that it sees in the
executable, chooses which version of the shared library to use.
We try to do a similar check here by checking whether this shared
library needs any other shared libraries which may conflict with
libraries we have already included in the link. If it does, we
skip it, and try to find another shared library farther on down the
link path.
This is called via lang_for_each_input_file.
GLOBAL_VERCHECK_NEEDED is the list of objects needed by the object
which we are checking. This sets GLOBAL_VERCHECK_FAILED if we find
a conflicting version. */
static void
ldelf_vercheck (lang_input_statement_type *s)
{
const char *soname;
struct bfd_link_needed_list *l;
if (global_vercheck_failed)
return;
if (s->the_bfd == NULL
|| (bfd_get_file_flags (s->the_bfd) & DYNAMIC) == 0)
return;
soname = bfd_elf_get_dt_soname (s->the_bfd);
if (soname == NULL)
soname = lbasename (bfd_get_filename (s->the_bfd));
for (l = global_vercheck_needed; l != NULL; l = l->next)
{
const char *suffix;
if (filename_cmp (soname, l->name) == 0)
{
/* Probably can't happen, but it's an easy check. */
continue;
}
if (strchr (l->name, '/') != NULL)
continue;
suffix = strstr (l->name, ".so.");
if (suffix == NULL)
continue;
suffix += sizeof ".so." - 1;
if (filename_ncmp (soname, l->name, suffix - l->name) == 0)
{
/* Here we know that S is a dynamic object FOO.SO.VER1, and
the object we are considering needs a dynamic object
FOO.SO.VER2, and VER1 and VER2 are different. This
appears to be a version mismatch, so we tell the caller
to try a different version of this library. */
global_vercheck_failed = TRUE;
return;
}
}
}
/* See if an input file matches a DT_NEEDED entry by running stat on
the file. */
static void
ldelf_stat_needed (lang_input_statement_type *s)
{
struct stat st;
const char *suffix;
const char *soname;
if (global_found != NULL)
return;
if (s->the_bfd == NULL)
return;
/* If this input file was an as-needed entry, and wasn't found to be
needed at the stage it was linked, then don't say we have loaded it. */
if ((bfd_elf_get_dyn_lib_class (s->the_bfd) & DYN_AS_NEEDED) != 0)
return;
if (bfd_stat (s->the_bfd, &st) != 0)
{
einfo (_("%P: %pB: bfd_stat failed: %E\n"), s->the_bfd);
return;
}
/* Some operating systems, e.g. Windows, do not provide a meaningful
st_ino; they always set it to zero. (Windows does provide a
meaningful st_dev.) Do not indicate a duplicate library in that
case. While there is no guarantee that a system that provides
meaningful inode numbers will never set st_ino to zero, this is
merely an optimization, so we do not need to worry about false
negatives. */
if (st.st_dev == global_stat.st_dev
&& st.st_ino == global_stat.st_ino
&& st.st_ino != 0)
{
global_found = s;
return;
}
/* We issue a warning if it looks like we are including two
different versions of the same shared library. For example,
there may be a problem if -lc picks up libc.so.6 but some other
shared library has a DT_NEEDED entry of libc.so.5. This is a
heuristic test, and it will only work if the name looks like
NAME.so.VERSION. FIXME: Depending on file names is error-prone.
If we really want to issue warnings about mixing version numbers
of shared libraries, we need to find a better way. */
if (strchr (global_needed->name, '/') != NULL)
return;
suffix = strstr (global_needed->name, ".so.");
if (suffix == NULL)
return;
suffix += sizeof ".so." - 1;
soname = bfd_elf_get_dt_soname (s->the_bfd);
if (soname == NULL)
soname = lbasename (s->filename);
if (filename_ncmp (soname, global_needed->name,
suffix - global_needed->name) == 0)
einfo (_("%P: warning: %s, needed by %pB, may conflict with %s\n"),
global_needed->name, global_needed->by, soname);
}
/* This function is called for each possible name for a dynamic object
named by a DT_NEEDED entry. The FORCE parameter indicates whether
to skip the check for a conflicting version. */
static bfd_boolean
ldelf_try_needed (struct dt_needed *needed, int force, int is_linux)
{
bfd *abfd;
const char *name = needed->name;
const char *soname;
int link_class;
abfd = bfd_openr (name, bfd_get_target (link_info.output_bfd));
if (abfd == NULL)
{
if (verbose)
info_msg (_("attempt to open %s failed\n"), name);
return FALSE;
}
/* Linker needs to decompress sections. */
abfd->flags |= BFD_DECOMPRESS;
if (! bfd_check_format (abfd, bfd_object))
{
bfd_close (abfd);
return FALSE;
}
if ((bfd_get_file_flags (abfd) & DYNAMIC) == 0)
{
bfd_close (abfd);
return FALSE;
}
/* For DT_NEEDED, they have to match. */
if (abfd->xvec != link_info.output_bfd->xvec)
{
bfd_close (abfd);
return FALSE;
}
/* Check whether this object would include any conflicting library
versions. If FORCE is set, then we skip this check; we use this
the second time around, if we couldn't find any compatible
instance of the shared library. */
if (!force)
{
struct bfd_link_needed_list *needs;
if (! bfd_elf_get_bfd_needed_list (abfd, &needs))
einfo (_("%F%P: %pB: bfd_elf_get_bfd_needed_list failed: %E\n"), abfd);
if (needs != NULL)
{
global_vercheck_needed = needs;
global_vercheck_failed = FALSE;
lang_for_each_input_file (ldelf_vercheck);
if (global_vercheck_failed)
{
bfd_close (abfd);
/* Return FALSE to force the caller to move on to try
another file on the search path. */
return FALSE;
}
/* But wait! It gets much worse. On Linux, if a shared
library does not use libc at all, we are supposed to skip
it the first time around in case we encounter a shared
library later on with the same name which does use the
version of libc that we want. This is much too horrible
to use on any system other than Linux. */
if (is_linux)
{
struct bfd_link_needed_list *l;
for (l = needs; l != NULL; l = l->next)
if (CONST_STRNEQ (l->name, "libc.so"))
break;
if (l == NULL)
{
bfd_close (abfd);
return FALSE;
}
}
}
}
/* We've found a dynamic object matching the DT_NEEDED entry. */
/* We have already checked that there is no other input file of the
same name. We must now check again that we are not including the
same file twice. We need to do this because on many systems
libc.so is a symlink to, e.g., libc.so.1. The SONAME entry will
reference libc.so.1. If we have already included libc.so, we
don't want to include libc.so.1 if they are the same file, and we
can only check that using stat. */
if (bfd_stat (abfd, &global_stat) != 0)
einfo (_("%F%P: %pB: bfd_stat failed: %E\n"), abfd);
/* First strip off everything before the last '/'. */
soname = lbasename (bfd_get_filename (abfd));
if (verbose)
info_msg (_("found %s at %s\n"), soname, name);
global_found = NULL;
lang_for_each_input_file (ldelf_stat_needed);
if (global_found != NULL)
{
/* Return TRUE to indicate that we found the file, even though
we aren't going to do anything with it. */
return TRUE;
}
/* Specify the soname to use. */
bfd_elf_set_dt_needed_name (abfd, soname);
/* Tell the ELF linker that we don't want the output file to have a
DT_NEEDED entry for this file, unless it is used to resolve
references in a regular object. */
link_class = DYN_DT_NEEDED;
/* Tell the ELF linker that we don't want the output file to have a
DT_NEEDED entry for this file at all if the entry is from a file
with DYN_NO_ADD_NEEDED. */
if (needed->by != NULL
&& (bfd_elf_get_dyn_lib_class (needed->by) & DYN_NO_ADD_NEEDED) != 0)
link_class |= DYN_NO_NEEDED | DYN_NO_ADD_NEEDED;
bfd_elf_set_dyn_lib_class (abfd, (enum dynamic_lib_link_class) link_class);
/* Add this file into the symbol table. */
if (! bfd_link_add_symbols (abfd, &link_info))
einfo (_("%F%P: %pB: error adding symbols: %E\n"), abfd);
return TRUE;
}
/* Search for a needed file in a path. */
static bfd_boolean
ldelf_search_needed (const char *path, struct dt_needed *n, int force,
int is_linux, int elfsize)
{
const char *s;
const char *name = n->name;
size_t len;
struct dt_needed needed;
if (name[0] == '/')
return ldelf_try_needed (n, force, is_linux);
if (path == NULL || *path == '\0')
return FALSE;
needed.by = n->by;
needed.name = n->name;
len = strlen (name);
while (1)
{
unsigned offset = 0;
char * var;
char *filename, *sset;
s = strchr (path, config.rpath_separator);
if (s == NULL)
s = path + strlen (path);
#if HAVE_DOS_BASED_FILE_SYSTEM
/* Assume a match on the second char is part of drive specifier. */
else if (config.rpath_separator == ':'
&& s == path + 1
&& ISALPHA (*path))
{
s = strchr (s + 1, config.rpath_separator);
if (s == NULL)
s = path + strlen (path);
}
#endif
filename = (char *) xmalloc (s - path + len + 2);
if (s == path)
sset = filename;
else
{
memcpy (filename, path, s - path);
filename[s - path] = '/';
sset = filename + (s - path) + 1;
}
strcpy (sset, name);
/* PR 20535: Support the same pseudo-environment variables that
are supported by ld.so. Namely, $ORIGIN, $LIB and $PLATFORM.
Since there can be more than one occurrence of these tokens in
the path we loop until no more are found. Since we might not
be able to substitute some of the tokens we maintain an offset
into the filename for where we should begin our scan. */
while ((var = strchr (filename + offset, '$')) != NULL)
{
/* The ld.so manual page does not say, but I am going to assume that
these tokens are terminated by a directory separator character
(/) or the end of the string. There is also an implication that
$ORIGIN should only be used at the start of a path, but that is
not enforced here.
The ld.so manual page also states that it allows ${ORIGIN},
${LIB} and ${PLATFORM}, so these are supported as well.
FIXME: The code could be a lot cleverer about allocating space
for the processed string. */
char * end = strchr (var, '/');
const char *replacement = NULL;
char * v = var + 1;
char * freeme = NULL;
unsigned flen = strlen (filename);
if (end != NULL)
/* Temporarily terminate the filename at the end of the token. */
* end = 0;
if (*v == '{')
++ v;
switch (*v++)
{
case 'O':
if (strcmp (v, "RIGIN") == 0 || strcmp (v, "RIGIN}") == 0)
{
/* ORIGIN - replace with the full path to the directory
containing the program or shared object. */
if (needed.by == NULL)
{
if (link_info.output_bfd == NULL)
{
break;
}
else
replacement = bfd_get_filename (link_info.output_bfd);
}
else
replacement = bfd_get_filename (needed.by);
if (replacement)
{
char * slash;
if (replacement[0] == '/')
freeme = xstrdup (replacement);
else
{
char * current_dir = getpwd ();
freeme = xmalloc (strlen (replacement)
+ strlen (current_dir) + 2);
sprintf (freeme, "%s/%s", current_dir, replacement);
}
replacement = freeme;
if ((slash = strrchr (replacement, '/')) != NULL)
* slash = 0;
}
}
break;
case 'L':
if (strcmp (v, "IB") == 0 || strcmp (v, "IB}") == 0)
{
/* LIB - replace with "lib" in 32-bit environments
and "lib64" in 64-bit environments. */
switch (elfsize)
{
case 32: replacement = "lib"; break;
case 64: replacement = "lib64"; break;
default:
abort ();
}
}
break;
case 'P':
/* Supporting $PLATFORM in a cross-hosted environment is not
possible. Supporting it in a native environment involves
loading the <sys/auxv.h> header file which loads the
system <elf.h> header file, which conflicts with the
"include/elf/mips.h" header file. */
/* Fall through. */
default:
break;
}
if (replacement)
{
char * filename2 = xmalloc (flen + strlen (replacement));
if (end)
{
sprintf (filename2, "%.*s%s/%s",
(int)(var - filename), filename,
replacement, end + 1);
offset = (var - filename) + 1 + strlen (replacement);
}
else
{
sprintf (filename2, "%.*s%s",
(int)(var - filename), filename,
replacement);
offset = var - filename + strlen (replacement);
}
free (filename);
filename = filename2;
/* There is no need to restore the path separator (when
end != NULL) as we have replaced the entire string. */
}
else
{
if (verbose)
/* We only issue an "unrecognised" message in verbose mode
as the $<foo> token might be a legitimate component of
a path name in the target's file system. */
info_msg (_("unrecognised or unsupported token "
"'%s' in search path\n"), var);
if (end)
/* Restore the path separator. */
* end = '/';
/* PR 20784: Make sure that we resume the scan *after*
the token that we could not replace. */
offset = (var + 1) - filename;
}
free (freeme);
}
needed.name = filename;
if (ldelf_try_needed (&needed, force, is_linux))
return TRUE;
free (filename);
if (*s == '\0')
break;
path = s + 1;
}
return FALSE;
}
/* Prefix the sysroot to absolute paths in PATH, a string containing
paths separated by config.rpath_separator. If running on a DOS
file system, paths containing a drive spec won't have the sysroot
prefix added, unless the sysroot also specifies the same drive. */
static const char *
ldelf_add_sysroot (const char *path)
{
size_t len, extra;
const char *p;
char *ret, *q;
int dos_drive_sysroot = HAS_DRIVE_SPEC (ld_sysroot);
len = strlen (ld_sysroot);
for (extra = 0, p = path; ; )
{
int dos_drive = HAS_DRIVE_SPEC (p);
if (dos_drive)
p += 2;
if (IS_DIR_SEPARATOR (*p)
&& (!dos_drive
|| (dos_drive_sysroot
&& ld_sysroot[0] == p[-2])))
{
if (dos_drive && dos_drive_sysroot)
extra += len - 2;
else
extra += len;
}
p = strchr (p, config.rpath_separator);
if (!p)
break;
++p;
}
ret = xmalloc (strlen (path) + extra + 1);
for (q = ret, p = path; ; )
{
const char *end;
int dos_drive = HAS_DRIVE_SPEC (p);
if (dos_drive)
{
*q++ = *p++;
*q++ = *p++;
}
if (IS_DIR_SEPARATOR (*p)
&& (!dos_drive
|| (dos_drive_sysroot
&& ld_sysroot[0] == p[-2])))
{
if (dos_drive && dos_drive_sysroot)
{
strcpy (q, ld_sysroot + 2);
q += len - 2;
}
else
{
strcpy (q, ld_sysroot);
q += len;
}
}
end = strchr (p, config.rpath_separator);
if (end)
{
size_t n = end - p + 1;
strncpy (q, p, n);
q += n;
p += n;
}
else
{
strcpy (q, p);
break;
}
}
return ret;
}
/* Read the system search path the FreeBSD way rather than the Linux way. */
#ifdef HAVE_ELF_HINTS_H
#include <elf-hints.h>
#else
#include "elf-hints-local.h"
#endif
static bfd_boolean
ldelf_check_ld_elf_hints (const struct bfd_link_needed_list *l, int force,
int elfsize)
{
static bfd_boolean initialized;
static const char *ld_elf_hints;
struct dt_needed needed;
if (!initialized)
{
FILE *f;
char *tmppath;
tmppath = concat (ld_sysroot, _PATH_ELF_HINTS, (const char *) NULL);
f = fopen (tmppath, FOPEN_RB);
free (tmppath);
if (f != NULL)
{
struct elfhints_hdr hdr;
if (fread (&hdr, 1, sizeof (hdr), f) == sizeof (hdr)
&& hdr.magic == ELFHINTS_MAGIC
&& hdr.version == 1)
{
if (fseek (f, hdr.strtab + hdr.dirlist, SEEK_SET) != -1)
{
char *b;
b = xmalloc (hdr.dirlistlen + 1);
if (fread (b, 1, hdr.dirlistlen + 1, f) ==
hdr.dirlistlen + 1)
ld_elf_hints = ldelf_add_sysroot (b);
free (b);
}
}
fclose (f);
}
initialized = TRUE;
}
if (ld_elf_hints == NULL)
return FALSE;
needed.by = l->by;
needed.name = l->name;
return ldelf_search_needed (ld_elf_hints, &needed, force, FALSE, elfsize);
}
/* For a native linker, check the file /etc/ld.so.conf for directories
in which we may find shared libraries. /etc/ld.so.conf is really
only meaningful on Linux. */
struct ldelf_ld_so_conf
{
char *path;
size_t len, alloc;
};
static bfd_boolean
ldelf_parse_ld_so_conf (struct ldelf_ld_so_conf *, const char *);
static void
ldelf_parse_ld_so_conf_include (struct ldelf_ld_so_conf *info,
const char *filename,
const char *pattern)
{
char *newp = NULL;
#ifdef HAVE_GLOB
glob_t gl;
#endif
if (pattern[0] != '/')
{
char *p = strrchr (filename, '/');
size_t patlen = strlen (pattern) + 1;
newp = xmalloc (p - filename + 1 + patlen);
memcpy (newp, filename, p - filename + 1);
memcpy (newp + (p - filename + 1), pattern, patlen);
pattern = newp;
}
#ifdef HAVE_GLOB
if (glob (pattern, 0, NULL, &gl) == 0)
{
size_t i;
for (i = 0; i < gl.gl_pathc; ++i)
ldelf_parse_ld_so_conf (info, gl.gl_pathv[i]);
globfree (&gl);
}
#else
/* If we do not have glob, treat the pattern as a literal filename. */
ldelf_parse_ld_so_conf (info, pattern);
#endif
if (newp)
free (newp);
}
static bfd_boolean
ldelf_parse_ld_so_conf (struct ldelf_ld_so_conf *info, const char *filename)
{
FILE *f = fopen (filename, FOPEN_RT);
char *line;
size_t linelen;
if (f == NULL)
return FALSE;
linelen = 256;
line = xmalloc (linelen);
do
{
char *p = line, *q;
/* Normally this would use getline(3), but we need to be portable. */
while ((q = fgets (p, linelen - (p - line), f)) != NULL
&& strlen (q) == linelen - (p - line) - 1
&& line[linelen - 2] != '\n')
{
line = xrealloc (line, 2 * linelen);
p = line + linelen - 1;
linelen += linelen;
}
if (q == NULL && p == line)
break;
p = strchr (line, '\n');
if (p)
*p = '\0';
/* Because the file format does not know any form of quoting we
can search forward for the next '#' character and if found
make it terminating the line. */
p = strchr (line, '#');
if (p)
*p = '\0';
/* Remove leading whitespace. NUL is no whitespace character. */
p = line;
while (*p == ' ' || *p == '\f' || *p == '\r' || *p == '\t' || *p == '\v')
++p;
/* If the line is blank it is ignored. */
if (p[0] == '\0')
continue;
if (CONST_STRNEQ (p, "include") && (p[7] == ' ' || p[7] == '\t'))
{
char *dir, c;
p += 8;
do
{
while (*p == ' ' || *p == '\t')
++p;
if (*p == '\0')
break;
dir = p;
while (*p != ' ' && *p != '\t' && *p)
++p;
c = *p;
*p++ = '\0';
if (dir[0] != '\0')
ldelf_parse_ld_so_conf_include (info, filename, dir);
}
while (c != '\0');
}
else
{
char *dir = p;
while (*p && *p != '=' && *p != ' ' && *p != '\t' && *p != '\f'
&& *p != '\r' && *p != '\v')
++p;
while (p != dir && p[-1] == '/')
--p;
if (info->path == NULL)
{
info->alloc = p - dir + 1 + 256;
info->path = xmalloc (info->alloc);
info->len = 0;
}
else
{
if (info->len + 1 + (p - dir) >= info->alloc)
{
info->alloc += p - dir + 256;
info->path = xrealloc (info->path, info->alloc);
}
info->path[info->len++] = config.rpath_separator;
}
memcpy (info->path + info->len, dir, p - dir);
info->len += p - dir;
info->path[info->len] = '\0';
}
}
while (! feof (f));
free (line);
fclose (f);
return TRUE;
}
static bfd_boolean
ldelf_check_ld_so_conf (const struct bfd_link_needed_list *l, int force,
int elfsize)
{
static bfd_boolean initialized;
static const char *ld_so_conf;
struct dt_needed needed;
if (! initialized)
{
char *tmppath;
struct ldelf_ld_so_conf info;
info.path = NULL;
info.len = info.alloc = 0;
tmppath = concat (ld_sysroot, "${prefix}/etc/ld.so.conf",
(const char *) NULL);
if (!ldelf_parse_ld_so_conf (&info, tmppath))
{
free (tmppath);
tmppath = concat (ld_sysroot, "/etc/ld.so.conf",
(const char *) NULL);
ldelf_parse_ld_so_conf (&info, tmppath);
}
free (tmppath);
if (info.path)
{
ld_so_conf = ldelf_add_sysroot (info.path);
free (info.path);
}
initialized = TRUE;
}
if (ld_so_conf == NULL)
return FALSE;
needed.by = l->by;
needed.name = l->name;
return ldelf_search_needed (ld_so_conf, &needed, force, TRUE, elfsize);
}
/* See if an input file matches a DT_NEEDED entry by name. */
static void
ldelf_check_needed (lang_input_statement_type *s)
{
const char *soname;
/* Stop looking if we've found a loaded lib. */
if (global_found != NULL
&& (bfd_elf_get_dyn_lib_class (global_found->the_bfd)
& DYN_AS_NEEDED) == 0)
return;
if (s->filename == NULL || s->the_bfd == NULL)
return;
/* Don't look for a second non-loaded as-needed lib. */
if (global_found != NULL
&& (bfd_elf_get_dyn_lib_class (s->the_bfd) & DYN_AS_NEEDED) != 0)
return;
if (filename_cmp (s->filename, global_needed->name) == 0)
{
global_found = s;
return;
}
if (s->flags.search_dirs)
{
const char *f = strrchr (s->filename, '/');
if (f != NULL
&& filename_cmp (f + 1, global_needed->name) == 0)
{
global_found = s;
return;
}
}
soname = bfd_elf_get_dt_soname (s->the_bfd);
if (soname != NULL
&& filename_cmp (soname, global_needed->name) == 0)
{
global_found = s;
return;
}
}
/* This is called after all the input files have been opened. */
void
ldelf_after_open (int use_libpath, int native, int is_linux, int is_freebsd,
int elfsize)
{
struct bfd_link_needed_list *needed, *l;
struct elf_link_hash_table *htab;
asection *s;
bfd *abfd;
after_open_default ();
htab = elf_hash_table (&link_info);
if (!is_elf_hash_table (htab))
return;