@@ -514,6 +514,29 @@ static int path_find(const char *name, const char *s, char *buf, size_t buf_size
514
514
}
515
515
}
516
516
517
+ // Resolve filename using LD_LIBRARY_PATH
518
+ static const char * resolve_path (char * buf , const char * file , size_t buflen ) {
519
+ if (!strchr (file , '/' )) {
520
+ const char * env_path = getenv ("LD_LIBRARY_PATH" );
521
+ if (env_path && path_find (file , env_path , buf , buflen ) == 0 ) {
522
+ dbg ("dlopen: found in LD_LIBRARY_PATH: %s" , buf );
523
+ return buf ;
524
+ }
525
+ }
526
+ return file ;
527
+ }
528
+
529
+ // Search for library name to see if it's already loaded
530
+ static struct dso * find_existing (const char * file ) {
531
+ for (struct dlevent * e = head ; e ; e = e -> next ) {
532
+ if (e -> sym_index == -1 && !strcmp (e -> dso -> name , file )) {
533
+ dbg ("dlopen: already opened: %p" , e -> dso );
534
+ return e -> dso ;
535
+ }
536
+ }
537
+ return NULL ;
538
+ }
539
+
517
540
// Internal version of dlopen with typed return value.
518
541
// Without this, the compiler won't tell us if we have the wrong return type.
519
542
static struct dso * _dlopen (const char * file , int flags ) {
@@ -533,25 +556,12 @@ static struct dso* _dlopen(const char* file, int flags) {
533
556
dlsync ();
534
557
#endif
535
558
536
- struct dso * p ;
537
-
538
- /* Resolve filename using LD_LIBRARY_PATH */
539
559
char buf [2 * NAME_MAX + 2 ];
540
- if (!strchr (file , '/' )) {
541
- const char * env_path = getenv ("LD_LIBRARY_PATH" );
542
- if (env_path && path_find (file , env_path , buf , sizeof buf ) == 0 ) {
543
- dbg ("dlopen: found in LD_LIBRARY_PATH: %s" , buf );
544
- file = buf ;
545
- }
546
- }
560
+ file = resolve_path (buf , file , sizeof buf );
547
561
548
- /* Search for the name to see if it's already loaded */
549
- for (struct dlevent * e = head ; e ; e = e -> next ) {
550
- if (e -> sym_index == -1 && !strcmp (e -> dso -> name , file )) {
551
- dbg ("dlopen: already opened: %p" , e -> dso );
552
- p = e -> dso ;
553
- goto end ;
554
- }
562
+ struct dso * p = find_existing (file );
563
+ if (p ) {
564
+ goto end ;
555
565
}
556
566
557
567
p = load_library_start (file , flags );
@@ -580,16 +590,24 @@ void* dlopen(const char* file, int flags) {
580
590
581
591
void emscripten_dlopen (const char * filename , int flags , void * user_data ,
582
592
em_dlopen_callback onsuccess , em_arg_callback_func onerror ) {
593
+ dbg ("emscripten_dlopen: %s" , filename );
583
594
if (!filename ) {
584
- onsuccess (user_data , head );
595
+ onsuccess (user_data , head -> dso );
585
596
return ;
586
597
}
587
598
do_write_lock ();
588
599
#ifdef _REENTRANT
589
600
// Make sure we are in sync before performing any write operations.
590
601
dlsync ();
591
602
#endif
592
- struct dso * p = load_library_start (filename , flags );
603
+ char buf [2 * NAME_MAX + 2 ];
604
+ filename = resolve_path (buf , filename , sizeof buf );
605
+ struct dso * p = find_existing (filename );
606
+ if (p ) {
607
+ onsuccess (user_data , p );
608
+ return ;
609
+ }
610
+ p = load_library_start (filename , flags );
593
611
if (!p ) {
594
612
do_write_unlock ();
595
613
onerror (user_data );
0 commit comments