Skip to content

Commit 0ad3a11

Browse files
committed
Work around linkage bug cross-compiling from x86_64-apple-darwin to i686-apple-darwin
The correct opendir/readdir to use appear to be the 64-bit versions called opendir$INODE64, etc. but for some reason I can't get them to link properly on i686. Putting them in librustrt and making gcc figure it out works. This mystery will have to wait for another day.
1 parent b60c3bf commit 0ad3a11

File tree

4 files changed

+48
-37
lines changed

4 files changed

+48
-37
lines changed

src/libcore/libc.rs

Lines changed: 18 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1385,48 +1385,30 @@ pub mod funcs {
13851385
use libc::types::common::posix88::{DIR, dirent_t};
13861386
use libc::types::os::arch::c95::{c_char, c_int, c_long};
13871387

1388-
pub extern {
1389-
// default bindings for opendir and readdir in
1390-
// non-macos unix
1391-
#[cfg(target_os = "linux")]
1392-
#[cfg(target_os = "android")]
1393-
#[cfg(target_os = "freebsd")]
1394-
unsafe fn opendir(dirname: *c_char) -> *DIR;
1395-
#[cfg(target_os = "linux")]
1396-
#[cfg(target_os = "android")]
1397-
#[cfg(target_os = "freebsd")]
1398-
unsafe fn readdir(dirp: *DIR) -> *dirent_t;
1388+
// NOTE: On OS X opendir and readdir have two versions,
1389+
// one for 32-bit kernelspace and one for 64.
1390+
// We should be linking to the 64-bit ones, called
1391+
// opendir$INODE64, etc. but for some reason rustc
1392+
// doesn't link it correctly on i686, so we're going
1393+
// through a C function that mysteriously does work.
1394+
pub unsafe fn opendir(dirname: *c_char) -> *DIR {
1395+
rust_opendir(dirname)
1396+
}
1397+
pub unsafe fn readdir(dirp: *DIR) -> *dirent_t {
1398+
rust_readdir(dirp)
1399+
}
1400+
1401+
extern {
1402+
unsafe fn rust_opendir(dirname: *c_char) -> *DIR;
1403+
unsafe fn rust_readdir(dirp: *DIR) -> *dirent_t;
1404+
}
13991405

1406+
pub extern {
14001407
unsafe fn closedir(dirp: *DIR) -> c_int;
14011408
unsafe fn rewinddir(dirp: *DIR);
14021409
unsafe fn seekdir(dirp: *DIR, loc: c_long);
14031410
unsafe fn telldir(dirp: *DIR) -> c_long;
14041411
}
1405-
1406-
#[cfg(target_word_size = "64")]
1407-
pub extern {
1408-
// on OSX (particularly when running with a
1409-
// 64bit kernel), we have an issue where there
1410-
// are separate bindings for opendir and readdir,
1411-
// which we have to explicitly link, as below.
1412-
#[cfg(target_os = "macos")]
1413-
#[link_name = "opendir$INODE64"]
1414-
unsafe fn opendir(dirname: *c_char) -> *DIR;
1415-
#[cfg(target_os = "macos")]
1416-
#[link_name = "readdir$INODE64"]
1417-
unsafe fn readdir(dirp: *DIR) -> *dirent_t;
1418-
}
1419-
#[cfg(target_word_size = "32")]
1420-
pub extern {
1421-
// on OSX (particularly when running with a
1422-
// 64bit kernel), we have an issue where there
1423-
// are separate bindings for opendir and readdir,
1424-
// which we have to explicitly link, as below.
1425-
#[cfg(target_os = "macos")]
1426-
unsafe fn opendir(dirname: *c_char) -> *DIR;
1427-
#[cfg(target_os = "macos")]
1428-
unsafe fn readdir(dirp: *DIR) -> *dirent_t;
1429-
}
14301412
}
14311413

14321414
#[nolink]

src/libuv

Submodule libuv updated 1 file

src/rt/rust_builtin.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -818,6 +818,33 @@ rust_dec_kernel_live_count() {
818818
task->kernel->dec_live_count();
819819
}
820820

821+
#ifndef _WIN32
822+
#include <sys/types.h>
823+
#include <dirent.h>
824+
825+
extern "C" DIR*
826+
rust_opendir(char *dirname) {
827+
return opendir(dirname);
828+
}
829+
830+
extern "C" dirent*
831+
rust_readdir(DIR *dirp) {
832+
return readdir(dirp);
833+
}
834+
835+
#else
836+
837+
extern "C" void
838+
rust_opendir() {
839+
}
840+
841+
extern "C" void
842+
rust_readdir() {
843+
}
844+
845+
#endif
846+
847+
821848
//
822849
// Local Variables:
823850
// mode: C++

src/rt/rustrt.def.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,3 +194,5 @@ rust_dec_kernel_live_count
194194
rust_get_exchange_count_ptr
195195
rust_get_sched_tls_key
196196
swap_registers
197+
rust_readdir
198+
rust_opendir

0 commit comments

Comments
 (0)