@@ -539,15 +539,29 @@ int is_library_for_architecture(const char* lib_path, uint16_t arch)
539
539
return FALSE;
540
540
}
541
541
542
+ int is_usable_library (const char * candidate_library , uint16_t desired_architecture )
543
+ {
544
+ if (access (candidate_library , F_OK ) == 0 &&
545
+ is_library_for_architecture (candidate_library , desired_architecture ) == TRUE) {
546
+ void * ret_handle = dlopen (candidate_library , RTLD_LAZY );
547
+ if (ret_handle == NULL ) {
548
+ return FALSE;
549
+ }
550
+ dlclose (ret_handle );
551
+
552
+ return TRUE;
553
+ }
554
+ return FALSE;
555
+ }
556
+
542
557
int find_xlib_by_arch (const char * possible_locations [],
543
558
int locations_length , uint16_t desired_architecture )
544
559
{
545
560
int i ;
546
561
for (i = 0 ; i < locations_length ; i ++ ) {
547
562
const char * possible_location = possible_locations [i ];
548
563
549
- if (access (possible_location , F_OK ) == 0 &&
550
- is_library_for_architecture (possible_location , desired_architecture ) == TRUE)
564
+ if (is_usable_library (possible_location , desired_architecture ))
551
565
{
552
566
return i ;
553
567
}
@@ -556,6 +570,34 @@ int find_xlib_by_arch(const char* possible_locations[],
556
570
return -1 ;
557
571
}
558
572
573
+
574
+ int find_xlib_by_env (char * library , uint16_t desired_architecture )
575
+ {
576
+ char * ld_env = getenv ("LD_LIBRARY_PATH" );
577
+ if (ld_env == 0 ) {
578
+ return FALSE;
579
+ }
580
+
581
+ char * ld_to_parse = strdup (ld_env );
582
+
583
+ int found_library = FALSE;
584
+ char * t = strtok (ld_to_parse , ":" );
585
+ char potential_library [MAX_LIBRARY_PATH + 1 ];
586
+
587
+ while ((t != NULL ) && (!found_library )) {
588
+ snprintf (potential_library , MAX_LIBRARY_PATH , "%s/libX11.so.6" , t );
589
+ if (is_usable_library (potential_library , desired_architecture )) {
590
+ strcpy (library , potential_library );
591
+ fprintf (stderr , "Found libX11.so.6 at: %s\n" , potential_library );
592
+ found_library = TRUE;
593
+ }
594
+ t = strtok (NULL , ":" );
595
+ }
596
+
597
+ free (ld_to_parse );
598
+ return found_library ;
599
+ }
600
+
559
601
void * get_xlib_handle ()
560
602
{
561
603
void * ret_handle = NULL ;
@@ -577,22 +619,27 @@ void* get_xlib_handle()
577
619
required_lib_arch = EM_X86_64 ;
578
620
}
579
621
int suitable_xlib_index = find_xlib_by_arch (possible_locations , locations_len , required_lib_arch );
580
- if (suitable_xlib_index < 0 ) {
622
+ int found_library = FALSE;
623
+ if (suitable_xlib_index >= 0 ) {
624
+ snprintf (library , MAX_LIBRARY_PATH , "%s" , possible_locations [suitable_xlib_index ]);
625
+ found_library = TRUE;
626
+ } else {
627
+ found_library = find_xlib_by_env (library , required_lib_arch );
628
+ }
629
+ if (found_library == FALSE) {
581
630
const char * desired_arch = (required_lib_arch == EM_386 ? "32-bit" : "64-bit" );
582
- fprintf (stderr , "None of the following is a %s version of Xlib:" , desired_arch );
631
+ fprintf (stderr , "None of the following is a %s version of Xlib:" , desired_arch );
583
632
int i ;
584
633
for (i = 0 ; i < locations_len ; i ++ ) {
585
- fprintf (stderr , " %s\n" , possible_locations [i ]);
634
+ fprintf (stderr , " %s\n" , possible_locations [i ]);
586
635
}
587
636
return NULL ;
588
637
}
589
638
590
- snprintf (library , MAX_LIBRARY_PATH , possible_locations [suitable_xlib_index ]);
591
-
592
- ret_handle = dlopen (library , RTLD_LAZY );
639
+ ret_handle = dlopen (library , RTLD_LAZY );
593
640
if (ret_handle == NULL ) {
594
641
fprintf (stderr , "Failed to dlopen %s\n" , library );
595
- fprintf (stderr , "dlerror says: %s\n" , dlerror ());
642
+ fprintf (stderr , "dlerror says: %s\n" , dlerror ());
596
643
}
597
644
598
645
return ret_handle ;
0 commit comments