From 9b0c74b9350e72f114f38b0e353de1f073218552 Mon Sep 17 00:00:00 2001
From: Jeremy Soller <jeremy@system76.com>
Date: Sun, 18 Nov 2018 10:35:33 -0700
Subject: [PATCH] Fix ioctl being defined on redox, add cxa_atexit for gcc

---
 src/cxa.rs                         | 28 ++++++++++++++++++++++++++++
 src/header/sys_ioctl/cbindgen.toml |  4 ++++
 src/header/sys_ioctl/mod.rs        | 18 +++++++++---------
 src/lib.rs                         |  1 +
 4 files changed, 42 insertions(+), 9 deletions(-)
 create mode 100644 src/cxa.rs

diff --git a/src/cxa.rs b/src/cxa.rs
new file mode 100644
index 00000000..621f5c7a
--- /dev/null
+++ b/src/cxa.rs
@@ -0,0 +1,28 @@
+use platform::types::*;
+
+#[derive(Clone, Copy)]
+struct CxaAtExitFunc {
+    func: extern "C" fn (*mut c_void),
+    arg: *mut c_void,
+    dso: *mut c_void
+}
+
+static mut CXA_ATEXIT_FUNCS: [Option<CxaAtExitFunc>; 32] = [None; 32];
+
+#[no_mangle]
+pub unsafe extern "C" fn __cxa_atexit (func_opt: Option<extern "C" fn (*mut c_void)>, arg: *mut c_void, dso: *mut c_void) -> c_int {
+    for i in 0..CXA_ATEXIT_FUNCS.len() {
+        if CXA_ATEXIT_FUNCS[i].is_none() {
+            CXA_ATEXIT_FUNCS[i] = func_opt.map(|func| CxaAtExitFunc {
+                func,
+                arg,
+                dso
+            });
+            return 0;
+        }
+    }
+
+    -1
+}
+
+// TODO: cxa_finalize
diff --git a/src/header/sys_ioctl/cbindgen.toml b/src/header/sys_ioctl/cbindgen.toml
index 65ae4ea0..40b2c3fb 100644
--- a/src/header/sys_ioctl/cbindgen.toml
+++ b/src/header/sys_ioctl/cbindgen.toml
@@ -5,6 +5,10 @@ language = "C"
 # sgtty is used by another header, and cbindgen doesn't prefix that with `struct` :|
 style = "Both"
 
+[defines]
+"target_os=linux" = "__linux__"
+"target_os=redox" = "__redox__"
+
 [enum]
 prefix_with_name = true
 
diff --git a/src/header/sys_ioctl/mod.rs b/src/header/sys_ioctl/mod.rs
index bf3b8d69..12ca0a4c 100644
--- a/src/header/sys_ioctl/mod.rs
+++ b/src/header/sys_ioctl/mod.rs
@@ -12,20 +12,20 @@ pub struct sgttyb {
     sg_flags: c_ushort,
 }
 
-#[repr(C)]
-#[derive(Default)]
-pub struct winsize {
-    ws_row: c_ushort,
-    ws_col: c_ushort,
-    ws_xpixel: c_ushort,
-    ws_ypixel: c_ushort,
-}
-
 #[cfg(target_os = "linux")]
 pub mod inner {
     use platform::types::*;
     use platform::Sys;
 
+    #[repr(C)]
+    #[derive(Default)]
+    pub struct winsize {
+        ws_row: c_ushort,
+        ws_col: c_ushort,
+        ws_xpixel: c_ushort,
+        ws_ypixel: c_ushort,
+    }
+
     #[no_mangle]
     pub extern "C" fn ioctl(fd: c_int, request: c_ulong, out: *mut c_void) -> c_int {
         // TODO: Somehow support varargs to syscall??
diff --git a/src/lib.rs b/src/lib.rs
index a9f19ab2..33f107ac 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -36,6 +36,7 @@ extern crate spin;
 #[macro_use]
 mod macros;
 pub mod c_str;
+pub mod cxa;
 pub mod fs;
 pub mod header;
 pub mod io;
-- 
GitLab